~vish/ubuntu/maverick/pidgin/bug25979

« back to all changes in this revision

Viewing changes to libpurple/protocols/jabber/useravatar.c

  • Committer: Bazaar Package Importer
  • Author(s): Felix Geyer
  • Date: 2009-10-09 19:40:26 UTC
  • mfrom: (1.4.1 upstream) (46.1.10 karmic)
  • Revision ID: james.westby@ubuntu.com-20091009194026-wbqqh0bsbz19nx5q
Tags: 1:2.6.2-1ubuntu7
* Don't stick the buddy list window to all desktops as some
  window managers have trouble to properly unstick it (LP: #346840)
  - debian/patches/11_buddy_list_really_show.patch
* Always use default tray icon size on KDE (LP: #209440)
  - debian/patches/62_tray_icon_size_kde.patch
* Use scrollbars in the preferences dialog if the screen height is
  below 700 px instead of 600 px
  - debian/patches/60_1024x600_gtkprefs.c.patch

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * purple - Jabber Protocol Plugin
 
3
 *
 
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.
 
7
 *
 
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.
 
12
 *
 
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.
 
17
 *
 
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
 
21
 *
 
22
 */
 
23
 
 
24
#include "internal.h"
 
25
 
 
26
#include "useravatar.h"
 
27
#include "pep.h"
 
28
#include "debug.h"
 
29
 
 
30
#define MAX_HTTP_BUDDYICON_BYTES (200 * 1024)
 
31
 
 
32
static void update_buddy_metadata(JabberStream *js, const char *from, xmlnode *items);
 
33
 
 
34
void jabber_avatar_init(void)
 
35
{
 
36
        jabber_add_feature(NS_AVATAR_1_1_METADATA,
 
37
                           jabber_pep_namespace_only_when_pep_enabled_cb);
 
38
        jabber_add_feature(NS_AVATAR_1_1_DATA,
 
39
                           jabber_pep_namespace_only_when_pep_enabled_cb);
 
40
 
 
41
        jabber_pep_register_handler(NS_AVATAR_1_1_METADATA,
 
42
                                    update_buddy_metadata);
 
43
}
 
44
 
 
45
static void
 
46
remove_avatar_0_12_nodes(JabberStream *js)
 
47
{
 
48
#if 0
 
49
        /* See note below for why this is #if 0'd */
 
50
 
 
51
        /* Publish an empty avatar according to the XEP-0084 v0.12 semantics */
 
52
        xmlnode *publish, *item, *metadata;
 
53
        /* publish the metadata */
 
54
        publish = xmlnode_new("publish");
 
55
        xmlnode_set_attrib(publish, "node", NS_AVATAR_0_12_METADATA);
 
56
 
 
57
        item = xmlnode_new_child(publish, "item");
 
58
        xmlnode_set_attrib(item, "id", "stop");
 
59
 
 
60
        metadata = xmlnode_new_child(item, "metadata");
 
61
        xmlnode_set_namespace(metadata, NS_AVATAR_0_12_METADATA);
 
62
 
 
63
        xmlnode_new_child(metadata, "stop");
 
64
 
 
65
        /* publish */
 
66
        jabber_pep_publish(js, publish);
 
67
#endif
 
68
 
 
69
        /*
 
70
         * This causes ejabberd 2.0.0 to kill the connection unceremoniously.
 
71
         * See https://support.process-one.net/browse/EJAB-623. When adiumx.com
 
72
         * was upgraded, the issue went away.
 
73
         *
 
74
         * I think it makes a lot of sense to not have an avatar at the old
 
75
         * node instead of having something interpreted as "no avatar". When
 
76
         * a contact with an older client logs in, in the latter situation,
 
77
         * there's a race between interpreting the <presence/> vcard-temp:x:update
 
78
         * avatar (non-empty) and the XEP-0084 v0.12 avatar (empty, so show no
 
79
         * avatar for the buddy) which leads to unhappy and confused users.
 
80
         *
 
81
         * A deluge of frustrating "Read error" bug reports may change my mind
 
82
         * about this.
 
83
         * --darkrain42
 
84
         */
 
85
        jabber_pep_delete_node(js, NS_AVATAR_0_12_METADATA);
 
86
        jabber_pep_delete_node(js, NS_AVATAR_0_12_DATA);
 
87
}
 
88
 
 
89
void jabber_avatar_set(JabberStream *js, PurpleStoredImage *img)
 
90
{
 
91
        xmlnode *publish, *metadata, *item;
 
92
 
 
93
        if (!js->pep)
 
94
                return;
 
95
 
 
96
        /* Hmmm, not sure if this is worth the traffic, but meh */
 
97
        remove_avatar_0_12_nodes(js);
 
98
 
 
99
        if (!img) {
 
100
                publish = xmlnode_new("publish");
 
101
                xmlnode_set_attrib(publish, "node", NS_AVATAR_1_1_METADATA);
 
102
 
 
103
                item = xmlnode_new_child(publish, "item");
 
104
                metadata = xmlnode_new_child(item, "metadata");
 
105
                xmlnode_set_namespace(metadata, NS_AVATAR_1_1_METADATA);
 
106
 
 
107
                /* publish */
 
108
                jabber_pep_publish(js, publish);
 
109
        } else {
 
110
                /*
 
111
                 * TODO: This is pretty gross.  The Jabber PRPL really shouldn't
 
112
                 *       do voodoo to try to determine the image type, height
 
113
                 *       and width.
 
114
                 */
 
115
                /* A PNG header, including the IHDR, but nothing else */
 
116
                const struct {
 
117
                        guchar signature[8]; /* must be hex 89 50 4E 47 0D 0A 1A 0A */
 
118
                        struct {
 
119
                                guint32 length; /* must be 0x0d */
 
120
                                guchar type[4]; /* must be 'I' 'H' 'D' 'R' */
 
121
                                guint32 width;
 
122
                                guint32 height;
 
123
                                guchar bitdepth;
 
124
                                guchar colortype;
 
125
                                guchar compression;
 
126
                                guchar filter;
 
127
                                guchar interlace;
 
128
                        } ihdr;
 
129
                } *png = purple_imgstore_get_data(img); /* ATTN: this is in network byte order! */
 
130
 
 
131
                /* check if the data is a valid png file (well, at least to some extent) */
 
132
                if(png->signature[0] == 0x89 &&
 
133
                   png->signature[1] == 0x50 &&
 
134
                   png->signature[2] == 0x4e &&
 
135
                   png->signature[3] == 0x47 &&
 
136
                   png->signature[4] == 0x0d &&
 
137
                   png->signature[5] == 0x0a &&
 
138
                   png->signature[6] == 0x1a &&
 
139
                   png->signature[7] == 0x0a &&
 
140
                   ntohl(png->ihdr.length) == 0x0d &&
 
141
                   png->ihdr.type[0] == 'I' &&
 
142
                   png->ihdr.type[1] == 'H' &&
 
143
                   png->ihdr.type[2] == 'D' &&
 
144
                   png->ihdr.type[3] == 'R') {
 
145
                        /* parse PNG header to get the size of the image (yes, this is required) */
 
146
                        guint32 width = ntohl(png->ihdr.width);
 
147
                        guint32 height = ntohl(png->ihdr.height);
 
148
                        xmlnode *data, *info;
 
149
                        char *lengthstring, *widthstring, *heightstring;
 
150
 
 
151
                        /* compute the sha1 hash */
 
152
                        char *hash = jabber_calculate_data_sha1sum(purple_imgstore_get_data(img),
 
153
                                                                   purple_imgstore_get_size(img));
 
154
                        char *base64avatar = purple_base64_encode(purple_imgstore_get_data(img),
 
155
                                                                  purple_imgstore_get_size(img));
 
156
 
 
157
                        publish = xmlnode_new("publish");
 
158
                        xmlnode_set_attrib(publish, "node", NS_AVATAR_1_1_DATA);
 
159
 
 
160
                        item = xmlnode_new_child(publish, "item");
 
161
                        xmlnode_set_attrib(item, "id", hash);
 
162
 
 
163
                        data = xmlnode_new_child(item, "data");
 
164
                        xmlnode_set_namespace(data, NS_AVATAR_1_1_DATA);
 
165
 
 
166
                        xmlnode_insert_data(data, base64avatar, -1);
 
167
                        /* publish the avatar itself */
 
168
                        jabber_pep_publish(js, publish);
 
169
 
 
170
                        g_free(base64avatar);
 
171
 
 
172
                        lengthstring = g_strdup_printf("%" G_GSIZE_FORMAT,
 
173
                                                       purple_imgstore_get_size(img));
 
174
                        widthstring = g_strdup_printf("%u", width);
 
175
                        heightstring = g_strdup_printf("%u", height);
 
176
 
 
177
                        /* publish the metadata */
 
178
                        publish = xmlnode_new("publish");
 
179
                        xmlnode_set_attrib(publish, "node", NS_AVATAR_1_1_METADATA);
 
180
 
 
181
                        item = xmlnode_new_child(publish, "item");
 
182
                        xmlnode_set_attrib(item, "id", hash);
 
183
 
 
184
                        metadata = xmlnode_new_child(item, "metadata");
 
185
                        xmlnode_set_namespace(metadata, NS_AVATAR_1_1_METADATA);
 
186
 
 
187
                        info = xmlnode_new_child(metadata, "info");
 
188
                        xmlnode_set_attrib(info, "id", hash);
 
189
                        xmlnode_set_attrib(info, "type", "image/png");
 
190
                        xmlnode_set_attrib(info, "bytes", lengthstring);
 
191
                        xmlnode_set_attrib(info, "width", widthstring);
 
192
                        xmlnode_set_attrib(info, "height", heightstring);
 
193
 
 
194
                        jabber_pep_publish(js, publish);
 
195
 
 
196
                        g_free(lengthstring);
 
197
                        g_free(widthstring);
 
198
                        g_free(heightstring);
 
199
                        g_free(hash);
 
200
                } else {
 
201
                        purple_debug_error("jabber", "Cannot set PEP avatar to non-PNG data\n");
 
202
                }
 
203
        }
 
204
}
 
205
 
 
206
static void
 
207
do_got_own_avatar_0_12_cb(JabberStream *js, const char *from, xmlnode *items)
 
208
{
 
209
        if (items)
 
210
                /* It wasn't an error (i.e. 'item-not-found') */
 
211
                remove_avatar_0_12_nodes(js);
 
212
}
 
213
 
 
214
static void
 
215
do_got_own_avatar_cb(JabberStream *js, const char *from, xmlnode *items)
 
216
{
 
217
        xmlnode *item = NULL, *metadata = NULL, *info = NULL;
 
218
        PurpleAccount *account = purple_connection_get_account(js->gc);
 
219
        const char *server_hash = NULL;
 
220
 
 
221
        if (items && (item = xmlnode_get_child(items, "item")) &&
 
222
                        (metadata = xmlnode_get_child(item, "metadata")) &&
 
223
                        (info = xmlnode_get_child(metadata, "info"))) {
 
224
                server_hash = xmlnode_get_attrib(info, "id");
 
225
        }
 
226
 
 
227
        /*
 
228
         * If we have an avatar and the server returned an error/malformed data,
 
229
         * push our avatar. If the server avatar doesn't match the local one, push
 
230
         * our avatar.
 
231
         */
 
232
        if (((!items || !metadata) && js->initial_avatar_hash) ||
 
233
                        !purple_strequal(server_hash, js->initial_avatar_hash)) {
 
234
                PurpleStoredImage *img = purple_buddy_icons_find_account_icon(account);
 
235
                jabber_avatar_set(js, img);
 
236
                purple_imgstore_unref(img);
 
237
        }
 
238
}
 
239
 
 
240
void jabber_avatar_fetch_mine(JabberStream *js)
 
241
{
 
242
        char *jid = g_strdup_printf("%s@%s", js->user->node, js->user->domain);
 
243
        jabber_pep_request_item(js, jid, NS_AVATAR_0_12_METADATA, NULL,
 
244
                                do_got_own_avatar_0_12_cb);
 
245
        jabber_pep_request_item(js, jid, NS_AVATAR_1_1_METADATA, NULL,
 
246
                                do_got_own_avatar_cb);
 
247
        g_free(jid);
 
248
}
 
249
 
 
250
typedef struct _JabberBuddyAvatarUpdateURLInfo {
 
251
        JabberStream *js;
 
252
        char *from;
 
253
        char *id;
 
254
} JabberBuddyAvatarUpdateURLInfo;
 
255
 
 
256
static void
 
257
do_buddy_avatar_update_fromurl(PurpleUtilFetchUrlData *url_data,
 
258
                               gpointer user_data, const gchar *url_text,
 
259
                               gsize len, const gchar *error_message)
 
260
{
 
261
        JabberBuddyAvatarUpdateURLInfo *info = user_data;
 
262
        gpointer icon_data;
 
263
 
 
264
        if(!url_text) {
 
265
                purple_debug(PURPLE_DEBUG_ERROR, "jabber",
 
266
                             "do_buddy_avatar_update_fromurl got error \"%s\"",
 
267
                             error_message);
 
268
                goto out;
 
269
        }
 
270
 
 
271
        icon_data = g_memdup(url_text, len);
 
272
        purple_buddy_icons_set_for_user(purple_connection_get_account(info->js->gc), info->from, icon_data, len, info->id);
 
273
 
 
274
out:
 
275
        g_free(info->from);
 
276
        g_free(info->id);
 
277
        g_free(info);
 
278
}
 
279
 
 
280
static void
 
281
do_buddy_avatar_update_data(JabberStream *js, const char *from, xmlnode *items)
 
282
{
 
283
        xmlnode *item, *data;
 
284
        const char *checksum;
 
285
        char *b64data;
 
286
        void *img;
 
287
        size_t size;
 
288
        if(!items)
 
289
                return;
 
290
 
 
291
        item = xmlnode_get_child(items, "item");
 
292
        if(!item)
 
293
                return;
 
294
 
 
295
        data = xmlnode_get_child(item, "data");
 
296
        if(!data)
 
297
                return;
 
298
 
 
299
        checksum = xmlnode_get_attrib(item,"id");
 
300
        if(!checksum)
 
301
                return;
 
302
 
 
303
        b64data = xmlnode_get_data(data);
 
304
        if(!b64data)
 
305
                return;
 
306
 
 
307
        img = purple_base64_decode(b64data, &size);
 
308
        if(!img) {
 
309
                g_free(b64data);
 
310
                return;
 
311
        }
 
312
 
 
313
        purple_buddy_icons_set_for_user(purple_connection_get_account(js->gc), from, img, size, checksum);
 
314
        g_free(b64data);
 
315
}
 
316
 
 
317
static void
 
318
update_buddy_metadata(JabberStream *js, const char *from, xmlnode *items)
 
319
{
 
320
        PurpleBuddy *buddy = purple_find_buddy(purple_connection_get_account(js->gc), from);
 
321
        const char *checksum;
 
322
        xmlnode *item, *metadata;
 
323
        if(!buddy)
 
324
                return;
 
325
 
 
326
        if (!items)
 
327
                return;
 
328
 
 
329
        item = xmlnode_get_child(items,"item");
 
330
        if (!item)
 
331
                return;
 
332
 
 
333
        metadata = xmlnode_get_child(item, "metadata");
 
334
        if(!metadata)
 
335
                return;
 
336
 
 
337
        checksum = purple_buddy_icons_get_checksum_for_user(buddy);
 
338
 
 
339
        /* <stop/> was the pre-v1.1 method of publishing an empty avatar */
 
340
        if(xmlnode_get_child(metadata, "stop")) {
 
341
                purple_buddy_icons_set_for_user(purple_connection_get_account(js->gc), from, NULL, 0, NULL);
 
342
        } else {
 
343
                xmlnode *info, *goodinfo = NULL;
 
344
                gboolean has_children = FALSE;
 
345
 
 
346
                /* iterate over all info nodes to get one we can use */
 
347
                for(info = metadata->child; info; info = info->next) {
 
348
                        if(info->type == XMLNODE_TYPE_TAG)
 
349
                                has_children = TRUE;
 
350
                        if(info->type == XMLNODE_TYPE_TAG && !strcmp(info->name,"info")) {
 
351
                                const char *type = xmlnode_get_attrib(info,"type");
 
352
                                const char *id = xmlnode_get_attrib(info,"id");
 
353
 
 
354
                                if(checksum && id && !strcmp(id, checksum)) {
 
355
                                        /* we already have that avatar, so we don't have to do anything */
 
356
                                        goodinfo = NULL;
 
357
                                        break;
 
358
                                }
 
359
                                /* We'll only pick the png one for now. It's a very nice image format anyways. */
 
360
                                if(type && id && !goodinfo && !strcmp(type, "image/png"))
 
361
                                        goodinfo = info;
 
362
                        }
 
363
                }
 
364
                if(has_children == FALSE) {
 
365
                        purple_buddy_icons_set_for_user(purple_connection_get_account(js->gc), from, NULL, 0, NULL);
 
366
                } else if(goodinfo) {
 
367
                        const char *url = xmlnode_get_attrib(goodinfo, "url");
 
368
                        const char *id = xmlnode_get_attrib(goodinfo,"id");
 
369
 
 
370
                        /* the avatar might either be stored in a pep node, or on a HTTP(S) URL */
 
371
                        if(!url) {
 
372
                                jabber_pep_request_item(js, from, NS_AVATAR_1_1_DATA, id,
 
373
                                                        do_buddy_avatar_update_data);
 
374
                        } else {
 
375
                                PurpleUtilFetchUrlData *url_data;
 
376
                                JabberBuddyAvatarUpdateURLInfo *info = g_new0(JabberBuddyAvatarUpdateURLInfo, 1);
 
377
                                info->js = js;
 
378
 
 
379
                                url_data = purple_util_fetch_url_len(url, TRUE, NULL, TRUE,
 
380
                                                                                  MAX_HTTP_BUDDYICON_BYTES,
 
381
                                                                                  do_buddy_avatar_update_fromurl, info);
 
382
                                if (url_data) {
 
383
                                        info->from = g_strdup(from);
 
384
                                        info->id = g_strdup(id);
 
385
                                        js->url_datas = g_slist_prepend(js->url_datas, url_data);
 
386
                                } else
 
387
                                        g_free(info);
 
388
 
 
389
                        }
 
390
                }
 
391
        }
 
392
}