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