Back to index

lightning-sunbird  0.9+nobinonly
nsOutlookSettings.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is mozilla.org code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *   Pierre Phaneuf <pp@ludusdesign.com>
00024  *
00025  * Alternatively, the contents of this file may be used under the terms of
00026  * either of the GNU General Public License Version 2 or later (the "GPL"),
00027  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00028  * in which case the provisions of the GPL or the LGPL are applicable instead
00029  * of those above. If you wish to allow use of your version of this file only
00030  * under the terms of either the GPL or the LGPL, and not to allow others to
00031  * use your version of this file under the terms of the MPL, indicate your
00032  * decision by deleting the provisions above and replace them with the notice
00033  * and other provisions required by the GPL or the LGPL. If you do not delete
00034  * the provisions above, a recipient may use your version of this file under
00035  * the terms of any one of the MPL, the GPL or the LGPL.
00036  *
00037  * ***** END LICENSE BLOCK ***** */
00038 
00039 /*
00040 
00041   Outlook Express (Win32) settings
00042 
00043 */
00044 
00045 #include "nsCOMPtr.h"
00046 #include "nscore.h"
00047 #include "nsReadableUtils.h"
00048 #include "nsOutlookImport.h"
00049 #include "nsIComponentManager.h"
00050 #include "nsIServiceManager.h"
00051 #include "nsIImportService.h"
00052 #include "nsOutlookRegUtil.h"
00053 #include "nsIMsgAccountManager.h"
00054 #include "nsIMsgAccount.h"
00055 #include "nsIImportSettings.h"
00056 #include "nsOutlookSettings.h"
00057 #include "nsMsgBaseCID.h"
00058 #include "nsMsgCompCID.h"
00059 #include "nsISmtpService.h"
00060 #include "nsISmtpServer.h"
00061 #include "nsOutlookStringBundle.h"
00062 #include "OutlookDebugLog.h"
00063 #include "nsIPop3IncomingServer.h"
00064 #include "nsIMessengerMigrator.h"
00065 
00066 class OutlookSettings {
00067 public:
00068        static HKEY FindAccountsKey( void);
00069 
00070        static PRBool DoImport( nsIMsgAccount **ppAccount);
00071 
00072        static PRBool DoIMAPServer( nsIMsgAccountManager *pMgr, HKEY hKey, char *pServerName, nsIMsgAccount **ppAccount);
00073        static PRBool DoPOP3Server( nsIMsgAccountManager *pMgr, HKEY hKey, char *pServerName, nsIMsgAccount **ppAccount);
00074        
00075        static void SetIdentities( nsIMsgAccountManager *pMgr, nsIMsgAccount *pAcc, HKEY hKey);
00076        static PRBool IdentityMatches( nsIMsgIdentity *pIdent, const char *pName, const char *pServer, const char *pEmail, const char *pReply, const char *pUserName);
00077 
00078        static void SetSmtpServer( nsIMsgAccountManager *pMgr, nsIMsgAccount *pAcc, char *pServer, char *pUser);
00079   static nsresult GetAccountName(HKEY hKey, char *defaultName, nsString &acctName);
00080 };
00081 
00082 
00084 nsresult nsOutlookSettings::Create(nsIImportSettings** aImport)
00085 {
00086     NS_PRECONDITION(aImport != nsnull, "null ptr");
00087     if (! aImport)
00088         return NS_ERROR_NULL_POINTER;
00089 
00090     *aImport = new nsOutlookSettings();
00091     if (! *aImport)
00092         return NS_ERROR_OUT_OF_MEMORY;
00093 
00094     NS_ADDREF(*aImport);
00095     return NS_OK;
00096 }
00097 
00098 nsOutlookSettings::nsOutlookSettings()
00099 {
00100 }
00101 
00102 nsOutlookSettings::~nsOutlookSettings()
00103 {
00104 }
00105 
00106 NS_IMPL_ISUPPORTS1(nsOutlookSettings, nsIImportSettings)
00107 
00108 NS_IMETHODIMP nsOutlookSettings::AutoLocate(PRUnichar **description, nsIFileSpec **location, PRBool *_retval)
00109 {
00110     NS_PRECONDITION(description != nsnull, "null ptr");
00111     NS_PRECONDITION(_retval != nsnull, "null ptr");
00112        if (!description || !_retval)
00113               return( NS_ERROR_NULL_POINTER);
00114        
00115        *description = nsOutlookStringBundle::GetStringByID( OUTLOOKIMPORT_NAME);
00116        *_retval = PR_FALSE;
00117 
00118        if (location)
00119               *location = nsnull;
00120 
00121        // look for the registry key for the accounts
00122        HKEY key = OutlookSettings::FindAccountsKey();
00123        if (key != nsnull) {
00124               *_retval = PR_TRUE;
00125               ::RegCloseKey( key);
00126        }
00127 
00128        return( NS_OK);
00129 }
00130 
00131 NS_IMETHODIMP nsOutlookSettings::SetLocation(nsIFileSpec *location)
00132 {
00133        return( NS_OK);
00134 }
00135 
00136 NS_IMETHODIMP nsOutlookSettings::Import(nsIMsgAccount **localMailAccount, PRBool *_retval)
00137 {
00138        NS_PRECONDITION( _retval != nsnull, "null ptr");
00139        
00140        if (OutlookSettings::DoImport( localMailAccount)) {
00141               *_retval = PR_TRUE;
00142               IMPORT_LOG0( "Settings import appears successful\n");
00143        }
00144        else {
00145               *_retval = PR_FALSE;
00146               IMPORT_LOG0( "Settings import returned FALSE\n");
00147        }
00148 
00149        return( NS_OK);
00150 }
00151 
00152 
00153 HKEY OutlookSettings::FindAccountsKey( void)
00154 {
00155        HKEY   sKey;
00156        if (::RegOpenKeyEx( HKEY_CURRENT_USER, "Software\\Microsoft\\Office\\Outlook\\OMI Account Manager\\Accounts", 0, KEY_QUERY_VALUE | KEY_ENUMERATE_SUB_KEYS, &sKey) == ERROR_SUCCESS) {
00157               return( sKey);
00158        }
00159        if (::RegOpenKeyEx( HKEY_CURRENT_USER, "Software\\Microsoft\\Office\\8.0\\Outlook\\OMI Account Manager\\Accounts", 0, KEY_QUERY_VALUE | KEY_ENUMERATE_SUB_KEYS, &sKey) == ERROR_SUCCESS) {
00160               return( sKey);
00161        }
00162 
00163        return( nsnull);
00164 }
00165 
00166 PRBool OutlookSettings::DoImport( nsIMsgAccount **ppAccount)
00167 {
00168        HKEY   hKey = FindAccountsKey();
00169        if (hKey == nsnull) {
00170               IMPORT_LOG0( "*** Error finding Outlook registry account keys\n");
00171               return( PR_FALSE);
00172        }
00173 
00174        nsresult      rv;
00175        
00176        nsCOMPtr<nsIMsgAccountManager> accMgr = 
00177                 do_GetService(NS_MSGACCOUNTMANAGER_CONTRACTID, &rv);
00178     if (NS_FAILED(rv)) {
00179               IMPORT_LOG0( "*** Failed to create a account manager!\n");
00180               return( PR_FALSE);
00181        }
00182 
00183        HKEY          subKey = NULL;
00184        nsCString     defMailName;
00185 
00186        if (::RegOpenKeyEx( HKEY_CURRENT_USER, "Software\\Microsoft\\Office\\Outlook\\OMI Account Manager", 0, KEY_QUERY_VALUE, &subKey) != ERROR_SUCCESS)
00187               if (::RegOpenKeyEx( HKEY_CURRENT_USER, "Software\\Microsoft\\Office\\8.0\\Outlook\\OMI Account Manager", 0, KEY_QUERY_VALUE, &subKey) != ERROR_SUCCESS)
00188                      subKey = NULL;
00189 
00190        if (subKey != NULL) {
00191               // First let's get the default mail account key name
00192               BYTE * pBytes = nsOutlookRegUtil::GetValueBytes( subKey, "Default Mail Account");
00193               ::RegCloseKey( subKey);
00194               if (pBytes) {
00195                      defMailName = (const char *)pBytes;
00196                      nsOutlookRegUtil::FreeValueBytes( pBytes);
00197               }
00198        }
00199 
00200        // Iterate the accounts looking for POP3 & IMAP accounts...
00201        // Ignore LDAP & NNTP for now!
00202        DWORD         index = 0;
00203        DWORD         numChars;
00204        TCHAR         keyName[256];
00205        FILETIME      modTime;
00206        LONG          result = ERROR_SUCCESS;
00207        BYTE *        pBytes;
00208        int                  popCount = 0;
00209        int                  accounts = 0;
00210        nsCString     keyComp;
00211 
00212        while (result == ERROR_SUCCESS) {
00213               numChars = 256;
00214               result = ::RegEnumKeyEx( hKey, index, keyName, &numChars, NULL, NULL, NULL, &modTime);
00215               index++;
00216               if (result == ERROR_SUCCESS) {
00217                      if (::RegOpenKeyEx( hKey, keyName, 0, KEY_QUERY_VALUE, &subKey) == ERROR_SUCCESS) {
00218                             // Get the values for this account.
00219                             IMPORT_LOG1( "Opened Outlook account: %s\n", (char *)keyName);
00220 
00221                             nsIMsgAccount *anAccount = nsnull;
00222                             pBytes = nsOutlookRegUtil::GetValueBytes( subKey, "IMAP Server");
00223                             if (pBytes) {
00224                                    if (DoIMAPServer( accMgr, subKey, (char *)pBytes, &anAccount))
00225                                           accounts++;
00226                                    nsOutlookRegUtil::FreeValueBytes( pBytes);
00227                             }
00228 
00229                             pBytes = nsOutlookRegUtil::GetValueBytes( subKey, "POP3 Server");
00230                             if (pBytes) {
00231                                    if (popCount == 0) {
00232                                           if (DoPOP3Server( accMgr, subKey, (char *)pBytes, &anAccount)) {
00233                                                  popCount++;
00234                                                  accounts++;
00235                                                  if (ppAccount && anAccount) {
00236                                                         *ppAccount = anAccount;
00237                                                         NS_ADDREF( anAccount);
00238                                                  }
00239                                           }
00240                                    }
00241                                    else {
00242                                           if (DoPOP3Server( accMgr, subKey, (char *)pBytes, &anAccount)) {
00243                                                  popCount++;
00244                                                  accounts++;
00245                                                  // If we created a mail account, get rid of it since
00246                                                  // we have 2 POP accounts!
00247                                                  if (ppAccount && *ppAccount) {
00248                                                         NS_RELEASE( *ppAccount);
00249                                                         *ppAccount = nsnull;
00250                                                  }
00251                                           }
00252                                    }
00253                                    nsOutlookRegUtil::FreeValueBytes( pBytes);
00254                             }
00255                             
00256                             if (anAccount) {
00257                                    // Is this the default account?
00258                                    keyComp = keyName;
00259                                    if (keyComp.Equals( defMailName)) {
00260                                           accMgr->SetDefaultAccount( anAccount);
00261                                    }
00262                                    NS_RELEASE( anAccount);
00263                             }
00264 
00265                             ::RegCloseKey( subKey);
00266                      }
00267               }
00268        }
00269 
00270        // Now save the new acct info to pref file.
00271        rv = accMgr->SaveAccountInfo();
00272        NS_ASSERTION(NS_SUCCEEDED(rv), "Can't save account info to pref file");
00273 
00274        return( accounts != 0);
00275 }
00276 
00277 nsresult OutlookSettings::GetAccountName(HKEY hKey, char *defaultName, nsString &acctName)
00278 {
00279   BYTE *pAccName = nsOutlookRegUtil::GetValueBytes( hKey, "Account Name");
00280   nsresult rv = NS_OK;
00281   if (pAccName) {
00282     nsCOMPtr<nsIImportService> impSvc = do_GetService(NS_IMPORTSERVICE_CONTRACTID);
00283     if (impSvc)
00284       rv = impSvc->SystemStringToUnicode((const char *)pAccName, acctName);
00285     nsOutlookRegUtil::FreeValueBytes( pAccName);
00286   }
00287   else
00288     acctName.AssignASCII(defaultName);
00289   return rv;
00290 }
00291 
00292 PRBool OutlookSettings::DoIMAPServer( nsIMsgAccountManager *pMgr, HKEY hKey, char *pServerName, nsIMsgAccount **ppAccount)
00293 {
00294        if (ppAccount)
00295               *ppAccount = nsnull;
00296 
00297        BYTE *pBytes;
00298        pBytes = nsOutlookRegUtil::GetValueBytes( hKey, "IMAP User Name");
00299        if (!pBytes)
00300               return( PR_FALSE);
00301 
00302        PRBool result = PR_FALSE;
00303 
00304        // I now have a user name/server name pair, find out if it already exists?
00305        nsCOMPtr<nsIMsgIncomingServer>     in;
00306        nsresult rv = pMgr->FindServer( (const char *)pBytes, pServerName, "imap", getter_AddRefs( in));
00307        if (NS_FAILED( rv) || (in == nsnull)) {
00308               // Create the incoming server and an account for it?
00309               rv = pMgr->CreateIncomingServer( (const char *)pBytes, pServerName, "imap", getter_AddRefs( in));
00310               if (NS_SUCCEEDED( rv) && in) {
00311                      rv = in->SetType( "imap");
00312                      // rv = in->SetHostName( pServerName);
00313                      // rv = in->SetUsername( (char *)pBytes);
00314                      
00315                      IMPORT_LOG2( "Created IMAP server named: %s, userName: %s\n", pServerName, (char *)pBytes);
00316 
00317                      nsString      prettyName;
00318       if (NS_SUCCEEDED(GetAccountName(hKey, pServerName, prettyName)))
00319       {
00320                      PRUnichar *pretty = ToNewUnicode(prettyName);
00321         if (pretty)
00322         {
00323                      rv = in->SetPrettyName( pretty);
00324                      nsCRT::free( pretty);
00325         }
00326       }
00327                      
00328                      // We have a server, create an account.
00329                      nsCOMPtr<nsIMsgAccount>     account;
00330                      rv = pMgr->CreateAccount( getter_AddRefs( account));
00331                      if (NS_SUCCEEDED( rv) && account) {
00332                             rv = account->SetIncomingServer( in);     
00333                             
00334                             IMPORT_LOG0( "Created an account and set the IMAP server as the incoming server\n");
00335 
00336                             // Fiddle with the identities
00337                             SetIdentities( pMgr, account, hKey);
00338                             result = PR_TRUE;
00339                             if (ppAccount)
00340                                    account->QueryInterface( NS_GET_IID(nsIMsgAccount), (void **)ppAccount);
00341                      }                           
00342               }
00343        }
00344        else
00345               result = PR_TRUE;
00346        
00347        nsOutlookRegUtil::FreeValueBytes( pBytes);
00348 
00349        return( result);
00350 }
00351 
00352 PRBool OutlookSettings::DoPOP3Server( nsIMsgAccountManager *pMgr, HKEY hKey, char *pServerName, nsIMsgAccount **ppAccount)
00353 {
00354        if (ppAccount)
00355               *ppAccount = nsnull;
00356 
00357        BYTE *pBytes;
00358        pBytes = nsOutlookRegUtil::GetValueBytes( hKey, "POP3 User Name");
00359        if (!pBytes)
00360               return( PR_FALSE);
00361 
00362        PRBool result = PR_FALSE;
00363 
00364        // I now have a user name/server name pair, find out if it already exists?
00365        nsCOMPtr<nsIMsgIncomingServer>     in;
00366        nsresult rv = pMgr->FindServer( (const char *)pBytes, pServerName, "pop3", getter_AddRefs( in));
00367        if (NS_FAILED( rv) || (in == nsnull)) {
00368               // Create the incoming server and an account for it?
00369               rv = pMgr->CreateIncomingServer( (const char *)pBytes, pServerName, "pop3", getter_AddRefs( in));
00370               if (NS_SUCCEEDED( rv) && in) {
00371                      rv = in->SetType( "pop3");
00372 
00373         nsCOMPtr<nsIPop3IncomingServer> pop3Server = do_QueryInterface(in);
00374         if (pop3Server) {
00375             // set local folders as the Inbox to use for this POP3 server
00376             nsCOMPtr<nsIMsgIncomingServer> localFoldersServer;
00377             pMgr->GetLocalFoldersServer(getter_AddRefs(localFoldersServer));
00378 
00379             if (!localFoldersServer)
00380             {
00381                 // XXX: We may need to move this local folder creation code to the generic nsImportSettings code
00382                 // if the other import modules end up needing to do this too.    
00383                 // if Local Folders does not exist already, create it
00384                 nsCOMPtr <nsIMessengerMigrator> messengerMigrator = do_GetService(NS_MESSENGERMIGRATOR_CONTRACTID, &rv);
00385                 if (NS_FAILED(rv)) {
00386                     IMPORT_LOG0( "*** Failed to create messenger migrator!\n");
00387                     return PR_FALSE;
00388                 }
00389           
00390                 rv = messengerMigrator->CreateLocalMailAccount(PR_FALSE);
00391                 if (NS_FAILED(rv)) {
00392                     IMPORT_LOG0( "*** Failed to create Local Folders!\n");
00393                     return PR_FALSE;
00394                 }
00395     
00396                 pMgr->GetLocalFoldersServer(getter_AddRefs(localFoldersServer)); 
00397             }
00398 
00399             // now get the account for this server
00400             nsCOMPtr<nsIMsgAccount> localFoldersAccount;
00401             pMgr->FindAccountForServer(localFoldersServer, getter_AddRefs(localFoldersAccount)); 
00402             if (localFoldersAccount)
00403             {
00404                 nsXPIDLCString localFoldersAcctKey;
00405                 localFoldersAccount->GetKey(getter_Copies(localFoldersAcctKey));
00406                 pop3Server->SetDeferredToAccount(localFoldersAcctKey.get()); 
00407                 pop3Server->SetDeferGetNewMail(PR_TRUE);
00408             }
00409         }
00410 
00411                      IMPORT_LOG2( "Created POP3 server named: %s, userName: %s\n", pServerName, (char *)pBytes);
00412 
00413                      nsString      prettyName;
00414       if (NS_SUCCEEDED(GetAccountName(hKey, pServerName, prettyName)))
00415       {
00416                      PRUnichar *pretty = ToNewUnicode(prettyName);
00417         if (pretty)
00418         {
00419                      rv = in->SetPrettyName( pretty);
00420                      nsCRT::free( pretty);
00421         }
00422       }
00423                      
00424                      // We have a server, create an account.
00425                      nsCOMPtr<nsIMsgAccount>     account;
00426                      rv = pMgr->CreateAccount( getter_AddRefs( account));
00427                      if (NS_SUCCEEDED( rv) && account) {
00428                             rv = account->SetIncomingServer( in);
00429                             
00430                             IMPORT_LOG0( "Created a new account and set the incoming server to the POP3 server.\n");
00431                                    
00432         nsCOMPtr<nsIPop3IncomingServer> pop3Server = do_QueryInterface(in, &rv);
00433         NS_ENSURE_SUCCESS(rv,rv);
00434         BYTE *pLeaveOnServer = nsOutlookRegUtil::GetValueBytes( hKey, "Leave Mail On Server");
00435         if (pLeaveOnServer)
00436         {
00437           pop3Server->SetLeaveMessagesOnServer(*pLeaveOnServer == 1 ? PR_TRUE : PR_FALSE);
00438           nsOutlookRegUtil::FreeValueBytes(pLeaveOnServer);
00439         }
00440 
00441         // Fiddle with the identities
00442                             SetIdentities( pMgr, account, hKey);
00443                             result = PR_TRUE;
00444                             if (ppAccount)
00445                                    account->QueryInterface( NS_GET_IID(nsIMsgAccount), (void **)ppAccount);
00446                      }                           
00447               }
00448        }
00449        else
00450               result = PR_TRUE;
00451        
00452        nsOutlookRegUtil::FreeValueBytes( pBytes);
00453 
00454        return( result);
00455 }
00456 
00457 PRBool OutlookSettings::IdentityMatches( nsIMsgIdentity *pIdent, const char *pName, const char *pServer, const char *pEmail, const char *pReply, const char *pUserName)
00458 {
00459        if (!pIdent)
00460               return( PR_FALSE);
00461 
00462        char * pIName = nsnull;
00463        char * pIEmail = nsnull;
00464        char * pIReply = nsnull;
00465        
00466        PRBool result = PR_TRUE;
00467 
00468        // The test here is:
00469        // If the smtp host is the same
00470        //     and the email address is the same (if it is supplied)
00471        //     and the reply to address is the same (if it is supplied)
00472        //     then we match regardless of the full name.
00473        
00474        PRUnichar *ppIName = nsnull;
00475 
00476        nsresult rv = pIdent->GetFullName( &ppIName);
00477        rv = pIdent->GetEmail( &pIEmail);
00478        rv = pIdent->GetReplyTo( &pIReply);
00479        
00480        if (ppIName) {
00481               nsString name(ppIName);
00482               nsCRT::free( ppIName);
00483               pIName = ToNewCString(name);
00484        }
00485 
00486        // for now, if it's the same server and reply to and email then it matches
00487        if (pReply) {
00488               if (!pIReply || nsCRT::strcasecmp( pReply, pIReply))
00489                      result = PR_FALSE;
00490        }
00491        if (pEmail) {
00492               if (!pIEmail || nsCRT::strcasecmp( pEmail, pIEmail))
00493                      result = PR_FALSE;
00494        }
00495 
00496        nsCRT::free( pIName);
00497        nsCRT::free( pIEmail);
00498        nsCRT::free( pIReply);
00499 
00500        return( result);
00501 }
00502 
00503 void OutlookSettings::SetIdentities( nsIMsgAccountManager *pMgr, nsIMsgAccount *pAcc, HKEY hKey)
00504 {
00505        // Get the relevant information for an identity
00506        char *pName = (char *)nsOutlookRegUtil::GetValueBytes( hKey, "SMTP Display Name");
00507        char *pServer = (char *)nsOutlookRegUtil::GetValueBytes( hKey, "SMTP Server");
00508        char *pEmail = (char *)nsOutlookRegUtil::GetValueBytes( hKey, "SMTP Email Address");
00509        char *pReply = (char *)nsOutlookRegUtil::GetValueBytes( hKey, "SMTP Reply To Email Address");
00510        char *pUserName = (char *)nsOutlookRegUtil::GetValueBytes( hKey, "SMTP User Name");
00511   char *pOrgName = (char *)nsOutlookRegUtil::GetValueBytes( hKey, "SMTP Organization Name");
00512 
00513        nsresult      rv;
00514 
00515        if (pEmail && pName && pServer) {
00516               // The default identity, nor any other identities matched,
00517               // create a new one and add it to the account.
00518               nsCOMPtr<nsIMsgIdentity>    id;
00519               rv = pMgr->CreateIdentity( getter_AddRefs( id));
00520               if (id) {
00521       nsAutoString name, organization;
00522       nsCOMPtr<nsIImportService> impSvc = do_GetService(NS_IMPORTSERVICE_CONTRACTID);
00523       if (impSvc)
00524       {
00525         rv = impSvc->SystemStringToUnicode((const char *)pName, name);
00526         if (NS_SUCCEEDED(rv))
00527         {
00528                      id->SetFullName( name.get());
00529                      id->SetIdentityName( name.get());
00530         }
00531         rv = impSvc->SystemStringToUnicode((const char *)pOrgName, organization);
00532         if (NS_SUCCEEDED(rv))
00533           id->SetOrganization( organization.get());
00534       }
00535                      id->SetEmail( pEmail);
00536                      if (pReply)
00537                             id->SetReplyTo( pReply);
00538                      pAcc->AddIdentity( id);
00539 
00540                      IMPORT_LOG0( "Created identity and added to the account\n");
00541                      IMPORT_LOG1( "\tname: %s\n", pName);
00542                      IMPORT_LOG1( "\temail: %s\n", pEmail);
00543               }
00544        }
00545        
00546   if (!pUserName) {
00547     nsCOMPtr <nsIMsgIncomingServer>       incomingServer;
00548     rv = pAcc->GetIncomingServer(getter_AddRefs( incomingServer));
00549     if (NS_SUCCEEDED(rv) && incomingServer) 
00550       rv = incomingServer->GetUsername(&pUserName);
00551     NS_ASSERTION(NS_SUCCEEDED(rv), "Unable to get UserName from incomingServer");
00552   }
00553 
00554        SetSmtpServer( pMgr, pAcc, pServer, pUserName);
00555 
00556        nsOutlookRegUtil::FreeValueBytes( (BYTE *)pName);
00557        nsOutlookRegUtil::FreeValueBytes( (BYTE *)pServer);
00558        nsOutlookRegUtil::FreeValueBytes( (BYTE *)pEmail);
00559        nsOutlookRegUtil::FreeValueBytes( (BYTE *)pReply);
00560        nsOutlookRegUtil::FreeValueBytes( (BYTE *)pUserName);
00561 }
00562 
00563 void OutlookSettings::SetSmtpServer( nsIMsgAccountManager *pMgr, nsIMsgAccount *pAcc, char *pServer, char *pUser)
00564 {
00565        nsresult      rv;
00566 
00567 
00568        nsCOMPtr<nsISmtpService> smtpService(do_GetService(NS_SMTPSERVICE_CONTRACTID, &rv)); 
00569        if (NS_SUCCEEDED(rv) && smtpService) {
00570               nsCOMPtr<nsISmtpServer>            foundServer;
00571        
00572               rv = smtpService->FindServer( pUser, pServer, getter_AddRefs( foundServer));
00573               if (NS_SUCCEEDED( rv) && foundServer) {
00574                      IMPORT_LOG1( "SMTP server already exists: %s\n", pServer);
00575                      return;
00576               }
00577               nsCOMPtr<nsISmtpServer>            smtpServer;
00578               
00579               rv = smtpService->CreateSmtpServer( getter_AddRefs( smtpServer));
00580               if (NS_SUCCEEDED( rv) && smtpServer) {
00581                      smtpServer->SetHostname( pServer);
00582                      if (pUser)
00583                             smtpServer->SetUsername( pUser);
00584 
00585                      IMPORT_LOG1( "Created new SMTP server: %s\n", pServer);
00586               }
00587        }
00588 }
00589 
00590