William Bowling is sharing code with you

Bitbucket is a code hosting site. Unlimited public and private repositories. Free for small teams.

Don't show this again

wbowling / adium (fork of adium / adium)

Fork of Adium for patches/improvements

Clone this repository (size: 338.7 MB): HTTPS / SSH
hg clone https://bitbucket.org/wbowling/adium
hg clone ssh://hg@bitbucket.org/wbowling/adium

adium / Source / AdiumPasswords.m

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
/* 
 * Adium is the legal property of its developers, whose names are listed in the copyright file included
 * with this source distribution.
 * 
 * This program is free software; you can redistribute it and/or modify it under the terms of the GNU
 * General Public License as published by the Free Software Foundation; either version 2 of the License,
 * or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
 * the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General
 * Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along with this program; if not,
 * write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

#import "AdiumPasswords.h"
#import <Adium/AIAccountControllerProtocol.h>
#import <Adium/AILoginControllerProtocol.h>
#import <Adium/AIAccount.h>
#import <Adium/AIService.h>
#import <AIUtilities/AIKeychain.h>
#import <AIUtilities/AIObjectAdditions.h>
#import <AIUtilities/AIStringAdditions.h>
#import <objc/objc-runtime.h>
#import <libkern/OSAtomic.h>

#import "AISpecialPasswordPromptController.h"
#import "ESAccountPasswordPromptController.h"
#import "ESProxyPasswordPromptController.h"

#define KEY_PERFORMED_ACCOUNT_PASSWORD_UPGRADE @"Adium 1.3: Account Passwords Upgraded"

@interface AdiumPasswords ()
+ (NSOperationQueue *)operationQueue;
- (NSString *)_oldStyleAccountNameForAccount:(AIAccount *)inAccount;
- (NSString *)_passKeyForAccount:(AIAccount *)inAccount;
- (NSString *)_accountNameForAccount:(AIAccount *)inAccount;
- (NSString *)_accountNameForSpecialPassword:(AISpecialPasswordType)inType account:(AIAccount *)inAccount name:(NSString *)inName;
- (NSString *)_serverNameForAccount:(AIAccount *)inAccount;
- (NSString *)_accountNameForProxyServer:(NSString *)proxyServer userName:(NSString *)userName;
- (NSString *)_passKeyForProxyServer:(NSString *)proxyServer;
- (void)_upgradeAccountPasswordKeychainEntries;
@end

@implementation AdiumPasswords

/*!
 * @brief Finish Initing
 *
 * Requires that all accounts have been loaded
 */
- (void)controllerDidLoad 
 {
        [self _upgradeAccountPasswordKeychainEntries];
}

+ (NSOperationQueue *)operationQueue {
        static OSSpinLock spinLock = OS_SPINLOCK_INIT;
        static NSOperationQueue *passwordQueue = nil;
        
        OSSpinLockLock(&spinLock);
        if (!passwordQueue) {
                passwordQueue = [[NSOperationQueue alloc] init];
                if([passwordQueue respondsToSelector:@selector(setName:)]) {
                        [passwordQueue setName:@"AdiumPasswordsOperationQueue"];
                }
        }
        OSSpinLockUnlock(&spinLock);
        
        return passwordQueue;
}

//Accounts -------------------------------------------------------------------------------------------------------------
#pragma mark Accounts

/*!
 * @brief Set the password of an account
 *
 * @param inPassword password to store
 * @param inAccount account the password belongs to
 */
- (void)setPassword:(NSString *)inPassword forAccount:(AIAccount *)inAccount
{
        NSError *error = nil;
        [[AIKeychain defaultKeychain_error:&error] setInternetPassword:inPassword
                                                                                                                 forServer:[self _serverNameForAccount:inAccount]
                                                                                                                   account:[self _accountNameForAccount:inAccount]
                                                                                                                  protocol:FOUR_CHAR_CODE('AdIM')
                                                                                                                         error:&error];
        if (error) {
                OSStatus err = [error code];
                /*errSecItemNotFound: no entry in the keychain. a harmless error.
                 *we don't ignore it if we're trying to set the password, though (because that would be strange).
                 *we don't get here at all for noErr (error will be nil).
                 */
                if (inPassword || (err != errSecItemNotFound)) {
                        NSDictionary *userInfo = [error userInfo];
                        NSLog(@"could not %@ password for account %@: %@ returned %i (%@)", inPassword ? @"set" : @"remove", [self _accountNameForAccount:inAccount], [userInfo objectForKey:AIKEYCHAIN_ERROR_USERINFO_SECURITYFUNCTIONNAME], err, [userInfo objectForKey:AIKEYCHAIN_ERROR_USERINFO_ERRORDESCRIPTION]);
                }
        }
}

