Esempio n. 1
0
        internal static bool TryParse(ParserOptions options, byte[] text, ref int index, int endIndex, bool throwOnError, out MailboxAddress mailbox)
        {
            var             flags = AddressParserFlags.AllowMailboxAddress;
            InternetAddress address;

            if (throwOnError)
            {
                flags |= AddressParserFlags.ThrowOnError;
            }

            if (!InternetAddress.TryParse(options, text, ref index, endIndex, flags, out address))
            {
                mailbox = null;
                return(false);
            }

            mailbox = (MailboxAddress)address;

            return(true);
        }
Esempio n. 2
0
        static bool TryParse(ParserOptions options, byte[] text, ref int index, int endIndex, bool throwOnError, out GroupAddress group)
        {
            var             flags = AddressParserFlags.AllowGroupAddress;
            InternetAddress address;

            if (throwOnError)
            {
                flags |= AddressParserFlags.ThrowOnError;
            }

            if (!InternetAddress.TryParse(options, text, ref index, endIndex, 0, flags, out address))
            {
                group = null;
                return(false);
            }

            group = (GroupAddress)address;

            return(true);
        }
Esempio n. 3
0
        internal static bool TryParse(ParserOptions options, byte[] text, ref int index, int endIndex, bool throwOnError, out InternetAddress address)
        {
            address = null;

            if (!ParseUtils.SkipCommentsAndWhiteSpace (text, ref index, endIndex, throwOnError))
                return false;

            if (index == endIndex) {
                if (throwOnError)
                    throw new ParseException ("No address found.", index, index);

                return false;
            }

            // keep track of the start & length of the phrase
            int startIndex = index;
            int length = 0;

            while (index < endIndex && ParseUtils.Skip8bitWord (text, ref index, endIndex, throwOnError)) {
                length = index - startIndex;

                do {
                    if (!ParseUtils.SkipCommentsAndWhiteSpace (text, ref index, endIndex, throwOnError))
                        return false;

                    // Note: some clients don't quote dots in the name
                    if (index >= endIndex || text[index] != (byte) '.')
                        break;

                    index++;
                } while (true);
            }

            if (!ParseUtils.SkipCommentsAndWhiteSpace (text, ref index, endIndex, throwOnError))
                return false;

            // specials    =  "(" / ")" / "<" / ">" / "@"  ; Must be in quoted-
            //             /  "," / ";" / ":" / "\" / <">  ;  string, to use
            //             /  "." / "[" / "]"              ;  within a word.

            if (index >= endIndex || text[index] == (byte) ',' || text[index] == ';') {
                // we've completely gobbled up an addr-spec w/o a domain
                byte sentinel = index < endIndex ? text[index] : (byte) ',';
                string name, addrspec;

                // rewind back to the beginning of the local-part
                index = startIndex;

                if (!TryParseAddrspec (text, ref index, endIndex, sentinel, throwOnError, out addrspec))
                    return false;

                ParseUtils.SkipWhiteSpace (text, ref index, endIndex);

                if (index < endIndex && text[index] == '(') {
                    int comment = index;

                    if (!ParseUtils.SkipComment (text, ref index, endIndex)) {
                        if (throwOnError)
                            throw new ParseException (string.Format ("Incomplete comment token at offset {0}", comment), comment, index);

                        return false;
                    }

                    comment++;

                    name = Rfc2047.DecodePhrase (options, text, comment, (index - 1) - comment).Trim ();
                } else {
                    name = string.Empty;
                }

                address = new MailboxAddress (name, addrspec);

                return true;
            }

            if (text[index] == (byte) ':') {
                // rfc2822 group address
                int codepage = -1;
                string name;

                if (length > 0) {
                    name = Rfc2047.DecodePhrase (options, text, startIndex, length, out codepage);
                } else {
                    name = string.Empty;
                }

                if (codepage == -1)
                    codepage = 65001;

                return TryParseGroup (options, text, startIndex, ref index, endIndex, MimeUtils.Unquote (name), codepage, throwOnError, out address);
            }

            if (text[index] == (byte) '<') {
                // rfc2822 angle-addr token
                int codepage = -1;
                string name;

                if (length > 0) {
                    name = Rfc2047.DecodePhrase (options, text, startIndex, length, out codepage);
                } else {
                    name = string.Empty;
                }

                if (codepage == -1)
                    codepage = 65001;

                return TryParseMailbox (options, text, startIndex, ref index, endIndex, MimeUtils.Unquote (name), codepage, throwOnError, out address);
            }

            if (text[index] == (byte) '@') {
                // we're either in the middle of an addr-spec token or we completely gobbled up an addr-spec w/o a domain
                string name, addrspec;

                // rewind back to the beginning of the local-part
                index = startIndex;

                if (!TryParseAddrspec (text, ref index, endIndex, (byte) ',', throwOnError, out addrspec))
                    return false;

                ParseUtils.SkipWhiteSpace (text, ref index, endIndex);

                if (index < endIndex && text[index] == '(') {
                    int comment = index;

                    if (!ParseUtils.SkipComment (text, ref index, endIndex)) {
                        if (throwOnError)
                            throw new ParseException (string.Format ("Incomplete comment token at offset {0}", comment), comment, index);

                        return false;
                    }

                    comment++;

                    name = Rfc2047.DecodePhrase (options, text, comment, (index - 1) - comment).Trim ();
                } else {
                    name = string.Empty;
                }

                address = new MailboxAddress (name, addrspec);

                return true;
            }

            if (throwOnError)
                throw new ParseException (string.Format ("Invalid address token at offset {0}", startIndex), startIndex, index);

            return false;
        }
