annotate mcabber/src/caps.c @ 1600:c5ee395fbc8c

Updated Entity Capabilities support (XEP-0115)
author franky
date Tue, 23 Sep 2008 10:59:25 +0200
parents
children d3cd4db23f55
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1600
c5ee395fbc8c Updated Entity Capabilities support (XEP-0115)
franky
parents:
diff changeset
1 /*
c5ee395fbc8c Updated Entity Capabilities support (XEP-0115)
franky
parents:
diff changeset
2 * caps.c -- Entity Capabilities Cache for mcabber
c5ee395fbc8c Updated Entity Capabilities support (XEP-0115)
franky
parents:
diff changeset
3 *
c5ee395fbc8c Updated Entity Capabilities support (XEP-0115)
franky
parents:
diff changeset
4 * Copyright (C) 2008 Frank Zschockelt <mcabber@freakysoft.de>
c5ee395fbc8c Updated Entity Capabilities support (XEP-0115)
franky
parents:
diff changeset
5 *
c5ee395fbc8c Updated Entity Capabilities support (XEP-0115)
franky
parents:
diff changeset
6 * This program is free software; you can redistribute it and/or modify
c5ee395fbc8c Updated Entity Capabilities support (XEP-0115)
franky
parents:
diff changeset
7 * it under the terms of the GNU General Public License as published by
c5ee395fbc8c Updated Entity Capabilities support (XEP-0115)
franky
parents:
diff changeset
8 * the Free Software Foundation; either version 2 of the License, or (at
c5ee395fbc8c Updated Entity Capabilities support (XEP-0115)
franky
parents:
diff changeset
9 * your option) any later version.
c5ee395fbc8c Updated Entity Capabilities support (XEP-0115)
franky
parents:
diff changeset
10 *
c5ee395fbc8c Updated Entity Capabilities support (XEP-0115)
franky
parents:
diff changeset
11 * This program is distributed in the hope that it will be useful, but
c5ee395fbc8c Updated Entity Capabilities support (XEP-0115)
franky
parents:
diff changeset
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
c5ee395fbc8c Updated Entity Capabilities support (XEP-0115)
franky
parents:
diff changeset
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
c5ee395fbc8c Updated Entity Capabilities support (XEP-0115)
franky
parents:
diff changeset
14 * General Public License for more details.
c5ee395fbc8c Updated Entity Capabilities support (XEP-0115)
franky
parents:
diff changeset
15 *
c5ee395fbc8c Updated Entity Capabilities support (XEP-0115)
franky
parents:
diff changeset
16 * You should have received a copy of the GNU General Public License
c5ee395fbc8c Updated Entity Capabilities support (XEP-0115)
franky
parents:
diff changeset
17 * along with this program; if not, write to the Free Software
c5ee395fbc8c Updated Entity Capabilities support (XEP-0115)
franky
parents:
diff changeset
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
c5ee395fbc8c Updated Entity Capabilities support (XEP-0115)
franky
parents:
diff changeset
19 * USA
c5ee395fbc8c Updated Entity Capabilities support (XEP-0115)
franky
parents:
diff changeset
20 */
c5ee395fbc8c Updated Entity Capabilities support (XEP-0115)
franky
parents:
diff changeset
21
c5ee395fbc8c Updated Entity Capabilities support (XEP-0115)
franky
parents:
diff changeset
22 #include <glib.h>
c5ee395fbc8c Updated Entity Capabilities support (XEP-0115)
franky
parents:
diff changeset
23
c5ee395fbc8c Updated Entity Capabilities support (XEP-0115)
franky
parents:
diff changeset
24 typedef struct {
c5ee395fbc8c Updated Entity Capabilities support (XEP-0115)
franky
parents:
diff changeset
25 char *category;
c5ee395fbc8c Updated Entity Capabilities support (XEP-0115)
franky
parents:
diff changeset
26 char *name;
c5ee395fbc8c Updated Entity Capabilities support (XEP-0115)
franky
parents:
diff changeset
27 char *type;
c5ee395fbc8c Updated Entity Capabilities support (XEP-0115)
franky
parents:
diff changeset
28 GHashTable *features;
c5ee395fbc8c Updated Entity Capabilities support (XEP-0115)
franky
parents:
diff changeset
29 } caps;
c5ee395fbc8c Updated Entity Capabilities support (XEP-0115)
franky
parents:
diff changeset
30
c5ee395fbc8c Updated Entity Capabilities support (XEP-0115)
franky
parents:
diff changeset
31 static GHashTable *caps_cache = NULL;
c5ee395fbc8c Updated Entity Capabilities support (XEP-0115)
franky
parents:
diff changeset
32
c5ee395fbc8c Updated Entity Capabilities support (XEP-0115)
franky
parents:
diff changeset
33 void caps_destroy(gpointer data)
c5ee395fbc8c Updated Entity Capabilities support (XEP-0115)
franky
parents:
diff changeset
34 {
c5ee395fbc8c Updated Entity Capabilities support (XEP-0115)
franky
parents:
diff changeset
35 caps *c = data;
c5ee395fbc8c Updated Entity Capabilities support (XEP-0115)
franky
parents:
diff changeset
36 g_free(c->category);
c5ee395fbc8c Updated Entity Capabilities support (XEP-0115)
franky
parents:
diff changeset
37 g_free(c->name);
c5ee395fbc8c Updated Entity Capabilities support (XEP-0115)
franky
parents:
diff changeset
38 g_free(c->type);
c5ee395fbc8c Updated Entity Capabilities support (XEP-0115)
franky
parents:
diff changeset
39 g_hash_table_destroy(c->features);
c5ee395fbc8c Updated Entity Capabilities support (XEP-0115)
franky
parents:
diff changeset
40 g_free(c);
c5ee395fbc8c Updated Entity Capabilities support (XEP-0115)
franky
parents:
diff changeset
41 }
c5ee395fbc8c Updated Entity Capabilities support (XEP-0115)
franky
parents:
diff changeset
42
c5ee395fbc8c Updated Entity Capabilities support (XEP-0115)
franky
parents:
diff changeset
43 void caps_init(void)
c5ee395fbc8c Updated Entity Capabilities support (XEP-0115)
franky
parents:
diff changeset
44 {
c5ee395fbc8c Updated Entity Capabilities support (XEP-0115)
franky
parents:
diff changeset
45 if (!caps_cache)
c5ee395fbc8c Updated Entity Capabilities support (XEP-0115)
franky
parents:
diff changeset
46 caps_cache = g_hash_table_new_full(g_str_hash, g_str_equal,
c5ee395fbc8c Updated Entity Capabilities support (XEP-0115)
franky
parents:
diff changeset
47 g_free, caps_destroy);
c5ee395fbc8c Updated Entity Capabilities support (XEP-0115)
franky
parents:
diff changeset
48 }
c5ee395fbc8c Updated Entity Capabilities support (XEP-0115)
franky
parents:
diff changeset
49
c5ee395fbc8c Updated Entity Capabilities support (XEP-0115)
franky
parents:
diff changeset
50 void caps_free(void)
c5ee395fbc8c Updated Entity Capabilities support (XEP-0115)
franky
parents:
diff changeset
51 {
c5ee395fbc8c Updated Entity Capabilities support (XEP-0115)
franky
parents:
diff changeset
52 if (caps_cache) {
c5ee395fbc8c Updated Entity Capabilities support (XEP-0115)
franky
parents:
diff changeset
53 g_hash_table_destroy(caps_cache);
c5ee395fbc8c Updated Entity Capabilities support (XEP-0115)
franky
parents:
diff changeset
54 caps_cache = NULL;
c5ee395fbc8c Updated Entity Capabilities support (XEP-0115)
franky
parents:
diff changeset
55 }
c5ee395fbc8c Updated Entity Capabilities support (XEP-0115)
franky
parents:
diff changeset
56 }
c5ee395fbc8c Updated Entity Capabilities support (XEP-0115)
franky
parents:
diff changeset
57
c5ee395fbc8c Updated Entity Capabilities support (XEP-0115)
franky
parents:
diff changeset
58 void caps_add(char *hash)
c5ee395fbc8c Updated Entity Capabilities support (XEP-0115)
franky
parents:
diff changeset
59 {
c5ee395fbc8c Updated Entity Capabilities support (XEP-0115)
franky
parents:
diff changeset
60 if (!hash)
c5ee395fbc8c Updated Entity Capabilities support (XEP-0115)
franky
parents:
diff changeset
61 return;
c5ee395fbc8c Updated Entity Capabilities support (XEP-0115)
franky
parents:
diff changeset
62 caps *c = g_new0(caps, 1);
c5ee395fbc8c Updated Entity Capabilities support (XEP-0115)
franky
parents:
diff changeset
63 c->features = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, NULL);
c5ee395fbc8c Updated Entity Capabilities support (XEP-0115)
franky
parents:
diff changeset
64 g_hash_table_insert(caps_cache, g_strdup(hash), c);
c5ee395fbc8c Updated Entity Capabilities support (XEP-0115)
franky
parents:
diff changeset
65 }
c5ee395fbc8c Updated Entity Capabilities support (XEP-0115)
franky
parents:
diff changeset
66
c5ee395fbc8c Updated Entity Capabilities support (XEP-0115)
franky
parents:
diff changeset
67 int caps_has_hash(const char *hash)
c5ee395fbc8c Updated Entity Capabilities support (XEP-0115)
franky
parents:
diff changeset
68 {
c5ee395fbc8c Updated Entity Capabilities support (XEP-0115)
franky
parents:
diff changeset
69 return (hash != NULL && (g_hash_table_lookup(caps_cache, hash) != NULL));
c5ee395fbc8c Updated Entity Capabilities support (XEP-0115)
franky
parents:
diff changeset
70 }
c5ee395fbc8c Updated Entity Capabilities support (XEP-0115)
franky
parents:
diff changeset
71
c5ee395fbc8c Updated Entity Capabilities support (XEP-0115)
franky
parents:
diff changeset
72 void caps_set_identity(char *hash,
c5ee395fbc8c Updated Entity Capabilities support (XEP-0115)
franky
parents:
diff changeset
73 const char *category,
c5ee395fbc8c Updated Entity Capabilities support (XEP-0115)
franky
parents:
diff changeset
74 const char *name,
c5ee395fbc8c Updated Entity Capabilities support (XEP-0115)
franky
parents:
diff changeset
75 const char *type)
c5ee395fbc8c Updated Entity Capabilities support (XEP-0115)
franky
parents:
diff changeset
76 {
c5ee395fbc8c Updated Entity Capabilities support (XEP-0115)
franky
parents:
diff changeset
77 caps *c;
c5ee395fbc8c Updated Entity Capabilities support (XEP-0115)
franky
parents:
diff changeset
78 if (!hash)
c5ee395fbc8c Updated Entity Capabilities support (XEP-0115)
franky
parents:
diff changeset
79 return;
c5ee395fbc8c Updated Entity Capabilities support (XEP-0115)
franky
parents:
diff changeset
80
c5ee395fbc8c Updated Entity Capabilities support (XEP-0115)
franky
parents:
diff changeset
81 c = g_hash_table_lookup(caps_cache, hash);
c5ee395fbc8c Updated Entity Capabilities support (XEP-0115)
franky
parents:
diff changeset
82 if (c) {
c5ee395fbc8c Updated Entity Capabilities support (XEP-0115)
franky
parents:
diff changeset
83 c->category = g_strdup(category);
c5ee395fbc8c Updated Entity Capabilities support (XEP-0115)
franky
parents:
diff changeset
84 c->name = g_strdup(name);
c5ee395fbc8c Updated Entity Capabilities support (XEP-0115)
franky
parents:
diff changeset
85 c->type = g_strdup(type);
c5ee395fbc8c Updated Entity Capabilities support (XEP-0115)
franky
parents:
diff changeset
86 }
c5ee395fbc8c Updated Entity Capabilities support (XEP-0115)
franky
parents:
diff changeset
87 }
c5ee395fbc8c Updated Entity Capabilities support (XEP-0115)
franky
parents:
diff changeset
88
c5ee395fbc8c Updated Entity Capabilities support (XEP-0115)
franky
parents:
diff changeset
89 void caps_add_feature(char *hash, const char *feature)
c5ee395fbc8c Updated Entity Capabilities support (XEP-0115)
franky
parents:
diff changeset
90 {
c5ee395fbc8c Updated Entity Capabilities support (XEP-0115)
franky
parents:
diff changeset
91 caps *c;
c5ee395fbc8c Updated Entity Capabilities support (XEP-0115)
franky
parents:
diff changeset
92 if (!hash)
c5ee395fbc8c Updated Entity Capabilities support (XEP-0115)
franky
parents:
diff changeset
93 return;
c5ee395fbc8c Updated Entity Capabilities support (XEP-0115)
franky
parents:
diff changeset
94 c = g_hash_table_lookup(caps_cache, hash);
c5ee395fbc8c Updated Entity Capabilities support (XEP-0115)
franky
parents:
diff changeset
95 if (c) {
c5ee395fbc8c Updated Entity Capabilities support (XEP-0115)
franky
parents:
diff changeset
96 char *f = g_strdup(feature);
c5ee395fbc8c Updated Entity Capabilities support (XEP-0115)
franky
parents:
diff changeset
97 g_hash_table_insert(c->features, f, f);
c5ee395fbc8c Updated Entity Capabilities support (XEP-0115)
franky
parents:
diff changeset
98 }
c5ee395fbc8c Updated Entity Capabilities support (XEP-0115)
franky
parents:
diff changeset
99 }
c5ee395fbc8c Updated Entity Capabilities support (XEP-0115)
franky
parents:
diff changeset
100
c5ee395fbc8c Updated Entity Capabilities support (XEP-0115)
franky
parents:
diff changeset
101 int caps_has_feature(char *hash, char *feature)
c5ee395fbc8c Updated Entity Capabilities support (XEP-0115)
franky
parents:
diff changeset
102 {
c5ee395fbc8c Updated Entity Capabilities support (XEP-0115)
franky
parents:
diff changeset
103 caps *c;
c5ee395fbc8c Updated Entity Capabilities support (XEP-0115)
franky
parents:
diff changeset
104 if (!hash)
c5ee395fbc8c Updated Entity Capabilities support (XEP-0115)
franky
parents:
diff changeset
105 return 0;
c5ee395fbc8c Updated Entity Capabilities support (XEP-0115)
franky
parents:
diff changeset
106 c = g_hash_table_lookup(caps_cache, hash);
c5ee395fbc8c Updated Entity Capabilities support (XEP-0115)
franky
parents:
diff changeset
107 if (c)
c5ee395fbc8c Updated Entity Capabilities support (XEP-0115)
franky
parents:
diff changeset
108 return (g_hash_table_lookup(c->features, feature) != NULL);
c5ee395fbc8c Updated Entity Capabilities support (XEP-0115)
franky
parents:
diff changeset
109 return 0;
c5ee395fbc8c Updated Entity Capabilities support (XEP-0115)
franky
parents:
diff changeset
110 }
c5ee395fbc8c Updated Entity Capabilities support (XEP-0115)
franky
parents:
diff changeset
111
c5ee395fbc8c Updated Entity Capabilities support (XEP-0115)
franky
parents:
diff changeset
112 static GFunc _foreach_function;
c5ee395fbc8c Updated Entity Capabilities support (XEP-0115)
franky
parents:
diff changeset
113
c5ee395fbc8c Updated Entity Capabilities support (XEP-0115)
franky
parents:
diff changeset
114 void _caps_foreach_helper(gpointer key, gpointer value, gpointer user_data)
c5ee395fbc8c Updated Entity Capabilities support (XEP-0115)
franky
parents:
diff changeset
115 {
c5ee395fbc8c Updated Entity Capabilities support (XEP-0115)
franky
parents:
diff changeset
116 // GFunc func = (GFunc)user_data;
c5ee395fbc8c Updated Entity Capabilities support (XEP-0115)
franky
parents:
diff changeset
117 _foreach_function(value, user_data);
c5ee395fbc8c Updated Entity Capabilities support (XEP-0115)
franky
parents:
diff changeset
118 }
c5ee395fbc8c Updated Entity Capabilities support (XEP-0115)
franky
parents:
diff changeset
119
c5ee395fbc8c Updated Entity Capabilities support (XEP-0115)
franky
parents:
diff changeset
120 void caps_foreach_feature(const char *hash, GFunc func, gpointer user_data)
c5ee395fbc8c Updated Entity Capabilities support (XEP-0115)
franky
parents:
diff changeset
121 {
c5ee395fbc8c Updated Entity Capabilities support (XEP-0115)
franky
parents:
diff changeset
122 caps *c;
c5ee395fbc8c Updated Entity Capabilities support (XEP-0115)
franky
parents:
diff changeset
123 if (!hash)
c5ee395fbc8c Updated Entity Capabilities support (XEP-0115)
franky
parents:
diff changeset
124 return;
c5ee395fbc8c Updated Entity Capabilities support (XEP-0115)
franky
parents:
diff changeset
125 c = g_hash_table_lookup(caps_cache, hash);
c5ee395fbc8c Updated Entity Capabilities support (XEP-0115)
franky
parents:
diff changeset
126 if (!c)
c5ee395fbc8c Updated Entity Capabilities support (XEP-0115)
franky
parents:
diff changeset
127 return;
c5ee395fbc8c Updated Entity Capabilities support (XEP-0115)
franky
parents:
diff changeset
128 _foreach_function = func;
c5ee395fbc8c Updated Entity Capabilities support (XEP-0115)
franky
parents:
diff changeset
129 g_hash_table_foreach(c->features, _caps_foreach_helper, user_data);
c5ee395fbc8c Updated Entity Capabilities support (XEP-0115)
franky
parents:
diff changeset
130 }
c5ee395fbc8c Updated Entity Capabilities support (XEP-0115)
franky
parents:
diff changeset
131
c5ee395fbc8c Updated Entity Capabilities support (XEP-0115)
franky
parents:
diff changeset
132 gint _strcmp_sort(gconstpointer a, gconstpointer b)
c5ee395fbc8c Updated Entity Capabilities support (XEP-0115)
franky
parents:
diff changeset
133 {
c5ee395fbc8c Updated Entity Capabilities support (XEP-0115)
franky
parents:
diff changeset
134 return g_strcmp0(a, b);
c5ee395fbc8c Updated Entity Capabilities support (XEP-0115)
franky
parents:
diff changeset
135 }
c5ee395fbc8c Updated Entity Capabilities support (XEP-0115)
franky
parents:
diff changeset
136
c5ee395fbc8c Updated Entity Capabilities support (XEP-0115)
franky
parents:
diff changeset
137 //generates the sha1 hash for the special capability "" and returns it
c5ee395fbc8c Updated Entity Capabilities support (XEP-0115)
franky
parents:
diff changeset
138 const char *caps_generate(void)
c5ee395fbc8c Updated Entity Capabilities support (XEP-0115)
franky
parents:
diff changeset
139 {
c5ee395fbc8c Updated Entity Capabilities support (XEP-0115)
franky
parents:
diff changeset
140 char *identity;
c5ee395fbc8c Updated Entity Capabilities support (XEP-0115)
franky
parents:
diff changeset
141 GList *features;
c5ee395fbc8c Updated Entity Capabilities support (XEP-0115)
franky
parents:
diff changeset
142 GChecksum *sha1;
c5ee395fbc8c Updated Entity Capabilities support (XEP-0115)
franky
parents:
diff changeset
143 guint8 digest[20];
c5ee395fbc8c Updated Entity Capabilities support (XEP-0115)
franky
parents:
diff changeset
144 gsize digest_size = 20;
c5ee395fbc8c Updated Entity Capabilities support (XEP-0115)
franky
parents:
diff changeset
145 gchar *hash, *old_hash = NULL;
c5ee395fbc8c Updated Entity Capabilities support (XEP-0115)
franky
parents:
diff changeset
146 caps *old_caps;
c5ee395fbc8c Updated Entity Capabilities support (XEP-0115)
franky
parents:
diff changeset
147 unsigned int i;
c5ee395fbc8c Updated Entity Capabilities support (XEP-0115)
franky
parents:
diff changeset
148 caps *c = g_hash_table_lookup(caps_cache, "");
c5ee395fbc8c Updated Entity Capabilities support (XEP-0115)
franky
parents:
diff changeset
149
c5ee395fbc8c Updated Entity Capabilities support (XEP-0115)
franky
parents:
diff changeset
150 g_hash_table_steal(caps_cache, "");
c5ee395fbc8c Updated Entity Capabilities support (XEP-0115)
franky
parents:
diff changeset
151 sha1 = g_checksum_new(G_CHECKSUM_SHA1);
c5ee395fbc8c Updated Entity Capabilities support (XEP-0115)
franky
parents:
diff changeset
152 identity = g_strdup_printf("%s/%s/%s<", c->category, c->type, c->name);
c5ee395fbc8c Updated Entity Capabilities support (XEP-0115)
franky
parents:
diff changeset
153 g_checksum_update(sha1, (guchar*)identity, -1);
c5ee395fbc8c Updated Entity Capabilities support (XEP-0115)
franky
parents:
diff changeset
154 g_free(identity);
c5ee395fbc8c Updated Entity Capabilities support (XEP-0115)
franky
parents:
diff changeset
155
c5ee395fbc8c Updated Entity Capabilities support (XEP-0115)
franky
parents:
diff changeset
156 features = g_list_copy(g_hash_table_get_values(c->features));
c5ee395fbc8c Updated Entity Capabilities support (XEP-0115)
franky
parents:
diff changeset
157 features = g_list_sort(features, _strcmp_sort);
c5ee395fbc8c Updated Entity Capabilities support (XEP-0115)
franky
parents:
diff changeset
158 for (i=0; i < g_list_length(features); i++) {
c5ee395fbc8c Updated Entity Capabilities support (XEP-0115)
franky
parents:
diff changeset
159 g_checksum_update(sha1, g_list_nth_data(features, i), -1);
c5ee395fbc8c Updated Entity Capabilities support (XEP-0115)
franky
parents:
diff changeset
160 g_checksum_update(sha1, (guchar *)"<", -1);
c5ee395fbc8c Updated Entity Capabilities support (XEP-0115)
franky
parents:
diff changeset
161 }
c5ee395fbc8c Updated Entity Capabilities support (XEP-0115)
franky
parents:
diff changeset
162 g_list_free(features);
c5ee395fbc8c Updated Entity Capabilities support (XEP-0115)
franky
parents:
diff changeset
163
c5ee395fbc8c Updated Entity Capabilities support (XEP-0115)
franky
parents:
diff changeset
164 g_checksum_get_digest(sha1, digest, &digest_size);
c5ee395fbc8c Updated Entity Capabilities support (XEP-0115)
franky
parents:
diff changeset
165 hash = g_base64_encode(digest, digest_size);
c5ee395fbc8c Updated Entity Capabilities support (XEP-0115)
franky
parents:
diff changeset
166 g_checksum_free(sha1);
c5ee395fbc8c Updated Entity Capabilities support (XEP-0115)
franky
parents:
diff changeset
167 g_hash_table_lookup_extended(caps_cache, hash,
c5ee395fbc8c Updated Entity Capabilities support (XEP-0115)
franky
parents:
diff changeset
168 (gpointer *)&old_hash, (gpointer *)&old_caps);
c5ee395fbc8c Updated Entity Capabilities support (XEP-0115)
franky
parents:
diff changeset
169 g_hash_table_insert(caps_cache, hash, c);
c5ee395fbc8c Updated Entity Capabilities support (XEP-0115)
franky
parents:
diff changeset
170 if (old_hash)
c5ee395fbc8c Updated Entity Capabilities support (XEP-0115)
franky
parents:
diff changeset
171 return old_hash;
c5ee395fbc8c Updated Entity Capabilities support (XEP-0115)
franky
parents:
diff changeset
172 else
c5ee395fbc8c Updated Entity Capabilities support (XEP-0115)
franky
parents:
diff changeset
173 return hash;
c5ee395fbc8c Updated Entity Capabilities support (XEP-0115)
franky
parents:
diff changeset
174 }
c5ee395fbc8c Updated Entity Capabilities support (XEP-0115)
franky
parents:
diff changeset
175
c5ee395fbc8c Updated Entity Capabilities support (XEP-0115)
franky
parents:
diff changeset
176 /* vim: set expandtab cindent cinoptions=>2\:2(0: For Vim users... */