Mercurial > pidgin.yaz
annotate src/protocols/jabber/message.c @ 10286:ff5e0f059924
[gaim-migrate @ 11460]
i committed this to oldstatus earlier. i really do need to fix the cause at some point (someone trying to register an invalid jabberid)
committer: Tailor Script <tailor@pidgin.im>
author | Nathan Walp <nwalp@pidgin.im> |
---|---|
date | Wed, 01 Dec 2004 05:41:12 +0000 |
parents | 3f97624e7753 |
children | a3a540ed2518 |
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 | |
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 | |
7310 | 63 if(!jid) |
64 return; | |
65 | |
7014 | 66 jb = jabber_buddy_find(jm->js, jm->from, TRUE); |
7306 | 67 jbr = jabber_buddy_find_resource(jb, jid->resource); |
7014 | 68 |
8043 | 69 if(jabber_find_unnormalized_conv(jm->from, jm->js->gc->account)) { |
7014 | 70 from = g_strdup(jm->from); |
7258 | 71 } else if(jid->node) { |
72 GaimConversation *conv; | |
73 | |
7014 | 74 from = g_strdup_printf("%s@%s", jid->node, jid->domain); |
8043 | 75 conv = jabber_find_unnormalized_conv(from, jm->js->gc->account); |
7258 | 76 if(conv) |
77 gaim_conversation_set_name(conv, jm->from); | |
78 g_free(from); | |
79 from = g_strdup(jm->from); | |
80 } else { | |
7014 | 81 from = g_strdup(jid->domain); |
7258 | 82 } |
7014 | 83 |
84 if(!jm->xhtml && !jm->body) { | |
85 if(jm->events & JABBER_MESSAGE_EVENT_COMPOSING) | |
86 serv_got_typing(jm->js->gc, from, 0, GAIM_TYPING); | |
87 else | |
88 serv_got_typing_stopped(jm->js->gc, from); | |
89 } else { | |
8400 | 90 if(jbr) { |
91 if(jm->events & JABBER_MESSAGE_EVENT_COMPOSING) | |
92 jbr->capabilities |= JABBER_CAP_COMPOSING; | |
93 if(jbr->thread_id) | |
94 g_free(jbr->thread_id); | |
95 jbr->thread_id = g_strdup(jbr->thread_id); | |
96 } | |
7014 | 97 serv_got_im(jm->js->gc, from, jm->xhtml ? jm->xhtml : jm->body, 0, |
98 jm->sent); | |
99 } | |
100 | |
101 g_free(from); | |
102 jabber_id_free(jid); | |
103 } | |
104 | |
7145 | 105 static void handle_headline(JabberMessage *jm) |
106 { | |
107 char *title; | |
108 GString *body = g_string_new(""); | |
109 GList *etc; | |
110 | |
111 title = g_strdup_printf(_("Message from %s"), jm->from); | |
112 | |
113 if(jm->xhtml) | |
114 g_string_append(body, jm->xhtml); | |
115 else if(jm->body) | |
116 g_string_append(body, jm->body); | |
117 | |
118 for(etc = jm->etc; etc; etc = etc->next) { | |
119 xmlnode *x = etc->data; | |
120 const char *xmlns = xmlnode_get_attrib(x, "xmlns"); | |
121 if(xmlns && !strcmp(xmlns, "jabber:x:oob")) { | |
122 xmlnode *url, *desc; | |
123 char *urltxt, *desctxt; | |
124 | |
125 url = xmlnode_get_child(x, "url"); | |
126 desc = xmlnode_get_child(x, "desc"); | |
127 | |
128 if(!url || !desc) | |
129 continue; | |
130 | |
131 urltxt = xmlnode_get_data(url); | |
132 desctxt = xmlnode_get_data(desc); | |
133 | |
134 /* I'm all about ugly hacks */ | |
135 if(body->len && !strcmp(body->str, jm->body)) | |
136 g_string_printf(body, "<a href='%s'>%s</a>", | |
137 urltxt, desctxt); | |
138 else | |
139 g_string_append_printf(body, "<br/><a href='%s'>%s</a>", | |
140 urltxt, desctxt); | |
141 | |
142 g_free(urltxt); | |
143 g_free(desctxt); | |
144 } | |
145 } | |
146 | |
147 gaim_notify_formatted(jm->js->gc, title, jm->subject ? jm->subject : title, | |
148 NULL, body->str, NULL, NULL); | |
149 | |
150 g_free(title); | |
151 g_string_free(body, TRUE); | |
152 } | |
153 | |
154 static void handle_groupchat(JabberMessage *jm) | |
7014 | 155 { |
156 JabberID *jid = jabber_id_new(jm->from); | |
7310 | 157 JabberChat *chat; |
158 | |
159 if(!jid) | |
160 return; | |
161 | |
162 chat = jabber_chat_find(jm->js, jid->node, jid->domain); | |
7014 | 163 |
164 if(!chat) | |
165 return; | |
166 | |
7971 | 167 if(jm->subject) { |
7183 | 168 gaim_conv_chat_set_topic(GAIM_CONV_CHAT(chat->conv), jid->resource, |
169 jm->subject); | |
7971 | 170 if(!jm->xhtml && !jm->body) { |
9762 | 171 char *msg, *tmp, *tmp2; |
172 tmp = gaim_escape_html(jm->subject); | |
173 tmp2 = gaim_markup_linkify(tmp); | |
7971 | 174 if(jid->resource) |
9762 | 175 msg = g_strdup_printf(_("%s has set the topic to: %s"), jid->resource, tmp2); |
7971 | 176 else |
9762 | 177 msg = g_strdup_printf(_("The topic is: %s"), tmp2); |
7971 | 178 gaim_conv_chat_write(GAIM_CONV_CHAT(chat->conv), "", msg, GAIM_MESSAGE_SYSTEM, jm->sent); |
9762 | 179 g_free(tmp); |
180 g_free(tmp2); | |
7971 | 181 g_free(msg); |
182 } | |
183 } | |
7014 | 184 |
7630 | 185 if(jm->xhtml || jm->body) { |
186 if(jid->resource) | |
9584 | 187 serv_got_chat_in(jm->js->gc, chat->id, jid->resource, |
188 jm->delayed ? GAIM_CONV_CHAT_DELAYED : 0, | |
7630 | 189 jm->xhtml ? jm->xhtml : jm->body, jm->sent); |
190 else if(chat->muc) | |
191 gaim_conv_chat_write(GAIM_CONV_CHAT(chat->conv), "", | |
192 jm->xhtml ? jm->xhtml : jm->body, | |
193 GAIM_MESSAGE_SYSTEM, jm->sent); | |
194 } | |
195 | |
7014 | 196 jabber_id_free(jid); |
197 } | |
198 | |
7145 | 199 static void handle_groupchat_invite(JabberMessage *jm) |
7014 | 200 { |
7310 | 201 GHashTable *components; |
7014 | 202 JabberID *jid = jabber_id_new(jm->to); |
8537 | 203 char *stripped; |
7014 | 204 |
7310 | 205 if(!jid) |
206 return; | |
207 | |
208 components = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free); | |
209 | |
7332 | 210 g_hash_table_replace(components, g_strdup("room"), g_strdup(jid->node)); |
7448 | 211 g_hash_table_replace(components, g_strdup("server"), g_strdup(jid->domain)); |
7332 | 212 g_hash_table_replace(components, g_strdup("handle"), |
213 g_strdup(jm->js->user->node)); | |
214 g_hash_table_replace(components, g_strdup("password"), | |
215 g_strdup(jm->password)); | |
7014 | 216 |
217 jabber_id_free(jid); | |
8537 | 218 stripped = gaim_markup_strip_html(jm->body); |
219 serv_got_chat_invite(jm->js->gc, jm->to, jm->from, stripped, components); | |
220 g_free(stripped); | |
7014 | 221 } |
222 | |
7145 | 223 static void handle_error(JabberMessage *jm) |
7014 | 224 { |
225 char *buf; | |
226 | |
227 if(!jm->body) | |
228 return; | |
229 | |
230 buf = g_strdup_printf(_("Message delivery to %s failed: %s"), | |
231 jm->from, jm->error); | |
232 | |
7944 | 233 gaim_notify_formatted(jm->js->gc, _("Jabber Message Error"), _("Jabber Message Error"), buf, |
234 jm->xhtml ? jm->xhtml : jm->body, NULL, NULL); | |
7014 | 235 |
236 g_free(buf); | |
237 } | |
238 | |
239 void jabber_message_parse(JabberStream *js, xmlnode *packet) | |
240 { | |
241 JabberMessage *jm; | |
242 const char *type; | |
243 xmlnode *child; | |
244 | |
245 if(strcmp(packet->name, "message")) | |
246 return; | |
247 | |
248 jm = g_new0(JabberMessage, 1); | |
249 jm->js = js; | |
250 jm->sent = time(NULL); | |
9584 | 251 jm->delayed = FALSE; |
7014 | 252 |
253 type = xmlnode_get_attrib(packet, "type"); | |
254 | |
255 if(type) { | |
256 if(!strcmp(type, "normal")) | |
257 jm->type = JABBER_MESSAGE_NORMAL; | |
258 else if(!strcmp(type, "chat")) | |
259 jm->type = JABBER_MESSAGE_CHAT; | |
260 else if(!strcmp(type, "groupchat")) | |
261 jm->type = JABBER_MESSAGE_GROUPCHAT; | |
262 else if(!strcmp(type, "headline")) | |
263 jm->type = JABBER_MESSAGE_HEADLINE; | |
264 else if(!strcmp(type, "error")) | |
265 jm->type = JABBER_MESSAGE_ERROR; | |
266 else | |
267 jm->type = JABBER_MESSAGE_OTHER; | |
268 } else { | |
269 jm->type = JABBER_MESSAGE_NORMAL; | |
270 } | |
271 | |
272 jm->from = g_strdup(xmlnode_get_attrib(packet, "from")); | |
273 jm->to = g_strdup(xmlnode_get_attrib(packet, "to")); | |
274 | |
275 for(child = packet->child; child; child = child->next) { | |
8135 | 276 if(child->type != XMLNODE_TYPE_TAG) |
7014 | 277 continue; |
278 | |
279 if(!strcmp(child->name, "subject")) { | |
280 if(!jm->subject) | |
281 jm->subject = xmlnode_get_data(child); | |
8400 | 282 } else if(!strcmp(child->name, "thread")) { |
283 if(!jm->thread_id) | |
284 jm->thread_id = xmlnode_get_data(child); | |
7014 | 285 } else if(!strcmp(child->name, "body")) { |
286 if(!jm->body) | |
7894 | 287 jm->body = xmlnode_to_str(child, NULL); |
7241 | 288 } else if(!strcmp(child->name, "html")) { |
289 if(!jm->xhtml) | |
7642 | 290 jm->xhtml = xmlnode_to_str(child, NULL); |
7014 | 291 } else if(!strcmp(child->name, "error")) { |
292 const char *code = xmlnode_get_attrib(child, "code"); | |
293 char *code_txt = NULL; | |
294 char *text = xmlnode_get_data(child); | |
295 | |
296 if(code) | |
297 code_txt = g_strdup_printf(_(" (Code %s)"), code); | |
298 | |
299 if(!jm->error) | |
300 jm->error = g_strdup_printf("%s%s", text ? text : "", | |
301 code_txt ? code_txt : ""); | |
302 | |
303 g_free(code_txt); | |
304 g_free(text); | |
305 } else if(!strcmp(child->name, "x")) { | |
306 const char *xmlns = xmlnode_get_attrib(child, "xmlns"); | |
307 if(xmlns && !strcmp(xmlns, "jabber:x:event")) { | |
308 if(xmlnode_get_child(child, "composing")) | |
309 jm->events |= JABBER_MESSAGE_EVENT_COMPOSING; | |
310 } else if(xmlns && !strcmp(xmlns, "jabber:x:delay")) { | |
311 const char *timestamp = xmlnode_get_attrib(child, "stamp"); | |
9584 | 312 jm->delayed = TRUE; |
7014 | 313 if(timestamp) |
8577 | 314 jm->sent = gaim_str_to_time(timestamp, TRUE); |
7014 | 315 } else if(xmlns && !strcmp(xmlns, "jabber:x:conference") && |
316 jm->type != JABBER_MESSAGE_GROUPCHAT_INVITE) { | |
317 const char *jid = xmlnode_get_attrib(child, "jid"); | |
318 if(jid) { | |
319 jm->type = JABBER_MESSAGE_GROUPCHAT_INVITE; | |
320 g_free(jm->to); | |
321 jm->to = g_strdup(jid); | |
322 } | |
323 } else if(xmlns && !strcmp(xmlns, | |
324 "http://jabber.org/protocol/muc#user")) { | |
325 xmlnode *invite = xmlnode_get_child(child, "invite"); | |
326 if(invite) { | |
327 xmlnode *reason, *password; | |
7968 | 328 const char *jid = xmlnode_get_attrib(invite, "from"); |
7014 | 329 g_free(jm->to); |
330 jm->to = jm->from; | |
331 jm->from = g_strdup(jid); | |
332 if((reason = xmlnode_get_child(invite, "reason"))) { | |
333 g_free(jm->body); | |
334 jm->body = xmlnode_get_data(reason); | |
335 } | |
336 if((password = xmlnode_get_child(invite, "password"))) | |
337 jm->password = xmlnode_get_data(password); | |
338 | |
339 jm->type = JABBER_MESSAGE_GROUPCHAT_INVITE; | |
340 } | |
7145 | 341 } else { |
342 jm->etc = g_list_append(jm->etc, child); | |
7014 | 343 } |
344 } | |
345 } | |
346 | |
347 switch(jm->type) { | |
348 case JABBER_MESSAGE_NORMAL: | |
349 case JABBER_MESSAGE_CHAT: | |
7145 | 350 handle_chat(jm); |
351 break; | |
7014 | 352 case JABBER_MESSAGE_HEADLINE: |
7145 | 353 handle_headline(jm); |
7014 | 354 break; |
355 case JABBER_MESSAGE_GROUPCHAT: | |
356 handle_groupchat(jm); | |
357 break; | |
358 case JABBER_MESSAGE_GROUPCHAT_INVITE: | |
359 handle_groupchat_invite(jm); | |
360 break; | |
361 case JABBER_MESSAGE_ERROR: | |
362 handle_error(jm); | |
363 break; | |
364 case JABBER_MESSAGE_OTHER: | |
365 gaim_debug(GAIM_DEBUG_INFO, "jabber", | |
366 "Received message of unknown type: %s\n", type); | |
367 break; | |
368 } | |
369 jabber_message_free(jm); | |
370 } | |
371 | |
372 void jabber_message_send(JabberMessage *jm) | |
373 { | |
374 xmlnode *message, *child; | |
375 const char *type = NULL; | |
376 | |
377 message = xmlnode_new("message"); | |
378 | |
379 switch(jm->type) { | |
380 case JABBER_MESSAGE_NORMAL: | |
381 type = "normal"; | |
382 break; | |
383 case JABBER_MESSAGE_CHAT: | |
384 case JABBER_MESSAGE_GROUPCHAT_INVITE: | |
385 type = "chat"; | |
386 break; | |
387 case JABBER_MESSAGE_HEADLINE: | |
388 type = "headline"; | |
389 break; | |
390 case JABBER_MESSAGE_GROUPCHAT: | |
391 type = "groupchat"; | |
392 break; | |
393 case JABBER_MESSAGE_ERROR: | |
394 type = "error"; | |
395 break; | |
396 case JABBER_MESSAGE_OTHER: | |
397 type = NULL; | |
398 break; | |
399 } | |
400 | |
401 if(type) | |
402 xmlnode_set_attrib(message, "type", type); | |
403 | |
404 xmlnode_set_attrib(message, "to", jm->to); | |
405 | |
8400 | 406 if(jm->thread_id) { |
407 child = xmlnode_new_child(message, "thread"); | |
408 xmlnode_insert_data(child, jm->thread_id, -1); | |
409 } | |
410 | |
7971 | 411 if(jm->events || (!jm->body && !jm->xhtml && !jm->subject)) { |
7014 | 412 child = xmlnode_new_child(message, "x"); |
413 xmlnode_set_attrib(child, "xmlns", "jabber:x:event"); | |
414 if(jm->events & JABBER_MESSAGE_EVENT_COMPOSING) | |
415 xmlnode_new_child(child, "composing"); | |
416 } | |
417 | |
418 if(jm->subject) { | |
419 child = xmlnode_new_child(message, "subject"); | |
420 xmlnode_insert_data(child, jm->subject, -1); | |
421 } | |
422 | |
423 if(jm->body) { | |
424 child = xmlnode_new_child(message, "body"); | |
425 xmlnode_insert_data(child, jm->body, -1); | |
426 } | |
427 | |
428 if(jm->xhtml) { | |
429 child = xmlnode_from_str(jm->xhtml, -1); | |
430 if(child) { | |
431 xmlnode_insert_child(message, child); | |
432 } else { | |
433 gaim_debug(GAIM_DEBUG_ERROR, "jabber", | |
434 "XHTML translation/validation failed, returning: %s\n", | |
435 jm->xhtml); | |
436 } | |
437 } | |
438 | |
439 jabber_send(jm->js, message); | |
440 | |
441 xmlnode_free(message); | |
442 } | |
443 | |
444 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
|
445 GaimConvImFlags flags) |
7014 | 446 { |
447 JabberMessage *jm; | |
448 JabberBuddy *jb; | |
449 JabberBuddyResource *jbr; | |
450 char *buf; | |
7135 | 451 char *xhtml; |
7306 | 452 char *resource; |
7014 | 453 |
454 if(!who || !msg) | |
455 return 0; | |
456 | |
7306 | 457 resource = jabber_get_resource(who); |
458 | |
7014 | 459 jb = jabber_buddy_find(gc->proto_data, who, TRUE); |
7306 | 460 jbr = jabber_buddy_find_resource(jb, resource); |
461 | |
462 g_free(resource); | |
7014 | 463 |
464 jm = g_new0(JabberMessage, 1); | |
465 jm->js = gc->proto_data; | |
466 jm->type = JABBER_MESSAGE_CHAT; | |
467 jm->events = JABBER_MESSAGE_EVENT_COMPOSING; | |
468 jm->to = g_strdup(who); | |
8400 | 469 if(jbr && jbr->thread_id) |
470 jm->thread_id = jbr->thread_id; | |
7014 | 471 |
7773 | 472 buf = g_strdup_printf("<html xmlns='http://jabber.org/protocol/xhtml-im'><body xmlns='http://www.w3.org/1999/xhtml'>%s</body></html>", msg); |
7014 | 473 |
7135 | 474 gaim_markup_html_to_xhtml(buf, &xhtml, &jm->body); |
7014 | 475 g_free(buf); |
476 | |
477 if(!jbr || jbr->capabilities & JABBER_CAP_XHTML) | |
478 jm->xhtml = xhtml; | |
479 else | |
480 g_free(xhtml); | |
481 | |
482 jabber_message_send(jm); | |
483 jabber_message_free(jm); | |
484 return 1; | |
485 } | |
486 | |
7345 | 487 int jabber_message_send_chat(GaimConnection *gc, int id, const char *msg) |
7014 | 488 { |
489 JabberChat *chat; | |
490 JabberMessage *jm; | |
8042 | 491 JabberStream *js; |
9130 | 492 char *buf; |
7014 | 493 |
8042 | 494 if(!msg || !gc) |
7014 | 495 return 0; |
496 | |
8042 | 497 js = gc->proto_data; |
7014 | 498 chat = jabber_chat_find_by_id(js, id); |
499 | |
8043 | 500 if(!chat) |
501 return 0; | |
502 | |
9130 | 503 jm = g_new0(JabberMessage, 1); |
504 jm->js = gc->proto_data; | |
505 jm->type = JABBER_MESSAGE_GROUPCHAT; | |
506 jm->to = g_strdup_printf("%s@%s", chat->room, chat->server); | |
507 | |
8858 | 508 buf = g_strdup_printf("<html xmlns='http://jabber.org/protocol/xhtml-im'><body xmlns='http://www.w3.org/1999/xhtml'>%s</body></html>", msg); |
9130 | 509 gaim_markup_html_to_xhtml(buf, &jm->xhtml, &jm->body); |
8858 | 510 g_free(buf); |
511 | |
9130 | 512 if(!chat->xhtml) { |
513 g_free(jm->xhtml); | |
514 jm->xhtml = NULL; | |
7923 | 515 } |
516 | |
9130 | 517 jabber_message_send(jm); |
518 jabber_message_free(jm); | |
519 | |
7014 | 520 return 1; |
521 } | |
522 | |
523 int jabber_send_typing(GaimConnection *gc, const char *who, int typing) | |
524 { | |
525 JabberMessage *jm; | |
526 JabberBuddy *jb; | |
527 JabberBuddyResource *jbr; | |
7306 | 528 char *resource = jabber_get_resource(who); |
7014 | 529 |
530 jb = jabber_buddy_find(gc->proto_data, who, TRUE); | |
7306 | 531 jbr = jabber_buddy_find_resource(jb, resource); |
532 | |
533 g_free(resource); | |
7014 | 534 |
7187 | 535 if(!jbr || !(jbr->capabilities & JABBER_CAP_COMPOSING)) |
7014 | 536 return 0; |
537 | |
538 jm = g_new0(JabberMessage, 1); | |
539 jm->js = gc->proto_data; | |
540 jm->type = JABBER_MESSAGE_CHAT; | |
541 jm->to = g_strdup(who); | |
542 | |
543 if(typing == GAIM_TYPING) | |
544 jm->events = JABBER_MESSAGE_EVENT_COMPOSING; | |
545 | |
546 jabber_message_send(jm); | |
547 jabber_message_free(jm); | |
548 | |
549 return JABBER_TYPING_NOTIFY_INT; | |
550 } | |
551 |