Exemple #1
0
        /// <summary>
        ///   The APPEND command appends the literal argument as a new message
        ///   to the end of the specified destination mailbox.
        ///   http://tools.ietf.org/html/rfc3501#section-6.3.11
        /// </summary>
        /// <param name = "mailboxName">The name of the malbox to insert the message.</param>
        /// <param name = "message">The message to append.</param>
        /// <param name = "flags">Sets the flags of the message. This is optional.</param>
        public ImapResponse Append(string mailboxName, Message message, MessageFlags flags = (MessageFlags)0x0000)
        {
            // we need to convert non ASCII names according to IMAP specs.
            // http://tools.ietf.org/html/rfc2060#section-5.1.3
            var name = MailboxNameEncoder.Encode(mailboxName);

            var mime       = message.ToMime();
            var size       = mime.Length;
            var flagString = flags == 0x0000 ? string.Empty : string.Format(" ({0})", flags.ToMimeFormat());
            var text       = string.Format("APPEND {0}{1} {{{2}}}", name, flagString, size);
            var reader     = SendAndReceive(new ImapCommand(text));

            if (reader.IsContinuation)
            {
                var finalReader = SendAndReceive(new BlankImapCommand(mime));

                var validResponse = new ImapResponse();
                validResponse.Parse(finalReader);
                return(validResponse);
            }

            var invalidResponse = new ImapResponse();

            invalidResponse.Parse(reader);
            return(invalidResponse);
        }
Exemple #2
0
        /// <summary>
        ///   The CREATE command creates a mailbox with the given name.
        ///   http://tools.ietf.org/html/rfc3501#section-6.3.3
        /// </summary>
        /// <param name = "mailboxName">The name of the mailbox to create.</param>
        public ImapResponse Create(string mailboxName)
        {
            // we need to convert non ASCII names according to IMAP specs.
            // http://tools.ietf.org/html/rfc2060#section-5.1.3
            var name = MailboxNameEncoder.Encode(mailboxName);

            var text    = string.Format("CREATE \"{0}\"", name);
            var command = new ImapCommand(text);
            var reader  = SendAndReceive(command);

            var response = new ImapResponse();

            response.Parse(reader);
            return(response);
        }
Exemple #3
0
        /// <summary>
        ///   The COPY command copies the specified message(s) to the end of the specified destination mailbox.
        ///   http://tools.ietf.org/html/rfc3501#section-6.4.7
        /// </summary>
        public ImapResponse Copy(SequenceSet set, string destinationMailboxName)
        {
            // we need to convert non ASCII names according to IMAP specs.
            // http://tools.ietf.org/html/rfc2060#section-5.1.3
            var name = MailboxNameEncoder.Encode(destinationMailboxName);

            var text    = string.Format("COPY {0} \"{1}\"", set, name);
            var command = new ImapCommand(text);
            var reader  = SendAndReceive(command);

            var response = new ImapResponse();

            response.Parse(reader);
            return(response);
        }
Exemple #4
0
        /// <summary>
        ///   The LSUB command returns a subset of names from the set of names that the user has declared as being "active" or "subscribed".
        ///   http://tools.ietf.org/html/rfc3501#section-6.3.9
        /// </summary>
        /// <param name = "referenceName">The reference name.</param>
        /// <param name = "wildcardedMailboxName">The mailbox name with possible wildcards.</param>
        public ListImapResponse LSub(string referenceName, string wildcardedMailboxName)
        {
            // we need to convert non ASCII names according to IMAP specs.
            // http://tools.ietf.org/html/rfc2060#section-5.1.3
            referenceName         = MailboxNameEncoder.Encode(referenceName);
            wildcardedMailboxName = MailboxNameEncoder.Encode(wildcardedMailboxName);

            var text    = string.Format("LSUB \"{0}\" \"{1}\"", referenceName, wildcardedMailboxName);
            var command = new ImapCommand(text);
            var reader  = SendAndReceive(command);

            var response = new ListImapResponse();

            response.Parse(reader);
            return(response);
        }
