AkismetSpamService.cs :  » Bloggers » SubText » Subtext » Framework » Services » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » Bloggers » SubText 
SubText » Subtext » Framework » Services » AkismetSpamService.cs
#region Disclaimer/Info

///////////////////////////////////////////////////////////////////////////////////////////////////
// Subtext WebLog
// 
// Subtext is an open source weblog system that is a fork of the .TEXT
// weblog system.
//
// For updated news and information please visit http://subtextproject.com/
// Subtext is hosted at Google Code at http://code.google.com/p/subtext/
// The development mailing list is at subtext@googlegroups.com 
//
// This project is licensed under the BSD license.  See the License.txt file for more information.
///////////////////////////////////////////////////////////////////////////////////////////////////

#endregion

using System;
using System.Globalization;
using System.Net;
using log4net;
using Subtext.Akismet;
using Subtext.Framework.Components;
using Subtext.Framework.Logging;
using Subtext.Framework.Routing;
using Subtext.Framework.Web;

namespace Subtext.Framework.Services{
    [Serializable]
    public class AkismetSpamService : ICommentSpamService
    {
        private readonly static ILog Log = new Log();
        readonly AkismetClient _akismet;
        readonly Blog _blog;
        readonly UrlHelper _urlHelper;

        /// <summary>
        /// Initializes a new instance of the <see cref="AkismetSpamService"/> class.
        /// </summary>
        /// <param name="apiKey">The API key.</param>
        /// <param name="blog">The blog.</param>
        public AkismetSpamService(string apiKey, Blog blog) : this(apiKey, blog, null, null)
        {
        }

        public AkismetSpamService(string apiKey, Blog blog, AkismetClient akismetClient, UrlHelper urlHelper)
        {
            _blog = blog;
            _akismet = akismetClient ?? new AkismetClient(apiKey, urlHelper.BlogUrl().ToFullyQualifiedUrl(blog));
            IWebProxy proxy = HttpHelper.GetProxy();
            if(proxy != null)
            {
                _akismet.Proxy = proxy;
            }
            _urlHelper = urlHelper ?? new UrlHelper(null, null);
        }

        /// <summary>
        /// Examines the item and determines whether or not it is spam.
        /// </summary>
        /// <param name="feedback"></param>
        /// <returns></returns>
        public bool IsSpam(FeedbackItem feedback)
        {
            Comment comment = ConvertToAkismetItem(feedback);

            try
            {
                if(_akismet.CheckCommentForSpam(comment))
                {
                    _akismet.SubmitSpam(comment);
                    return true;
                }
            }
            catch(InvalidResponseException e)
            {
                Log.Error(e.Message, e);
            }
            return false;
        }

        /// <summary>
        /// Submits the item to the service as a false positive. 
        /// Something that should not have been marked as spam.
        /// </summary>
        /// <param name="feedback"></param>
        public void SubmitGoodFeedback(FeedbackItem feedback)
        {
            Comment comment = ConvertToAkismetItem(feedback);
            _akismet.SubmitHam(comment);
        }

        /// <summary>
        /// Submits the item to the service as a piece of SPAM that got through 
        /// the filter. Something that should've been marked as SPAM.
        /// </summary>
        /// <param name="feedback"></param>
        public void SubmitSpam(FeedbackItem feedback)
        {
            Comment comment = ConvertToAkismetItem(feedback);
            _akismet.SubmitSpam(comment);
        }

        /// <summary>
        /// Verifies the api key.
        /// </summary>
        /// <returns></returns>
        public bool VerifyApiKey()
        {
            try
            {
                return _akismet.VerifyApiKey();
            }
            catch(WebException e)
            {
                Log.Error("Error occured while verifying Akismet.", e);
                return false;
            }
        }

        public Comment ConvertToAkismetItem(FeedbackItem feedback)
        {
            var comment = new Comment(feedback.IpAddress, feedback.UserAgent) {Author = feedback.Author ?? string.Empty, AuthorEmail = feedback.Email};
            if(feedback.SourceUrl != null)
            {
                comment.AuthorUrl = feedback.SourceUrl;
            }
            comment.Content = feedback.Body;
            comment.Referrer = feedback.Referrer;

            var feedbackUrl = _urlHelper.FeedbackUrl(feedback);
            if(feedbackUrl != null)
            {
                Uri permalink = feedbackUrl.ToFullyQualifiedUrl(_blog);
                if(permalink != null)
                {
                    comment.Permalink = permalink;
                }
            }

            comment.CommentType = feedback.FeedbackType.ToString().ToLower(CultureInfo.InvariantCulture);
            return comment;
        }
    }
}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.