Esempio n. 4
0
 /// <summary>
 /// Tries to parse the given text into a new <see cref="MimeKit.InternetAddress"/> instance.
 /// </summary>
 /// <remarks>
 /// Parses a single <see cref="MailboxAddress"/> or <see cref="GroupAddress"/>. If the text contains
 /// more data, then parsing will fail.
 /// </remarks>
 /// <returns><c>true</c>, if the address was successfully parsed, <c>false</c> otherwise.</returns>
 /// <param name="text">The text.</param>
 /// <param name="address">The parsed address.</param>
 /// <exception cref="System.ArgumentNullException">
 /// <paramref name="text"/> is <c>null</c>.
 /// </exception>
 public static bool TryParse(string text, out InternetAddress address)
 {
     return TryParse (ParserOptions.Default, text, out address);
 }
Esempio n. 5
0
        /// <summary>
        /// Tries to parse the given text into a new <see cref="MimeKit.InternetAddress"/> instance.
        /// </summary>
        /// <remarks>
        /// Parses a single <see cref="MailboxAddress"/> or <see cref="GroupAddress"/>. If the text contains
        /// more data, then parsing will fail.
        /// </remarks>
        /// <returns><c>true</c>, if the address was successfully parsed, <c>false</c> otherwise.</returns>
        /// <param name="options">The parser options to use.</param>
        /// <param name="text">The text.</param>
        /// <param name="address">The parsed address.</param>
        /// <exception cref="System.ArgumentNullException">
        /// <paramref name="text"/> is <c>null</c>.
        /// </exception>
        public static bool TryParse(ParserOptions options, string text, out InternetAddress address)
        {
            if (options == null)
                throw new ArgumentNullException ("options");

            if (text == null)
                throw new ArgumentNullException ("text");

            var buffer = Encoding.UTF8.GetBytes (text);
            int endIndex = buffer.Length;
            int index = 0;

            if (!TryParse (options, buffer, ref index, endIndex, false, out address))
                return false;

            if (!ParseUtils.SkipCommentsAndWhiteSpace (buffer, ref index, endIndex, false)) {
                address = null;
                return false;
            }

            if (index != endIndex) {
                address = null;
                return false;
            }

            return true;
        }
