/// <summary> /// Processes mails from non-tic@ort. This function is used in AddNewToQueue /// <para>This method prints information to the console.</para> /// </summary> /// <param name="statusToList">The list that holds the status requests to later be answered</param> /// <param name="queue">The main IAction queue</param> /// <param name="mail">The Mail to process</param> private static void _processNormalMail(List <String> statusToList, Queue <IAction> queue, RecievedMail mail) { if (mail.Subject.Contains("TEXT")) { #region WriteText int until = mail.Text.Length - 1; while ((mail.Text[until] == ' ' || mail.Text[until] == '\n' || mail.Text[until] == '\r') && until > 0) { until--; } int start = 0; while ((mail.Text[start] == ' ' || mail.Text[start] == '\n' || mail.Text[start] == '\r') && start < mail.Text.Length) { start++; } if (until > start && start >= 0 && until < mail.Text.Length) { String extract = mail.Text.Substring(start, until - start + 1); Console.ForegroundColor = Colors.Success; Console.WriteLine("[GMail] Extracted: " + extract); _addWrite(queue, ref extract); } else { Console.ForegroundColor = Colors.Error; Console.WriteLine("[GMail] Error parsing message."); } #endregion } else if (mail.Subject.Contains("STATUS")) { #region SendStatus statusToList.Add(mail.From); #endregion } else if (mail.Subject.Contains("DRAW")) { #region Draw if (String.IsNullOrEmpty(mail.Text)) { Console.ForegroundColor = Colors.Error; Console.WriteLine("[GMail] Error parsing message."); } else { if (mail.Text.Contains("ANGRY_CROMULON")) { queue.Enqueue(new DrawUndistortedChar(Program.font, (char)4)); Console.ForegroundColor = Colors.Success; Console.WriteLine("[GMail] DRAW Command: ANGRY_CROMULON Enqueued."); } else if (mail.Text.Contains("LOLO")) { queue.Enqueue(new DrawUndistortedChar(Program.font, (char)3)); Console.ForegroundColor = Colors.Success; Console.WriteLine("[GMail] DRAW Command: LOLO Enqueued."); } else if (mail.Text.Contains("CWMAP")) { queue.Enqueue(new DrawUndistortedChar(Program.font, (char)0)); Console.ForegroundColor = Colors.Success; Console.WriteLine("[GMail] DRAW Command: CWMAP Enqueued."); } else if (mail.Text.Contains("REALMEN")) { queue.Enqueue(new DrawUndistortedChar(Program.font, (char)2)); Console.ForegroundColor = Colors.Success; Console.WriteLine("[GMail] DRAW Command: REALMEN Enqueued."); } else if (mail.Text.Contains("CUAC")) { queue.Enqueue(new DrawUndistortedChar(Program.font, (char)1)); Console.ForegroundColor = Colors.Success; Console.WriteLine("[GMail] DRAW Command: CUAC Enqueued."); } else { Console.ForegroundColor = Colors.Error; Console.WriteLine("[GMail] DRAW Command Error: No such drawable found."); } } #endregion } else if (mail.Subject.Contains("VALIDCHARS")) { #region SendValidChars SendValidCharsTo(mail.From); #endregion } else { Console.ForegroundColor = Colors.Message; Console.WriteLine("[GMail] Message from=" + mail.From + "; Subject=" + mail.Subject + " has no meaning! Discarding."); } }
/// <summary> /// Gets the new mails and adds new entries to the Queue. Also handles status requests, draw requests, etc. /// <para>Maybe I should have named this "ProcessMails" or something like that...</para> /// <para>This method prints information to the console.</para> /// </summary> /// <param name="queue">... the f*****g queue</param> public static void AddNewToQueue(Queue <IAction> queue) { List <String> statusToList = new List <string>(3); List <RecievedMail> mails; if (ForceGetAllUnreadMails(5, out mails)) { if (mails.Count == 0) { } else { hasAddedDisconnectMessage = false; LastMailRecieved = Program.Time; #region ProcessMails for (int i = 0; i < mails.Count; i++) { RecievedMail m = mails[i]; #region ProcessMail m.LoadFull(); Console.ForegroundColor = Colors.Message; Console.WriteLine("\n[GMail] Got mail from " + (m.From ?? "unknown") + "; subject: " + (m.Subject ?? "unknown")); if (String.IsNullOrEmpty(m.Text)) { Console.ForegroundColor = Colors.Error; Console.WriteLine("[GMail] Couldn't interpret mail's body. Discarding :("); } else { if (m.From.Contains("tic") && m.From.Contains("ort.edu")) { #region DaroMail Console.ForegroundColor = Colors.Message; Console.WriteLine("[GMail] Mail from tic@ort detected, extracting content from Fw."); int until = m.Text.Length - 1; // Ignores characters until it sees no more spaces or new lines. while ((m.Text[until] == ' ' || m.Text[until] == '\n' || m.Text[until] == '\r') && until > 0) { until--; } // Searches for the end of the "Daro Forward" marked by an "Asunto: <subject>\r\n" // and ignores characters until it finds something useful. (no spaces, \n or \r) int start = m.Text.Length > 200 ? (m.Text.IndexOf('\n', m.Text.IndexOf("Asunto: ", 200))) : 0; if (start != -1) { while ((m.Text[start] == ' ' || m.Text[start] == '\n' || m.Text[start] == '\r') && start < m.Text.Length) { start++; } } String extract; if (until > start && until < m.Text.Length && start >= 0) { extract = m.Text.Substring(start, until - start + 1); } else { until = m.Text.Length - 1; while ((m.Text[until] == ' ' || m.Text[until] == '\n' || m.Text[until] == '\r') && until > 0) { until--; } start = 0; while ((m.Text[start] == ' ' || m.Text[start] == '\n' || m.Text[start] == '\r') && start < m.Text.Length) { start++; } if (until > start && until > 0 && until < m.Text.Length && start > 0 && start < m.Text.Length) { Console.ForegroundColor = Colors.Message; Console.WriteLine("[GMail] The Message could not be extracted from the Fw. Utilizing it 'as is'."); extract = m.Text.Substring(start, until - start + 1); } else { Console.ForegroundColor = Colors.Error; Console.WriteLine("[GMail] Could not parse text from message. Discarding :("); extract = ""; } } if (extract.Length != 0) { Console.ForegroundColor = Colors.Success; Console.WriteLine("[GMail] Extracted: " + extract); _addWrite(queue, ref extract); } else { Console.ForegroundColor = Colors.Message; Console.WriteLine("[GMail] Message discarded."); } #endregion } else { // Normal mail. _processNormalMail(statusToList, queue, m); } } #endregion ForceTrashMail(m.Id, 5); } #endregion #region RespondStatusRequests for (int i = 0; i < statusToList.Count; i++) { String f = statusToList[i]; Console.ForegroundColor = Colors.Message; Console.WriteLine("[GMail] Got Status Request from " + f); if (SendStatusTo(f)) { Console.ForegroundColor = Colors.Success; Console.WriteLine("[GMail] Success sending status info to " + f); } else { Console.ForegroundColor = Colors.Error; Console.WriteLine("[GMail] ERROR: Couldn't send status info to " + f + "."); } } #endregion } } else { // if more than 3 minutes passed since it was able to read mails, we might have gotten disconnected... if (!hasAddedDisconnectMessage && LastRead - LastSuccessfullRead > 3 * 60 && queue.Count == 0 && Program.Time - Program.LastDraw > 5 * 60) { Console.ForegroundColor = Colors.Error; DateTime now = DateTime.Now; Console.WriteLine("[GMail] Possibly lost connection. (" + now.Hour + ":" + now.Minute + ")"); queue.Enqueue(new SimpleWrite(Program.font, "Creo q me quede sin internet\n(" + now.Hour + ":" + now.Minute + ") (llamen a miz)")); hasAddedDisconnectMessage = true; } } }