libpurple/protocols/jabber/presence.c

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

mercurial