Exemple #5
0
        /// <summary>
        ///   The RENAME command changes the name of a mailbox.  A tagged OK
        ///   response is returned only if the mailbox has been renamed.  It is
        ///   an error to attempt to rename from a mailbox name that does not
        ///   exist or to a mailbox name that already exists.  Any error in
        ///   renaming will return a tagged NO response.
        /// </summary>
        /// <param name = "sourceName">The fullname of the mailbox to rename.</param>
        /// <param name = "targetName">The new name for the mailbox.</param>
        /// <returns>Returns the server response.</returns>
        public ImapResponse Rename(string sourceName, string targetName)
        {
            // we need to convert non ASCII names according to IMAP specs.
            // http://tools.ietf.org/html/rfc2060#section-5.1.3
            var source = MailboxNameEncoder.Encode(sourceName);

            // we need to convert non ASCII names according to IMAP specs.
            // http://tools.ietf.org/html/rfc2060#section-5.1.3
            var target = MailboxNameEncoder.Encode(targetName);

            var text    = string.Format("RENAME \"{0}\" \"{1}\"", source, target);
            var command = new ImapCommand(text);
            var reader  = SendAndReceive(command);

            var response = new ImapResponse();

            response.Parse(reader);
            return(response);
        }
Exemple #6
0
        /// <summary>
        ///   The SELECT command selects a mailbox so that messages in the mailbox can be accessed.
        ///   http://tools.ietf.org/html/rfc3501#section-6.3.1
        /// </summary>
        /// <param name = "mailboxName">The name of the mailbox to select.</param>
        public SelectExamineImapResponse Select(string mailboxName)
        {
            // we need to convert non ASCII names according to IMAP specs.
            // http://tools.ietf.org/html/rfc2060#section-5.1.3
            var name = MailboxNameEncoder.Encode(mailboxName);

            var text    = string.Format("SELECT \"{0}\"", name);
            var command = new ImapCommand(text);
            var reader  = SendAndReceive(command, false);

            var response = new SelectExamineImapResponse(mailboxName);

            response.Parse(reader);

            if (response.IsOk)
            {
                SelectedMailbox = mailboxName;
            }

            return(response);
        }
Exemple #7
0
        protected internal override void Parse(ImapResponseReader reader)
        {
            while (true)
            {
                if (reader.IsCompleted)
                {
                    TakeSnapshot(reader);
                    break;
                }

                if (!reader.IsUntagged)
                {
                    continue;
                }

                var mailbox = new Mailbox();
                var line    = reader.CurrentLine;
                // pattern is identical to message flags
                var attMatches = Regex.Matches(line, RegexPatterns.SingleFlagPattern);
                foreach (Match match in attMatches)
                {
                    NameAttributes flag;
                    var            value   = match.Value.TrimAnySeperate(1, 0);
                    var            success = Enum.TryParse(value, true, out flag);
                    if (success)
                    {
                        mailbox.Attributes |= flag;
                    }
                    else
                    {
                        ((IList <string>)mailbox.Keywords).Add(match.Value);
                    }
                }
                string encodedName;

                var quotedItemsMatch = Regex.Matches(line, RegexPatterns.QuotedItemsPattern);
                if (quotedItemsMatch.Count == 1)
                {
                    mailbox.Delimiter = quotedItemsMatch[0].Value.TrimQuotes()[0];
                }
                if (quotedItemsMatch.Count == 2)
                {
                    mailbox.Delimiter = quotedItemsMatch[0].Value.TrimQuotes()[0];
                    encodedName       = quotedItemsMatch[1].Value.TrimQuotes();
                }
                else
                {
                    var split     = line.Split(Characters.Space);
                    var lastIndex = split.Length - 1;
                    encodedName = split[lastIndex].TrimQuotes();
                }

                // we need to decode ASCII names according to IMAP specs.
                // http://tools.ietf.org/html/rfc2060#section-5.1.3
                mailbox.Name = MailboxNameEncoder.Decode(encodedName);

                _mailboxes.Add(mailbox);

                reader.ReadNextLine();
            }
        }