/*!
 * @brief Forget the password of an account
 *
 * @param inAccount account whose password should be forgotten
 */
- (void)forgetPasswordForAccount:(AIAccount *)inAccount
{
        NSError         *error    = nil;
        AIKeychain      *keychain = [AIKeychain defaultKeychain_error:&error];
        [keychain deleteInternetPasswordForServer:[self _serverNameForAccount:inAccount]
                                                                          account:[self _accountNameForAccount:inAccount]
                                                                         protocol:FOUR_CHAR_CODE('AdIM')
                                                                                error:&error];

        if (error) {
                OSStatus err = [error code];
                /*errSecItemNotFound: no entry in the keychain. a harmless error.
                 *we don't get here at all for noErr (error will be nil).
                 */
                if (err != errSecItemNotFound) {
                        NSDictionary *userInfo = [error userInfo];
                        NSLog(@"could not delete password for account %@: %@ returned %i (%@)", [self _accountNameForAccount:inAccount], [userInfo objectForKey:AIKEYCHAIN_ERROR_USERINFO_SECURITYFUNCTIONNAME], err, [userInfo objectForKey:AIKEYCHAIN_ERROR_USERINFO_ERRORDESCRIPTION]);
                }
        }
}

/*!
 * @brief Retrieve the password of an account
 * 
 * @param inAccount account whose password is desired
 * @return account password, or nil if the password is not available
 */
- (NSString *)passwordForAccount:(AIAccount *)inAccount
{
        NSError         *error    = nil;
        AIKeychain      *keychain = [AIKeychain defaultKeychain_error:&error];
        NSString        *password = [keychain internetPasswordForServer:[self _serverNameForAccount:inAccount]
                                                                                                                account:[self _accountNameForAccount:inAccount]
                                                                                                           protocol:FOUR_CHAR_CODE('AdIM')
                                                                                                                  error:&error];
        
        if (error) {
                OSStatus err = [error code];
                /*errSecItemNotFound: no entry in the keychain. a harmless error.
                 *we don't get here at all for noErr (error will be nil).
                 */
                if (err != errSecItemNotFound) {
                        NSDictionary *userInfo = [error userInfo];
                        NSLog(@"could not retrieve password for account %@: %@ returned %i (%@)", [self _accountNameForAccount:inAccount], [userInfo objectForKey:AIKEYCHAIN_ERROR_USERINFO_SECURITYFUNCTIONNAME], err, [userInfo objectForKey:AIKEYCHAIN_ERROR_USERINFO_ERRORDESCRIPTION]);
                }
        }
        return password;
}

- (void)retrievedPassword:(NSDictionary *)requestDict
{
        NSString                *password = [requestDict objectForKey:@"Password"];
        AIAccount               *account = [requestDict objectForKey:@"Account"];
        AIPromptOption  promptOption = [[requestDict objectForKey:@"AIPromptOption"] integerValue];
        id                              target = [requestDict objectForKey:@"Target"];
        SEL                             selector = NSSelectorFromString([requestDict objectForKey:@"Selector"]);
        id                              context = [requestDict objectForKey:@"Context"];
        BOOL                    shouldPrompt = YES;

        switch (promptOption)
        {
                case AIPromptAlways:
                        shouldPrompt = YES;
                        break;
                case AIPromptAsNeeded:
                {
                        if (password && [password length])
                                shouldPrompt = NO;
                        else 
                                shouldPrompt = YES;
                        break;
                }
                case AIPromptNever:
                        shouldPrompt = NO;
                        break;
        }
        
        if (shouldPrompt) {
                //Prompt the user for their password
                [ESAccountPasswordPromptController showPasswordPromptForAccount:account
                                                                                                                           password:password
                                                                                                                notifyingTarget:target
                                                                                                                           selector:selector
                                                                                                                                context:context];
        } else {
                //Invoke the target right away
                void (*targetMethodSender)(id, SEL, id, AIPasswordPromptReturn, id) = (void (*)(id, SEL, id, AIPasswordPromptReturn, id)) objc_msgSend;
                targetMethodSender(target, selector, password, AIPasswordPromptOKReturn, context);
        }       
}

