Replace functions which called once with their bodies
[pidgin-git.git] / libpurple / protocols / jabber / data.c
blob9b3468d518446c7775a80f54a13dd90b9294cb57
1 /*
2 * purple - Handling of XEP-0231: Bits of Binary.
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 <stdlib.h>
27 #include <glib.h>
28 #include <string.h>
30 #include "data.h"
31 #include "debug.h"
32 #include "xmlnode.h"
33 #include "util.h"
34 #include "iq.h"
36 static GHashTable *local_data_by_alt = NULL;
37 static GHashTable *local_data_by_cid = NULL;
38 static GHashTable *remote_data_by_cid = NULL;
40 JabberData *
41 jabber_data_create_from_data(gconstpointer rawdata, gsize size, const char *type,
42 gboolean ephemeral, JabberStream *js)
44 JabberData *data;
45 gchar *checksum;
47 g_return_val_if_fail(rawdata != NULL, NULL);
48 g_return_val_if_fail(size > 0, NULL);
49 g_return_val_if_fail(type != NULL, NULL);
51 checksum = g_compute_checksum_for_data(G_CHECKSUM_SHA1, rawdata, size);
53 data = g_new0(JabberData, 1);
54 data->cid = g_strdup_printf("sha1+%s@bob.xmpp.org", checksum);
55 data->type = g_strdup(type);
56 data->size = size;
57 data->ephemeral = ephemeral;
58 data->data = g_memdup(rawdata, size);
60 g_free(checksum);
61 return data;
64 static void
65 jabber_data_delete(gpointer cbdata)
67 JabberData *data = cbdata;
69 g_free(data->cid);
70 g_free(data->type);
71 g_free(data->data);
72 g_free(data);
76 JabberData *
77 jabber_data_create_from_xml(PurpleXmlNode *tag)
79 JabberData *data;
80 gchar *raw_data = NULL;
81 const gchar *cid, *type;
83 g_return_val_if_fail(tag != NULL, NULL);
85 /* check if this is a "data" tag */
86 if (!purple_strequal(tag->name, "data")) {
87 purple_debug_error("jabber", "Invalid data element\n");
88 return NULL;
91 cid = purple_xmlnode_get_attrib(tag, "cid");
92 type = purple_xmlnode_get_attrib(tag, "type");
94 if (!cid || !type) {
95 purple_debug_error("jabber", "cid or type missing\n");
96 return NULL;
99 raw_data = purple_xmlnode_get_data(tag);
101 if (raw_data == NULL || *raw_data == '\0') {
102 purple_debug_error("jabber", "data element was empty");
103 g_free(raw_data);
104 return NULL;
107 data = g_new0(JabberData, 1);
108 data->data = g_base64_decode(raw_data, &data->size);
109 g_free(raw_data);
111 if (data->data == NULL) {
112 purple_debug_error("jabber", "Malformed base64 data\n");
113 g_free(data);
114 return NULL;
117 data->cid = g_strdup(cid);
118 data->type = g_strdup(type);
120 return data;
123 void
124 jabber_data_destroy(JabberData *data)
126 g_return_if_fail(data != NULL);
128 jabber_data_delete(data);
131 const char *
132 jabber_data_get_cid(const JabberData *data)
134 g_return_val_if_fail(data != NULL, NULL);
136 return data->cid;
140 const char *
141 jabber_data_get_type(const JabberData *data)
143 g_return_val_if_fail(data != NULL, NULL);
145 return data->type;
148 gsize
149 jabber_data_get_size(const JabberData *data)
151 g_return_val_if_fail(data != NULL, 0);
153 return data->size;
156 gpointer
157 jabber_data_get_data(const JabberData *data)
159 g_return_val_if_fail(data != NULL, NULL);
161 return data->data;
164 PurpleXmlNode *
165 jabber_data_get_xml_definition(const JabberData *data)
167 PurpleXmlNode *tag;
168 char *base64data;
170 g_return_val_if_fail(data != NULL, NULL);
172 tag = purple_xmlnode_new("data");
173 base64data = g_base64_encode(data->data, data->size);
175 purple_xmlnode_set_namespace(tag, NS_BOB);
176 purple_xmlnode_set_attrib(tag, "cid", data->cid);
177 purple_xmlnode_set_attrib(tag, "type", data->type);
179 purple_xmlnode_insert_data(tag, base64data, -1);
181 g_free(base64data);
183 return tag;
186 PurpleXmlNode *
187 jabber_data_get_xhtml_im(const JabberData *data, const gchar *alt)
189 PurpleXmlNode *img;
190 char *src;
192 g_return_val_if_fail(data != NULL, NULL);
193 g_return_val_if_fail(alt != NULL, NULL);
195 img = purple_xmlnode_new("img");
196 purple_xmlnode_set_attrib(img, "alt", alt);
198 src = g_strconcat("cid:", data->cid, NULL);
199 purple_xmlnode_set_attrib(img, "src", src);
200 g_free(src);
202 return img;
205 static PurpleXmlNode *
206 jabber_data_get_xml_request(const gchar *cid)
208 PurpleXmlNode *tag = purple_xmlnode_new("data");
210 purple_xmlnode_set_namespace(tag, NS_BOB);
211 purple_xmlnode_set_attrib(tag, "cid", cid);
213 return tag;
216 static gboolean
217 jabber_data_has_valid_hash(const JabberData *data)
219 const gchar *cid = jabber_data_get_cid(data);
220 gchar **cid_parts = g_strsplit(cid, "@", -1);
221 guint num_cid_parts = 0;
222 gboolean ret = FALSE;
224 if (cid_parts)
225 num_cid_parts = g_strv_length(cid_parts);
227 if (num_cid_parts == 2 && purple_strequal(cid_parts[1], "bob.xmpp.org")) {
228 gchar **sub_parts = g_strsplit(cid_parts[0], "+", -1);
229 guint num_sub_parts = 0;
231 if (sub_parts)
232 num_sub_parts = g_strv_length(sub_parts);
234 if (num_sub_parts == 2) {
235 const gchar *hash_algo = sub_parts[0];
236 const gchar *hash_value = sub_parts[1];
237 GChecksumType hash_type;
238 gboolean valid_hash_type = TRUE;
240 if (purple_strequal(hash_algo, "sha1"))
241 hash_type = G_CHECKSUM_SHA1;
242 else if (purple_strequal(hash_algo, "sha256"))
243 hash_type = G_CHECKSUM_SHA256;
244 else if (purple_strequal(hash_algo, "sha512"))
245 hash_type = G_CHECKSUM_SHA512;
246 else if (purple_strequal(hash_algo, "md5"))
247 hash_type = G_CHECKSUM_MD5;
248 else
249 valid_hash_type = FALSE;
251 if (valid_hash_type) {
252 gchar *digest = g_compute_checksum_for_data(
253 hash_type, jabber_data_get_data(data),
254 jabber_data_get_size(data));
256 ret = purple_strequal(digest, hash_value);
258 if (!ret)
259 purple_debug_warning("jabber", "Unable to validate BoB "
260 "hash; expecting %s, got %s\n",
261 cid, digest);
263 g_free(digest);
264 } else {
265 purple_debug_warning("jabber", "Unable to validate BoB hash; "
266 "unknown hash algorithm %s\n", hash_algo);
268 } else {
269 purple_debug_warning("jabber", "Malformed BoB CID\n");
272 g_strfreev(sub_parts);
275 g_strfreev(cid_parts);
276 return ret;
280 typedef struct {
281 gpointer userdata;
282 gchar *alt;
283 gboolean ephemeral;
284 JabberDataRequestCallback *cb;
285 } JabberDataRequestData;
287 static void
288 jabber_data_request_cb(JabberStream *js, const char *from,
289 JabberIqType type, const char *id, PurpleXmlNode *packet, gpointer data)
291 JabberDataRequestData *request_data = (JabberDataRequestData *) data;
292 gpointer userdata = request_data->userdata;
293 gchar *alt = request_data->alt;
294 gboolean ephemeral = request_data->ephemeral;
295 JabberDataRequestCallback *cb = request_data->cb;
297 PurpleXmlNode *data_element = purple_xmlnode_get_child(packet, "data");
298 PurpleXmlNode *item_not_found = purple_xmlnode_get_child(packet, "item-not-found");
300 /* did we get a data element as result? */
301 if (data_element && type == JABBER_IQ_RESULT) {
302 JabberData *data = jabber_data_create_from_xml(data_element);
304 if (data && !ephemeral) {
305 jabber_data_associate_remote(js, from, data);
307 cb(data, alt, userdata);
308 } else if (item_not_found) {
309 purple_debug_info("jabber",
310 "Responder didn't recognize requested data\n");
311 cb(NULL, alt, userdata);
312 } else {
313 purple_debug_warning("jabber", "Unknown response to data request\n");
314 cb(NULL, alt, userdata);
317 g_free(request_data);
320 void
321 jabber_data_request(JabberStream *js, const gchar *cid, const gchar *who,
322 gchar *alt, gboolean ephemeral, JabberDataRequestCallback cb,
323 gpointer userdata)
325 JabberIq *request;
326 PurpleXmlNode *data_request;
327 JabberDataRequestData *data;
329 g_return_if_fail(cid != NULL);
330 g_return_if_fail(who != NULL);
331 g_return_if_fail(alt != NULL);
333 request = jabber_iq_new(js, JABBER_IQ_GET);
334 data_request = jabber_data_get_xml_request(cid);
335 data = g_new0(JabberDataRequestData, 1);
337 data->userdata = userdata;
338 data->alt = alt;
339 data->ephemeral = ephemeral;
340 data->cb = cb;
342 purple_xmlnode_set_attrib(request->node, "to", who);
343 jabber_iq_set_callback(request, jabber_data_request_cb, data);
344 purple_xmlnode_insert_child(request->node, data_request);
345 jabber_iq_send(request);
348 const JabberData *
349 jabber_data_find_local_by_alt(const gchar *alt)
351 purple_debug_info("jabber", "looking up local data object with alt = %s\n", alt);
352 return g_hash_table_lookup(local_data_by_alt, alt);
355 const JabberData *
356 jabber_data_find_local_by_cid(const gchar *cid)
358 purple_debug_info("jabber", "lookup local data object with cid = %s\n", cid);
359 return g_hash_table_lookup(local_data_by_cid, cid);
362 const JabberData *
363 jabber_data_find_remote_by_cid(JabberStream *js, const gchar *who,
364 const gchar *cid)
366 const JabberData *data = g_hash_table_lookup(remote_data_by_cid, cid);
367 purple_debug_info("jabber", "lookup remote data object with cid = %s\n", cid);
369 if (data == NULL) {
370 gchar *jid_cid =
371 g_strdup_printf("%s@%s/%s%s%s", js->user->node, js->user->domain,
372 js->user->resource, who, cid);
373 purple_debug_info("jabber",
374 "didn't find BoB object by pure CID, try including JIDs: %s\n",
375 jid_cid);
376 data = g_hash_table_lookup(remote_data_by_cid, jid_cid);
377 g_free(jid_cid);
379 return data;
382 void
383 jabber_data_associate_local(JabberData *data, const gchar *alt)
385 g_return_if_fail(data != NULL);
387 purple_debug_info("jabber", "associating local data object\n alt = %s, cid = %s\n",
388 alt , jabber_data_get_cid(data));
389 if (alt)
390 g_hash_table_insert(local_data_by_alt, g_strdup(alt), data);
391 g_hash_table_insert(local_data_by_cid, g_strdup(jabber_data_get_cid(data)),
392 data);
395 void
396 jabber_data_associate_remote(JabberStream *js, const gchar *who, JabberData *data)
398 gchar *cid;
400 g_return_if_fail(data != NULL);
402 if (jabber_data_has_valid_hash(data)) {
403 cid = g_strdup(jabber_data_get_cid(data));
404 } else {
405 cid = g_strdup_printf("%s@%s/%s%s%s", js->user->node, js->user->domain,
406 js->user->resource, who, jabber_data_get_cid(data));
409 purple_debug_info("jabber", "associating remote BoB object with cid = %s\n",
410 cid);
412 g_hash_table_insert(remote_data_by_cid, cid, data);
415 void
416 jabber_data_parse(JabberStream *js, const char *who, JabberIqType type,
417 const char *id, PurpleXmlNode *data_node)
419 JabberIq *result = NULL;
420 const char *cid = purple_xmlnode_get_attrib(data_node, "cid");
421 const JabberData *data = cid ? jabber_data_find_local_by_cid(cid) : NULL;
423 if (!data) {
424 PurpleXmlNode *item_not_found = purple_xmlnode_new("item-not-found");
426 result = jabber_iq_new(js, JABBER_IQ_ERROR);
427 if (who)
428 purple_xmlnode_set_attrib(result->node, "to", who);
429 purple_xmlnode_set_attrib(result->node, "id", id);
430 purple_xmlnode_insert_child(result->node, item_not_found);
431 } else {
432 result = jabber_iq_new(js, JABBER_IQ_RESULT);
433 if (who)
434 purple_xmlnode_set_attrib(result->node, "to", who);
435 purple_xmlnode_set_attrib(result->node, "id", id);
436 purple_xmlnode_insert_child(result->node,
437 jabber_data_get_xml_definition(data));
438 /* if the data object is temporary, destroy it and remove the references
439 to it */
440 if (data->ephemeral) {
441 g_hash_table_remove(local_data_by_cid, cid);
444 jabber_iq_send(result);
447 void
448 jabber_data_init(void)
450 if (purple_debug_is_verbose())
451 purple_debug_misc("jabber", "creating hash tables for data objects");
452 local_data_by_alt = g_hash_table_new_full(g_str_hash, g_str_equal,
453 g_free, NULL);
454 local_data_by_cid = g_hash_table_new_full(g_str_hash, g_str_equal,
455 g_free, jabber_data_delete);
456 remote_data_by_cid = g_hash_table_new_full(g_str_hash, g_str_equal,
457 g_free, jabber_data_delete);
459 jabber_iq_register_handler("data", NS_BOB, jabber_data_parse);
462 void
463 jabber_data_uninit(void)
465 if (purple_debug_is_verbose())
466 purple_debug_info("jabber", "destroying hash tables for data objects");
467 g_hash_table_destroy(local_data_by_alt);
468 g_hash_table_destroy(local_data_by_cid);
469 g_hash_table_destroy(remote_data_by_cid);
470 local_data_by_alt = local_data_by_cid = remote_data_by_cid = NULL;