Esempio n. 6
0
 /// <summary>
 /// Tries to parse the given input buffer into a new <see cref="MimeKit.InternetAddress"/> instance.
 /// </summary>
 /// <remarks>
 /// Parses a single <see cref="MailboxAddress"/> or <see cref="GroupAddress"/>. If the buffer contains
 /// more data, then parsing will fail.
 /// </remarks>
 /// <returns><c>true</c>, if the address was successfully parsed, <c>false</c> otherwise.</returns>
 /// <param name="buffer">The input buffer.</param>
 /// <param name="address">The parsed address.</param>
 /// <exception cref="System.ArgumentNullException">
 /// <paramref name="buffer"/> is <c>null</c>.
 /// </exception>
 public static bool TryParse(byte[] buffer, out InternetAddress address)
 {
     return TryParse (ParserOptions.Default, buffer, out address);
 }
		/// <summary>
		/// Determines whether the specified <see cref="MimeKit.GroupAddress"/> is equal to the current <see cref="MimeKit.GroupAddress"/>.
		/// </summary>
		/// <remarks>
		/// Compares two group addresses to determine if they are identical or not.
		/// </remarks>
		/// <param name="other">The <see cref="MimeKit.GroupAddress"/> to compare with the current <see cref="MimeKit.GroupAddress"/>.</param>
		/// <returns><c>true</c> if the specified <see cref="MimeKit.GroupAddress"/> is equal to the current
		/// <see cref="MimeKit.GroupAddress"/>; otherwise, <c>false</c>.</returns>
		public override bool Equals (InternetAddress other)
		{
			var group = other as GroupAddress;

			if (group == null)
				return false;

			return Name == group.Name && Members.Equals (group.Members);
		}
