Joel.Net.Akismet.1.0.1 BlogEngine.NET
Produced: 5/14/2009 11:49:10 PM
   
Mode:  Differences, With Context, Ignoring Unimportant  
Left base folder: E:\CodeOptimism\Joel.Net.Akismet.1.0.1 original  
Right base folder: E:\CodeOptimism\Joel.Net.Akismet.1.0.1  
   
File: Akismet.cs  
7 *              Console.WriteLine's, which are not desireable on a website. = 7 *              Console.WriteLine's, which are not desireable on a website.
8 */   8 */
9 using System;   9 using System;
10 using System.Net;   10 using System.Net;
11 using System.Web;   11 using System.Web;
12 using System.IO;   12 using System.IO;
    -+ 13 using BlogEngine.Core;
13   = 14  
14 namespace Joel.Net {   15 namespace Joel.Net {
15        
16     #region - public class AkismetComment - +-    
17     public class AkismetComment {      
18         public string Blog = null;      
19         public string UserIp = null;      
20         public string UserAgent = null;      
21         public string Referrer = null;      
22         public string Permalink = null;      
23         public string CommentType = null;      
24         public string CommentAuthor = null;      
25         public string CommentAuthorEmail = null;      
26         public string CommentAuthorUrl = null;      
27         public string CommentContent = null;      
28     }      
29     #endregion      
30   =    
31     public class Akismet {   16     public class Akismet {
32         const string verifyUrl = "http://rest.akismet.com/1.1/verify-key";   17         const string verifyUrl = "http://rest.akismet.com/1.1/verify-key";
33         const string commentCheckUrl = "http://{0}.rest.akismet.com/1.1/comment-check";   18         const string commentCheckUrl = "http://{0}.rest.akismet.com/1.1/comment-check";
34         const string submitSpamUrl = "http://{0}.rest.akismet.com/1.1/submit-spam";   19         const string submitSpamUrl = "http://{0}.rest.akismet.com/1.1/submit-spam";
35         const string submitHamUrl = "http://{0}.rest.akismet.com/1.1/submit-ham";   20         const string submitHamUrl = "http://{0}.rest.akismet.com/1.1/submit-ham";
 
66             return value; = 51             return value;
67         }   52         }
68     53  
69         /// <summary>Checks AkismetComment object against Akismet database.</summary>   54         /// <summary>Checks AkismetComment object against Akismet database.</summary>
70         /// <param name="comment">AkismetComment object to check.</param>   55         /// <param name="comment">AkismetComment object to check.</param>
71         /// <returns>'True' if spam, 'False' if not spam.</returns>   56         /// <returns>'True' if spam, 'False' if not spam.</returns>
72         public bool CommentCheck(AkismetComment comment) { <> 57         public bool CommentCheck(Comment comment, HttpRequest request, Post post) {
73             bool value = false; = 58             bool value = false;
74     59  
75             value = Convert.ToBoolean(HttpPost(String.Format(commentCheckUrl, apiKey), CreateData(comment), CharSet)); <> 60             value = Convert.ToBoolean(HttpPost(String.Format(commentCheckUrl, apiKey), CreateData(comment, request, post), CharSet));
76   = 61  
77 #if DEBUG   62 #if DEBUG
78             Console.WriteLine("CommentCheck() = {0}.", value);   63             Console.WriteLine("CommentCheck() = {0}.", value);
79 #endif   64 #endif
80     65  
81             return value;   66             return value;
82         }   67         }
83     68  
84         /// <summary>Submits AkismetComment object into Akismet database.</summary>   69         /// <summary>Submits AkismetComment object into Akismet database.</summary>
85         /// <param name="comment">AkismetComment object to submit.</param>   70         /// <param name="comment">AkismetComment object to submit.</param>
86         public void SubmitSpam(AkismetComment comment) { <> 71         public void SubmitSpam(Comment comment, HttpRequest request, Post post) {
87             string value = HttpPost(String.Format(submitSpamUrl, apiKey), CreateData(comment), CharSet);   72             string value = HttpPost(String.Format(submitSpamUrl, apiKey), CreateData(comment, request, post), CharSet);
88 #if DEBUG = 73 #if DEBUG
89             Console.WriteLine("SubmitSpam() = {0}.", value);   74             Console.WriteLine("SubmitSpam() = {0}.", value);
90 #endif   75 #endif
91         }   76         }
92     77  
93         /// <summary>Retracts false positive from Akismet database.</summary>   78         /// <summary>Retracts false positive from Akismet database.</summary>
94         /// <param name="comment">AkismetComment object to retract.</param>   79         /// <param name="comment">AkismetComment object to retract.</param>
95         public void SubmitHam(AkismetComment comment) { <> 80         public void SubmitHam(Comment comment, HttpRequest request, Post post) {
96             string value = HttpPost(String.Format(submitHamUrl, apiKey), CreateData(comment), CharSet);   81             string value = HttpPost(String.Format(submitHamUrl, apiKey), CreateData(comment, request, post), CharSet);
97 #if DEBUG = 82 #if DEBUG
98             Console.WriteLine("SubmitHam() = {0}.", value);   83             Console.WriteLine("SubmitHam() = {0}.", value);
99 #endif   84 #endif
100         }   85         }
101     86  
102     87  
103     88  
104         #region - Private Methods (CreateData, HttpPost) -   89         #region - Private Methods (CreateData, HttpPost) -
105     90  
106         /// <summary>Takes an AkismetComment object and returns an (escaped) string of data to POST.</summary>   91         /// <summary>Takes an AkismetComment object and returns an (escaped) string of data to POST.</summary>
107         /// <param name="comment">AkismetComment object to translate.</param>   92         /// <param name="comment">AkismetComment object to translate.</param>
108         /// <returns>A System.String containing the data to POST to Akismet API.</returns>   93         /// <returns>A System.String containing the data to POST to Akismet API.</returns>
109         private string CreateData(AkismetComment comment) { <> 94         private string CreateData(Comment comment, HttpRequest request, Post post) {
      95             var userIp = request.UserHostAddress;
      96             var userAgent = request.UserAgent;
      97             var referrer = "";
      98             try
      99             {
      100                 referrer = request.UrlReferrer.ToString();
      101             } catch (UriFormatException) {}
      102             var permalink = post.PermaLink.ToString();
      103             var type = "comment";
      104             var author = comment.Author;
      105             var authorEmail = comment.Email;
      106             var authorUrl = "";
      107             if (comment.Website != null)
      108                 authorUrl = comment.Website.ToString();
      109             var content = comment.Content;
    = 110  
110             string value = String.Format("blog={0}&user_ip={1}&user_agent={2}&referrer={3}&permalink={4}&comment_type={5}" +   111             string value = String.Format("blog={0}&user_ip={1}&user_agent={2}&referrer={3}&permalink={4}&comment_type={5}" +
111                 "&comment_author={6}&comment_author_email={7}&comment_author_url={8}&comment_content={9}",   112                 "&comment_author={6}&comment_author_email={7}&comment_author_url={8}&comment_content={9}",
112                 HttpUtility.UrlEncode(comment.Blog), <> 113                 HttpUtility.UrlEncode(blog),
113                 HttpUtility.UrlEncode(comment.UserIp),   114                 HttpUtility.UrlEncode(userIp),
114                 HttpUtility.UrlEncode(comment.UserAgent),   115                 HttpUtility.UrlEncode(userAgent),
115                 HttpUtility.UrlEncode(comment.Referrer),   116                 HttpUtility.UrlEncode(referrer),
116                 HttpUtility.UrlEncode(comment.Permalink),   117                 HttpUtility.UrlEncode(permalink),
117                 HttpUtility.UrlEncode(comment.CommentType),   118                 HttpUtility.UrlEncode(type),
118                 HttpUtility.UrlEncode(comment.CommentAuthor),   119                 HttpUtility.UrlEncode(author),
119                 HttpUtility.UrlEncode(comment.CommentAuthorEmail),   120                 HttpUtility.UrlEncode(authorEmail),
120                 HttpUtility.UrlEncode(comment.CommentAuthorUrl),   121                 HttpUtility.UrlEncode(authorUrl),
121                 HttpUtility.UrlEncode(comment.CommentContent)   122                 HttpUtility.UrlEncode(content)
122             ); = 123             );
123     124  
124             return value;   125             return value;
125         }   126         }
126     127  
127         /// <summary>Sends HttpPost</summary>   128         /// <summary>Sends HttpPost</summary>