1 #import "JubChatClient.h"
2 #import "ObjXMPP/namespaces.h"
4 #define JUB_CLIENT_URI @"http://babelmonkeys.de/jubjub"
6 @implementation JubChatClient
7 @synthesize connection = _connection;
8 @synthesize roster = _roster;
9 @synthesize contactManager = _contactManager;
10 @synthesize discoEntity = _discoEntity;
11 @synthesize presence = _presence;
14 - initWithConfig: (JubConfig*)config
19 _chatMap = [[OFMutableDictionary alloc] init];
21 _connection = [XMPPConnection new];
22 _connection.username = config.username;
23 _connection.domain = config.domain;
24 _connection.server = config.server;
25 _connection.password = config.password;
26 [_connection addDelegate: self];
28 _roster = [[XMPPRoster alloc] initWithConnection: _connection];
29 [_roster addDelegate: self];
31 _contactManager = [[XMPPContactManager alloc]
32 initWithConnection: _connection
34 [_contactManager addDelegate: self];
36 _streamManagement = [[XMPPStreamManagement alloc]
37 initWithConnection: _connection];
48 [_roster removeDelegate: self];
49 [_contactManager removeDelegate: self];
50 [_connection removeDelegate: self];
53 [_contactManager release];
54 [_discoEntity release];
55 [_streamManagement release];
56 [_connection release];
63 - (id<JubChatUI>)chatForContact: (XMPPContact*)contact
65 OFAutoreleasePool *pool = [OFAutoreleasePool new];
66 OFString *bareJID = [contact.rosterItem.JID bareJID];
68 id<JubChatUI> chat = [_chatMap objectForKey: bareJID];
71 [@"Chat with " stringByAppendingString: bareJID];
73 chat = [[[[_ui chatUIClass] alloc]
76 [_chatMap removeObjectForKey: bareJID];
78 sendBlock: ^(OFString *text) {
80 [XMPPMessage messageWithType: @"chat"];
82 [contact sendMessage: msg
83 connection: _connection];
87 [_chatMap setObject: chat
96 - (void)sendPresenceWithStatus: (OFString*)status
98 [self sendPresenceWithStatus: status
102 - (void)sendPresenceWithStatus: (OFString*)status
103 text: (OFString*)text
105 XMPPPresence *presence;
107 if ([status isEqual: @"unavailable"])
108 presence = [XMPPPresence presenceWithType: @"unavailable"];
110 presence = [XMPPPresence presence];
112 if (!([status isEqual: @"available"] ||
113 [status isEqual: @"unavailable"]))
114 presence.show = status;
117 presence.status = text;
119 OFXMLElement *caps = [OFXMLElement elementWithName: @"c"
120 namespace: XMPP_NS_CAPS];
121 [caps addAttributeWithName: @"hash"
122 stringValue: @"sha-1"];
123 [caps addAttributeWithName: @"ver"
124 stringValue: [_discoEntity capsHash]];
125 [caps addAttributeWithName: @"node"
126 stringValue: JUB_CLIENT_URI];
128 [presence addChild: caps];
130 [_connection sendStanza: presence];
133 - (void)connection: (XMPPConnection*)connection
134 wasBoundToJID: (XMPPJID*)jid
136 of_log(@"Bound to JID: %@", [jid fullJID]);
139 [[XMPPDiscoEntity alloc] initWithConnection: connection
140 capsNode: JUB_CLIENT_URI];
142 XMPPDiscoIdentity *identity =
143 [XMPPDiscoIdentity identityWithCategory: @"client"
146 [_discoEntity addIdentity: identity];
147 [_discoEntity addFeature: XMPP_NS_CAPS];
149 [_roster requestRoster];
152 - (void)connection: (XMPPConnection*)connection
153 didReceivePresence: (XMPPPresence*)presence
155 if ([presence.from isEqual: connection.JID]) {
157 didChangePresence: presence];
158 OF_SETTER(_presence, presence, YES, 0);
162 - (void)contact: (XMPPContact*)contact
163 didSendMessage: (XMPPMessage*)message
165 if (message.body == nil || ![message.type isEqual: @"chat"])
168 id<JubChatUI> chat = [self chatForContact: contact];
169 [chat addMessage: message.body
170 sender: [message.from bareJID]];
173 - (void)rosterWasReceived: (XMPPRoster*)roster
175 [self sendPresenceWithStatus: @"available"
176 text: @"Hello from JubJub"];