Mercurial > pidgin.yaz
comparison libpurple/protocols/jabber/useravatar.c @ 25815:5dd25c58b65e
Migrate the XMPP User Avatar (XEP-0084) code to its own file
I also slightly rearranged the ordering of jabber_avatar_set to make the
error cases slightly more apparent.
author | Paul Aurich <paul@darkrain42.org> |
---|---|
date | Wed, 21 Jan 2009 17:55:09 +0000 |
parents | |
children | b68ac693ae2d |
comparison
equal
deleted
inserted
replaced
24981:7f9b8351a6b4 | 25815:5dd25c58b65e |
---|---|
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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("avatarmeta", AVATARNAMESPACEMETA, | |
37 jabber_pep_namespace_only_when_pep_enabled_cb); | |
38 jabber_add_feature("avatardata", AVATARNAMESPACEMETA, | |
39 jabber_pep_namespace_only_when_pep_enabled_cb); | |
40 | |
41 jabber_pep_register_handler("avatar", AVATARNAMESPACEMETA, | |
42 update_buddy_metadata); | |
43 } | |
44 | |
45 void jabber_avatar_set(JabberStream *js, PurpleStoredImage *img) | |
46 { | |
47 xmlnode *publish, *metadata, *item; | |
48 | |
49 if (!js->pep) | |
50 return; | |
51 | |
52 if (!img) { | |
53 /* remove the metadata */ | |
54 publish = xmlnode_new("publish"); | |
55 xmlnode_set_attrib(publish, "node", AVATARNAMESPACEMETA); | |
56 | |
57 item = xmlnode_new_child(publish, "item"); | |
58 metadata = xmlnode_new_child(item, "metadata"); | |
59 xmlnode_set_namespace(metadata, AVATARNAMESPACEMETA); | |
60 | |
61 xmlnode_new_child(metadata, "stop"); | |
62 /* publish */ | |
63 jabber_pep_publish(js, publish); | |
64 } else { | |
65 /* | |
66 * TODO: This is pretty gross. The Jabber PRPL really shouldn't | |
67 * do voodoo to try to determine the image type, height | |
68 * and width. | |
69 */ | |
70 /* A PNG header, including the IHDR, but nothing else */ | |
71 const struct { | |
72 guchar signature[8]; /* must be hex 89 50 4E 47 0D 0A 1A 0A */ | |
73 struct { | |
74 guint32 length; /* must be 0x0d */ | |
75 guchar type[4]; /* must be 'I' 'H' 'D' 'R' */ | |
76 guint32 width; | |
77 guint32 height; | |
78 guchar bitdepth; | |
79 guchar colortype; | |
80 guchar compression; | |
81 guchar filter; | |
82 guchar interlace; | |
83 } ihdr; | |
84 } *png = purple_imgstore_get_data(img); /* ATTN: this is in network byte order! */ | |
85 | |
86 /* check if the data is a valid png file (well, at least to some extend) */ | |
87 if(png->signature[0] == 0x89 && | |
88 png->signature[1] == 0x50 && | |
89 png->signature[2] == 0x4e && | |
90 png->signature[3] == 0x47 && | |
91 png->signature[4] == 0x0d && | |
92 png->signature[5] == 0x0a && | |
93 png->signature[6] == 0x1a && | |
94 png->signature[7] == 0x0a && | |
95 ntohl(png->ihdr.length) == 0x0d && | |
96 png->ihdr.type[0] == 'I' && | |
97 png->ihdr.type[1] == 'H' && | |
98 png->ihdr.type[2] == 'D' && | |
99 png->ihdr.type[3] == 'R') { | |
100 /* parse PNG header to get the size of the image (yes, this is required) */ | |
101 guint32 width = ntohl(png->ihdr.width); | |
102 guint32 height = ntohl(png->ihdr.height); | |
103 xmlnode *data, *info; | |
104 char *lengthstring, *widthstring, *heightstring; | |
105 | |
106 /* compute the sha1 hash */ | |
107 char *hash = jabber_calculate_data_sha1sum(purple_imgstore_get_data(img), purple_imgstore_get_size(img)); | |
108 char *base64avatar; | |
109 | |
110 publish = xmlnode_new("publish"); | |
111 xmlnode_set_attrib(publish, "node", AVATARNAMESPACEDATA); | |
112 | |
113 item = xmlnode_new_child(publish, "item"); | |
114 xmlnode_set_attrib(item, "id", hash); | |
115 | |
116 data = xmlnode_new_child(item, "data"); | |
117 xmlnode_set_namespace(data, AVATARNAMESPACEDATA); | |
118 | |
119 base64avatar = purple_base64_encode(purple_imgstore_get_data(img), | |
120 purple_imgstore_get_size(img)); | |
121 xmlnode_insert_data(data,base64avatar,-1); | |
122 g_free(base64avatar); | |
123 | |
124 /* publish the avatar itself */ | |
125 jabber_pep_publish(js, publish); | |
126 | |
127 /* next step: publish the metadata */ | |
128 publish = xmlnode_new("publish"); | |
129 xmlnode_set_attrib(publish,"node", AVATARNAMESPACEMETA); | |
130 | |
131 item = xmlnode_new_child(publish, "item"); | |
132 xmlnode_set_attrib(item, "id", hash); | |
133 | |
134 metadata = xmlnode_new_child(item, "metadata"); | |
135 xmlnode_set_namespace(metadata, AVATARNAMESPACEMETA); | |
136 | |
137 lengthstring = g_strdup_printf("%u", (unsigned)purple_imgstore_get_size(img)); | |
138 widthstring = g_strdup_printf("%u", width); | |
139 heightstring = g_strdup_printf("%u", height); | |
140 | |
141 info = xmlnode_new_child(metadata, "info"); | |
142 xmlnode_set_attrib(info, "id", hash); | |
143 xmlnode_set_attrib(info, "type", "image/png"); | |
144 xmlnode_set_attrib(info, "bytes", lengthstring); | |
145 xmlnode_set_attrib(info, "width", widthstring); | |
146 xmlnode_set_attrib(info, "height", heightstring); | |
147 g_free(lengthstring); | |
148 g_free(widthstring); | |
149 g_free(heightstring); | |
150 | |
151 /* publish the metadata */ | |
152 jabber_pep_publish(js, publish); | |
153 | |
154 g_free(hash); | |
155 } else { | |
156 purple_debug_error("jabber", "Cannot set PEP avatar to non-PNG data\n"); | |
157 } | |
158 } | |
159 } | |
160 | |
161 typedef struct _JabberBuddyAvatarUpdateURLInfo { | |
162 JabberStream *js; | |
163 char *from; | |
164 char *id; | |
165 } JabberBuddyAvatarUpdateURLInfo; | |
166 | |
167 static void | |
168 do_buddy_avatar_update_fromurl(PurpleUtilFetchUrlData *url_data, | |
169 gpointer user_data, const gchar *url_text, | |
170 gsize len, const gchar *error_message) | |
171 { | |
172 JabberBuddyAvatarUpdateURLInfo *info = user_data; | |
173 if(!url_text) { | |
174 purple_debug(PURPLE_DEBUG_ERROR, "jabber", | |
175 "do_buddy_avatar_update_fromurl got error \"%s\"", | |
176 error_message); | |
177 return; | |
178 } | |
179 | |
180 purple_buddy_icons_set_for_user(purple_connection_get_account(info->js->gc), info->from, (void*)url_text, len, info->id); | |
181 g_free(info->from); | |
182 g_free(info->id); | |
183 g_free(info); | |
184 } | |
185 | |
186 static void | |
187 do_buddy_avatar_update_data(JabberStream *js, const char *from, xmlnode *items) | |
188 { | |
189 xmlnode *item, *data; | |
190 const char *checksum; | |
191 char *b64data; | |
192 void *img; | |
193 size_t size; | |
194 if(!items) | |
195 return; | |
196 | |
197 item = xmlnode_get_child(items, "item"); | |
198 if(!item) | |
199 return; | |
200 | |
201 data = xmlnode_get_child_with_namespace(item,"data",AVATARNAMESPACEDATA); | |
202 if(!data) | |
203 return; | |
204 | |
205 checksum = xmlnode_get_attrib(item,"id"); | |
206 if(!checksum) | |
207 return; | |
208 | |
209 b64data = xmlnode_get_data(data); | |
210 if(!b64data) | |
211 return; | |
212 | |
213 img = purple_base64_decode(b64data, &size); | |
214 if(!img) { | |
215 g_free(b64data); | |
216 return; | |
217 } | |
218 | |
219 purple_buddy_icons_set_for_user(purple_connection_get_account(js->gc), from, img, size, checksum); | |
220 g_free(b64data); | |
221 } | |
222 | |
223 static void | |
224 update_buddy_metadata(JabberStream *js, const char *from, xmlnode *items) | |
225 { | |
226 PurpleBuddy *buddy = purple_find_buddy(purple_connection_get_account(js->gc), from); | |
227 const char *checksum; | |
228 xmlnode *item, *metadata; | |
229 if(!buddy) | |
230 return; | |
231 | |
232 checksum = purple_buddy_icons_get_checksum_for_user(buddy); | |
233 item = xmlnode_get_child(items,"item"); | |
234 metadata = xmlnode_get_child_with_namespace(item, "metadata", AVATARNAMESPACEMETA); | |
235 if(!metadata) | |
236 return; | |
237 /* check if we have received a stop */ | |
238 if(xmlnode_get_child(metadata, "stop")) { | |
239 purple_buddy_icons_set_for_user(purple_connection_get_account(js->gc), from, NULL, 0, NULL); | |
240 } else { | |
241 xmlnode *info, *goodinfo = NULL; | |
242 gboolean has_children = FALSE; | |
243 | |
244 /* iterate over all info nodes to get one we can use */ | |
245 for(info = metadata->child; info; info = info->next) { | |
246 if(info->type == XMLNODE_TYPE_TAG) | |
247 has_children = TRUE; | |
248 if(info->type == XMLNODE_TYPE_TAG && !strcmp(info->name,"info")) { | |
249 const char *type = xmlnode_get_attrib(info,"type"); | |
250 const char *id = xmlnode_get_attrib(info,"id"); | |
251 | |
252 if(checksum && id && !strcmp(id, checksum)) { | |
253 /* we already have that avatar, so we don't have to do anything */ | |
254 goodinfo = NULL; | |
255 break; | |
256 } | |
257 /* We'll only pick the png one for now. It's a very nice image format anyways. */ | |
258 if(type && id && !goodinfo && !strcmp(type, "image/png")) | |
259 goodinfo = info; | |
260 } | |
261 } | |
262 if(has_children == FALSE) { | |
263 purple_buddy_icons_set_for_user(purple_connection_get_account(js->gc), from, NULL, 0, NULL); | |
264 } else if(goodinfo) { | |
265 const char *url = xmlnode_get_attrib(goodinfo, "url"); | |
266 const char *id = xmlnode_get_attrib(goodinfo,"id"); | |
267 | |
268 /* the avatar might either be stored in a pep node, or on a HTTP(S) URL */ | |
269 if(!url) | |
270 jabber_pep_request_item(js, from, AVATARNAMESPACEDATA, id, do_buddy_avatar_update_data); | |
271 else { | |
272 PurpleUtilFetchUrlData *url_data; | |
273 JabberBuddyAvatarUpdateURLInfo *info = g_new0(JabberBuddyAvatarUpdateURLInfo, 1); | |
274 info->js = js; | |
275 | |
276 url_data = purple_util_fetch_url_len(url, TRUE, NULL, TRUE, | |
277 MAX_HTTP_BUDDYICON_BYTES, | |
278 do_buddy_avatar_update_fromurl, info); | |
279 if (url_data) { | |
280 info->from = g_strdup(from); | |
281 info->id = g_strdup(id); | |
282 js->url_datas = g_slist_prepend(js->url_datas, url_data); | |
283 } else | |
284 g_free(info); | |
285 | |
286 } | |
287 } | |
288 } | |
289 } |