Mercurial > ~mikael > mcabber > hg
annotate mcabber/mcabber/caps.c @ 1909:9c14153e2580
Do not display unhandled IQ result messages to the log window
We display the message only in the debug log file, because these messages
are usually ignored anyway (ideally we would create a handler explicitly
when sending the initial IQ request).
Thanks to VarLog for the report!
author | Mikael Berthe <mikael@lilotux.net> |
---|---|
date | Sun, 18 Apr 2010 14:14:05 +0200 |
parents | c30fa2baf387 |
children | 51f032d5ca22 |
rev | line source |
---|---|
1600 | 1 /* |
2 * caps.c -- Entity Capabilities Cache for mcabber | |
3 * | |
1872 | 4 * Copyright (C) 2008-2010 Frank Zschockelt <mcabber@freakysoft.de> |
1600 | 5 * |
6 * This program is free software; you can redistribute it and/or modify | |
7 * it under the terms of the GNU General Public License as published by | |
8 * the Free Software Foundation; either version 2 of the License, or (at | |
9 * your option) any later version. | |
10 * | |
11 * This program is distributed in the hope that it will be useful, but | |
12 * WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
14 * General Public License for more details. | |
15 * | |
16 * You should have received a copy of the GNU General Public License | |
17 * along with this program; if not, write to the Free Software | |
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 | |
19 * USA | |
20 */ | |
21 | |
22 #include <glib.h> | |
23 | |
24 typedef struct { | |
25 char *category; | |
26 char *name; | |
27 char *type; | |
28 GHashTable *features; | |
29 } caps; | |
30 | |
31 static GHashTable *caps_cache = NULL; | |
32 | |
33 void caps_destroy(gpointer data) | |
34 { | |
35 caps *c = data; | |
36 g_free(c->category); | |
37 g_free(c->name); | |
38 g_free(c->type); | |
39 g_hash_table_destroy(c->features); | |
40 g_free(c); | |
41 } | |
42 | |
43 void caps_init(void) | |
44 { | |
45 if (!caps_cache) | |
46 caps_cache = g_hash_table_new_full(g_str_hash, g_str_equal, | |
47 g_free, caps_destroy); | |
48 } | |
49 | |
50 void caps_free(void) | |
51 { | |
52 if (caps_cache) { | |
53 g_hash_table_destroy(caps_cache); | |
54 caps_cache = NULL; | |
55 } | |
56 } | |
57 | |
58 void caps_add(char *hash) | |
59 { | |
60 if (!hash) | |
61 return; | |
62 caps *c = g_new0(caps, 1); | |
63 c->features = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, NULL); | |
64 g_hash_table_insert(caps_cache, g_strdup(hash), c); | |
65 } | |
66 | |
67 int caps_has_hash(const char *hash) | |
68 { | |
69 return (hash != NULL && (g_hash_table_lookup(caps_cache, hash) != NULL)); | |
70 } | |
71 | |
72 void caps_set_identity(char *hash, | |
73 const char *category, | |
74 const char *name, | |
75 const char *type) | |
76 { | |
77 caps *c; | |
1873
1f5015ef43e8
Accept identity without name
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1872
diff
changeset
|
78 if (!hash || !category || !type) |
1600 | 79 return; |
80 | |
81 c = g_hash_table_lookup(caps_cache, hash); | |
82 if (c) { | |
83 c->category = g_strdup(category); | |
84 c->name = g_strdup(name); | |
85 c->type = g_strdup(type); | |
86 } | |
87 } | |
88 | |
89 void caps_add_feature(char *hash, const char *feature) | |
90 { | |
91 caps *c; | |
1872 | 92 if (!hash || !feature) |
1600 | 93 return; |
94 c = g_hash_table_lookup(caps_cache, hash); | |
95 if (c) { | |
96 char *f = g_strdup(feature); | |
1645
d3cd4db23f55
Fix for double-inserted features
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1600
diff
changeset
|
97 g_hash_table_replace(c->features, f, f); |
1600 | 98 } |
99 } | |
100 | |
101 int caps_has_feature(char *hash, char *feature) | |
102 { | |
103 caps *c; | |
1872 | 104 if (!hash || !feature) |
1600 | 105 return 0; |
106 c = g_hash_table_lookup(caps_cache, hash); | |
107 if (c) | |
108 return (g_hash_table_lookup(c->features, feature) != NULL); | |
109 return 0; | |
110 } | |
111 | |
112 static GFunc _foreach_function; | |
113 | |
114 void _caps_foreach_helper(gpointer key, gpointer value, gpointer user_data) | |
115 { | |
116 // GFunc func = (GFunc)user_data; | |
117 _foreach_function(value, user_data); | |
118 } | |
119 | |
120 void caps_foreach_feature(const char *hash, GFunc func, gpointer user_data) | |
121 { | |
122 caps *c; | |
123 if (!hash) | |
124 return; | |
125 c = g_hash_table_lookup(caps_cache, hash); | |
126 if (!c) | |
127 return; | |
128 _foreach_function = func; | |
129 g_hash_table_foreach(c->features, _caps_foreach_helper, user_data); | |
130 } | |
131 | |
132 gint _strcmp_sort(gconstpointer a, gconstpointer b) | |
133 { | |
134 return g_strcmp0(a, b); | |
135 } | |
136 | |
1729
e6e89b1d7831
Minor style and header updates
Mikael Berthe <mikael@lilotux.net>
parents:
1668
diff
changeset
|
137 // Generates the sha1 hash for the special capability "" and returns it |
1600 | 138 const char *caps_generate(void) |
139 { | |
140 char *identity; | |
141 GList *features; | |
142 GChecksum *sha1; | |
143 guint8 digest[20]; | |
144 gsize digest_size = 20; | |
145 gchar *hash, *old_hash = NULL; | |
146 caps *old_caps; | |
147 caps *c = g_hash_table_lookup(caps_cache, ""); | |
148 | |
149 g_hash_table_steal(caps_cache, ""); | |
150 sha1 = g_checksum_new(G_CHECKSUM_SHA1); | |
1877
c30fa2baf387
Correctly process missing name in identity
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1873
diff
changeset
|
151 identity = g_strdup_printf("%s/%s//%s<", c->category, c->type, |
c30fa2baf387
Correctly process missing name in identity
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1873
diff
changeset
|
152 c->name ? c->name : ""); |
1600 | 153 g_checksum_update(sha1, (guchar*)identity, -1); |
154 g_free(identity); | |
155 | |
1662
63ebb1d32c7d
Fix for entity capabilities hash generation.
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1645
diff
changeset
|
156 features = g_hash_table_get_values(c->features); |
1600 | 157 features = g_list_sort(features, _strcmp_sort); |
1662
63ebb1d32c7d
Fix for entity capabilities hash generation.
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1645
diff
changeset
|
158 { |
63ebb1d32c7d
Fix for entity capabilities hash generation.
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1645
diff
changeset
|
159 GList *feature; |
63ebb1d32c7d
Fix for entity capabilities hash generation.
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1645
diff
changeset
|
160 for (feature=features; feature; feature=feature->next) { |
63ebb1d32c7d
Fix for entity capabilities hash generation.
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1645
diff
changeset
|
161 g_checksum_update(sha1, feature->data, -1); |
63ebb1d32c7d
Fix for entity capabilities hash generation.
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1645
diff
changeset
|
162 g_checksum_update(sha1, (guchar *)"<", -1); |
63ebb1d32c7d
Fix for entity capabilities hash generation.
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1645
diff
changeset
|
163 } |
1600 | 164 } |
165 g_list_free(features); | |
166 | |
167 g_checksum_get_digest(sha1, digest, &digest_size); | |
168 hash = g_base64_encode(digest, digest_size); | |
169 g_checksum_free(sha1); | |
170 g_hash_table_lookup_extended(caps_cache, hash, | |
171 (gpointer *)&old_hash, (gpointer *)&old_caps); | |
172 g_hash_table_insert(caps_cache, hash, c); | |
173 if (old_hash) | |
174 return old_hash; | |
175 else | |
176 return hash; | |
177 } | |
178 | |
1811 | 179 /* vim: set expandtab cindent cinoptions=>2\:2(0 sw=2 ts=2: For Vim users... */ |