[Csmail-patches] CVS: csmail/src/CSMail ChangeLog,1.57,1.58 Constants.cs,1.6,1.7 EMailAddress.cs,1.6
Status: Pre-Alpha
Brought to you by:
mastergaurav
From: Gaurav V. <mas...@us...> - 2002-10-25 17:06:09
|
Update of /cvsroot/csmail/csmail/src/CSMail In directory usw-pr-cvs1:/tmp/cvs-serv24268 Modified Files: ChangeLog Constants.cs EMailAddress.cs EMailAddressList.cs Log Message: 2002-10-25 * Constants.cs : RFC822Phrase, : RFC822PhraseNoDot, : RFC822PhraseNoDotAt - New constants. * EMailAddres.cs : Validate(string, bool) - Implemented. * EMailAddressList.cs : Parse(string, bool, bool) - Completed. 2002-10-24 Gaurav Vaish <mastergaurav AT users DOT sf DOT net> * EMailAddressList.cs : Parse(string, bool, bool) - Partial implementation. Index: ChangeLog =================================================================== RCS file: /cvsroot/csmail/csmail/src/CSMail/ChangeLog,v retrieving revision 1.57 retrieving revision 1.58 diff -u -r1.57 -r1.58 --- ChangeLog 17 Oct 2002 10:34:14 -0000 1.57 +++ ChangeLog 25 Oct 2002 08:52:12 -0000 1.58 @@ -1,4 +1,17 @@ +2002-10-25 Gaurav Vaish <mastergaurav AT users DOT sf DOT net> + + * Constants.cs : RFC822Phrase, + : RFC822PhraseNoDot, + : RFC822PhraseNoDotAt - New constants. + * EMailAddres.cs : Validate(string, bool) - Implemented. + * EMailAddressList.cs : Parse(string, bool, bool) - Completed. + +2002-10-24 Gaurav Vaish <mastergaurav AT users DOT sf DOT net> + + * EMailAddressList.cs : Parse(string, bool, bool) + - Partial implementation. + 2002-10-17 Gaurav Vaish <mastergaurav AT users DOT sf DOT net> * AddressException.cs : Added new exception class. Index: Constants.cs =================================================================== RCS file: /cvsroot/csmail/csmail/src/CSMail/Constants.cs,v retrieving revision 1.6 retrieving revision 1.7 diff -u -r1.6 -r1.7 --- Constants.cs 4 Oct 2002 11:14:42 -0000 1.6 +++ Constants.cs 25 Oct 2002 08:52:13 -0000 1.7 @@ -60,8 +60,11 @@ public const char CR = '\r'; public const char LF = '\n'; - public const string RFC822Delimiters = "()<>@,;:\\\"\t []."; - public const string MIMEDelimiters = "()<>@,;:\\\"\t []/?="; + public const string RFC822Delimiters = "()<>@,;:\\\"\t []."; + public const string RFC822Phrase = "()<>@,;:\\\"[]."; + public const string RFC822PhraseNoDot = "()<>@,;:\\\"[]"; + public const string RFC822PhraseNoDotAt = "()<>,;:\\\"[]"; + public const string MIMEDelimiters = "()<>@,;:\\\"\t []/?="; /// <summary> /// Key that holds class-name for store service provider. Index: EMailAddress.cs =================================================================== RCS file: /cvsroot/csmail/csmail/src/CSMail/EMailAddress.cs,v retrieving revision 1.6 retrieving revision 1.7 diff -u -r1.6 -r1.7 --- EMailAddress.cs 17 Oct 2002 10:34:14 -0000 1.6 +++ EMailAddress.cs 25 Oct 2002 08:52:13 -0000 1.7 @@ -26,6 +26,11 @@ private string host; private string name; + public EMailAddress(string email) + { + Parse(email); + } + /// <summary> /// Creates an instance using userid and hostname. /// </summary> @@ -50,6 +55,80 @@ public EMailAddress(string name, string user, string host): this(user, host) { this.name = name; + } + + private void Parse(string email) + { + string[] parts = email.Split(new char[] { '@' }); + if(parts.Length != 2) + throw new AddressException("[EMailAddress] Invalid EMail"); + user = parts[0]; + host = parts[1]; + } + + protected internal static void ValidateAddress(string address, + bool isEmail) + { + if(address.IndexOf('"') >= 0) + return; + int i = 0; + if(isEmail) + { + int j; + for(i = 0; (j = address.IndexOfAny(",:".ToCharArray(), + i)) >= 0; i = j + 1) + { + if(address[i] != '@') + throw new AddressException("[EMailAddressList]" + + " Illegal route-address", address, i); + if(address[j] == ':') + { + i = j + 1; + break; + } + } + } + int k = 0; + string tuser = null; + string thost = null; + k = address.IndexOf('@'); + if(k < 0) + throw new AddressException("[EMailAddressList] Missing" + + " final @domain", address, 0); + if(k == i) + throw new AddressException("[EMailAddressList] Missing" + + " userid", address, 0); + if(k == (address.Length - 1)) + throw new AddressException("[EMailAddressList] Missing" + + " domain", address, 0); + tuser = address.Substring(i, k - i); + thost = address.Substring(k + 1); + if(address.IndexOfAny(" \t\r\n".ToCharArray()) >= 0) + throw new AddressException("[EMailAddressList] Illegal" + + " whitespace in address", address, 0); + if(tuser.IndexOfAny(Constants.RFC822PhraseNoDot.ToCharArray()) >= 0) + throw new AddressException("[EMailAddressList] Illegal" + + " character in userid", address, 0); + if(thost.IndexOf('[') < 0 && + thost.IndexOfAny(Constants.RFC822PhraseNoDot.ToCharArray()) >= 0) + throw new AddressException("[EMailAddressList] Illegal" + + " character in domain", address, 0); + } + + public string EMail + { + get + { + if(user == null) + return String.Empty; + if(host == null) + return user; + return user + '@' + host; + } + set + { + Parse(value); + } } /// <summary> Index: EMailAddressList.cs =================================================================== RCS file: /cvsroot/csmail/csmail/src/CSMail/EMailAddressList.cs,v retrieving revision 1.5 retrieving revision 1.6 diff -u -r1.5 -r1.6 --- EMailAddressList.cs 17 Oct 2002 10:34:14 -0000 1.5 +++ EMailAddressList.cs 25 Oct 2002 08:52:13 -0000 1.6 @@ -80,7 +80,7 @@ { return Parse(headerValue, strict, false); } - + public static EMailAddress[] ParseHeader(string headerValue, bool strict) { @@ -91,19 +91,20 @@ private static EMailAddress[] Parse(string headerValue, bool strict, bool validate) { - int i1 = -1; - int j1 = -1; + int nameStartIndex = -1; // i1 + int addressStartIndex = -1; // j1 int len = headerValue.Length; - - bool flag2 = false; - bool flag3 = false; // addressRead - bool startTag = false; // flag4 - + + bool isEmailPart = false; + bool startTag = false; + bool restart = false; + ArrayList addressList = new ArrayList(); - int j = -1; - int i = -1; + EMailAddress toAdd = null; + int quotedBraceIndex = -1; // j + int quoteIndex = -1; // i int index; - + for(index = 0; index < len; index++) { char c = headerValue[index]; @@ -113,12 +114,12 @@ case '\n' : break; case '\r' : break; case ' ' : break; - + case '(' : startTag = true; - if(i >= 0 && j == -1) - j = index; - if(i1 == -1) - i1 = index + 1; + if(quoteIndex >= 0 && quotedBraceIndex == -1) + quotedBraceIndex = index; + if(nameStartIndex == -1) + nameStartIndex = index + 1; index++; int unbalance; for(unbalance = 1; index < len && @@ -135,14 +136,219 @@ default : break; } } - + if(unbalance > 0) throw new AddressException("[EMailAddressList] Missing ')'", headerValue, index); - throw new NotImplementedException(); + index--; + if(addressStartIndex == -1) + addressStartIndex = index; + break; + case ')': throw new AddressException("[EMailAddressList] Missing ')'", + headerValue, index); + case '<': startTag = true; + if(isEmailPart) + throw new AddressException("Extra route-address", + headerValue, index); + nameStartIndex = quoteIndex; + if(nameStartIndex >= 0) + { + addressStartIndex = index; + } + quoteIndex = index+1; + bool isQuoted = false; + restart = false; + while(true) + { + for(index++; index < len; index++) + { + switch(headerValue[index]) + { + case '\\': index++; + restart = false; + break; + case '"' : isQuoted = !isQuoted; + restart = false; + break; + case '>' : if(!isQuoted) + { + restart = true; + } + break; + default : restart = false; + break; + } + if(restart) + break; + } + if(!restart) + break; + } + + if(index >= len) + if(isQuoted) + throw new AddressException("[EMailAddressList]" + + " Missing '\"'", headerValue, index); + else + throw new AddressException("[EMailAddressList]" + + " Missing '>'", headerValue, index); + isEmailPart = true; + quotedBraceIndex = index; + break; + case '>': throw new AddressException("[EMailAddressList]" + + " Missing '<'", headerValue, index); + case '"': startTag = true; + if(quoteIndex == -1) + quoteIndex = index; + restart = false; + while(true) + { + for(index++; index < len; index++) + { + switch(headerValue[index]) + { + case '"' : restart = true; + break; + case '\\': index++; + goto default; + default : index++; + restart = false; + break; + } + if(restart) + break; + } + if(!restart) + break; + } + if(index >= len) + throw new AddressException("[EMailAddressList]" + + " Missing '\"'", headerValue, index); + break; + case '[': startTag = true; + restart = false; + while(true) + { + for(index++; index < len; index++) + { + switch(headerValue[index]) + { + case ']' : restart = true; + break; + case '\\': restart = false; + index++; + goto default; + default : index++; + break; + } + if(restart) + break; + } + if(!restart) + break; + } + + if(index >= len) + throw new AddressException("[EMailAddressList]" + + "Missing ']'", headerValue, index); + break; + case ':': throw new AddressException("[EMailAddressList]" + + "Looks like a news group", headerValue, index); + case ';': if(quoteIndex == -1) + { + quoteIndex = index; + } + if(nameStartIndex == -1) + nameStartIndex = index; + quotedBraceIndex = index + 1; + string email = headerValue.Substring(quoteIndex, + quotedBraceIndex - quoteIndex).Trim(); + toAdd = new EMailAddress(email); + addressList.Add(toAdd); + isEmailPart = false; + quoteIndex = quotedBraceIndex = -1; + break; + case ',': if(quoteIndex == -1) + { + isEmailPart = false; + startTag = false; + quotedBraceIndex = -1; + break; + } + if(quotedBraceIndex == -1) + quotedBraceIndex = index; + string actVal = headerValue.Substring(quoteIndex, + index - quoteIndex).Trim(); + if(startTag || strict || validate) + { + if(strict || validate) + EMailAddress.ValidateAddress(actVal, isEmailPart); + toAdd = new EMailAddress(actVal); + if(nameStartIndex >= 0) + { + throw new NotImplementedException("[EMailAddressList]" + + " Don't know yet, how to unquote"); + // reset nameStartIndex and addressStartIndex + } + addressList.Add(toAdd); + toAdd = null; + } else + { + string[] allAddrs = actVal.Split(new char[] + {' ', '\t', '\n', '\r', '\f'}); + foreach(string currentAddress in allAddrs) + { + EMailAddress.ValidateAddress(currentAddress, false); + toAdd = new EMailAddress(currentAddress); + addressList.Add(toAdd); + } + } + isEmailPart = false; + startTag = false; + quoteIndex = quotedBraceIndex = -1; + break; + default: if(quoteIndex == -1) + { + quoteIndex = index; + } + break; } } - throw new NotImplementedException(); + + if(quoteIndex >= 0) + { + if(quotedBraceIndex == -1) + quotedBraceIndex = index; + string actVal = headerValue.Substring(quoteIndex, quotedBraceIndex - 1).Trim(); + if(startTag || strict || validate) + { + if(strict || validate) + EMailAddress.ValidateAddress(actVal, isEmailPart); + toAdd = new EMailAddress(actVal); + if(nameStartIndex >= 0) + { + throw new NotImplementedException("[EMailAddressList]" + + " Don't know yet, how to unquote"); + // reset nameStartIndex and addressStartIndex + } + addressList.Add(toAdd); + toAdd = null; + } else + { + string[] allAddrs = actVal.Split(new char[] + { ' ', '\t', '\n', '\r', '\f' }); + foreach(string currentAddress in allAddrs) + { + EMailAddress.ValidateAddress(currentAddress, false); + toAdd = new EMailAddress(currentAddress); + addressList.Add(toAdd); + } + } + } + + EMailAddress[] retVal = new EMailAddress[addressList.Count]; + addressList.CopyTo(retVal, 0); + return retVal; } /// <summary> |