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)connection: (XMPPConnection*)connection
92 wasBoundToJID: (XMPPJID*)jid
94 of_log(@"Bound to JID: %@", [jid fullJID]);
96 [_roster requestRoster];
99 - (void)connection: (XMPPConnection*)connection
100 didReceivePresence: (XMPPPresence*)presence
102 if ([presence.from isEqual: connection.JID]) {
104 didChangePresence: presence];
105 OF_SETTER(_presence, presence, YES, 0);
109 - (void)contact: (XMPPContact*)contact
110 didSendMessage: (XMPPMessage*)message
112 if (message.body == nil || ![message.type isEqual: @"chat"])
115 id<JubChatUI> chat = [self chatForContact: contact];
116 [chat addMessage: message.body
117 sender: [message.from bareJID]];
120 - (void)rosterWasReceived: (XMPPRoster*)roster
122 XMPPPresence *pres = [XMPPPresence presence];
123 [pres setStatus: @"Hello from JubJub"];
124 [_connection sendStanza: pres];