Esempio n. 8
0
        internal static bool TryParse(ParserOptions options, byte[] text, ref int index, int endIndex, bool throwOnError, out InternetAddress address)
        {
            address = null;

            if (!ParseUtils.SkipCommentsAndWhiteSpace(text, ref index, endIndex, throwOnError))
            {
                return(false);
            }

            // keep track of the start & length of the phrase
            int startIndex = index;
            int length     = 0;

            while (index < endIndex && ParseUtils.Skip8bitWord(text, ref index, endIndex, throwOnError))
            {
                length = index - startIndex;

                do
                {
                    if (!ParseUtils.SkipCommentsAndWhiteSpace(text, ref index, endIndex, throwOnError))
                    {
                        return(false);
                    }

                    // Note: some clients don't quote dots in the name
                    if (index >= endIndex || text[index] != (byte)'.')
                    {
                        break;
                    }

                    index++;
                } while (true);
            }

            if (!ParseUtils.SkipCommentsAndWhiteSpace(text, ref index, endIndex, throwOnError))
            {
                return(false);
            }

            // specials    =  "(" / ")" / "<" / ">" / "@"  ; Must be in quoted-
            //             /  "," / ";" / ":" / "\" / <">  ;  string, to use
            //             /  "." / "[" / "]"              ;  within a word.

            if (index >= endIndex || text[index] == (byte)',' || text[index] == ';')
            {
                // we've completely gobbled up an addr-spec w/o a domain
                byte   sentinel = index < endIndex ? text[index] : (byte)',';
                string name, addrspec;

                // rewind back to the beginning of the local-part
                index = startIndex;

                if (!TryParseAddrspec(text, ref index, endIndex, sentinel, throwOnError, out addrspec))
                {
                    return(false);
                }

                ParseUtils.SkipWhiteSpace(text, ref index, endIndex);

                if (index < endIndex && text[index] == '(')
                {
                    int comment = index;

                    if (!ParseUtils.SkipComment(text, ref index, endIndex))
                    {
                        if (throwOnError)
                        {
                            throw new ParseException(string.Format("Incomplete comment token at offset {0}", comment), comment, index);
                        }

                        return(false);
                    }

                    comment++;

                    name = Rfc2047.DecodePhrase(options, text, comment, (index - 1) - comment).Trim();
                }
                else
                {
                    name = string.Empty;
                }

                address = new MailboxAddress(name, addrspec);

                return(true);
            }

            if (text[index] == (byte)':')
            {
                // rfc2822 group address
                int    codepage;
                string name;

                if (length > 0)
                {
                    name = Rfc2047.DecodePhrase(options, text, startIndex, length, out codepage);
                }
                else
                {
                    name     = string.Empty;
                    codepage = 65001;
                }

                return(TryParseGroup(options, text, startIndex, ref index, endIndex, MimeUtils.Unquote(name), codepage, throwOnError, out address));
            }

            if (text[index] == (byte)'<')
            {
                // rfc2822 angle-addr token
                int    codepage;
                string name;

                if (length > 0)
                {
                    name = Rfc2047.DecodePhrase(options, text, startIndex, length, out codepage);
                }
                else
                {
                    name     = string.Empty;
                    codepage = 65001;
                }

                return(TryParseMailbox(text, startIndex, ref index, endIndex, MimeUtils.Unquote(name), codepage, throwOnError, out address));
            }

            if (text[index] == (byte)'@')
            {
                // we're either in the middle of an addr-spec token or we completely gobbled up an addr-spec w/o a domain
                string name, addrspec;

                // rewind back to the beginning of the local-part
                index = startIndex;

                if (!TryParseAddrspec(text, ref index, endIndex, (byte)',', throwOnError, out addrspec))
                {
                    return(false);
                }

                ParseUtils.SkipWhiteSpace(text, ref index, endIndex);

                if (index < endIndex && text[index] == '(')
                {
                    int comment = index;

                    if (!ParseUtils.SkipComment(text, ref index, endIndex))
                    {
                        if (throwOnError)
                        {
                            throw new ParseException(string.Format("Incomplete comment token at offset {0}", comment), comment, index);
                        }

                        return(false);
                    }

                    comment++;

                    name = Rfc2047.DecodePhrase(options, text, comment, (index - 1) - comment).Trim();
                }
                else
                {
                    name = string.Empty;
                }

                address = new MailboxAddress(name, addrspec);

                return(true);
            }

            if (throwOnError)
            {
                throw new ParseException(string.Format("Invalid address token at offset {0}", startIndex), startIndex, index);
            }

            return(false);
        }
Esempio n. 9
0
        internal static bool TryParseMailbox(byte[] text, int startIndex, ref int index, int endIndex, string name, int codepage, bool throwOnError, out InternetAddress address)
        {
            Encoding   encoding = Encoding.GetEncoding(codepage);
            DomainList route    = null;

            address = null;

            // skip over the '<'
            index++;
            if (index >= endIndex)
            {
                if (throwOnError)
                {
                    throw new ParseException(string.Format("Incomplete mailbox at offset {0}", startIndex), startIndex, index);
                }

                return(false);
            }

            if (text[index] == (byte)'@')
            {
                if (!DomainList.TryParse(text, ref index, endIndex, throwOnError, out route))
                {
                    if (throwOnError)
                    {
                        throw new ParseException(string.Format("Invalid route in mailbox at offset {0}", startIndex), startIndex, index);
                    }

                    return(false);
                }

                if (index + 1 >= endIndex || text[index] != (byte)':')
                {
                    if (throwOnError)
                    {
                        throw new ParseException(string.Format("Incomplete route in mailbox at offset {0}", startIndex), startIndex, index);
                    }

                    return(false);
                }

                index++;
            }

            string addrspec;

            if (!TryParseAddrspec(text, ref index, endIndex, (byte)'>', throwOnError, out addrspec))
            {
                return(false);
            }

            if (!ParseUtils.SkipCommentsAndWhiteSpace(text, ref index, endIndex, throwOnError))
            {
                return(false);
            }

            if (index >= endIndex || text[index] != (byte)'>')
            {
                if (throwOnError)
                {
                    throw new ParseException(string.Format("Unexpected end of mailbox at offset {0}", startIndex), startIndex, index);
                }

                return(false);
            }

            if (route != null)
            {
                address = new MailboxAddress(encoding, name, route, addrspec);
            }
            else
            {
                address = new MailboxAddress(encoding, name, addrspec);
            }

            index++;

            return(true);
        }
        /*
         * puts in an email address into the given node
         */
        private static void GetAddress(InternetAddress adr, string field, Node node)
        {
            if (adr == null || !(adr is MailboxAddress))
                return;

            MailboxAddress mailboxAdr = adr as MailboxAddress;
            Node nNode = new Node(field, mailboxAdr.Address);
            if (!string.IsNullOrEmpty(mailboxAdr.Name))
                nNode.Add("display-name", mailboxAdr.Name);
            node.Add(nNode);
        }
