rename accountopt.[ch] to purpleaccountoption.[ch]
[pidgin-git.git] / libpurple / protocols / jabber / caps.c
blob74866d15503cbfecbbcf61fbf83dba5b25d72022
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 "debug.h"
27 #include "caps.h"
28 #include "iq.h"
29 #include "presence.h"
30 #include "util.h"
31 #include "xdata.h"
33 #define JABBER_CAPS_FILENAME "xmpp-caps.xml"
35 typedef struct {
36 gchar *var;
37 GList *values;
38 } JabberDataFormField;
40 static GHashTable *capstable = NULL; /* JabberCapsTuple -> JabberCapsClientInfo */
41 static GHashTable *nodetable = NULL; /* char *node -> JabberCapsNodeExts */
42 static guint save_timer = 0;
44 /* Free a GList of allocated char* */
45 static void
46 free_string_glist(GList *list)
48 while (list) {
49 g_free(list->data);
50 list = g_list_delete_link(list, list);
54 static JabberCapsNodeExts*
55 jabber_caps_node_exts_ref(JabberCapsNodeExts *exts)
57 g_return_val_if_fail(exts != NULL, NULL);
59 ++exts->ref;
60 return exts;
63 static void
64 jabber_caps_node_exts_unref(JabberCapsNodeExts *exts)
66 if (exts == NULL)
67 return;
69 g_return_if_fail(exts->ref != 0);
71 if (--exts->ref != 0)
72 return;
74 g_hash_table_destroy(exts->exts);
75 g_free(exts);
78 static guint jabber_caps_hash(gconstpointer data) {
79 const JabberCapsTuple *key = data;
80 guint nodehash = g_str_hash(key->node);
81 guint verhash = g_str_hash(key->ver);
83 * 'hash' was optional in XEP-0115 v1.4 and g_str_hash crashes on NULL >:O.
84 * Okay, maybe I've played too much Zelda, but that looks like
85 * a Deku Shrub...
87 guint hashhash = (key->hash ? g_str_hash(key->hash) : 0);
88 return nodehash ^ verhash ^ hashhash;
91 static gboolean jabber_caps_compare(gconstpointer v1, gconstpointer v2) {
92 const JabberCapsTuple *name1 = v1;
93 const JabberCapsTuple *name2 = v2;
95 return purple_strequal(name1->node, name2->node) &&
96 purple_strequal(name1->ver, name2->ver) &&
97 purple_strequal(name1->hash, name2->hash);
100 static void
101 jabber_caps_client_info_destroy(JabberCapsClientInfo *info)
103 if (info == NULL)
104 return;
106 while(info->identities) {
107 JabberIdentity *id = info->identities->data;
108 g_free(id->category);
109 g_free(id->type);
110 g_free(id->name);
111 g_free(id->lang);
112 g_free(id);
113 info->identities = g_list_delete_link(info->identities, info->identities);
116 free_string_glist(info->features);
118 while (info->forms) {
119 purple_xmlnode_free(info->forms->data);
120 info->forms = g_list_delete_link(info->forms, info->forms);
123 jabber_caps_node_exts_unref(info->exts);
125 g_free((char *)info->tuple.node);
126 g_free((char *)info->tuple.ver);
127 g_free((char *)info->tuple.hash);
129 g_free(info);
132 /* NOTE: Takes a reference to the exts, unref it if you don't really want to
133 * keep it around. */
134 static JabberCapsNodeExts*
135 jabber_caps_find_exts_by_node(const char *node)
137 JabberCapsNodeExts *exts;
138 if (NULL == (exts = g_hash_table_lookup(nodetable, node))) {
139 exts = g_new0(JabberCapsNodeExts, 1);
140 exts->exts = g_hash_table_new_full(g_str_hash, g_str_equal, g_free,
141 (GDestroyNotify)free_string_glist);
142 g_hash_table_insert(nodetable, g_strdup(node), jabber_caps_node_exts_ref(exts));
145 return jabber_caps_node_exts_ref(exts);
148 static void
149 exts_to_xmlnode(gconstpointer key, gconstpointer value, gpointer user_data)
151 const char *identifier = key;
152 const GList *features = value, *node;
153 PurpleXmlNode *client = user_data, *ext, *feature;
155 ext = purple_xmlnode_new_child(client, "ext");
156 purple_xmlnode_set_attrib(ext, "identifier", identifier);
158 for (node = features; node; node = node->next) {
159 feature = purple_xmlnode_new_child(ext, "feature");
160 purple_xmlnode_set_attrib(feature, "var", (const gchar *)node->data);
164 static void jabber_caps_store_client(gpointer key, gpointer value, gpointer user_data) {
165 const JabberCapsTuple *tuple = key;
166 const JabberCapsClientInfo *props = value;
167 PurpleXmlNode *root = user_data;
168 PurpleXmlNode *client = purple_xmlnode_new_child(root, "client");
169 GList *iter;
171 purple_xmlnode_set_attrib(client, "node", tuple->node);
172 purple_xmlnode_set_attrib(client, "ver", tuple->ver);
173 if (tuple->hash)
174 purple_xmlnode_set_attrib(client, "hash", tuple->hash);
175 for(iter = props->identities; iter; iter = g_list_next(iter)) {
176 JabberIdentity *id = iter->data;
177 PurpleXmlNode *identity = purple_xmlnode_new_child(client, "identity");
178 purple_xmlnode_set_attrib(identity, "category", id->category);
179 purple_xmlnode_set_attrib(identity, "type", id->type);
180 if (id->name)
181 purple_xmlnode_set_attrib(identity, "name", id->name);
182 if (id->lang)
183 purple_xmlnode_set_attrib(identity, "lang", id->lang);
186 for(iter = props->features; iter; iter = g_list_next(iter)) {
187 const char *feat = iter->data;
188 PurpleXmlNode *feature = purple_xmlnode_new_child(client, "feature");
189 purple_xmlnode_set_attrib(feature, "var", feat);
192 for(iter = props->forms; iter; iter = g_list_next(iter)) {
193 /* FIXME: See #7814 */
194 PurpleXmlNode *xdata = iter->data;
195 purple_xmlnode_insert_child(client, purple_xmlnode_copy(xdata));
198 /* TODO: Ideally, only save this once-per-node... */
199 if (props->exts)
200 g_hash_table_foreach(props->exts->exts, (GHFunc)exts_to_xmlnode, client);
203 static gboolean
204 do_jabber_caps_store(gpointer data)
206 char *str;
207 int length = 0;
208 PurpleXmlNode *root = purple_xmlnode_new("capabilities");
210 g_hash_table_foreach(capstable, jabber_caps_store_client, root);
211 str = purple_xmlnode_to_formatted_str(root, &length);
212 purple_xmlnode_free(root);
213 purple_util_write_data_to_cache_file(JABBER_CAPS_FILENAME, str, length);
214 g_free(str);
216 save_timer = 0;
217 return FALSE;
220 static void
221 schedule_caps_save(void)
223 if (save_timer == 0)
224 save_timer = g_timeout_add_seconds(5, do_jabber_caps_store, NULL);
227 static void
228 jabber_caps_load(void)
230 PurpleXmlNode *capsdata = purple_util_read_xml_from_cache_file(JABBER_CAPS_FILENAME, "XMPP capabilities cache");
231 PurpleXmlNode *client;
233 if(!capsdata)
234 return;
236 if (!purple_strequal(capsdata->name, "capabilities")) {
237 purple_xmlnode_free(capsdata);
238 return;
241 for (client = capsdata->child; client; client = client->next) {
242 if (client->type != PURPLE_XMLNODE_TYPE_TAG)
243 continue;
244 if (purple_strequal(client->name, "client")) {
245 JabberCapsClientInfo *value = g_new0(JabberCapsClientInfo, 1);
246 JabberCapsTuple *key = (JabberCapsTuple*)&value->tuple;
247 PurpleXmlNode *child;
248 JabberCapsNodeExts *exts = NULL;
249 key->node = g_strdup(purple_xmlnode_get_attrib(client,"node"));
250 key->ver = g_strdup(purple_xmlnode_get_attrib(client,"ver"));
251 key->hash = g_strdup(purple_xmlnode_get_attrib(client,"hash"));
253 /* v1.3 capabilities */
254 if (key->hash == NULL)
255 exts = jabber_caps_find_exts_by_node(key->node);
257 for (child = client->child; child; child = child->next) {
258 if (child->type != PURPLE_XMLNODE_TYPE_TAG)
259 continue;
260 if (purple_strequal(child->name, "feature")) {
261 const char *var = purple_xmlnode_get_attrib(child, "var");
262 if(!var)
263 continue;
264 value->features = g_list_append(value->features,g_strdup(var));
265 } else if (purple_strequal(child->name, "identity")) {
266 const char *category = purple_xmlnode_get_attrib(child, "category");
267 const char *type = purple_xmlnode_get_attrib(child, "type");
268 const char *name = purple_xmlnode_get_attrib(child, "name");
269 const char *lang = purple_xmlnode_get_attrib(child, "lang");
270 JabberIdentity *id;
272 if (!category || !type)
273 continue;
275 id = g_new0(JabberIdentity, 1);
276 id->category = g_strdup(category);
277 id->type = g_strdup(type);
278 id->name = g_strdup(name);
279 id->lang = g_strdup(lang);
281 value->identities = g_list_append(value->identities,id);
282 } else if (purple_strequal(child->name, "x")) {
283 /* TODO: See #7814 -- this might cause problems if anyone
284 * ever actually specifies forms. In fact, for this to
285 * work properly, that bug needs to be fixed in
286 * purple_xmlnode_from_str, not the output version... */
287 value->forms = g_list_append(value->forms, purple_xmlnode_copy(child));
288 } else if (purple_strequal(child->name, "ext")) {
289 if (key->hash != NULL)
290 purple_debug_warning("jabber", "Ignoring exts when reading new-style caps\n");
291 else {
292 /* TODO: Do we care about reading in the identities listed here? */
293 const char *identifier = purple_xmlnode_get_attrib(child, "identifier");
294 PurpleXmlNode *node;
295 GList *features = NULL;
297 if (!identifier)
298 continue;
300 for (node = child->child; node; node = node->next) {
301 if (node->type != PURPLE_XMLNODE_TYPE_TAG)
302 continue;
303 if (purple_strequal(node->name, "feature")) {
304 const char *var = purple_xmlnode_get_attrib(node, "var");
305 if (!var)
306 continue;
307 features = g_list_prepend(features, g_strdup(var));
311 if (features) {
312 g_hash_table_insert(exts->exts, g_strdup(identifier),
313 features);
314 } else
315 purple_debug_warning("jabber", "Caps ext %s had no features.\n",
316 identifier);
321 value->exts = exts;
322 g_hash_table_replace(capstable, key, value);
326 purple_xmlnode_free(capsdata);
329 void jabber_caps_init(void)
331 nodetable = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, (GDestroyNotify)jabber_caps_node_exts_unref);
332 capstable = g_hash_table_new_full(jabber_caps_hash, jabber_caps_compare, NULL, (GDestroyNotify)jabber_caps_client_info_destroy);
333 jabber_caps_load();
336 void jabber_caps_uninit(void)
338 if (save_timer != 0) {
339 g_source_remove(save_timer);
340 save_timer = 0;
341 do_jabber_caps_store(NULL);
343 g_hash_table_destroy(capstable);
344 g_hash_table_destroy(nodetable);
345 capstable = nodetable = NULL;
348 gboolean jabber_caps_exts_known(const JabberCapsClientInfo *info,
349 char **exts)
351 int i;
352 g_return_val_if_fail(info != NULL, FALSE);
354 if (!exts)
355 return TRUE;
357 for (i = 0; exts[i]; ++i) {
358 /* Hack since we advertise the ext along with v1.5 caps but don't
359 * store any exts */
360 if (purple_strequal(exts[i], "voice-v1") && !info->exts)
361 continue;
362 if (!info->exts ||
363 !g_hash_table_lookup(info->exts->exts, exts[i]))
364 return FALSE;
367 return TRUE;
370 typedef struct {
371 guint ref;
373 jabber_caps_get_info_cb cb;
374 gpointer cb_data;
376 char *who;
377 char *node;
378 char *ver;
379 char *hash;
381 JabberCapsClientInfo *info;
383 GList *exts;
384 guint extOutstanding;
385 JabberCapsNodeExts *node_exts;
386 } jabber_caps_cbplususerdata;
388 static jabber_caps_cbplususerdata*
389 cbplususerdata_ref(jabber_caps_cbplususerdata *data)
391 g_return_val_if_fail(data != NULL, NULL);
393 ++data->ref;
394 return data;
397 static void
398 cbplususerdata_unref(jabber_caps_cbplususerdata *data)
400 if (data == NULL)
401 return;
403 g_return_if_fail(data->ref != 0);
405 if (--data->ref > 0)
406 return;
408 g_free(data->who);
409 g_free(data->node);
410 g_free(data->ver);
411 g_free(data->hash);
413 /* If we have info here, it's already in the capstable, so don't free it */
414 if (data->exts)
415 free_string_glist(data->exts);
416 if (data->node_exts)
417 jabber_caps_node_exts_unref(data->node_exts);
418 g_free(data);
421 static void
422 jabber_caps_get_info_complete(jabber_caps_cbplususerdata *userdata)
424 if (userdata->cb) {
425 userdata->cb(userdata->info, userdata->exts, userdata->cb_data);
426 userdata->info = NULL;
427 userdata->exts = NULL;
430 if (userdata->ref != 1)
431 purple_debug_warning("jabber", "Lost a reference to caps cbdata: %d\n",
432 userdata->ref);
435 static void
436 jabber_caps_client_iqcb(JabberStream *js, const char *from, JabberIqType type,
437 const char *id, PurpleXmlNode *packet, gpointer data)
439 PurpleXmlNode *query = purple_xmlnode_get_child_with_namespace(packet, "query",
440 NS_DISCO_INFO);
441 jabber_caps_cbplususerdata *userdata = data;
442 JabberCapsClientInfo *info = NULL, *value;
443 JabberCapsTuple key;
445 if (!query || type == JABBER_IQ_ERROR) {
446 /* Any outstanding exts will be dealt with via ref-counting */
447 userdata->cb(NULL, NULL, userdata->cb_data);
448 cbplususerdata_unref(userdata);
449 return;
452 /* check hash */
453 info = jabber_caps_parse_client_info(query);
455 /* Only validate if these are v1.5 capabilities */
456 if (userdata->hash) {
457 gchar *hash = NULL;
458 GChecksumType hash_type;
459 gboolean supported_hash = TRUE;
461 if (purple_strequal(userdata->hash, "sha-1")) {
462 hash_type = G_CHECKSUM_SHA1;
463 } else if (purple_strequal(userdata->hash, "md5")) {
464 hash_type = G_CHECKSUM_MD5;
465 } else {
466 supported_hash = FALSE;
469 if (supported_hash) {
470 hash = jabber_caps_calculate_hash(info, hash_type);
473 if (!hash || !purple_strequal(hash, userdata->ver)) {
474 purple_debug_warning("jabber", "Could not validate caps info from "
475 "%s. Expected %s, got %s\n",
476 purple_xmlnode_get_attrib(packet, "from"),
477 userdata->ver, hash ? hash : "(null)");
479 userdata->cb(NULL, NULL, userdata->cb_data);
480 jabber_caps_client_info_destroy(info);
481 cbplususerdata_unref(userdata);
482 g_free(hash);
483 return;
486 g_free(hash);
489 if (!userdata->hash && userdata->node_exts) {
490 /* If the ClientInfo doesn't have information about the exts, give them
491 * ours (along with our ref) */
492 info->exts = userdata->node_exts;
493 userdata->node_exts = NULL;
496 key.node = userdata->node;
497 key.ver = userdata->ver;
498 key.hash = userdata->hash;
500 /* Use the copy of this data already in the table if it exists or insert
501 * a new one if we need to */
502 if ((value = g_hash_table_lookup(capstable, &key))) {
503 jabber_caps_client_info_destroy(info);
504 info = value;
505 } else {
506 JabberCapsTuple *n_key = NULL;
508 if (G_UNLIKELY(info == NULL)) {
509 g_warn_if_reached();
510 return;
513 n_key = (JabberCapsTuple *)&info->tuple;
514 n_key->node = userdata->node;
515 n_key->ver = userdata->ver;
516 n_key->hash = userdata->hash;
517 userdata->node = userdata->ver = userdata->hash = NULL;
519 /* The capstable gets a reference */
520 g_hash_table_insert(capstable, n_key, info);
521 schedule_caps_save();
524 userdata->info = info;
526 if (userdata->extOutstanding == 0)
527 jabber_caps_get_info_complete(userdata);
529 cbplususerdata_unref(userdata);
532 typedef struct {
533 const char *name;
534 jabber_caps_cbplususerdata *data;
535 } ext_iq_data;
537 static void
538 jabber_caps_ext_iqcb(JabberStream *js, const char *from, JabberIqType type,
539 const char *id, PurpleXmlNode *packet, gpointer data)
541 PurpleXmlNode *query = purple_xmlnode_get_child_with_namespace(packet, "query",
542 NS_DISCO_INFO);
543 PurpleXmlNode *child;
544 ext_iq_data *userdata = data;
545 GList *features = NULL;
546 JabberCapsNodeExts *node_exts;
548 if (!query || type == JABBER_IQ_ERROR) {
549 cbplususerdata_unref(userdata->data);
550 g_free(userdata);
551 return;
554 node_exts = (userdata->data->info ? userdata->data->info->exts :
555 userdata->data->node_exts);
557 /* TODO: I don't see how this can actually happen, but it crashed khc. */
558 if (!node_exts) {
559 purple_debug_error("jabber", "Couldn't find JabberCapsNodeExts. If you "
560 "see this, please tell darkrain42 and save your debug log.\n"
561 "JabberCapsClientInfo = %p\n", userdata->data->info);
564 /* Try once more to find the exts and then fail */
565 node_exts = jabber_caps_find_exts_by_node(userdata->data->node);
566 if (node_exts) {
567 purple_debug_info("jabber", "Found the exts on the second try.\n");
568 if (userdata->data->info)
569 userdata->data->info->exts = node_exts;
570 else
571 userdata->data->node_exts = node_exts;
572 } else {
573 cbplususerdata_unref(userdata->data);
574 g_free(userdata);
575 g_return_if_reached();
579 /* So, we decrement this after checking for an error, which means that
580 * if there *is* an error, we'll never call the callback passed to
581 * jabber_caps_get_info. We will still free all of our data, though.
583 --userdata->data->extOutstanding;
585 for (child = purple_xmlnode_get_child(query, "feature"); child;
586 child = purple_xmlnode_get_next_twin(child)) {
587 const char *var = purple_xmlnode_get_attrib(child, "var");
588 if (var)
589 features = g_list_prepend(features, g_strdup(var));
592 g_hash_table_insert(node_exts->exts, g_strdup(userdata->name), features);
593 schedule_caps_save();
595 /* Are we done? */
596 if (userdata->data->info && userdata->data->extOutstanding == 0)
597 jabber_caps_get_info_complete(userdata->data);
599 cbplususerdata_unref(userdata->data);
600 g_free(userdata);
603 void jabber_caps_get_info(JabberStream *js, const char *who, const char *node,
604 const char *ver, const char *hash, char **exts,
605 jabber_caps_get_info_cb cb, gpointer user_data)
607 JabberCapsClientInfo *info;
608 JabberCapsTuple key;
609 jabber_caps_cbplususerdata *userdata;
611 if (exts && hash) {
612 purple_debug_misc("jabber", "Ignoring exts in new-style caps from %s\n",
613 who);
614 g_strfreev(exts);
615 exts = NULL;
618 /* Using this in a read-only fashion, so the cast is OK */
619 key.node = (char *)node;
620 key.ver = (char *)ver;
621 key.hash = (char *)hash;
623 info = g_hash_table_lookup(capstable, &key);
624 if (info && hash) {
625 /* v1.5 - We already have all the information we care about */
626 if (cb)
627 cb(info, NULL, user_data);
628 return;
631 userdata = g_new0(jabber_caps_cbplususerdata, 1);
632 /* We start out with 0 references. Every query takes one */
633 userdata->cb = cb;
634 userdata->cb_data = user_data;
635 userdata->who = g_strdup(who);
636 userdata->node = g_strdup(node);
637 userdata->ver = g_strdup(ver);
638 userdata->hash = g_strdup(hash);
640 if (info) {
641 userdata->info = info;
642 } else {
643 /* If we don't have the basic information about the client, we need
644 * to fetch it. */
645 JabberIq *iq;
646 PurpleXmlNode *query;
647 char *nodever;
649 iq = jabber_iq_new_query(js, JABBER_IQ_GET, NS_DISCO_INFO);
650 query = purple_xmlnode_get_child_with_namespace(iq->node, "query",
651 NS_DISCO_INFO);
652 nodever = g_strdup_printf("%s#%s", node, ver);
653 purple_xmlnode_set_attrib(query, "node", nodever);
654 g_free(nodever);
655 purple_xmlnode_set_attrib(iq->node, "to", who);
657 cbplususerdata_ref(userdata);
659 jabber_iq_set_callback(iq, jabber_caps_client_iqcb, userdata);
660 jabber_iq_send(iq);
663 /* Are there any exts that we don't recognize? */
664 if (exts) {
665 JabberCapsNodeExts *node_exts;
666 int i;
668 if (info) {
669 if (info->exts)
670 node_exts = info->exts;
671 else
672 node_exts = info->exts = jabber_caps_find_exts_by_node(node);
673 } else
674 /* We'll put it in later once we have the client info */
675 node_exts = userdata->node_exts = jabber_caps_find_exts_by_node(node);
677 for (i = 0; exts[i]; ++i) {
678 userdata->exts = g_list_prepend(userdata->exts, exts[i]);
679 /* Look it up if we don't already know what it means */
680 if (!g_hash_table_lookup(node_exts->exts, exts[i])) {
681 JabberIq *iq;
682 PurpleXmlNode *query;
683 char *nodeext;
684 ext_iq_data *cbdata = g_new(ext_iq_data, 1);
686 cbdata->name = exts[i];
687 cbdata->data = cbplususerdata_ref(userdata);
689 iq = jabber_iq_new_query(js, JABBER_IQ_GET, NS_DISCO_INFO);
690 query = purple_xmlnode_get_child_with_namespace(iq->node, "query",
691 NS_DISCO_INFO);
692 nodeext = g_strdup_printf("%s#%s", node, exts[i]);
693 purple_xmlnode_set_attrib(query, "node", nodeext);
694 g_free(nodeext);
695 purple_xmlnode_set_attrib(iq->node, "to", who);
697 jabber_iq_set_callback(iq, jabber_caps_ext_iqcb, cbdata);
698 jabber_iq_send(iq);
700 ++userdata->extOutstanding;
702 exts[i] = NULL;
704 /* All the strings are now part of the GList, so don't need
705 * g_strfreev. */
706 g_free(exts);
709 if (userdata->info && userdata->extOutstanding == 0) {
710 /* Start with 1 ref so the below functions are happy */
711 userdata->ref = 1;
713 /* We have everything we need right now */
714 jabber_caps_get_info_complete(userdata);
715 cbplususerdata_unref(userdata);
719 static gint
720 jabber_xdata_compare(gconstpointer a, gconstpointer b)
722 const PurpleXmlNode *aformtypefield = a;
723 const PurpleXmlNode *bformtypefield = b;
724 char *aformtype;
725 char *bformtype;
726 int result;
728 aformtype = jabber_x_data_get_formtype(aformtypefield);
729 bformtype = jabber_x_data_get_formtype(bformtypefield);
731 result = strcmp(aformtype, bformtype);
732 g_free(aformtype);
733 g_free(bformtype);
734 return result;
737 JabberCapsClientInfo *jabber_caps_parse_client_info(PurpleXmlNode *query)
739 PurpleXmlNode *child;
740 JabberCapsClientInfo *info;
742 if (!query || !purple_strequal(query->name, "query") ||
743 !purple_strequal(query->xmlns, NS_DISCO_INFO))
744 return NULL;
746 info = g_new0(JabberCapsClientInfo, 1);
748 for(child = query->child; child; child = child->next) {
749 if (child->type != PURPLE_XMLNODE_TYPE_TAG)
750 continue;
751 if (purple_strequal(child->name, "identity")) {
752 /* parse identity */
753 const char *category = purple_xmlnode_get_attrib(child, "category");
754 const char *type = purple_xmlnode_get_attrib(child, "type");
755 const char *name = purple_xmlnode_get_attrib(child, "name");
756 const char *lang = purple_xmlnode_get_attrib(child, "lang");
757 JabberIdentity *id;
759 if (!category || !type)
760 continue;
762 id = g_new0(JabberIdentity, 1);
763 id->category = g_strdup(category);
764 id->type = g_strdup(type);
765 id->name = g_strdup(name);
766 id->lang = g_strdup(lang);
768 info->identities = g_list_append(info->identities, id);
769 } else if (purple_strequal(child->name, "feature")) {
770 /* parse feature */
771 const char *var = purple_xmlnode_get_attrib(child, "var");
772 if (var)
773 info->features = g_list_prepend(info->features, g_strdup(var));
774 } else if (purple_strequal(child->name, "x")) {
775 if (purple_strequal(child->xmlns, "jabber:x:data")) {
776 /* x-data form */
777 PurpleXmlNode *dataform = purple_xmlnode_copy(child);
778 info->forms = g_list_append(info->forms, dataform);
782 return info;
785 static gint jabber_caps_xdata_field_compare(gconstpointer a, gconstpointer b)
787 const JabberDataFormField *ac = a;
788 const JabberDataFormField *bc = b;
790 return strcmp(ac->var, bc->var);
793 static GList* jabber_caps_xdata_get_fields(const PurpleXmlNode *x)
795 GList *fields = NULL;
796 PurpleXmlNode *field;
798 if (!x)
799 return NULL;
801 for (field = purple_xmlnode_get_child(x, "field"); field; field = purple_xmlnode_get_next_twin(field)) {
802 PurpleXmlNode *value;
803 JabberDataFormField *xdatafield = g_new0(JabberDataFormField, 1);
804 xdatafield->var = g_strdup(purple_xmlnode_get_attrib(field, "var"));
806 for (value = purple_xmlnode_get_child(field, "value"); value; value = purple_xmlnode_get_next_twin(value)) {
807 gchar *val = purple_xmlnode_get_data(value);
808 xdatafield->values = g_list_prepend(xdatafield->values, val);
811 xdatafield->values = g_list_sort(xdatafield->values, (GCompareFunc)strcmp);
812 fields = g_list_prepend(fields, xdatafield);
815 fields = g_list_sort(fields, jabber_caps_xdata_field_compare);
816 return fields;
819 static void
820 append_escaped_string(GChecksum *hash, const gchar *str)
822 g_return_if_fail(hash != NULL);
824 if (str && *str) {
825 char *tmp = g_markup_escape_text(str, -1);
826 g_checksum_update(hash, (const guchar *)tmp, -1);
827 g_free(tmp);
830 g_checksum_update(hash, (const guchar *)"<", -1);
833 gchar *jabber_caps_calculate_hash(JabberCapsClientInfo *info,
834 GChecksumType hash_type)
836 GChecksum *hash;
837 GList *node;
838 guint8 *checksum;
839 gsize checksum_size;
840 gchar *ret;
842 if (!info)
843 return NULL;
845 /* sort identities, features and x-data forms */
846 info->identities = g_list_sort(info->identities, jabber_identity_compare);
847 info->features = g_list_sort(info->features, (GCompareFunc)strcmp);
848 info->forms = g_list_sort(info->forms, jabber_xdata_compare);
850 hash = g_checksum_new(hash_type);
852 if (hash == NULL) {
853 return NULL;
856 /* Add identities to the hash data */
857 for (node = info->identities; node; node = node->next) {
858 JabberIdentity *id = (JabberIdentity*)node->data;
859 char *category = g_markup_escape_text(id->category, -1);
860 char *type = g_markup_escape_text(id->type, -1);
861 char *lang = NULL;
862 char *name = NULL;
863 char *tmp;
865 if (id->lang)
866 lang = g_markup_escape_text(id->lang, -1);
867 if (id->name)
868 name = g_markup_escape_text(id->name, -1);
870 tmp = g_strconcat(category, "/", type, "/", lang ? lang : "",
871 "/", name ? name : "", "<", NULL);
873 g_checksum_update(hash, (const guchar *)tmp, -1);
875 g_free(tmp);
876 g_free(category);
877 g_free(type);
878 g_free(lang);
879 g_free(name);
882 /* concat features to the verification string */
883 for (node = info->features; node; node = node->next) {
884 append_escaped_string(hash, node->data);
887 /* concat x-data forms to the verification string */
888 for(node = info->forms; node; node = node->next) {
889 PurpleXmlNode *data = (PurpleXmlNode *)node->data;
890 gchar *formtype = jabber_x_data_get_formtype(data);
891 GList *fields = jabber_caps_xdata_get_fields(data);
893 /* append FORM_TYPE's field value to the verification string */
894 append_escaped_string(hash, formtype);
895 g_free(formtype);
897 while (fields) {
898 JabberDataFormField *field = (JabberDataFormField*)fields->data;
900 if (!purple_strequal(field->var, "FORM_TYPE")) {
901 /* Append the "var" attribute */
902 append_escaped_string(hash, field->var);
903 /* Append <value/> elements' cdata */
904 while (field->values) {
905 append_escaped_string(hash, field->values->data);
906 g_free(field->values->data);
907 field->values = g_list_delete_link(field->values,
908 field->values);
910 } else {
911 g_list_free_full(field->values, g_free);
914 g_free(field->var);
915 g_free(field);
917 fields = g_list_delete_link(fields, fields);
921 checksum_size = g_checksum_type_get_length(hash_type);
922 checksum = g_new(guint8, checksum_size);
924 /* generate hash */
925 g_checksum_get_digest(hash, checksum, &checksum_size);
927 ret = g_base64_encode(checksum, checksum_size);
928 g_free(checksum);
930 return ret;
933 void jabber_caps_calculate_own_hash(JabberStream *js) {
934 JabberCapsClientInfo info;
935 GList *iter = NULL;
936 GList *features = NULL;
938 if (!jabber_identities && !jabber_features) {
939 /* This really shouldn't ever happen */
940 purple_debug_warning("jabber", "No features or identities, cannot calculate own caps hash.\n");
941 g_free(js->caps_hash);
942 js->caps_hash = NULL;
943 return;
946 /* build the currently-supported list of features */
947 if (jabber_features) {
948 for (iter = jabber_features; iter; iter = iter->next) {
949 JabberFeature *feat = iter->data;
950 if(!feat->is_enabled || feat->is_enabled(js, feat->namespace)) {
951 features = g_list_append(features, feat->namespace);
956 info.features = features;
957 /* TODO: This copy can go away, I think, since jabber_identities
958 * is pre-sorted, so the sort in calculate_hash should be idempotent.
959 * However, I want to test that. --darkrain
961 info.identities = g_list_copy(jabber_identities);
962 info.forms = NULL;
964 g_free(js->caps_hash);
965 js->caps_hash = jabber_caps_calculate_hash(&info, G_CHECKSUM_SHA1);
966 g_list_free(info.identities);
967 g_list_free(info.features);
970 const gchar* jabber_caps_get_own_hash(JabberStream *js)
972 if (!js->caps_hash)
973 jabber_caps_calculate_own_hash(js);
975 return js->caps_hash;
978 void jabber_caps_broadcast_change()
980 GList *node, *accounts = purple_accounts_get_all_active();
982 for (node = accounts; node; node = node->next) {
983 PurpleAccount *account = node->data;
984 const char *protocol_id = purple_account_get_protocol_id(account);
985 if (purple_strequal("prpl-jabber", protocol_id) && purple_account_is_connected(account)) {
986 PurpleConnection *gc = purple_account_get_connection(account);
987 jabber_presence_send(purple_connection_get_protocol_data(gc), TRUE);
991 g_list_free(accounts);