comparison libgaim/protocols/jabber/presence.c @ 14192:60b1bc8dbf37

[gaim-migrate @ 16863] Renamed 'core' to 'libgaim' committer: Tailor Script <tailor@pidgin.im>
author Evan Schoenberg <evan.s@dreskin.net>
date Sat, 19 Aug 2006 01:50:10 +0000
parents
children 7df4ab213577
comparison
equal deleted inserted replaced
14191:009db0b357b5 14192:60b1bc8dbf37
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 "cipher.h"
24 #include "debug.h"
25 #include "notify.h"
26 #include "request.h"
27 #include "server.h"
28 #include "status.h"
29 #include "util.h"
30
31 #include "buddy.h"
32 #include "chat.h"
33 #include "presence.h"
34 #include "iq.h"
35 #include "jutil.h"
36 #include "xmlnode.h"
37
38
39 static void chats_send_presence_foreach(gpointer key, gpointer val,
40 gpointer user_data)
41 {
42 JabberChat *chat = val;
43 xmlnode *presence = user_data;
44 char *chat_full_jid;
45
46 if(!chat->conv)
47 return;
48
49 chat_full_jid = g_strdup_printf("%s@%s/%s", chat->room, chat->server,
50 chat->handle);
51
52 xmlnode_set_attrib(presence, "to", chat_full_jid);
53 jabber_send(chat->js, presence);
54 g_free(chat_full_jid);
55 }
56
57 void jabber_presence_fake_to_self(JabberStream *js, const GaimStatus *gstatus) {
58 char *my_base_jid;
59
60 if(!js->user)
61 return;
62
63 my_base_jid = g_strdup_printf("%s@%s", js->user->node, js->user->domain);
64 if(gaim_find_buddy(js->gc->account, my_base_jid)) {
65 JabberBuddy *jb;
66 JabberBuddyResource *jbr;
67 if((jb = jabber_buddy_find(js, my_base_jid, TRUE))) {
68 JabberBuddyState state;
69 const char *msg;
70 int priority;
71
72 gaim_status_to_jabber(gstatus, &state, &msg, &priority);
73
74 if (state == JABBER_BUDDY_STATE_UNAVAILABLE || state == JABBER_BUDDY_STATE_UNKNOWN) {
75 jabber_buddy_remove_resource(jb, js->user->resource);
76 } else {
77 jabber_buddy_track_resource(jb, js->user->resource, priority, state, msg);
78 }
79 if((jbr = jabber_buddy_find_resource(jb, NULL))) {
80 gaim_prpl_got_user_status(js->gc->account, my_base_jid, jabber_buddy_state_get_status_id(jbr->state), "priority", jbr->priority, jbr->status ? "message" : NULL, jbr->status, NULL);
81 } else {
82 gaim_prpl_got_user_status(js->gc->account, my_base_jid, "offline", msg ? "message" : NULL, msg, NULL);
83 }
84 }
85 }
86 g_free(my_base_jid);
87 }
88
89
90 void jabber_presence_send(GaimAccount *account, GaimStatus *status)
91 {
92 GaimConnection *gc = NULL;
93 JabberStream *js = NULL;
94 gboolean disconnected;
95 int primitive;
96 xmlnode *presence, *x, *photo;
97 char *stripped = NULL;
98 const char *msg;
99 JabberBuddyState state;
100 int priority;
101
102 if(!gaim_status_is_active(status))
103 return;
104
105 disconnected = gaim_account_is_disconnected(account);
106 primitive = gaim_status_type_get_primitive(gaim_status_get_type(status));
107
108 if(disconnected)
109 return;
110
111 gc = gaim_account_get_connection(account);
112 js = gc->proto_data;
113
114 gaim_status_to_jabber(status, &state, &msg, &priority);
115
116 if(msg)
117 gaim_markup_html_to_xhtml(msg, NULL, &stripped);
118
119 presence = jabber_presence_create(state, stripped, priority);
120 g_free(stripped);
121
122 if(js->avatar_hash) {
123 x = xmlnode_new_child(presence, "x");
124 xmlnode_set_namespace(x, "vcard-temp:x:update");
125 photo = xmlnode_new_child(x, "photo");
126 xmlnode_insert_data(photo, js->avatar_hash, -1);
127 }
128
129 jabber_send(js, presence);
130
131 g_hash_table_foreach(js->chats, chats_send_presence_foreach, presence);
132 xmlnode_free(presence);
133
134 jabber_presence_fake_to_self(js, status);
135 }
136
137 xmlnode *jabber_presence_create(JabberBuddyState state, const char *msg, int priority)
138 {
139 xmlnode *show, *status, *presence, *pri, *c;
140 const char *show_string = NULL;
141
142 presence = xmlnode_new("presence");
143
144 if(state == JABBER_BUDDY_STATE_UNAVAILABLE)
145 xmlnode_set_attrib(presence, "type", "unavailable");
146 else if(state != JABBER_BUDDY_STATE_ONLINE &&
147 state != JABBER_BUDDY_STATE_UNKNOWN &&
148 state != JABBER_BUDDY_STATE_ERROR)
149 show_string = jabber_buddy_state_get_show(state);
150
151 if(show_string) {
152 show = xmlnode_new_child(presence, "show");
153 xmlnode_insert_data(show, show_string, -1);
154 }
155
156 if(msg) {
157 status = xmlnode_new_child(presence, "status");
158 xmlnode_insert_data(status, msg, -1);
159 }
160
161 if(priority) {
162 char *pstr = g_strdup_printf("%d", priority);
163 pri = xmlnode_new_child(presence, "priority");
164 xmlnode_insert_data(pri, pstr, -1);
165 g_free(pstr);
166 }
167
168 /* JEP-0115 */
169 c = xmlnode_new_child(presence, "c");
170 xmlnode_set_namespace(c, "http://jabber.org/protocol/caps");
171 xmlnode_set_attrib(c, "node", CAPS0115_NODE);
172 xmlnode_set_attrib(c, "ver", VERSION);
173
174 return presence;
175 }
176
177 struct _jabber_add_permit {
178 GaimConnection *gc;
179 JabberStream *js;
180 char *who;
181 };
182
183 static void authorize_add_cb(struct _jabber_add_permit *jap)
184 {
185 GaimBuddy *buddy = NULL;
186
187 jabber_presence_subscription_set(jap->gc->proto_data, jap->who,
188 "subscribed");
189
190 buddy = gaim_find_buddy(jap->gc->account, jap->who);
191
192 if (buddy) {
193 JabberBuddy *jb = NULL;
194
195 jb = jabber_buddy_find(jap->js, jap->who, TRUE);
196
197 if ((jb->subscription & JABBER_SUB_TO) == 0) {
198 gaim_account_request_add(jap->gc->account,
199 jap->who, NULL,
200 NULL, NULL);
201 } else {
202 gaim_account_notify_added(jap->gc->account,
203 jap->who, NULL,
204 NULL, NULL);
205 }
206 } else {
207 gaim_account_request_add(jap->gc->account, jap->who,
208 NULL, NULL, NULL);
209 }
210
211 g_free(jap->who);
212 g_free(jap);
213 }
214
215 static void deny_add_cb(struct _jabber_add_permit *jap)
216 {
217 jabber_presence_subscription_set(jap->gc->proto_data, jap->who,
218 "unsubscribed");
219
220 g_free(jap->who);
221 g_free(jap);
222 }
223
224 static void jabber_vcard_parse_avatar(JabberStream *js, xmlnode *packet, gpointer blah)
225 {
226 JabberBuddy *jb = NULL;
227 GaimBuddy *b = NULL;
228 xmlnode *vcard, *photo, *binval;
229 char *text;
230 guchar *data;
231 gsize size;
232 const char *from = xmlnode_get_attrib(packet, "from");
233
234 if(!from)
235 return;
236
237 jb = jabber_buddy_find(js, from, TRUE);
238
239 js->pending_avatar_requests = g_slist_remove(js->pending_avatar_requests, jb);
240
241 if((vcard = xmlnode_get_child(packet, "vCard")) ||
242 (vcard = xmlnode_get_child_with_namespace(packet, "query", "vcard-temp"))) {
243 if((photo = xmlnode_get_child(vcard, "PHOTO")) &&
244 (( (binval = xmlnode_get_child(photo, "BINVAL")) &&
245 (text = xmlnode_get_data(binval))) ||
246 (text = xmlnode_get_data(photo)))) {
247 data = gaim_base64_decode(text, &size);
248
249 gaim_buddy_icons_set_for_user(js->gc->account, from, data, size);
250 if((b = gaim_find_buddy(js->gc->account, from))) {
251 unsigned char hashval[20];
252 char hash[41], *p;
253 int i;
254
255 gaim_cipher_digest_region("sha1", data, size,
256 sizeof(hashval), hashval, NULL);
257 p = hash;
258 for(i=0; i<20; i++, p+=2)
259 snprintf(p, 3, "%02x", hashval[i]);
260 gaim_blist_node_set_string((GaimBlistNode*)b, "avatar_hash", hash);
261 }
262 g_free(data);
263 g_free(text);
264 }
265 }
266 }
267
268 void jabber_presence_parse(JabberStream *js, xmlnode *packet)
269 {
270 const char *from = xmlnode_get_attrib(packet, "from");
271 const char *type = xmlnode_get_attrib(packet, "type");
272 const char *real_jid = NULL;
273 const char *affiliation = NULL;
274 const char *role = NULL;
275 char *status = NULL;
276 int priority = 0;
277 JabberID *jid;
278 JabberChat *chat;
279 JabberBuddy *jb;
280 JabberBuddyResource *jbr = NULL, *found_jbr = NULL;
281 GaimConvChatBuddyFlags flags = GAIM_CBFLAGS_NONE;
282 gboolean delayed = FALSE;
283 GaimBuddy *b = NULL;
284 char *buddy_name;
285 JabberBuddyState state = JABBER_BUDDY_STATE_UNKNOWN;
286 xmlnode *y;
287 gboolean muc = FALSE;
288 char *avatar_hash = NULL;
289
290 if(!(jb = jabber_buddy_find(js, from, TRUE)))
291 return;
292
293 if(!(jid = jabber_id_new(from)))
294 return;
295
296 if(jb->error_msg) {
297 g_free(jb->error_msg);
298 jb->error_msg = NULL;
299 }
300
301 if(type && !strcmp(type, "error")) {
302 char *msg = jabber_parse_error(js, packet);
303
304 state = JABBER_BUDDY_STATE_ERROR;
305 jb->error_msg = msg ? msg : g_strdup(_("Unknown Error in presence"));
306 } else if(type && !strcmp(type, "subscribe")) {
307 struct _jabber_add_permit *jap = g_new0(struct _jabber_add_permit, 1);
308 char *msg;
309
310 msg = g_strdup_printf(_("The user %s wants to add %s to his or "
311 "her buddy list."),
312 from, gaim_account_get_username(js->gc->account));
313 jap->gc = js->gc;
314 jap->who = g_strdup(from);
315 jap->js = js;
316
317 gaim_request_action(js->gc, NULL, msg, NULL, GAIM_DEFAULT_ACTION_NONE,
318 jap, 2,
319 _("_Authorize"), G_CALLBACK(authorize_add_cb),
320 _("_Deny"), G_CALLBACK(deny_add_cb));
321 g_free(msg);
322 jabber_id_free(jid);
323 return;
324 } else if(type && !strcmp(type, "subscribed")) {
325 /* we've been allowed to see their presence, but we don't care */
326 jabber_id_free(jid);
327 return;
328 } else if(type && !strcmp(type, "unsubscribe")) {
329 /* XXX I'm not sure this is the right way to handle this, it
330 * might be better to add "unsubscribe" to the presence status
331 * if lower down, but I'm not sure. */
332 /* they are unsubscribing from our presence, we don't care */
333 /* Well, maybe just a little, we might want/need to start
334 * acknowledging this (and the others) at some point. */
335 jabber_id_free(jid);
336 return;
337 } else {
338 if((y = xmlnode_get_child(packet, "show"))) {
339 char *show = xmlnode_get_data(y);
340 state = jabber_buddy_show_get_state(show);
341 g_free(show);
342 } else {
343 state = JABBER_BUDDY_STATE_ONLINE;
344 }
345 }
346
347
348 for(y = packet->child; y; y = y->next) {
349 if(y->type != XMLNODE_TYPE_TAG)
350 continue;
351
352 if(!strcmp(y->name, "status")) {
353 g_free(status);
354 status = xmlnode_get_data(y);
355 } else if(!strcmp(y->name, "priority")) {
356 char *p = xmlnode_get_data(y);
357 if(p) {
358 priority = atoi(p);
359 g_free(p);
360 }
361 } else if(!strcmp(y->name, "x")) {
362 const char *xmlns = xmlnode_get_namespace(y);
363 if(xmlns && !strcmp(xmlns, "jabber:x:delay")) {
364 /* XXX: compare the time. jabber:x:delay can happen on presence packets that aren't really and truly delayed */
365 delayed = TRUE;
366 } else if(xmlns && !strcmp(xmlns, "http://jabber.org/protocol/muc#user")) {
367 xmlnode *z;
368
369 muc = TRUE;
370 if((z = xmlnode_get_child(y, "status"))) {
371 const char *code = xmlnode_get_attrib(z, "code");
372 if(code && !strcmp(code, "201")) {
373 if((chat = jabber_chat_find(js, jid->node, jid->domain))) {
374 chat->config_dialog_type = GAIM_REQUEST_ACTION;
375 chat->config_dialog_handle =
376 gaim_request_action(js->gc,
377 _("Create New Room"),
378 _("Create New Room"),
379 _("You are creating a new room. Would"
380 " you like to configure it, or"
381 " accept the default settings?"),
382 1, chat, 2, _("_Configure Room"),
383 G_CALLBACK(jabber_chat_request_room_configure),
384 _("_Accept Defaults"),
385 G_CALLBACK(jabber_chat_create_instant_room));
386 }
387 }
388 }
389 if((z = xmlnode_get_child(y, "item"))) {
390 real_jid = xmlnode_get_attrib(z, "jid");
391 affiliation = xmlnode_get_attrib(z, "affiliation");
392 role = xmlnode_get_attrib(z, "role");
393 if(affiliation != NULL && !strcmp(affiliation, "owner"))
394 flags |= GAIM_CBFLAGS_FOUNDER;
395 if (role != NULL) {
396 if (!strcmp(role, "moderator"))
397 flags |= GAIM_CBFLAGS_OP;
398 else if (!strcmp(role, "participant"))
399 flags |= GAIM_CBFLAGS_VOICE;
400 }
401 }
402 } else if(xmlns && !strcmp(xmlns, "vcard-temp:x:update")) {
403 xmlnode *photo = xmlnode_get_child(y, "photo");
404 if(photo) {
405 if(avatar_hash)
406 g_free(avatar_hash);
407 avatar_hash = xmlnode_get_data(photo);
408 }
409 }
410 }
411 }
412
413
414 if(jid->node && (chat = jabber_chat_find(js, jid->node, jid->domain))) {
415 static int i = 1;
416 char *room_jid = g_strdup_printf("%s@%s", jid->node, jid->domain);
417
418 if(state == JABBER_BUDDY_STATE_ERROR) {
419 char *title, *msg = jabber_parse_error(js, packet);
420
421 if(chat->conv) {
422 title = g_strdup_printf(_("Error in chat %s"), from);
423 serv_got_chat_left(js->gc, chat->id);
424 } else {
425 title = g_strdup_printf(_("Error joining chat %s"), from);
426 }
427 gaim_notify_error(js->gc, title, title, msg);
428 g_free(title);
429 g_free(msg);
430
431 jabber_chat_destroy(chat);
432 jabber_id_free(jid);
433 g_free(status);
434 g_free(room_jid);
435 if(avatar_hash)
436 g_free(avatar_hash);
437 return;
438 }
439
440
441 if(type && !strcmp(type, "unavailable")) {
442 gboolean nick_change = FALSE;
443
444 /* If we haven't joined the chat yet, we don't care that someone
445 * left, or it was us leaving after we closed the chat */
446 if(!chat->conv) {
447 if(jid->resource && chat->handle && !strcmp(jid->resource, chat->handle))
448 jabber_chat_destroy(chat);
449 jabber_id_free(jid);
450 g_free(status);
451 g_free(room_jid);
452 if(avatar_hash)
453 g_free(avatar_hash);
454 return;
455 }
456
457 jabber_buddy_remove_resource(jb, jid->resource);
458 if(chat->muc) {
459 xmlnode *x;
460 for(x = xmlnode_get_child(packet, "x"); x; x = xmlnode_get_next_twin(x)) {
461 const char *xmlns, *nick, *code;
462 xmlnode *stat, *item;
463 if(!(xmlns = xmlnode_get_namespace(x)) ||
464 strcmp(xmlns, "http://jabber.org/protocol/muc#user"))
465 continue;
466 if(!(stat = xmlnode_get_child(x, "status")))
467 continue;
468 if(!(code = xmlnode_get_attrib(stat, "code")))
469 continue;
470 if(!strcmp(code, "301")) {
471 /* XXX: we got banned */
472 } else if(!strcmp(code, "303")) {
473 if(!(item = xmlnode_get_child(x, "item")))
474 continue;
475 if(!(nick = xmlnode_get_attrib(item, "nick")))
476 continue;
477 nick_change = TRUE;
478 if(!strcmp(jid->resource, chat->handle)) {
479 g_free(chat->handle);
480 chat->handle = g_strdup(nick);
481 }
482 gaim_conv_chat_rename_user(GAIM_CONV_CHAT(chat->conv), jid->resource, nick);
483 jabber_chat_remove_handle(chat, jid->resource);
484 break;
485 } else if(!strcmp(code, "307")) {
486 /* XXX: we got kicked */
487 } else if(!strcmp(code, "321")) {
488 /* XXX: removed due to an affiliation change */
489 } else if(!strcmp(code, "322")) {
490 /* XXX: removed because room is now members-only */
491 } else if(!strcmp(code, "332")) {
492 /* XXX: removed due to system shutdown */
493 }
494 }
495 }
496 if(!nick_change) {
497 if(!g_utf8_collate(jid->resource, chat->handle)) {
498 serv_got_chat_left(js->gc, chat->id);
499 jabber_chat_destroy(chat);
500 } else {
501 gaim_conv_chat_remove_user(GAIM_CONV_CHAT(chat->conv), jid->resource,
502 status);
503 jabber_chat_remove_handle(chat, jid->resource);
504 }
505 }
506 } else {
507 if(!chat->conv) {
508 chat->id = i++;
509 chat->muc = muc;
510 chat->conv = serv_got_joined_chat(js->gc, chat->id, room_jid);
511 gaim_conv_chat_set_nick(GAIM_CONV_CHAT(chat->conv), chat->handle);
512
513 jabber_chat_disco_traffic(chat);
514 }
515
516 jabber_buddy_track_resource(jb, jid->resource, priority, state,
517 status);
518
519 jabber_chat_track_handle(chat, jid->resource, real_jid, affiliation, role);
520
521 if(!jabber_chat_find_buddy(chat->conv, jid->resource))
522 gaim_conv_chat_add_user(GAIM_CONV_CHAT(chat->conv), jid->resource,
523 real_jid, flags, !delayed);
524 else
525 gaim_conv_chat_user_set_flags(GAIM_CONV_CHAT(chat->conv), jid->resource,
526 flags);
527 }
528 g_free(room_jid);
529 } else {
530 buddy_name = g_strdup_printf("%s%s%s", jid->node ? jid->node : "",
531 jid->node ? "@" : "", jid->domain);
532 if((b = gaim_find_buddy(js->gc->account, buddy_name)) == NULL) {
533 jabber_id_free(jid);
534 if(avatar_hash)
535 g_free(avatar_hash);
536 g_free(buddy_name);
537 g_free(status);
538 return;
539 }
540
541 if(avatar_hash) {
542 const char *avatar_hash2 = gaim_blist_node_get_string((GaimBlistNode*)b, "avatar_hash");
543 if(!avatar_hash2 || strcmp(avatar_hash, avatar_hash2)) {
544 JabberIq *iq;
545 xmlnode *vcard;
546
547 /* XXX this is a crappy way of trying to prevent
548 * someone from spamming us with presence packets
549 * and causing us to DoS ourselves...what we really
550 * need is a queue system that can throttle itself,
551 * but i'm too tired to write that right now */
552 if(!g_slist_find(js->pending_avatar_requests, jb)) {
553
554 js->pending_avatar_requests = g_slist_prepend(js->pending_avatar_requests, jb);
555
556 iq = jabber_iq_new(js, JABBER_IQ_GET);
557 xmlnode_set_attrib(iq->node, "to", buddy_name);
558 vcard = xmlnode_new_child(iq->node, "vCard");
559 xmlnode_set_namespace(vcard, "vcard-temp");
560
561 jabber_iq_set_callback(iq, jabber_vcard_parse_avatar, NULL);
562 jabber_iq_send(iq);
563 }
564 }
565 }
566
567 if(state == JABBER_BUDDY_STATE_ERROR ||
568 (type && (!strcmp(type, "unavailable") ||
569 !strcmp(type, "unsubscribed")))) {
570 GaimConversation *conv;
571
572 jabber_buddy_remove_resource(jb, jid->resource);
573 if((conv = jabber_find_unnormalized_conv(from, js->gc->account)))
574 gaim_conversation_set_name(conv, buddy_name);
575
576 } else {
577 jbr = jabber_buddy_track_resource(jb, jid->resource, priority,
578 state, status);
579 }
580
581 if((found_jbr = jabber_buddy_find_resource(jb, NULL))) {
582 if(!jbr || jbr == found_jbr) {
583 gaim_prpl_got_user_status(js->gc->account, buddy_name, jabber_buddy_state_get_status_id(state), "priority", found_jbr->priority, found_jbr->status ? "message" : NULL, found_jbr->status, NULL);
584 }
585 } else {
586 gaim_prpl_got_user_status(js->gc->account, buddy_name, "offline", status ? "message" : NULL, status, NULL);
587 }
588 g_free(buddy_name);
589 }
590 g_free(status);
591 jabber_id_free(jid);
592 if(avatar_hash)
593 g_free(avatar_hash);
594 }
595
596 void jabber_presence_subscription_set(JabberStream *js, const char *who, const char *type)
597 {
598 xmlnode *presence = xmlnode_new("presence");
599
600 xmlnode_set_attrib(presence, "to", who);
601 xmlnode_set_attrib(presence, "type", type);
602
603 jabber_send(js, presence);
604 xmlnode_free(presence);
605 }
606
607 void gaim_status_to_jabber(const GaimStatus *status, JabberBuddyState *state, const char **msg, int *priority)
608 {
609 const char *status_id = NULL;
610
611 if(state) *state = JABBER_BUDDY_STATE_UNKNOWN;
612 if(msg) *msg = NULL;
613 if(priority) *priority = 0;
614
615 if(!status) {
616 if(state) *state = JABBER_BUDDY_STATE_UNAVAILABLE;
617 } else {
618 if(state) {
619 status_id = gaim_status_get_id(status);
620 *state = jabber_buddy_status_id_get_state(status_id);
621 }
622
623 if(msg) {
624 *msg = gaim_status_get_attr_string(status, "message");
625
626 /* if the message is blank, then there really isn't a message */
627 if(*msg && !**msg)
628 *msg = NULL;
629 }
630
631 if(priority)
632 *priority = gaim_status_get_attr_int(status, "priority");
633 }
634 }