]> cgit.babelmonkeys.de Git - jubjub.git/blob - src/core/JubChatClient.m
Move away from BOOL
[jubjub.git] / src / core / JubChatClient.m
1 #import "JubChatClient.h"
2 #import "ObjXMPP/namespaces.h"
3
4 #import "JubAvatarManager.h"
5
6 #define JUB_CLIENT_URI @"http://babelmonkeys.de/jubjub"
7
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;
15 @synthesize ui = _ui;
16
17 - initWithConfig: (JubConfig*)config
18 {
19         self = [super init];
20
21         @try {
22                 _chatMap = [[OFMutableDictionary alloc] init];
23
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];
30
31                 _roster = [[XMPPRoster alloc] initWithConnection: _connection];
32                 [_roster addDelegate: self];
33
34                 _discoEntity =
35                     [[XMPPDiscoEntity alloc] initWithConnection: _connection
36                                                        capsNode: JUB_CLIENT_URI];
37
38                 XMPPDiscoIdentity *identity =
39                     [XMPPDiscoIdentity identityWithCategory: @"client"
40                                                        type: @"pc"
41                                                        name: @"JubJub"];
42                 [_discoEntity addIdentity: identity];
43                 [_discoEntity addFeature: XMPP_NS_CAPS];
44
45                 _avatarManager =
46                     [[JubAvatarManager alloc] initWithClient: self];
47
48                 _contactManager = [[XMPPContactManager alloc]
49                     initWithConnection: _connection
50                                 roster: _roster];
51                 [_contactManager addDelegate: self];
52
53                 _streamManagement = [[XMPPStreamManagement alloc]
54                     initWithConnection: _connection];
55         } @catch (id e) {
56                 [self release];
57                 @throw e;
58         }
59
60         return self;
61 }
62
63 - (void)dealloc
64 {
65         [_roster removeDelegate: self];
66         [_contactManager removeDelegate: self];
67         [_connection removeDelegate: self];
68
69         [_roster release];
70         [_contactManager release];
71         [_discoEntity release];
72         [_streamManagement release];
73         [_avatarManager release];
74         [_connection release];
75         [_presence release];
76         [_chatMap release];
77
78         [super dealloc];
79 }
80
81 - (id<JubChatUI>)chatForContact: (XMPPContact*)contact
82 {
83         OFAutoreleasePool *pool = [OFAutoreleasePool new];
84         OFString *bareJID = [contact.rosterItem.JID bareJID];
85
86         id<JubChatUI> chat = [_chatMap objectForKey: bareJID];
87         if (chat == nil) {
88                 OFString * title =
89                     [@"Chat with " stringByAppendingString: bareJID];
90
91                 chat = [[[[_ui chatUIClass] alloc]
92                     initWithTitle: title
93                        closeBlock: ^{
94                                 [_chatMap removeObjectForKey: bareJID];
95                         }
96                         sendBlock: ^(OFString *text) {
97                                 XMPPMessage *msg =
98                                     [XMPPMessage messageWithType: @"chat"];
99                                 msg.body = text;
100                                 [contact sendMessage: msg
101                                           connection: _connection];
102                         }
103                 ] autorelease];
104
105                 [_chatMap setObject: chat
106                              forKey: bareJID];
107         }
108
109         [pool release];
110
111         return chat;
112 }
113
114 - (void)sendPresenceWithStatus: (OFString*)status
115 {
116         [self sendPresenceWithStatus: status
117                                 text: nil];
118 }
119
120 - (void)sendPresenceWithStatus: (OFString*)status
121                           text: (OFString*)text
122 {
123         XMPPPresence *presence;
124
125         if ([status isEqual: @"unavailable"])
126                 presence = [XMPPPresence presenceWithType: @"unavailable"];
127         else
128                 presence = [XMPPPresence presence];
129
130         if (!([status isEqual: @"available"] ||
131               [status isEqual: @"unavailable"]))
132                 presence.show = status;
133
134         if (text != nil)
135                 presence.status = text;
136
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];
145
146         [presence addChild: caps];
147
148         [_connection sendStanza: presence];
149 }
150
151 - (void)connection: (XMPPConnection*)connection
152      wasBoundToJID: (XMPPJID*)jid
153 {
154         [_roster requestRoster];
155 }
156
157 -   (void)connection: (XMPPConnection*)connection
158   didReceivePresence: (XMPPPresence*)presence
159 {
160         if ([presence.from isEqual: connection.JID]) {
161                 [_ui         client: self
162                   didChangePresence: presence];
163                 OF_SETTER(_presence, presence, true, 0);
164         }
165 }
166
167 -  (void)contact: (XMPPContact*)contact
168   didSendMessage: (XMPPMessage*)message
169 {
170         if (message.body == nil || ![message.type isEqual: @"chat"])
171                 return;
172
173         id<JubChatUI> chat = [self chatForContact: contact];
174         [chat addMessage: message.body
175                   sender: [message.from bareJID]];
176 }
177
178 - (void)rosterWasReceived: (XMPPRoster*)roster
179 {
180         [self sendPresenceWithStatus: @"available"
181                                 text: @"Hello from JubJub"];
182 }
183 @end