rename accountopt.[ch] to purpleaccountoption.[ch]
[pidgin-git.git] / libpurple / protocols / jabber / useravatar.c
blob9db3dc9af6bf8bd748fdf53fae14ffc2d2b6bba0
1 /*
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
6 * source distribution.
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
24 #include "internal.h"
26 #include "http.h"
27 #include "useravatar.h"
28 #include "pep.h"
29 #include "debug.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);
46 static void
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
62 * about this.
63 * --darkrain42
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;
73 if (!js->pep)
74 return;
76 /* Hmmm, not sure if this is worth the traffic, but meh */
77 remove_avatar_0_12_nodes(js);
79 if (!img) {
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);
87 /* publish */
88 jabber_pep_publish(js, publish);
89 } else {
91 * TODO: This is pretty gross. The Jabber protocol really shouldn't
92 * do voodoo to try to determine the image type, height
93 * and width.
95 /* A PNG header, including the IHDR, but nothing else */
96 /* ATTN: this is in network byte order! */
97 const struct {
98 guchar signature[8]; /* must be hex 89 50 4E 47 0D 0A 1A 0A */
99 struct {
100 guint32 length; /* must be 0x0d */
101 guchar type[4]; /* must be 'I' 'H' 'D' 'R' */
102 guint32 width;
103 guint32 height;
104 guchar bitdepth;
105 guchar colortype;
106 guchar compression;
107 guchar filter;
108 guchar interlace;
109 } ihdr;
110 } *png = NULL;
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(
137 G_CHECKSUM_SHA1,
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);
184 g_free(widthstring);
185 g_free(heightstring);
186 g_free(hash);
187 } else {
188 purple_debug_error("jabber", "Cannot set PEP avatar to non-PNG data\n");
193 static void
194 do_got_own_avatar_0_12_cb(JabberStream *js, const char *from, PurpleXmlNode *items)
196 if (items)
197 /* It wasn't an error (i.e. 'item-not-found') */
198 remove_avatar_0_12_nodes(js);
201 static void
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
217 * our avatar.
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);
223 if (img)
224 g_object_unref(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);
238 typedef struct {
239 JabberStream *js;
240 char *from;
241 char *id;
242 } JabberBuddyAvatarUpdateURLInfo;
244 static void
245 do_buddy_avatar_update_fromurl(PurpleHttpConnection *http_conn,
246 PurpleHttpResponse *response, gpointer _info)
248 JabberBuddyAvatarUpdateURLInfo *info = _info;
249 gpointer icon_data;
250 const gchar *got_data;
251 size_t got_len;
253 if (!purple_http_response_is_successful(response)) {
254 purple_debug_error("jabber", "do_buddy_avatar_update_fromurl "
255 "got error \"%s\"",
256 purple_http_response_get_error(response));
257 goto out;
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);
264 out:
265 g_free(info->from);
266 g_free(info->id);
267 g_free(info);
270 static void
271 do_buddy_avatar_update_data(JabberStream *js, const char *from, PurpleXmlNode *items)
273 PurpleXmlNode *item, *data;
274 const char *checksum;
275 char *b64data;
276 void *img;
277 size_t size;
278 if(!items)
279 return;
281 item = purple_xmlnode_get_child(items, "item");
282 if(!item)
283 return;
285 data = purple_xmlnode_get_child(item, "data");
286 if(!data)
287 return;
289 checksum = purple_xmlnode_get_attrib(item,"id");
290 if(!checksum)
291 return;
293 b64data = purple_xmlnode_get_data(data);
294 if(!b64data)
295 return;
297 img = g_base64_decode(b64data, &size);
298 if(!img) {
299 g_free(b64data);
300 return;
303 purple_buddy_icons_set_for_user(purple_connection_get_account(js->gc), from, img, size, checksum);
304 g_free(b64data);
307 static void
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;
313 if(!buddy)
314 return;
316 if (!items)
317 return;
319 item = purple_xmlnode_get_child(items,"item");
320 if (!item)
321 return;
323 metadata = purple_xmlnode_get_child(item, "metadata");
324 if(!metadata)
325 return;
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);
332 } else {
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)
339 has_children = TRUE;
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 */
346 goodinfo = NULL;
347 break;
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"))
351 goodinfo = info;
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 */
361 if(!url) {
362 jabber_pep_request_item(js, from, NS_AVATAR_1_1_DATA, id,
363 do_buddy_avatar_update_data);
364 } else {
365 PurpleHttpRequest *req;
366 JabberBuddyAvatarUpdateURLInfo *info = g_new0(JabberBuddyAvatarUpdateURLInfo, 1);
367 info->js = js;
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);