Back to index

lightning-sunbird  0.9+nobinonly
nsOESettings.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 "nsOEImport.h"
00049 #include "nsIComponentManager.h"
00050 #include "nsIServiceManager.h"
00051 #include "nsIImportService.h"
00052 #include "nsOERegUtil.h"
00053 #include "nsIMsgAccountManager.h"
00054 #include "nsIMsgAccount.h"
00055 #include "nsIImportSettings.h"
00056 #include "nsOESettings.h"
00057 #include "nsMsgBaseCID.h"
00058 #include "nsMsgCompCID.h"
00059 #include "nsISmtpService.h"
00060 #include "nsISmtpServer.h"
00061 #include "nsOEStringBundle.h"
00062 #include "OEDebugLog.h"
00063 #include "nsIPop3IncomingServer.h"
00064 #include "nsIMessengerMigrator.h"
00065 
00066 class OESettings {
00067 public:
00068        static HKEY   Find50Key( void);
00069        static HKEY   Find40Key( void);
00070        static HKEY FindAccountsKey( void);
00071 
00072        static PRBool DoImport( nsIMsgAccount **ppAccount);
00073 
00074        static PRBool DoIMAPServer( nsIMsgAccountManager *pMgr, HKEY hKey, char *pServerName, nsIMsgAccount **ppAccount);
00075        static PRBool DoPOP3Server( nsIMsgAccountManager *pMgr, HKEY hKey, char *pServerName, nsIMsgAccount **ppAccount);
00076        
00077        static void SetIdentities( nsIMsgAccountManager *pMgr, nsIMsgAccount *pAcc, HKEY hKey);
00078        static PRBool IdentityMatches( nsIMsgIdentity *pIdent, const char *pName, const char *pServer, const char *pEmail, const char *pReply, const char *pUserName);
00079 
00080        static void SetSmtpServer( nsIMsgAccountManager *pMgr, nsIMsgAccount *pAcc, char *pServer, char *pUser);
00081   static nsresult GetAccountName(HKEY hKey, char *defaultName, nsString &acctName);
00082 };
00083 
00084 
00086 nsresult nsOESettings::Create(nsIImportSettings** aImport)
00087 {
00088     NS_PRECONDITION(aImport != nsnull, "null ptr");
00089     if (! aImport)
00090         return NS_ERROR_NULL_POINTER;
00091 
00092     *aImport = new nsOESettings();
00093     if (! *aImport)
00094         return NS_ERROR_OUT_OF_MEMORY;
00095 
00096     NS_ADDREF(*aImport);
00097     return NS_OK;
00098 }
00099 
00100 nsOESettings::nsOESettings()
00101 {
00102 }
00103 
00104 nsOESettings::~nsOESettings()
00105 {
00106 }
00107 
00108 NS_IMPL_ISUPPORTS1(nsOESettings, nsIImportSettings)
00109 
00110 NS_IMETHODIMP nsOESettings::AutoLocate(PRUnichar **description, nsIFileSpec **location, PRBool *_retval)
00111 {
00112     NS_PRECONDITION(description != nsnull, "null ptr");
00113     NS_PRECONDITION(_retval != nsnull, "null ptr");
00114        if (!description || !_retval)
00115               return( NS_ERROR_NULL_POINTER);
00116        
00117        *description = nsOEStringBundle::GetStringByID( OEIMPORT_NAME);
00118        *_retval = PR_FALSE;
00119 
00120        if (location)
00121               *location = nsnull;
00122        HKEY   key;
00123        key = OESettings::Find50Key();
00124        if (key != nsnull) {
00125               *_retval = PR_TRUE;
00126               ::RegCloseKey( key);
00127        }
00128        else {
00129               key = OESettings::Find40Key();
00130               if (key != nsnull) {
00131                      *_retval = PR_TRUE;
00132                      ::RegCloseKey( key);
00133               }
00134        }
00135        if (*_retval) {
00136               key = OESettings::FindAccountsKey();
00137               if (key == nsnull) {
00138                      *_retval = PR_FALSE;
00139               }
00140               else {
00141                      ::RegCloseKey( key);
00142               }
00143        }
00144 
00145        return( NS_OK);
00146 }
00147 
00148 NS_IMETHODIMP nsOESettings::SetLocation(nsIFileSpec *location)
00149 {
00150        return( NS_OK);
00151 }
00152 
00153 NS_IMETHODIMP nsOESettings::Import(nsIMsgAccount **localMailAccount, PRBool *_retval)
00154 {
00155        NS_PRECONDITION( _retval != nsnull, "null ptr");
00156        
00157        if (OESettings::DoImport( localMailAccount)) {
00158               *_retval = PR_TRUE;
00159               IMPORT_LOG0( "Settings import appears successful\n");
00160        }
00161        else {
00162               *_retval = PR_FALSE;
00163               IMPORT_LOG0( "Settings import returned FALSE\n");
00164        }
00165 
00166        return( NS_OK);
00167 }
00168 
00169 
00170 HKEY OESettings::FindAccountsKey( void)
00171 {
00172        HKEY   sKey;
00173        if (::RegOpenKeyEx( HKEY_CURRENT_USER, "Software\\Microsoft\\Internet Account Manager\\Accounts", 0, KEY_QUERY_VALUE | KEY_ENUMERATE_SUB_KEYS, &sKey) == ERROR_SUCCESS) {
00174               return( sKey);
00175        }
00176 
00177        return( nsnull);
00178 }
00179 
00180 HKEY OESettings::Find50Key( void)
00181 {
00182        PRBool        success = PR_FALSE;
00183        HKEY          sKey;
00184 
00185        if (::RegOpenKeyEx( HKEY_CURRENT_USER, "Identities", 0, KEY_QUERY_VALUE, &sKey) == ERROR_SUCCESS) {
00186               BYTE * pBytes = nsOERegUtil::GetValueBytes( sKey, "Default User ID");
00187               ::RegCloseKey( sKey);
00188               if (pBytes) {
00189                      nsCString     key( "Identities\\");
00190                      key += (const char *)pBytes;
00191                      nsOERegUtil::FreeValueBytes( pBytes);
00192                      key += "\\Software\\Microsoft\\Outlook Express\\5.0";
00193                      if (::RegOpenKeyEx( HKEY_CURRENT_USER, key.get(), 0, KEY_QUERY_VALUE, &sKey) == ERROR_SUCCESS) {
00194                             return( sKey);
00195                      }
00196               }
00197        }
00198 
00199        return( nsnull);
00200 }
00201 
00202 HKEY OESettings::Find40Key( void)
00203 {
00204        HKEY   sKey;
00205        if (::RegOpenKeyEx( HKEY_CURRENT_USER, "Software\\Microsoft\\Outlook Express", 0, KEY_QUERY_VALUE, &sKey) == ERROR_SUCCESS) {
00206               return( sKey);
00207        }
00208 
00209        return( nsnull);
00210 }
00211 
00212 
00213 PRBool OESettings::DoImport( nsIMsgAccount **ppAccount)
00214 {
00215        HKEY   hKey = FindAccountsKey();
00216        if (hKey == nsnull) {
00217               IMPORT_LOG0( "*** Error finding Outlook Express registry account keys\n");
00218               return( PR_FALSE);
00219        }
00220 
00221        nsresult      rv;
00222        
00223        nsCOMPtr<nsIMsgAccountManager> accMgr = 
00224                 do_GetService(NS_MSGACCOUNTMANAGER_CONTRACTID, &rv);
00225     if (NS_FAILED(rv)) {
00226               IMPORT_LOG0( "*** Failed to create a account manager!\n");
00227               return( PR_FALSE);
00228        }
00229 
00230        HKEY          subKey;
00231        nsCString     defMailName;
00232 
00233        // First let's get the default mail account key name
00234        if (::RegOpenKeyEx( HKEY_CURRENT_USER, "Software\\Microsoft\\Outlook Express", 0, KEY_QUERY_VALUE, &subKey) == ERROR_SUCCESS) {
00235               BYTE * pBytes = nsOERegUtil::GetValueBytes( subKey, "Default Mail Account");
00236               ::RegCloseKey( subKey);
00237               if (pBytes) {
00238                      defMailName = (const char *)pBytes;
00239                      nsOERegUtil::FreeValueBytes( pBytes);
00240               }
00241        }
00242 
00243        // Iterate the accounts looking for POP3 & IMAP accounts...
00244        // Ignore LDAP & NNTP for now!
00245        DWORD         index = 0;
00246        DWORD         numChars;
00247        TCHAR         keyName[256];
00248        FILETIME      modTime;
00249        LONG          result = ERROR_SUCCESS;
00250        BYTE *        pBytes;
00251        int                  popCount = 0;
00252        int                  accounts = 0;
00253        nsCString     keyComp;
00254 
00255        while (result == ERROR_SUCCESS) {
00256               numChars = 256;
00257               result = ::RegEnumKeyEx( hKey, index, keyName, &numChars, NULL, NULL, NULL, &modTime);
00258               index++;
00259               if (result == ERROR_SUCCESS) {
00260                      if (::RegOpenKeyEx( hKey, keyName, 0, KEY_QUERY_VALUE, &subKey) == ERROR_SUCCESS) {
00261                             // Get the values for this account.
00262                             IMPORT_LOG1( "Opened Outlook Express account: %s\n", (char *)keyName);
00263 
00264                             nsIMsgAccount *anAccount = nsnull;
00265                             pBytes = nsOERegUtil::GetValueBytes( subKey, "IMAP Server");
00266                             if (pBytes) {
00267                                    if (DoIMAPServer( accMgr, subKey, (char *)pBytes, &anAccount))
00268                                           accounts++;
00269                                    nsOERegUtil::FreeValueBytes( pBytes);
00270                             }
00271 
00272                             pBytes = nsOERegUtil::GetValueBytes( subKey, "POP3 Server");
00273                             if (pBytes) {
00274                                    if (popCount == 0) {
00275                                           if (DoPOP3Server( accMgr, subKey, (char *)pBytes, &anAccount)) {
00276                                                  popCount++;
00277                                                  accounts++;
00278                                                  if (ppAccount && anAccount) {
00279                                                         *ppAccount = anAccount;
00280                                                         NS_ADDREF( anAccount);
00281                                                  }
00282                                           }
00283                                    }
00284                                    else {
00285                                           if (DoPOP3Server( accMgr, subKey, (char *)pBytes, &anAccount)) {
00286                                                  popCount++;
00287                                                  accounts++;
00288                                                  // If we created a mail account, get rid of it since
00289                                                  // we have 2 POP accounts!
00290                                                  if (ppAccount && *ppAccount) {
00291                                                         NS_RELEASE( *ppAccount);
00292                                                         *ppAccount = nsnull;
00293                                                  }
00294                                           }
00295                                    }
00296                                    nsOERegUtil::FreeValueBytes( pBytes);
00297                             }
00298                             
00299                             if (anAccount) {
00300                                    // Is this the default account?
00301                                    keyComp = keyName;
00302                                    if (keyComp.Equals( defMailName)) {
00303                                           accMgr->SetDefaultAccount( anAccount);
00304                                    }
00305                                    NS_RELEASE( anAccount);
00306                             }
00307 
00308                             ::RegCloseKey( subKey);
00309                      }
00310               }
00311        }
00312 
00313        // Now save the new acct info to pref file.
00314        rv = accMgr->SaveAccountInfo();
00315        NS_ASSERTION(NS_SUCCEEDED(rv), "Can't save account info to pref file");
00316 
00317        return( accounts != 0);
00318 }
00319 
00320 nsresult OESettings::GetAccountName(HKEY hKey, char *defaultName, nsString &acctName)
00321 {
00322   BYTE *pAccName = nsOERegUtil::GetValueBytes( hKey, "Account Name");
00323   nsresult rv = NS_OK;
00324   if (pAccName) {
00325     nsCOMPtr<nsIImportService> impSvc = do_GetService(NS_IMPORTSERVICE_CONTRACTID);
00326     if (impSvc)
00327       rv = impSvc->SystemStringToUnicode((const char *)pAccName, acctName);
00328     nsOERegUtil::FreeValueBytes( pAccName);
00329   }
00330   else
00331     acctName.AssignASCII(defaultName);
00332   return rv;
00333 }
00334 
00335 PRBool OESettings::DoIMAPServer( nsIMsgAccountManager *pMgr, HKEY hKey, char *pServerName, nsIMsgAccount **ppAccount)
00336 {
00337        if (ppAccount)
00338               *ppAccount = nsnull;
00339 
00340        BYTE *pBytes;
00341        pBytes = nsOERegUtil::GetValueBytes( hKey, "IMAP User Name");
00342        if (!pBytes)
00343               return( PR_FALSE);
00344 
00345        PRBool result = PR_FALSE;
00346 
00347        // I now have a user name/server name pair, find out if it already exists?
00348        nsCOMPtr<nsIMsgIncomingServer>     in;
00349        nsresult rv = pMgr->FindServer( (const char *)pBytes, pServerName, "imap", getter_AddRefs( in));
00350        if (NS_FAILED( rv) || (in == nsnull)) {
00351               // Create the incoming server and an account for it?
00352               rv = pMgr->CreateIncomingServer( (const char *)pBytes, pServerName, "imap", getter_AddRefs( in));
00353               if (NS_SUCCEEDED( rv) && in) {
00354                      rv = in->SetType( "imap");
00355                      // rv = in->SetHostName( pServerName);
00356                      // rv = in->SetUsername( (char *)pBytes);
00357                      
00358                      IMPORT_LOG2( "Created IMAP server named: %s, userName: %s\n", pServerName, (char *)pBytes);
00359 
00360                      nsString      prettyName;
00361       if (NS_SUCCEEDED(GetAccountName(hKey, pServerName, prettyName)))
00362       {
00363                      PRUnichar *pretty = ToNewUnicode(prettyName);
00364         if (pretty)
00365         {
00366                      rv = in->SetPrettyName( pretty);
00367                      nsCRT::free( pretty);
00368         }
00369       }
00370                      
00371                      // We have a server, create an account.
00372                      nsCOMPtr<nsIMsgAccount>     account;
00373                      rv = pMgr->CreateAccount( getter_AddRefs( account));
00374                      if (NS_SUCCEEDED( rv) && account) {
00375                             rv = account->SetIncomingServer( in);     
00376                             
00377                             IMPORT_LOG0( "Created an account and set the IMAP server as the incoming server\n");
00378 
00379                             // Fiddle with the identities
00380                             SetIdentities( pMgr, account, hKey);
00381                             result = PR_TRUE;
00382                             if (ppAccount)
00383                                    account->QueryInterface( NS_GET_IID(nsIMsgAccount), (void **)ppAccount);
00384                      }                           
00385               }
00386        }
00387        else
00388               result = PR_TRUE;
00389        
00390        nsOERegUtil::FreeValueBytes( pBytes);
00391 
00392        return( result);
00393 }
00394 
00395 PRBool OESettings::DoPOP3Server( nsIMsgAccountManager *pMgr, HKEY hKey, char *pServerName, nsIMsgAccount **ppAccount)
00396 {
00397        if (ppAccount)
00398               *ppAccount = nsnull;
00399 
00400        BYTE *pBytes;
00401        pBytes = nsOERegUtil::GetValueBytes( hKey, "POP3 User Name");
00402        if (!pBytes)
00403               return( PR_FALSE);
00404 
00405        PRBool result = PR_FALSE;
00406 
00407        // I now have a user name/server name pair, find out if it already exists?
00408        nsCOMPtr<nsIMsgIncomingServer>     in;
00409        nsresult rv = pMgr->FindServer( (const char *)pBytes, pServerName, "pop3", getter_AddRefs( in));
00410        if (NS_FAILED( rv) || (in == nsnull)) {
00411               // Create the incoming server and an account for it?
00412               rv = pMgr->CreateIncomingServer( (const char *)pBytes, pServerName, "pop3", getter_AddRefs( in));
00413               if (NS_SUCCEEDED( rv) && in) {
00414                      rv = in->SetType( "pop3");
00415                      rv = in->SetHostName( pServerName);
00416                      rv = in->SetUsername( (char *)pBytes);
00417 
00418             nsCOMPtr<nsIPop3IncomingServer> pop3Server = do_QueryInterface(in);
00419             if (pop3Server) {
00420                 // set local folders as the Inbox to use for this POP3 server
00421                 nsCOMPtr<nsIMsgIncomingServer> localFoldersServer;
00422                 pMgr->GetLocalFoldersServer(getter_AddRefs(localFoldersServer));
00423 
00424                 if (!localFoldersServer)
00425                 {
00426                     // XXX: We may need to move this local folder creation code to the generic nsImportSettings code
00427                     // if the other import modules end up needing to do this too.
00428 
00429                     // if Local Folders does not exist already, create it
00430                     nsCOMPtr <nsIMessengerMigrator> messengerMigrator = do_GetService(NS_MESSENGERMIGRATOR_CONTRACTID, &rv);
00431                     if (NS_FAILED(rv)) {
00432                         IMPORT_LOG0( "*** Failed to create messenger migrator!\n");
00433                         return PR_FALSE;
00434                     }
00435           
00436                     rv = messengerMigrator->CreateLocalMailAccount(PR_FALSE);
00437                     if (NS_FAILED(rv)) {
00438                         IMPORT_LOG0( "*** Failed to create Local Folders!\n");
00439                         return PR_FALSE;
00440                     }
00441     
00442                     pMgr->GetLocalFoldersServer(getter_AddRefs(localFoldersServer)); 
00443                 }
00444 
00445                 // now get the account for this server
00446                 nsCOMPtr<nsIMsgAccount> localFoldersAccount;
00447                 pMgr->FindAccountForServer(localFoldersServer, getter_AddRefs(localFoldersAccount)); 
00448                 if (localFoldersAccount)
00449                 {
00450                     nsXPIDLCString localFoldersAcctKey;
00451                     localFoldersAccount->GetKey(getter_Copies(localFoldersAcctKey));
00452                     pop3Server->SetDeferredToAccount(localFoldersAcctKey.get()); 
00453                     pop3Server->SetDeferGetNewMail(PR_TRUE);
00454                 }
00455             }
00456 
00457                      IMPORT_LOG2( "Created POP3 server named: %s, userName: %s\n", pServerName, (char *)pBytes);
00458 
00459                      nsString      prettyName;
00460       if (NS_SUCCEEDED(GetAccountName(hKey, pServerName, prettyName)))
00461       {
00462                      PRUnichar *pretty = ToNewUnicode(prettyName);
00463         if (pretty)
00464         {
00465                      rv = in->SetPrettyName( pretty);
00466                      nsCRT::free( pretty);
00467         }
00468       }
00469                      
00470                      // We have a server, create an account.
00471                      nsCOMPtr<nsIMsgAccount>     account;
00472                      rv = pMgr->CreateAccount( getter_AddRefs( account));
00473                      if (NS_SUCCEEDED( rv) && account) {
00474                             rv = account->SetIncomingServer( in);
00475                             IMPORT_LOG0( "Created a new account and set the incoming server to the POP3 server.\n");
00476                                    
00477         nsCOMPtr<nsIPop3IncomingServer> pop3Server = do_QueryInterface(in, &rv);
00478         NS_ENSURE_SUCCESS(rv,rv);
00479         BYTE *pLeaveOnServer = nsOERegUtil::GetValueBytes( hKey, "Leave Mail On Server");
00480         if (pLeaveOnServer)
00481         {
00482           pop3Server->SetLeaveMessagesOnServer(*pLeaveOnServer == 1 ? PR_TRUE : PR_FALSE);
00483           nsOERegUtil::FreeValueBytes(pLeaveOnServer);
00484         }
00485 
00486         // Fiddle with the identities
00487                             SetIdentities( pMgr, account, hKey);
00488                             result = PR_TRUE;
00489                             if (ppAccount)
00490                                    account->QueryInterface( NS_GET_IID(nsIMsgAccount), (void **)ppAccount);
00491                      }                           
00492               }
00493        }
00494        else
00495               result = PR_TRUE;
00496        
00497        nsOERegUtil::FreeValueBytes( pBytes);
00498 
00499        return( result);
00500 }
00501 
00502 
00503 PRBool OESettings::IdentityMatches( nsIMsgIdentity *pIdent, const char *pName, const char *pServer, const char *pEmail, const char *pReply, const char *pUserName)
00504 {
00505        if (!pIdent)
00506               return( PR_FALSE);
00507 
00508        char * pIName = nsnull;
00509        char * pIEmail = nsnull;
00510        char * pIReply = nsnull;
00511        
00512        PRBool result = PR_TRUE;
00513 
00514        // The test here is:
00515        // If the smtp host is the same
00516        //     and the email address is the same (if it is supplied)
00517        //     and the reply to address is the same (if it is supplied)
00518        //     then we match regardless of the full name.
00519        PRUnichar *   ppIName = nsnull;
00520 
00521        nsresult rv = pIdent->GetFullName( &ppIName);
00522        rv = pIdent->GetEmail( &pIEmail);
00523        rv = pIdent->GetReplyTo( &pIReply);
00524        if (ppIName) {
00525               nsString name(ppIName);
00526               nsCRT::free( ppIName);
00527               pIName = ToNewCString(name);
00528        }
00529 
00530        // for now, if it's the same server and reply to and email then it matches
00531        if (pReply) {
00532               if (!pIReply || nsCRT::strcasecmp( pReply, pIReply))
00533                      result = PR_FALSE;
00534        }
00535        if (pEmail) {
00536               if (!pIEmail || nsCRT::strcasecmp( pEmail, pIEmail))
00537                      result = PR_FALSE;
00538        }
00539 
00540        nsCRT::free( pIName);
00541        nsCRT::free( pIEmail);
00542        nsCRT::free( pIReply);
00543 
00544        return( result);
00545 }
00546 
00547 void OESettings::SetIdentities( nsIMsgAccountManager *pMgr, nsIMsgAccount *pAcc, HKEY hKey)
00548 {
00549        // Get the relevant information for an identity
00550        char *pName = (char *)nsOERegUtil::GetValueBytes( hKey, "SMTP Display Name");
00551        char *pServer = (char *)nsOERegUtil::GetValueBytes( hKey, "SMTP Server");
00552        char *pEmail = (char *)nsOERegUtil::GetValueBytes( hKey, "SMTP Email Address");
00553        char *pReply = (char *)nsOERegUtil::GetValueBytes( hKey, "SMTP Reply To Email Address");
00554        char *pUserName = (char *)nsOERegUtil::GetValueBytes( hKey, "SMTP User Name");
00555   char *pOrgName = (char *)nsOERegUtil::GetValueBytes( hKey, "SMTP Organization Name");
00556 
00557        nsresult      rv;
00558 
00559        if (pEmail && pName && pServer) {
00560               // The default identity, nor any other identities matched,
00561               // create a new one and add it to the account.
00562               nsCOMPtr<nsIMsgIdentity>    id;
00563               rv = pMgr->CreateIdentity( getter_AddRefs( id));
00564               if (id) {
00565       nsAutoString fullName, organization;
00566       nsCOMPtr<nsIImportService> impSvc = do_GetService(NS_IMPORTSERVICE_CONTRACTID);
00567       if (impSvc)
00568       {
00569         rv = impSvc->SystemStringToUnicode((const char *)pName, fullName);
00570         if (NS_SUCCEEDED(rv))
00571         {
00572                      id->SetFullName( fullName.get());
00573                      id->SetIdentityName( fullName.get());
00574         }
00575         rv = impSvc->SystemStringToUnicode((const char *)pOrgName, organization);
00576         if (NS_SUCCEEDED(rv))
00577           id->SetOrganization( organization.get());
00578       }
00579                      id->SetEmail( pEmail);
00580                      if (pReply)
00581                             id->SetReplyTo( pReply);
00582 
00583       // Outlook Express users are used to top style quoting.
00584       id->SetReplyOnTop(1); 
00585                      pAcc->AddIdentity( id);
00586 
00587                      IMPORT_LOG0( "Created identity and added to the account\n");
00588                      IMPORT_LOG1( "\tname: %s\n", pName);
00589                      IMPORT_LOG1( "\temail: %s\n", pEmail);
00590               }
00591        }
00592   if (!pUserName) {
00593     nsCOMPtr <nsIMsgIncomingServer>       incomingServer;
00594     rv = pAcc->GetIncomingServer(getter_AddRefs( incomingServer));
00595     if (NS_SUCCEEDED(rv) && incomingServer) 
00596       rv = incomingServer->GetUsername(&pUserName);
00597     NS_ASSERTION(NS_SUCCEEDED(rv), "Unable to get UserName from incomingServer");
00598   }
00599 
00600        SetSmtpServer( pMgr, pAcc, pServer, pUserName);
00601 
00602        nsOERegUtil::FreeValueBytes( (BYTE *)pName);
00603        nsOERegUtil::FreeValueBytes( (BYTE *)pServer);
00604        nsOERegUtil::FreeValueBytes( (BYTE *)pEmail);
00605        nsOERegUtil::FreeValueBytes( (BYTE *)pReply);
00606        nsOERegUtil::FreeValueBytes( (BYTE *)pUserName);
00607 }
00608 
00609 void OESettings::SetSmtpServer( nsIMsgAccountManager *pMgr, nsIMsgAccount *pAcc, char *pServer, char *pUser)
00610 {
00611        nsresult      rv;
00612 
00613 
00614        nsCOMPtr<nsISmtpService> smtpService(do_GetService(NS_SMTPSERVICE_CONTRACTID, &rv)); 
00615        if (NS_SUCCEEDED(rv) && smtpService) {
00616               nsCOMPtr<nsISmtpServer>            foundServer;
00617        
00618               rv = smtpService->FindServer( pUser, pServer, getter_AddRefs( foundServer));
00619               if (NS_SUCCEEDED( rv) && foundServer) {
00620                      IMPORT_LOG1( "SMTP server already exists: %s\n", pServer);
00621                      return;
00622               }
00623               nsCOMPtr<nsISmtpServer>            smtpServer;
00624               
00625               rv = smtpService->CreateSmtpServer( getter_AddRefs( smtpServer));
00626               if (NS_SUCCEEDED( rv) && smtpServer) {
00627                      smtpServer->SetHostname( pServer);
00628                      if (pUser)
00629                             smtpServer->SetUsername( pUser);
00630 
00631                      IMPORT_LOG1( "Created new SMTP server: %s\n", pServer);
00632               }
00633        }
00634 
00635        /*
00636               nsXPIDLCString                            hostName;
00637         nsXPIDLCString                           senderName;
00638               smtpServer->GetHostname( getter_Copies(hostName));
00639               smtpServer->GetUsername( getter_Copies(senderName));
00640               */
00641 }
00642 
00643