1 | /***************************************************************************\ |
---|
2 | * * |
---|
3 | * BitlBee - An IRC to IM gateway * |
---|
4 | * Jabber module - Conference rooms * |
---|
5 | * * |
---|
6 | * Copyright 2007 Wilmer van der Gaast <wilmer@gaast.net> * |
---|
7 | * * |
---|
8 | * This program is free software; you can redistribute it and/or modify * |
---|
9 | * it under the terms of the GNU General Public License as published by * |
---|
10 | * the Free Software Foundation; either version 2 of the License, or * |
---|
11 | * (at your option) any later version. * |
---|
12 | * * |
---|
13 | * This program is distributed in the hope that it will be useful, * |
---|
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
---|
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
---|
16 | * GNU General Public License for more details. * |
---|
17 | * * |
---|
18 | * You should have received a copy of the GNU General Public License along * |
---|
19 | * with this program; if not, write to the Free Software Foundation, Inc., * |
---|
20 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. * |
---|
21 | * * |
---|
22 | \***************************************************************************/ |
---|
23 | |
---|
24 | #include "jabber.h" |
---|
25 | |
---|
26 | static xt_status jabber_chat_join_failed( struct im_connection *ic, struct xt_node *node, struct xt_node *orig ); |
---|
27 | |
---|
28 | struct groupchat *jabber_chat_join( struct im_connection *ic, const char *room, const char *nick, const char *password ) |
---|
29 | { |
---|
30 | struct jabber_chat *jc; |
---|
31 | struct xt_node *node; |
---|
32 | struct groupchat *c; |
---|
33 | char *roomjid; |
---|
34 | |
---|
35 | roomjid = g_strdup_printf( "%s/%s", room, nick ); |
---|
36 | node = xt_new_node( "x", NULL, NULL ); |
---|
37 | xt_add_attr( node, "xmlns", XMLNS_MUC ); |
---|
38 | if( password ) |
---|
39 | xt_add_child( node, xt_new_node( "password", password, NULL ) ); |
---|
40 | node = jabber_make_packet( "presence", NULL, roomjid, node ); |
---|
41 | jabber_cache_add( ic, node, jabber_chat_join_failed ); |
---|
42 | |
---|
43 | if( !jabber_write_packet( ic, node ) ) |
---|
44 | { |
---|
45 | g_free( roomjid ); |
---|
46 | return NULL; |
---|
47 | } |
---|
48 | |
---|
49 | jc = g_new0( struct jabber_chat, 1 ); |
---|
50 | jc->name = jabber_normalize( room ); |
---|
51 | |
---|
52 | if( ( jc->me = jabber_buddy_add( ic, roomjid ) ) == NULL ) |
---|
53 | { |
---|
54 | g_free( roomjid ); |
---|
55 | g_free( jc->name ); |
---|
56 | g_free( jc ); |
---|
57 | return NULL; |
---|
58 | } |
---|
59 | |
---|
60 | /* roomjid isn't normalized yet, and we need an original version |
---|
61 | of the nick to send a proper presence update. */ |
---|
62 | jc->my_full_jid = roomjid; |
---|
63 | |
---|
64 | c = imcb_chat_new( ic, room ); |
---|
65 | c->data = jc; |
---|
66 | |
---|
67 | return c; |
---|
68 | } |
---|
69 | |
---|
70 | static xt_status jabber_chat_join_failed( struct im_connection *ic, struct xt_node *node, struct xt_node *orig ) |
---|
71 | { |
---|
72 | struct jabber_error *err; |
---|
73 | struct jabber_buddy *bud; |
---|
74 | char *room; |
---|
75 | |
---|
76 | room = xt_find_attr( orig, "to" ); |
---|
77 | bud = jabber_buddy_by_jid( ic, room, 0 ); |
---|
78 | err = jabber_error_parse( xt_find_node( node->children, "error" ), XMLNS_STANZA_ERROR ); |
---|
79 | if( err ) |
---|
80 | { |
---|
81 | imcb_error( ic, "Error joining groupchat %s: %s%s%s", room, err->code, |
---|
82 | err->text ? ": " : "", err->text ? err->text : "" ); |
---|
83 | jabber_error_free( err ); |
---|
84 | } |
---|
85 | if( bud ) |
---|
86 | jabber_chat_free( jabber_chat_by_jid( ic, bud->bare_jid ) ); |
---|
87 | |
---|
88 | return XT_HANDLED; |
---|
89 | } |
---|
90 | |
---|
91 | struct groupchat *jabber_chat_by_jid( struct im_connection *ic, const char *name ) |
---|
92 | { |
---|
93 | char *normalized = jabber_normalize( name ); |
---|
94 | struct groupchat *ret; |
---|
95 | struct jabber_chat *jc; |
---|
96 | |
---|
97 | for( ret = ic->groupchats; ret; ret = ret->next ) |
---|
98 | { |
---|
99 | jc = ret->data; |
---|
100 | if( strcmp( normalized, jc->name ) == 0 ) |
---|
101 | break; |
---|
102 | } |
---|
103 | g_free( normalized ); |
---|
104 | |
---|
105 | return ret; |
---|
106 | } |
---|
107 | |
---|
108 | void jabber_chat_free( struct groupchat *c ) |
---|
109 | { |
---|
110 | struct jabber_chat *jc = c->data; |
---|
111 | |
---|
112 | jabber_buddy_remove_bare( c->ic, jc->name ); |
---|
113 | |
---|
114 | g_free( jc->my_full_jid ); |
---|
115 | g_free( jc->name ); |
---|
116 | g_free( jc ); |
---|
117 | |
---|
118 | imcb_chat_free( c ); |
---|
119 | } |
---|
120 | |
---|
121 | int jabber_chat_msg( struct groupchat *c, char *message, int flags ) |
---|
122 | { |
---|
123 | struct im_connection *ic = c->ic; |
---|
124 | struct jabber_chat *jc = c->data; |
---|
125 | struct xt_node *node; |
---|
126 | |
---|
127 | jc->flags |= JCFLAG_MESSAGE_SENT; |
---|
128 | |
---|
129 | node = xt_new_node( "body", message, NULL ); |
---|
130 | node = jabber_make_packet( "message", "groupchat", jc->name, node ); |
---|
131 | |
---|
132 | if( !jabber_write_packet( ic, node ) ) |
---|
133 | { |
---|
134 | xt_free_node( node ); |
---|
135 | return 0; |
---|
136 | } |
---|
137 | xt_free_node( node ); |
---|
138 | |
---|
139 | return 1; |
---|
140 | } |
---|
141 | |
---|
142 | int jabber_chat_topic( struct groupchat *c, char *topic ) |
---|
143 | { |
---|
144 | struct im_connection *ic = c->ic; |
---|
145 | struct jabber_chat *jc = c->data; |
---|
146 | struct xt_node *node; |
---|
147 | |
---|
148 | node = xt_new_node( "subject", topic, NULL ); |
---|
149 | node = jabber_make_packet( "message", "groupchat", jc->name, node ); |
---|
150 | |
---|
151 | if( !jabber_write_packet( ic, node ) ) |
---|
152 | { |
---|
153 | xt_free_node( node ); |
---|
154 | return 0; |
---|
155 | } |
---|
156 | xt_free_node( node ); |
---|
157 | |
---|
158 | return 1; |
---|
159 | } |
---|
160 | |
---|
161 | int jabber_chat_leave( struct groupchat *c, const char *reason ) |
---|
162 | { |
---|
163 | struct im_connection *ic = c->ic; |
---|
164 | struct jabber_chat *jc = c->data; |
---|
165 | struct xt_node *node; |
---|
166 | |
---|
167 | node = xt_new_node( "x", NULL, NULL ); |
---|
168 | xt_add_attr( node, "xmlns", XMLNS_MUC ); |
---|
169 | node = jabber_make_packet( "presence", "unavailable", jc->my_full_jid, node ); |
---|
170 | |
---|
171 | if( !jabber_write_packet( ic, node ) ) |
---|
172 | { |
---|
173 | xt_free_node( node ); |
---|
174 | return 0; |
---|
175 | } |
---|
176 | xt_free_node( node ); |
---|
177 | |
---|
178 | return 1; |
---|
179 | } |
---|
180 | |
---|
181 | void jabber_chat_invite( struct groupchat *c, char *who, char *message ) |
---|
182 | { |
---|
183 | struct xt_node *node; |
---|
184 | struct im_connection *ic = c->ic; |
---|
185 | struct jabber_chat *jc = c->data; |
---|
186 | |
---|
187 | node = xt_new_node( "reason", message, NULL ); |
---|
188 | |
---|
189 | node = xt_new_node( "invite", NULL, node ); |
---|
190 | xt_add_attr( node, "to", who ); |
---|
191 | |
---|
192 | node = xt_new_node( "x", NULL, node ); |
---|
193 | xt_add_attr( node, "xmlns", XMLNS_MUC_USER ); |
---|
194 | |
---|
195 | node = jabber_make_packet( "message", NULL, jc->name, node ); |
---|
196 | |
---|
197 | jabber_write_packet( ic, node ); |
---|
198 | |
---|
199 | xt_free_node( node ); |
---|
200 | } |
---|
201 | |
---|
202 | /* Not really the same syntax as the normal pkt_ functions, but this isn't |
---|
203 | called by the xmltree parser directly and this way I can add some extra |
---|
204 | parameters so we won't have to repeat too many things done by the caller |
---|
205 | already. */ |
---|
206 | void jabber_chat_pkt_presence( struct im_connection *ic, struct jabber_buddy *bud, struct xt_node *node ) |
---|
207 | { |
---|
208 | struct groupchat *chat; |
---|
209 | struct xt_node *c; |
---|
210 | char *type = xt_find_attr( node, "type" ); |
---|
211 | struct jabber_chat *jc; |
---|
212 | char *s; |
---|
213 | |
---|
214 | if( ( chat = jabber_chat_by_jid( ic, bud->bare_jid ) ) == NULL ) |
---|
215 | { |
---|
216 | /* How could this happen?? We could do kill( self, 11 ) |
---|
217 | now or just wait for the OS to do it. :-) */ |
---|
218 | return; |
---|
219 | } |
---|
220 | |
---|
221 | jc = chat->data; |
---|
222 | |
---|
223 | if( type == NULL && !( bud->flags & JBFLAG_IS_CHATROOM ) ) |
---|
224 | { |
---|
225 | bud->flags |= JBFLAG_IS_CHATROOM; |
---|
226 | /* If this one wasn't set yet, this buddy just joined the chat. |
---|
227 | Slightly hackish way of finding out eh? ;-) */ |
---|
228 | |
---|
229 | /* This is pretty messy... Here it sets ext_jid to the real |
---|
230 | JID of the participant. Works for non-anonymized channels. |
---|
231 | Might break if someone joins a chat twice, though. */ |
---|
232 | for( c = node->children; ( c = xt_find_node( c, "x" ) ); c = c->next ) |
---|
233 | if( ( s = xt_find_attr( c, "xmlns" ) ) && |
---|
234 | ( strcmp( s, XMLNS_MUC_USER ) == 0 ) ) |
---|
235 | { |
---|
236 | c = xt_find_node( c->children, "item" ); |
---|
237 | if( ( s = xt_find_attr( c, "jid" ) ) ) |
---|
238 | { |
---|
239 | /* Yay, found what we need. :-) */ |
---|
240 | bud->ext_jid = jabber_normalize( s ); |
---|
241 | break; |
---|
242 | } |
---|
243 | } |
---|
244 | |
---|
245 | /* Make up some other handle, if necessary. */ |
---|
246 | if( bud->ext_jid == NULL ) |
---|
247 | { |
---|
248 | if( bud == jc->me ) |
---|
249 | { |
---|
250 | bud->ext_jid = jabber_normalize( ic->acc->user ); |
---|
251 | } |
---|
252 | else |
---|
253 | { |
---|
254 | int i; |
---|
255 | |
---|
256 | /* Don't want the nick to be at the end, so let's |
---|
257 | think of some slightly different notation to use |
---|
258 | for anonymous groupchat participants in BitlBee. */ |
---|
259 | bud->ext_jid = g_strdup_printf( "%s=%s", bud->resource, bud->bare_jid ); |
---|
260 | |
---|
261 | /* And strip any unwanted characters. */ |
---|
262 | for( i = 0; bud->resource[i]; i ++ ) |
---|
263 | if( bud->ext_jid[i] == '=' || bud->ext_jid[i] == '@' ) |
---|
264 | bud->ext_jid[i] = '_'; |
---|
265 | |
---|
266 | /* Some program-specific restrictions. */ |
---|
267 | imcb_clean_handle( ic, bud->ext_jid ); |
---|
268 | } |
---|
269 | bud->flags |= JBFLAG_IS_ANONYMOUS; |
---|
270 | } |
---|
271 | |
---|
272 | if( bud != jc->me ) |
---|
273 | { |
---|
274 | imcb_add_buddy( ic, bud->ext_jid, NULL ); |
---|
275 | imcb_buddy_nick_hint( ic, bud->ext_jid, bud->resource ); |
---|
276 | } |
---|
277 | |
---|
278 | s = strchr( bud->ext_jid, '/' ); |
---|
279 | if( s ) *s = 0; /* Should NEVER be NULL, but who knows... */ |
---|
280 | imcb_chat_add_buddy( chat, bud->ext_jid ); |
---|
281 | if( s ) *s = '/'; |
---|
282 | } |
---|
283 | else if( type ) /* type can only be NULL or "unavailable" in this function */ |
---|
284 | { |
---|
285 | s = strchr( bud->ext_jid, '/' ); |
---|
286 | if( s ) *s = 0; |
---|
287 | imcb_chat_remove_buddy( chat, bud->ext_jid, NULL ); |
---|
288 | if( bud != jc->me && bud->flags & JBFLAG_IS_ANONYMOUS ) |
---|
289 | imcb_remove_buddy( ic, bud->ext_jid, NULL ); |
---|
290 | if( s ) *s = '/'; |
---|
291 | |
---|
292 | if( bud == jc->me ) |
---|
293 | jabber_chat_free( chat ); |
---|
294 | } |
---|
295 | } |
---|
296 | |
---|
297 | void jabber_chat_pkt_message( struct im_connection *ic, struct jabber_buddy *bud, struct xt_node *node ) |
---|
298 | { |
---|
299 | struct xt_node *subject = xt_find_node( node->children, "subject" ); |
---|
300 | struct xt_node *body = xt_find_node( node->children, "body" ); |
---|
301 | struct groupchat *chat = bud ? jabber_chat_by_jid( ic, bud->bare_jid ) : NULL; |
---|
302 | struct jabber_chat *jc = chat ? chat->data : NULL; |
---|
303 | char *s; |
---|
304 | |
---|
305 | if( bud == NULL || ( jc && ~jc->flags & JCFLAG_MESSAGE_SENT && bud == jc->me ) ) |
---|
306 | { |
---|
307 | char *nick; |
---|
308 | |
---|
309 | if( body == NULL || body->text_len == 0 ) |
---|
310 | /* Meh. Empty messages aren't very interesting, no matter |
---|
311 | how much some servers love to send them. */ |
---|
312 | return; |
---|
313 | |
---|
314 | s = xt_find_attr( node, "from" ); /* pkt_message() already NULL-checked this one. */ |
---|
315 | nick = strchr( s, '/' ); |
---|
316 | if( nick ) |
---|
317 | { |
---|
318 | /* If this message included a resource/nick we don't know, |
---|
319 | we might still know the groupchat itself. */ |
---|
320 | *nick = 0; |
---|
321 | chat = jabber_chat_by_jid( ic, s ); |
---|
322 | *nick = '/'; |
---|
323 | |
---|
324 | nick ++; |
---|
325 | } |
---|
326 | else |
---|
327 | { |
---|
328 | /* message.c uses the EXACT_JID option, so bud should |
---|
329 | always be NULL here for bare JIDs. */ |
---|
330 | chat = jabber_chat_by_jid( ic, s ); |
---|
331 | } |
---|
332 | |
---|
333 | if( nick == NULL ) |
---|
334 | { |
---|
335 | /* This is fine, the groupchat itself isn't in jd->buddies. */ |
---|
336 | if( chat ) |
---|
337 | imcb_chat_log( chat, "From conference server: %s", body->text ); |
---|
338 | else |
---|
339 | imcb_log( ic, "System message from unknown groupchat %s: %s", s, body->text ); |
---|
340 | } |
---|
341 | else |
---|
342 | { |
---|
343 | /* This can happen too, at least when receiving a backlog when |
---|
344 | just joining a channel. */ |
---|
345 | if( chat ) |
---|
346 | imcb_chat_log( chat, "Message from unknown participant %s: %s", nick, body->text ); |
---|
347 | else |
---|
348 | imcb_log( ic, "Groupchat message from unknown JID %s: %s", s, body->text ); |
---|
349 | } |
---|
350 | |
---|
351 | return; |
---|
352 | } |
---|
353 | else if( chat == NULL ) |
---|
354 | { |
---|
355 | /* How could this happen?? We could do kill( self, 11 ) |
---|
356 | now or just wait for the OS to do it. :-) */ |
---|
357 | return; |
---|
358 | } |
---|
359 | |
---|
360 | if( subject ) |
---|
361 | { |
---|
362 | s = strchr( bud->ext_jid, '/' ); |
---|
363 | if( s ) *s = 0; |
---|
364 | imcb_chat_topic( chat, bud->ext_jid, subject->text_len > 0 ? |
---|
365 | subject->text : NULL, jabber_get_timestamp( node ) ); |
---|
366 | if( s ) *s = '/'; |
---|
367 | } |
---|
368 | if( body && body->text_len > 0 ) |
---|
369 | { |
---|
370 | s = strchr( bud->ext_jid, '/' ); |
---|
371 | if( s ) *s = 0; |
---|
372 | imcb_chat_msg( chat, bud->ext_jid, body->text, 0, jabber_get_timestamp( node ) ); |
---|
373 | if( s ) *s = '/'; |
---|
374 | } |
---|
375 | } |
---|