Esempio n. 11
0
 /// <summary>
 /// Tries to parse the given text into a new <see cref="MimeKit.InternetAddress"/> instance.
 /// </summary>
 /// <remarks>
 /// Parses a single <see cref="MailboxAddress"/> or <see cref="GroupAddress"/>. If the text contains
 /// more data, then parsing will fail.
 /// </remarks>
 /// <returns><c>true</c>, if the address was successfully parsed, <c>false</c> otherwise.</returns>
 /// <param name="text">The text.</param>
 /// <param name="address">The parsed address.</param>
 /// <exception cref="System.ArgumentNullException">
 /// <paramref name="text"/> is <c>null</c>.
 /// </exception>
 public static bool TryParse(string text, out InternetAddress address)
 {
     return(TryParse(ParserOptions.Default, text, out address));
 }
Esempio n. 12
0
 /// <summary>
 /// Tries to parse the given input buffer into a new <see cref="MimeKit.InternetAddress"/> instance.
 /// </summary>
 /// <remarks>
 /// Parses a single <see cref="MailboxAddress"/> or <see cref="GroupAddress"/>. If the buffer contains
 /// more data, then parsing will fail.
 /// </remarks>
 /// <returns><c>true</c>, if the address was successfully parsed, <c>false</c> otherwise.</returns>
 /// <param name="buffer">The input buffer.</param>
 /// <param name="address">The parsed address.</param>
 /// <exception cref="System.ArgumentNullException">
 /// <paramref name="buffer"/> is <c>null</c>.
 /// </exception>
 public static bool TryParse(byte[] buffer, out InternetAddress address)
 {
     return(TryParse(ParserOptions.Default, buffer, out address));
 }
Esempio n. 13
0
        /// <summary>
        /// Tries to parse the given input buffer into a new <see cref="MimeKit.InternetAddress"/> instance.
        /// </summary>
        /// <remarks>
        /// Parses a single <see cref="MailboxAddress"/> or <see cref="GroupAddress"/>. If the buffer contains
        /// more data, then parsing will fail.
        /// </remarks>
        /// <returns><c>true</c>, if the address was successfully parsed, <c>false</c> otherwise.</returns>
        /// <param name="options">The parser options to use.</param>
        /// <param name="buffer">The input buffer.</param>
        /// <param name="startIndex">The starting index of the input buffer.</param>
        /// <param name="address">The parsed address.</param>
        /// <exception cref="System.ArgumentNullException">
        /// <para><paramref name="options"/> is <c>null</c>.</para>
        /// <para>-or-</para>
        /// <para><paramref name="buffer"/> is <c>null</c>.</para>
        /// </exception>
        /// <exception cref="System.ArgumentOutOfRangeException">
        /// <paramref name="startIndex"/> is out of range.
        /// </exception>
        public static bool TryParse(ParserOptions options, byte[] buffer, int startIndex, out InternetAddress address)
        {
            if (options == null)
            {
                throw new ArgumentNullException("options");
            }

            if (buffer == null)
            {
                throw new ArgumentNullException("buffer");
            }

            if (startIndex < 0 || startIndex >= buffer.Length)
            {
                throw new ArgumentOutOfRangeException("startIndex");
            }

            int endIndex = buffer.Length;
            int index    = startIndex;

            if (!TryParse(options, buffer, ref index, endIndex, false, out address))
            {
                return(false);
            }

            if (!ParseUtils.SkipCommentsAndWhiteSpace(buffer, ref index, endIndex, false))
            {
                address = null;
                return(false);
            }

            if (index != endIndex)
            {
                address = null;
                return(false);
            }

            return(true);
        }
