public MailQueryType parse(Email message, IList <IMonitor> monitors, IDictionary <string, ApprovalType> authorization_dictionary) { MailQueryType query_type = MailQueryType.Authorizing; string user = message.from_address.to_lower(); bool user_is_authorized = false; if (authorization_dictionary.ContainsKey(user)) { ApprovalType user_is_approved = authorization_dictionary[user]; if (user_is_approved == ApprovalType.Approved) { user_is_authorized = true; } } if (user_is_authorized) { query_type = MailQueryType.Help; string subject_and_body = message.subject + " | " + message.message_body; string[] message_words = subject_and_body.Split(' '); foreach (string message_word in message_words) { if (message_contains_status(message_word)) { query_type = MailQueryType.Status; break; } if (message_contains_config(message_word)) { query_type = MailQueryType.Configuration; break; } if (message_contains_down(message_word)) { query_type = MailQueryType.CurrentDownItems; break; } if (message_contains_approve(message_word)) { query_type = MailQueryType.Authorized; break; } if (message_contains_deny(message_word)) { query_type = MailQueryType.Denied; break; } if (message_contains_version(message_word)) { query_type = MailQueryType.Version; break; } if (message_contains_subscribe(message_word)) { query_type = MailQueryType.Subscribe; break; } if (message_contains_unsubscribe(message_word)) { query_type = MailQueryType.Unsubscribe; break; } } } return(query_type); }
private void parse_and_send_response(Email mail_message) { string respond_to = mail_message.from_address.to_lower(); MailQueryType query_type = mail_processor.parse(mail_message, monitors, authorization_dictionary); Log.bound_to(this).Info("{0} received a message from {1} of type {2}.", ApplicationParameters.name, respond_to, query_type.ToString()); string response_text = string.Empty; if (query_type == MailQueryType.Authorized || query_type == MailQueryType.Denied) { string[] body_words = mail_message.message_body.Split(' '); foreach (string body_word in body_words) { if (body_word.Contains("@")) { respond_to = body_word.Replace(Environment.NewLine, ""); break; } } } switch (query_type) { case MailQueryType.Denied: authorization_dictionary.Add(respond_to, ApprovalType.Denied); return; break; case MailQueryType.Authorized: if (!authorization_dictionary.ContainsKey(respond_to.to_lower())) { authorization_dictionary.Add(respond_to.to_lower(), ApprovalType.Approved); } response_text = string.Format("Congratulations - you have been approved!{0}Send 'help' for options", Environment.NewLine); break; case MailQueryType.Help: response_text = string.Format( "Options- send 1:{0}help{0}status{0}config{0}down - errors{0}version{0}sub{0}unsub", Environment.NewLine); break; case MailQueryType.Status: TimeSpan up_time_current = up_time.Elapsed; response_text = string.Format("{0} has been up and running for {1} days {2} hours {3} minutes and {4} seconds.", ApplicationParameters.name, up_time_current.Days, up_time_current.Hours, up_time_current.Minutes, up_time_current.Seconds); break; case MailQueryType.CurrentDownItems: response_text = string.Format("Services currently down:{0}", Environment.NewLine); foreach (IMonitor monitor in monitors) { if (monitor.who_to_notify_as_comma_separated_list.to_lower().Contains(respond_to)) { if (!monitor.status_is_good) { response_text += string.Format("{0}{1}", monitor.name, Environment.NewLine); } } } break; case MailQueryType.Authorizing: response_text = string.Format("Bombali notified admin to add you to approved list. If you are added, you will receive a response."); break; case MailQueryType.Version: response_text = string.Format("Bombali is currently running version {0}.", Version.get_version()); break; case MailQueryType.Subscribe: if (!subscribers.ContainsKey(respond_to.to_lower())) { subscribers.Add(respond_to.to_lower(), respond_to); } response_text = string.Format("You are now subscribed to receive daily status messages"); break; case MailQueryType.Unsubscribe: if (subscribers.ContainsKey(respond_to.to_lower())) { subscribers.Remove(respond_to.to_lower()); } response_text = string.Format("You are now unsubscribed from daily status messages"); break; default: response_text = string.Format("{0} has not been implemented yet. Please watch for updates.", query_type); break; } send_notification(respond_to, response_text); if (query_type == MailQueryType.Authorizing) { send_notification(BombaliConfiguration.settings.administrator_email, string.Format("{0} reqests approval. Send approve/deny w/email address. Ex. 'deny [email protected]'", respond_to)); } }