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