email/pop3andsmtpmtm/clientmtms/src/MIUTPARS.CPP
changeset 0 72b543305e3a
equal deleted inserted replaced
-1:000000000000 0:72b543305e3a
       
     1 // Copyright (c) 1998-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 //
       
    15 
       
    16 #include <e32std.h>
       
    17 #include "MIUTPARS.H"
       
    18 
       
    19 /** Tests if the specified string contains a valid email address.
       
    20 
       
    21 @param aAddress String to test
       
    22 @return ETrue if aAddress contains one valid email address. The email address 
       
    23 can contain aliases and comments. EFalse otherwise.
       
    24 */
       
    25 EXPORT_C TBool TImMessageField::ValidInternetEmailAddress(const TDesC16& aAddress)
       
    26 	{
       
    27 	TInt leftEdge=0,rightEdge=0;
       
    28 	TBool result = (isValidEmailAddress(aAddress, leftEdge, rightEdge));
       
    29 	result &= hasAngledBrackets(aAddress);
       
    30 	return result;
       
    31 	}
       
    32 
       
    33 /** Tests if the specified string contains a valid email address.
       
    34 
       
    35 @param aAddress String to test
       
    36 @param rFirstBadCharPos On return, if the test has succeeded, the character position in
       
    37 aAddress where the email address begins. If the test fails, the position of the 
       
    38 character that caused the test to fail.
       
    39  
       
    40 @return ETrue if aAddress contains one valid email address; EFalse otherwise.
       
    41 */
       
    42 EXPORT_C TBool TImMessageField::ValidInternetEmailAddress(const TDesC16& aAddress, TInt& rFirstBadCharPos)
       
    43 	{
       
    44 	TInt discardPos;
       
    45 	TBool result = (isValidEmailAddress(aAddress, rFirstBadCharPos, discardPos));
       
    46 	result &= hasAngledBrackets(aAddress);
       
    47 	return result;
       
    48 	}
       
    49 
       
    50 /** Tests if the specified string contains a valid email address.
       
    51 
       
    52 @param aAddress String to test
       
    53 @param rFirstChar On return, if the test has succeeded, the character position in
       
    54 aAddress where the email address begins. If the test fails, the position of the 
       
    55 character that caused the test to fail.
       
    56 @param rLastChar On return, if the test has succeeded, the character position in
       
    57 aAddress where the email address ends.
       
    58 @return ETrue if aAddress contains one valid email address; EFalse otherwise.
       
    59 */
       
    60 EXPORT_C TBool TImMessageField::ValidInternetEmailAddress(const TDesC16& aAddress, TInt& rFirstChar, TInt& rLastChar)
       
    61 	{
       
    62 	TBool result = (isValidEmailAddress(aAddress, rFirstChar, rLastChar));
       
    63 	result &= hasAngledBrackets(aAddress);
       
    64 	return result;
       
    65 	}
       
    66 
       
    67 /** Tests if the specified string contains a valid email address.
       
    68 
       
    69 @param aAddress String to test
       
    70 @param rFirstChar On return, if the test has succeeded, the character position in
       
    71 aAddress where the email address begins. If the test fails, the position of the 
       
    72 character that caused the test to fail.
       
    73 @param rLastChar On return, if the test has succeeded, the character position in
       
    74 aAddress where the email address ends.
       
    75 @param rFirstBadCharPos On return, if the test has succeeded, KErrNotFound. If the test 
       
    76 fails, the position of the character that caused the test to fail.
       
    77 @return ETrue if aAddress contains one valid email address; EFalse otherwise.
       
    78 */
       
    79 EXPORT_C TBool TImMessageField::ValidInternetEmailAddress(const TDesC16& aAddress, TInt& rFirstChar, TInt& rLastChar, TInt& rFirstBadCharPos)
       
    80 	{
       
    81 	TBool result = isValidEmailAddress(aAddress, rFirstChar, rLastChar);
       
    82 	// if test failed, then aLeft is the first bad character found in the email address
       
    83 	rFirstBadCharPos = (result) ? KErrNotFound : rFirstChar;
       
    84 	result &= hasAngledBrackets(aAddress);
       
    85 	return result;
       
    86 	}
       
    87 
       
    88 /** Tests if the specified character is valid (in ASCII range 32-127) for use in an email address.
       
    89 
       
    90 @param aChar Character to test
       
    91 @return ETrue if test succeeds; EFalse otherwise.
       
    92 */
       
    93 EXPORT_C TBool TImMessageField::ValidInternetEmailAddressChar(const TChar& aChar)	
       
    94 	{
       
    95 	return isValidChar(aChar);
       
    96 	}
       
    97 
       
    98 /** This function always returns true, so can be ignored.
       
    99 
       
   100 @param aSubjectLine Unused
       
   101 @return Always ETrue
       
   102 */
       
   103 EXPORT_C TBool TImMessageField::ValidSubjectLine(const TDesC16& /*aSubjectLine*/)
       
   104 	{
       
   105 	return ETrue;
       
   106 	}
       
   107 
       
   108 /** This function always returns true, so can be ignored.
       
   109 
       
   110 @param aSubjectLine Unused
       
   111 @param rFirstBadCharPos Unused
       
   112 @return Always ETrue
       
   113 */
       
   114 EXPORT_C TBool TImMessageField::ValidSubjectLine(const TDesC16& /*aSubjectLine*/, TInt& /*rFirstBadCharPos*/)
       
   115 	{
       
   116 	return ETrue;
       
   117 	}
       
   118 
       
   119 /** This function always returns true, so can be ignored.
       
   120 
       
   121 @param aChar Unused
       
   122 @return Always ETrue
       
   123 */
       
   124 EXPORT_C TBool TImMessageField::ValidSubjectLineChar(const TChar& /*aChar*/)
       
   125 	{
       
   126 	// return ( (aChar>=0 &&aChar<128) || (aChar>=160 &&aChar<256));
       
   127 	// All chars are valid UNICODE world.
       
   128 	return ETrue;
       
   129 	}
       
   130 
       
   131 /** This function always returns true, so can be ignored.
       
   132 
       
   133 @param aAliasName Unused
       
   134 @return Always ETrue
       
   135 */
       
   136 EXPORT_C TBool TImMessageField::ValidAliasName(const TDesC16& /*aAliasName*/)
       
   137 	{
       
   138 	return ETrue;
       
   139 	}
       
   140 
       
   141 /** This function always returns true, so can be ignored.
       
   142 
       
   143 @param aAliasName Unused
       
   144 @param rFirstBadCharPos Unused
       
   145 @return Always ETrue
       
   146 */
       
   147 EXPORT_C TBool TImMessageField::ValidAliasName(const TDesC16& /*aAliasName*/, TInt& /*rFirstBadCharPos*/)
       
   148 	{
       
   149 	return ETrue;
       
   150 	}
       
   151 
       
   152 /** Gets a valid email address, if one exists, from the specified string.
       
   153 
       
   154 @param aAddress String to parse
       
   155 @return If a valid email address was found in aAddress, a pointer descriptor to the 
       
   156 address part (without surrounding aliases or comments). If an address could not be found, 
       
   157 the whole of the original aAddress string is returned.
       
   158 */
       
   159 EXPORT_C TPtrC16 TImMessageField::GetValidInternetEmailAddressFromString(const TDesC16& aAddress)
       
   160 	{
       
   161 	// returns Email address (without the enclosing angled brackets) - if one can be found,
       
   162 	// otherwise return whole string as best-guess
       
   163 
       
   164 	// Best-fit = "<name@address>"
       
   165 	TInt LeftEdge=0,RightEdge=0;
       
   166 	if (isLegalEmailAddress(aAddress,LeftEdge,RightEdge))
       
   167 		{
       
   168 		// NB removes surrounding angled brackets
       
   169 		return aAddress.Mid(1+LeftEdge,RightEdge-LeftEdge-1);
       
   170 		}
       
   171 
       
   172 	// Next best-fit = "name@address"
       
   173 	if (isValidEmailAddress(aAddress, LeftEdge, RightEdge))
       
   174 		{
       
   175 		return aAddress.Mid(LeftEdge,RightEdge-LeftEdge+1);
       
   176 		}
       
   177 
       
   178 	// default-fit = "whole contents of aDesc"
       
   179 	return aAddress;
       
   180 	}
       
   181 
       
   182 /** Gets a valid email address, if one exists, from the specified string.
       
   183 
       
   184 This does not differ in functionality from the other overload of 
       
   185 GetValidInternetEmailAddressFromString().
       
   186 
       
   187 @param aAddress String to parse
       
   188 @param rError On return, always KErrNone
       
   189 @return If a valid email address was found in aAddress, a pointer descriptor to the 
       
   190 address substring (without surrounding aliases or comments). If an address could not be found, 
       
   191 the whole of the original aAddress string is returned.
       
   192 */
       
   193 EXPORT_C TPtrC16 TImMessageField::GetValidInternetEmailAddressFromString(const TDesC16& aAddress, TInt& rError)
       
   194 	{
       
   195 	// NOTE: This needs a proper implementation!
       
   196 	GetValidInternetEmailAddressFromString(aAddress);
       
   197 	rError = KErrNone;
       
   198 	return aAddress;
       
   199 	}
       
   200 
       
   201 
       
   202 /** This function overload is not implemented.
       
   203 
       
   204 @param aAddress String to parse
       
   205 @param rFirstChar Unused
       
   206 @param rLastChar Unused
       
   207 @return Original aAddress string
       
   208 */
       
   209 EXPORT_C TPtrC16 TImMessageField::GetValidInternetEmailAddressFromString(const TDesC16& aAddress, TInt& /*rFirstChar*/, TInt& /*rLastChar*/)
       
   210 	{
       
   211 	return aAddress;
       
   212 	}
       
   213 
       
   214 /** This function overload is not implemented.
       
   215 
       
   216 @param aAddress String to parse
       
   217 @param rFirstChar Unused
       
   218 @param rLastChar Unused
       
   219 @param rError Unused
       
   220 @return Original aAddress string
       
   221 */
       
   222 EXPORT_C TPtrC16 TImMessageField::GetValidInternetEmailAddressFromString(const TDesC16& aAddress, TInt& /*rFirstChar*/, TInt& /*rLastChar*/, TInt& /*rError*/)
       
   223 	{
       
   224 	return aAddress;
       
   225 	}
       
   226 
       
   227 /** Gets a valid alias, if one exists, from the specified string.
       
   228 
       
   229 A valid alias is defined here as being any substring that appears 
       
   230 to the left of a legal email address.
       
   231 
       
   232 This overload supplies an error code in rError if an alias was not found.
       
   233 
       
   234 @param aAddress String to parse
       
   235 @param rError On return, KErrNone if a valid alias was found; otherwise, KErrNotFound.
       
   236 @return If a valid alias was found in aAddress, a pointer descriptor to the 
       
   237 alias substring. If a valid alias could not be found, the whole of the original 
       
   238 aAddress string is returned.
       
   239 */
       
   240 EXPORT_C TPtrC16 TImMessageField::GetValidAlias(const TDesC16& aAddress, TInt& rError)
       
   241 	{
       
   242 	// the alias part of the string is defined here as being any
       
   243 	//  substring which appears to the left of a legal email address.
       
   244 	// If the string contains no Email address or no alias then the full address is returned
       
   245 	// and rError is set to KErrNotFound
       
   246 	rError=KErrNone;
       
   247 	TInt LeftEdge=0,RightEdge=0;
       
   248 	if (isLegalEmailAddress(aAddress,LeftEdge,RightEdge))
       
   249 		{
       
   250 		// skip space between alias and left edge of email address.
       
   251 		LeftEdge--;
       
   252 		while (LeftEdge>=0 && aAddress[LeftEdge]==' ')
       
   253 			{
       
   254 			LeftEdge--;
       
   255 			}
       
   256 
       
   257 		// left edge now points to the last character of the alias,
       
   258 		// or -1 if  there were just spaces before the email address
       
   259 		if(LeftEdge>=0)
       
   260 			{
       
   261 			// set right edge of alias to left edge of email address minus spaces
       
   262 			RightEdge=LeftEdge;
       
   263 			
       
   264 			// find first non-space character at start of descriptor
       
   265 			LeftEdge=0;
       
   266 			while (LeftEdge<RightEdge && aAddress[LeftEdge]==' ')
       
   267 				{
       
   268 				LeftEdge++;
       
   269 				}
       
   270 
       
   271 			if (RightEdge>=LeftEdge)
       
   272 				{
       
   273 				return aAddress.Mid(LeftEdge,RightEdge-LeftEdge+1);
       
   274 				}
       
   275 			}
       
   276 		}
       
   277 	rError=KErrNotFound;
       
   278 	return aAddress;
       
   279 	}
       
   280 
       
   281 /** Gets a valid alias, if one exists, from the specified string.
       
   282 
       
   283 A valid alias is defined here as being any substring that appears 
       
   284 to the left of a legal email address.
       
   285 
       
   286 @param aAddress String to parse
       
   287 @return If a valid alias was found in aAddress, a pointer descriptor to the 
       
   288 alias substring. If a valid alias could not be found, the whole of the original 
       
   289 aAddress string is returned.
       
   290 */
       
   291 EXPORT_C TPtrC16 TImMessageField::GetValidAlias(const TDesC16& aAddress)
       
   292 	{
       
   293 	TInt discardedInt;
       
   294 	return GetValidAlias(aAddress,discardedInt);
       
   295 	}
       
   296 
       
   297 /** Gets a valid comment, if one exists, from the specified string.
       
   298 
       
   299 A valid comment is found if the string contains both: 1) a legal email address; and
       
   300 2) a comment (text surrounded by parentheses).
       
   301 
       
   302 This overload supplies an error code in rError if a comment was not found.
       
   303 
       
   304 @param aAddress String to parse
       
   305 @param rError On return, KErrNone if a valid alias was found; otherwise, KErrNotFound.
       
   306 @return If a valid comment was found in aAddress, a pointer descriptor to the 
       
   307 comment substring. If a valid comment could not be found, the whole of the original 
       
   308 aAddress string is returned.
       
   309 */
       
   310 EXPORT_C TPtrC16 TImMessageField::GetValidComment(const TDesC16& aAddress, TInt& rError)
       
   311 	{
       
   312 	// Returns text contained within round brackets
       
   313 
       
   314 	// Note that string must contain a Legal email address
       
   315 	rError=KErrNone;
       
   316 	TInt LeftEdge=0,RightEdge=0;
       
   317 	if (isLegalEmailAddress(aAddress,LeftEdge,RightEdge))
       
   318 		{
       
   319 		if (isSurroundedByRoundBrackets(aAddress,LeftEdge,RightEdge))
       
   320 			{
       
   321 			LeftEdge++;
       
   322 			return aAddress.Mid(LeftEdge,RightEdge-LeftEdge);
       
   323 			}
       
   324 		}
       
   325 	rError=KErrNotFound;
       
   326 	return aAddress;
       
   327 	}
       
   328 
       
   329 /** Gets a valid comment, if one exists, from the specified string.
       
   330 
       
   331 A valid comment is found if the string contains both: 1) a legal email address; and
       
   332 2) a comment (text surrounded by parentheses).
       
   333 
       
   334 @param aAddress String to parse
       
   335 @return If a valid comment was found in aAddress, a pointer descriptor to the 
       
   336 comment substring. If a valid comment could not be found, the whole of the original 
       
   337 aAddress string is returned.
       
   338 */
       
   339 EXPORT_C TPtrC16 TImMessageField::GetValidComment(const TDesC16& aAddress)
       
   340 	{
       
   341 	TInt discardedInt;
       
   342 	return GetValidComment(aAddress,discardedInt);
       
   343 	}
       
   344 
       
   345 TBool TImMessageField::hasAngledBrackets(const TDesC16& anAddress)
       
   346 	{
       
   347 	TInt leftBracketPos=anAddress.LocateReverse('<');
       
   348 	TInt rightBracketPos=0;
       
   349 	TBool result = ETrue;
       
   350 	if (leftBracketPos==KErrNotFound)
       
   351 		{
       
   352 		rightBracketPos=anAddress.LocateReverse('>');
       
   353 		result &= (rightBracketPos==KErrNotFound);
       
   354 		}
       
   355 	else
       
   356 		{
       
   357 		rightBracketPos=anAddress.LocateReverse('>');
       
   358 		result &= (rightBracketPos>leftBracketPos);
       
   359 		}
       
   360 	return result;
       
   361 	}
       
   362 
       
   363 /** Counts email addresses in string, returns true if specified max is reached.
       
   364 
       
   365 This function is provided to allow truncation of strings of email addresses to be
       
   366 included in the body header of reply/forwarded emails, hence only a simple check 
       
   367 is made on the validity of contained email addresses.
       
   368 
       
   369 @param  aDesc  String to parse
       
   370 @param  aLimit  The number of emails to identify before truncation should occur.
       
   371 @param  aLastChar The position in the passed string of the closing angled bracket 
       
   372                   of the last email address before truncation should occur.
       
   373 @return ETrue if the truncation limit reached, EFalse otherwise.
       
   374 */
       
   375 TBool TImMessageField::TruncateAddressString(const TDesC16& aDesc, TInt aLimit, TInt& aLastChar)
       
   376 	{
       
   377 	TInt firstChar=0;
       
   378 	TInt startPos=0;
       
   379 	TInt atPos;
       
   380 	TInt length=aDesc.Length();
       
   381 	TInt count=0;
       
   382 	do	{
       
   383 		TPtrC16 rightString=aDesc.Right(length-startPos);
       
   384 		atPos=rightString.Locate('@');
       
   385 		if (atPos>0)
       
   386 			{
       
   387 			TBool valid = isSurroundedByAngledBrackets(rightString,firstChar,aLastChar,atPos);
       
   388 			if (valid)
       
   389 				{
       
   390 				++count;
       
   391 				if (count<=aLimit)
       
   392 					{
       
   393 					firstChar+=startPos;
       
   394 					aLastChar+=startPos;
       
   395 					startPos=aLastChar+1;
       
   396 					}
       
   397 				}
       
   398 			else
       
   399 				{
       
   400 				startPos+=atPos+1; // allows loop to skip "@" in aliases
       
   401 				}
       
   402 			}
       
   403 		}
       
   404 	while (atPos>=0 && startPos<length && count<aLimit);
       
   405 	aLastChar+=1; // update to position of closing angled bracket
       
   406 
       
   407 	// return true if truncate limit reached
       
   408 	return (!(count<aLimit));
       
   409 	}
       
   410 
       
   411 TBool TImMessageField::isLegalEmailAddress(const TDesC16& aDesc, TInt& rFirstChar, TInt& rLastChar)
       
   412 	{
       
   413 	// search through string for sub-string contained within angled brackets
       
   414 	// if found, see whether substring is a valid email address
       
   415 	TInt startPos=0;
       
   416 	TInt atPos;
       
   417 	TInt length=aDesc.Length();
       
   418 	do	{
       
   419 		TPtrC16 rightString=aDesc.Right(length-startPos);
       
   420 		atPos=rightString.Locate('@');
       
   421 		if (atPos>0)
       
   422 			{
       
   423 			TBool valid = isSurroundedByAngledBrackets(rightString,rFirstChar,rLastChar,atPos);
       
   424 			if (valid)
       
   425 				{
       
   426 				if (rLastChar>rFirstChar)
       
   427 					{
       
   428 					if (rFirstChar + 1 < rLastChar)
       
   429 						{
       
   430 						TPtrC rightStringWithoutAngles = rightString.Mid(rFirstChar+1, rLastChar-rFirstChar-1);
       
   431 						if (isValidRoutedEmailAddress(rightStringWithoutAngles))
       
   432 							return ETrue;
       
   433 						}
       
   434 					}
       
   435 				}
       
   436 
       
   437 			if (valid && atPos>rFirstChar+1 && atPos<rLastChar-1)
       
   438 				{
       
   439 				rFirstChar+=startPos;
       
   440 				rLastChar+=startPos;
       
   441 				TInt discardedChar;
       
   442 
       
   443 #if defined(__NO_EMAIL_ADDRESS_CHECKING__)
       
   444 				discardedChar=0;
       
   445 				return ETrue;
       
   446 #else
       
   447 				return isValidString(aDesc.Mid(rFirstChar,rLastChar-rFirstChar+1),discardedChar);	// finally, check for illegal chars in substring
       
   448 #endif
       
   449 				}
       
   450 			}
       
   451 		startPos+=atPos+1;
       
   452 		}
       
   453 	while (atPos>=0 && startPos<length);
       
   454 
       
   455 #if defined(__NO_EMAIL_ADDRESS_CHECKING__)
       
   456 	return ETrue;
       
   457 #else
       
   458 	return EFalse;
       
   459 #endif
       
   460 	}
       
   461 
       
   462 TBool TImMessageField::isValidEmailAddress(const TDesC16& aAddress, TInt& rFirstChar, TInt& rLastChar)
       
   463 	{
       
   464 	TInt startPos=0;
       
   465 	TInt atPos;
       
   466 	TInt length=aAddress.Length();
       
   467 	do	{
       
   468 		TPtrC16 rightString=aAddress.Right(length-startPos);
       
   469 		atPos=rightString.Locate('@');
       
   470 		if (isValidRoutedEmailAddress(aAddress))
       
   471 			{
       
   472 			rFirstChar = 0;
       
   473 			rLastChar = aAddress.Length() - 1;
       
   474 			return ETrue;
       
   475 			}
       
   476 		else if (LocateSubString(rightString,atPos,rFirstChar,rLastChar))
       
   477 			{
       
   478 			// Assume that this is an Internet Email address as it contains an '@' character
       
   479 			if (rLastChar-rFirstChar>1 && rFirstChar<atPos && rLastChar>atPos)		// string must be at least 3 chars long, eg "a@c" and must not contain an '@' char at either end
       
   480 				{
       
   481 				rFirstChar+=startPos;
       
   482 				rLastChar+=startPos;
       
   483 				TInt discardedChar;
       
   484 				TPtrC16 address=aAddress.Mid(rFirstChar,rLastChar-rFirstChar+1);
       
   485 				TInt result=address.LocateReverse('@');	// search for any more bogus '@' characters in remainder of email address
       
   486 
       
   487 #if defined(__NO_EMAIL_ADDRESS_CHECKING__)
       
   488 				discardedChar=0;
       
   489 				return ETrue;
       
   490 #else
       
   491 				return ((rFirstChar+result==atPos+startPos) && isValidEmailString(address,discardedChar));	// finally, check for multiple '@' chars, and also for illegal chars in substring 
       
   492 #endif
       
   493 				}
       
   494 			}
       
   495 		startPos+=atPos+1;
       
   496 		}
       
   497 	while (atPos>=0 && startPos<length);
       
   498 
       
   499 #if defined(__NO_EMAIL_ADDRESS_CHECKING__)
       
   500 	return ETrue;
       
   501 #else
       
   502 	return EFalse;
       
   503 #endif
       
   504 	}
       
   505 
       
   506 TBool TImMessageField::LocateSubString(const TDesC16& aAddress, const TInt atPos, TInt& rFirstChar, TInt& rLastChar)
       
   507 	{
       
   508 	// extract a substring containing '@' character from aDesc
       
   509 	// The substring must contain an '@' character, but this character must not be 1st or last char in substring.
       
   510 	// and which is at least three characters long (NB an arbitrary-chosen criterion to preven lone '@' chars being tested)
       
   511 	TInt LeftEdge=0,RightEdge=0;
       
   512 	TInt endPos=aAddress.Length()-1;
       
   513 	rFirstChar=atPos;
       
   514 	while (rFirstChar>0 && aAddress[rFirstChar]!=' ')
       
   515 		{
       
   516 		rFirstChar--;
       
   517 		}
       
   518 	if (rFirstChar>=0 && aAddress[rFirstChar]==' ')
       
   519 		{
       
   520 		rFirstChar++;	// point to 1st character in substring
       
   521 		}
       
   522 	if (rFirstChar<atPos)
       
   523 		{
       
   524 		rLastChar=atPos;
       
   525 		while (rLastChar<endPos && aAddress[rLastChar]!=' ')
       
   526 			{
       
   527 			rLastChar++;
       
   528 			}
       
   529 
       
   530 		if (rLastChar<endPos && aAddress[rLastChar]==' ')
       
   531 			{
       
   532 			rLastChar--;	// point to last char in substring
       
   533 			}
       
   534 
       
   535 		if (atPos<rLastChar+1)
       
   536 			{
       
   537 			// check whether substring contains embedded legal email address eg "x<name@com>z@"
       
   538 			TInt left,right;
       
   539 			if (isSurroundedByAngledBrackets(aAddress,left,right,atPos))
       
   540 				{
       
   541 				rFirstChar=left+1;
       
   542 				rLastChar=right-1;
       
   543 				}
       
   544 			return (aAddress[LeftEdge]!='@' && aAddress[RightEdge]!='@');	// email address mustn't start or end with '@' character
       
   545 			}
       
   546 		}
       
   547 	return EFalse;
       
   548 	}
       
   549 
       
   550 TBool TImMessageField::isEnclosedSubString(const TDesC16& aDesc, const TChar& aLeftBracket, const TChar& aRightBracket,TInt& aLeftPos, TInt& aRightPos)
       
   551 	{
       
   552 	//Checking if the email address has  "< "  in the username part : ex: <xxxxxxx<ssss@sdsds.com>
       
   553 	aLeftPos=aDesc.LocateReverse(aLeftBracket);
       
   554 	if (aLeftPos>=0)
       
   555 		{
       
   556  		TInt aPos = aLeftPos - 1 ; 
       
   557  		aRightPos=aDesc.LocateReverse(aRightBracket);		
       
   558  		if(aPos >= 0)
       
   559  			{
       
   560  			TPtrC16 aChar = aDesc.Mid(aPos);// To extract email address at specific position.
       
   561  			if(aPos < aChar.Length())
       
   562  				{
       
   563  				if(aChar[aPos] == '<' && aChar[aLeftPos] == '<' )
       
   564  					{
       
   565  					return EFalse;
       
   566  					}			
       
   567  				}	
       
   568  			}
       
   569  		return (aLeftPos<aRightPos);
       
   570 		}
       
   571 	return(EFalse);
       
   572 	}
       
   573 
       
   574 TBool TImMessageField::isSurroundedByAngledBrackets(const TDesC16& aAddress, TInt& aLeftEnd, TInt& aRightEnd, TInt aAtPos)
       
   575 	{	
       
   576 	// find the '>' position in the string		
       
   577 	TInt length=aAddress.Length();
       
   578 	TPtrC16 rightString=aAddress.Right(length-aAtPos-1);
       
   579 	aRightEnd=rightString.Locate('>');	
       
   580 	if(aRightEnd<0)
       
   581 		{
       
   582 		aRightEnd=aAddress.Locate('>');			
       
   583 		}
       
   584 	else
       
   585 		{
       
   586 		aRightEnd=aRightEnd+aAtPos+1;
       
   587 		}				
       
   588 	// find the '<' position in the string		
       
   589 	TPtrC16 leftString=aAddress.Left(aAtPos);
       
   590 	aLeftEnd=leftString.LocateReverse('<');			
       
   591 	if(aLeftEnd<0)
       
   592 		{
       
   593 		aLeftEnd=aAddress.Locate('<');
       
   594 		if(aLeftEnd<0) 
       
   595 			{
       
   596 			// no existance of '<' in the string
       
   597 			return(EFalse);
       
   598 			}
       
   599 		}		
       
   600 	return(aLeftEnd<aRightEnd);
       
   601 	}
       
   602 
       
   603 TBool TImMessageField::isValidChar(const TChar& aChar)
       
   604 	{
       
   605 	// Is character within RFC822 limits ie 32<=ASC(aChar)<128
       
   606 #if defined(__NO_EMAIL_ADDRESS_CHECKING__)
       
   607 	return (aChar>=0 && aChar<65536);
       
   608 #else
       
   609 	return (aChar>31 && aChar<128);
       
   610 #endif
       
   611 	}
       
   612 
       
   613 TBool TImMessageField::isValidString(const TDesC16& aString,TInt& aPos)
       
   614 	{
       
   615 	aPos=0;
       
   616 	TInt end=aString.Length()-1;
       
   617 	if (end<0)
       
   618 		{
       
   619 		return ETrue;	// nothing in string - so cannot be bogus!
       
   620 		}
       
   621 
       
   622 	while (aPos<end && isValidChar(aString[aPos]))
       
   623 		{
       
   624 		aPos++;
       
   625 		}
       
   626 
       
   627 #if defined(__NO_EMAIL_ADDRESS_CHECKING__)
       
   628 	aPos=0;
       
   629 	return ETrue;
       
   630 #else
       
   631 	return (aPos==end && isValidChar(aString[end]));
       
   632 #endif
       
   633 	}
       
   634 
       
   635 
       
   636 TBool TImMessageField::isValidEmailString(const TDesC16& aString,TInt& aPos)
       
   637 	{
       
   638 	aPos = 0;
       
   639 	TInt atCharPos = aString.Locate('@'); //redo to take a proper constant character
       
   640 	TInt stringLength = aString.Length();
       
   641 	
       
   642 	// Perform dot checking first i.e. that it is not the first or last character of the mailbox or domain
       
   643 	TInt mailboxEnd = atCharPos-1;
       
   644 	TInt domainBegin = atCharPos+1;
       
   645 	TInt domainEnd = stringLength-1;
       
   646 
       
   647 	if(aString[0] == '.' || aString[mailboxEnd] == '.' || aString[domainBegin] == '.' || aString[domainEnd] == '.')
       
   648 		{
       
   649 		return EFalse;
       
   650 		}
       
   651 	
       
   652 	// This first while-loop checks if the mailbox contains valid characters
       
   653 	while(aPos < atCharPos)
       
   654 		{
       
   655 		if(isValidMailboxChar(aString[aPos]))
       
   656 			{
       
   657 			aPos++;
       
   658 			}
       
   659 		else
       
   660 			{
       
   661 			return EFalse;
       
   662 			}
       
   663 		}
       
   664 
       
   665 	// If we reach this point in the code then the local host is OK
       
   666 	aPos = atCharPos + 1; //increment past the '@' as we don't need to check this
       
   667 	
       
   668 
       
   669 	// This second while - loop checks if the domain name is valid, this also includes checking for dot
       
   670 	// repetitions
       
   671 	TBool lastCharDot = EFalse;
       
   672 
       
   673 	while(aPos < stringLength)
       
   674 		{
       
   675 		if(lastCharDot)
       
   676 			{
       
   677 			if(isDotChar(aString[aPos]))
       
   678 				{
       
   679 				// We have two dots after each other therefore address is invalid
       
   680 				return EFalse;
       
   681 				}
       
   682 			else if(isValidDomainNameChar(aString[aPos]))
       
   683 				{
       
   684 				lastCharDot = EFalse;
       
   685 				aPos++;
       
   686 				}
       
   687 			else
       
   688 				{
       
   689 				return EFalse;
       
   690 				}
       
   691 			}
       
   692 		else
       
   693 			{
       
   694 			if(isDotChar(aString[aPos]))
       
   695 				{				
       
   696 				lastCharDot = ETrue;
       
   697 				aPos++;
       
   698 				}
       
   699 			else if(isValidDomainNameChar(aString[aPos]))
       
   700 				{
       
   701 				lastCharDot = EFalse; // excessive but just in case
       
   702 				aPos++;
       
   703 				}
       
   704 			else
       
   705 				{
       
   706 				return EFalse;
       
   707 				}
       
   708 			}
       
   709 		}
       
   710 
       
   711 	// If we reach this point then we have not encountered an invalid character so we can return ETrue
       
   712 	return ETrue;
       
   713 	}
       
   714 
       
   715 
       
   716 TBool TImMessageField::isSurroundedByRoundBrackets(const TDesC16& aDesc, TInt& aLeftEnd, TInt& aRightEnd)
       
   717 	{
       
   718 	return (isEnclosedSubString(aDesc,'(',')',aLeftEnd,aRightEnd));
       
   719 	}
       
   720 
       
   721 TBool TImMessageField::isValidEmailChar(const TChar& aChar)
       
   722 	{
       
   723 #if defined(__NO_EMAIL_ADDRESS_CHECKING__)
       
   724 	return (aChar>=0 && aChar<65536);
       
   725 #else
       
   726 	return (aChar>32 && aChar<128 && aChar!=44 && aChar!=91 && aChar!=92 && aChar!=93);
       
   727 #endif
       
   728 	}
       
   729 
       
   730 TBool TImMessageField::isValidRoutedEmailAddress(const TDesC16& anAddress)
       
   731 	{
       
   732 	TBool valid = EFalse;
       
   733 
       
   734 	if (anAddress.Length() > 1)
       
   735 		{
       
   736 		if ((anAddress[0] == '@') && (anAddress.Locate(':') != KErrNotFound))
       
   737 			// Presume that an email address starting with '@' is a routed address
       
   738 			// but only if it also contains a ':' character
       
   739 			{
       
   740 			valid = ETrue;
       
   741 			}
       
   742 		}
       
   743 
       
   744 	return valid;
       
   745    	}
       
   746 
       
   747 
       
   748 TBool TImMessageField::isValidDomainNameChar(const TChar& aChar)
       
   749 	{
       
   750 	// The following characters are valid for a Domain name
       
   751 	// 45 = - hyphen
       
   752 	// 48 to 57 = 0 to 9
       
   753 	// 65 to 90 = A to Z
       
   754 	// 97 to 122 = a to z
       
   755 	// N.B. Whilst dot is a valid char it is considered a special case as
       
   756 	// two cannot follow each other
       
   757 	 
       
   758 	return ( ( aChar >= 'a' && aChar <= 'z') ||   // 97 - 122
       
   759 	         ( aChar >= '0' && aChar <= '9') ||	  // 48 - 57	 
       
   760              ( aChar >= 'A' && aChar <= 'Z') ||   // 65 - 90 
       
   761 			   aChar == '.' ||                    // 46 
       
   762 	           aChar == '-'                       // 45
       
   763 	       );
       
   764 	}
       
   765 
       
   766 
       
   767 TBool TImMessageField::isValidMailboxChar(const TChar& aChar)
       
   768 	{
       
   769 	// The following characters are valid for a mailbox name
       
   770 	// 33 = ! excalamation
       
   771 	// 35 - 39 = #, $, %, &, ' : Hash, dollar, percent, ampersand, single quote
       
   772 	// 42 - 43 = *, + : Star, plus
       
   773 	// 45 - 57 = -, ., /, 0 - 9 : Dash, dot, fwd slash, 0 to 9
       
   774 	// 61 = = : Equals
       
   775 	// 63 = ? : Question mark
       
   776 	// 65 - 90 = A - Z : A to Z
       
   777 	// 94 - 126 = ^, _, `, a - z, {, |, }, ~ : caret, underscore, single opening quote, a to z, open curly braces, line, closed curly braces, tilda
       
   778 	// N.B. Dot is considered valid but not a special case as although RFC does not allow the following sequence '..'
       
   779 	// in a mailbox too many commercial email clients and server seem to allow this
       
   780 	
       
   781 	return ( ( aChar >= 'A' && aChar <= 'Z') ||   // 65 - 90 
       
   782 	         ( aChar >= '^' && aChar <= '~') ||   // 94 - 126
       
   783 	         ( aChar >= '-' && aChar <= '9') ||	  // 45 - 57	
       
   784 	         ( aChar >= '#' && aChar <= '\'') ||  // 35 - 39
       
   785 	           aChar == '!' ||                    // 33 
       
   786 	           aChar == '*' ||                    // 42  
       
   787 	           aChar == '+' ||                    // 43
       
   788 	           aChar == '=' ||                    // 61
       
   789 	           aChar == '?'                       // 63 	       
       
   790 	       ); 
       
   791 	}
       
   792 
       
   793 TBool TImMessageField::isDotChar(const TChar& aChar)
       
   794 	{
       
   795 	return (aChar == '.');
       
   796 	}