[e35d1a1] | 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 | |
---|
[5bd21df] | 26 | static xt_status jabber_chat_join_failed( struct im_connection *ic, struct xt_node *node, struct xt_node *orig ); |
---|
| 27 | |
---|
[94acdd0] | 28 | struct groupchat *jabber_chat_join( struct im_connection *ic, const char *room, const char *nick, const char *password ) |
---|
[e35d1a1] | 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 ); |
---|
[3038e47] | 38 | if( password ) |
---|
| 39 | xt_add_child( node, xt_new_node( "password", password, NULL ) ); |
---|
[b2c062d] | 40 | node = jabber_make_packet( "presence", NULL, roomjid, node ); |
---|
[5bd21df] | 41 | jabber_cache_add( ic, node, jabber_chat_join_failed ); |
---|
[e35d1a1] | 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 | } |
---|
[9c9b37c] | 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; |
---|
[e35d1a1] | 63 | |
---|
| 64 | c = imcb_chat_new( ic, room ); |
---|
| 65 | c->data = jc; |
---|
| 66 | |
---|
| 67 | return c; |
---|
| 68 | } |
---|
| 69 | |
---|
[5bd21df] | 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" ); |
---|
[13857c6] | 77 | bud = jabber_buddy_by_jid( ic, room, 0 ); |
---|
[5bd21df] | 78 | err = jabber_error_parse( xt_find_node( node->children, "error" ), XMLNS_STANZA_ERROR ); |
---|
| 79 | if( err ) |
---|
| 80 | { |
---|
[13857c6] | 81 | imcb_error( ic, "Error joining groupchat %s: %s%s%s", room, err->code, |
---|
| 82 | err->text ? ": " : "", err->text ? err->text : "" ); |
---|
[5bd21df] | 83 | jabber_error_free( err ); |
---|
| 84 | } |
---|
[13857c6] | 85 | if( bud ) |
---|
| 86 | jabber_chat_free( jabber_chat_by_jid( ic, bud->bare_jid ) ); |
---|
[5bd21df] | 87 | |
---|
[80e9db9] | 88 | return XT_HANDLED; |
---|
[5bd21df] | 89 | } |
---|
| 90 | |
---|
| 91 | struct groupchat *jabber_chat_by_jid( struct im_connection *ic, const char *name ) |
---|
| 92 | { |
---|
| 93 | char *normalized = jabber_normalize( name ); |
---|
[aea8b68] | 94 | GSList *l; |
---|
[5bd21df] | 95 | struct groupchat *ret; |
---|
| 96 | struct jabber_chat *jc; |
---|
| 97 | |
---|
[aea8b68] | 98 | for( l = ic->groupchats; l; l = l->next ) |
---|
[5bd21df] | 99 | { |
---|
[aea8b68] | 100 | ret = l->data; |
---|
[5bd21df] | 101 | jc = ret->data; |
---|
| 102 | if( strcmp( normalized, jc->name ) == 0 ) |
---|
| 103 | break; |
---|
| 104 | } |
---|
| 105 | g_free( normalized ); |
---|
| 106 | |
---|
[58f5ef7] | 107 | return l ? ret : NULL; |
---|
[5bd21df] | 108 | } |
---|
| 109 | |
---|
[9da0bbf] | 110 | void jabber_chat_free( struct groupchat *c ) |
---|
| 111 | { |
---|
| 112 | struct jabber_chat *jc = c->data; |
---|
| 113 | |
---|
| 114 | jabber_buddy_remove_bare( c->ic, jc->name ); |
---|
| 115 | |
---|
| 116 | g_free( jc->my_full_jid ); |
---|
| 117 | g_free( jc->name ); |
---|
| 118 | g_free( jc ); |
---|
| 119 | |
---|
| 120 | imcb_chat_free( c ); |
---|
| 121 | } |
---|
| 122 | |
---|
[43671b9] | 123 | int jabber_chat_msg( struct groupchat *c, char *message, int flags ) |
---|
| 124 | { |
---|
| 125 | struct im_connection *ic = c->ic; |
---|
| 126 | struct jabber_chat *jc = c->data; |
---|
| 127 | struct xt_node *node; |
---|
| 128 | |
---|
[63075d7] | 129 | jc->flags |= JCFLAG_MESSAGE_SENT; |
---|
| 130 | |
---|
[43671b9] | 131 | node = xt_new_node( "body", message, NULL ); |
---|
| 132 | node = jabber_make_packet( "message", "groupchat", jc->name, node ); |
---|
| 133 | |
---|
| 134 | if( !jabber_write_packet( ic, node ) ) |
---|
| 135 | { |
---|
| 136 | xt_free_node( node ); |
---|
| 137 | return 0; |
---|
| 138 | } |
---|
| 139 | xt_free_node( node ); |
---|
| 140 | |
---|
| 141 | return 1; |
---|
| 142 | } |
---|
| 143 | |
---|
[ef5c185] | 144 | int jabber_chat_topic( struct groupchat *c, char *topic ) |
---|
| 145 | { |
---|
| 146 | struct im_connection *ic = c->ic; |
---|
| 147 | struct jabber_chat *jc = c->data; |
---|
| 148 | struct xt_node *node; |
---|
| 149 | |
---|
| 150 | node = xt_new_node( "subject", topic, NULL ); |
---|
| 151 | node = jabber_make_packet( "message", "groupchat", jc->name, node ); |
---|
| 152 | |
---|
| 153 | if( !jabber_write_packet( ic, node ) ) |
---|
| 154 | { |
---|
| 155 | xt_free_node( node ); |
---|
| 156 | return 0; |
---|
| 157 | } |
---|
| 158 | xt_free_node( node ); |
---|
| 159 | |
---|
| 160 | return 1; |
---|
| 161 | } |
---|
| 162 | |
---|
[e35d1a1] | 163 | int jabber_chat_leave( struct groupchat *c, const char *reason ) |
---|
| 164 | { |
---|
| 165 | struct im_connection *ic = c->ic; |
---|
| 166 | struct jabber_chat *jc = c->data; |
---|
| 167 | struct xt_node *node; |
---|
| 168 | |
---|
| 169 | node = xt_new_node( "x", NULL, NULL ); |
---|
| 170 | xt_add_attr( node, "xmlns", XMLNS_MUC ); |
---|
[9c9b37c] | 171 | node = jabber_make_packet( "presence", "unavailable", jc->my_full_jid, node ); |
---|
[e35d1a1] | 172 | |
---|
| 173 | if( !jabber_write_packet( ic, node ) ) |
---|
| 174 | { |
---|
| 175 | xt_free_node( node ); |
---|
| 176 | return 0; |
---|
| 177 | } |
---|
| 178 | xt_free_node( node ); |
---|
| 179 | |
---|
| 180 | return 1; |
---|
| 181 | } |
---|
| 182 | |
---|
[c058ff9] | 183 | void jabber_chat_invite( struct groupchat *c, char *who, char *message ) |
---|
| 184 | { |
---|
| 185 | struct xt_node *node; |
---|
| 186 | struct im_connection *ic = c->ic; |
---|
| 187 | struct jabber_chat *jc = c->data; |
---|
| 188 | |
---|
| 189 | node = xt_new_node( "reason", message, NULL ); |
---|
| 190 | |
---|
| 191 | node = xt_new_node( "invite", NULL, node ); |
---|
| 192 | xt_add_attr( node, "to", who ); |
---|
| 193 | |
---|
| 194 | node = xt_new_node( "x", NULL, node ); |
---|
| 195 | xt_add_attr( node, "xmlns", XMLNS_MUC_USER ); |
---|
| 196 | |
---|
| 197 | node = jabber_make_packet( "message", NULL, jc->name, node ); |
---|
| 198 | |
---|
| 199 | jabber_write_packet( ic, node ); |
---|
| 200 | |
---|
| 201 | xt_free_node( node ); |
---|
| 202 | } |
---|
| 203 | |
---|
[e35d1a1] | 204 | /* Not really the same syntax as the normal pkt_ functions, but this isn't |
---|
[0e7ab64] | 205 | called by the xmltree parser directly and this way I can add some extra |
---|
[e35d1a1] | 206 | parameters so we won't have to repeat too many things done by the caller |
---|
| 207 | already. */ |
---|
| 208 | void jabber_chat_pkt_presence( struct im_connection *ic, struct jabber_buddy *bud, struct xt_node *node ) |
---|
| 209 | { |
---|
| 210 | struct groupchat *chat; |
---|
| 211 | struct xt_node *c; |
---|
| 212 | char *type = xt_find_attr( node, "type" ); |
---|
[68286eb] | 213 | struct jabber_data *jd = ic->proto_data; |
---|
[e35d1a1] | 214 | struct jabber_chat *jc; |
---|
| 215 | char *s; |
---|
| 216 | |
---|
[5bd21df] | 217 | if( ( chat = jabber_chat_by_jid( ic, bud->bare_jid ) ) == NULL ) |
---|
[e35d1a1] | 218 | { |
---|
| 219 | /* How could this happen?? We could do kill( self, 11 ) |
---|
| 220 | now or just wait for the OS to do it. :-) */ |
---|
| 221 | return; |
---|
| 222 | } |
---|
| 223 | |
---|
| 224 | jc = chat->data; |
---|
| 225 | |
---|
| 226 | if( type == NULL && !( bud->flags & JBFLAG_IS_CHATROOM ) ) |
---|
| 227 | { |
---|
| 228 | bud->flags |= JBFLAG_IS_CHATROOM; |
---|
| 229 | /* If this one wasn't set yet, this buddy just joined the chat. |
---|
| 230 | Slightly hackish way of finding out eh? ;-) */ |
---|
| 231 | |
---|
[b9f8b87] | 232 | /* This is pretty messy... Here it sets ext_jid to the real |
---|
| 233 | JID of the participant. Works for non-anonymized channels. |
---|
| 234 | Might break if someone joins a chat twice, though. */ |
---|
[e35d1a1] | 235 | for( c = node->children; ( c = xt_find_node( c, "x" ) ); c = c->next ) |
---|
| 236 | if( ( s = xt_find_attr( c, "xmlns" ) ) && |
---|
| 237 | ( strcmp( s, XMLNS_MUC_USER ) == 0 ) ) |
---|
| 238 | { |
---|
[19176513] | 239 | struct xt_node *item; |
---|
| 240 | |
---|
| 241 | item = xt_find_node( c->children, "item" ); |
---|
| 242 | if( ( s = xt_find_attr( item, "jid" ) ) ) |
---|
[e35d1a1] | 243 | { |
---|
| 244 | /* Yay, found what we need. :-) */ |
---|
[b9f8b87] | 245 | bud->ext_jid = jabber_normalize( s ); |
---|
[e35d1a1] | 246 | break; |
---|
| 247 | } |
---|
| 248 | } |
---|
| 249 | |
---|
[6286f80] | 250 | /* Make up some other handle, if necessary. */ |
---|
| 251 | if( bud->ext_jid == NULL ) |
---|
| 252 | { |
---|
[54f2f55] | 253 | if( bud == jc->me ) |
---|
[5d7dc00] | 254 | { |
---|
[68286eb] | 255 | bud->ext_jid = g_strdup( jd->me ); |
---|
[5d7dc00] | 256 | } |
---|
[54f2f55] | 257 | else |
---|
[5d7dc00] | 258 | { |
---|
| 259 | int i; |
---|
| 260 | |
---|
[54f2f55] | 261 | /* Don't want the nick to be at the end, so let's |
---|
| 262 | think of some slightly different notation to use |
---|
| 263 | for anonymous groupchat participants in BitlBee. */ |
---|
| 264 | bud->ext_jid = g_strdup_printf( "%s=%s", bud->resource, bud->bare_jid ); |
---|
[5d7dc00] | 265 | |
---|
| 266 | /* And strip any unwanted characters. */ |
---|
| 267 | for( i = 0; bud->resource[i]; i ++ ) |
---|
| 268 | if( bud->ext_jid[i] == '=' || bud->ext_jid[i] == '@' ) |
---|
| 269 | bud->ext_jid[i] = '_'; |
---|
[85023c6] | 270 | |
---|
| 271 | /* Some program-specific restrictions. */ |
---|
| 272 | imcb_clean_handle( ic, bud->ext_jid ); |
---|
[5d7dc00] | 273 | } |
---|
[6286f80] | 274 | bud->flags |= JBFLAG_IS_ANONYMOUS; |
---|
| 275 | } |
---|
[e35d1a1] | 276 | |
---|
[e73a501] | 277 | if( bud != jc->me && bud->flags & JBFLAG_IS_ANONYMOUS ) |
---|
[c570c86] | 278 | { |
---|
[e73a501] | 279 | /* If JIDs are anonymized, add them to the local |
---|
| 280 | list for the duration of this chat. */ |
---|
[c570c86] | 281 | imcb_add_buddy( ic, bud->ext_jid, NULL ); |
---|
| 282 | imcb_buddy_nick_hint( ic, bud->ext_jid, bud->resource ); |
---|
| 283 | } |
---|
| 284 | |
---|
[6286f80] | 285 | s = strchr( bud->ext_jid, '/' ); |
---|
[e35d1a1] | 286 | if( s ) *s = 0; /* Should NEVER be NULL, but who knows... */ |
---|
[6286f80] | 287 | imcb_chat_add_buddy( chat, bud->ext_jid ); |
---|
[e35d1a1] | 288 | if( s ) *s = '/'; |
---|
| 289 | } |
---|
[998b103] | 290 | else if( type ) /* type can only be NULL or "unavailable" in this function */ |
---|
[e35d1a1] | 291 | { |
---|
[bdad407] | 292 | if( ( bud->flags & JBFLAG_IS_CHATROOM ) && bud->ext_jid ) |
---|
| 293 | { |
---|
| 294 | s = strchr( bud->ext_jid, '/' ); |
---|
| 295 | if( s ) *s = 0; |
---|
| 296 | imcb_chat_remove_buddy( chat, bud->ext_jid, NULL ); |
---|
| 297 | if( bud != jc->me && bud->flags & JBFLAG_IS_ANONYMOUS ) |
---|
| 298 | imcb_remove_buddy( ic, bud->ext_jid, NULL ); |
---|
| 299 | if( s ) *s = '/'; |
---|
| 300 | } |
---|
[e35d1a1] | 301 | |
---|
| 302 | if( bud == jc->me ) |
---|
[9da0bbf] | 303 | jabber_chat_free( chat ); |
---|
[e35d1a1] | 304 | } |
---|
| 305 | } |
---|
| 306 | |
---|
| 307 | void jabber_chat_pkt_message( struct im_connection *ic, struct jabber_buddy *bud, struct xt_node *node ) |
---|
| 308 | { |
---|
[ef5c185] | 309 | struct xt_node *subject = xt_find_node( node->children, "subject" ); |
---|
[e35d1a1] | 310 | struct xt_node *body = xt_find_node( node->children, "body" ); |
---|
[63075d7] | 311 | struct groupchat *chat = bud ? jabber_chat_by_jid( ic, bud->bare_jid ) : NULL; |
---|
| 312 | struct jabber_chat *jc = chat ? chat->data : NULL; |
---|
[e35d1a1] | 313 | char *s; |
---|
| 314 | |
---|
[41e0c00] | 315 | if( subject && chat ) |
---|
| 316 | { |
---|
| 317 | s = bud ? strchr( bud->ext_jid, '/' ) : NULL; |
---|
| 318 | if( s ) *s = 0; |
---|
| 319 | imcb_chat_topic( chat, bud ? bud->ext_jid : NULL, subject->text_len > 0 ? |
---|
| 320 | subject->text : NULL, jabber_get_timestamp( node ) ); |
---|
| 321 | if( s ) *s = '/'; |
---|
| 322 | } |
---|
| 323 | |
---|
[63075d7] | 324 | if( bud == NULL || ( jc && ~jc->flags & JCFLAG_MESSAGE_SENT && bud == jc->me ) ) |
---|
[e35d1a1] | 325 | { |
---|
[a882d6c] | 326 | char *nick; |
---|
| 327 | |
---|
| 328 | if( body == NULL || body->text_len == 0 ) |
---|
| 329 | /* Meh. Empty messages aren't very interesting, no matter |
---|
| 330 | how much some servers love to send them. */ |
---|
| 331 | return; |
---|
| 332 | |
---|
[e35d1a1] | 333 | s = xt_find_attr( node, "from" ); /* pkt_message() already NULL-checked this one. */ |
---|
[a882d6c] | 334 | nick = strchr( s, '/' ); |
---|
| 335 | if( nick ) |
---|
| 336 | { |
---|
| 337 | /* If this message included a resource/nick we don't know, |
---|
| 338 | we might still know the groupchat itself. */ |
---|
| 339 | *nick = 0; |
---|
| 340 | chat = jabber_chat_by_jid( ic, s ); |
---|
| 341 | *nick = '/'; |
---|
| 342 | |
---|
| 343 | nick ++; |
---|
| 344 | } |
---|
| 345 | else |
---|
| 346 | { |
---|
| 347 | /* message.c uses the EXACT_JID option, so bud should |
---|
| 348 | always be NULL here for bare JIDs. */ |
---|
| 349 | chat = jabber_chat_by_jid( ic, s ); |
---|
| 350 | } |
---|
| 351 | |
---|
| 352 | if( nick == NULL ) |
---|
| 353 | { |
---|
[e35d1a1] | 354 | /* This is fine, the groupchat itself isn't in jd->buddies. */ |
---|
[a882d6c] | 355 | if( chat ) |
---|
| 356 | imcb_chat_log( chat, "From conference server: %s", body->text ); |
---|
| 357 | else |
---|
| 358 | imcb_log( ic, "System message from unknown groupchat %s: %s", s, body->text ); |
---|
| 359 | } |
---|
[e35d1a1] | 360 | else |
---|
[a882d6c] | 361 | { |
---|
| 362 | /* This can happen too, at least when receiving a backlog when |
---|
| 363 | just joining a channel. */ |
---|
| 364 | if( chat ) |
---|
| 365 | imcb_chat_log( chat, "Message from unknown participant %s: %s", nick, body->text ); |
---|
| 366 | else |
---|
| 367 | imcb_log( ic, "Groupchat message from unknown JID %s: %s", s, body->text ); |
---|
| 368 | } |
---|
[e35d1a1] | 369 | |
---|
| 370 | return; |
---|
| 371 | } |
---|
[63075d7] | 372 | else if( chat == NULL ) |
---|
[e35d1a1] | 373 | { |
---|
| 374 | /* How could this happen?? We could do kill( self, 11 ) |
---|
| 375 | now or just wait for the OS to do it. :-) */ |
---|
| 376 | return; |
---|
| 377 | } |
---|
| 378 | if( body && body->text_len > 0 ) |
---|
| 379 | { |
---|
[6286f80] | 380 | s = strchr( bud->ext_jid, '/' ); |
---|
[e35d1a1] | 381 | if( s ) *s = 0; |
---|
[6286f80] | 382 | imcb_chat_msg( chat, bud->ext_jid, body->text, 0, jabber_get_timestamp( node ) ); |
---|
[e35d1a1] | 383 | if( s ) *s = '/'; |
---|
| 384 | } |
---|
| 385 | } |
---|