internal static ImapMailboxInfo Parse(ImapResponseLine line) { const string pattern = "\\(.*?\\)|\".+?\"|\\w+"; var parts = Regex.Matches(line.Text, pattern) .OfType <Match>() .Select(x => x.Value) .ToArray(); var name = ImapMailbox.DecodeName(parts[3].TrimQuotes()); var info = new ImapMailboxInfo(name, parts[2].TrimQuotes().ToCharArray().First()) { Flags = Regex.Match(parts[1], @"\(.*\)").Value.Trim(new[] { '(', ')' }).Split(' ') }; return(info); }
private Task <byte[]> ReadMessageBodyResponseAsync(string id) { return(Task.Run(() => { var byteCount = 0; var encoding = Encoding.GetEncoding(CodePages.Latin1); var stream = new MemoryStream(); OnByteCountChanged(byteCount); using (var writer = new BinaryWriter(stream, encoding, true)) { using (var reader = new BinaryReader(_connection.SecureStream, Encoding.Default, true)) { while (true) { var buffer = new MemoryStream(); while (true) { var b = reader.ReadByte(); buffer.WriteByte(b); // new lines if (b != 13) { continue; } b = reader.ReadByte(); buffer.WriteByte(b); if (b == 10) { break; } } var bytes = buffer.ToArray(); var line = new ImapResponseLine(Encoding.ASCII.GetString(bytes)); if (line.IsUntagged) { continue; } if (line.TerminatesCommand(id)) { break; } writer.Write(bytes); byteCount += bytes.Length; OnByteCountChanged(byteCount); } } var trimLength = 0; using (var reader = new BinaryReader(stream)) { stream.Seek(0, SeekOrigin.Begin); var buffer = reader.ReadBytes((int)stream.Length); for (var i = buffer.Length - 1; i > 0; i--) { var b = buffer[i]; // Detect trailing line feeds (13), carriage returns (10) and the closing bracket (41). if (b == 10 || b == 13 || b == 41) { trimLength++; } if (b == 41) { break; } } var length = buffer.Length - trimLength; var target = new byte[length]; Buffer.BlockCopy(buffer, 0, target, 0, length); return target; } } })); }