예제 #1
0
        public virtual Quota GetQuota(string mailbox)
        {
            if (!Supports("NAMESPACE"))
            {
                new Exception("This command is not supported by the server!");
            }
            IdlePause();

            Quota  quota    = null;
            string command  = GetTag() + "GETQUOTAROOT " + ModifiedUtf7Encoding.Encode(mailbox).QuoteString();
            string response = SendCommandGetResponse(command);
            string reg      = "\\* QUOTA (.*?) \\((.*?) (.*?) (.*?)\\)";

            while (response.StartsWith("*"))
            {
                Match m = Regex.Match(response, reg);
                if (m.Groups.Count > 1)
                {
                    quota = new Quota(m.Groups[1].ToString(),
                                      m.Groups[2].ToString(),
                                      Int32.Parse(m.Groups[3].ToString()),
                                      Int32.Parse(m.Groups[4].ToString())
                                      );
                    break;
                }
                response = GetResponse();
            }

            IdleResume();
            return(quota);
        }
예제 #2
0
        public virtual async Task <int> GetMessageCountAsync(string mailbox)
        {
            await IdlePauseAsync();

            string command  = GetTag() + "STATUS " + Utilities.QuoteString(ModifiedUtf7Encoding.Encode(mailbox) ?? _SelectedMailbox) + " (MESSAGES)";
            string response = await SendCommandGetResponseAsync(command);

            string reg    = @"\* STATUS.*MESSAGES (\d+)";
            int    result = 0;

            while (response.StartsWith("*"))
            {
                Match m = Regex.Match(response, reg);
                if (m.Groups.Count > 1)
                {
                    result = Convert.ToInt32(m.Groups[1].ToString());
                }
                response = await GetResponseAsync();

                m = Regex.Match(response, reg);
            }
            await IdleResumeAsync();

            return(result);
        }
예제 #3
0
        public virtual void AppendMail(MailMessage email, string mailbox = null)
        {
            IdlePause();

            mailbox = ModifiedUtf7Encoding.Encode(mailbox);
            string flags = String.Empty;
            var    body  = new StringBuilder();

            using (var txt = new System.IO.StringWriter(body))
                email.Save(txt);

            string size = body.Length.ToString();

            if (email.RawFlags.Length > 0)
            {
                flags = " (" + string.Join(" ", email.Flags) + ")";
            }

            if (mailbox == null)
            {
                CheckMailboxSelected();
            }
            mailbox = mailbox ?? _SelectedMailbox;

            string command  = GetTag() + "APPEND " + (mailbox ?? _SelectedMailbox).QuoteString() + flags + " {" + size + "}";
            string response = SendCommandGetResponse(command);

            if (response.StartsWith("+"))
            {
                response = SendCommandGetResponse(body.ToString());
            }
            IdleResume();
        }
예제 #4
0
        public virtual void DeleteMailbox(string mailbox)
        {
            IdlePause();
            string command = GetTag() + "DELETE " + ModifiedUtf7Encoding.Encode(mailbox).QuoteString();

            SendCommandCheckOK(command);
            IdleResume();
        }
예제 #5
0
        public void Encode_InputWithConventionalAmpersand_ReturnsStringWithAmpersandMinus()
        {
            string mailboxWithAmpersand = "Test & Test";

            string result = ModifiedUtf7Encoding.Encode(mailboxWithAmpersand);

            result.ShouldBe("Test &- Test");
        }
예제 #6
0
        public void Encode_InputWithCyrillicCharacters_ReturnsStringWithEncodedCyrillicCharacters()
        {
            string mailboxNameWithCyrillicCharacters = "Отправленные";

            string result = ModifiedUtf7Encoding.Encode(mailboxNameWithCyrillicCharacters);

            result.ShouldBe("&BB4EQgQ,BEAEMAQyBDsENQQ9BD0ESwQ1-");
        }
예제 #7
0
        public void Encode_InputWithUmlaut_ReturnsStringWithEncodedUmlaut()
        {
            string mailboxNameWithUmlaut = "Entwürfe";

            string result = ModifiedUtf7Encoding.Encode(mailboxNameWithUmlaut);

            result.ShouldBe("Entw&APw-rfe");
        }
