[csmaild-cvs] csmaild/src/Common/Rfc2822 Address.cs,NONE,1.1 AddressGroup.cs,NONE,1.1 AddressList.cs
Brought to you by:
tamc
From: <ta...@us...> - 2003-08-01 22:02:40
|
Update of /cvsroot/csmaild/csmaild/src/Common/Rfc2822 In directory sc8-pr-cvs1:/tmp/cvs-serv25503/src/Common/Rfc2822 Added Files: Address.cs AddressGroup.cs AddressList.cs AddressMailbox.cs AddressMailboxList.cs HeaderField.cs HeaderFieldList.cs Interpreter.cs Message.cs MessageBody.cs Log Message: Updated 1.0 project files to include new stuff Added blank SMTP project Added initial rfc2822 message parser and object model (not finished but it's lightening outside ;)) Can now add messages to the messagecollection The enumerator for messagecollection works properly now Added method to mailstore provider to allow for accessing the raw message via a reader Added ability to have optional arguments on IMAP commands Fixed literal string parsing External accessors to the sequence range Very high level generic exception handling to handle all those errors I don't want to fix (and don't want them to mess up the connection) --- NEW FILE: Address.cs --- using System; namespace Common.Rfc2822 { abstract public class Address { } } --- NEW FILE: AddressGroup.cs --- using System; namespace Common.Rfc2822 { public class AddressGroup : Address { private string mDisplayName; private AddressMailboxList mList; public string DisplayName { get { return mDisplayName; } } public AddressMailboxList List { get { return mList; } } public AddressGroup(string displayName, AddressMailboxList list) { mDisplayName = displayName; mList = list; } } } --- NEW FILE: AddressList.cs --- using System; using System.Collections; using System.Text; namespace Common.Rfc2822 { public class AddressList { private ArrayList mList; public Address this[int idx] { get { return (Address)mList[idx]; } } public AddressList() { mList = new ArrayList(); } public void Add(Address addr) { mList.Add(addr); } } } --- NEW FILE: AddressMailbox.cs --- using System; namespace Common.Rfc2822 { public class AddressMailbox : Address { private string mLocalPart; private string mDisplayName; private string mHost; public string MailAddress { get { return mLocalPart + "@" + mHost; } } public string LocalPart { get { return mLocalPart; } } public string Host { get { return mHost; } } public string DisplayName { get { return mDisplayName; } } public AddressMailbox(string displayName, string localPart, string host) { mDisplayName = displayName; mLocalPart = localPart; mHost = host; } } } --- NEW FILE: AddressMailboxList.cs --- using System; using System.Collections; namespace Common.Rfc2822 { public class AddressMailboxList { private ArrayList mList; public AddressMailbox this[int idx] { get { return (AddressMailbox)mList[idx]; } } public AddressMailboxList() { mList = new ArrayList(); } public void Add(AddressMailbox box) { mList.Add(box); } } } --- NEW FILE: HeaderField.cs --- using System; namespace Common.Rfc2822 { public class HeaderField { protected string mFieldName; protected object mFieldValue; public string FieldName { get { return mFieldName; } } public string FieldValueString { get { return (string)mFieldValue; } } public AddressList FieldValueAddressList { get { return (AddressList)mFieldValue; } } public HeaderField(string fieldName, object fieldValue) { mFieldName = fieldName; mFieldValue = fieldValue; } } } --- NEW FILE: HeaderFieldList.cs --- using System; using System.Collections; using System.Text; namespace Common.Rfc2822 { public class HeaderFieldList { private ArrayList mList; private Hashtable mNameList; public HeaderField this[int idx] { get { return (HeaderField)mList[idx]; } } public HeaderField[] this[string name] { get { ArrayList lst = (ArrayList)mNameList[name]; if(lst != null) return (HeaderField[])lst.ToArray(typeof(HeaderField)); else return new HeaderField[]{}; } } public HeaderFieldList() { mList = new ArrayList(); mNameList = new Hashtable(); } public void Add(HeaderField fld) { mList.Add(fld); ArrayList lst = (ArrayList)mNameList[fld.FieldName]; if(lst == null) lst = new ArrayList(); lst.Add(fld); mNameList[fld.FieldName] = lst; } } } --- NEW FILE: Interpreter.cs --- using System; using System.IO; using System.Text; namespace Common.Rfc2822 { public class Interpreter { private static string Whitespace = " \t"; private TextReader mRawMessage; public Interpreter(string msg) : this(new StringReader(msg)) {} public Interpreter(TextReader msg) { mRawMessage = msg; } public Message Interpret() { Message msg = new Message(); InterpretHeaderList(msg.Headers); InterpretBody(msg.Body); return msg; } #region Headers private void InterpretHeaderList(HeaderFieldList hdrList) { while(true) { string hdr = mRawMessage.ReadLine(); if(hdr == null || hdr == string.Empty) // not there or empty, done with headers break; // while lines continue to start with whitespace, add 'em to the header while(Whitespace.IndexOf(Convert.ToChar(mRawMessage.Peek())) != -1) hdr += mRawMessage.ReadLine(); // we're done reading the header hdrList.Add(InterpretHeader(hdr)); } } private HeaderField InterpretHeader(string rawHeader) { int colonIdx = rawHeader.IndexOf(':'); string fieldName = rawHeader.Substring(0, colonIdx); string fieldValu = rawHeader.Substring(colonIdx+1); // TODO: parse/validate/correct the header based on its name switch(fieldName.ToLower()) { case "date": return new HeaderField(fieldName, InterpretDateTime(fieldValu)); case "to": return new HeaderField(fieldName, InterpretAddressList(fieldValu)); default: return new HeaderField(fieldName, fieldValu); } } #endregion #region Addresses private AddressList InterpretAddressList(string rawList) { AddressList list = new AddressList(); int startIdx = 0; while(true) { int idx = rawList.IndexOfAny(new char[]{':', ','}, startIdx); if(idx == -1) { list.Add(InterpretAddressMailbox(rawList.Substring(startIdx))); break; } else if(rawList[idx] == ',') list.Add(InterpretAddressMailbox(rawList.Substring(startIdx, idx-startIdx))); else { idx = rawList.IndexOf(';', idx+1); list.Add(InterpretAddressGroup(rawList.Substring(startIdx, idx-startIdx))); } startIdx = idx+1; // set the next starting point to the character after the seperator } return list; } private AddressMailbox InterpretAddressMailbox(string rawMailbox) { int atIdx = rawMailbox.IndexOf('@'); int spIdx = rawMailbox.LastIndexOf(' ', atIdx); string host = rawMailbox.Substring(atIdx+1); string displayName = rawMailbox.Substring(0, spIdx); string localPart = rawMailbox.Substring(spIdx+1, atIdx-spIdx); return new AddressMailbox(displayName, localPart, host); } private AddressGroup InterpretAddressGroup(string rawGroup) { int colonIdx = rawGroup.IndexOf(':'); string displayName = rawGroup.Substring(0, colonIdx); string addrList = rawGroup.Substring(colonIdx+1); AddressMailboxList list = new AddressMailboxList(); InterpretAddressMailboxList(addrList, list); return new AddressGroup(displayName, list); } private void InterpretAddressMailboxList(string rawList, AddressMailboxList list) { string[] addrs = rawList.Split(','); foreach(string addr in addrs) list.Add(InterpretAddressMailbox(addr)); } #endregion #region Date Time private string InterpretDateTime(string rawDateTime) { // validate and correct, possibly also convert to System.DateTime for easy use outside of Rfc2822 return rawDateTime; } #endregion #region Body private void InterpretBody(MessageBody bdy) { } #endregion } } --- NEW FILE: Message.cs --- using System; using System.IO; using System.Text; namespace Common.Rfc2822 { public class Message { private HeaderFieldList mHeaders; private MessageBody mBody; public HeaderFieldList Headers { get { return mHeaders; } } public MessageBody Body { get { return mBody; } } public Message() { mHeaders = new HeaderFieldList(); mBody = new MessageBody(); } } } --- NEW FILE: MessageBody.cs --- using System; using System.IO; namespace Common.Rfc2822 { public class MessageBody { public MessageBody() { } } } |