Exemplo n.º 1
0
        private bool TryGetBracketedLongValue(ImapList list, string name, out long retVal)
        {
            bool isOk = false;
            retVal = default(long);

            var pos = list.IndexOfString(name);
            if (pos >= 0)
            {
                isOk = long.TryParse(list.GetStringAt(pos + 1).TrimEnd(']'), out retVal);
            }

            return isOk;
        }
Exemplo n.º 2
0
        internal ImapBodyPart(string section, ImapList list)
        {
            Section = section;

            StringBuilder builder = new StringBuilder(string.Format("{0}/{1}", list.GetStringAt(0).ToLowerInvariant(), list.GetStringAt(1).ToLowerInvariant()));
            ImapList paramsList = list.GetListAt(2);

            for (int i = 0; i < paramsList.Count; i += 2)
            {
                builder.AppendFormat(";{0}=\"{1}\"", paramsList.GetStringAt(i), paramsList.GetStringAt(i + 1));
            }
			
			try
			{
            	ContentType = new ContentType(builder.ToString());
			}
			catch
			{
				ContentType = new ContentType();
			}
			
            ID = list.GetStringAt(3);
            Description = list.GetStringAt(4);
            Encoding = list.GetStringAt(5);

            int size;

            if (int.TryParse(list.GetStringAt(6), out size))
            {
                Size = size;
            }

            if (string.IsNullOrEmpty(ContentType.CharSet))
            {
                ContentType.CharSet = ASCIIEncoding.ASCII.BodyName;
            }
        }
Exemplo n.º 3
0
        internal ImapMessage(long number, ImapList list, IEnumerable<string> extensionParameterNames)
        {
            Number = number;

            int uidIndex = list.IndexOfString("UID");
            int bodyIndex = list.IndexOfString("BODYSTRUCTURE");
            int envelopeIndex = list.IndexOfString("ENVELOPE");

            if (uidIndex != -1)
            {
                Uid = long.Parse(list.GetStringAt(uidIndex + 1));
            }

            if (envelopeIndex != -1)
            {
                ImapList envelopeList = list.GetListAt(envelopeIndex + 1);

                string timestampString = envelopeList.GetStringAt(0);
                DateTime timestamp;

                if (TryParseTimestamp(timestampString, out timestamp))
                {
                    Timestamp = timestamp;
                }

                Subject = RFC2047Decoder.Parse(envelopeList.GetStringAt(1));
                Sender = ParseAddresses(envelopeList.GetListAt(2)).FirstOrDefault();
                From = ParseAddresses(envelopeList.GetListAt(3)).FirstOrDefault();
                ReplyTo = ParseAddresses(envelopeList.GetListAt(4)).FirstOrDefault();
                To = ParseAddresses(envelopeList.GetListAt(5)).ToArray();
                Cc = ParseAddresses(envelopeList.GetListAt(6)).ToArray();
                Bcc = ParseAddresses(envelopeList.GetListAt(7)).ToArray();
                ID = envelopeList.GetStringAt(8);
            }

            if (bodyIndex != -1)
            {
                ImapList bodyList = list.GetListAt(bodyIndex + 1);

                if (bodyList.Count != 0)
                {
                    BodyParts = ParseBodyParts(string.Empty, bodyList).ToArray();
                }
            }

            if (null != extensionParameterNames)
            {
                var extensionParams = new Dictionary<string, object>();

                foreach (var paramName in extensionParameterNames)
                {
                    int index = list.IndexOfString(paramName);
                    if (index != -1)
                    {
                        int valueIndex = index + 1;
                        object value = null;

                        if (list.IsStringAt(valueIndex))
                        {
                            value = list.GetStringAt(valueIndex);
                        }
                        else if (list.IsListAt(valueIndex))
                        {
                            value = list.GetListAt(valueIndex).ToBasicTypesList();
                        }

                        if (null != value)
                        {
                            extensionParams[paramName] = value;
                        }
                    }
                }

                if (extensionParams.Count > 0)
                {
                    ExtensionParameters = extensionParams;
                }
            }
        }
Exemplo n.º 4
0
 internal ImapMessage(long number, ImapList list)
     : this(number, list, null)
 { }
Exemplo n.º 5
0
        private static IEnumerable<MailAddress> ParseAddresses(ImapList list)
        {
            for (int i = 0; i < list.Count; i++)
            {
                ImapList addressList = list.GetListAt(i);

                string displayName = RFC2047Decoder.Parse(addressList.GetStringAt(0));
                string user = RFC2047Decoder.Parse(addressList.GetStringAt(2));
                string host = RFC2047Decoder.Parse(addressList.GetStringAt(3));
                
                if (!string.IsNullOrEmpty(user) &&
                    !string.IsNullOrEmpty(host))
                {
                    string addressString = string.Format("{0}@{1}",
                            user, host);
    
                    MailAddress address = null;
                    
                    try
                    {
                        if (string.IsNullOrEmpty(displayName))
                        {
                            address = new MailAddress(addressString);
                        }
                        else
                        {
                            address = new MailAddress(addressString, displayName);
                        }
                    }
                    catch (FormatException) { }
                    
                    if (address != null)
                    {
                        yield return address;
                    }
                }
            }
        }
Exemplo n.º 6
0
        private static IEnumerable<ImapBodyPart> ParseBodyParts(string section, ImapList bodyList)
        {
            if (bodyList.IsStringAt(0))
            {
                yield return new ImapBodyPart(string.IsNullOrEmpty(section) ? "1" : section, bodyList);
            }
            else
            {
                string innerSectionPrefix = string.IsNullOrEmpty(section) ? string.Empty : section + ".";

                string mutipartType = bodyList.GetStringAt(bodyList.Count - 4);

                if (!string.IsNullOrEmpty(mutipartType))
                {
                    for (int i = 0; i < bodyList.Count - 4; i++)
                    {
                        string innerSection = innerSectionPrefix + (i + 1).ToString();
                        ImapList innerBodyList = bodyList.GetListAt(i);

                        if (innerBodyList.Count != 0)
                        {
                            foreach (ImapBodyPart part in ParseBodyParts(innerSection, innerBodyList))
                            {
                                yield return part;
                            }
                        }
                    }
                }
            }
        }