email/imap4mtm/imapsession/src/cimaplogin.cpp
changeset 31 ebfee66fde93
parent 0 72b543305e3a
equal deleted inserted replaced
30:6a20128ce557 31:ebfee66fde93
       
     1 // Copyright (c) 2006-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 "cimaplogin.h"
       
    17 #include "moutputstream.h"
       
    18 #include "cimapsessionconsts.h"
       
    19 #include "imappaniccodes.h"
       
    20 
       
    21 // IMAP LOGIN command
       
    22 _LIT8(KCommandLogin, "%d LOGIN %S %S\r\n");
       
    23 _LIT8(KCommandLoginUsernameLength, "%d LOGIN {%d}\r\n");
       
    24 _LIT8(KCommandLoginUsernamePasswordLength, "%d LOGIN %S {%d}\r\n");
       
    25 
       
    26 _LIT8(KCommandContinueUsernameLitPassword, "%S %S\r\n");
       
    27 _LIT8(KCommandContinueUsernameLitPasswordLength, "%S {%d}\r\n");
       
    28 _LIT8(KCommandContinuePasswordLit, "%S\r\n");
       
    29 
       
    30 
       
    31 CImapLogin* CImapLogin::NewL(CImapFolderInfo* aSelectedFolderData, TInt aLogId, const TDesC8& aUserName, const TDesC8& aPassword, CImapCapabilityInfo& aCapabilityInfo)
       
    32 // static method first phase construction
       
    33 	{		
       
    34 	CImapLogin* self = new (ELeave) CImapLogin(aSelectedFolderData, aLogId, aCapabilityInfo);
       
    35 	CleanupStack::PushL(self);
       
    36 	self->ConstructL(aUserName, aPassword);
       
    37 	CleanupStack::Pop();
       
    38 	return self;
       
    39 	}
       
    40 		
       
    41 CImapLogin::CImapLogin(CImapFolderInfo* aSelectedFolderData, TInt aLogId, CImapCapabilityInfo& aCapabilityInfo)
       
    42 	: CImapCapability(aSelectedFolderData, aLogId, aCapabilityInfo)
       
    43 	{				
       
    44 	}
       
    45 		
       
    46 void CImapLogin::ConstructL(const TDesC8& aUserName, const TDesC8& aPassword)
       
    47 	{
       
    48 	iUserName =	aUserName.AllocL();
       
    49 	iPassword = aPassword.AllocL();
       
    50 	}
       
    51 
       
    52 CImapLogin::~CImapLogin()
       
    53 	{
       
    54 	delete iUserName;
       
    55 	delete iPassword;
       
    56 	}
       
    57 
       
    58 /**
       
    59 Check whether the username or/and password needs to be sent as literal data.
       
    60 The data needs to be sent as a literal if it contains non ASCII characters
       
    61 or it contains special characters
       
    62 */
       
    63 void CImapLogin::CheckLiteralInUserNameAndPassword()
       
    64 	{
       
    65 	iLiteralUsername = EFalse;
       
    66 	TPtr8 userName = iUserName->Des();
       
    67 	for(TInt a=0;a<iUserName->Length();a++)
       
    68 		{
       
    69 		if (userName[a]<=32  || userName[a]>=127 || userName[a]=='\"' || userName[a]=='%'  ||
       
    70 		    userName[a]=='(' || userName[a]==')' || userName[a]=='*'  || userName[a]=='\\' ||
       
    71 		    userName[a]=='{' || userName[a]=='}' )
       
    72 			{
       
    73 			iLiteralUsername=ETrue;
       
    74 			break;
       
    75 			}
       
    76 		}
       
    77 
       
    78 	iLiteralPassword=EFalse;
       
    79 	TPtr8 password = iPassword->Des();
       
    80 	for(TInt a=0;a<iPassword->Length();a++)
       
    81 		{
       
    82 		if (password[a]<=32  || password[a]>=127 || password[a]=='\"' || password[a]=='%'  ||
       
    83 		    password[a]=='(' || password[a]==')' || password[a]=='*'  || password[a]=='\\' ||
       
    84 		    password[a]=='{' || password[a]=='}' )
       
    85 			{
       
    86 			iLiteralPassword=ETrue;
       
    87 			break;
       
    88 			}
       
    89 		}
       
    90 	}
       
    91 /** 
       
    92 Formats and sends the IMAP LOGIN command.
       
    93 @param aTagId Command sequence id which will be send along with the IMAP command.
       
    94 @param aStream A wrapper for the outbound stream of a connected socket, using which 
       
    95 the command will be send to the server
       
    96 */
       
    97 void CImapLogin::SendMessageL(TInt aTagId, MOutputStream& aStream)
       
    98 	{
       
    99 	iTagId = aTagId;
       
   100 	iStream = &aStream;
       
   101 		
       
   102 	CheckLiteralInUserNameAndPassword();
       
   103 
       
   104 	__ASSERT_DEBUG(iOutputBuffer==NULL, TImapServerPanic::ImapPanic(TImapServerPanic::ECommandOutputBufferNotNull));		
       
   105 	if (!iLiteralUsername && !iLiteralPassword)
       
   106 		{
       
   107 		// send username and password now
       
   108 		TInt bufLength = KCommandLogin().Length() + TagLength(aTagId) + iUserName->Length() + iPassword->Length();
       
   109 		iOutputBuffer = HBufC8::NewL(bufLength);
       
   110 		
       
   111 		iOutputBuffer->Des().Format(KCommandLogin, aTagId, iUserName, iPassword);		
       
   112 		}
       
   113 	else
       
   114 		{	
       
   115 		if (iLiteralUsername)
       
   116 			{
       
   117 			// send the username's length
       
   118 			TInt bufLength = KCommandLoginUsernameLength().Length() + TagLength(aTagId) + TagLength(iUserName->Length());
       
   119 			iOutputBuffer = HBufC8::NewL(bufLength);
       
   120 			
       
   121 			iOutputBuffer->Des().Format(KCommandLoginUsernameLength(), aTagId, iUserName->Length());
       
   122 			iSentUserNameLength = ETrue;	
       
   123 			}
       
   124 		else 
       
   125 			{
       
   126 			// send the username and the passwords's length
       
   127 			TInt bufLength = KCommandLoginUsernamePasswordLength().Length() + TagLength(aTagId) + iUserName->Length() + TagLength(iPassword->Length());
       
   128 			iOutputBuffer = HBufC8::NewL(bufLength);
       
   129 			iOutputBuffer->Des().Format(KCommandLoginUsernamePasswordLength(), aTagId, iUserName, iPassword->Length());
       
   130 			}
       
   131 		}
       
   132 	
       
   133 	//send the command to the server
       
   134 	aStream.SendDataReq(*iOutputBuffer);
       
   135 	}
       
   136 
       
   137 void CImapLogin::ParseContinuationResponseL()
       
   138 	{
       
   139 	__ASSERT_DEBUG(iStream != NULL, TImapServerPanic::ImapPanic(TImapServerPanic::ECommandOutputStreamIsNull));
       
   140 	
       
   141 	delete iOutputBuffer;
       
   142 	iOutputBuffer = NULL;
       
   143 	
       
   144 	if(iLiteralUsername && !iLiteralPassword)
       
   145 		{
       
   146 		// send the username's literal data and then password.
       
   147 		TInt bufLength = KCommandContinueUsernameLitPassword().Length() + iUserName->Length() + iPassword->Length();
       
   148 		iOutputBuffer = HBufC8::NewL(bufLength);				
       
   149 		
       
   150 		iOutputBuffer->Des().Format(KCommandContinueUsernameLitPassword, iUserName, iPassword);
       
   151 		}
       
   152 	else if(iSentUserNameLength && iLiteralPassword)
       
   153 		{
       
   154 		// send the username's literal data and then password's length.
       
   155 		TInt bufLength = KCommandContinueUsernameLitPasswordLength().Length() + iUserName->Length() + TagLength(iPassword->Length());
       
   156 		iOutputBuffer = HBufC8::NewL(bufLength);
       
   157 		
       
   158 		iOutputBuffer->Des().Format(KCommandContinueUsernameLitPasswordLength, iUserName, iPassword->Length());
       
   159 		iSentUserNameLength = EFalse;
       
   160 		}
       
   161 	else
       
   162 		{
       
   163 		// send the password as literal data
       
   164 		TInt bufLength = KCommandContinuePasswordLit().Length() + iPassword->Length();
       
   165 		iOutputBuffer = HBufC8::NewL(bufLength);
       
   166 		
       
   167 		iOutputBuffer->Des().Format(KCommandContinuePasswordLit, iPassword);
       
   168 		}
       
   169 		
       
   170 	iStream->SendDataReq(*iOutputBuffer);
       
   171 	}
       
   172 
       
   173 	
       
   174 /**
       
   175 Calls the base class for default parsing,
       
   176 then checks for and processes the CAPABILITY response text code if it has been sent.
       
   177 E.g. The tagged response may have been something like
       
   178 123 OK [CAPABILITY IMAP4rev1 STARTTLS AUTH=PLAIN LOGINDISABLED]
       
   179 @param aTagId The incoming tag id.
       
   180 @return ETrue - indicating that the command is completed by this tagged response.
       
   181 */
       
   182 TBool CImapLogin::ParseTaggedResponseL(TInt aTagId)
       
   183 	{
       
   184 	BaseParseTaggedResponseL(aTagId);
       
   185 		
       
   186 	TPtrC8 responseTextCode = GetResponseTextCodeL();
       
   187 	if (responseTextCode.Length() > 0)
       
   188 		{
       
   189 		// If this is a CAPABILITY response text code, then process it now
       
   190 		iUnparsedData.Set(responseTextCode);
       
   191 		TPtrC8 part = GetNextPart();
       
   192 		
       
   193 		if (part.CompareF(KTxtImapCapability) == 0)
       
   194 			{
       
   195 			ParseCapabilityDataL(iUnparsedData);
       
   196 			}
       
   197 		}
       
   198 		
       
   199 	return ETrue;
       
   200 	}
       
   201