- (void)threadedPasswordRetrieval:(NSMutableDictionary *)requestDict
{
        NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
        NSString        *password = [self passwordForAccount:[requestDict objectForKey:@"Account"]];
        if (password)
                [requestDict setObject:password forKey:@"Password"];

        [self performSelectorOnMainThread:@selector(retrievedPassword:)
                                                   withObject:requestDict
                                                waitUntilDone:NO];
        [pool release];
}

/*!
 * @brief Retrieve the password of an account, prompting the user if necessary
 *
 * @param inAccount account whose password is desired
 * @param promptOption An AIPromptOption determining whether and how a prompt for the password should be displayed if it is needed. This allows forcing or suppressing of the prompt dialogue.
 * @param inTarget target to notify when password is available
 * @param inSelector selector to notify when password is available
 * @param inContext context passed to target
 */
- (void)passwordForAccount:(AIAccount *)inAccount promptOption:(AIPromptOption)promptOption notifyingTarget:(id)inTarget selector:(SEL)inSelector context:(id)inContext
{
        [[AdiumPasswords operationQueue] addOperation:[[[NSInvocationOperation alloc]
                                                                                                                                                                                                        initWithTarget:self
                                                                                                                                                                                                              selector:@selector(threadedPasswordRetrieval:)
                                                                                                                                                                                                                object:[NSMutableDictionary dictionaryWithObjectsAndKeys:
                                                                                                                                                                                                                                                inAccount, @"Account",
                                                                                                                                                                                                                                                [NSNumber numberWithInteger:promptOption], @"AIPromptOption",
                                                                                                                                                                                                                                                inTarget, @"Target",
                                                                                                                                                                                                                                                NSStringFromSelector(inSelector), @"Selector",
                                                                                                                                                                                                                                                inContext, @"Context" /* may be nil so should be last */,
                                                                                                                                                                                                                                                nil]]
                                                                                                                                                                                                  autorelease]];
}

//Proxy Servers --------------------------------------------------------------------------------------------------------
#pragma mark Proxy Servers

/*!
 * @brief Set the password for a proxy server
 *
 * @param inPassword password to store
 * @param server proxy server name
 * @param userName proxy server user name
 *
 * XXX - This is inconsistent.  Above we have a separate forget method, here we forget when nil is passed...
 */
- (void)setPassword:(NSString *)inPassword forProxyServer:(NSString *)server userName:(NSString *)userName
{
        NSError *error = nil;
        [[AIKeychain defaultKeychain_error:&error] setInternetPassword:inPassword
                                                                                                                 forServer:[self _passKeyForProxyServer:server]
                                                                                                                   account:[self _accountNameForProxyServer:server 
                                                                                                                                                                                   userName:userName]
                                                                                                                  protocol:FOUR_CHAR_CODE('AdIM')
                                                                                                                         error:&error];
        if (error) {
                OSStatus err = [error code];
                /*errSecItemNotFound: no entry in the keychain. a harmless error.
                 *we don't ignore it if we're trying to set the password, though (because that would be strange).
                 *we don't get here at all for noErr (error will be nil).
                 */
                if (inPassword || (err != errSecItemNotFound)) {
                        NSDictionary *userInfo = [error userInfo];
                        NSLog(@"could not %@ password for proxy server %@: %@ returned %i (%@)",
                              inPassword ? @"set" : @"remove",
                              [self _accountNameForProxyServer:server
                                                          userName:userName],
                                  [userInfo objectForKey:AIKEYCHAIN_ERROR_USERINFO_SECURITYFUNCTIONNAME],
                                  err,
                                  [userInfo objectForKey:AIKEYCHAIN_ERROR_USERINFO_ERRORDESCRIPTION]);
                }
        }
}

