Mercurial > pidgin
annotate src/protocols/jabber/message.c @ 7281:0f7dd6715a90
[gaim-migrate @ 7860]
this fixes the { type entities
committer: Tailor Script <tailor@pidgin.im>
| author | Nathan Walp <nwalp@pidgin.im> |
|---|---|
| date | Thu, 16 Oct 2003 04:35:41 +0000 |
| parents | 1930e3d00ecd |
| children | 7c12dab8e513 |
| rev | line source |
|---|---|
| 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
c8bf2da398e3
[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 | |
| 7261 | 55 static GaimConversation * |
| 56 find_unnormalized_conv(const char *name, GaimAccount *account) | |
| 57 { | |
| 58 GaimConversation *c = NULL; | |
| 59 GList *cnv; | |
| 60 | |
| 61 g_return_val_if_fail(name != NULL, NULL); | |
| 62 | |
| 63 for(cnv = gaim_get_conversations(); cnv; cnv = cnv->next) { | |
| 64 c = (GaimConversation*)cnv->data; | |
| 65 if(!gaim_utf8_strcasecmp(name, gaim_conversation_get_name(c)) && | |
| 66 account == gaim_conversation_get_account(c)) | |
| 67 return c; | |
| 68 } | |
| 69 | |
| 70 return NULL; | |
| 71 } | |
| 72 | |
| 7145 | 73 static void handle_chat(JabberMessage *jm) |
| 7014 | 74 { |
| 75 JabberID *jid = jabber_id_new(jm->from); | |
| 76 char *from; | |
| 77 | |
| 78 JabberBuddy *jb; | |
| 79 JabberBuddyResource *jbr; | |
| 80 | |
| 81 jb = jabber_buddy_find(jm->js, jm->from, TRUE); | |
| 82 jbr = jabber_buddy_find_resource(jb, jabber_get_resource(jm->from)); | |
| 83 | |
| 7261 | 84 if(find_unnormalized_conv(jm->from, jm->js->gc->account)) { |
| 7014 | 85 from = g_strdup(jm->from); |
| 7258 | 86 } else if(jid->node) { |
| 87 GaimConversation *conv; | |
| 88 | |
| 7014 | 89 from = g_strdup_printf("%s@%s", jid->node, jid->domain); |
| 7261 | 90 conv = find_unnormalized_conv(from, jm->js->gc->account); |
| 7258 | 91 if(conv) |
| 92 gaim_conversation_set_name(conv, jm->from); | |
| 93 g_free(from); | |
| 94 from = g_strdup(jm->from); | |
| 95 } else { | |
| 7014 | 96 from = g_strdup(jid->domain); |
| 7258 | 97 } |
| 7014 | 98 |
| 99 if(!jm->xhtml && !jm->body) { | |
| 100 if(jm->events & JABBER_MESSAGE_EVENT_COMPOSING) | |
| 101 serv_got_typing(jm->js->gc, from, 0, GAIM_TYPING); | |
| 102 else | |
| 103 serv_got_typing_stopped(jm->js->gc, from); | |
| 104 } else { | |
| 105 if(jbr && jm->events & JABBER_MESSAGE_EVENT_COMPOSING) | |
| 106 jbr->capabilities |= JABBER_CAP_COMPOSING; | |
| 107 serv_got_im(jm->js->gc, from, jm->xhtml ? jm->xhtml : jm->body, 0, | |
| 108 jm->sent); | |
| 109 } | |
| 110 | |
| 111 g_free(from); | |
| 112 jabber_id_free(jid); | |
| 113 } | |
| 114 | |
| 7145 | 115 static void handle_headline(JabberMessage *jm) |
| 116 { | |
| 117 char *title; | |
| 118 GString *body = g_string_new(""); | |
| 119 GList *etc; | |
| 120 | |
| 121 title = g_strdup_printf(_("Message from %s"), jm->from); | |
| 122 | |
| 123 if(jm->xhtml) | |
| 124 g_string_append(body, jm->xhtml); | |
| 125 else if(jm->body) | |
| 126 g_string_append(body, jm->body); | |
| 127 | |
| 128 for(etc = jm->etc; etc; etc = etc->next) { | |
| 129 xmlnode *x = etc->data; | |
| 130 const char *xmlns = xmlnode_get_attrib(x, "xmlns"); | |
| 131 if(xmlns && !strcmp(xmlns, "jabber:x:oob")) { | |
| 132 xmlnode *url, *desc; | |
| 133 char *urltxt, *desctxt; | |
| 134 | |
| 135 url = xmlnode_get_child(x, "url"); | |
| 136 desc = xmlnode_get_child(x, "desc"); | |
| 137 | |
| 138 if(!url || !desc) | |
| 139 continue; | |
| 140 | |
| 141 urltxt = xmlnode_get_data(url); | |
| 142 desctxt = xmlnode_get_data(desc); | |
| 143 | |
| 144 /* I'm all about ugly hacks */ | |
| 145 if(body->len && !strcmp(body->str, jm->body)) | |
| 146 g_string_printf(body, "<a href='%s'>%s</a>", | |
| 147 urltxt, desctxt); | |
| 148 else | |
| 149 g_string_append_printf(body, "<br/><a href='%s'>%s</a>", | |
| 150 urltxt, desctxt); | |
| 151 | |
| 152 g_free(urltxt); | |
| 153 g_free(desctxt); | |
| 154 } | |
| 155 } | |
| 156 | |
| 157 gaim_notify_formatted(jm->js->gc, title, jm->subject ? jm->subject : title, | |
| 158 NULL, body->str, NULL, NULL); | |
| 159 | |
| 160 g_free(title); | |
| 161 g_string_free(body, TRUE); | |
| 162 } | |
| 163 | |
| 164 static void handle_groupchat(JabberMessage *jm) | |
| 7014 | 165 { |
| 166 JabberID *jid = jabber_id_new(jm->from); | |
| 167 JabberChat *chat = jabber_chat_find(jm->js, jid->node, jid->domain); | |
| 168 | |
| 169 if(!chat) | |
| 170 return; | |
| 171 | |
| 172 if(jm->subject) | |
| 7183 | 173 gaim_conv_chat_set_topic(GAIM_CONV_CHAT(chat->conv), jid->resource, |
| 174 jm->subject); | |
| 7014 | 175 |
| 7183 | 176 if(jm->xhtml || jm->body) |
| 177 serv_got_chat_in(jm->js->gc, chat->id, jabber_get_resource(jm->from), | |
| 178 0, jm->xhtml ? jm->xhtml : jm->body, jm->sent); | |
| 7014 | 179 jabber_id_free(jid); |
| 180 } | |
| 181 | |
| 7145 | 182 static void handle_groupchat_invite(JabberMessage *jm) |
| 7014 | 183 { |
| 184 GHashTable *components = g_hash_table_new_full(g_str_hash, g_str_equal, | |
| 185 g_free, g_free); | |
| 186 JabberID *jid = jabber_id_new(jm->to); | |
| 187 | |
| 188 g_hash_table_replace(components, g_strdup("room"), jid->node); | |
| 189 g_hash_table_replace(components, g_strdup("server"), jid->node); | |
| 190 g_hash_table_replace(components, g_strdup("handle"), jm->js->user->node); | |
| 191 g_hash_table_replace(components, g_strdup("password"), jm->password); | |
| 192 | |
| 193 jabber_id_free(jid); | |
| 194 serv_got_chat_invite(jm->js->gc, jm->to, jm->from, jm->body, components); | |
| 195 } | |
| 196 | |
| 7145 | 197 static void handle_error(JabberMessage *jm) |
| 7014 | 198 { |
| 199 char *buf; | |
| 200 | |
| 201 if(!jm->body) | |
| 202 return; | |
| 203 | |
| 204 buf = g_strdup_printf(_("Message delivery to %s failed: %s"), | |
| 205 jm->from, jm->error); | |
| 206 | |
| 207 gaim_notify_error(jm->js->gc, _("Jabber Message Error"), buf, jm->body); | |
| 208 | |
| 209 g_free(buf); | |
| 210 } | |
| 211 | |
| 212 void jabber_message_parse(JabberStream *js, xmlnode *packet) | |
| 213 { | |
| 214 JabberMessage *jm; | |
| 215 const char *type; | |
| 216 xmlnode *child; | |
| 217 | |
| 218 if(strcmp(packet->name, "message")) | |
| 219 return; | |
| 220 | |
| 221 jm = g_new0(JabberMessage, 1); | |
| 222 jm->js = js; | |
| 223 jm->sent = time(NULL); | |
| 224 | |
| 225 type = xmlnode_get_attrib(packet, "type"); | |
| 226 | |
| 227 if(type) { | |
| 228 if(!strcmp(type, "normal")) | |
| 229 jm->type = JABBER_MESSAGE_NORMAL; | |
| 230 else if(!strcmp(type, "chat")) | |
| 231 jm->type = JABBER_MESSAGE_CHAT; | |
| 232 else if(!strcmp(type, "groupchat")) | |
| 233 jm->type = JABBER_MESSAGE_GROUPCHAT; | |
| 234 else if(!strcmp(type, "headline")) | |
| 235 jm->type = JABBER_MESSAGE_HEADLINE; | |
| 236 else if(!strcmp(type, "error")) | |
| 237 jm->type = JABBER_MESSAGE_ERROR; | |
| 238 else | |
| 239 jm->type = JABBER_MESSAGE_OTHER; | |
| 240 } else { | |
| 241 jm->type = JABBER_MESSAGE_NORMAL; | |
| 242 } | |
| 243 | |
| 244 jm->from = g_strdup(xmlnode_get_attrib(packet, "from")); | |
| 245 jm->to = g_strdup(xmlnode_get_attrib(packet, "to")); | |
| 246 | |
| 247 for(child = packet->child; child; child = child->next) { | |
| 248 if(child->type != NODE_TYPE_TAG) | |
| 249 continue; | |
| 250 | |
| 251 if(!strcmp(child->name, "subject")) { | |
| 252 if(!jm->subject) | |
| 253 jm->subject = xmlnode_get_data(child); | |
| 254 } else if(!strcmp(child->name, "body")) { | |
| 255 if(!jm->body) | |
| 256 jm->body = xmlnode_get_data(child); | |
| 7241 | 257 } else if(!strcmp(child->name, "html")) { |
| 258 if(!jm->xhtml) | |
| 7014 | 259 jm->xhtml = xmlnode_to_str(child); |
| 260 } else if(!strcmp(child->name, "error")) { | |
| 261 const char *code = xmlnode_get_attrib(child, "code"); | |
| 262 char *code_txt = NULL; | |
| 263 char *text = xmlnode_get_data(child); | |
| 264 | |
| 265 if(code) | |
| 266 code_txt = g_strdup_printf(_(" (Code %s)"), code); | |
| 267 | |
| 268 if(!jm->error) | |
| 269 jm->error = g_strdup_printf("%s%s", text ? text : "", | |
| 270 code_txt ? code_txt : ""); | |
| 271 | |
| 272 g_free(code_txt); | |
| 273 g_free(text); | |
| 274 } else if(!strcmp(child->name, "x")) { | |
| 275 const char *xmlns = xmlnode_get_attrib(child, "xmlns"); | |
| 276 if(xmlns && !strcmp(xmlns, "jabber:x:event")) { | |
| 277 if(xmlnode_get_child(child, "composing")) | |
| 278 jm->events |= JABBER_MESSAGE_EVENT_COMPOSING; | |
| 279 } else if(xmlns && !strcmp(xmlns, "jabber:x:delay")) { | |
| 280 const char *timestamp = xmlnode_get_attrib(child, "stamp"); | |
| 281 if(timestamp) | |
| 282 jm->sent = str_to_time(timestamp); | |
| 283 } else if(xmlns && !strcmp(xmlns, "jabber:x:conference") && | |
| 284 jm->type != JABBER_MESSAGE_GROUPCHAT_INVITE) { | |
| 285 const char *jid = xmlnode_get_attrib(child, "jid"); | |
| 286 if(jid) { | |
| 287 jm->type = JABBER_MESSAGE_GROUPCHAT_INVITE; | |
| 288 g_free(jm->to); | |
| 289 jm->to = g_strdup(jid); | |
| 290 } | |
| 291 } else if(xmlns && !strcmp(xmlns, | |
| 292 "http://jabber.org/protocol/muc#user")) { | |
| 293 xmlnode *invite = xmlnode_get_child(child, "invite"); | |
| 294 if(invite) { | |
| 295 xmlnode *reason, *password; | |
| 296 const char *jid = xmlnode_get_attrib(child, "from"); | |
| 297 g_free(jm->to); | |
| 298 jm->to = jm->from; | |
| 299 jm->from = g_strdup(jid); | |
| 300 if((reason = xmlnode_get_child(invite, "reason"))) { | |
| 301 g_free(jm->body); | |
| 302 jm->body = xmlnode_get_data(reason); | |
| 303 } | |
| 304 if((password = xmlnode_get_child(invite, "password"))) | |
| 305 jm->password = xmlnode_get_data(password); | |
| 306 | |
| 307 jm->type = JABBER_MESSAGE_GROUPCHAT_INVITE; | |
| 308 } | |
| 7145 | 309 } else { |
| 310 jm->etc = g_list_append(jm->etc, child); | |
| 7014 | 311 } |
| 312 } | |
| 313 } | |
| 314 | |
| 315 switch(jm->type) { | |
| 316 case JABBER_MESSAGE_NORMAL: | |
| 317 case JABBER_MESSAGE_CHAT: | |
| 7145 | 318 handle_chat(jm); |
| 319 break; | |
| 7014 | 320 case JABBER_MESSAGE_HEADLINE: |
| 7145 | 321 handle_headline(jm); |
| 7014 | 322 break; |
| 323 case JABBER_MESSAGE_GROUPCHAT: | |
| 324 handle_groupchat(jm); | |
| 325 break; | |
| 326 case JABBER_MESSAGE_GROUPCHAT_INVITE: | |
| 327 handle_groupchat_invite(jm); | |
| 328 break; | |
| 329 case JABBER_MESSAGE_ERROR: | |
| 330 handle_error(jm); | |
| 331 break; | |
| 332 case JABBER_MESSAGE_OTHER: | |
| 333 gaim_debug(GAIM_DEBUG_INFO, "jabber", | |
| 334 "Received message of unknown type: %s\n", type); | |
| 335 break; | |
| 336 } | |
| 337 jabber_message_free(jm); | |
| 338 } | |
| 339 | |
| 340 void jabber_message_send(JabberMessage *jm) | |
| 341 { | |
| 342 xmlnode *message, *child; | |
| 343 const char *type = NULL; | |
| 344 | |
| 345 message = xmlnode_new("message"); | |
| 346 | |
| 347 switch(jm->type) { | |
| 348 case JABBER_MESSAGE_NORMAL: | |
| 349 type = "normal"; | |
| 350 break; | |
| 351 case JABBER_MESSAGE_CHAT: | |
| 352 case JABBER_MESSAGE_GROUPCHAT_INVITE: | |
| 353 type = "chat"; | |
| 354 break; | |
| 355 case JABBER_MESSAGE_HEADLINE: | |
| 356 type = "headline"; | |
| 357 break; | |
| 358 case JABBER_MESSAGE_GROUPCHAT: | |
| 359 type = "groupchat"; | |
| 360 break; | |
| 361 case JABBER_MESSAGE_ERROR: | |
| 362 type = "error"; | |
| 363 break; | |
| 364 case JABBER_MESSAGE_OTHER: | |
| 365 type = NULL; | |
| 366 break; | |
| 367 } | |
| 368 | |
| 369 if(type) | |
| 370 xmlnode_set_attrib(message, "type", type); | |
| 371 | |
| 372 xmlnode_set_attrib(message, "to", jm->to); | |
| 373 | |
| 374 if(jm->events || (!jm->body && !jm->xhtml)) { | |
| 375 child = xmlnode_new_child(message, "x"); | |
| 376 xmlnode_set_attrib(child, "xmlns", "jabber:x:event"); | |
| 377 if(jm->events & JABBER_MESSAGE_EVENT_COMPOSING) | |
| 378 xmlnode_new_child(child, "composing"); | |
| 379 } | |
| 380 | |
| 381 if(jm->subject) { | |
| 382 child = xmlnode_new_child(message, "subject"); | |
| 383 xmlnode_insert_data(child, jm->subject, -1); | |
| 384 } | |
| 385 | |
| 386 if(jm->body) { | |
| 387 child = xmlnode_new_child(message, "body"); | |
| 388 xmlnode_insert_data(child, jm->body, -1); | |
| 389 } | |
| 390 | |
| 391 if(jm->xhtml) { | |
| 392 child = xmlnode_from_str(jm->xhtml, -1); | |
| 393 if(child) { | |
| 394 xmlnode_insert_child(message, child); | |
| 395 } else { | |
| 396 gaim_debug(GAIM_DEBUG_ERROR, "jabber", | |
| 397 "XHTML translation/validation failed, returning: %s\n", | |
| 398 jm->xhtml); | |
| 399 } | |
| 400 } | |
| 401 | |
| 402 jabber_send(jm->js, message); | |
| 403 | |
| 404 xmlnode_free(message); | |
| 405 } | |
| 406 | |
| 407 int jabber_message_send_im(GaimConnection *gc, const char *who, const char *msg, | |
|
7118
bf630f7dfdcd
[gaim-migrate @ 7685]
Christian Hammond <chipx86@chipx86.com>
parents:
7095
diff
changeset
|
408 GaimConvImFlags flags) |
| 7014 | 409 { |
| 410 JabberMessage *jm; | |
| 411 JabberBuddy *jb; | |
| 412 JabberBuddyResource *jbr; | |
| 413 char *buf; | |
| 7135 | 414 char *xhtml; |
| 7014 | 415 |
| 416 if(!who || !msg) | |
| 417 return 0; | |
| 418 | |
| 419 jb = jabber_buddy_find(gc->proto_data, who, TRUE); | |
| 420 jbr = jabber_buddy_find_resource(jb, jabber_get_resource(who)); | |
| 421 | |
| 422 jm = g_new0(JabberMessage, 1); | |
| 423 jm->js = gc->proto_data; | |
| 424 jm->type = JABBER_MESSAGE_CHAT; | |
| 425 jm->events = JABBER_MESSAGE_EVENT_COMPOSING; | |
| 426 jm->to = g_strdup(who); | |
| 427 | |
| 428 buf = g_strdup_printf("<html xmlns='http://jabber.org/protocol/xhtml-im'><body>%s</body></html>", msg); | |
| 429 | |
| 7135 | 430 gaim_markup_html_to_xhtml(buf, &xhtml, &jm->body); |
| 7014 | 431 g_free(buf); |
| 432 | |
| 433 if(!jbr || jbr->capabilities & JABBER_CAP_XHTML) | |
| 434 jm->xhtml = xhtml; | |
| 435 else | |
| 436 g_free(xhtml); | |
| 437 | |
| 438 jabber_message_send(jm); | |
| 439 jabber_message_free(jm); | |
| 440 return 1; | |
| 441 } | |
| 442 | |
| 443 int jabber_message_send_chat(GaimConnection *gc, int id, const char *message) | |
| 444 { | |
| 445 JabberChat *chat; | |
| 446 JabberMessage *jm; | |
| 447 JabberStream *js = gc->proto_data; | |
| 448 | |
| 449 if(!message) | |
| 450 return 0; | |
| 451 | |
| 452 chat = jabber_chat_find_by_id(js, id); | |
| 453 | |
| 454 jm = g_new0(JabberMessage, 1); | |
| 455 jm->js = gc->proto_data; | |
| 456 jm->type = JABBER_MESSAGE_CHAT; | |
| 457 jm->to = g_strdup_printf("%s@%s", chat->room, chat->server); | |
| 458 | |
| 7135 | 459 gaim_markup_html_to_xhtml(message, &jm->xhtml, &jm->body); |
| 7014 | 460 |
| 461 jabber_message_send(jm); | |
| 462 jabber_message_free(jm); | |
| 463 return 1; | |
| 464 } | |
| 465 | |
| 466 int jabber_send_typing(GaimConnection *gc, const char *who, int typing) | |
| 467 { | |
| 468 JabberMessage *jm; | |
| 469 JabberBuddy *jb; | |
| 470 JabberBuddyResource *jbr; | |
| 471 | |
| 472 jb = jabber_buddy_find(gc->proto_data, who, TRUE); | |
| 473 jbr = jabber_buddy_find_resource(jb, jabber_get_resource(who)); | |
| 474 | |
| 7187 | 475 if(!jbr || !(jbr->capabilities & JABBER_CAP_COMPOSING)) |
| 7014 | 476 return 0; |
| 477 | |
| 478 jm = g_new0(JabberMessage, 1); | |
| 479 jm->js = gc->proto_data; | |
| 480 jm->type = JABBER_MESSAGE_CHAT; | |
| 481 jm->to = g_strdup(who); | |
| 482 | |
| 483 if(typing == GAIM_TYPING) | |
| 484 jm->events = JABBER_MESSAGE_EVENT_COMPOSING; | |
| 485 | |
| 486 jabber_message_send(jm); | |
| 487 jabber_message_free(jm); | |
| 488 | |
| 489 return JABBER_TYPING_NOTIFY_INT; | |
| 490 } | |
| 491 |
