Tue, 07 Oct 2003 18:29:33 +0000
[gaim-migrate @ 7755]
I can't wait till I get to implement JEP-0085 and get rid of this crap
| 7014 | 1 | /* |
| 2 | * gaim - Jabber Protocol Plugin | |
| 3 | * | |
| 4 | * Copyright (C) 2003, Nathan Walp <faceprint@faceprint.com> | |
| 5 | * | |
| 6 | * This program is free software; you can redistribute it and/or modify | |
| 7 | * it under the terms of the GNU General Public License as published by | |
| 8 | * the Free Software Foundation; either version 2 of the License, or | |
| 9 | * (at your option) any later version. | |
| 10 | * | |
| 11 | * This program is distributed in the hope that it will be useful, | |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 14 | * GNU General Public License for more details. | |
| 15 | * | |
| 16 | * You should have received a copy of the GNU General Public License | |
| 17 | * along with this program; if not, write to the Free Software | |
| 18 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
| 19 | * | |
| 20 | */ | |
| 21 | #include "internal.h" | |
| 22 | ||
| 23 | #include "debug.h" | |
| 24 | #include "notify.h" | |
| 25 | #include "server.h" | |
|
7095
17d2b54254f8
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7014
diff
changeset
|
26 | #include "util.h" |
| 7014 | 27 | |
| 28 | #include "buddy.h" | |
| 29 | #include "chat.h" | |
| 30 | #include "message.h" | |
| 31 | #include "xmlnode.h" | |
| 32 | ||
| 33 | #define JABBER_TYPING_NOTIFY_INT 15 | |
| 34 | ||
| 35 | void jabber_message_free(JabberMessage *jm) | |
| 36 | { | |
| 37 | if(jm->from) | |
| 38 | g_free(jm->from); | |
| 39 | if(jm->to) | |
| 40 | g_free(jm->to); | |
| 41 | if(jm->subject) | |
| 42 | g_free(jm->subject); | |
| 43 | if(jm->body) | |
| 44 | g_free(jm->body); | |
| 45 | if(jm->xhtml) | |
| 46 | g_free(jm->xhtml); | |
| 47 | if(jm->password) | |
| 48 | g_free(jm->password); | |
| 7145 | 49 | if(jm->etc) |
| 50 | g_list_free(jm->etc); | |
| 7014 | 51 | |
| 52 | g_free(jm); | |
| 53 | } | |
| 54 | ||
| 7145 | 55 | static void handle_chat(JabberMessage *jm) |
| 7014 | 56 | { |
| 57 | JabberID *jid = jabber_id_new(jm->from); | |
| 58 | char *from; | |
| 59 | ||
| 60 | JabberBuddy *jb; | |
| 61 | JabberBuddyResource *jbr; | |
| 62 | ||
| 63 | jb = jabber_buddy_find(jm->js, jm->from, TRUE); | |
| 64 | jbr = jabber_buddy_find_resource(jb, jabber_get_resource(jm->from)); | |
| 65 | ||
| 66 | if(gaim_find_conversation_with_account(jm->from, jm->js->gc->account)) | |
| 67 | from = g_strdup(jm->from); | |
| 68 | else if(jid->node) | |
| 69 | from = g_strdup_printf("%s@%s", jid->node, jid->domain); | |
| 70 | else | |
| 71 | from = g_strdup(jid->domain); | |
| 72 | ||
| 73 | if(!jm->xhtml && !jm->body) { | |
| 74 | if(jm->events & JABBER_MESSAGE_EVENT_COMPOSING) | |
| 75 | serv_got_typing(jm->js->gc, from, 0, GAIM_TYPING); | |
| 76 | else | |
| 77 | serv_got_typing_stopped(jm->js->gc, from); | |
| 78 | } else { | |
| 79 | if(jbr && jm->events & JABBER_MESSAGE_EVENT_COMPOSING) | |
| 80 | jbr->capabilities |= JABBER_CAP_COMPOSING; | |
| 81 | serv_got_im(jm->js->gc, from, jm->xhtml ? jm->xhtml : jm->body, 0, | |
| 82 | jm->sent); | |
| 83 | } | |
| 84 | ||
| 85 | g_free(from); | |
| 86 | jabber_id_free(jid); | |
| 87 | } | |
| 88 | ||
| 7145 | 89 | static void handle_headline(JabberMessage *jm) |
| 90 | { | |
| 91 | char *title; | |
| 92 | GString *body = g_string_new(""); | |
| 93 | GList *etc; | |
| 94 | ||
| 95 | title = g_strdup_printf(_("Message from %s"), jm->from); | |
| 96 | ||
| 97 | if(jm->xhtml) | |
| 98 | g_string_append(body, jm->xhtml); | |
| 99 | else if(jm->body) | |
| 100 | g_string_append(body, jm->body); | |
| 101 | ||
| 102 | for(etc = jm->etc; etc; etc = etc->next) { | |
| 103 | xmlnode *x = etc->data; | |
| 104 | const char *xmlns = xmlnode_get_attrib(x, "xmlns"); | |
| 105 | if(xmlns && !strcmp(xmlns, "jabber:x:oob")) { | |
| 106 | xmlnode *url, *desc; | |
| 107 | char *urltxt, *desctxt; | |
| 108 | ||
| 109 | url = xmlnode_get_child(x, "url"); | |
| 110 | desc = xmlnode_get_child(x, "desc"); | |
| 111 | ||
| 112 | if(!url || !desc) | |
| 113 | continue; | |
| 114 | ||
| 115 | urltxt = xmlnode_get_data(url); | |
| 116 | desctxt = xmlnode_get_data(desc); | |
| 117 | ||
| 118 | /* I'm all about ugly hacks */ | |
| 119 | if(body->len && !strcmp(body->str, jm->body)) | |
| 120 | g_string_printf(body, "<a href='%s'>%s</a>", | |
| 121 | urltxt, desctxt); | |
| 122 | else | |
| 123 | g_string_append_printf(body, "<br/><a href='%s'>%s</a>", | |
| 124 | urltxt, desctxt); | |
| 125 | ||
| 126 | g_free(urltxt); | |
| 127 | g_free(desctxt); | |
| 128 | } | |
| 129 | } | |
| 130 | ||
| 131 | gaim_notify_formatted(jm->js->gc, title, jm->subject ? jm->subject : title, | |
| 132 | NULL, body->str, NULL, NULL); | |
| 133 | ||
| 134 | g_free(title); | |
| 135 | g_string_free(body, TRUE); | |
| 136 | } | |
| 137 | ||
| 138 | static void handle_groupchat(JabberMessage *jm) | |
| 7014 | 139 | { |
| 140 | JabberID *jid = jabber_id_new(jm->from); | |
| 141 | JabberChat *chat = jabber_chat_find(jm->js, jid->node, jid->domain); | |
| 142 | ||
| 143 | if(!chat) | |
| 144 | return; | |
| 145 | ||
| 146 | if(jm->subject) | |
| 7183 | 147 | gaim_conv_chat_set_topic(GAIM_CONV_CHAT(chat->conv), jid->resource, |
| 148 | jm->subject); | |
| 7014 | 149 | |
| 7183 | 150 | if(jm->xhtml || jm->body) |
| 151 | serv_got_chat_in(jm->js->gc, chat->id, jabber_get_resource(jm->from), | |
| 152 | 0, jm->xhtml ? jm->xhtml : jm->body, jm->sent); | |
| 7014 | 153 | jabber_id_free(jid); |
| 154 | } | |
| 155 | ||
| 7145 | 156 | static void handle_groupchat_invite(JabberMessage *jm) |
| 7014 | 157 | { |
| 158 | GHashTable *components = g_hash_table_new_full(g_str_hash, g_str_equal, | |
| 159 | g_free, g_free); | |
| 160 | JabberID *jid = jabber_id_new(jm->to); | |
| 161 | ||
| 162 | g_hash_table_replace(components, g_strdup("room"), jid->node); | |
| 163 | g_hash_table_replace(components, g_strdup("server"), jid->node); | |
| 164 | g_hash_table_replace(components, g_strdup("handle"), jm->js->user->node); | |
| 165 | g_hash_table_replace(components, g_strdup("password"), jm->password); | |
| 166 | ||
| 167 | jabber_id_free(jid); | |
| 168 | serv_got_chat_invite(jm->js->gc, jm->to, jm->from, jm->body, components); | |
| 169 | } | |
| 170 | ||
| 7145 | 171 | static void handle_error(JabberMessage *jm) |
| 7014 | 172 | { |
| 173 | char *buf; | |
| 174 | ||
| 175 | if(!jm->body) | |
| 176 | return; | |
| 177 | ||
| 178 | buf = g_strdup_printf(_("Message delivery to %s failed: %s"), | |
| 179 | jm->from, jm->error); | |
| 180 | ||
| 181 | gaim_notify_error(jm->js->gc, _("Jabber Message Error"), buf, jm->body); | |
| 182 | ||
| 183 | g_free(buf); | |
| 184 | } | |
| 185 | ||
| 186 | void jabber_message_parse(JabberStream *js, xmlnode *packet) | |
| 187 | { | |
| 188 | JabberMessage *jm; | |
| 189 | const char *type; | |
| 190 | xmlnode *child; | |
| 191 | ||
| 192 | if(strcmp(packet->name, "message")) | |
| 193 | return; | |
| 194 | ||
| 195 | jm = g_new0(JabberMessage, 1); | |
| 196 | jm->js = js; | |
| 197 | jm->sent = time(NULL); | |
| 198 | ||
| 199 | type = xmlnode_get_attrib(packet, "type"); | |
| 200 | ||
| 201 | if(type) { | |
| 202 | if(!strcmp(type, "normal")) | |
| 203 | jm->type = JABBER_MESSAGE_NORMAL; | |
| 204 | else if(!strcmp(type, "chat")) | |
| 205 | jm->type = JABBER_MESSAGE_CHAT; | |
| 206 | else if(!strcmp(type, "groupchat")) | |
| 207 | jm->type = JABBER_MESSAGE_GROUPCHAT; | |
| 208 | else if(!strcmp(type, "headline")) | |
| 209 | jm->type = JABBER_MESSAGE_HEADLINE; | |
| 210 | else if(!strcmp(type, "error")) | |
| 211 | jm->type = JABBER_MESSAGE_ERROR; | |
| 212 | else | |
| 213 | jm->type = JABBER_MESSAGE_OTHER; | |
| 214 | } else { | |
| 215 | jm->type = JABBER_MESSAGE_NORMAL; | |
| 216 | } | |
| 217 | ||
| 218 | jm->from = g_strdup(xmlnode_get_attrib(packet, "from")); | |
| 219 | jm->to = g_strdup(xmlnode_get_attrib(packet, "to")); | |
| 220 | ||
| 221 | for(child = packet->child; child; child = child->next) { | |
| 222 | if(child->type != NODE_TYPE_TAG) | |
| 223 | continue; | |
| 224 | ||
| 225 | if(!strcmp(child->name, "subject")) { | |
| 226 | if(!jm->subject) | |
| 227 | jm->subject = xmlnode_get_data(child); | |
| 228 | } else if(!strcmp(child->name, "body")) { | |
| 229 | if(!jm->body) | |
| 230 | jm->body = xmlnode_get_data(child); | |
| 231 | } else if(!strcmp(child->name, "html") && child->child) { | |
| 232 | /* check to see if the <html> actually contains anything, | |
| 233 | * otherwise we'll ignore it */ | |
| 234 | char *txt = xmlnode_get_data(child); | |
| 235 | if(!jm->xhtml && txt) | |
| 236 | jm->xhtml = xmlnode_to_str(child); | |
| 237 | g_free(txt); | |
| 238 | } else if(!strcmp(child->name, "error")) { | |
| 239 | const char *code = xmlnode_get_attrib(child, "code"); | |
| 240 | char *code_txt = NULL; | |
| 241 | char *text = xmlnode_get_data(child); | |
| 242 | ||
| 243 | if(code) | |
| 244 | code_txt = g_strdup_printf(_(" (Code %s)"), code); | |
| 245 | ||
| 246 | if(!jm->error) | |
| 247 | jm->error = g_strdup_printf("%s%s", text ? text : "", | |
| 248 | code_txt ? code_txt : ""); | |
| 249 | ||
| 250 | g_free(code_txt); | |
| 251 | g_free(text); | |
| 252 | } else if(!strcmp(child->name, "x")) { | |
| 253 | const char *xmlns = xmlnode_get_attrib(child, "xmlns"); | |
| 254 | if(xmlns && !strcmp(xmlns, "jabber:x:event")) { | |
| 255 | if(xmlnode_get_child(child, "composing")) | |
| 256 | jm->events |= JABBER_MESSAGE_EVENT_COMPOSING; | |
| 257 | } else if(xmlns && !strcmp(xmlns, "jabber:x:delay")) { | |
| 258 | const char *timestamp = xmlnode_get_attrib(child, "stamp"); | |
| 259 | if(timestamp) | |
| 260 | jm->sent = str_to_time(timestamp); | |
| 261 | } else if(xmlns && !strcmp(xmlns, "jabber:x:conference") && | |
| 262 | jm->type != JABBER_MESSAGE_GROUPCHAT_INVITE) { | |
| 263 | const char *jid = xmlnode_get_attrib(child, "jid"); | |
| 264 | if(jid) { | |
| 265 | jm->type = JABBER_MESSAGE_GROUPCHAT_INVITE; | |
| 266 | g_free(jm->to); | |
| 267 | jm->to = g_strdup(jid); | |
| 268 | } | |
| 269 | } else if(xmlns && !strcmp(xmlns, | |
| 270 | "http://jabber.org/protocol/muc#user")) { | |
| 271 | xmlnode *invite = xmlnode_get_child(child, "invite"); | |
| 272 | if(invite) { | |
| 273 | xmlnode *reason, *password; | |
| 274 | const char *jid = xmlnode_get_attrib(child, "from"); | |
| 275 | g_free(jm->to); | |
| 276 | jm->to = jm->from; | |
| 277 | jm->from = g_strdup(jid); | |
| 278 | if((reason = xmlnode_get_child(invite, "reason"))) { | |
| 279 | g_free(jm->body); | |
| 280 | jm->body = xmlnode_get_data(reason); | |
| 281 | } | |
| 282 | if((password = xmlnode_get_child(invite, "password"))) | |
| 283 | jm->password = xmlnode_get_data(password); | |
| 284 | ||
| 285 | jm->type = JABBER_MESSAGE_GROUPCHAT_INVITE; | |
| 286 | } | |
| 7145 | 287 | } else { |
| 288 | jm->etc = g_list_append(jm->etc, child); | |
| 7014 | 289 | } |
| 290 | } | |
| 291 | } | |
| 292 | ||
| 293 | switch(jm->type) { | |
| 294 | case JABBER_MESSAGE_NORMAL: | |
| 295 | case JABBER_MESSAGE_CHAT: | |
| 7145 | 296 | handle_chat(jm); |
| 297 | break; | |
| 7014 | 298 | case JABBER_MESSAGE_HEADLINE: |
| 7145 | 299 | handle_headline(jm); |
| 7014 | 300 | break; |
| 301 | case JABBER_MESSAGE_GROUPCHAT: | |
| 302 | handle_groupchat(jm); | |
| 303 | break; | |
| 304 | case JABBER_MESSAGE_GROUPCHAT_INVITE: | |
| 305 | handle_groupchat_invite(jm); | |
| 306 | break; | |
| 307 | case JABBER_MESSAGE_ERROR: | |
| 308 | handle_error(jm); | |
| 309 | break; | |
| 310 | case JABBER_MESSAGE_OTHER: | |
| 311 | gaim_debug(GAIM_DEBUG_INFO, "jabber", | |
| 312 | "Received message of unknown type: %s\n", type); | |
| 313 | break; | |
| 314 | } | |
| 315 | jabber_message_free(jm); | |
| 316 | } | |
| 317 | ||
| 318 | void jabber_message_send(JabberMessage *jm) | |
| 319 | { | |
| 320 | xmlnode *message, *child; | |
| 321 | const char *type = NULL; | |
| 322 | ||
| 323 | message = xmlnode_new("message"); | |
| 324 | ||
| 325 | switch(jm->type) { | |
| 326 | case JABBER_MESSAGE_NORMAL: | |
| 327 | type = "normal"; | |
| 328 | break; | |
| 329 | case JABBER_MESSAGE_CHAT: | |
| 330 | case JABBER_MESSAGE_GROUPCHAT_INVITE: | |
| 331 | type = "chat"; | |
| 332 | break; | |
| 333 | case JABBER_MESSAGE_HEADLINE: | |
| 334 | type = "headline"; | |
| 335 | break; | |
| 336 | case JABBER_MESSAGE_GROUPCHAT: | |
| 337 | type = "groupchat"; | |
| 338 | break; | |
| 339 | case JABBER_MESSAGE_ERROR: | |
| 340 | type = "error"; | |
| 341 | break; | |
| 342 | case JABBER_MESSAGE_OTHER: | |
| 343 | type = NULL; | |
| 344 | break; | |
| 345 | } | |
| 346 | ||
| 347 | if(type) | |
| 348 | xmlnode_set_attrib(message, "type", type); | |
| 349 | ||
| 350 | xmlnode_set_attrib(message, "to", jm->to); | |
| 351 | ||
| 352 | if(jm->events || (!jm->body && !jm->xhtml)) { | |
| 353 | child = xmlnode_new_child(message, "x"); | |
| 354 | xmlnode_set_attrib(child, "xmlns", "jabber:x:event"); | |
| 355 | if(jm->events & JABBER_MESSAGE_EVENT_COMPOSING) | |
| 356 | xmlnode_new_child(child, "composing"); | |
| 357 | } | |
| 358 | ||
| 359 | if(jm->subject) { | |
| 360 | child = xmlnode_new_child(message, "subject"); | |
| 361 | xmlnode_insert_data(child, jm->subject, -1); | |
| 362 | } | |
| 363 | ||
| 364 | if(jm->body) { | |
| 365 | child = xmlnode_new_child(message, "body"); | |
| 366 | xmlnode_insert_data(child, jm->body, -1); | |
| 367 | } | |
| 368 | ||
| 369 | if(jm->xhtml) { | |
| 370 | child = xmlnode_from_str(jm->xhtml, -1); | |
| 371 | if(child) { | |
| 372 | xmlnode_insert_child(message, child); | |
| 373 | } else { | |
| 374 | gaim_debug(GAIM_DEBUG_ERROR, "jabber", | |
| 375 | "XHTML translation/validation failed, returning: %s\n", | |
| 376 | jm->xhtml); | |
| 377 | } | |
| 378 | } | |
| 379 | ||
| 380 | jabber_send(jm->js, message); | |
| 381 | ||
| 382 | xmlnode_free(message); | |
| 383 | } | |
| 384 | ||
| 385 | int jabber_message_send_im(GaimConnection *gc, const char *who, const char *msg, | |
|
7118
280b3b85a28a
[gaim-migrate @ 7685]
Christian Hammond <chipx86@chipx86.com>
parents:
7095
diff
changeset
|
386 | GaimConvImFlags flags) |
| 7014 | 387 | { |
| 388 | JabberMessage *jm; | |
| 389 | JabberBuddy *jb; | |
| 390 | JabberBuddyResource *jbr; | |
| 391 | char *buf; | |
| 7135 | 392 | char *xhtml; |
| 7014 | 393 | |
| 394 | if(!who || !msg) | |
| 395 | return 0; | |
| 396 | ||
| 397 | jb = jabber_buddy_find(gc->proto_data, who, TRUE); | |
| 398 | jbr = jabber_buddy_find_resource(jb, jabber_get_resource(who)); | |
| 399 | ||
| 400 | jm = g_new0(JabberMessage, 1); | |
| 401 | jm->js = gc->proto_data; | |
| 402 | jm->type = JABBER_MESSAGE_CHAT; | |
| 403 | jm->events = JABBER_MESSAGE_EVENT_COMPOSING; | |
| 404 | jm->to = g_strdup(who); | |
| 405 | ||
| 406 | buf = g_strdup_printf("<html xmlns='http://jabber.org/protocol/xhtml-im'><body>%s</body></html>", msg); | |
| 407 | ||
| 7135 | 408 | gaim_markup_html_to_xhtml(buf, &xhtml, &jm->body); |
| 7014 | 409 | g_free(buf); |
| 410 | ||
| 411 | if(!jbr || jbr->capabilities & JABBER_CAP_XHTML) | |
| 412 | jm->xhtml = xhtml; | |
| 413 | else | |
| 414 | g_free(xhtml); | |
| 415 | ||
| 416 | jabber_message_send(jm); | |
| 417 | jabber_message_free(jm); | |
| 418 | return 1; | |
| 419 | } | |
| 420 | ||
| 421 | int jabber_message_send_chat(GaimConnection *gc, int id, const char *message) | |
| 422 | { | |
| 423 | JabberChat *chat; | |
| 424 | JabberMessage *jm; | |
| 425 | JabberStream *js = gc->proto_data; | |
| 426 | ||
| 427 | if(!message) | |
| 428 | return 0; | |
| 429 | ||
| 430 | chat = jabber_chat_find_by_id(js, id); | |
| 431 | ||
| 432 | jm = g_new0(JabberMessage, 1); | |
| 433 | jm->js = gc->proto_data; | |
| 434 | jm->type = JABBER_MESSAGE_CHAT; | |
| 435 | jm->to = g_strdup_printf("%s@%s", chat->room, chat->server); | |
| 436 | ||
| 7135 | 437 | gaim_markup_html_to_xhtml(message, &jm->xhtml, &jm->body); |
| 7014 | 438 | |
| 439 | jabber_message_send(jm); | |
| 440 | jabber_message_free(jm); | |
| 441 | return 1; | |
| 442 | } | |
| 443 | ||
| 444 | int jabber_send_typing(GaimConnection *gc, const char *who, int typing) | |
| 445 | { | |
| 446 | JabberMessage *jm; | |
| 447 | JabberBuddy *jb; | |
| 448 | JabberBuddyResource *jbr; | |
| 449 | ||
| 450 | jb = jabber_buddy_find(gc->proto_data, who, TRUE); | |
| 451 | jbr = jabber_buddy_find_resource(jb, jabber_get_resource(who)); | |
| 452 | ||
| 7187 | 453 | if(!jbr || !(jbr->capabilities & JABBER_CAP_COMPOSING)) |
| 7014 | 454 | return 0; |
| 455 | ||
| 456 | jm = g_new0(JabberMessage, 1); | |
| 457 | jm->js = gc->proto_data; | |
| 458 | jm->type = JABBER_MESSAGE_CHAT; | |
| 459 | jm->to = g_strdup(who); | |
| 460 | ||
| 461 | if(typing == GAIM_TYPING) | |
| 462 | jm->events = JABBER_MESSAGE_EVENT_COMPOSING; | |
| 463 | ||
| 464 | jabber_message_send(jm); | |
| 465 | jabber_message_free(jm); | |
| 466 | ||
| 467 | return JABBER_TYPING_NOTIFY_INT; | |
| 468 | } | |
| 469 |