CSharpFeeds - All your C# feeds in one place.

Sponsors

Monday, August 14, 2006

Community Server 2.1 Spam Report Job

by Keyvan Nayyeri via Keyvan Nayyeri on 8/14/2006 2:55:52 PM

To complete my post about writing a CSJob I publish a real job here.

This job tries to get the list of all comment and trackback spams in all blogs on a site and sends an email which contains name and url of spams to all owners of that blog.  Having good information about spammers is worth thing for bloggers.  The importance of this job is to help site owners analyze their blog spammers and use good strategies to block them.

This is the source code of my job:

public class ReportManager : IJob

{

    private string AlternateEmails = null;

    private bool RemoveAfterSend = false;

 

    #region IJob Members

 

    public void Execute(System.Xml.XmlNode node)

    {

        XmlAttribute AlternativeEmailAtribute =

            node.Attributes["alternativeemails"];

        if (AlternativeEmailAtribute != null)

            this.AlternateEmails =

                AlternativeEmailAtribute.Value.ToString();

 

        XmlAttribute RemoveAfterSendAtribute =

            node.Attributes["removeaftersend"];

        try

        {

            this.RemoveAfterSend =

                Convert.ToBoolean

                (RemoveAfterSendAtribute.Value);

        }

        catch

        {

            throw new Exception

                ("Please set 'removeaftersend' " +

                "Boolean attribute in configurations");

        }           

 

 

        foreach (Weblog Blog in

            Weblogs.GetWeblogs(false, true, false))

        {

            BlogThreadQuery Query = new BlogThreadQuery();

            Query.SectionID = Blog.SectionID;

            Query.BlogPostType = BlogPostType.Comment

                | BlogPostType.Trackback;

            Query.SortBy = BlogThreadSortBy.MostRecent;

            Query.SortOrder = SortOrder.Descending;

            Query.PageSize = ((Blog.CommentCount

                + Blog.TrackbackCount + Blog.TotalPosts)* 2);

 

            ThreadSet Posts =

                WeblogPosts.GetBlogThreads(Query, false);

 

            if (Posts.Threads.Count > 0)

            {

 

                StringBuilder objOutput =

                    new StringBuilder("Hi,<br />");

                objOutput.Append

                    ("This is your latest spam list:");

                objOutput.Append("<br /><br />");

 

 

                foreach (Post post in Posts.Threads)

                {

                    WeblogPost wpost =

                        WeblogPosts.GetPost

                        (post.PostID, false, false, false);

                    if

                        ((wpost.PostStatus == PostStatus.Ham) ||

                        (wpost.PostStatus == PostStatus.LikelySpam) ||

                        (wpost.PostStatus == PostStatus.NeedsApproval) ||

                        (wpost.PostStatus == PostStatus.Spam))

                    {

                        if ((wpost.GetExtendedAttribute

                            ("SubmittedUserName") != null) &&

                            (wpost.GetExtendedAttribute

                            ("SubmittedUserName") != string.Empty))

                        {

                            objOutput.Append

                                (wpost.GetExtendedAttribute

                                ("SubmittedUserName") + "<br />");

                        }

                        else

                        {

                            objOutput.Append(wpost.Username + "<br />");

                        }

 

                        if (wpost.GetExtendedAttribute

                            ("trackbackName") != null &&

                            wpost.GetExtendedAttribute

                            ("trackbackName") != string.Empty)

                        {

                            objOutput.Append

                                (wpost.GetExtendedAttribute

                                ("trackbackName") + "<br />");

                        }

                        objOutput.Append

                            (wpost.GetExtendedAttribute

                            ("TitleUrl") + "<br />");

                        objOutput.Append

                            ("--------------------<br />");

                    }

                }

 

 

                MailMessage objMail = new MailMessage();

                objMail.Subject =

                    "[" + Blog.Name + "] " + "Spams List";

                objMail.From =

                    CSContext.Current

                    .SiteSettings.AdminEmailAddress;

 

 

                ArrayList OwnersEmail = new ArrayList();

                foreach (string OwnerName in Blog.OwnerArray)

                {

                    User Owner =

                        Users.FindUserByUsername(OwnerName);

                    OwnersEmail.Add(Owner.Email);

                }

 

                if ((this.AlternateEmails == null) &&

                    (OwnersEmail != null) &&

                    (OwnersEmail.Count > 0))

                {

                    objMail.To = string.Join

                        (";", (String[])OwnersEmail.ToArray(typeof(string)));

                }

                else

                    objMail.To = this.AlternateEmails;

 

                objMail.Body = objOutput.ToString();

                objMail.Priority = MailPriority.Low;

                objMail.BodyFormat = MailFormat.Html;

 

                bool Success = true;

                try

                {

                    EmailQueueProvider.Instance().QueueEmail(objMail);

                }

                catch

                {

                    Success = false;

                }

                finally

                {

                    if ((Success) && (this.RemoveAfterSend))

                    {

                        foreach (Post post in Posts.Threads)

                        {

                            WeblogPost wpost =

                                WeblogPosts.GetPost

                                (post.PostID, false, false, false);

                            WeblogPosts.Delete(wpost, wpost.AuthorID);

                        }

                    }

                }

            }

        }

    }

    #endregion

}

Note that I ignored using more Community Server specific code here to simplify the code and the process of installation.  I had to use a template for email text as well as Community Server exceptions for better exception handling (current exception handling is just for decoration).

This CSJob has some options:

  • removeaftersend: This Boolean attribute specifies that if you want to remove all spam feedbacks after sending their list to blog owners or not.  This is a required attribute and will remove spams from site only and only when it could send the email.  Otherwise (on any exception) it won't remove anything.
  • alternativeemails: Semicolon delimited list of some emails that you want to receive the list of spammers instead of blog owners.  If you don't want to provide this attribute, all your blogs must have at least one owner.

Here is a common configuration for this job.  I recommend you to use it on different thread and use a logical interval for its minutes attribute:

<Jobs minutes = "15" singleThread = "true">

  <job

    singleThread = "false"

    minutes="720" 

    name = "SpamReport"

    type = "Keyvan.SpamReportCSJob.ReportManager, Keyvan.SpamReportCSJob"

    enabled="true"

    enableShutDown = "false"

    removeaftersend="false" />

</Jobs>

This is another example of using it with alternativeemails and removeaftersend attributes:

<Jobs minutes = "15" singleThread = "true">

  <job

    singleThread = "false"

    minutes="720" 

    name = "SpamReport"

    type = "Keyvan.SpamReportCSJob.ReportManager, Keyvan.SpamReportCSJob"

    enabled="true"

    enableShutDown = "false"

    alternativeemails = "someone@somewhere.com"

    removeaftersend="true" />

</Jobs>

Download this job with source code and binary files for Community Server 2.1 and ASP.NET 2.0 from my file gallery.

email it!bookmark it!digg it!

Original Post: Community Server 2.1 Spam Report Job

Subscribe

New Feed

Product Spotlight

Recently Updated Sources

Legal Note

The content of the postings is owned by the respective author. CSharpFeeds is not responsible for the contents of the postings. This site is automatically generated and cannot be reviewed for abusive content. If you find abusive content on CSharpFeeds, please contact us. Designated trademarks and brands are the property of their respective owners. All rights reserved.

Advertise with us