예제 #8
0
        public void Encode_AsciiOnlyInput_ReturnsOriginalString()
        {
            string mailboxNameWithAsciiOnly = "Sent";

            string result = ModifiedUtf7Encoding.Encode(mailboxNameWithAsciiOnly);

            result.ShouldBe(mailboxNameWithAsciiOnly);
        }
예제 #9
0
        public virtual void CreateMailbox(string mailbox)
        {
            IdlePause();
            var command = GetTag() + "CREATE " + ModifiedUtf7Encoding.Encode(mailbox).QuoteString();

            SendCommandCheckOK(command);
            IdleResume();
        }
예제 #10
0
        public virtual async Task UnSuscribeMailboxAsync(string mailbox)
        {
            await IdlePauseAsync();

            string command = GetTag() + "UNSUBSCRIBE " + ModifiedUtf7Encoding.Encode(mailbox).QuoteString();

            await SendCommandCheckOKAsync(command);
            await IdleResumeAsync();
        }
예제 #11
0
        public virtual void UnSuscribeMailbox(string mailbox)
        {
            IdlePause();

            string command = GetTag() + "UNSUBSCRIBE " + ModifiedUtf7Encoding.Encode(mailbox).QuoteString();

            SendCommandCheckOK(command);
            IdleResume();
        }
예제 #12
0
        public virtual async Task DeleteMailboxAsync(string mailbox)
        {
            await IdlePauseAsync();

            string command = GetTag() + "DELETE " + ModifiedUtf7Encoding.Encode(mailbox).QuoteString();

            await SendCommandCheckOKAsync(command);
            await IdleResumeAsync();
        }
예제 #13
0
        public virtual Mailbox Examine(string mailbox)
        {
            IdlePause();

            Mailbox box      = null;
            var     tag      = GetTag();
            var     command  = tag + "EXAMINE " + ModifiedUtf7Encoding.Encode(mailbox).QuoteString();
            var     response = SendCommandGetResponse(command);

            if (response.StartsWith("*"))
            {
                box = new Mailbox(mailbox);
                while (response.StartsWith("*"))
                {
                    Match m;

                    m = Regex.Match(response, @"(\d+) EXISTS");
                    if (m.Groups.Count > 1)
                    {
                        box.NumMsg = m.Groups[1].ToString().ToInt();
                    }

                    m = Regex.Match(response, @"(\d+) RECENT");
                    if (m.Groups.Count > 1)
                    {
                        box.NumNewMsg = m.Groups[1].Value.ToInt();
                    }

                    m = Regex.Match(response, @"UNSEEN (\d+)");
                    if (m.Groups.Count > 1)
                    {
                        box.NumUnSeen = m.Groups[1].Value.ToInt();
                    }

                    m = Regex.Match(response, @"UIDVALIDITY (\d+)");
                    if (m.Groups.Count > 1)
                    {
                        box.UIDValidity = m.Groups[1].Value.ToInt();
                    }

                    m = Regex.Match(response, @" FLAGS \((.*?)\)");
                    if (m.Groups.Count > 1)
                    {
                        box.SetFlags(m.Groups[1].ToString());
                    }

                    response = GetResponse();
                }

                SelectedMailbox = mailbox;
            }

            IdleResume();
            return(box);
        }