/*!
 * @brief Retrieve the password for a proxy server
 * 
 * @param server proxy server name
 * @param userName proxy server user name
 * @return proxy server password, or nil if the password is not available
 */
- (NSString *)passwordForProxyServer:(NSString *)server userName:(NSString *)userName
{
        NSError         *error    = nil;
        AIKeychain      *keychain = [AIKeychain defaultKeychain_error:&error];
        NSString        *password = [keychain internetPasswordForServer:[self _passKeyForProxyServer:server]
                                                                                                                account:[self _accountNameForProxyServer:server 
                                                                                                                                                                                userName:userName]
                                                                                                           protocol:FOUR_CHAR_CODE('AdIM')
                                                                                                                  error:&error];
        if (error) {
                OSStatus err = [error code];
                /*errSecItemNotFound: no entry in the keychain. a harmless error.
                 *we don't get here at all for noErr (error will be nil).
                 */
                if (err != errSecItemNotFound) {
                        NSDictionary *userInfo = [error userInfo];
                        NSLog(@"could not retrieve password for proxy server %@: %@ returned %i (%@)",
                                  [self _accountNameForProxyServer:server
                                                          userName:userName],
                                  [userInfo objectForKey:AIKEYCHAIN_ERROR_USERINFO_SECURITYFUNCTIONNAME],
                                  err,
                                  [userInfo objectForKey:AIKEYCHAIN_ERROR_USERINFO_ERRORDESCRIPTION]);
                }
        }
        return password;
}

/*!
 * @brief Retrieve the password for a proxy server, prompting the user if necessary
 *
 * @param server proxy server name
 * @param userName proxy server user name
 * @param inTarget target to notify when password is available
 * @param inSelector selector to notify when password is available
 * @param inContext context passed to target
 */
- (void)passwordForProxyServer:(NSString *)server userName:(NSString *)userName notifyingTarget:(id)inTarget selector:(SEL)inSelector context:(id)inContext
{
        NSString        *password = [self passwordForProxyServer:server userName:userName];
        
        if (password && [password length] != 0) {
                //Invoke the target right away
                void (*targetMethodSender)(id, SEL, id, AIPasswordPromptReturn, id) = (void (*)(id, SEL, id, AIPasswordPromptReturn, id)) objc_msgSend;
                targetMethodSender(inTarget, inSelector, password, AIPasswordPromptOKReturn, inContext);

        } else {
                //Prompt the user for their password
                [ESProxyPasswordPromptController showPasswordPromptForProxyServer:server
                                                                                                                                 userName:userName
                                                                                                                  notifyingTarget:inTarget
                                                                                                                                 selector:inSelector
                                                                                                                                  context:inContext];
        }
}

#pragma mark Special passwords
- (void)setPassword:(NSString *)inPassword forType:(AISpecialPasswordType)inType forAccount:(AIAccount *)inAccount name:(NSString *)inName 
{
        NSError *error = nil;
        [[AIKeychain defaultKeychain_error:&error] setInternetPassword:inPassword
                                                                                                                 forServer:[self _serverNameForAccount:inAccount]
                                                                                                                   account:[self _accountNameForSpecialPassword:inType account:inAccount name:inName.lowercaseString]
                                                                                                                  protocol:FOUR_CHAR_CODE('AdIM')
                                                                                                                         error:&error];
        if (error) {
                OSStatus err = [error code];
                /*errSecItemNotFound: no entry in the keychain. a harmless error.
                 *we don't ignore it if we're trying to set the password, though (because that would be strange).
                 *we don't get here at all for noErr (error will be nil).
                 */
                if (inPassword || (err != errSecItemNotFound)) {
                        NSDictionary *userInfo = [error userInfo];
                        NSLog(@"could not %@ password for special password %@: %@ returned %i (%@)",
                              inPassword ? @"set" : @"remove",
                              [self _accountNameForSpecialPassword:inType account:inAccount name:inName],
                                  [userInfo objectForKey:AIKEYCHAIN_ERROR_USERINFO_SECURITYFUNCTIONNAME],
                                  err,
                                  [userInfo objectForKey:AIKEYCHAIN_ERROR_USERINFO_ERRORDESCRIPTION]);
                }
        }
}

