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];
35 [_connection asyncConnectAndHandle];
46 [_roster removeDelegate: self];
47 [_contactManager removeDelegate: self];
48 [_connection removeDelegate: self];
51 [_contactManager release];
52 [_streamManagement release];
53 [_connection release];
60 - (id<JubChatUI>)chatForContact: (XMPPContact*)contact
62 OFAutoreleasePool *pool = [OFAutoreleasePool new];
63 OFString *bareJID = [contact.rosterItem.JID bareJID];
65 id<JubChatUI> chat = [_chatMap objectForKey: bareJID];
68 [@"Chat with " stringByAppendingString: bareJID];
70 chat = [[[[_ui chatUIClass] alloc]
73 [_chatMap removeObjectForKey: bareJID];
75 sendBlock: ^(OFString *text) {
77 [XMPPMessage messageWithType: @"chat"];
79 [contact sendMessage: msg
80 connection: _connection];
84 [_chatMap setObject: chat
93 - (void)connection: (XMPPConnection*)connection
94 wasBoundToJID: (XMPPJID*)jid
96 of_log(@"Bound to JID: %@", [jid fullJID]);
98 [_roster requestRoster];
101 - (void)connection: (XMPPConnection*)connection
102 didReceivePresence: (XMPPPresence*)presence
104 if ([presence.from isEqual: connection.JID]) {
106 didChangePresence: presence];
107 OF_SETTER(_presence, presence, YES, 0);
111 - (void)contact: (XMPPContact*)contact
112 didSendMessage: (XMPPMessage*)message
114 if (message.body == nil || ![message.type isEqual: @"chat"])
117 id<JubChatUI> chat = [self chatForContact: contact];
118 [chat addMessage: message.body
119 sender: [message.from bareJID]];
122 - (void)rosterWasReceived: (XMPPRoster*)roster
124 XMPPPresence *pres = [XMPPPresence presence];
125 [pres setStatus: @"Hello from JubJub"];
126 [_connection sendStanza: pres];