예제 #14
0
        public virtual async Task <Mailbox> ExamineAsync(string mailbox)
        {
            await IdlePauseAsync();

            Mailbox x        = null;
            string  tag      = GetTag();
            string  command  = tag + "EXAMINE " + ModifiedUtf7Encoding.Encode(mailbox).QuoteString();
            string  response = await SendCommandGetResponseAsync(command);

            if (response.StartsWith("*"))
            {
                x = new Mailbox(mailbox);
                while (response.StartsWith("*"))
                {
                    Match m;

                    m = Regex.Match(response, @"(\d+) EXISTS");
                    if (m.Groups.Count > 1)
                    {
                        x.NumMsg = m.Groups[1].ToString().ToInt();
                    }

                    m = Regex.Match(response, @"(\d+) RECENT");
                    if (m.Groups.Count > 1)
                    {
                        x.NumNewMsg = m.Groups[1].Value.ToInt();
                    }

                    m = Regex.Match(response, @"UNSEEN (\d+)");
                    if (m.Groups.Count > 1)
                    {
                        x.NumUnSeen = m.Groups[1].Value.ToInt();
                    }

                    m = Regex.Match(response, @"UIDVALIDITY (\d+)");
                    if (m.Groups.Count > 1)
                    {
                        x.UIDValidity = m.Groups[1].Value.ToInt();
                    }

                    m = Regex.Match(response, @" FLAGS \((.*?)\)");
                    if (m.Groups.Count > 1)
                    {
                        x.SetFlags(m.Groups[1].ToString());
                    }

                    response = await GetResponseAsync();
                }
                _SelectedMailbox = mailbox;
            }
            await IdleResumeAsync();

            return(x);
        }
예제 #15
0
        public virtual Mailbox SelectMailbox(string mailbox)
        {
            IdlePause();

            mailbox = ModifiedUtf7Encoding.Encode(mailbox);
            Mailbox x        = null;
            string  tag      = GetTag();
            string  command  = tag + "SELECT " + mailbox.QuoteString();
            string  response = SendCommandGetResponse(command);

            if (response.StartsWith("*"))
            {
                x = new Mailbox(mailbox);
                while (response.StartsWith("*"))
                {
                    Match m;
                    m = Regex.Match(response, @"(\d+) EXISTS");
                    if (m.Groups.Count > 1)
                    {
                        x.NumMsg = Convert.ToInt32(m.Groups[1].ToString());
                    }
                    m = Regex.Match(response, @"(\d+) RECENT");
                    if (m.Groups.Count > 1)
                    {
                        x.NumNewMsg = Convert.ToInt32(m.Groups[1].ToString());
                    }
                    m = Regex.Match(response, @"UNSEEN (\d+)");
                    if (m.Groups.Count > 1)
                    {
                        x.NumUnSeen = Convert.ToInt32(m.Groups[1].ToString());
                    }
                    m = Regex.Match(response, @" FLAGS \((.*?)\)");
                    if (m.Groups.Count > 1)
                    {
                        x.SetFlags(m.Groups[1].ToString());
                    }
                    response = GetResponse();
                }
                if (IsResultOK(response))
                {
                    x.IsWritable = Regex.IsMatch(response, "READ.WRITE", RegexOptions.IgnoreCase);
                }
                _SelectedMailbox = mailbox;
            }
            else
            {
                throw new Exception(response);
            }
            IdleResume();
            return(x);
        }
예제 #16
0
        /*
         * public virtual Lazy<MailMessage>[] SearchMessages(SearchCondition criteria, bool headersonly = false, bool setseen = false)
         * {
         *  return Search(criteria, true)
         *          .Select(x => new Lazy<MailMessage>(() => GetMessage(x, headersonly, setseen)))
         *          .ToArray();
         * }
         */
        public virtual async Task <Mailbox> SelectMailboxAsync(string mailboxName)
        {
            await IdlePauseAsync();

            mailboxName = ModifiedUtf7Encoding.Encode(mailboxName);
            var tag      = GetTag();
            var command  = tag + "SELECT " + mailboxName.QuoteString();
            var response = await SendCommandGetResponseAsync(command);

            if (IsResultOK(response))
            {
                response = await GetResponseAsync();
            }
            var   mailbox = new Mailbox(mailboxName);
            Match match;

            while (response.StartsWith("*"))
            {
                if ((match = Regex.Match(response, @"\d+(?=\s+EXISTS)")).Success)
                {
                    mailbox.NumMsg = match.Value.ToInt();
                }

                else if ((match = Regex.Match(response, @"\d+(?=\s+RECENT)")).Success)
                {
                    mailbox.NumNewMsg = match.Value.ToInt();
                }

                else if ((match = Regex.Match(response, @"(?<=UNSEEN\s+)\d+")).Success)
                {
                    mailbox.NumUnSeen = match.Value.ToInt();
                }

                else if ((match = Regex.Match(response, @"(?<=\sFLAGS\s+\().*?(?=\))")).Success)
                {
                    mailbox.SetFlags(match.Value);
                }

                response = await GetResponseAsync();
            }

            CheckResultOK(response);
            mailbox.IsWritable = Regex.IsMatch(response, "READ.WRITE", RegexOptions.IgnoreCase);
            _SelectedMailbox   = mailboxName;

            await IdleResumeAsync();

            return(mailbox);
        }
