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
27 #include "useravatar.h"
31 #define MAX_HTTP_BUDDYICON_BYTES (200 * 1024)
33 static void update_buddy_metadata(JabberStream
*js
, const char *from
, PurpleXmlNode
*items
);
35 void jabber_avatar_init(void)
37 jabber_add_feature(NS_AVATAR_1_1_METADATA
,
38 jabber_pep_namespace_only_when_pep_enabled_cb
);
39 jabber_add_feature(NS_AVATAR_1_1_DATA
,
40 jabber_pep_namespace_only_when_pep_enabled_cb
);
42 jabber_pep_register_handler(NS_AVATAR_1_1_METADATA
,
43 update_buddy_metadata
);
47 remove_avatar_0_12_nodes(JabberStream
*js
)
50 /* See note below for why this is #if 0'd */
52 /* Publish an empty avatar according to the XEP-0084 v0.12 semantics */
53 PurpleXmlNode
*publish
, *item
, *metadata
;
54 /* publish the metadata */
55 publish
= purple_xmlnode_new("publish");
56 purple_xmlnode_set_attrib(publish
, "node", NS_AVATAR_0_12_METADATA
);
58 item
= purple_xmlnode_new_child(publish
, "item");
59 purple_xmlnode_set_attrib(item
, "id", "stop");
61 metadata
= purple_xmlnode_new_child(item
, "metadata");
62 purple_xmlnode_set_namespace(metadata
, NS_AVATAR_0_12_METADATA
);
64 purple_xmlnode_new_child(metadata
, "stop");
67 jabber_pep_publish(js
, publish
);
71 * This causes ejabberd 2.0.0 to kill the connection unceremoniously.
72 * See https://support.process-one.net/browse/EJAB-623. When adiumx.com
73 * was upgraded, the issue went away.
75 * I think it makes a lot of sense to not have an avatar at the old
76 * node instead of having something interpreted as "no avatar". When
77 * a contact with an older client logs in, in the latter situation,
78 * there's a race between interpreting the <presence/> vcard-temp:x:update
79 * avatar (non-empty) and the XEP-0084 v0.12 avatar (empty, so show no
80 * avatar for the buddy) which leads to unhappy and confused users.
82 * A deluge of frustrating "Read error" bug reports may change my mind
86 jabber_pep_delete_node(js
, NS_AVATAR_0_12_METADATA
);
87 jabber_pep_delete_node(js
, NS_AVATAR_0_12_DATA
);
90 void jabber_avatar_set(JabberStream
*js
, PurpleImage
*img
)
92 PurpleXmlNode
*publish
, *metadata
, *item
;
97 /* Hmmm, not sure if this is worth the traffic, but meh */
98 remove_avatar_0_12_nodes(js
);
101 publish
= purple_xmlnode_new("publish");
102 purple_xmlnode_set_attrib(publish
, "node", NS_AVATAR_1_1_METADATA
);
104 item
= purple_xmlnode_new_child(publish
, "item");
105 metadata
= purple_xmlnode_new_child(item
, "metadata");
106 purple_xmlnode_set_namespace(metadata
, NS_AVATAR_1_1_METADATA
);
109 jabber_pep_publish(js
, publish
);
112 * TODO: This is pretty gross. The Jabber protocol really shouldn't
113 * do voodoo to try to determine the image type, height
116 /* A PNG header, including the IHDR, but nothing else */
117 /* ATTN: this is in network byte order! */
119 guchar signature
[8]; /* must be hex 89 50 4E 47 0D 0A 1A 0A */
121 guint32 length
; /* must be 0x0d */
122 guchar type
[4]; /* must be 'I' 'H' 'D' 'R' */
133 if (purple_image_get_size(img
) > sizeof(*png
))
134 png
= purple_image_get_data(img
);
136 /* check if the data is a valid png file (well, at least to some extent) */
137 if(png
&& png
->signature
[0] == 0x89 &&
138 png
->signature
[1] == 0x50 &&
139 png
->signature
[2] == 0x4e &&
140 png
->signature
[3] == 0x47 &&
141 png
->signature
[4] == 0x0d &&
142 png
->signature
[5] == 0x0a &&
143 png
->signature
[6] == 0x1a &&
144 png
->signature
[7] == 0x0a &&
145 ntohl(png
->ihdr
.length
) == 0x0d &&
146 png
->ihdr
.type
[0] == 'I' &&
147 png
->ihdr
.type
[1] == 'H' &&
148 png
->ihdr
.type
[2] == 'D' &&
149 png
->ihdr
.type
[3] == 'R') {
150 /* parse PNG header to get the size of the image (yes, this is required) */
151 guint32 width
= ntohl(png
->ihdr
.width
);
152 guint32 height
= ntohl(png
->ihdr
.height
);
153 PurpleXmlNode
*data
, *info
;
154 char *lengthstring
, *widthstring
, *heightstring
;
156 /* compute the sha1 hash */
157 char *hash
= jabber_calculate_data_hash(
158 purple_image_get_data(img
),
159 purple_image_get_size(img
), "sha1");
160 char *base64avatar
= purple_base64_encode(
161 purple_image_get_data(img
),
162 purple_image_get_size(img
));
164 publish
= purple_xmlnode_new("publish");
165 purple_xmlnode_set_attrib(publish
, "node", NS_AVATAR_1_1_DATA
);
167 item
= purple_xmlnode_new_child(publish
, "item");
168 purple_xmlnode_set_attrib(item
, "id", hash
);
170 data
= purple_xmlnode_new_child(item
, "data");
171 purple_xmlnode_set_namespace(data
, NS_AVATAR_1_1_DATA
);
173 purple_xmlnode_insert_data(data
, base64avatar
, -1);
174 /* publish the avatar itself */
175 jabber_pep_publish(js
, publish
);
177 g_free(base64avatar
);
179 lengthstring
= g_strdup_printf("%" G_GSIZE_FORMAT
,
180 purple_image_get_size(img
));
181 widthstring
= g_strdup_printf("%u", width
);
182 heightstring
= g_strdup_printf("%u", height
);
184 /* publish the metadata */
185 publish
= purple_xmlnode_new("publish");
186 purple_xmlnode_set_attrib(publish
, "node", NS_AVATAR_1_1_METADATA
);
188 item
= purple_xmlnode_new_child(publish
, "item");
189 purple_xmlnode_set_attrib(item
, "id", hash
);
191 metadata
= purple_xmlnode_new_child(item
, "metadata");
192 purple_xmlnode_set_namespace(metadata
, NS_AVATAR_1_1_METADATA
);
194 info
= purple_xmlnode_new_child(metadata
, "info");
195 purple_xmlnode_set_attrib(info
, "id", hash
);
196 purple_xmlnode_set_attrib(info
, "type", "image/png");
197 purple_xmlnode_set_attrib(info
, "bytes", lengthstring
);
198 purple_xmlnode_set_attrib(info
, "width", widthstring
);
199 purple_xmlnode_set_attrib(info
, "height", heightstring
);
201 jabber_pep_publish(js
, publish
);
203 g_free(lengthstring
);
205 g_free(heightstring
);
208 purple_debug_error("jabber", "Cannot set PEP avatar to non-PNG data\n");
214 do_got_own_avatar_0_12_cb(JabberStream
*js
, const char *from
, PurpleXmlNode
*items
)
217 /* It wasn't an error (i.e. 'item-not-found') */
218 remove_avatar_0_12_nodes(js
);
222 do_got_own_avatar_cb(JabberStream
*js
, const char *from
, PurpleXmlNode
*items
)
224 PurpleXmlNode
*item
= NULL
, *metadata
= NULL
, *info
= NULL
;
225 PurpleAccount
*account
= purple_connection_get_account(js
->gc
);
226 const char *server_hash
= NULL
;
228 if (items
&& (item
= purple_xmlnode_get_child(items
, "item")) &&
229 (metadata
= purple_xmlnode_get_child(item
, "metadata")) &&
230 (info
= purple_xmlnode_get_child(metadata
, "info"))) {
231 server_hash
= purple_xmlnode_get_attrib(info
, "id");
235 * If we have an avatar and the server returned an error/malformed data,
236 * push our avatar. If the server avatar doesn't match the local one, push
239 if ((!items
|| !metadata
) ||
240 !purple_strequal(server_hash
, js
->initial_avatar_hash
)) {
241 PurpleImage
*img
= purple_buddy_icons_find_account_icon(account
);
242 jabber_avatar_set(js
, img
);
248 void jabber_avatar_fetch_mine(JabberStream
*js
)
250 if (js
->initial_avatar_hash
) {
251 jabber_pep_request_item(js
, NULL
, NS_AVATAR_0_12_METADATA
, NULL
,
252 do_got_own_avatar_0_12_cb
);
253 jabber_pep_request_item(js
, NULL
, NS_AVATAR_1_1_METADATA
, NULL
,
254 do_got_own_avatar_cb
);
258 typedef struct _JabberBuddyAvatarUpdateURLInfo
{
262 } JabberBuddyAvatarUpdateURLInfo
;
265 do_buddy_avatar_update_fromurl(PurpleHttpConnection
*http_conn
,
266 PurpleHttpResponse
*response
, gpointer _info
)
268 JabberBuddyAvatarUpdateURLInfo
*info
= _info
;
270 const gchar
*got_data
;
273 if (!purple_http_response_is_successful(response
)) {
274 purple_debug_error("jabber", "do_buddy_avatar_update_fromurl "
276 purple_http_response_get_error(response
));
280 got_data
= purple_http_response_get_data(response
, &got_len
);
281 icon_data
= g_memdup(got_data
, got_len
);
282 purple_buddy_icons_set_for_user(purple_connection_get_account(info
->js
->gc
), info
->from
, icon_data
, got_len
, info
->id
);
291 do_buddy_avatar_update_data(JabberStream
*js
, const char *from
, PurpleXmlNode
*items
)
293 PurpleXmlNode
*item
, *data
;
294 const char *checksum
;
301 item
= purple_xmlnode_get_child(items
, "item");
305 data
= purple_xmlnode_get_child(item
, "data");
309 checksum
= purple_xmlnode_get_attrib(item
,"id");
313 b64data
= purple_xmlnode_get_data(data
);
317 img
= purple_base64_decode(b64data
, &size
);
323 purple_buddy_icons_set_for_user(purple_connection_get_account(js
->gc
), from
, img
, size
, checksum
);
328 update_buddy_metadata(JabberStream
*js
, const char *from
, PurpleXmlNode
*items
)
330 PurpleBuddy
*buddy
= purple_blist_find_buddy(purple_connection_get_account(js
->gc
), from
);
331 const char *checksum
;
332 PurpleXmlNode
*item
, *metadata
;
339 item
= purple_xmlnode_get_child(items
,"item");
343 metadata
= purple_xmlnode_get_child(item
, "metadata");
347 checksum
= purple_buddy_icons_get_checksum_for_user(buddy
);
349 /* <stop/> was the pre-v1.1 method of publishing an empty avatar */
350 if(purple_xmlnode_get_child(metadata
, "stop")) {
351 purple_buddy_icons_set_for_user(purple_connection_get_account(js
->gc
), from
, NULL
, 0, NULL
);
353 PurpleXmlNode
*info
, *goodinfo
= NULL
;
354 gboolean has_children
= FALSE
;
356 /* iterate over all info nodes to get one we can use */
357 for(info
= metadata
->child
; info
; info
= info
->next
) {
358 if(info
->type
== PURPLE_XMLNODE_TYPE_TAG
)
360 if(info
->type
== PURPLE_XMLNODE_TYPE_TAG
&& !strcmp(info
->name
,"info")) {
361 const char *type
= purple_xmlnode_get_attrib(info
,"type");
362 const char *id
= purple_xmlnode_get_attrib(info
,"id");
364 if(checksum
&& id
&& !strcmp(id
, checksum
)) {
365 /* we already have that avatar, so we don't have to do anything */
369 /* We'll only pick the png one for now. It's a very nice image format anyways. */
370 if(type
&& id
&& !goodinfo
&& !strcmp(type
, "image/png"))
374 if(has_children
== FALSE
) {
375 purple_buddy_icons_set_for_user(purple_connection_get_account(js
->gc
), from
, NULL
, 0, NULL
);
376 } else if(goodinfo
) {
377 const char *url
= purple_xmlnode_get_attrib(goodinfo
, "url");
378 const char *id
= purple_xmlnode_get_attrib(goodinfo
,"id");
380 /* the avatar might either be stored in a pep node, or on a HTTP(S) URL */
382 jabber_pep_request_item(js
, from
, NS_AVATAR_1_1_DATA
, id
,
383 do_buddy_avatar_update_data
);
385 PurpleHttpRequest
*req
;
386 JabberBuddyAvatarUpdateURLInfo
*info
= g_new0(JabberBuddyAvatarUpdateURLInfo
, 1);
389 req
= purple_http_request_new(url
);
390 purple_http_request_set_max_len(req
, MAX_HTTP_BUDDYICON_BYTES
);
391 purple_http_connection_set_add(js
->http_conns
,
392 purple_http_request(js
->gc
, req
,
393 do_buddy_avatar_update_fromurl
, info
));
394 purple_http_request_unref(req
);
396 info
->from
= g_strdup(from
);
397 info
->id
= g_strdup(id
);