Table of contents
No headers
using System;
using System.Collections.Generic;
using System.Text;
using MindTouch.Dream;
using System.IO;
namespace VespaLabsEmail
{
class Program
{
static void Main(string[] args)
{
// flags
bool Email_Alerts = false;
bool Daily_My_Watchlist_Alert = false;
bool Daily_Contributions_Page_Alert = false;
bool Monthly_VespaLabs_Update = false;
// get email client ready
System.Net.Mail.SmtpClient smtpclient = new System.Net.Mail.SmtpClient();
*** smtpclient.Host = "your email host";
//smtpclient.Port = smtpport;
// get admin plug *** warning we are using admin priviledges for retrieving information and emailing to users***
*** Plug adminPlug = Plug.New("http://www.vespalabs.org/@api/deki");
*** adminPlug.At("users", "authenticate").WithCredentials("admin", "password").GetAsync().Wait();
// get users
Console.WriteLine("Get Users");
DreamMessage usersResult = adminPlug.At("users").GetAsync().Wait();
Console.WriteLine("Got Users");
XDoc xmlResult = usersResult.AsDocument();
XDoc users = xmlResult["//users"];
// Use the following link to help you get stuck into the data
// http://wiki.developer.mindtouch.com/..._XML_documents
xmlResult.Save("user.xml"); // for debugging
foreach (XDoc user in users[".//user"])
{
//reset flags
Email_Alerts = false;
Daily_My_Watchlist_Alert = false;
Daily_Contributions_Page_Alert = false;
Monthly_VespaLabs_Update = false;
Console.Write(user["username"].Contents + " : ");
Console.WriteLine(user["email"].Contents);
// find what email options the user has set through watched pages
DreamMessage favoritesResult = adminPlug.At("users", "=" + user["username"].Contents, "favorites").GetAsync().Wait();
xmlResult = favoritesResult.AsDocument();
// check the "magic" pages that users use to manage alerts
foreach (XDoc pageTitle in xmlResult[".//title"])
{
if (pageTitle.Contents.Equals("Email Alerts")) Email_Alerts = true; // emails enabled, below are "which" emails
if (pageTitle.Contents.Equals("Daily My Watchlist Alert")) Daily_My_Watchlist_Alert = true;
if (pageTitle.Contents.Equals("Daily Contributions Page Alert")) Daily_Contributions_Page_Alert = true;
if (pageTitle.Contents.Equals("Monthly vespalabs Update")) Monthly_VespaLabs_Update = true;
}
if (Email_Alerts && !user["email"].Contents.Equals(""))
{
//format and send emails
System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage();
msg.To.Add(user["email"].Contents);
*** msg.From = new System.Net.Mail.MailAddress("vespalabs@internetscooter.com", "vespalabs.org");
msg.IsBodyHtml = true;
string subjectTag = "vespalabs: ";
string starthtml = "<html><head></head><body>";
string endhtml = "</body></html>";
string greetings = "<h3><font face=\"Courier New\">Hi " + user["fullname"].Contents + ",</font></h3>";
string instructions = "<p><font face=\"Courier New\">This is an automated email to keep you up to date on what is happening at <A href=\"http://www.vespalabs.org\">vespalabs.org</a>." +
"To manage your alerts please login and visit <A href=\"http://www.vespalabs.org/1st_Read/Email_Alerts\">Email Alerts</a>.</font><p>";
// add content based on options
msg.Body = starthtml + greetings + instructions;
// Monthly
if (Monthly_VespaLabs_Update && DateTime.Now.Day.Equals(1))
{
Console.WriteLine(" Send: Monthly vespalabs Update");
subjectTag += " :Monthly Update: ";
msg.Body += "<h3><font face=\"Courier New\">Monthly Update</font></h3>";
msg.Body += "<p>test content</p>";
}
// Daily
if (Daily_My_Watchlist_Alert)
{
Console.WriteLine(" Send: Watchlist AND Contributions Alert");
subjectTag += " :Watchlist: ";
msg.Body += "<h3><font face=\"Courier New\">Watched Pages</font></h3>";
msg.Body += "<p>test content</p>";
}
if (Daily_Contributions_Page_Alert)
{
Console.WriteLine(" Send: Watchlist Alert");
subjectTag += " :Contributions: ";
msg.Body += "<h3><font face=\"Courier New\">Changes to My Contributions</font></h3>";
msg.Body += "<p>test content</p>";
//DreamMessage pagesResult = adminPlug.At("users", "=" + user["username"].Contents, "favorites", "feed").GetAsync().Wait();
//// Console.WriteLine("Email Enabled");
//xmlResult = pagesResult.AsDocument();
//xmlResult.Save("feed" + user["username"].Contents + ".xml");
}
msg.Subject = subjectTag;
msg.Body += endhtml;
smtpclient.Send(msg);
}
}
Console.WriteLine("Done");
Console.ReadKey();// wait for user to kill process with keypress