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
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
36 static GHashTable
*local_data_by_alt
= NULL
;
37 static GHashTable
*local_data_by_cid
= NULL
;
38 static GHashTable
*remote_data_by_cid
= NULL
;
41 jabber_data_create_from_data(gconstpointer rawdata
, gsize size
, const char *type
,
42 gboolean ephemeral
, JabberStream
*js
)
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
);
57 data
->ephemeral
= ephemeral
;
58 data
->data
= g_memdup(rawdata
, size
);
65 jabber_data_delete(gpointer cbdata
)
67 JabberData
*data
= cbdata
;
77 jabber_data_create_from_xml(PurpleXmlNode
*tag
)
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");
91 cid
= purple_xmlnode_get_attrib(tag
, "cid");
92 type
= purple_xmlnode_get_attrib(tag
, "type");
95 purple_debug_error("jabber", "cid or type missing\n");
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");
107 data
= g_new0(JabberData
, 1);
108 data
->data
= g_base64_decode(raw_data
, &data
->size
);
111 if (data
->data
== NULL
) {
112 purple_debug_error("jabber", "Malformed base64 data\n");
117 data
->cid
= g_strdup(cid
);
118 data
->type
= g_strdup(type
);
124 jabber_data_destroy(JabberData
*data
)
126 g_return_if_fail(data
!= NULL
);
128 jabber_data_delete(data
);
132 jabber_data_get_cid(const JabberData
*data
)
134 g_return_val_if_fail(data
!= NULL
, NULL
);
141 jabber_data_get_type(const JabberData
*data
)
143 g_return_val_if_fail(data
!= NULL
, NULL
);
149 jabber_data_get_size(const JabberData
*data
)
151 g_return_val_if_fail(data
!= NULL
, 0);
157 jabber_data_get_data(const JabberData
*data
)
159 g_return_val_if_fail(data
!= NULL
, NULL
);
165 jabber_data_get_xml_definition(const JabberData
*data
)
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);
187 jabber_data_get_xhtml_im(const JabberData
*data
, const gchar
*alt
)
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
);
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
);
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
;
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;
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
;
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
);
259 purple_debug_warning("jabber", "Unable to validate BoB "
260 "hash; expecting %s, got %s\n",
265 purple_debug_warning("jabber", "Unable to validate BoB hash; "
266 "unknown hash algorithm %s\n", hash_algo
);
269 purple_debug_warning("jabber", "Malformed BoB CID\n");
272 g_strfreev(sub_parts
);
275 g_strfreev(cid_parts
);
284 JabberDataRequestCallback
*cb
;
285 } JabberDataRequestData
;
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
);
313 purple_debug_warning("jabber", "Unknown response to data request\n");
314 cb(NULL
, alt
, userdata
);
317 g_free(request_data
);
321 jabber_data_request(JabberStream
*js
, const gchar
*cid
, const gchar
*who
,
322 gchar
*alt
, gboolean ephemeral
, JabberDataRequestCallback cb
,
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
;
339 data
->ephemeral
= ephemeral
;
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
);
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
);
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
);
363 jabber_data_find_remote_by_cid(JabberStream
*js
, const gchar
*who
,
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
);
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",
376 data
= g_hash_table_lookup(remote_data_by_cid
, jid_cid
);
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
));
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
)),
396 jabber_data_associate_remote(JabberStream
*js
, const gchar
*who
, JabberData
*data
)
400 g_return_if_fail(data
!= NULL
);
402 if (jabber_data_has_valid_hash(data
)) {
403 cid
= g_strdup(jabber_data_get_cid(data
));
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",
412 g_hash_table_insert(remote_data_by_cid
, cid
, data
);
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
;
424 PurpleXmlNode
*item_not_found
= purple_xmlnode_new("item-not-found");
426 result
= jabber_iq_new(js
, JABBER_IQ_ERROR
);
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
);
432 result
= jabber_iq_new(js
, JABBER_IQ_RESULT
);
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
440 if (data
->ephemeral
) {
441 g_hash_table_remove(local_data_by_cid
, cid
);
444 jabber_iq_send(result
);
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
,
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
);
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
;