[e35d1a1] | 1 | /***************************************************************************\ |
---|
| 2 | * * |
---|
| 3 | * BitlBee - An IRC to IM gateway * |
---|
| 4 | * Jabber module - Conference rooms * |
---|
| 5 | * * |
---|
[0e788f5] | 6 | * Copyright 2007-2012 Wilmer van der Gaast <wilmer@gaast.net> * |
---|
[e35d1a1] | 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" |
---|
[fc0640e] | 25 | #include "sha1.h" |
---|
[e35d1a1] | 26 | |
---|
[5ebff60] | 27 | static xt_status jabber_chat_join_failed(struct im_connection *ic, struct xt_node *node, struct xt_node *orig); |
---|
[fb2338d] | 28 | static xt_status jabber_chat_self_message(struct im_connection *ic, struct xt_node *node, struct xt_node *orig); |
---|
[5bd21df] | 29 | |
---|
[3320d6d] | 30 | struct groupchat *jabber_chat_join(struct im_connection *ic, const char *room, const char *nick, const char *password, |
---|
| 31 | gboolean always_use_nicks) |
---|
[e35d1a1] | 32 | { |
---|
| 33 | struct jabber_chat *jc; |
---|
| 34 | struct xt_node *node; |
---|
| 35 | struct groupchat *c; |
---|
| 36 | char *roomjid; |
---|
[5ebff60] | 37 | |
---|
| 38 | roomjid = g_strdup_printf("%s/%s", room, nick); |
---|
| 39 | node = xt_new_node("x", NULL, NULL); |
---|
| 40 | xt_add_attr(node, "xmlns", XMLNS_MUC); |
---|
| 41 | if (password) { |
---|
| 42 | xt_add_child(node, xt_new_node("password", password, NULL)); |
---|
| 43 | } |
---|
| 44 | node = jabber_make_packet("presence", NULL, roomjid, node); |
---|
| 45 | jabber_cache_add(ic, node, jabber_chat_join_failed); |
---|
| 46 | |
---|
| 47 | if (!jabber_write_packet(ic, node)) { |
---|
| 48 | g_free(roomjid); |
---|
[e35d1a1] | 49 | return NULL; |
---|
| 50 | } |
---|
[5ebff60] | 51 | |
---|
| 52 | jc = g_new0(struct jabber_chat, 1); |
---|
| 53 | jc->name = jabber_normalize(room); |
---|
| 54 | |
---|
| 55 | if ((jc->me = jabber_buddy_add(ic, roomjid)) == NULL) { |
---|
| 56 | g_free(roomjid); |
---|
| 57 | g_free(jc->name); |
---|
| 58 | g_free(jc); |
---|
[e35d1a1] | 59 | return NULL; |
---|
| 60 | } |
---|
[5ebff60] | 61 | |
---|
[3320d6d] | 62 | if (always_use_nicks) { |
---|
| 63 | jc->flags = JCFLAG_ALWAYS_USE_NICKS; |
---|
| 64 | } |
---|
| 65 | |
---|
[9c9b37c] | 66 | /* roomjid isn't normalized yet, and we need an original version |
---|
| 67 | of the nick to send a proper presence update. */ |
---|
| 68 | jc->my_full_jid = roomjid; |
---|
[5ebff60] | 69 | |
---|
| 70 | c = imcb_chat_new(ic, room); |
---|
[e35d1a1] | 71 | c->data = jc; |
---|
[5ebff60] | 72 | |
---|
[e35d1a1] | 73 | return c; |
---|
| 74 | } |
---|
| 75 | |
---|
[5ebff60] | 76 | struct groupchat *jabber_chat_with(struct im_connection *ic, char *who) |
---|
[fc0640e] | 77 | { |
---|
| 78 | struct jabber_data *jd = ic->proto_data; |
---|
| 79 | struct jabber_chat *jc; |
---|
| 80 | struct groupchat *c; |
---|
| 81 | sha1_state_t sum; |
---|
| 82 | double now = gettime(); |
---|
| 83 | char *uuid, *rjid, *cserv; |
---|
[5ebff60] | 84 | |
---|
| 85 | sha1_init(&sum); |
---|
| 86 | sha1_append(&sum, (uint8_t *) ic->acc->user, strlen(ic->acc->user)); |
---|
| 87 | sha1_append(&sum, (uint8_t *) &now, sizeof(now)); |
---|
| 88 | sha1_append(&sum, (uint8_t *) who, strlen(who)); |
---|
| 89 | uuid = sha1_random_uuid(&sum); |
---|
| 90 | |
---|
| 91 | if (jd->flags & JFLAG_GTALK) { |
---|
| 92 | cserv = g_strdup("groupchat.google.com"); |
---|
| 93 | } else { |
---|
[fc0640e] | 94 | /* Guess... */ |
---|
[5ebff60] | 95 | cserv = g_strdup_printf("conference.%s", jd->server); |
---|
| 96 | } |
---|
| 97 | |
---|
| 98 | rjid = g_strdup_printf("private-chat-%s@%s", uuid, cserv); |
---|
| 99 | g_free(uuid); |
---|
| 100 | g_free(cserv); |
---|
| 101 | |
---|
[3320d6d] | 102 | c = jabber_chat_join(ic, rjid, jd->username, NULL, FALSE); |
---|
[5ebff60] | 103 | g_free(rjid); |
---|
| 104 | if (c == NULL) { |
---|
[fc0640e] | 105 | return NULL; |
---|
[5ebff60] | 106 | } |
---|
| 107 | |
---|
[fc0640e] | 108 | jc = c->data; |
---|
[5ebff60] | 109 | jc->invite = g_strdup(who); |
---|
| 110 | |
---|
[fc0640e] | 111 | return c; |
---|
| 112 | } |
---|
| 113 | |
---|
[5ebff60] | 114 | static xt_status jabber_chat_join_failed(struct im_connection *ic, struct xt_node *node, struct xt_node *orig) |
---|
[5bd21df] | 115 | { |
---|
| 116 | struct jabber_error *err; |
---|
| 117 | struct jabber_buddy *bud; |
---|
| 118 | char *room; |
---|
[5ebff60] | 119 | |
---|
| 120 | room = xt_find_attr(orig, "to"); |
---|
| 121 | bud = jabber_buddy_by_jid(ic, room, 0); |
---|
| 122 | err = jabber_error_parse(xt_find_node(node->children, "error"), XMLNS_STANZA_ERROR); |
---|
| 123 | if (err) { |
---|
| 124 | imcb_error(ic, "Error joining groupchat %s: %s%s%s", room, err->code, |
---|
| 125 | err->text ? ": " : "", err->text ? err->text : ""); |
---|
| 126 | jabber_error_free(err); |
---|
[5bd21df] | 127 | } |
---|
[5ebff60] | 128 | if (bud) { |
---|
[c34247d] | 129 | struct groupchat *c = jabber_chat_by_jid(ic, bud->bare_jid); |
---|
| 130 | if (c) { |
---|
| 131 | jabber_chat_free(c); |
---|
| 132 | } |
---|
[5ebff60] | 133 | } |
---|
| 134 | |
---|
[80e9db9] | 135 | return XT_HANDLED; |
---|
[5bd21df] | 136 | } |
---|
| 137 | |
---|
[fb2338d] | 138 | static xt_status jabber_chat_self_message(struct im_connection *ic, struct xt_node *node, struct xt_node *orig) |
---|
| 139 | { |
---|
| 140 | /* This is a self message sent by this bitlbee - just drop it */ |
---|
| 141 | return XT_ABORT; |
---|
| 142 | } |
---|
| 143 | |
---|
[5ebff60] | 144 | struct groupchat *jabber_chat_by_jid(struct im_connection *ic, const char *name) |
---|
[5bd21df] | 145 | { |
---|
[5ebff60] | 146 | char *normalized = jabber_normalize(name); |
---|
[aea8b68] | 147 | GSList *l; |
---|
[5bd21df] | 148 | struct groupchat *ret; |
---|
| 149 | struct jabber_chat *jc; |
---|
[5ebff60] | 150 | |
---|
| 151 | for (l = ic->groupchats; l; l = l->next) { |
---|
[aea8b68] | 152 | ret = l->data; |
---|
[5bd21df] | 153 | jc = ret->data; |
---|
[5ebff60] | 154 | if (strcmp(normalized, jc->name) == 0) { |
---|
[5bd21df] | 155 | break; |
---|
[5ebff60] | 156 | } |
---|
[5bd21df] | 157 | } |
---|
[5ebff60] | 158 | g_free(normalized); |
---|
| 159 | |
---|
[58f5ef7] | 160 | return l ? ret : NULL; |
---|
[5bd21df] | 161 | } |
---|
| 162 | |
---|
[5ebff60] | 163 | void jabber_chat_free(struct groupchat *c) |
---|
[9da0bbf] | 164 | { |
---|
| 165 | struct jabber_chat *jc = c->data; |
---|
[5ebff60] | 166 | |
---|
| 167 | jabber_buddy_remove_bare(c->ic, jc->name); |
---|
| 168 | |
---|
| 169 | g_free(jc->my_full_jid); |
---|
| 170 | g_free(jc->name); |
---|
| 171 | g_free(jc->invite); |
---|
| 172 | g_free(jc); |
---|
| 173 | |
---|
| 174 | imcb_chat_free(c); |
---|
[9da0bbf] | 175 | } |
---|
| 176 | |
---|
[5ebff60] | 177 | int jabber_chat_msg(struct groupchat *c, char *message, int flags) |
---|
[43671b9] | 178 | { |
---|
| 179 | struct im_connection *ic = c->ic; |
---|
| 180 | struct jabber_chat *jc = c->data; |
---|
| 181 | struct xt_node *node; |
---|
[5ebff60] | 182 | |
---|
[63075d7] | 183 | jc->flags |= JCFLAG_MESSAGE_SENT; |
---|
[5ebff60] | 184 | |
---|
| 185 | node = xt_new_node("body", message, NULL); |
---|
| 186 | node = jabber_make_packet("message", "groupchat", jc->name, node); |
---|
| 187 | |
---|
[fb2338d] | 188 | jabber_cache_add(ic, node, jabber_chat_self_message); |
---|
[5ebff60] | 189 | |
---|
[fb2338d] | 190 | return !jabber_write_packet(ic, node); |
---|
[43671b9] | 191 | } |
---|
| 192 | |
---|
[5ebff60] | 193 | int jabber_chat_topic(struct groupchat *c, char *topic) |
---|
[ef5c185] | 194 | { |
---|
| 195 | struct im_connection *ic = c->ic; |
---|
| 196 | struct jabber_chat *jc = c->data; |
---|
| 197 | struct xt_node *node; |
---|
[5ebff60] | 198 | |
---|
| 199 | node = xt_new_node("subject", topic, NULL); |
---|
| 200 | node = jabber_make_packet("message", "groupchat", jc->name, node); |
---|
| 201 | |
---|
| 202 | if (!jabber_write_packet(ic, node)) { |
---|
| 203 | xt_free_node(node); |
---|
[ef5c185] | 204 | return 0; |
---|
| 205 | } |
---|
[5ebff60] | 206 | xt_free_node(node); |
---|
| 207 | |
---|
[ef5c185] | 208 | return 1; |
---|
| 209 | } |
---|
| 210 | |
---|
[5ebff60] | 211 | int jabber_chat_leave(struct groupchat *c, const char *reason) |
---|
[e35d1a1] | 212 | { |
---|
| 213 | struct im_connection *ic = c->ic; |
---|
| 214 | struct jabber_chat *jc = c->data; |
---|
| 215 | struct xt_node *node; |
---|
[5ebff60] | 216 | |
---|
| 217 | node = xt_new_node("x", NULL, NULL); |
---|
| 218 | xt_add_attr(node, "xmlns", XMLNS_MUC); |
---|
| 219 | node = jabber_make_packet("presence", "unavailable", jc->my_full_jid, node); |
---|
| 220 | |
---|
| 221 | if (!jabber_write_packet(ic, node)) { |
---|
| 222 | xt_free_node(node); |
---|
[e35d1a1] | 223 | return 0; |
---|
| 224 | } |
---|
[5ebff60] | 225 | xt_free_node(node); |
---|
| 226 | |
---|
[e35d1a1] | 227 | return 1; |
---|
| 228 | } |
---|
| 229 | |
---|
[5ebff60] | 230 | void jabber_chat_invite(struct groupchat *c, char *who, char *message) |
---|
[c058ff9] | 231 | { |
---|
| 232 | struct xt_node *node; |
---|
| 233 | struct im_connection *ic = c->ic; |
---|
| 234 | struct jabber_chat *jc = c->data; |
---|
| 235 | |
---|
[5ebff60] | 236 | node = xt_new_node("reason", message, NULL); |
---|
| 237 | |
---|
| 238 | node = xt_new_node("invite", NULL, node); |
---|
| 239 | xt_add_attr(node, "to", who); |
---|
[c058ff9] | 240 | |
---|
[5ebff60] | 241 | node = xt_new_node("x", NULL, node); |
---|
| 242 | xt_add_attr(node, "xmlns", XMLNS_MUC_USER); |
---|
[c058ff9] | 243 | |
---|
[5ebff60] | 244 | node = jabber_make_packet("message", NULL, jc->name, node); |
---|
[c058ff9] | 245 | |
---|
[5ebff60] | 246 | jabber_write_packet(ic, node); |
---|
[c058ff9] | 247 | |
---|
[5ebff60] | 248 | xt_free_node(node); |
---|
[c058ff9] | 249 | } |
---|
| 250 | |
---|
[8f8a56f] | 251 | static int jabber_chat_has_other_resources(struct im_connection *ic, struct jabber_buddy *bud) |
---|
| 252 | { |
---|
| 253 | struct jabber_buddy *cur; |
---|
| 254 | |
---|
| 255 | for (cur = jabber_buddy_by_jid(ic, bud->bare_jid, GET_BUDDY_FIRST); cur; cur = cur->next) { |
---|
| 256 | if (cur != bud && jabber_compare_jid(cur->ext_jid, bud->ext_jid)) { |
---|
| 257 | return TRUE; |
---|
| 258 | } |
---|
| 259 | } |
---|
| 260 | |
---|
| 261 | return FALSE; |
---|
| 262 | } |
---|
| 263 | |
---|
[e35d1a1] | 264 | /* Not really the same syntax as the normal pkt_ functions, but this isn't |
---|
[0e7ab64] | 265 | called by the xmltree parser directly and this way I can add some extra |
---|
[e35d1a1] | 266 | parameters so we won't have to repeat too many things done by the caller |
---|
| 267 | already. */ |
---|
[5ebff60] | 268 | void jabber_chat_pkt_presence(struct im_connection *ic, struct jabber_buddy *bud, struct xt_node *node) |
---|
[e35d1a1] | 269 | { |
---|
| 270 | struct groupchat *chat; |
---|
| 271 | struct xt_node *c; |
---|
[5ebff60] | 272 | char *type = xt_find_attr(node, "type"); |
---|
[68286eb] | 273 | struct jabber_data *jd = ic->proto_data; |
---|
[e35d1a1] | 274 | struct jabber_chat *jc; |
---|
| 275 | char *s; |
---|
[5ebff60] | 276 | |
---|
| 277 | if ((chat = jabber_chat_by_jid(ic, bud->bare_jid)) == NULL) { |
---|
[e35d1a1] | 278 | /* How could this happen?? We could do kill( self, 11 ) |
---|
| 279 | now or just wait for the OS to do it. :-) */ |
---|
| 280 | return; |
---|
| 281 | } |
---|
[5ebff60] | 282 | |
---|
[e35d1a1] | 283 | jc = chat->data; |
---|
[5ebff60] | 284 | |
---|
| 285 | if (type == NULL && !(bud->flags & JBFLAG_IS_CHATROOM)) { |
---|
[e35d1a1] | 286 | bud->flags |= JBFLAG_IS_CHATROOM; |
---|
| 287 | /* If this one wasn't set yet, this buddy just joined the chat. |
---|
| 288 | Slightly hackish way of finding out eh? ;-) */ |
---|
[5ebff60] | 289 | |
---|
[b9f8b87] | 290 | /* This is pretty messy... Here it sets ext_jid to the real |
---|
| 291 | JID of the participant. Works for non-anonymized channels. |
---|
| 292 | Might break if someone joins a chat twice, though. */ |
---|
[5ebff60] | 293 | for (c = node->children; (c = xt_find_node(c, "x")); c = c->next) { |
---|
| 294 | if ((s = xt_find_attr(c, "xmlns")) && |
---|
| 295 | (strcmp(s, XMLNS_MUC_USER) == 0)) { |
---|
[19176513] | 296 | struct xt_node *item; |
---|
[5ebff60] | 297 | |
---|
| 298 | item = xt_find_node(c->children, "item"); |
---|
| 299 | if ((s = xt_find_attr(item, "jid"))) { |
---|
[e35d1a1] | 300 | /* Yay, found what we need. :-) */ |
---|
[5ebff60] | 301 | bud->ext_jid = jabber_normalize(s); |
---|
[e35d1a1] | 302 | break; |
---|
| 303 | } |
---|
| 304 | } |
---|
[5ebff60] | 305 | } |
---|
| 306 | |
---|
[6286f80] | 307 | /* Make up some other handle, if necessary. */ |
---|
[5ebff60] | 308 | if (bud->ext_jid == NULL) { |
---|
| 309 | if (bud == jc->me) { |
---|
| 310 | bud->ext_jid = g_strdup(jd->me); |
---|
| 311 | } else { |
---|
[5d7dc00] | 312 | int i; |
---|
[5ebff60] | 313 | |
---|
[54f2f55] | 314 | /* Don't want the nick to be at the end, so let's |
---|
| 315 | think of some slightly different notation to use |
---|
| 316 | for anonymous groupchat participants in BitlBee. */ |
---|
[5ebff60] | 317 | bud->ext_jid = g_strdup_printf("%s=%s", bud->resource, bud->bare_jid); |
---|
| 318 | |
---|
[5d7dc00] | 319 | /* And strip any unwanted characters. */ |
---|
[5ebff60] | 320 | for (i = 0; bud->resource[i]; i++) { |
---|
| 321 | if (bud->ext_jid[i] == '=' || bud->ext_jid[i] == '@') { |
---|
[5d7dc00] | 322 | bud->ext_jid[i] = '_'; |
---|
[5ebff60] | 323 | } |
---|
| 324 | } |
---|
[5d7dc00] | 325 | } |
---|
[6286f80] | 326 | bud->flags |= JBFLAG_IS_ANONYMOUS; |
---|
| 327 | } |
---|
[5ebff60] | 328 | |
---|
| 329 | if (bud != jc->me && bud->flags & JBFLAG_IS_ANONYMOUS) { |
---|
[e73a501] | 330 | /* If JIDs are anonymized, add them to the local |
---|
| 331 | list for the duration of this chat. */ |
---|
[5ebff60] | 332 | imcb_add_buddy(ic, bud->ext_jid, NULL); |
---|
| 333 | imcb_buddy_nick_hint(ic, bud->ext_jid, bud->resource); |
---|
[c570c86] | 334 | } |
---|
[5ebff60] | 335 | |
---|
| 336 | if (bud == jc->me && jc->invite != NULL) { |
---|
| 337 | char *msg = g_strdup_printf("Please join me in room %s", jc->name); |
---|
| 338 | jabber_chat_invite(chat, jc->invite, msg); |
---|
| 339 | g_free(jc->invite); |
---|
| 340 | g_free(msg); |
---|
[fc0640e] | 341 | jc->invite = NULL; |
---|
| 342 | } |
---|
[5ebff60] | 343 | |
---|
| 344 | s = strchr(bud->ext_jid, '/'); |
---|
| 345 | if (s) { |
---|
| 346 | *s = 0; /* Should NEVER be NULL, but who knows... */ |
---|
| 347 | } |
---|
[3320d6d] | 348 | |
---|
| 349 | if (bud != jc->me && (jc->flags & JCFLAG_ALWAYS_USE_NICKS) && !(bud->flags & JBFLAG_IS_ANONYMOUS)) { |
---|
| 350 | imcb_buddy_nick_change(ic, bud->ext_jid, bud->resource); |
---|
| 351 | } |
---|
| 352 | |
---|
[5ebff60] | 353 | imcb_chat_add_buddy(chat, bud->ext_jid); |
---|
| 354 | if (s) { |
---|
| 355 | *s = '/'; |
---|
| 356 | } |
---|
| 357 | } else if (type) { /* type can only be NULL or "unavailable" in this function */ |
---|
[8f8a56f] | 358 | if ((bud->flags & JBFLAG_IS_CHATROOM) && bud->ext_jid && !jabber_chat_has_other_resources(ic, bud)) { |
---|
[83f179d6] | 359 | char *reason = NULL; |
---|
[3c23681] | 360 | char *status = NULL; |
---|
| 361 | char *status_text = NULL; |
---|
[83f179d6] | 362 | |
---|
| 363 | if ((c = xt_find_node_by_attr(node->children, "x", "xmlns", XMLNS_MUC_USER))) { |
---|
[3c23681] | 364 | struct xt_node *c2 = c->children; |
---|
| 365 | |
---|
| 366 | while ((c2 = xt_find_node(c2, "status"))) { |
---|
| 367 | char *code = xt_find_attr(c2, "code"); |
---|
| 368 | if (g_strcmp0(code, "301") == 0) { |
---|
[83f179d6] | 369 | status = "Banned"; |
---|
[3c23681] | 370 | break; |
---|
| 371 | } else if (g_strcmp0(code, "303") == 0) { |
---|
[83f179d6] | 372 | /* This could be handled in a cleverer way, |
---|
| 373 | * but let's just show a literal part/join for now */ |
---|
| 374 | status = "Changing nicks"; |
---|
[3c23681] | 375 | break; |
---|
| 376 | } else if (g_strcmp0(code, "307") == 0) { |
---|
[83f179d6] | 377 | status = "Kicked"; |
---|
[3c23681] | 378 | break; |
---|
[83f179d6] | 379 | } |
---|
[3c23681] | 380 | c2 = c2->next; |
---|
[83f179d6] | 381 | } |
---|
| 382 | |
---|
[3c23681] | 383 | /* Sometimes the status message is in presence/x/item/reason */ |
---|
[83f179d6] | 384 | if ((c2 = xt_find_path(c, "item/reason")) && c2->text && c2->text_len) { |
---|
[3c23681] | 385 | status_text = c2->text; |
---|
[83f179d6] | 386 | } |
---|
| 387 | } |
---|
| 388 | |
---|
[3c23681] | 389 | /* Sometimes the status message is right inside <presence> */ |
---|
| 390 | if ((c = xt_find_node(node->children, "status")) && c->text && c->text_len) { |
---|
| 391 | status_text = c->text; |
---|
| 392 | } |
---|
| 393 | |
---|
| 394 | if (status_text && status) { |
---|
| 395 | reason = g_strdup_printf("%s: %s", status, status_text); |
---|
| 396 | } else { |
---|
| 397 | reason = g_strdup(status_text ? : status); |
---|
| 398 | } |
---|
| 399 | |
---|
[5ebff60] | 400 | s = strchr(bud->ext_jid, '/'); |
---|
| 401 | if (s) { |
---|
| 402 | *s = 0; |
---|
| 403 | } |
---|
[83f179d6] | 404 | imcb_chat_remove_buddy(chat, bud->ext_jid, reason); |
---|
[5ebff60] | 405 | if (bud != jc->me && bud->flags & JBFLAG_IS_ANONYMOUS) { |
---|
[83f179d6] | 406 | imcb_remove_buddy(ic, bud->ext_jid, reason); |
---|
[5ebff60] | 407 | } |
---|
| 408 | if (s) { |
---|
| 409 | *s = '/'; |
---|
| 410 | } |
---|
[83f179d6] | 411 | |
---|
| 412 | g_free(reason); |
---|
[5ebff60] | 413 | } |
---|
| 414 | |
---|
| 415 | if (bud == jc->me) { |
---|
| 416 | jabber_chat_free(chat); |
---|
[bdad407] | 417 | } |
---|
[e35d1a1] | 418 | } |
---|
| 419 | } |
---|
| 420 | |
---|
[5ebff60] | 421 | void jabber_chat_pkt_message(struct im_connection *ic, struct jabber_buddy *bud, struct xt_node *node) |
---|
[e35d1a1] | 422 | { |
---|
[5ebff60] | 423 | struct xt_node *subject = xt_find_node(node->children, "subject"); |
---|
| 424 | struct xt_node *body = xt_find_node(node->children, "body"); |
---|
[3d31618] | 425 | struct groupchat *chat = NULL; |
---|
| 426 | struct jabber_chat *jc = NULL; |
---|
| 427 | char *from = NULL; |
---|
| 428 | char *nick = NULL; |
---|
| 429 | char *final_from = NULL; |
---|
| 430 | char *bare_jid = NULL; |
---|
[fb2338d] | 431 | guint32 flags = 0; |
---|
[5ebff60] | 432 | |
---|
[3d31618] | 433 | from = (bud) ? bud->full_jid : xt_find_attr(node, "from"); |
---|
| 434 | |
---|
| 435 | if (from) { |
---|
| 436 | nick = strchr(from, '/'); |
---|
| 437 | if (nick) { |
---|
| 438 | *nick = 0; |
---|
[5ebff60] | 439 | } |
---|
[3d31618] | 440 | chat = jabber_chat_by_jid(ic, from); |
---|
| 441 | if (nick) { |
---|
| 442 | *nick = '/'; |
---|
| 443 | nick++; |
---|
[5ebff60] | 444 | } |
---|
[41e0c00] | 445 | } |
---|
[5ebff60] | 446 | |
---|
[3d31618] | 447 | jc = (chat) ? chat->data : NULL; |
---|
[5ebff60] | 448 | |
---|
[3d31618] | 449 | if (!bud) { |
---|
| 450 | struct xt_node *c; |
---|
| 451 | char *s; |
---|
| 452 | |
---|
| 453 | /* Try some clever stuff to find out the real JID here */ |
---|
| 454 | c = xt_find_node_by_attr(node->children, "delay", "xmlns", XMLNS_DELAY); |
---|
| 455 | |
---|
| 456 | if (c && ((s = xt_find_attr(c, "from")) || |
---|
| 457 | (s = xt_find_attr(c, "from_jid")))) { |
---|
| 458 | /* This won't be useful if it's the MUC JID */ |
---|
| 459 | if (!(jc && jabber_compare_jid(s, jc->name))) { |
---|
| 460 | /* Hopefully this one makes more sense! */ |
---|
| 461 | bud = jabber_buddy_by_jid(ic, s, GET_BUDDY_FIRST | GET_BUDDY_CREAT); |
---|
| 462 | } |
---|
[5ebff60] | 463 | } |
---|
| 464 | |
---|
[3d31618] | 465 | } |
---|
[5ebff60] | 466 | |
---|
[3d31618] | 467 | if (subject && chat) { |
---|
[3cbf71d] | 468 | char *subject_text = subject->text_len > 0 ? subject->text : ""; |
---|
[3d31618] | 469 | if (g_strcmp0(chat->topic, subject_text) != 0) { |
---|
| 470 | bare_jid = (bud) ? jabber_get_bare_jid(bud->ext_jid) : NULL; |
---|
| 471 | imcb_chat_topic(chat, bare_jid, subject_text, |
---|
| 472 | jabber_get_timestamp(node)); |
---|
| 473 | g_free(bare_jid); |
---|
[3cbf71d] | 474 | bare_jid = NULL; |
---|
[a882d6c] | 475 | } |
---|
[3d31618] | 476 | } |
---|
[5ebff60] | 477 | |
---|
[3d31618] | 478 | if (body == NULL || body->text_len == 0) { |
---|
| 479 | /* Meh. Empty messages aren't very interesting, no matter |
---|
| 480 | how much some servers love to send them. */ |
---|
| 481 | return; |
---|
| 482 | } |
---|
| 483 | |
---|
| 484 | if (chat == NULL) { |
---|
[5ebff60] | 485 | if (nick == NULL) { |
---|
[3d31618] | 486 | imcb_log(ic, "System message from unknown groupchat %s: %s", from, body->text); |
---|
[5ebff60] | 487 | } else { |
---|
[3d31618] | 488 | imcb_log(ic, "Groupchat message from unknown JID %s: %s", from, body->text); |
---|
[a882d6c] | 489 | } |
---|
[5ebff60] | 490 | |
---|
[e35d1a1] | 491 | return; |
---|
[3d31618] | 492 | } else if (chat != NULL && bud == NULL && nick == NULL) { |
---|
| 493 | imcb_chat_log(chat, "From conference server: %s", body->text); |
---|
| 494 | return; |
---|
[fb2338d] | 495 | } else if (jc && jc->flags & JCFLAG_MESSAGE_SENT && bud == jc->me && |
---|
| 496 | (jabber_cache_handle_packet(ic, node) == XT_ABORT)) { |
---|
| 497 | /* Self message marked by this bitlbee, don't show it */ |
---|
[e35d1a1] | 498 | return; |
---|
| 499 | } |
---|
[3d31618] | 500 | |
---|
[fb2338d] | 501 | if (bud) { |
---|
[3d31618] | 502 | bare_jid = jabber_get_bare_jid(bud->ext_jid ? bud->ext_jid : bud->full_jid); |
---|
| 503 | final_from = bare_jid; |
---|
[eee7294] | 504 | if (bud == jc->me || (g_strcasecmp(final_from, ic->acc->user) == 0)) { |
---|
| 505 | flags = OPT_SELFMESSAGE; |
---|
| 506 | } |
---|
[3d31618] | 507 | } else { |
---|
| 508 | final_from = nick; |
---|
[e35d1a1] | 509 | } |
---|
[3d31618] | 510 | |
---|
[fb2338d] | 511 | imcb_chat_msg(chat, final_from, body->text, flags, jabber_get_timestamp(node)); |
---|
[3d31618] | 512 | |
---|
| 513 | g_free(bare_jid); |
---|
[e35d1a1] | 514 | } |
---|