- (NSString *)passwordForType:(AISpecialPasswordType)inType forAccount:(AIAccount *)inAccount name:(NSString *)inName
{
        NSError         *error    = nil;
        AIKeychain      *keychain = [AIKeychain defaultKeychain_error:&error];
        NSString        *password = [keychain internetPasswordForServer:[self _serverNameForAccount:inAccount]
                                                                                                                account:[self _accountNameForSpecialPassword:inType account:inAccount name:inName.lowercaseString]
                                                                                                           protocol:FOUR_CHAR_CODE('AdIM')
                                                                                                                  error:&error];
        if (error) {
                OSStatus err = [error code];
                /*errSecItemNotFound: no entry in the keychain. a harmless error.
                 *we don't get here at all for noErr (error will be nil).
                 */
                if (err != errSecItemNotFound) {
                        NSDictionary *userInfo = [error userInfo];
                        NSLog(@"could not retrieve password for special password %@: %@ returned %i (%@)",
                                  [self _accountNameForSpecialPassword:inType account:inAccount name:inName],
                                  [userInfo objectForKey:AIKEYCHAIN_ERROR_USERINFO_SECURITYFUNCTIONNAME],
                                  err,
                                  [userInfo objectForKey:AIKEYCHAIN_ERROR_USERINFO_ERRORDESCRIPTION]);
                }
        }
        return password;
}

- (void)passwordForType:(AISpecialPasswordType)inType forAccount:(AIAccount *)inAccount promptOption:(AIPromptOption)inOption name:(NSString *)inName notifyingTarget:(id)inTarget selector:(SEL)inSelector context:(id)inContext
{
        NSString        *password = [self passwordForType:inType forAccount:inAccount name:inName.lowercaseString];
        
        if (inOption != AIPromptAlways && password && [password length] != 0) {
                //Invoke the target right away
                void (*targetMethodSender)(id, SEL, id, AIPasswordPromptReturn, id) = (void (*)(id, SEL, id, AIPasswordPromptReturn, id)) objc_msgSend;
                targetMethodSender(inTarget, inSelector, password, AIPasswordPromptOKReturn, inContext);
        } else {
                //Prompt the user for their password
                [AISpecialPasswordPromptController showPasswordPromptForType:inType
                                                                                                                         account:inAccount
                                                                                                                                name:inName
                                                                                                                        password:password
                                                                                                         notifyingTarget:inTarget
                                                                                                                        selector:inSelector
                                                                                                                         context:inContext];
        }       
}

//Password Keys --------------------------------------------------------------------------------------------------------
#pragma mark Password Keys

/*!
 * @brief Old-style Keychain identifier for an account
 */
- (NSString *)_oldStyleAccountNameForAccount:(AIAccount *)inAccount{
        return [NSString stringWithFormat:@"%@.%@", inAccount.service.serviceID, inAccount.internalObjectID];
}
- (NSString *)_passKeyForAccount:(AIAccount *)inAccount{
        if ([[adium.loginController userArray] count] > 1) {
                return [NSString stringWithFormat:@"Adium.%@.%@", adium.loginController.currentUser, [self _oldStyleAccountNameForAccount:inAccount]];
        } else {
                return [NSString stringWithFormat:@"Adium.%@",[self _oldStyleAccountNameForAccount:inAccount]];
        }
}

/*!
 * @brief New-style Keychain identifier for an account
 */
