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