Esempio n. 14
0
 /// <summary>
 /// Tries to parse the given input buffer into a new <see cref="MimeKit.InternetAddress"/> instance.
 /// </summary>
 /// <remarks>
 /// Parses a single <see cref="MailboxAddress"/> or <see cref="GroupAddress"/>. If the buffer contains
 /// more data, then parsing will fail.
 /// </remarks>
 /// <returns><c>true</c>, if the address was successfully parsed, <c>false</c> otherwise.</returns>
 /// <param name="buffer">The input buffer.</param>
 /// <param name="startIndex">The starting index of the input buffer.</param>
 /// <param name="length">The number of bytes in the input buffer to parse.</param>
 /// <param name="address">The parsed address.</param>
 /// <exception cref="System.ArgumentNullException">
 /// <paramref name="buffer"/> is <c>null</c>.
 /// </exception>
 /// <exception cref="System.ArgumentOutOfRangeException">
 /// <paramref name="startIndex"/> and <paramref name="length"/> do not specify
 /// a valid range in the byte array.
 /// </exception>
 public static bool TryParse(byte[] buffer, int startIndex, int length, out InternetAddress address)
 {
     return(TryParse(ParserOptions.Default, buffer, startIndex, length, out address));
 }
Esempio n. 15
0
        internal static bool TryParseMailbox(ParserOptions options, byte[] text, int startIndex, ref int index, int endIndex, string name, int codepage, bool throwOnError, out InternetAddress address)
        {
            Encoding encoding = Encoding.GetEncoding (codepage);
            DomainList route = null;

            address = null;

            // skip over the '<'
            index++;
            if (index >= endIndex) {
                if (throwOnError)
                    throw new ParseException (string.Format ("Incomplete mailbox at offset {0}", startIndex), startIndex, index);

                return false;
            }

            if (text[index] == (byte) '@') {
                if (!DomainList.TryParse (text, ref index, endIndex, throwOnError, out route)) {
                    if (throwOnError)
                        throw new ParseException (string.Format ("Invalid route in mailbox at offset {0}", startIndex), startIndex, index);

                    return false;
                }

                if (index + 1 >= endIndex || text[index] != (byte) ':') {
                    if (throwOnError)
                        throw new ParseException (string.Format ("Incomplete route in mailbox at offset {0}", startIndex), startIndex, index);

                    return false;
                }

                index++;
            }

            string addrspec;
            if (!TryParseAddrspec (text, ref index, endIndex, (byte) '>', throwOnError, out addrspec))
                return false;

            if (!ParseUtils.SkipCommentsAndWhiteSpace (text, ref index, endIndex, throwOnError))
                return false;

            if (index >= endIndex || text[index] != (byte) '>') {
                if (options.AddressParserComplianceMode == RfcComplianceMode.Strict) {
                    if (throwOnError)
                        throw new ParseException (string.Format ("Unexpected end of mailbox at offset {0}", startIndex), startIndex, index);

                    return false;
                }
            } else {
                index++;
            }

            if (route != null)
                address = new MailboxAddress (encoding, name, route, addrspec);
            else
                address = new MailboxAddress (encoding, name, addrspec);

            return true;
        }
