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))
("SubmittedUserName") + "<br />");
else
objOutput.Append(wpost.Username + "<br />");
if (wpost.GetExtendedAttribute
("trackbackName") != null &&
wpost.GetExtendedAttribute
("trackbackName") != string.Empty)
("trackbackName") + "<br />");
("TitleUrl") + "<br />");
("--------------------<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)));
objMail.To = this.AlternateEmails;
objMail.Body = objOutput.ToString();
objMail.Priority = MailPriority.Low;
objMail.BodyFormat = MailFormat.Html;
bool Success = true;
EmailQueueProvider.Instance().QueueEmail(objMail);
Success = false;
finally
if ((Success) && (this.RemoveAfterSend))
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:
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:
alternativeemails = "someone@somewhere.com"
removeaftersend="true" />
Download this job with source code and binary files for Community Server 2.1 and ASP.NET 2.0 from my file gallery.
Original Post: Community Server 2.1 Spam Report Job
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.