2 * purple - Jabber Protocol Plugin
4 * Purple is the legal property of its developers, whose names are too numerous
5 * to list here. Please refer to the COPYRIGHT file distributed with this
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA
26 #include "conversation.h"
37 #include "google/google.h"
38 #include "google/google_presence.h"
42 #include "adhoccommands.h"
47 static GHashTable
*presence_handlers
= NULL
;
51 JabberPresenceType type
;
52 } jabber_presence_types
[] = {
53 { "error", JABBER_PRESENCE_ERROR
},
54 { "probe", JABBER_PRESENCE_PROBE
},
55 { "unavailable", JABBER_PRESENCE_UNAVAILABLE
},
56 { "subscribe", JABBER_PRESENCE_SUBSCRIBE
},
57 { "subscribed", JABBER_PRESENCE_SUBSCRIBED
},
58 { "unsubscribe", JABBER_PRESENCE_UNSUBSCRIBE
},
59 { "unsubscribed", JABBER_PRESENCE_UNSUBSCRIBED
}
60 /* { NULL, JABBER_PRESENCE_AVAILABLE } the default */
63 static JabberPresenceType
64 str_to_presence_type(const char *type
)
69 return JABBER_PRESENCE_AVAILABLE
;
71 for (i
= 0; i
< G_N_ELEMENTS(jabber_presence_types
); ++i
)
72 if (purple_strequal(type
, jabber_presence_types
[i
].name
))
73 return jabber_presence_types
[i
].type
;
75 purple_debug_warning("jabber", "Unknown presence type '%s'\n", type
);
76 return JABBER_PRESENCE_AVAILABLE
;
79 static void chats_send_presence_foreach(gpointer key
, gpointer val
,
82 JabberChat
*chat
= val
;
83 xmlnode
*presence
= user_data
;
86 if(!chat
->conv
|| chat
->left
)
89 chat_full_jid
= g_strdup_printf("%s@%s/%s", chat
->room
, chat
->server
,
92 xmlnode_set_attrib(presence
, "to", chat_full_jid
);
93 jabber_send(chat
->js
, presence
);
94 g_free(chat_full_jid
);
97 void jabber_presence_fake_to_self(JabberStream
*js
, PurpleStatus
*status
)
99 PurpleAccount
*account
;
100 PurplePresence
*presence
;
102 JabberBuddyResource
*jbr
;
103 const char *username
;
104 JabberBuddyState state
;
108 g_return_if_fail(js
->user
!= NULL
);
110 account
= purple_connection_get_account(js
->gc
);
111 username
= purple_connection_get_display_name(js
->gc
);
112 presence
= purple_account_get_presence(account
);
114 status
= purple_presence_get_active_status(presence
);
115 purple_status_to_jabber(status
, &state
, &msg
, &priority
);
119 if (state
== JABBER_BUDDY_STATE_UNAVAILABLE
||
120 state
== JABBER_BUDDY_STATE_UNKNOWN
) {
121 jabber_buddy_remove_resource(jb
, js
->user
->resource
);
123 jbr
= jabber_buddy_track_resource(jb
, js
->user
->resource
, priority
,
125 jbr
->idle
= purple_presence_is_idle(presence
) ?
126 purple_presence_get_idle_time(presence
) : 0;
130 * While we need to track the status of this resource, the core
131 * only cares if we're on our own buddy list.
133 if (purple_find_buddy(account
, username
)) {
134 jbr
= jabber_buddy_find_resource(jb
, NULL
);
136 purple_prpl_got_user_status(account
, username
,
137 jabber_buddy_state_get_status_id(jbr
->state
),
138 "priority", jbr
->priority
,
139 jbr
->status
? "message" : NULL
, jbr
->status
,
141 purple_prpl_got_user_idle(account
, username
, jbr
->idle
, jbr
->idle
);
143 purple_prpl_got_user_status(account
, username
, "offline",
144 msg
? "message" : NULL
, msg
,
151 void jabber_set_status(PurpleAccount
*account
, PurpleStatus
*status
)
153 PurpleConnection
*gc
;
156 if (!purple_account_is_connected(account
))
159 if (purple_status_is_exclusive(status
) && !purple_status_is_active(status
)) {
160 /* An exclusive status can't be deactivated. You should just
161 * activate some other exclusive status. */
165 gc
= purple_account_get_connection(account
);
166 js
= purple_connection_get_protocol_data(gc
);
168 /* it's a mood update */
169 if (purple_status_type_get_primitive(purple_status_get_type(status
)) == PURPLE_STATUS_MOOD
) {
171 purple_status_get_attr_string(status
, PURPLE_MOOD_NAME
);
172 const char *mood_text
=
173 purple_status_get_attr_string(status
, PURPLE_MOOD_COMMENT
);
174 jabber_mood_set(js
, mood
, mood_text
);
178 jabber_presence_send(js
, FALSE
);
181 void jabber_presence_send(JabberStream
*js
, gboolean force
)
183 PurpleAccount
*account
;
184 xmlnode
*presence
, *x
, *photo
;
185 char *stripped
= NULL
;
186 JabberBuddyState state
;
188 const char *artist
= NULL
, *title
= NULL
, *source
= NULL
, *uri
= NULL
, *track
= NULL
;
192 PurpleStatus
*status
, *tune
;
194 account
= purple_connection_get_account(js
->gc
);
195 p
= purple_account_get_presence(account
);
196 status
= purple_presence_get_active_status(p
);
198 /* we don't want to send presence before we've gotten our roster */
199 if (js
->state
!= JABBER_STREAM_CONNECTED
) {
200 purple_debug_misc("jabber", "attempt to send presence before roster retrieved\n");
204 purple_status_to_jabber(status
, &state
, &stripped
, &priority
);
206 /* check for buzz support */
207 allowBuzz
= purple_status_get_attr_boolean(status
,"buzz");
208 /* changing the buzz state has to trigger a re-broadcasting of the presence for caps */
210 tune
= purple_presence_get_status(p
, "tune");
211 if (js
->googletalk
&& !stripped
&& purple_status_is_active(tune
)) {
212 stripped
= jabber_google_presence_outgoing(tune
);
215 #define CHANGED(a,b) ((!a && b) || (a && a[0] == '\0' && b && b[0] != '\0') || \
216 (a && !b) || (a && a[0] != '\0' && b && b[0] == '\0') || (a && b && !purple_strequal(a,b)))
217 /* check if there are any differences to the <presence> and send them in that case */
218 if (force
|| allowBuzz
!= js
->allowBuzz
|| js
->old_state
!= state
||
219 CHANGED(js
->old_msg
, stripped
) || js
->old_priority
!= priority
||
220 CHANGED(js
->old_avatarhash
, js
->avatar_hash
) || js
->old_idle
!= js
->idle
) {
221 /* Need to update allowBuzz before creating the presence (with caps) */
222 js
->allowBuzz
= allowBuzz
;
224 presence
= jabber_presence_create_js(js
, state
, stripped
, priority
);
226 /* Per XEP-0153 4.1, we must always send the <x> */
227 x
= xmlnode_new_child(presence
, "x");
228 xmlnode_set_namespace(x
, "vcard-temp:x:update");
230 * FIXME: Per XEP-0153 4.3.2 bullet 2, we must not publish our
231 * image hash if another resource has logged in and updated the
232 * vcard avatar. Requires changes in jabber_presence_parse.
234 if (js
->vcard_fetched
) {
235 /* Always publish a <photo>; it's empty if we have no image. */
236 photo
= xmlnode_new_child(x
, "photo");
238 xmlnode_insert_data(photo
, js
->avatar_hash
, -1);
241 jabber_send(js
, presence
);
243 g_hash_table_foreach(js
->chats
, chats_send_presence_foreach
, presence
);
244 xmlnode_free(presence
);
246 /* update old values */
250 if(js
->old_avatarhash
)
251 g_free(js
->old_avatarhash
);
252 js
->old_msg
= g_strdup(stripped
);
253 js
->old_avatarhash
= g_strdup(js
->avatar_hash
);
254 js
->old_state
= state
;
255 js
->old_priority
= priority
;
256 js
->old_idle
= js
->idle
;
260 /* next, check if there are any changes to the tune values */
261 if (purple_status_is_active(tune
)) {
262 artist
= purple_status_get_attr_string(tune
, PURPLE_TUNE_ARTIST
);
263 title
= purple_status_get_attr_string(tune
, PURPLE_TUNE_TITLE
);
264 source
= purple_status_get_attr_string(tune
, PURPLE_TUNE_ALBUM
);
265 uri
= purple_status_get_attr_string(tune
, PURPLE_TUNE_URL
);
266 track
= purple_status_get_attr_string(tune
, PURPLE_TUNE_TRACK
);
267 length
= (!purple_status_get_attr_value(tune
, PURPLE_TUNE_TIME
)) ? -1 :
268 purple_status_get_attr_int(tune
, PURPLE_TUNE_TIME
);
271 if(CHANGED(artist
, js
->old_artist
) || CHANGED(title
, js
->old_title
) || CHANGED(source
, js
->old_source
) ||
272 CHANGED(uri
, js
->old_uri
) || CHANGED(track
, js
->old_track
) || (length
!= js
->old_length
)) {
273 PurpleJabberTuneInfo tuneinfo
= {
281 jabber_tune_set(js
->gc
, &tuneinfo
);
283 /* update old values */
284 g_free(js
->old_artist
);
285 g_free(js
->old_title
);
286 g_free(js
->old_source
);
288 g_free(js
->old_track
);
289 js
->old_artist
= g_strdup(artist
);
290 js
->old_title
= g_strdup(title
);
291 js
->old_source
= g_strdup(source
);
292 js
->old_uri
= g_strdup(uri
);
293 js
->old_length
= length
;
294 js
->old_track
= g_strdup(track
);
299 jabber_presence_fake_to_self(js
, status
);
302 xmlnode
*jabber_presence_create(JabberBuddyState state
, const char *msg
, int priority
)
304 return jabber_presence_create_js(NULL
, state
, msg
, priority
);
307 xmlnode
*jabber_presence_create_js(JabberStream
*js
, JabberBuddyState state
, const char *msg
, int priority
)
309 xmlnode
*show
, *status
, *presence
, *pri
, *c
;
310 const char *show_string
= NULL
;
312 gboolean audio_enabled
, video_enabled
;
315 presence
= xmlnode_new("presence");
317 if(state
== JABBER_BUDDY_STATE_UNAVAILABLE
)
318 xmlnode_set_attrib(presence
, "type", "unavailable");
319 else if(state
!= JABBER_BUDDY_STATE_ONLINE
&&
320 state
!= JABBER_BUDDY_STATE_UNKNOWN
&&
321 state
!= JABBER_BUDDY_STATE_ERROR
)
322 show_string
= jabber_buddy_state_get_show(state
);
325 show
= xmlnode_new_child(presence
, "show");
326 xmlnode_insert_data(show
, show_string
, -1);
330 status
= xmlnode_new_child(presence
, "status");
331 xmlnode_insert_data(status
, msg
, -1);
335 char *pstr
= g_strdup_printf("%d", priority
);
336 pri
= xmlnode_new_child(presence
, "priority");
337 xmlnode_insert_data(pri
, pstr
, -1);
341 /* if we are idle and not offline, include idle */
342 if (js
->idle
&& state
!= JABBER_BUDDY_STATE_UNAVAILABLE
) {
343 xmlnode
*query
= xmlnode_new_child(presence
, "query");
345 g_snprintf(seconds
, 10, "%d", (int) (time(NULL
) - js
->idle
));
347 xmlnode_set_namespace(query
, NS_LAST_ACTIVITY
);
348 xmlnode_set_attrib(query
, "seconds", seconds
);
353 jabber_caps_calculate_own_hash(js
);
355 c
= xmlnode_new_child(presence
, "c");
356 xmlnode_set_namespace(c
, "http://jabber.org/protocol/caps");
357 xmlnode_set_attrib(c
, "node", CAPS0115_NODE
);
358 xmlnode_set_attrib(c
, "hash", "sha-1");
359 xmlnode_set_attrib(c
, "ver", jabber_caps_get_own_hash(js
));
363 * MASSIVE HUGE DISGUSTING HACK
364 * This is a huge hack. As far as I can tell, Google Talk's gmail client
365 * doesn't bother to check the actual features we advertise; they
366 * just assume that if we specify a 'voice-v1' ext (ignoring that
367 * these are to be assigned no semantic value), we support receiving voice
370 * Ditto for 'video-v1'.
372 audio_enabled
= jabber_audio_enabled(js
, NULL
/* unused */);
373 video_enabled
= jabber_video_enabled(js
, NULL
/* unused */);
375 if (audio_enabled
&& video_enabled
)
376 xmlnode_set_attrib(c
, "ext", "voice-v1 camera-v1 video-v1");
377 else if (audio_enabled
)
378 xmlnode_set_attrib(c
, "ext", "voice-v1");
379 else if (video_enabled
)
380 xmlnode_set_attrib(c
, "ext", "camera-v1 video-v1");
386 struct _jabber_add_permit
{
387 PurpleConnection
*gc
;
392 static void authorize_add_cb(gpointer data
)
394 struct _jabber_add_permit
*jap
= data
;
395 if(PURPLE_CONNECTION_IS_VALID(jap
->gc
))
396 jabber_presence_subscription_set(jap
->gc
->proto_data
,
397 jap
->who
, "subscribed");
402 static void deny_add_cb(gpointer data
)
404 struct _jabber_add_permit
*jap
= data
;
405 if(PURPLE_CONNECTION_IS_VALID(jap
->gc
))
406 jabber_presence_subscription_set(jap
->gc
->proto_data
,
407 jap
->who
, "unsubscribed");
413 jabber_vcard_parse_avatar(JabberStream
*js
, const char *from
,
414 JabberIqType type
, const char *id
,
415 xmlnode
*packet
, gpointer blah
)
417 JabberBuddy
*jb
= NULL
;
418 xmlnode
*vcard
, *photo
, *binval
, *fn
, *nick
;
424 jb
= jabber_buddy_find(js
, from
, TRUE
);
426 js
->pending_avatar_requests
= g_slist_remove(js
->pending_avatar_requests
, jb
);
428 if((vcard
= xmlnode_get_child(packet
, "vCard")) ||
429 (vcard
= xmlnode_get_child_with_namespace(packet
, "query", "vcard-temp"))) {
430 /* The logic here regarding the nickname and full name is copied from
431 * buddy.c:jabber_vcard_parse. */
432 gchar
*nickname
= NULL
;
433 if ((fn
= xmlnode_get_child(vcard
, "FN")))
434 nickname
= xmlnode_get_data(fn
);
436 if ((nick
= xmlnode_get_child(vcard
, "NICKNAME"))) {
437 char *tmp
= xmlnode_get_data(nick
);
438 char *bare_jid
= jabber_get_bare_jid(from
);
439 if (tmp
&& strstr(bare_jid
, tmp
) == NULL
) {
449 serv_got_alias(js
->gc
, from
, nickname
);
453 if ((photo
= xmlnode_get_child(vcard
, "PHOTO"))) {
458 if ((binval
= xmlnode_get_child(photo
, "BINVAL")) &&
459 (text
= xmlnode_get_data(binval
))) {
460 data
= purple_base64_decode(text
, &size
);
464 hash
= jabber_calculate_data_hash(data
, size
, "sha1");
467 purple_buddy_icons_set_for_user(js
->gc
->account
, from
, data
, size
, hash
);
474 typedef struct _JabberPresenceCapabilities
{
478 } JabberPresenceCapabilities
;
481 jabber_presence_set_capabilities(JabberCapsClientInfo
*info
, GList
*exts
,
482 JabberPresenceCapabilities
*userdata
)
484 JabberBuddyResource
*jbr
;
485 char *resource
= strchr(userdata
->from
, '/');
490 jbr
= jabber_buddy_find_resource(userdata
->jb
, resource
);
492 g_free(userdata
->from
);
495 g_list_foreach(exts
, (GFunc
)g_free
, NULL
);
501 /* Any old jbr->caps.info is owned by the caps code */
502 if (jbr
->caps
.exts
) {
503 g_list_foreach(jbr
->caps
.exts
, (GFunc
)g_free
, NULL
);
504 g_list_free(jbr
->caps
.exts
);
507 jbr
->caps
.info
= info
;
508 jbr
->caps
.exts
= exts
;
510 purple_prpl_got_media_caps(
511 purple_connection_get_account(userdata
->js
->gc
),
516 if (!jbr
->commands_fetched
&& jabber_resource_has_capability(jbr
, "http://jabber.org/protocol/commands")) {
517 JabberIq
*iq
= jabber_iq_new_query(userdata
->js
, JABBER_IQ_GET
, NS_DISCO_ITEMS
);
518 xmlnode
*query
= xmlnode_get_child_with_namespace(iq
->node
, "query", NS_DISCO_ITEMS
);
519 xmlnode_set_attrib(iq
->node
, "to", userdata
->from
);
520 xmlnode_set_attrib(query
, "node", "http://jabber.org/protocol/commands");
521 jabber_iq_set_callback(iq
, jabber_adhoc_disco_result_cb
, NULL
);
524 jbr
->commands_fetched
= TRUE
;
529 * Versions of libpurple before 2.6.0 didn't advertise this capability, so
530 * we can't yet use Entity Capabilities to determine whether or not the
531 * other client supports Chat States.
533 if (jabber_resource_has_capability(jbr
, "http://jabber.org/protocol/chatstates"))
534 jbr
->chat_states
= JABBER_CHAT_STATES_SUPPORTED
;
536 jbr
->chat_states
= JABBER_CHAT_STATES_UNSUPPORTED
;
540 g_free(userdata
->from
);
545 handle_presence_chat(JabberStream
*js
, JabberPresence
*presence
, xmlnode
*packet
)
548 PurpleConvChatBuddyFlags flags
= PURPLE_CBFLAGS_NONE
;
549 JabberChat
*chat
= presence
->chat
;
551 if (presence
->state
== JABBER_BUDDY_STATE_ERROR
) {
552 char *title
, *msg
= jabber_parse_error(js
, packet
, NULL
);
555 title
= g_strdup_printf(_("Error joining chat %s"), presence
->from
);
556 purple_serv_got_join_chat_failed(js
->gc
, chat
->components
);
558 title
= g_strdup_printf(_("Error in chat %s"), presence
->from
);
559 if (g_hash_table_size(chat
->members
) == 0)
560 serv_got_chat_left(js
->gc
, chat
->id
);
562 purple_notify_error(js
->gc
, title
, title
, msg
);
566 if (g_hash_table_size(chat
->members
) == 0)
567 /* Only destroy the chat if the error happened while joining */
568 jabber_chat_destroy(chat
);
572 if (presence
->type
== JABBER_PRESENCE_AVAILABLE
) {
573 const char *jid
= NULL
;
574 const char *affiliation
= NULL
;
575 const char *role
= NULL
;
576 gboolean is_our_resource
= FALSE
; /* Is the presence about us? */
577 JabberBuddyResource
*jbr
;
580 * XEP-0045 mandates the presence to include a resource (which is
581 * treated as the chat nick). Some non-compliant servers allow
582 * joining without a nick.
584 if (!presence
->jid_from
->resource
)
587 if (presence
->chat_info
.item
) {
588 jid
= xmlnode_get_attrib(presence
->chat_info
.item
, "jid");
589 affiliation
= xmlnode_get_attrib(presence
->chat_info
.item
, "affiliation");
590 role
= xmlnode_get_attrib(presence
->chat_info
.item
, "role");
593 if (g_slist_find(presence
->chat_info
.codes
, GINT_TO_POINTER(110)) ||
594 purple_strequal(presence
->jid_from
->resource
, chat
->handle
) ||
595 purple_strequal(presence
->to
, jid
))
596 is_our_resource
= TRUE
;
598 if (g_slist_find(presence
->chat_info
.codes
, GINT_TO_POINTER(201))) {
599 chat
->config_dialog_type
= PURPLE_REQUEST_ACTION
;
600 chat
->config_dialog_handle
=
601 purple_request_action(js
->gc
,
602 _("Create New Room"),
603 _("Create New Room"),
604 _("You are creating a new room. Would"
605 " you like to configure it, or"
606 " accept the default settings?"),
607 /* Default Action */ 1,
608 purple_connection_get_account(js
->gc
), NULL
, chat
->conv
,
610 _("_Configure Room"), G_CALLBACK(jabber_chat_request_room_configure
),
611 _("_Accept Defaults"), G_CALLBACK(jabber_chat_create_instant_room
));
614 if (g_slist_find(presence
->chat_info
.codes
, GINT_TO_POINTER(210))) {
615 /* server rewrote room-nick */
616 g_free(chat
->handle
);
617 chat
->handle
= g_strdup(presence
->jid_from
->resource
);
620 if (purple_strequal(affiliation
, "owner"))
621 flags
|= PURPLE_CBFLAGS_FOUNDER
;
623 if (purple_strequal(role
, "moderator"))
624 flags
|= PURPLE_CBFLAGS_OP
;
625 else if (purple_strequal(role
, "participant"))
626 flags
|= PURPLE_CBFLAGS_VOICE
;
630 char *room_jid
= g_strdup_printf("%s@%s", presence
->jid_from
->node
, presence
->jid_from
->domain
);
632 chat
->conv
= serv_got_joined_chat(js
->gc
, chat
->id
, room_jid
);
633 purple_conv_chat_set_nick(PURPLE_CONV_CHAT(chat
->conv
), chat
->handle
);
635 jabber_chat_disco_traffic(chat
);
639 jbr
= jabber_buddy_track_resource(presence
->jb
, presence
->jid_from
->resource
, presence
->priority
, presence
->state
, presence
->status
);
640 jbr
->commands_fetched
= TRUE
;
642 jabber_chat_track_handle(chat
, presence
->jid_from
->resource
, jid
, affiliation
, role
);
644 if(!jabber_chat_find_buddy(chat
->conv
, presence
->jid_from
->resource
))
645 purple_conv_chat_add_user(PURPLE_CONV_CHAT(chat
->conv
), presence
->jid_from
->resource
,
646 jid
, flags
, chat
->joined
> 0 && ((!presence
->delayed
) || (presence
->sent
> chat
->joined
)));
648 purple_conv_chat_user_set_flags(PURPLE_CONV_CHAT(chat
->conv
), presence
->jid_from
->resource
,
651 if (is_our_resource
&& chat
->joined
== 0)
652 chat
->joined
= time(NULL
);
654 } else if (presence
->type
== JABBER_PRESENCE_UNAVAILABLE
) {
655 gboolean nick_change
= FALSE
;
656 gboolean kick
= FALSE
;
657 gboolean is_our_resource
= FALSE
; /* Is the presence about us? */
659 const char *jid
= NULL
;
661 /* If the chat nick is invalid, we haven't yet joined, or we've
662 * already left (it was probably us leaving after we closed the
663 * chat), we don't care.
665 if (!presence
->jid_from
->resource
|| !chat
->conv
|| chat
->left
) {
667 presence
->jid_from
->resource
&& chat
->handle
&& purple_strequal(presence
->jid_from
->resource
, chat
->handle
))
668 jabber_chat_destroy(chat
);
672 is_our_resource
= purple_strequal(presence
->jid_from
->resource
, chat
->handle
);
674 jabber_buddy_remove_resource(presence
->jb
, presence
->jid_from
->resource
);
676 if (presence
->chat_info
.item
)
677 jid
= xmlnode_get_attrib(presence
->chat_info
.item
, "jid");
680 if (g_slist_find(presence
->chat_info
.codes
, GINT_TO_POINTER(110))) {
681 is_our_resource
= TRUE
;
685 if (g_slist_find(presence
->chat_info
.codes
, GINT_TO_POINTER(301))) {
686 /* XXX: We got banned. YAY! (No GIR, that's bad) */
690 if (g_slist_find(presence
->chat_info
.codes
, GINT_TO_POINTER(303))) {
691 const char *nick
= NULL
;
692 if (presence
->chat_info
.item
)
693 nick
= xmlnode_get_attrib(presence
->chat_info
.item
, "nick");
697 purple_debug_warning("jabber", "Chat presence indicating a nick change, but no new nickname!\n");
701 if (purple_strequal(presence
->jid_from
->resource
, chat
->handle
)) {
702 /* Changing our own nickname */
703 g_free(chat
->handle
);
704 /* TODO: This should be resourceprep'd */
705 chat
->handle
= g_strdup(nick
);
708 purple_conv_chat_rename_user(PURPLE_CONV_CHAT(chat
->conv
),
709 presence
->jid_from
->resource
,
711 jabber_chat_remove_handle(chat
,
712 presence
->jid_from
->resource
);
716 if (g_slist_find(presence
->chat_info
.codes
, GINT_TO_POINTER(307))) {
717 /* Someone was kicked from the room */
718 const char *actor
= NULL
;
724 if (presence
->chat_info
.item
) {
727 node
= xmlnode_get_child(presence
->chat_info
.item
, "actor");
729 actor
= xmlnode_get_attrib(node
, "jid");
730 node
= xmlnode_get_child(presence
->chat_info
.item
, "reason");
732 reason
= xmlnode_get_data(node
);
736 reason
= g_strdup(_("No reason"));
738 if (is_our_resource
) {
740 tmp
= g_strdup_printf(_("You have been kicked by %s: (%s)"),
743 tmp
= g_strdup_printf(_("You have been kicked: (%s)"),
747 tmp
= g_strdup_printf(_("Kicked by %s (%s)"),
750 tmp
= g_strdup_printf(_("Kicked (%s)"),
754 g_free(presence
->status
);
755 presence
->status
= tmp
;
760 if (g_slist_find(presence
->chat_info
.codes
, GINT_TO_POINTER(321))) {
761 /* XXX: removed due to an affiliation change */
764 if (g_slist_find(presence
->chat_info
.codes
, GINT_TO_POINTER(322))) {
765 /* XXX: removed because room is now members-only */
768 if (g_slist_find(presence
->chat_info
.codes
, GINT_TO_POINTER(332))) {
769 /* XXX: removed due to system shutdown */
774 * Possibly another connected resource of our JID (see XEP-0045
775 * v1.24 section 7.1.10) being disconnected. Should be
776 * distinguished by the item_jid.
777 * Also possibly works around bits of an Openfire bug. See
780 if (is_our_resource
&& jid
&& !purple_strequal(presence
->to
, jid
)) {
781 /* TODO: When the above is a loop, this needs to still act
782 * sanely for all cases (this code is a little fragile). */
783 if (!kick
&& !nick_change
)
784 /* Presumably, kicks and nick changes also affect us. */
785 is_our_resource
= FALSE
;
789 if (is_our_resource
) {
791 purple_conv_chat_write(PURPLE_CONV_CHAT(chat
->conv
), presence
->jid_from
->resource
,
792 presence
->status
, PURPLE_MESSAGE_SYSTEM
, time(NULL
));
794 serv_got_chat_left(js
->gc
, chat
->id
);
795 jabber_chat_destroy(chat
);
797 purple_conv_chat_remove_user(PURPLE_CONV_CHAT(chat
->conv
), presence
->jid_from
->resource
,
799 jabber_chat_remove_handle(chat
, presence
->jid_from
->resource
);
808 handle_presence_contact(JabberStream
*js
, JabberPresence
*presence
)
810 JabberBuddyResource
*jbr
;
811 PurpleAccount
*account
;
814 PurpleConversation
*conv
;
816 buddy_name
= jabber_id_get_bare_jid(presence
->jid_from
);
818 account
= purple_connection_get_account(js
->gc
);
819 b
= purple_find_buddy(account
, buddy_name
);
822 * Unbind/unlock from sending messages to a specific resource on
823 * presence changes. This is locked to a specific resource when
824 * receiving a message (in message.c).
826 conv
= purple_find_conversation_with_account(PURPLE_CONV_TYPE_IM
,
827 buddy_name
, account
);
829 purple_debug_info("jabber", "Changed conversation binding from %s to %s\n",
830 purple_conversation_get_name(conv
), buddy_name
);
831 purple_conversation_set_name(conv
, buddy_name
);
835 if (presence
->jb
!= js
->user_jb
) {
836 purple_debug_warning("jabber", "Got presence for unknown buddy %s on account %s (%p)\n",
837 buddy_name
, purple_account_get_username(account
), account
);
841 /* this is a different resource of our own account. Resume even when this account isn't on our blist */
845 if (b
&& presence
->vcard_avatar_hash
) {
846 const char *ah
= presence
->vcard_avatar_hash
[0] != '\0' ?
847 presence
->vcard_avatar_hash
: NULL
;
848 const char *ah2
= purple_buddy_icons_get_checksum_for_user(b
);
849 if (!purple_strequal(ah
, ah2
)) {
850 /* XXX this is a crappy way of trying to prevent
851 * someone from spamming us with presence packets
852 * and causing us to DoS ourselves...what we really
853 * need is a queue system that can throttle itself,
854 * but i'm too tired to write that right now */
855 if(!g_slist_find(js
->pending_avatar_requests
, presence
->jb
)) {
859 js
->pending_avatar_requests
=
860 g_slist_prepend(js
->pending_avatar_requests
, presence
->jb
);
862 iq
= jabber_iq_new(js
, JABBER_IQ_GET
);
863 xmlnode_set_attrib(iq
->node
, "to", buddy_name
);
864 vcard
= xmlnode_new_child(iq
->node
, "vCard");
865 xmlnode_set_namespace(vcard
, "vcard-temp");
867 jabber_iq_set_callback(iq
, jabber_vcard_parse_avatar
, NULL
);
873 if (presence
->state
== JABBER_BUDDY_STATE_ERROR
||
874 presence
->type
== JABBER_PRESENCE_UNAVAILABLE
||
875 presence
->type
== JABBER_PRESENCE_UNSUBSCRIBED
) {
876 jabber_buddy_remove_resource(presence
->jb
, presence
->jid_from
->resource
);
878 jbr
= jabber_buddy_track_resource(presence
->jb
,
879 presence
->jid_from
->resource
, presence
->priority
,
880 presence
->state
, presence
->status
);
881 jbr
->idle
= presence
->idle
? time(NULL
) - presence
->idle
: 0;
884 jbr
= jabber_buddy_find_resource(presence
->jb
, NULL
);
886 jabber_google_presence_incoming(js
, buddy_name
, jbr
);
887 purple_prpl_got_user_status(account
, buddy_name
,
888 jabber_buddy_state_get_status_id(jbr
->state
),
889 "priority", jbr
->priority
,
890 "message", jbr
->status
,
892 purple_prpl_got_user_idle(account
, buddy_name
,
893 jbr
->idle
, jbr
->idle
);
894 if (presence
->nickname
)
895 serv_got_alias(js
->gc
, buddy_name
, presence
->nickname
);
897 purple_prpl_got_user_status(account
, buddy_name
,
898 jabber_buddy_state_get_status_id(JABBER_BUDDY_STATE_UNAVAILABLE
),
899 presence
->status
? "message" : NULL
, presence
->status
,
907 void jabber_presence_parse(JabberStream
*js
, xmlnode
*packet
)
910 JabberBuddyResource
*jbr
= NULL
;
911 gboolean signal_return
, ret
;
912 JabberPresence presence
;
915 memset(&presence
, 0, sizeof(presence
));
917 presence
.state
= JABBER_BUDDY_STATE_UNKNOWN
;
918 presence
.sent
= time(NULL
);
919 /* interesting values */
920 presence
.from
= xmlnode_get_attrib(packet
, "from");
921 presence
.to
= xmlnode_get_attrib(packet
, "to");
922 type
= xmlnode_get_attrib(packet
, "type");
923 presence
.type
= str_to_presence_type(type
);
925 presence
.jb
= jabber_buddy_find(js
, presence
.from
, TRUE
);
926 g_return_if_fail(presence
.jb
!= NULL
);
928 presence
.jid_from
= jabber_id_new(presence
.from
);
929 if (presence
.jid_from
== NULL
) {
930 purple_debug_error("jabber", "Ignoring presence with malformed 'from' "
931 "JID: %s\n", presence
.from
);
935 signal_return
= GPOINTER_TO_INT(purple_signal_emit_return_1(purple_connection_get_prpl(js
->gc
),
936 "jabber-receiving-presence", js
->gc
, type
, presence
.from
, packet
));
941 if (presence
.jid_from
->node
)
942 presence
.chat
= jabber_chat_find(js
, presence
.jid_from
->node
,
943 presence
.jid_from
->domain
);
944 if(presence
.jb
->error_msg
) {
945 g_free(presence
.jb
->error_msg
);
946 presence
.jb
->error_msg
= NULL
;
949 if (presence
.type
== JABBER_PRESENCE_AVAILABLE
) {
950 presence
.state
= JABBER_BUDDY_STATE_ONLINE
;
951 } else if (presence
.type
== JABBER_PRESENCE_ERROR
) {
952 /* TODO: Is this handled properly? Should it be treated as per-jbr? */
953 char *msg
= jabber_parse_error(js
, packet
, NULL
);
954 presence
.state
= JABBER_BUDDY_STATE_ERROR
;
955 presence
.jb
->error_msg
= msg
? msg
: g_strdup(_("Unknown Error in presence"));
956 } else if (presence
.type
== JABBER_PRESENCE_SUBSCRIBE
) {
957 /* TODO: Move to handle_subscribe() (so nick is extracted by the
959 struct _jabber_add_permit
*jap
= g_new0(struct _jabber_add_permit
, 1);
960 gboolean onlist
= FALSE
;
961 PurpleAccount
*account
;
965 account
= purple_connection_get_account(js
->gc
);
966 buddy
= purple_find_buddy(account
, presence
.from
);
967 nick
= xmlnode_get_child_with_namespace(packet
, "nick", "http://jabber.org/protocol/nick");
969 presence
.nickname
= xmlnode_get_data(nick
);
972 if ((presence
.jb
->subscription
& (JABBER_SUB_TO
| JABBER_SUB_PENDING
)))
977 jap
->who
= g_strdup(presence
.from
);
980 purple_account_request_authorization(account
, presence
.from
, NULL
, presence
.nickname
,
981 NULL
, onlist
, authorize_add_cb
, deny_add_cb
, jap
);
984 } else if (presence
.type
== JABBER_PRESENCE_SUBSCRIBED
) {
985 /* This case (someone has approved our subscribe request) is handled
986 * by the roster push the server sends along with this.
989 } else if (presence
.type
== JABBER_PRESENCE_UNSUBSCRIBE
) {
990 /* XXX I'm not sure this is the right way to handle this, it
991 * might be better to add "unsubscribe" to the presence status
992 * if lower down, but I'm not sure. */
993 /* they are unsubscribing from our presence, we don't care */
994 /* Well, maybe just a little, we might want/need to start
995 * acknowledging this (and the others) at some point. */
997 } else if (presence
.type
== JABBER_PRESENCE_PROBE
) {
998 purple_debug_warning("jabber", "Ignoring presence probe\n");
1000 } else if (presence
.type
== JABBER_PRESENCE_UNAVAILABLE
) {
1001 presence
.state
= JABBER_BUDDY_STATE_UNAVAILABLE
;
1002 } else if (presence
.type
== JABBER_PRESENCE_UNSUBSCRIBED
) {
1003 presence
.state
= JABBER_BUDDY_STATE_UNKNOWN
;
1005 purple_debug_warning("jabber", "Ignoring presence with invalid type "
1010 for (child
= packet
->child
; child
; child
= child
->next
) {
1013 JabberPresenceHandler
*pih
;
1014 if (child
->type
!= XMLNODE_TYPE_TAG
)
1017 xmlns
= xmlnode_get_namespace(child
);
1018 key
= g_strdup_printf("%s %s", child
->name
, xmlns
? xmlns
: "");
1019 pih
= g_hash_table_lookup(presence_handlers
, key
);
1022 pih(js
, &presence
, child
);
1025 if (presence
.delayed
&& presence
.idle
) {
1026 /* Delayed and idle, so update idle time */
1027 presence
.idle
= presence
.idle
+ (time(NULL
) - presence
.sent
);
1030 /* TODO: Handle tracking jb(r) here? */
1033 ret
= handle_presence_chat(js
, &presence
, packet
);
1035 ret
= handle_presence_contact(js
, &presence
);
1039 if (presence
.caps
&& presence
.type
== JABBER_PRESENCE_AVAILABLE
) {
1040 /* handle Entity Capabilities (XEP-0115) */
1041 const char *node
= xmlnode_get_attrib(presence
.caps
, "node");
1042 const char *ver
= xmlnode_get_attrib(presence
.caps
, "ver");
1043 const char *hash
= xmlnode_get_attrib(presence
.caps
, "hash");
1044 const char *ext
= xmlnode_get_attrib(presence
.caps
, "ext");
1046 /* v1.3 uses: node, ver, and optionally ext.
1047 * v1.5 uses: node, ver, and hash. */
1048 if (node
&& *node
&& ver
&& *ver
) {
1049 gchar
**exts
= ext
&& *ext
? g_strsplit(ext
, " ", -1) : NULL
;
1050 jbr
= jabber_buddy_find_resource(presence
.jb
, presence
.jid_from
->resource
);
1052 /* Look it up if we don't already have all this information */
1053 if (!jbr
|| !jbr
->caps
.info
||
1054 !purple_strequal(node
, jbr
->caps
.info
->tuple
.node
) ||
1055 !purple_strequal(ver
, jbr
->caps
.info
->tuple
.ver
) ||
1056 !purple_strequal(hash
, jbr
->caps
.info
->tuple
.hash
) ||
1057 !jabber_caps_exts_known(jbr
->caps
.info
, (gchar
**)exts
)) {
1058 JabberPresenceCapabilities
*userdata
= g_new0(JabberPresenceCapabilities
, 1);
1060 userdata
->jb
= presence
.jb
;
1061 userdata
->from
= g_strdup(presence
.from
);
1062 jabber_caps_get_info(js
, presence
.from
, node
, ver
, hash
, exts
,
1063 (jabber_caps_get_info_cb
)jabber_presence_set_capabilities
,
1073 while (presence
.chat_info
.codes
)
1074 presence
.chat_info
.codes
=
1075 g_slist_delete_link(presence
.chat_info
.codes
,
1076 presence
.chat_info
.codes
);
1078 g_free(presence
.status
);
1079 g_free(presence
.vcard_avatar_hash
);
1080 g_free(presence
.nickname
);
1081 jabber_id_free(presence
.jid_from
);
1084 void jabber_presence_subscription_set(JabberStream
*js
, const char *who
, const char *type
)
1086 xmlnode
*presence
= xmlnode_new("presence");
1088 xmlnode_set_attrib(presence
, "to", who
);
1089 xmlnode_set_attrib(presence
, "type", type
);
1091 jabber_send(js
, presence
);
1092 xmlnode_free(presence
);
1095 void purple_status_to_jabber(const PurpleStatus
*status
, JabberBuddyState
*state
, char **msg
, int *priority
)
1097 const char *status_id
= NULL
;
1098 const char *formatted_msg
= NULL
;
1100 if(state
) *state
= JABBER_BUDDY_STATE_UNKNOWN
;
1101 if(msg
) *msg
= NULL
;
1102 if(priority
) *priority
= 0;
1105 if(state
) *state
= JABBER_BUDDY_STATE_UNAVAILABLE
;
1108 status_id
= purple_status_get_id(status
);
1109 *state
= jabber_buddy_status_id_get_state(status_id
);
1113 formatted_msg
= purple_status_get_attr_string(status
, "message");
1115 /* if the message is blank, then there really isn't a message */
1116 if(formatted_msg
&& *formatted_msg
)
1117 *msg
= purple_markup_strip_html(formatted_msg
);
1121 *priority
= purple_status_get_attr_int(status
, "priority");
1125 /* Incoming presence handlers */
1127 parse_priority(JabberStream
*js
, JabberPresence
*presence
, xmlnode
*priority
)
1129 char *p
= xmlnode_get_data(priority
);
1131 if (presence
->priority
!= 0)
1132 purple_debug_warning("jabber", "presence stanza received with multiple "
1133 "priority children!?\n");
1136 presence
->priority
= atoi(p
);
1139 purple_debug_warning("jabber", "Empty <priority/> in presence!\n");
1143 parse_show(JabberStream
*js
, JabberPresence
*presence
, xmlnode
*show
)
1147 if (presence
->type
!= JABBER_PRESENCE_AVAILABLE
) {
1148 purple_debug_warning("jabber", "<show/> present on presence, but "
1149 "type is not default ('available')\n");
1153 cdata
= xmlnode_get_data(show
);
1155 presence
->state
= jabber_buddy_show_get_state(cdata
);
1158 purple_debug_warning("jabber", "<show/> present on presence, but "
1163 parse_status(JabberStream
*js
, JabberPresence
*presence
, xmlnode
*status
)
1165 /* TODO: Check/track language attribute? */
1167 g_free(presence
->status
);
1168 presence
->status
= xmlnode_get_data(status
);
1172 parse_delay(JabberStream
*js
, JabberPresence
*presence
, xmlnode
*delay
)
1174 const char *stamp
= xmlnode_get_attrib(delay
, "stamp");
1175 presence
->delayed
= TRUE
;
1176 presence
->sent
= purple_str_to_time(stamp
, TRUE
, NULL
, NULL
, NULL
);
1180 parse_idle(JabberStream
*js
, JabberPresence
*presence
, xmlnode
*query
)
1182 const gchar
*seconds
= xmlnode_get_attrib(query
, "seconds");
1184 presence
->idle
= atoi(seconds
);
1185 if (presence
->idle
< 0) {
1186 purple_debug_warning("jabber", "Received bogus idle time %s\n", seconds
);
1193 parse_caps(JabberStream
*js
, JabberPresence
*presence
, xmlnode
*c
)
1195 /* TODO: Move the rest of the caps handling in here, after changing the
1196 * the "do we have details about this (node, ver) and exts" to not
1197 * require the jbr to be present (since that happens later).
1203 parse_nickname(JabberStream
*js
, JabberPresence
*presence
, xmlnode
*nick
)
1205 g_free(presence
->nickname
);
1206 presence
->nickname
= xmlnode_get_data(nick
);
1210 parse_vcard_avatar(JabberStream
*js
, JabberPresence
*presence
, xmlnode
*x
)
1212 xmlnode
*photo
= xmlnode_get_child(x
, "photo");
1215 char *hash_tmp
= xmlnode_get_data(photo
);
1216 g_free(presence
->vcard_avatar_hash
);
1217 presence
->vcard_avatar_hash
=
1218 hash_tmp
? hash_tmp
: g_strdup("");
1223 parse_muc_user(JabberStream
*js
, JabberPresence
*presence
, xmlnode
*x
)
1227 if (presence
->chat
== NULL
) {
1228 purple_debug_warning("jabber", "Ignoring MUC gloop on non-MUC presence\n");
1232 if (presence
->chat
->conv
== NULL
)
1233 presence
->chat
->muc
= TRUE
;
1235 for (status
= xmlnode_get_child(x
, "status"); status
;
1236 status
= xmlnode_get_next_twin(status
)) {
1237 const char *code
= xmlnode_get_attrib(status
, "code");
1243 if (val
== 0 || val
< 0) {
1244 purple_debug_warning("jabber", "Ignoring bogus status code '%s'\n",
1249 presence
->chat_info
.codes
= g_slist_prepend(presence
->chat_info
.codes
, GINT_TO_POINTER(val
));
1252 presence
->chat_info
.item
= xmlnode_get_child(x
, "item");
1255 void jabber_presence_register_handler(const char *node
, const char *xmlns
,
1256 JabberPresenceHandler
*handler
)
1259 * This is valid because nodes nor namespaces cannot have spaces in them
1260 * (see http://www.w3.org/TR/2006/REC-xml-20060816/ and
1261 * http://www.w3.org/TR/REC-xml-names/)
1263 char *key
= g_strdup_printf("%s %s", node
, xmlns
);
1264 g_hash_table_replace(presence_handlers
, key
, handler
);
1267 void jabber_presence_init(void)
1269 presence_handlers
= g_hash_table_new_full(g_str_hash
, g_str_equal
, g_free
, NULL
);
1271 /* Core RFC things */
1272 jabber_presence_register_handler("priority", "jabber:client", parse_priority
);
1273 jabber_presence_register_handler("show", "jabber:client", parse_show
);
1274 jabber_presence_register_handler("status", "jabber:client", parse_status
);
1277 jabber_presence_register_handler("c", "http://jabber.org/protocol/caps", parse_caps
);
1278 jabber_presence_register_handler("delay", NS_DELAYED_DELIVERY
, parse_delay
);
1279 jabber_presence_register_handler("nick", "http://jabber.org/protocol/nick", parse_nickname
);
1280 jabber_presence_register_handler("query", NS_LAST_ACTIVITY
, parse_idle
);
1281 jabber_presence_register_handler("x", NS_DELAYED_DELIVERY_LEGACY
, parse_delay
);
1282 jabber_presence_register_handler("x", "http://jabber.org/protocol/muc#user", parse_muc_user
);
1283 jabber_presence_register_handler("x", "vcard-temp:x:update", parse_vcard_avatar
);
1286 void jabber_presence_uninit(void)
1288 g_hash_table_destroy(presence_handlers
);
1289 presence_handlers
= NULL
;