1 #import "JubChatClient.h"
3 @implementation JubChatClient
4 @synthesize connection = _connection;
5 @synthesize roster = _roster;
6 @synthesize contactManager = _contactManager;
7 @synthesize presence = _presence;
10 - initWithConfig: (JubConfig*)config
15 _chatMap = [[OFMutableDictionary alloc] init];
17 _connection = [XMPPConnection new];
18 _connection.username = config.username;
19 _connection.domain = config.domain;
20 _connection.server = config.server;
21 _connection.password = config.password;
22 [_connection addDelegate: self];
24 _roster = [[XMPPRoster alloc] initWithConnection: _connection];
25 [_roster addDelegate: self];
27 _contactManager = [[XMPPContactManager alloc]
28 initWithConnection: _connection
30 [_contactManager addDelegate: self];
32 _streamManagement = [[XMPPStreamManagement alloc]
33 initWithConnection: _connection];
44 [_roster removeDelegate: self];
45 [_contactManager removeDelegate: self];
46 [_connection removeDelegate: self];
49 [_contactManager release];
50 [_streamManagement release];
51 [_connection release];
58 - (id<JubChatUI>)chatForContact: (XMPPContact*)contact
60 OFAutoreleasePool *pool = [OFAutoreleasePool new];
61 OFString *bareJID = [contact.rosterItem.JID bareJID];
63 id<JubChatUI> chat = [_chatMap objectForKey: bareJID];
66 [@"Chat with " stringByAppendingString: bareJID];
68 chat = [[[[_ui chatUIClass] alloc]
71 [_chatMap removeObjectForKey: bareJID];
73 sendBlock: ^(OFString *text) {
75 [XMPPMessage messageWithType: @"chat"];
77 [contact sendMessage: msg
78 connection: _connection];
82 [_chatMap setObject: chat
91 - (void)sendPresenceWithStatus: (OFString*)status
93 [self sendPresenceWithStatus: status
97 - (void)sendPresenceWithStatus: (OFString*)status
100 XMPPPresence *presence;
102 if ([status isEqual: @"unavailable"])
103 presence = [XMPPPresence presenceWithType: @"unavailable"];
105 presence = [XMPPPresence presence];
107 if (!([status isEqual: @"available"] ||
108 [status isEqual: @"unavailable"]))
109 presence.show = status;
112 presence.status = text;
114 [_connection sendStanza: presence];
117 - (void)connection: (XMPPConnection*)connection
118 wasBoundToJID: (XMPPJID*)jid
120 of_log(@"Bound to JID: %@", [jid fullJID]);
122 [_roster requestRoster];
125 - (void)connection: (XMPPConnection*)connection
126 didReceivePresence: (XMPPPresence*)presence
128 if ([presence.from isEqual: connection.JID]) {
130 didChangePresence: presence];
131 OF_SETTER(_presence, presence, YES, 0);
135 - (void)contact: (XMPPContact*)contact
136 didSendMessage: (XMPPMessage*)message
138 if (message.body == nil || ![message.type isEqual: @"chat"])
141 id<JubChatUI> chat = [self chatForContact: contact];
142 [chat addMessage: message.body
143 sender: [message.from bareJID]];
146 - (void)rosterWasReceived: (XMPPRoster*)roster
148 XMPPPresence *pres = [XMPPPresence presence];
149 [pres setStatus: @"Hello from JubJub"];
150 [_connection sendStanza: pres];