예제 #17
0
        public virtual async Task <bool> CopyAsync(string messageset, string destination)
        {
            await CheckMailboxSelectedAsync();
            await IdlePauseAsync();

            string prefix = null;

            if (messageset.StartsWith("UID ", StringComparison.OrdinalIgnoreCase))
            {
                messageset = messageset.Substring(4);
                prefix     = "UID ";
            }

            string tag     = GetTag();
            string command = string.Concat(tag, prefix, "COPY ", messageset, " " + ModifiedUtf7Encoding.Encode(destination).QuoteString());

            await SendCommandAsync(command);

            // Drain Expunge responses for cases where the item was removed from this mailbox by the server.
            bool   moved = false;
            string response;

            while (true)
            {
                response = await GetResponseAsync();

                if (string.IsNullOrEmpty(response) || response.Contains(tag + "OK"))
                {
                    break;
                }

                if (response[0] != '*' || !response.Contains(" EXPUNGE"))
                {
                    continue;
                }

                // We could return the UIDs for the expunged messages, but they should match the input so we don't care.
                moved = true;

                response = await GetResponseAsync();
            }

            await IdleResumeAsync();

            return(moved);
        }
예제 #18
0
        public virtual Mailbox Examine(string mailbox)
        {
            IdlePause();

            Mailbox x        = null;
            string  tag      = GetTag();
            string  command  = tag + "EXAMINE " + ModifiedUtf7Encoding.Encode(mailbox).QuoteString();
            string  response = SendCommandGetResponse(command);

            if (response.StartsWith("*"))
            {
                x = new Mailbox(mailbox);
                while (response.StartsWith("*"))
                {
                    Match m;
                    m = Regex.Match(response, @"(\d+) EXISTS");
                    if (m.Groups.Count > 1)
                    {
                        x.NumMsg = Convert.ToInt32(m.Groups[1].ToString());
                    }
                    m = Regex.Match(response, @"(\d+) RECENT");
                    if (m.Groups.Count > 1)
                    {
                        x.NumNewMsg = Convert.ToInt32(m.Groups[1].ToString());
                    }
                    m = Regex.Match(response, @"UNSEEN (\d+)");
                    if (m.Groups.Count > 1)
                    {
                        x.NumUnSeen = Convert.ToInt32(m.Groups[1].ToString());
                    }
                    m = Regex.Match(response, @" FLAGS \((.*?)\)");
                    if (m.Groups.Count > 1)
                    {
                        x.SetFlags(m.Groups[1].ToString());
                    }
                    response = GetResponse();
                }
                _SelectedMailbox = mailbox;
            }
            IdleResume();
            return(x);
        }
예제 #19
0
        public virtual int GetMessageCount(string mailbox)
        {
            IdlePause();

            var command  = GetTag() + "STATUS " + Utilities.QuoteString(ModifiedUtf7Encoding.Encode(mailbox) ?? SelectedMailbox) + " (MESSAGES)";
            var response = SendCommandGetResponse(command);
            var reg      = @"\* STATUS.*MESSAGES (\d+)";
            var result   = 0;

            while (response.StartsWith("*"))
            {
                var m = Regex.Match(response, reg);
                if (m.Groups.Count > 1)
                {
                    result = Convert.ToInt32(m.Groups[1].ToString());
                }
                response = GetResponse();
                m        = Regex.Match(response, reg);
            }

            IdleResume();
            return(result);
        }
예제 #20
0
 public Mailbox(string name)
 {
     Name  = ModifiedUtf7Encoding.Decode(name);
     Flags = new string[0];
 }
예제 #21
0
 public void Encode_InputNull_ReturnsNull()
 {
     ModifiedUtf7Encoding.Encode(null).ShouldBe(null);
 }