Esempio n. 16
0
        static bool TryParseGroup(ParserOptions options, byte[] text, int startIndex, ref int index, int endIndex, string name, int codepage, bool throwOnError, out InternetAddress address)
        {
            Encoding encoding = Encoding.GetEncoding (codepage);
            List<InternetAddress> members;

            address = null;

            // skip over the ':'
            index++;
            if (index >= endIndex) {
                if (throwOnError)
                    throw new ParseException (string.Format ("Incomplete address group at offset {0}", startIndex), startIndex, index);

                return false;
            }

            if (InternetAddressList.TryParse (options, text, ref index, endIndex, true, throwOnError, out members))
                address = new GroupAddress (encoding, name, members);
            else
                address = new GroupAddress (encoding, name);

            if (index >= endIndex || text[index] != (byte) ';') {
                if (throwOnError)
                    throw new ParseException (string.Format ("Expected to find ';' at offset {0}", index), startIndex, index);

                while (index < endIndex && text[index] != (byte) ';')
                    index++;
            } else {
                index++;
            }

            return true;
        }
Esempio n. 17
0
        /// <summary>
        /// Tries to parse the given input buffer into a new <see cref="MimeKit.InternetAddress"/> instance.
        /// </summary>
        /// <remarks>
        /// Parses a single <see cref="MailboxAddress"/> or <see cref="GroupAddress"/>. If the buffer contains
        /// more data, then parsing will fail.
        /// </remarks>
        /// <returns><c>true</c>, if the address was successfully parsed, <c>false</c> otherwise.</returns>
        /// <param name="options">The parser options to use.</param>
        /// <param name="buffer">The input buffer.</param>
        /// <param name="startIndex">The starting index of the input buffer.</param>
        /// <param name="length">The number of bytes in the input buffer to parse.</param>
        /// <param name="address">The parsed address.</param>
        /// <exception cref="System.ArgumentNullException">
        /// <para><paramref name="options"/> is <c>null</c>.</para>
        /// <para>-or-</para>
        /// <para><paramref name="buffer"/> is <c>null</c>.</para>
        /// </exception>
        /// <exception cref="System.ArgumentOutOfRangeException">
        /// <paramref name="startIndex"/> and <paramref name="length"/> do not specify
        /// a valid range in the byte array.
        /// </exception>
        public static bool TryParse(ParserOptions options, byte[] buffer, int startIndex, int length, out InternetAddress address)
        {
            if (options == null)
                throw new ArgumentNullException ("options");

            if (buffer == null)
                throw new ArgumentNullException ("buffer");

            if (startIndex < 0 || startIndex > buffer.Length)
                throw new ArgumentOutOfRangeException ("startIndex");

            if (length < 0 || length > (buffer.Length - startIndex))
                throw new ArgumentOutOfRangeException ("length");

            int endIndex = startIndex + length;
            int index = startIndex;

            if (!TryParse (options, buffer, ref index, endIndex, false, out address))
                return false;

            if (!ParseUtils.SkipCommentsAndWhiteSpace (buffer, ref index, endIndex, false)) {
                address = null;
                return false;
            }

            if (index != endIndex) {
                address = null;
                return false;
            }

            return true;
        }
Esempio n. 18
0
        static bool TryParseGroup(ParserOptions options, byte[] text, int startIndex, ref int index, int endIndex, string name, int codepage, bool throwOnError, out InternetAddress address)
        {
            Encoding encoding = Encoding.GetEncoding(codepage);
            List <InternetAddress> members;

            address = null;

            // skip over the ':'
            index++;
            if (index >= endIndex)
            {
                if (throwOnError)
                {
                    throw new ParseException(string.Format("Incomplete address group at offset {0}", startIndex), startIndex, index);
                }

                return(false);
            }

            if (InternetAddressList.TryParse(options, text, ref index, endIndex, true, throwOnError, out members))
            {
                address = new GroupAddress(encoding, name, members);
            }
            else
            {
                address = new GroupAddress(encoding, name);
            }

            if (index >= endIndex || text[index] != (byte)';')
            {
                if (throwOnError)
                {
                    throw new ParseException(string.Format("Expected to find ';' at offset {0}", index), startIndex, index);
                }

                while (index < endIndex && text[index] != (byte)';')
                {
                    index++;
                }
            }
            else
            {
                index++;
            }

            return(true);
        }
