PageRenderTime 46ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/BlogEngine/BlogEngine.NET/App_Code/Extensions/TypePadFilter.cs

#
C# | 262 lines | 169 code | 45 blank | 48 comment | 12 complexity | c68e4d51834cc3c5233eccc339b670a8 MD5 | raw file
Possible License(s): LGPL-2.1, Apache-2.0, BSD-3-Clause
  1. namespace App_Code.Extensions
  2. {
  3. using BlogEngine.Core;
  4. using BlogEngine.Core.Web.Controls;
  5. using BlogEngine.Core.Web.Extensions;
  6. using Joel.Net;
  7. using System.Collections.Generic;
  8. using System;
  9. /// <summary>
  10. /// The type pad filter.
  11. /// </summary>
  12. [Extension("TypePad anti-spam comment filter (based on AkismetFilter)", "1.0",
  13. "<a href=\"http://lucsiferre.net\">By Chris Nicola</a>")]
  14. public class TypePadFilter : ICustomFilter
  15. {
  16. #region Constants and Fields
  17. /// <summary>
  18. /// The sync root.
  19. /// </summary>
  20. private static readonly object syncRoot = new object();
  21. /// <summary>
  22. /// The Akismet api.
  23. /// </summary>
  24. private static Dictionary<Guid, Akismet> blogsApi = new Dictionary<Guid, Akismet>();
  25. /// <summary>
  26. /// The fall through.
  27. /// </summary>
  28. private bool fallThrough = true;
  29. /// <summary>
  30. /// The TypePad key.
  31. /// </summary>
  32. private static Dictionary<Guid, string> blogsKey = new Dictionary<Guid, string>();
  33. /// <summary>
  34. /// The settings.
  35. /// </summary>
  36. private static Dictionary<Guid, ExtensionSettings> blogsSettings = new Dictionary<Guid, ExtensionSettings>();
  37. /// <summary>
  38. /// The TypePad site.
  39. /// </summary>
  40. private static Dictionary<Guid, string> blogsSite = new Dictionary<Guid, string>();
  41. #endregion
  42. #region Constructors and Destructors
  43. /// <summary>
  44. /// Initializes a new instance of the <see cref="TypePadFilter"/> class.
  45. /// </summary>
  46. public TypePadFilter()
  47. {
  48. InitSettings();
  49. }
  50. #endregion
  51. #region Properties
  52. private static Akismet Api
  53. {
  54. get
  55. {
  56. Akismet akismet = null;
  57. blogsApi.TryGetValue(Blog.CurrentInstance.Id, out akismet);
  58. return akismet;
  59. }
  60. set
  61. {
  62. blogsApi[Blog.CurrentInstance.Id] = value;
  63. }
  64. }
  65. private static string Key
  66. {
  67. get
  68. {
  69. string key = null;
  70. blogsKey.TryGetValue(Blog.CurrentInstance.Id, out key);
  71. return key;
  72. }
  73. set
  74. {
  75. blogsKey[Blog.CurrentInstance.Id] = value;
  76. }
  77. }
  78. private static string Site
  79. {
  80. get
  81. {
  82. string key = null;
  83. blogsSite.TryGetValue(Blog.CurrentInstance.Id, out key);
  84. return key;
  85. }
  86. set
  87. {
  88. blogsSite[Blog.CurrentInstance.Id] = value;
  89. }
  90. }
  91. private static ExtensionSettings Settings
  92. {
  93. get
  94. {
  95. Guid blogId = Blog.CurrentInstance.Id;
  96. ExtensionSettings settings = null;
  97. blogsSettings.TryGetValue(blogId, out settings);
  98. if (settings == null)
  99. {
  100. lock (syncRoot)
  101. {
  102. blogsSettings.TryGetValue(blogId, out settings);
  103. if (settings == null)
  104. {
  105. var extensionSettings = new ExtensionSettings("TypePadFilter") { IsScalar = true };
  106. extensionSettings.AddParameter("SiteURL", "Site URL");
  107. extensionSettings.AddParameter("ApiKey", "API Key");
  108. extensionSettings.AddValue("SiteURL", "http://example.com/blog");
  109. extensionSettings.AddValue("ApiKey", "123456789");
  110. blogsSettings[blogId] = ExtensionManager.InitSettings("TypePadFilter", extensionSettings);
  111. ExtensionManager.SetStatus("TypePadFilter", false);
  112. }
  113. }
  114. }
  115. return settings;
  116. }
  117. }
  118. /// <summary>
  119. /// Gets a value indicating whether FallThrough.
  120. /// </summary>
  121. public bool FallThrough
  122. {
  123. get
  124. {
  125. return fallThrough;
  126. }
  127. }
  128. #endregion
  129. #region Implemented Interfaces
  130. #region ICustomFilter
  131. /// <summary>
  132. /// Check if comment is spam
  133. /// </summary>
  134. /// <param name="comment">BlogEngine comment</param>
  135. /// <returns>True if comment is spam</returns>
  136. public bool Check(Comment comment)
  137. {
  138. if (Api == null)
  139. {
  140. this.Initialize();
  141. }
  142. var typePadComment = GetAkismetComment(comment);
  143. var isspam = Api.CommentCheck(typePadComment);
  144. fallThrough = !isspam;
  145. return isspam;
  146. }
  147. /// <summary>
  148. /// Initializes anti-spam service
  149. /// </summary>
  150. /// <returns>
  151. /// True if service online and credentials validated
  152. /// </returns>
  153. public bool Initialize()
  154. {
  155. if (!ExtensionManager.ExtensionEnabled("TypePadFilter"))
  156. {
  157. return false;
  158. }
  159. Site = Settings.GetSingleValue("SiteURL");
  160. Key = Settings.GetSingleValue("ApiKey");
  161. Api = new Akismet(Key, Site, "BlogEngine.NET 1.5", "api.antispam.typepad.com");
  162. return Api.VerifyKey();
  163. }
  164. /// <summary>
  165. /// Report mistakes back to service
  166. /// </summary>
  167. /// <param name="comment">BlogEngine comment</param>
  168. public void Report(Comment comment)
  169. {
  170. if (Api == null)
  171. {
  172. this.Initialize();
  173. }
  174. var akismetComment = GetAkismetComment(comment);
  175. if (comment.IsApproved)
  176. {
  177. Utils.Log(string.Format("TypePad: Reporting NOT spam from \"{0}\" at \"{1}\"", comment.Author, comment.IP));
  178. Api.SubmitHam(akismetComment);
  179. }
  180. else
  181. {
  182. Utils.Log(string.Format("TypePad: Reporting SPAM from \"{0}\" at \"{1}\"", comment.Author, comment.IP));
  183. Api.SubmitSpam(akismetComment);
  184. }
  185. }
  186. #endregion
  187. #endregion
  188. #region Methods
  189. /// <summary>
  190. /// Gets the akismet comment.
  191. /// </summary>
  192. /// <param name="comment">The comment.</param>
  193. /// <returns>An Akismet Comment.</returns>
  194. private static AkismetComment GetAkismetComment(Comment comment)
  195. {
  196. var akismetComment = new AkismetComment
  197. {
  198. Blog = Settings.GetSingleValue("SiteURL"),
  199. UserIp = comment.IP,
  200. CommentContent = comment.Content,
  201. CommentAuthor = comment.Author,
  202. CommentAuthorEmail = comment.Email
  203. };
  204. if (comment.Website != null)
  205. {
  206. akismetComment.CommentAuthorUrl = comment.Website.OriginalString;
  207. }
  208. return akismetComment;
  209. }
  210. public void InitSettings()
  211. {
  212. // call Settings getter so default settings are loaded on application start.
  213. var s = Settings;
  214. }
  215. #endregion
  216. }
  217. }