1 #import "JubChatClient.h"
2 #import "ObjXMPP/namespaces.h"
4 #import "JubAvatarManager.h"
6 #define JUB_CLIENT_URI @"http://babelmonkeys.de/jubjub"
8 @implementation JubChatClient
9 @synthesize connection = _connection;
10 @synthesize roster = _roster;
11 @synthesize avatarManager = _avatarManager;
12 @synthesize contactManager = _contactManager;
13 @synthesize discoEntity = _discoEntity;
14 @synthesize presence = _presence;
17 - initWithConfig: (JubConfig*)config
22 _chatMap = [[OFMutableDictionary alloc] init];
24 _connection = [XMPPConnection new];
25 _connection.username = config.username;
26 _connection.domain = config.domain;
27 _connection.server = config.server;
28 _connection.password = config.password;
29 [_connection addDelegate: self];
31 _roster = [[XMPPRoster alloc] initWithConnection: _connection];
32 [_roster addDelegate: self];
35 [[XMPPDiscoEntity alloc] initWithConnection: _connection
36 capsNode: JUB_CLIENT_URI];
38 XMPPDiscoIdentity *identity =
39 [XMPPDiscoIdentity identityWithCategory: @"client"
42 [_discoEntity addIdentity: identity];
43 [_discoEntity addFeature: XMPP_NS_CAPS];
46 [[JubAvatarManager alloc] initWithClient: self];
48 _contactManager = [[XMPPContactManager alloc]
49 initWithConnection: _connection
51 [_contactManager addDelegate: self];
53 _streamManagement = [[XMPPStreamManagement alloc]
54 initWithConnection: _connection];
65 [_roster removeDelegate: self];
66 [_contactManager removeDelegate: self];
67 [_connection removeDelegate: self];
70 [_contactManager release];
71 [_discoEntity release];
72 [_streamManagement release];
73 [_avatarManager release];
74 [_connection release];
81 - (id<JubChatUI>)chatForContact: (XMPPContact*)contact
83 OFAutoreleasePool *pool = [OFAutoreleasePool new];
84 OFString *bareJID = [contact.rosterItem.JID bareJID];
86 id<JubChatUI> chat = [_chatMap objectForKey: bareJID];
89 [@"Chat with " stringByAppendingString: bareJID];
91 chat = [[[[_ui chatUIClass] alloc]
94 [_chatMap removeObjectForKey: bareJID];
96 sendBlock: ^(OFString *text) {
98 [XMPPMessage messageWithType: @"chat"];
100 [contact sendMessage: msg
101 connection: _connection];
105 [_chatMap setObject: chat
114 - (void)sendPresenceWithStatus: (OFString*)status
116 [self sendPresenceWithStatus: status
120 - (void)sendPresenceWithStatus: (OFString*)status
121 text: (OFString*)text
123 XMPPPresence *presence;
125 if ([status isEqual: @"unavailable"])
126 presence = [XMPPPresence presenceWithType: @"unavailable"];
128 presence = [XMPPPresence presence];
130 if (!([status isEqual: @"available"] ||
131 [status isEqual: @"unavailable"]))
132 presence.show = status;
135 presence.status = text;
137 OFXMLElement *caps = [OFXMLElement elementWithName: @"c"
138 namespace: XMPP_NS_CAPS];
139 [caps addAttributeWithName: @"hash"
140 stringValue: @"sha-1"];
141 [caps addAttributeWithName: @"ver"
142 stringValue: [_discoEntity capsHash]];
143 [caps addAttributeWithName: @"node"
144 stringValue: JUB_CLIENT_URI];
146 [presence addChild: caps];
148 [_connection sendStanza: presence];
151 - (void)connection: (XMPPConnection*)connection
152 wasBoundToJID: (XMPPJID*)jid
154 [_roster requestRoster];
157 - (void)connection: (XMPPConnection*)connection
158 didReceivePresence: (XMPPPresence*)presence
160 if ([presence.from isEqual: connection.JID]) {
162 didChangePresence: presence];
163 OF_SETTER(_presence, presence, true, 0);
167 - (void)contact: (XMPPContact*)contact
168 didSendMessage: (XMPPMessage*)message
170 if (message.body == nil || ![message.type isEqual: @"chat"])
173 id<JubChatUI> chat = [self chatForContact: contact];
174 [chat addMessage: message.body
175 sender: [message.from bareJID]];
178 - (void)rosterWasReceived: (XMPPRoster*)roster
180 [self sendPresenceWithStatus: @"available"
181 text: @"Hello from JubJub"];