Пример #1
0
        /// <summary>
        /// Gets the messages flags for the provided uids
        /// </summary>
        /// <param name="uids">The list of uids to get the flags for</param>
        /// <param name="mailbox">The mailbox to get the messages from</param>
        /// <returns>Returns a list of MessageInfo objects. Only the flags are filled in.</returns>
        public List<MessageInfo> GetMessageFlags(long[] uids)
        {
            if (!Authed)
                throw new NotAuthenticatedException();
            lock (sequenceLock)
            {
                if (this.selectedMailbox == null)
                    throw new InvalidOperationException("No mailbox or folder currently selected.");

                string tag = GetTag();

                string uidRange = Util.BuildUIDRange(uids);

                string response = SendCommandGetResponse(tag + "UID FETCH " + uidRange +
                    " (FLAGS)");

                List<MessageInfo> messageFlags = new List<MessageInfo>();
                while (response.StartsWith("*"))
                {
                    MessageInfo info = new MessageInfo();
                    List<MessageFlag> flags = new List<MessageFlag>();
                    Match m = ImapParsing.FetchFlags.Match(response);
                    if (m.Success)
                    {
                        info.Flags = m.Groups[2].Value.Split(' ').ToList();

                        info.MessageNumber = Convert.ToInt64(m.Groups[1].Value);
                    }

                    m = ImapParsing.FetchUID.Match(response);
                    if (m.Success)
                        info.UID = Convert.ToInt64(m.Groups[1].Value);

                    messageFlags.Add(info);

                    response = GetResponse();
                }
  
                if (!IsResponseOK(response, tag))
                    throw new BadServerResponseException(response);
                return messageFlags;
            }
        }
Пример #2
0
        private List<MessageInfo> GetMailHeader(long[] uids, bool seen = true, MessageReceived callback = null)
        {
            if (!Authed)
                throw new NotAuthenticatedException();
            lock (sequenceLock)
            {
                if (this.selectedMailbox == null)
                    throw new InvalidOperationException("No mailbox or folder currently selected.");

                string EXTRAHEADERS = "";

                if(this.supportsXGMEXT)
                    EXTRAHEADERS = "X-GM-LABELS X-GM-THRID X-GM-MSGID ";

                Dictionary<long,MessageInfo> headers = new Dictionary<long,MessageInfo>();
                Dictionary<long, string> rawResponse = new Dictionary<long, string>();
                string uidRange = Util.BuildUIDRange(uids);
                string tag = GetTag();
                string response = SendCommandGetResponse(tag + "UID FETCH " + uidRange + " (FLAGS " + EXTRAHEADERS + "BODY" +
                    (seen ? null : ".PEEK") + "[HEADER])");
                while (response.StartsWith("*"))
                {
                    MessageInfo info = new MessageInfo();
                    Match m = ImapParsing.Fetch.Match(response);
                    string builder = "";
                    if (m.Success)
                    {
                        int size = Convert.ToInt32(m.Groups[2].Value);
                        builder = GetData(size);
                        info.MessageNumber = Convert.ToInt64(m.Groups[1].Value);
                    }
                    m = ImapParsing.FetchUID.Match(response);
                    if (m.Success)
                        info.UID = Convert.ToInt64(m.Groups[1].Value);

                    info.RawHeader = builder;

                    headers.Add(info.UID, info);
                    rawResponse.Add(info.UID, response);

                    response = GetResponse();

                    //We need to go ahead and swallow this response
                    //Since we have already gathered the data from the stream
                    if (response == ")")
                        response = GetResponse();
                }

                if (!IsResponseOK(response, tag))
                    throw new BadServerResponseException(response);

                foreach (long uid in headers.Keys)
                {
                    MessageInfo info = headers[uid];
                    Match m = null;
                    response = rawResponse[uid];

                    if (this.supportsXGMEXT)
                    {
                        m = ImapParsing.FetchLabels.Match(response);
                        if (m.Success)
                        {
                            List<string> labels = Util.ParseLabels(Regex.Replace(m.Groups[1].Value, "(?!\\\\\")(\\\\)", string.Empty));

                            info.Labels = new List<string>();

                            foreach (string label in labels)
                            {
                                info.Labels.Add(Regex.Replace(label, "\"", string.Empty));
                            }
                        }

                        m = ImapParsing.FetchThreadID.Match(response);
                        if (m.Success)
                            info.ThreadID = m.Groups[1].Value;

                        m = ImapParsing.FetchMessageID.Match(response);
                        if (m.Success)
                            info.MessageID = m.Groups[1].Value;
                    }

                    m = ImapParsing.FetchFlags.Match(response);
                    if (m.Success)
                    {
                        info.Flags = m.Groups[1].Value.Split(' ').ToList();
                    }

                    info.Envelope = MessageBuilder.FromHeader(info.RawHeader);
                    info.Date = Util.ParseDateTime(info.Envelope.Headers["Date"]);

                    if (callback != null)
                        callback.Invoke(info);
                }

                return headers.Values.ToList();
            }
        }