/// <summary> /// Identifies the corresponding status update item and adds it to the current instance. /// </summary> /// <param name = "line">The line to identify and parse.</param> internal void InjectLine(string line) { if (line.StartsWith("* OK")) { #region PermanentFlags { var match = Regex.Match(line, RegexPatterns.ExaminePermanentFlagsPattern); if (match.Success) { var matches = Regex.Matches(match.Value.Substring(14), RegexPatterns.ExamineSingleFlagOrKeywordPattern); if (PermanentFlags == null) { PermanentFlags = new MessageFlagGroup(0x0000); } PermanentFlags.InjectMatches(matches); return; } } #endregion #region UidNext { var match = Regex.Match(line, RegexPatterns.ExamineUidNextPattern); if (match.Success) { var digit = match.Value.Split(Characters.Space)[1]; UidNext = int.Parse(digit); return; } } #endregion #region Unseen { var match = Regex.Match(line, RegexPatterns.ExamineUnseenPattern); if (match.Success) { var digit = match.Value.Split(Characters.Space)[1]; Unseen = int.Parse(digit); return; } } #endregion #region UidValidity { var match = Regex.Match(line, RegexPatterns.ExamineUidValidityPattern); if (match.Success) { var digit = match.Value.Split(Characters.Space)[1]; UidValidity = long.Parse(digit); return; } } #endregion return; } #region Flags { var match = Regex.Match(line, RegexPatterns.ExamineAndFetchFlagsPattern); if (match.Success) { var matches = Regex.Matches(match.Value.Substring(5), RegexPatterns.ExamineSingleFlagOrKeywordPattern); if (Flags == null) { Flags = new MessageFlagGroup(0x0000); } Flags.InjectMatches(matches); return; } } #endregion #region Exists if (line.Contains("EXISTS")) { var value = Regex.Match(line, @"\d+").Value; Exists = int.Parse(value); return; } #endregion #region Recent if (line.Contains("RECENT")) { var value = Regex.Match(line, @"\d+").Value; Recent = int.Parse(value); return; } #endregion }
/// <summary> /// Parse IMAP output from an EXAMINE or SELECT command. /// </summary> /// <param name="name">Name of the mailbox.</param> /// <param name="imapResponse">Raw IMAP output of an EXAMINE or SELECT command.</param> public Mailbox(string name, string imapResponse) : this() { // Escape modifed UTF-7 encoding for ampersands or Unicode characters. Name = Functions.FromModifiedUTF7(name); string[] responseLines = imapResponse.Replace("\r", "").Split('\n'); foreach (string responseLine in responseLines) { if (responseLine.StartsWith("* FLAGS (")) { string[] flags = responseLine.Substring(9, responseLine.Length - 10).Split(' '); foreach (string flag in flags) { if (!Flags.Contains(flag)) { Flags.Add(flag); } } } else if (responseLine.StartsWith("* OK [NOMODSEQ]")) { NoModSeq = true; } else if (responseLine.StartsWith("* OK [HIGHESTMODSEQ ")) { string highestModSeq = responseLine.Substring(20, responseLine.IndexOf("]") - 20); int highestModSeqValue; int.TryParse(highestModSeq, out highestModSeqValue); HighestModSeq = highestModSeqValue; } else if (responseLine.StartsWith("* OK [PERMANENTFLAGS (")) { string[] permanentFlags = responseLine.Substring(22, responseLine.IndexOf("]") - 22).Split(' '); foreach (string permanentFlag in permanentFlags) { if (!PermanentFlags.Contains(permanentFlag)) { PermanentFlags.Add(permanentFlag); } } } else if (responseLine.StartsWith("* OK [UIDNEXT ")) { string uidNext = responseLine.Substring(14, responseLine.IndexOf("]") - 14); int uidNextValue; int.TryParse(uidNext, out uidNextValue); UidNext = uidNextValue; } else if (responseLine.StartsWith("* OK [UIDVALIDITY ")) { string uidValidity = responseLine.Substring(18, responseLine.IndexOf("]") - 18); int UidValidityValue; int.TryParse(uidValidity, out UidValidityValue); UidValidity = UidValidityValue; } else if (responseLine.StartsWith("* VANISHED ")) { VanishedList = responseLine.Substring(11); } else if (responseLine.IndexOf(" FETCH ", StringComparison.Ordinal) > -1) { FetchList.Add(responseLine); } else if (responseLine.EndsWith(" EXISTS")) { string existsCount = responseLine.Substring(2, responseLine.Length - 9); int existsCountValue; int.TryParse(existsCount, out existsCountValue); Count = existsCountValue; } else if (responseLine.EndsWith(" RECENT")) { string recentCount = responseLine.Substring(2, responseLine.Length - 9); int recentCountValue; int.TryParse(recentCount, out recentCountValue); Recent = recentCountValue; } } }