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 * This causes ejabberd 2.0.0 to kill the connection unceremoniously.
51 * See https://support.process-one.net/browse/EJAB-623. When adiumx.com
52 * was upgraded, the issue went away.
54 * I think it makes a lot of sense to not have an avatar at the old
55 * node instead of having something interpreted as "no avatar". When
56 * a contact with an older client logs in, in the latter situation,
57 * there's a race between interpreting the <presence/> vcard-temp:x:update
58 * avatar (non-empty) and the XEP-0084 v0.12 avatar (empty, so show no
59 * avatar for the buddy) which leads to unhappy and confused users.
61 * A deluge of frustrating "Read error" bug reports may change my mind
65 jabber_pep_delete_node(js
, NS_AVATAR_0_12_METADATA
);
66 jabber_pep_delete_node(js
, NS_AVATAR_0_12_DATA
);
69 void jabber_avatar_set(JabberStream
*js
, PurpleImage
*img
)
71 PurpleXmlNode
*publish
, *metadata
, *item
;
76 /* Hmmm, not sure if this is worth the traffic, but meh */
77 remove_avatar_0_12_nodes(js
);
80 publish
= purple_xmlnode_new("publish");
81 purple_xmlnode_set_attrib(publish
, "node", NS_AVATAR_1_1_METADATA
);
83 item
= purple_xmlnode_new_child(publish
, "item");
84 metadata
= purple_xmlnode_new_child(item
, "metadata");
85 purple_xmlnode_set_namespace(metadata
, NS_AVATAR_1_1_METADATA
);
88 jabber_pep_publish(js
, publish
);
91 * TODO: This is pretty gross. The Jabber protocol really shouldn't
92 * do voodoo to try to determine the image type, height
95 /* A PNG header, including the IHDR, but nothing else */
96 /* ATTN: this is in network byte order! */
98 guchar signature
[8]; /* must be hex 89 50 4E 47 0D 0A 1A 0A */
100 guint32 length
; /* must be 0x0d */
101 guchar type
[4]; /* must be 'I' 'H' 'D' 'R' */
112 if (purple_image_get_data_size(img
) > sizeof(*png
))
113 png
= purple_image_get_data(img
);
115 /* check if the data is a valid png file (well, at least to some extent) */
116 if(png
&& png
->signature
[0] == 0x89 &&
117 png
->signature
[1] == 0x50 &&
118 png
->signature
[2] == 0x4e &&
119 png
->signature
[3] == 0x47 &&
120 png
->signature
[4] == 0x0d &&
121 png
->signature
[5] == 0x0a &&
122 png
->signature
[6] == 0x1a &&
123 png
->signature
[7] == 0x0a &&
124 ntohl(png
->ihdr
.length
) == 0x0d &&
125 png
->ihdr
.type
[0] == 'I' &&
126 png
->ihdr
.type
[1] == 'H' &&
127 png
->ihdr
.type
[2] == 'D' &&
128 png
->ihdr
.type
[3] == 'R') {
129 /* parse PNG header to get the size of the image (yes, this is required) */
130 guint32 width
= ntohl(png
->ihdr
.width
);
131 guint32 height
= ntohl(png
->ihdr
.height
);
132 PurpleXmlNode
*data
, *info
;
133 char *lengthstring
, *widthstring
, *heightstring
;
135 /* compute the sha1 hash */
136 char *hash
= g_compute_checksum_for_data(
138 purple_image_get_data(img
),
139 purple_image_get_data_size(img
));
140 char *base64avatar
= g_base64_encode(
141 purple_image_get_data(img
),
142 purple_image_get_data_size(img
));
144 publish
= purple_xmlnode_new("publish");
145 purple_xmlnode_set_attrib(publish
, "node", NS_AVATAR_1_1_DATA
);
147 item
= purple_xmlnode_new_child(publish
, "item");
148 purple_xmlnode_set_attrib(item
, "id", hash
);
150 data
= purple_xmlnode_new_child(item
, "data");
151 purple_xmlnode_set_namespace(data
, NS_AVATAR_1_1_DATA
);
153 purple_xmlnode_insert_data(data
, base64avatar
, -1);
154 /* publish the avatar itself */
155 jabber_pep_publish(js
, publish
);
157 g_free(base64avatar
);
159 lengthstring
= g_strdup_printf("%" G_GSIZE_FORMAT
,
160 purple_image_get_data_size(img
));
161 widthstring
= g_strdup_printf("%u", width
);
162 heightstring
= g_strdup_printf("%u", height
);
164 /* publish the metadata */
165 publish
= purple_xmlnode_new("publish");
166 purple_xmlnode_set_attrib(publish
, "node", NS_AVATAR_1_1_METADATA
);
168 item
= purple_xmlnode_new_child(publish
, "item");
169 purple_xmlnode_set_attrib(item
, "id", hash
);
171 metadata
= purple_xmlnode_new_child(item
, "metadata");
172 purple_xmlnode_set_namespace(metadata
, NS_AVATAR_1_1_METADATA
);
174 info
= purple_xmlnode_new_child(metadata
, "info");
175 purple_xmlnode_set_attrib(info
, "id", hash
);
176 purple_xmlnode_set_attrib(info
, "type", "image/png");
177 purple_xmlnode_set_attrib(info
, "bytes", lengthstring
);
178 purple_xmlnode_set_attrib(info
, "width", widthstring
);
179 purple_xmlnode_set_attrib(info
, "height", heightstring
);
181 jabber_pep_publish(js
, publish
);
183 g_free(lengthstring
);
185 g_free(heightstring
);
188 purple_debug_error("jabber", "Cannot set PEP avatar to non-PNG data\n");
194 do_got_own_avatar_0_12_cb(JabberStream
*js
, const char *from
, PurpleXmlNode
*items
)
197 /* It wasn't an error (i.e. 'item-not-found') */
198 remove_avatar_0_12_nodes(js
);
202 do_got_own_avatar_cb(JabberStream
*js
, const char *from
, PurpleXmlNode
*items
)
204 PurpleXmlNode
*item
= NULL
, *metadata
= NULL
, *info
= NULL
;
205 PurpleAccount
*account
= purple_connection_get_account(js
->gc
);
206 const char *server_hash
= NULL
;
208 if (items
&& (item
= purple_xmlnode_get_child(items
, "item")) &&
209 (metadata
= purple_xmlnode_get_child(item
, "metadata")) &&
210 (info
= purple_xmlnode_get_child(metadata
, "info"))) {
211 server_hash
= purple_xmlnode_get_attrib(info
, "id");
215 * If we have an avatar and the server returned an error/malformed data,
216 * push our avatar. If the server avatar doesn't match the local one, push
219 if ((!items
|| !metadata
) ||
220 !purple_strequal(server_hash
, js
->initial_avatar_hash
)) {
221 PurpleImage
*img
= purple_buddy_icons_find_account_icon(account
);
222 jabber_avatar_set(js
, img
);
228 void jabber_avatar_fetch_mine(JabberStream
*js
)
230 if (js
->initial_avatar_hash
) {
231 jabber_pep_request_item(js
, NULL
, NS_AVATAR_0_12_METADATA
, NULL
,
232 do_got_own_avatar_0_12_cb
);
233 jabber_pep_request_item(js
, NULL
, NS_AVATAR_1_1_METADATA
, NULL
,
234 do_got_own_avatar_cb
);
242 } JabberBuddyAvatarUpdateURLInfo
;
245 do_buddy_avatar_update_fromurl(PurpleHttpConnection
*http_conn
,
246 PurpleHttpResponse
*response
, gpointer _info
)
248 JabberBuddyAvatarUpdateURLInfo
*info
= _info
;
250 const gchar
*got_data
;
253 if (!purple_http_response_is_successful(response
)) {
254 purple_debug_error("jabber", "do_buddy_avatar_update_fromurl "
256 purple_http_response_get_error(response
));
260 got_data
= purple_http_response_get_data(response
, &got_len
);
261 icon_data
= g_memdup(got_data
, got_len
);
262 purple_buddy_icons_set_for_user(purple_connection_get_account(info
->js
->gc
), info
->from
, icon_data
, got_len
, info
->id
);
271 do_buddy_avatar_update_data(JabberStream
*js
, const char *from
, PurpleXmlNode
*items
)
273 PurpleXmlNode
*item
, *data
;
274 const char *checksum
;
281 item
= purple_xmlnode_get_child(items
, "item");
285 data
= purple_xmlnode_get_child(item
, "data");
289 checksum
= purple_xmlnode_get_attrib(item
,"id");
293 b64data
= purple_xmlnode_get_data(data
);
297 img
= g_base64_decode(b64data
, &size
);
303 purple_buddy_icons_set_for_user(purple_connection_get_account(js
->gc
), from
, img
, size
, checksum
);
308 update_buddy_metadata(JabberStream
*js
, const char *from
, PurpleXmlNode
*items
)
310 PurpleBuddy
*buddy
= purple_blist_find_buddy(purple_connection_get_account(js
->gc
), from
);
311 const char *checksum
;
312 PurpleXmlNode
*item
, *metadata
;
319 item
= purple_xmlnode_get_child(items
,"item");
323 metadata
= purple_xmlnode_get_child(item
, "metadata");
327 checksum
= purple_buddy_icons_get_checksum_for_user(buddy
);
329 /* <stop/> was the pre-v1.1 method of publishing an empty avatar */
330 if(purple_xmlnode_get_child(metadata
, "stop")) {
331 purple_buddy_icons_set_for_user(purple_connection_get_account(js
->gc
), from
, NULL
, 0, NULL
);
333 PurpleXmlNode
*info
, *goodinfo
= NULL
;
334 gboolean has_children
= FALSE
;
336 /* iterate over all info nodes to get one we can use */
337 for(info
= metadata
->child
; info
; info
= info
->next
) {
338 if(info
->type
== PURPLE_XMLNODE_TYPE_TAG
)
340 if(info
->type
== PURPLE_XMLNODE_TYPE_TAG
&& purple_strequal(info
->name
,"info")) {
341 const char *type
= purple_xmlnode_get_attrib(info
,"type");
342 const char *id
= purple_xmlnode_get_attrib(info
,"id");
344 if(checksum
&& id
&& purple_strequal(id
, checksum
)) {
345 /* we already have that avatar, so we don't have to do anything */
349 /* We'll only pick the png one for now. It's a very nice image format anyways. */
350 if(id
&& !goodinfo
&& purple_strequal(type
, "image/png"))
354 if(has_children
== FALSE
) {
355 purple_buddy_icons_set_for_user(purple_connection_get_account(js
->gc
), from
, NULL
, 0, NULL
);
356 } else if(goodinfo
) {
357 const char *url
= purple_xmlnode_get_attrib(goodinfo
, "url");
358 const char *id
= purple_xmlnode_get_attrib(goodinfo
,"id");
360 /* the avatar might either be stored in a pep node, or on a HTTP(S) URL */
362 jabber_pep_request_item(js
, from
, NS_AVATAR_1_1_DATA
, id
,
363 do_buddy_avatar_update_data
);
365 PurpleHttpRequest
*req
;
366 JabberBuddyAvatarUpdateURLInfo
*info
= g_new0(JabberBuddyAvatarUpdateURLInfo
, 1);
369 req
= purple_http_request_new(url
);
370 purple_http_request_set_max_len(req
, MAX_HTTP_BUDDYICON_BYTES
);
371 purple_http_connection_set_add(js
->http_conns
,
372 purple_http_request(js
->gc
, req
,
373 do_buddy_avatar_update_fromurl
, info
));
374 purple_http_request_unref(req
);
376 info
->from
= g_strdup(from
);
377 info
->id
= g_strdup(id
);