Esempio n. 1
0
 /// <summary>
 /// Parse a rfc 2822 header field with parameters
 /// </summary>
 /// <param name="field">field name</param>
 /// <param name="fieldbody">field body to parse</param>
 /// <returns>A <see cref="System.Collections.Specialized.StringDictionary" /> from the parsed field body</returns>
 public static System.Collections.Specialized.StringDictionary parseHeaderFieldBody(System.String field, System.String fieldbody)
 {
     if (fieldbody == null)
     {
         return(null);
     }
     // FIXME: rewrite parseHeaderFieldBody to being regexp based.
     fieldbody = MimeTools.uncommentString(fieldbody);
     System.Collections.Specialized.StringDictionary fieldbodycol = new System.Collections.Specialized.StringDictionary();
     System.String[] words = fieldbody.Split(new Char[] { ';' });
     if (words.Length > 0)
     {
         fieldbodycol.Add(field.ToLower(), words[0].ToLower());
         for (int i = 1; i < words.Length; i++)
         {
             System.String[] param = words[i].Trim(new Char[] { ' ', '\t' }).Split(new Char[] { '=' }, 2);
             if (param.Length == 2)
             {
                 param[0] = param[0].Trim(new Char[] { ' ', '\t' });
                 param[1] = param[1].Trim(new Char[] { ' ', '\t' });
                 if (param[1].StartsWith("\"") && !param[1].EndsWith("\""))
                 {
                     do
                     {
                         param[1] += ";" + words[++i];
                     } while  (!words[i].EndsWith("\"") && i < words.Length);
                 }
                 fieldbodycol.Add(param[0], MimeTools.parserfc2047Header(param[1].TrimEnd(';').Trim('\"')));
             }
         }
     }
     return(fieldbodycol);
 }
Esempio n. 2
0
        /// <summary>
        /// Parse a rfc 2822 name-address specification. rfc 2822 section 3.4
        /// </summary>
        /// <param name="from">address</param>
        /// <param name="part">1 is display-name; 2 is addr-spec</param>
        /// <returns>the requested <see cref="System.String" /></returns>
        public static System.String parseFrom(System.String from, int part)
        {
            int pos;

            if (from == null || from.Length < 1)
            {
                return(System.String.Empty);
            }
            switch (part)
            {
            case 1:
                pos  = from.LastIndexOf('<');
                pos  = (pos < 0)?from.Length:pos;
                from = from.Substring(0, pos).Trim();
                from = MimeTools.parserfc2047Header(from);
                return(from);

            case 2:
                pos = from.LastIndexOf('<') + 1;
                return(from.Substring(pos, from.Length - pos).Trim(new char[] { '<', '>', ' ' }));
            }
            return(from);
        }