PageRenderTime 39ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/BlogEngine/BlogEngine.NET/App_Code/Extensions/StopForumSpam.cs

#
C# | 115 lines | 58 code | 20 blank | 37 comment | 2 complexity | a364921498b9221e9556dc9e1134559e MD5 | raw file
Possible License(s): LGPL-2.1, Apache-2.0, BSD-3-Clause
  1. namespace App_Code.Extensions
  2. {
  3. using System;
  4. using System.IO;
  5. using System.Net;
  6. using BlogEngine.Core;
  7. /// <summary>
  8. /// StopForumSpam.com custom comment filter
  9. /// </summary>
  10. public class StopForumSpam : ICustomFilter
  11. {
  12. #region Constants and Fields
  13. /// <summary>
  14. /// The pass through.
  15. /// </summary>
  16. private bool passThrough = true;
  17. #endregion
  18. #region Properties
  19. /// <summary>
  20. /// Gets a value indicating whether comment will be passed to other
  21. /// custom filters for validation
  22. /// </summary>
  23. public bool FallThrough
  24. {
  25. get
  26. {
  27. return passThrough;
  28. }
  29. }
  30. #endregion
  31. #region Implemented Interfaces
  32. #region ICustomFilter
  33. /// <summary>
  34. /// Check if comment is spam
  35. /// </summary>
  36. /// <param name="comment">
  37. /// The comment
  38. /// </param>
  39. /// <returns>
  40. /// True if comment is spam
  41. /// </returns>
  42. public bool Check(Comment comment)
  43. {
  44. try
  45. {
  46. var url = string.Format("http://www.stopforumspam.com/api?ip={0}", comment.IP);
  47. var request = (HttpWebRequest)WebRequest.Create(url);
  48. var response = (HttpWebResponse)request.GetResponse();
  49. var responseStream = response.GetResponseStream();
  50. if (responseStream != null)
  51. {
  52. var reader = new StreamReader(responseStream);
  53. var value = reader.ReadToEnd();
  54. reader.Close();
  55. var spam = value.ToLowerInvariant().Contains("<appears>yes</appears>") ? true : false;
  56. // if comment IP appears in the stopforumspam list
  57. // it is for sure spam; no need to pass to others.
  58. passThrough = spam ? false : true;
  59. return spam;
  60. }
  61. return false;
  62. }
  63. catch (Exception e)
  64. {
  65. Utils.Log(string.Format("Error checking stopforumspam.com: {0}", e.Message));
  66. return false;
  67. }
  68. }
  69. /// <summary>
  70. /// Enables or disables filter
  71. /// </summary>
  72. /// <returns>
  73. /// True of false
  74. /// </returns>
  75. public bool Initialize()
  76. {
  77. // do not need any initialization
  78. // simply return true to enable filter
  79. return true;
  80. }
  81. /// <summary>
  82. /// Report mistakes to service
  83. /// </summary>
  84. /// <param name="comment">
  85. /// A Comment.
  86. /// </param>
  87. public void Report(Comment comment)
  88. {
  89. // if we needed report mistakes back to
  90. // service, we would put code here
  91. }
  92. #endregion
  93. #endregion
  94. }
  95. }