diff -r 000000000000 -r 72b543305e3a email/pop3andsmtpmtm/clientmtms/src/MIUTPARS.CPP --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/email/pop3andsmtpmtm/clientmtms/src/MIUTPARS.CPP Thu Dec 17 08:44:11 2009 +0200 @@ -0,0 +1,796 @@ +// Copyright (c) 1998-2009 Nokia Corporation and/or its subsidiary(-ies). +// All rights reserved. +// This component and the accompanying materials are made available +// under the terms of "Eclipse Public License v1.0" +// which accompanies this distribution, and is available +// at the URL "http://www.eclipse.org/legal/epl-v10.html". +// +// Initial Contributors: +// Nokia Corporation - initial contribution. +// +// Contributors: +// +// Description: +// + +#include +#include "MIUTPARS.H" + +/** Tests if the specified string contains a valid email address. + +@param aAddress String to test +@return ETrue if aAddress contains one valid email address. The email address +can contain aliases and comments. EFalse otherwise. +*/ +EXPORT_C TBool TImMessageField::ValidInternetEmailAddress(const TDesC16& aAddress) + { + TInt leftEdge=0,rightEdge=0; + TBool result = (isValidEmailAddress(aAddress, leftEdge, rightEdge)); + result &= hasAngledBrackets(aAddress); + return result; + } + +/** Tests if the specified string contains a valid email address. + +@param aAddress String to test +@param rFirstBadCharPos On return, if the test has succeeded, the character position in +aAddress where the email address begins. If the test fails, the position of the +character that caused the test to fail. + +@return ETrue if aAddress contains one valid email address; EFalse otherwise. +*/ +EXPORT_C TBool TImMessageField::ValidInternetEmailAddress(const TDesC16& aAddress, TInt& rFirstBadCharPos) + { + TInt discardPos; + TBool result = (isValidEmailAddress(aAddress, rFirstBadCharPos, discardPos)); + result &= hasAngledBrackets(aAddress); + return result; + } + +/** Tests if the specified string contains a valid email address. + +@param aAddress String to test +@param rFirstChar On return, if the test has succeeded, the character position in +aAddress where the email address begins. If the test fails, the position of the +character that caused the test to fail. +@param rLastChar On return, if the test has succeeded, the character position in +aAddress where the email address ends. +@return ETrue if aAddress contains one valid email address; EFalse otherwise. +*/ +EXPORT_C TBool TImMessageField::ValidInternetEmailAddress(const TDesC16& aAddress, TInt& rFirstChar, TInt& rLastChar) + { + TBool result = (isValidEmailAddress(aAddress, rFirstChar, rLastChar)); + result &= hasAngledBrackets(aAddress); + return result; + } + +/** Tests if the specified string contains a valid email address. + +@param aAddress String to test +@param rFirstChar On return, if the test has succeeded, the character position in +aAddress where the email address begins. If the test fails, the position of the +character that caused the test to fail. +@param rLastChar On return, if the test has succeeded, the character position in +aAddress where the email address ends. +@param rFirstBadCharPos On return, if the test has succeeded, KErrNotFound. If the test +fails, the position of the character that caused the test to fail. +@return ETrue if aAddress contains one valid email address; EFalse otherwise. +*/ +EXPORT_C TBool TImMessageField::ValidInternetEmailAddress(const TDesC16& aAddress, TInt& rFirstChar, TInt& rLastChar, TInt& rFirstBadCharPos) + { + TBool result = isValidEmailAddress(aAddress, rFirstChar, rLastChar); + // if test failed, then aLeft is the first bad character found in the email address + rFirstBadCharPos = (result) ? KErrNotFound : rFirstChar; + result &= hasAngledBrackets(aAddress); + return result; + } + +/** Tests if the specified character is valid (in ASCII range 32-127) for use in an email address. + +@param aChar Character to test +@return ETrue if test succeeds; EFalse otherwise. +*/ +EXPORT_C TBool TImMessageField::ValidInternetEmailAddressChar(const TChar& aChar) + { + return isValidChar(aChar); + } + +/** This function always returns true, so can be ignored. + +@param aSubjectLine Unused +@return Always ETrue +*/ +EXPORT_C TBool TImMessageField::ValidSubjectLine(const TDesC16& /*aSubjectLine*/) + { + return ETrue; + } + +/** This function always returns true, so can be ignored. + +@param aSubjectLine Unused +@param rFirstBadCharPos Unused +@return Always ETrue +*/ +EXPORT_C TBool TImMessageField::ValidSubjectLine(const TDesC16& /*aSubjectLine*/, TInt& /*rFirstBadCharPos*/) + { + return ETrue; + } + +/** This function always returns true, so can be ignored. + +@param aChar Unused +@return Always ETrue +*/ +EXPORT_C TBool TImMessageField::ValidSubjectLineChar(const TChar& /*aChar*/) + { + // return ( (aChar>=0 &&aChar<128) || (aChar>=160 &&aChar<256)); + // All chars are valid UNICODE world. + return ETrue; + } + +/** This function always returns true, so can be ignored. + +@param aAliasName Unused +@return Always ETrue +*/ +EXPORT_C TBool TImMessageField::ValidAliasName(const TDesC16& /*aAliasName*/) + { + return ETrue; + } + +/** This function always returns true, so can be ignored. + +@param aAliasName Unused +@param rFirstBadCharPos Unused +@return Always ETrue +*/ +EXPORT_C TBool TImMessageField::ValidAliasName(const TDesC16& /*aAliasName*/, TInt& /*rFirstBadCharPos*/) + { + return ETrue; + } + +/** Gets a valid email address, if one exists, from the specified string. + +@param aAddress String to parse +@return If a valid email address was found in aAddress, a pointer descriptor to the +address part (without surrounding aliases or comments). If an address could not be found, +the whole of the original aAddress string is returned. +*/ +EXPORT_C TPtrC16 TImMessageField::GetValidInternetEmailAddressFromString(const TDesC16& aAddress) + { + // returns Email address (without the enclosing angled brackets) - if one can be found, + // otherwise return whole string as best-guess + + // Best-fit = "" + TInt LeftEdge=0,RightEdge=0; + if (isLegalEmailAddress(aAddress,LeftEdge,RightEdge)) + { + // NB removes surrounding angled brackets + return aAddress.Mid(1+LeftEdge,RightEdge-LeftEdge-1); + } + + // Next best-fit = "name@address" + if (isValidEmailAddress(aAddress, LeftEdge, RightEdge)) + { + return aAddress.Mid(LeftEdge,RightEdge-LeftEdge+1); + } + + // default-fit = "whole contents of aDesc" + return aAddress; + } + +/** Gets a valid email address, if one exists, from the specified string. + +This does not differ in functionality from the other overload of +GetValidInternetEmailAddressFromString(). + +@param aAddress String to parse +@param rError On return, always KErrNone +@return If a valid email address was found in aAddress, a pointer descriptor to the +address substring (without surrounding aliases or comments). If an address could not be found, +the whole of the original aAddress string is returned. +*/ +EXPORT_C TPtrC16 TImMessageField::GetValidInternetEmailAddressFromString(const TDesC16& aAddress, TInt& rError) + { + // NOTE: This needs a proper implementation! + GetValidInternetEmailAddressFromString(aAddress); + rError = KErrNone; + return aAddress; + } + + +/** This function overload is not implemented. + +@param aAddress String to parse +@param rFirstChar Unused +@param rLastChar Unused +@return Original aAddress string +*/ +EXPORT_C TPtrC16 TImMessageField::GetValidInternetEmailAddressFromString(const TDesC16& aAddress, TInt& /*rFirstChar*/, TInt& /*rLastChar*/) + { + return aAddress; + } + +/** This function overload is not implemented. + +@param aAddress String to parse +@param rFirstChar Unused +@param rLastChar Unused +@param rError Unused +@return Original aAddress string +*/ +EXPORT_C TPtrC16 TImMessageField::GetValidInternetEmailAddressFromString(const TDesC16& aAddress, TInt& /*rFirstChar*/, TInt& /*rLastChar*/, TInt& /*rError*/) + { + return aAddress; + } + +/** Gets a valid alias, if one exists, from the specified string. + +A valid alias is defined here as being any substring that appears +to the left of a legal email address. + +This overload supplies an error code in rError if an alias was not found. + +@param aAddress String to parse +@param rError On return, KErrNone if a valid alias was found; otherwise, KErrNotFound. +@return If a valid alias was found in aAddress, a pointer descriptor to the +alias substring. If a valid alias could not be found, the whole of the original +aAddress string is returned. +*/ +EXPORT_C TPtrC16 TImMessageField::GetValidAlias(const TDesC16& aAddress, TInt& rError) + { + // the alias part of the string is defined here as being any + // substring which appears to the left of a legal email address. + // If the string contains no Email address or no alias then the full address is returned + // and rError is set to KErrNotFound + rError=KErrNone; + TInt LeftEdge=0,RightEdge=0; + if (isLegalEmailAddress(aAddress,LeftEdge,RightEdge)) + { + // skip space between alias and left edge of email address. + LeftEdge--; + while (LeftEdge>=0 && aAddress[LeftEdge]==' ') + { + LeftEdge--; + } + + // left edge now points to the last character of the alias, + // or -1 if there were just spaces before the email address + if(LeftEdge>=0) + { + // set right edge of alias to left edge of email address minus spaces + RightEdge=LeftEdge; + + // find first non-space character at start of descriptor + LeftEdge=0; + while (LeftEdge=LeftEdge) + { + return aAddress.Mid(LeftEdge,RightEdge-LeftEdge+1); + } + } + } + rError=KErrNotFound; + return aAddress; + } + +/** Gets a valid alias, if one exists, from the specified string. + +A valid alias is defined here as being any substring that appears +to the left of a legal email address. + +@param aAddress String to parse +@return If a valid alias was found in aAddress, a pointer descriptor to the +alias substring. If a valid alias could not be found, the whole of the original +aAddress string is returned. +*/ +EXPORT_C TPtrC16 TImMessageField::GetValidAlias(const TDesC16& aAddress) + { + TInt discardedInt; + return GetValidAlias(aAddress,discardedInt); + } + +/** Gets a valid comment, if one exists, from the specified string. + +A valid comment is found if the string contains both: 1) a legal email address; and +2) a comment (text surrounded by parentheses). + +This overload supplies an error code in rError if a comment was not found. + +@param aAddress String to parse +@param rError On return, KErrNone if a valid alias was found; otherwise, KErrNotFound. +@return If a valid comment was found in aAddress, a pointer descriptor to the +comment substring. If a valid comment could not be found, the whole of the original +aAddress string is returned. +*/ +EXPORT_C TPtrC16 TImMessageField::GetValidComment(const TDesC16& aAddress, TInt& rError) + { + // Returns text contained within round brackets + + // Note that string must contain a Legal email address + rError=KErrNone; + TInt LeftEdge=0,RightEdge=0; + if (isLegalEmailAddress(aAddress,LeftEdge,RightEdge)) + { + if (isSurroundedByRoundBrackets(aAddress,LeftEdge,RightEdge)) + { + LeftEdge++; + return aAddress.Mid(LeftEdge,RightEdge-LeftEdge); + } + } + rError=KErrNotFound; + return aAddress; + } + +/** Gets a valid comment, if one exists, from the specified string. + +A valid comment is found if the string contains both: 1) a legal email address; and +2) a comment (text surrounded by parentheses). + +@param aAddress String to parse +@return If a valid comment was found in aAddress, a pointer descriptor to the +comment substring. If a valid comment could not be found, the whole of the original +aAddress string is returned. +*/ +EXPORT_C TPtrC16 TImMessageField::GetValidComment(const TDesC16& aAddress) + { + TInt discardedInt; + return GetValidComment(aAddress,discardedInt); + } + +TBool TImMessageField::hasAngledBrackets(const TDesC16& anAddress) + { + TInt leftBracketPos=anAddress.LocateReverse('<'); + TInt rightBracketPos=0; + TBool result = ETrue; + if (leftBracketPos==KErrNotFound) + { + rightBracketPos=anAddress.LocateReverse('>'); + result &= (rightBracketPos==KErrNotFound); + } + else + { + rightBracketPos=anAddress.LocateReverse('>'); + result &= (rightBracketPos>leftBracketPos); + } + return result; + } + +/** Counts email addresses in string, returns true if specified max is reached. + +This function is provided to allow truncation of strings of email addresses to be +included in the body header of reply/forwarded emails, hence only a simple check +is made on the validity of contained email addresses. + +@param aDesc String to parse +@param aLimit The number of emails to identify before truncation should occur. +@param aLastChar The position in the passed string of the closing angled bracket + of the last email address before truncation should occur. +@return ETrue if the truncation limit reached, EFalse otherwise. +*/ +TBool TImMessageField::TruncateAddressString(const TDesC16& aDesc, TInt aLimit, TInt& aLastChar) + { + TInt firstChar=0; + TInt startPos=0; + TInt atPos; + TInt length=aDesc.Length(); + TInt count=0; + do { + TPtrC16 rightString=aDesc.Right(length-startPos); + atPos=rightString.Locate('@'); + if (atPos>0) + { + TBool valid = isSurroundedByAngledBrackets(rightString,firstChar,aLastChar,atPos); + if (valid) + { + ++count; + if (count<=aLimit) + { + firstChar+=startPos; + aLastChar+=startPos; + startPos=aLastChar+1; + } + } + else + { + startPos+=atPos+1; // allows loop to skip "@" in aliases + } + } + } + while (atPos>=0 && startPos0) + { + TBool valid = isSurroundedByAngledBrackets(rightString,rFirstChar,rLastChar,atPos); + if (valid) + { + if (rLastChar>rFirstChar) + { + if (rFirstChar + 1 < rLastChar) + { + TPtrC rightStringWithoutAngles = rightString.Mid(rFirstChar+1, rLastChar-rFirstChar-1); + if (isValidRoutedEmailAddress(rightStringWithoutAngles)) + return ETrue; + } + } + } + + if (valid && atPos>rFirstChar+1 && atPos=0 && startPos1 && rFirstCharatPos) // string must be at least 3 chars long, eg "a@c" and must not contain an '@' char at either end + { + rFirstChar+=startPos; + rLastChar+=startPos; + TInt discardedChar; + TPtrC16 address=aAddress.Mid(rFirstChar,rLastChar-rFirstChar+1); + TInt result=address.LocateReverse('@'); // search for any more bogus '@' characters in remainder of email address + +#if defined(__NO_EMAIL_ADDRESS_CHECKING__) + discardedChar=0; + return ETrue; +#else + return ((rFirstChar+result==atPos+startPos) && isValidEmailString(address,discardedChar)); // finally, check for multiple '@' chars, and also for illegal chars in substring +#endif + } + } + startPos+=atPos+1; + } + while (atPos>=0 && startPos0 && aAddress[rFirstChar]!=' ') + { + rFirstChar--; + } + if (rFirstChar>=0 && aAddress[rFirstChar]==' ') + { + rFirstChar++; // point to 1st character in substring + } + if (rFirstCharz@" + TInt left,right; + if (isSurroundedByAngledBrackets(aAddress,left,right,atPos)) + { + rFirstChar=left+1; + rLastChar=right-1; + } + return (aAddress[LeftEdge]!='@' && aAddress[RightEdge]!='@'); // email address mustn't start or end with '@' character + } + } + return EFalse; + } + +TBool TImMessageField::isEnclosedSubString(const TDesC16& aDesc, const TChar& aLeftBracket, const TChar& aRightBracket,TInt& aLeftPos, TInt& aRightPos) + { + //Checking if the email address has "< " in the username part : ex: + aLeftPos=aDesc.LocateReverse(aLeftBracket); + if (aLeftPos>=0) + { + TInt aPos = aLeftPos - 1 ; + aRightPos=aDesc.LocateReverse(aRightBracket); + if(aPos >= 0) + { + TPtrC16 aChar = aDesc.Mid(aPos);// To extract email address at specific position. + if(aPos < aChar.Length()) + { + if(aChar[aPos] == '<' && aChar[aLeftPos] == '<' ) + { + return EFalse; + } + } + } + return (aLeftPos' position in the string + TInt length=aAddress.Length(); + TPtrC16 rightString=aAddress.Right(length-aAtPos-1); + aRightEnd=rightString.Locate('>'); + if(aRightEnd<0) + { + aRightEnd=aAddress.Locate('>'); + } + else + { + aRightEnd=aRightEnd+aAtPos+1; + } + // find the '<' position in the string + TPtrC16 leftString=aAddress.Left(aAtPos); + aLeftEnd=leftString.LocateReverse('<'); + if(aLeftEnd<0) + { + aLeftEnd=aAddress.Locate('<'); + if(aLeftEnd<0) + { + // no existance of '<' in the string + return(EFalse); + } + } + return(aLeftEnd=0 && aChar<65536); +#else + return (aChar>31 && aChar<128); +#endif + } + +TBool TImMessageField::isValidString(const TDesC16& aString,TInt& aPos) + { + aPos=0; + TInt end=aString.Length()-1; + if (end<0) + { + return ETrue; // nothing in string - so cannot be bogus! + } + + while (aPos=0 && aChar<65536); +#else + return (aChar>32 && aChar<128 && aChar!=44 && aChar!=91 && aChar!=92 && aChar!=93); +#endif + } + +TBool TImMessageField::isValidRoutedEmailAddress(const TDesC16& anAddress) + { + TBool valid = EFalse; + + if (anAddress.Length() > 1) + { + if ((anAddress[0] == '@') && (anAddress.Locate(':') != KErrNotFound)) + // Presume that an email address starting with '@' is a routed address + // but only if it also contains a ':' character + { + valid = ETrue; + } + } + + return valid; + } + + +TBool TImMessageField::isValidDomainNameChar(const TChar& aChar) + { + // The following characters are valid for a Domain name + // 45 = - hyphen + // 48 to 57 = 0 to 9 + // 65 to 90 = A to Z + // 97 to 122 = a to z + // N.B. Whilst dot is a valid char it is considered a special case as + // two cannot follow each other + + return ( ( aChar >= 'a' && aChar <= 'z') || // 97 - 122 + ( aChar >= '0' && aChar <= '9') || // 48 - 57 + ( aChar >= 'A' && aChar <= 'Z') || // 65 - 90 + aChar == '.' || // 46 + aChar == '-' // 45 + ); + } + + +TBool TImMessageField::isValidMailboxChar(const TChar& aChar) + { + // The following characters are valid for a mailbox name + // 33 = ! excalamation + // 35 - 39 = #, $, %, &, ' : Hash, dollar, percent, ampersand, single quote + // 42 - 43 = *, + : Star, plus + // 45 - 57 = -, ., /, 0 - 9 : Dash, dot, fwd slash, 0 to 9 + // 61 = = : Equals + // 63 = ? : Question mark + // 65 - 90 = A - Z : A to Z + // 94 - 126 = ^, _, `, a - z, {, |, }, ~ : caret, underscore, single opening quote, a to z, open curly braces, line, closed curly braces, tilda + // N.B. Dot is considered valid but not a special case as although RFC does not allow the following sequence '..' + // in a mailbox too many commercial email clients and server seem to allow this + + return ( ( aChar >= 'A' && aChar <= 'Z') || // 65 - 90 + ( aChar >= '^' && aChar <= '~') || // 94 - 126 + ( aChar >= '-' && aChar <= '9') || // 45 - 57 + ( aChar >= '#' && aChar <= '\'') || // 35 - 39 + aChar == '!' || // 33 + aChar == '*' || // 42 + aChar == '+' || // 43 + aChar == '=' || // 61 + aChar == '?' // 63 + ); + } + +TBool TImMessageField::isDotChar(const TChar& aChar) + { + return (aChar == '.'); + }