- (NSString *)_accountNameForAccount:(AIAccount *)inAccount {
        if (inAccount.useInternalObjectIDForPasswordName) {
                return [NSString stringWithFormat:@"%@.%@", inAccount.service.serviceID, inAccount.internalObjectID];
        } else {
                return [inAccount.UID compactedString];
        }
}
- (NSString *)_serverNameForAccount:(AIAccount *)inAccount {
        if(inAccount.useHostForPasswordServerName) {
                return [NSString stringWithFormat:@"%@.%@", inAccount.service.serviceID, inAccount.host];
        } else {
                return [NSString stringWithFormat:@"%@.%@", inAccount.service.serviceID, [self _accountNameForAccount:inAccount]];
        }
}

/*!
 * @brief Special password name
 */
- (NSString *)_accountNameForSpecialPassword:(AISpecialPasswordType)inType account:(AIAccount *)inAccount name:(NSString *)inName
{
        return [NSString stringWithFormat:@"%@.%d.%@", [self _accountNameForAccount:inAccount], inType, inName];
}

/*!
 * @brief Keychain identifier for a proxy server
 */
- (NSString *)_accountNameForProxyServer:(NSString *)proxyServer userName:(NSString *)userName{
        return [NSString stringWithFormat:@"%@.%@",proxyServer,userName];
}
- (NSString *)_passKeyForProxyServer:(NSString *)proxyServer{
        if ([[adium.loginController userArray] count] > 1) {
                return [NSString stringWithFormat:@"Adium.%@.%@",[adium.loginController currentUser],proxyServer];
        } else {
                return [NSString stringWithFormat:@"Adium.%@",proxyServer];     
        }
}

#pragma mark Upgrade code

/*!
 * @brief Changes the naming of the Keychain password entries from AdIM://Adium.{Service ID}.{Account internalObjectID} to AdIM://{Service ID}.{Account UID}.
 */
- (void)_upgradeAccountPasswordKeychainEntries
{
        if (![[NSUserDefaults standardUserDefaults] boolForKey:KEY_PERFORMED_ACCOUNT_PASSWORD_UPGRADE]) {
                NSError                 *error;
                AIKeychain              *keychain = [AIKeychain defaultKeychain_error:&error];
                
                for (AIAccount *account in adium.accountController.accounts) {
                        NSString *password = [keychain internetPasswordForServer:[self _passKeyForAccount:account]
                                                                                                   account:[self _oldStyleAccountNameForAccount:account]
                                                                                                  protocol:FOUR_CHAR_CODE('AdIM')
                                                                                                         error:&error];
                        if (error) {
                                OSStatus err = [error code];
                                if (err != errSecItemNotFound) {
                                        NSDictionary *userInfo = [error userInfo];
                                        NSLog(@"could not retrieve password for account %@: %@ returned %i (%@)", [self _oldStyleAccountNameForAccount:account], [userInfo objectForKey:AIKEYCHAIN_ERROR_USERINFO_SECURITYFUNCTIONNAME], err, [userInfo objectForKey:AIKEYCHAIN_ERROR_USERINFO_ERRORDESCRIPTION]);
                                }
                        } else {
                                [self setPassword:password forAccount:account];
                                
                                // Delete old keychain entry
                                [keychain deleteInternetPasswordForServer:[self _passKeyForAccount:account]
                                                                                                  account:[self _oldStyleAccountNameForAccount:account]
                                                                                                 protocol:FOUR_CHAR_CODE('AdIM')
                                                                                                        error:&error];
                                if (error) {
                                        OSStatus err = [error code];
                                        /*errSecItemNotFound: no entry in the keychain. a harmless error.
                                         *we don't get here at all for noErr (error will be nil).
                                         */
                                        if (err != errSecItemNotFound) {
                                                NSDictionary *userInfo = [error userInfo];
                                                NSLog(@"could not delete password for account %@: %@ returned %i (%@)", [self _oldStyleAccountNameForAccount:account], [userInfo objectForKey:AIKEYCHAIN_ERROR_USERINFO_SECURITYFUNCTIONNAME], err, [userInfo objectForKey:AIKEYCHAIN_ERROR_USERINFO_ERRORDESCRIPTION]);
                                        }
                                }
                        }
                }
                
                [[NSUserDefaults standardUserDefaults] setBool:YES
                                                                                                forKey:KEY_PERFORMED_ACCOUNT_PASSWORD_UPGRADE];
        }
}

@end