/// <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); }
/// <summary> /// Parse a rfc 2822 date and time specification. rfc 2822 section 3.3 /// </summary> /// <param name="date">rfc 2822 date-time</param> /// <returns>A <see cref="System.DateTime" /> from the parsed header body</returns> public static System.DateTime parseDate(System.String date) { if (date == null || date.Equals(System.String.Empty)) { return(System.DateTime.MinValue); } System.DateTime msgDateTime; date = MimeTools.uncommentString(date); msgDateTime = new System.DateTime(0); try { // TODO: Complete the list date = date.Replace("UT", "+0000"); date = date.Replace("GMT", "+0000"); date = date.Replace("EDT", "-0400"); date = date.Replace("EST", "-0500"); date = date.Replace("CDT", "-0500"); date = date.Replace("MDT", "-0600"); date = date.Replace("MST", "-0600"); date = date.Replace("EST", "-0700"); date = date.Replace("PDT", "-0700"); date = date.Replace("PST", "-0800"); date = date.Replace("AM", System.String.Empty); date = date.Replace("PM", System.String.Empty); int rpos = date.LastIndexOfAny(new Char[] { ' ', '\t' }); if (rpos > 0 && rpos != date.Length - 6) { date = date.Substring(0, rpos + 1) + "-0000"; } date = date.Insert(date.Length - 2, ":"); msgDateTime = DateTime.ParseExact(date, _date_formats, System.Globalization.CultureInfo.CreateSpecificCulture("en-us"), System.Globalization.DateTimeStyles.AllowInnerWhite); #if LOG } catch (System.Exception e) { if (log.IsErrorEnabled) { log.Error(System.String.Concat("Error parsing date: [", date, "]"), e); } #else } catch (System.Exception) { #endif msgDateTime = new System.DateTime(0); } return(msgDateTime); }
/// <summary> /// Returns the requested header field body. /// </summary> /// <param name="name">Header field name</param> /// <param name="defaultvalue">Value to return when the requested field is not present</param> /// <param name="uncomment"><b>true</b> to uncomment using <see cref="MimeTools.uncommentString" />; <b>false</b> to return the value unchanged.</param> /// <param name="rfc2047decode"><b>true</b> to decode <see cref="MimeTools.rfc2047decode" />; <b>false</b> to return the value unchanged.</param> /// <returns>Header field body</returns> public System.String GetHeaderField(System.String name, System.String defaultvalue, bool uncomment, bool rfc2047decode) { System.String tmp = this.GetProperty(name); if (tmp == null) { tmp = defaultvalue; } else { if (uncomment) { tmp = MimeTools.uncommentString(tmp); } if (rfc2047decode) { tmp = MimeTools.rfc2047decode(tmp); } } return(tmp); }