Esempio n. 19
0
 /// <summary>
 /// Tries to parse the given input buffer into a new <see cref="MimeKit.InternetAddress"/> instance.
 /// </summary>
 /// <remarks>
 /// Parses a single <see cref="MailboxAddress"/> or <see cref="GroupAddress"/>. If the buffer contains
 /// more data, then parsing will fail.
 /// </remarks>
 /// <returns><c>true</c>, if the address was successfully parsed, <c>false</c> otherwise.</returns>
 /// <param name="buffer">The input buffer.</param>
 /// <param name="startIndex">The starting index of the input buffer.</param>
 /// <param name="length">The number of bytes in the input buffer to parse.</param>
 /// <param name="address">The parsed address.</param>
 /// <exception cref="System.ArgumentNullException">
 /// <paramref name="buffer"/> is <c>null</c>.
 /// </exception>
 /// <exception cref="System.ArgumentOutOfRangeException">
 /// <paramref name="startIndex"/> and <paramref name="length"/> do not specify
 /// a valid range in the byte array.
 /// </exception>
 public static bool TryParse(byte[] buffer, int startIndex, int length, out InternetAddress address)
 {
     return TryParse (ParserOptions.Default, buffer, startIndex, length, out address);
 }
Esempio n. 20
0
        /// <summary>
        /// Tries to parse the given input buffer into a new <see cref="MimeKit.InternetAddress"/> instance.
        /// </summary>
        /// <remarks>
        /// Parses a single <see cref="MailboxAddress"/> or <see cref="GroupAddress"/>. If the buffer contains
        /// more data, then parsing will fail.
        /// </remarks>
        /// <returns><c>true</c>, if the address was successfully parsed, <c>false</c> otherwise.</returns>
        /// <param name="options">The parser options to use.</param>
        /// <param name="buffer">The input buffer.</param>
        /// <param name="address">The parsed address.</param>
        /// <exception cref="System.ArgumentNullException">
        /// <para><paramref name="options"/> is <c>null</c>.</para>
        /// <para>-or-</para>
        /// <para><paramref name="buffer"/> is <c>null</c>.</para>
        /// </exception>
        public static bool TryParse(ParserOptions options, byte[] buffer, out InternetAddress address)
        {
            if (options == null)
                throw new ArgumentNullException ("options");

            if (buffer == null)
                throw new ArgumentNullException ("buffer");

            int endIndex = buffer.Length;
            int index = 0;

            if (!TryParse (options, buffer, ref index, endIndex, false, out address))
                return false;

            if (!ParseUtils.SkipCommentsAndWhiteSpace (buffer, ref index, endIndex, false)) {
                address = null;
                return false;
            }

            if (index != endIndex) {
                address = null;
                return false;
            }

            return true;
        }
Esempio n. 21
0
		/// <summary>
		/// Determines whether the specified <see cref="MimeKit.MailboxAddress"/> is equal to the current <see cref="MimeKit.MailboxAddress"/>.
		/// </summary>
		/// <remarks>
		/// Compares two mailbox addresses to determine if they are identical or not.
		/// </remarks>
		/// <param name="other">The <see cref="MimeKit.MailboxAddress"/> to compare with the current <see cref="MimeKit.MailboxAddress"/>.</param>
		/// <returns><c>true</c> if the specified <see cref="MimeKit.MailboxAddress"/> is equal to the current
		/// <see cref="MimeKit.MailboxAddress"/>; otherwise, <c>false</c>.</returns>
		public override bool Equals (InternetAddress other)
		{
			var mailbox = other as MailboxAddress;

			if (mailbox == null)
				return false;

			return Name == mailbox.Name && Address == mailbox.Address;
		}