Ejemplo n.º 1
0
        internal static ImapMailboxInfo FromAnswer(string name, ImapAnswer answer)
        {
            var result = new ImapMailboxInfo()
            {
                Name = name,
            };

            foreach (var line in answer.GetDataLines())
            {
                if (line.StartsWith("* "))
                {
                    var parts = ImapParser.SplitAnswer(line);
                    switch (parts[1])
                    {
                    case "FLAGS":
                        result.Flags = parts[2].UnboxBrackets().Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                        continue;

                    case "OK":
                        if (parts.Length > 2)
                        {
                            var subParts = ImapParser.SplitAnswer(parts[2].UnboxBrackets());
                            switch (subParts[0])
                            {
                            case "PERMANENTFLAGS":
                                result.PermanentFlags = subParts[1].UnboxBrackets().Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                                continue;

                            case "UIDVALIDITY":
                                result.UidValidity = uint.Parse(subParts[1]);
                                continue;

                            case "UIDNEXT":
                                result.UidNext = uint.Parse(subParts[1]);
                                continue;

                            case "UNSEEN":
                                result.Unseen = uint.Parse(subParts[1]);
                                continue;
                            }
                        }
                        break;

                    default:
                        switch (parts[2])
                        {
                        case "EXISTS":
                            result.Exist = int.Parse(parts[1]);
                            continue;

                        case "RECENT":
                            result.Recent = int.Parse(parts[1]);
                            continue;
                        }
                        break;
                    }
                }
            }
            return(result);
        }
Ejemplo n.º 2
0
        public static ImapAnswer Parse(string id, Stream stream)
        {
            var answer = new ImapAnswer
            {
                ID = id
            };

            var buffer  = new FifoStream();
            var current = new List <byte>(80);

            while (true)
            {
                var b = stream.ReadByte();
                if (b < 0)
                {
                    throw new EndOfStreamException();
                }

                current.Add((byte)b);
                if (b == '\n')
                {
                    var line = current.ToArray();
                    var str  = ASCII.GetString(line);
                    if (str.StartsWith(answer.ID + " "))
                    {
                        answer.Result = str;
                        break;
                    }
                    buffer.AppendBuffer(line, 0, line.Length);
                    current.Clear();
                }
            }

            answer.Data = buffer.ToArray();
            return(answer);
        }