-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathXMPPAccount.m
131 lines (118 loc) · 4.06 KB
/
XMPPAccount.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
//
// XMPPAccount.m
// Jabber
//
// Created by David Chisnall on 21/09/2004.
// Copyright 2004 __MyCompanyName__. All rights reserved.
//
#ifndef GNUSTEP
#include <Security/Security.h>
#define kABJabberInstantProperty kABInstantMessageServiceJabber
#define kABMSNInstantProperty kABInstantMessageServiceMSN
#define kABAIMInstantProperty kABInstantMessageServiceAIM
#define kABICQInstantProperty kABInstantMessageServiceICQ
#define kABYahooInstantProperty kABInstantMessageServiceYahoo
#endif
#import <AddressBook/AddressBook.h>
#import "XMPPAccount.h"
#import "XMPPRoster.h"
#import "JID.h"
//NOTE: These could probably be done more neatly with KVC
void setDefault(NSString * dictionary, id key, id value)
{
NSMutableDictionary * dict = [NSMutableDictionary dictionaryWithDictionary:[[NSUserDefaults standardUserDefaults] dictionaryForKey:dictionary]];
if(dict == nil)
{
dict = [NSMutableDictionary dictionary];
}
[dict setValue:value forKey:key];
[[NSUserDefaults standardUserDefaults] setObject:dict forKey:dictionary];
}
id getDefault(NSString * dictionary, id key)
{
NSDictionary * dict = [[NSUserDefaults standardUserDefaults] dictionaryForKey:dictionary];
return [dict valueForKey:key];
}
@implementation XMPPAccount
+ (void) setDefaultJID:(JID*) aJID
{
[self setDefaultJID:aJID withServer:[aJID domain]];
}
+ (void) setDefaultJID:(JID*) aJID withServer:(NSString*) aServer
{
ABPerson * me = [[ABAddressBook sharedAddressBook] me];
if(me == nil)
{
me = [[ABPerson alloc] init];
[[ABAddressBook sharedAddressBook] addRecord:me];
[[ABAddressBook sharedAddressBook] setMe:me];
}
ABMutableMultiValue * jids = [[me valueForProperty:kABJabberInstantProperty] mutableCopy];
if(jids == nil)
{
jids = [[ABMutableMultiValue alloc] init];
}
NSString * defaultID = [jids primaryIdentifier];
if(defaultID == nil)
{
//TODO: This could arguably be more sensible.
defaultID = @"home";
}
[jids addValue:[aJID jidString] withLabel:defaultID];
[me setValue:jids forProperty:kABJabberInstantProperty];
[[ABAddressBook sharedAddressBook] save];
setDefault(@"Servers", [aJID jidString], aServer);
}
- (id) initWithName:(NSString*)aName withJid:(JID*)aJid withPassword:(NSString*)aPassword
{
self = [super init];
if(self == nil)
{
return nil;
}
name = aName;
roster = (XMPPRoster*)[[XMPPRoster alloc] initWithAccount:self];
connection = (XMPPConnection*)[[XMPPConnection alloc] initWithAccount:self];
[connection setPresenceDisplay:[roster delegate]];
//Get user's Jabber ID from Address Book
myJID = aJid;
if(aPassword != nil)
{
NSString * server = getDefault(@"Servers", [myJID jidString]);
[connection connectToJabberServer:server
withJID:myJID
password:aPassword];
return self;
}
else
{
[[NSException exceptionWithName:XMPPNOPASSWORDEXCEPTION
reason:@"Unable to find password for connection"
userInfo:[NSDictionary dictionaryWithObject:myJID
forKey:@"JID"]] raise];
}
// Never reached; just used to eliminate a warning
// from the compiler that doesn't know about exceptions
return nil;
}
- (void) reconnect
{
[connection reconnectToJabberServer];
}
- (XMPPRoster*) roster
{
return roster;
}
- (XMPPConnection*) connection
{
return connection;
}
- (JID*) jid
{
return myJID;
}
- (NSString*) name
{
return name;
}
@end