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