Mercurial > ~mikael > mcabber > hg
annotate mcabber/mcabber/caps.c @ 1999:51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
author | Hermitifier |
---|---|
date | Mon, 03 Oct 2011 16:00:34 +0200 |
parents | c30fa2baf387 |
children | 76d7c5721210 |
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> | |
1999
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
23 #include <string.h> |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
24 #include <sys/stat.h> |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
25 #include <unistd.h> |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
26 #include <fcntl.h> |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
27 |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
28 #include "settings.h" |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
29 #include "utils.h" |
1600 | 30 |
31 typedef struct { | |
32 char *category; | |
1999
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
33 char *type; |
1600 | 34 char *name; |
1999
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
35 } identity; |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
36 |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
37 typedef struct { |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
38 GHashTable *fields; |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
39 } dataform; |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
40 |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
41 typedef struct { |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
42 GHashTable *identities; |
1600 | 43 GHashTable *features; |
1999
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
44 GHashTable *forms; |
1600 | 45 } caps; |
46 | |
47 static GHashTable *caps_cache = NULL; | |
48 | |
49 void caps_destroy(gpointer data) | |
50 { | |
51 caps *c = data; | |
1999
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
52 g_hash_table_destroy(c->identities); |
1600 | 53 g_hash_table_destroy(c->features); |
1999
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
54 g_hash_table_destroy(c->forms); |
1600 | 55 g_free(c); |
56 } | |
57 | |
1999
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
58 void identity_destroy(gpointer data) |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
59 { |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
60 identity *i = data; |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
61 g_free(i->category); |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
62 g_free(i->type); |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
63 g_free(i->name); |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
64 g_free(i); |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
65 } |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
66 |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
67 void form_destroy(gpointer data) |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
68 { |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
69 dataform *f = data; |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
70 g_hash_table_destroy(f->fields); |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
71 g_free(f); |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
72 } |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
73 |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
74 void field_destroy(gpointer data) |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
75 { |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
76 GList *v = data; |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
77 g_list_free_full(v, g_free); |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
78 } |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
79 |
1600 | 80 void caps_init(void) |
81 { | |
82 if (!caps_cache) | |
83 caps_cache = g_hash_table_new_full(g_str_hash, g_str_equal, | |
84 g_free, caps_destroy); | |
85 } | |
86 | |
87 void caps_free(void) | |
88 { | |
89 if (caps_cache) { | |
90 g_hash_table_destroy(caps_cache); | |
91 caps_cache = NULL; | |
92 } | |
93 } | |
94 | |
1999
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
95 void caps_add(const char *hash) |
1600 | 96 { |
97 if (!hash) | |
98 return; | |
99 caps *c = g_new0(caps, 1); | |
100 c->features = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, NULL); | |
1999
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
101 c->identities = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, identity_destroy); |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
102 c->forms = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, form_destroy); |
1600 | 103 g_hash_table_insert(caps_cache, g_strdup(hash), c); |
104 } | |
105 | |
1999
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
106 void caps_remove(const char *hash) |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
107 { |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
108 if (!hash) |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
109 return; |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
110 g_hash_table_remove(caps_cache, hash); |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
111 } |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
112 |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
113 void caps_move_to_local(char *hash, char *bjid) |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
114 { |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
115 char *orig_hash; |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
116 caps *c = NULL; |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
117 if (!hash || !bjid) |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
118 return; |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
119 g_hash_table_lookup_extended(caps_cache, hash, (gpointer*)&orig_hash, (gpointer*)&c); |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
120 if (c) { |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
121 g_hash_table_steal(caps_cache, hash); |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
122 g_free(orig_hash); |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
123 g_hash_table_replace(caps_cache, g_strdup_printf("%s/#%s", bjid, hash), c); |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
124 // solidus is guaranteed to never appear in bare jid |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
125 // hash will not appear in base64 encoded hash |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
126 // sequence "/#" is deterministic separator, and allows to identify local cache entry |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
127 } |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
128 } |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
129 |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
130 int caps_has_hash(const char *hash, const char *bjid) |
1600 | 131 { |
1999
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
132 caps *c = NULL; |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
133 if (!hash) |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
134 return 0; |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
135 c = g_hash_table_lookup(caps_cache, hash); |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
136 if (!c && bjid) { |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
137 char *key = g_strdup_printf("%s/#%s", bjid, hash); |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
138 c = g_hash_table_lookup(caps_cache, key); |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
139 g_free(key); |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
140 } |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
141 return (c != NULL); |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
142 } |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
143 |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
144 void caps_add_identity(const char *hash, |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
145 const char *category, |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
146 const char *name, |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
147 const char *type, |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
148 const char *lang) |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
149 { |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
150 caps *c; |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
151 if (!hash || !category || !type) |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
152 return; |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
153 if (!lang) |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
154 lang = ""; |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
155 |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
156 c = g_hash_table_lookup(caps_cache, hash); |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
157 if (c) { |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
158 identity *i = g_new0(identity, 1); |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
159 |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
160 i->category = g_strdup(category); |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
161 i->name = g_strdup(name); |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
162 i->type = g_strdup(type); |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
163 g_hash_table_replace(c->identities, g_strdup(lang), i); |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
164 } |
1600 | 165 } |
166 | |
167 void caps_set_identity(char *hash, | |
168 const char *category, | |
169 const char *name, | |
170 const char *type) | |
171 { | |
1999
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
172 caps_add_identity(hash, category, name, type, NULL); |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
173 } |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
174 |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
175 void caps_add_dataform(const char *hash, const char *formtype) |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
176 { |
1600 | 177 caps *c; |
1999
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
178 if (!formtype) |
1600 | 179 return; |
180 c = g_hash_table_lookup(caps_cache, hash); | |
181 if (c) { | |
1999
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
182 dataform *d = g_new0(dataform, 1); |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
183 char *f = g_strdup(formtype); |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
184 |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
185 d->fields = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, field_destroy); |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
186 g_hash_table_replace(c->forms, f, d); |
1600 | 187 } |
188 } | |
189 | |
1999
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
190 gint _strcmp_sort(gconstpointer a, gconstpointer b) |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
191 { |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
192 return g_strcmp0(a, b); |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
193 } |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
194 |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
195 void caps_add_dataform_field(const char *hash, const char *formtype, |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
196 const char *field, const char *value) |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
197 { |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
198 caps *c; |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
199 if (!formtype || !field || !value) |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
200 return; |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
201 c = g_hash_table_lookup(caps_cache, hash); |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
202 if (c) { |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
203 dataform *d; |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
204 d = g_hash_table_lookup(c->forms, formtype); |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
205 if (d) { |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
206 gpointer key, val; |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
207 char *f; |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
208 GList *v = NULL; |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
209 if (g_hash_table_lookup_extended(d->fields, field, &key, &val)) { |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
210 g_hash_table_steal(d->fields, field); |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
211 g_free(key); |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
212 v = val; |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
213 } |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
214 f = g_strdup(field); |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
215 v = g_list_insert_sorted(v, g_strdup(value), _strcmp_sort); |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
216 g_hash_table_replace(d->fields, f, v); |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
217 } |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
218 } |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
219 } |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
220 |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
221 void caps_add_feature(const char *hash, const char *feature) |
1600 | 222 { |
223 caps *c; | |
1872 | 224 if (!hash || !feature) |
1600 | 225 return; |
226 c = g_hash_table_lookup(caps_cache, hash); | |
227 if (c) { | |
228 char *f = g_strdup(feature); | |
1645
d3cd4db23f55
Fix for double-inserted features
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1600
diff
changeset
|
229 g_hash_table_replace(c->features, f, f); |
1600 | 230 } |
231 } | |
232 | |
1999
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
233 int caps_has_feature(const char *hash, char *feature, char *bjid) |
1600 | 234 { |
1999
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
235 caps *c = NULL; |
1872 | 236 if (!hash || !feature) |
1600 | 237 return 0; |
238 c = g_hash_table_lookup(caps_cache, hash); | |
1999
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
239 if (!c && bjid) { |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
240 char *key = g_strdup_printf("%s/#%s", bjid, hash); |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
241 c = g_hash_table_lookup(caps_cache, key); |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
242 g_free(key); |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
243 } |
1600 | 244 if (c) |
245 return (g_hash_table_lookup(c->features, feature) != NULL); | |
246 return 0; | |
247 } | |
248 | |
249 static GFunc _foreach_function; | |
250 | |
251 void _caps_foreach_helper(gpointer key, gpointer value, gpointer user_data) | |
252 { | |
253 // GFunc func = (GFunc)user_data; | |
254 _foreach_function(value, user_data); | |
255 } | |
256 | |
257 void caps_foreach_feature(const char *hash, GFunc func, gpointer user_data) | |
258 { | |
259 caps *c; | |
260 if (!hash) | |
261 return; | |
262 c = g_hash_table_lookup(caps_cache, hash); | |
263 if (!c) | |
264 return; | |
265 _foreach_function = func; | |
266 g_hash_table_foreach(c->features, _caps_foreach_helper, user_data); | |
267 } | |
268 | |
1729
e6e89b1d7831
Minor style and header updates
Mikael Berthe <mikael@lilotux.net>
parents:
1668
diff
changeset
|
269 // Generates the sha1 hash for the special capability "" and returns it |
1600 | 270 const char *caps_generate(void) |
271 { | |
1999
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
272 GList *features, *langs; |
1600 | 273 GChecksum *sha1; |
274 guint8 digest[20]; | |
275 gsize digest_size = 20; | |
276 gchar *hash, *old_hash = NULL; | |
277 caps *old_caps; | |
278 caps *c = g_hash_table_lookup(caps_cache, ""); | |
279 | |
280 g_hash_table_steal(caps_cache, ""); | |
281 sha1 = g_checksum_new(G_CHECKSUM_SHA1); | |
1999
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
282 |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
283 langs = g_hash_table_get_keys(c->identities); |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
284 langs = g_list_sort(langs, _strcmp_sort); |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
285 { |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
286 identity *i; |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
287 GList *lang; |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
288 char *identity_S; |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
289 for (lang=langs; lang; lang=lang->next) { |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
290 i = g_hash_table_lookup(c->identities, lang->data); |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
291 identity_S = g_strdup_printf("%s/%s/%s/%s<", i->category, i->type, |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
292 (char *)lang->data, i->name ? i->name : ""); |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
293 g_checksum_update(sha1, (guchar *)identity_S, -1); |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
294 g_free(identity_S); |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
295 } |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
296 } |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
297 g_list_free(langs); |
1600 | 298 |
1662
63ebb1d32c7d
Fix for entity capabilities hash generation.
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1645
diff
changeset
|
299 features = g_hash_table_get_values(c->features); |
1600 | 300 features = g_list_sort(features, _strcmp_sort); |
1662
63ebb1d32c7d
Fix for entity capabilities hash generation.
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1645
diff
changeset
|
301 { |
63ebb1d32c7d
Fix for entity capabilities hash generation.
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1645
diff
changeset
|
302 GList *feature; |
63ebb1d32c7d
Fix for entity capabilities hash generation.
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1645
diff
changeset
|
303 for (feature=features; feature; feature=feature->next) { |
63ebb1d32c7d
Fix for entity capabilities hash generation.
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1645
diff
changeset
|
304 g_checksum_update(sha1, feature->data, -1); |
63ebb1d32c7d
Fix for entity capabilities hash generation.
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1645
diff
changeset
|
305 g_checksum_update(sha1, (guchar *)"<", -1); |
63ebb1d32c7d
Fix for entity capabilities hash generation.
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1645
diff
changeset
|
306 } |
1600 | 307 } |
308 g_list_free(features); | |
309 | |
310 g_checksum_get_digest(sha1, digest, &digest_size); | |
311 hash = g_base64_encode(digest, digest_size); | |
312 g_checksum_free(sha1); | |
313 g_hash_table_lookup_extended(caps_cache, hash, | |
314 (gpointer *)&old_hash, (gpointer *)&old_caps); | |
315 g_hash_table_insert(caps_cache, hash, c); | |
316 if (old_hash) | |
317 return old_hash; | |
318 else | |
319 return hash; | |
320 } | |
321 | |
1999
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
322 gboolean caps_verify(const char *hash, char *function) |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
323 { |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
324 GList *features, *langs, *forms; |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
325 GChecksum *checksum; |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
326 guint8 digest[20]; |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
327 gsize digest_size = 20; |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
328 gchar *local_hash; |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
329 gboolean match = FALSE; |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
330 caps *c = g_hash_table_lookup(caps_cache, hash); |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
331 |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
332 if (!g_strcmp0(function, "sha-1")) { |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
333 checksum = g_checksum_new(G_CHECKSUM_SHA1); |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
334 } else if (!g_strcmp0(function, "md5")) { |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
335 checksum = g_checksum_new(G_CHECKSUM_MD5); |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
336 digest_size = 16; |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
337 } else |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
338 return FALSE; |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
339 |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
340 langs = g_hash_table_get_keys(c->identities); |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
341 langs = g_list_sort(langs, _strcmp_sort); |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
342 { |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
343 identity *i; |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
344 GList *lang; |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
345 char *identity_S; |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
346 for (lang=langs; lang; lang=lang->next) { |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
347 i = g_hash_table_lookup(c->identities, lang->data); |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
348 identity_S = g_strdup_printf("%s/%s/%s/%s<", i->category, i->type, |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
349 (char *)lang->data, i->name ? i->name : ""); |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
350 g_checksum_update(checksum, (guchar *)identity_S, -1); |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
351 g_free(identity_S); |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
352 } |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
353 } |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
354 g_list_free(langs); |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
355 |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
356 features = g_hash_table_get_values(c->features); |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
357 features = g_list_sort(features, _strcmp_sort); |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
358 { |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
359 GList *feature; |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
360 for (feature=features; feature; feature=feature->next) { |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
361 g_checksum_update(checksum, feature->data, -1); |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
362 g_checksum_update(checksum, (guchar *)"<", -1); |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
363 } |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
364 } |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
365 g_list_free(features); |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
366 |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
367 forms = g_hash_table_get_keys(c->forms); |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
368 forms = g_list_sort(forms, _strcmp_sort); |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
369 { |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
370 dataform *d; |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
371 GList *form, *fields; |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
372 for (form=forms; form; form=form->next) { |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
373 d = g_hash_table_lookup(c->forms, form->data); |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
374 g_checksum_update(checksum, form->data, -1); |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
375 g_checksum_update(checksum, (guchar *)"<", -1); |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
376 fields = g_hash_table_get_keys(d->fields); |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
377 fields = g_list_sort(fields, _strcmp_sort); |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
378 { |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
379 GList *field; |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
380 GList *values; |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
381 for (field=fields; field; field=field->next) { |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
382 g_checksum_update(checksum, field->data, -1); |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
383 g_checksum_update(checksum, (guchar *)"<", -1); |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
384 values = g_hash_table_lookup(d->fields, field->data); |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
385 { |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
386 GList *value; |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
387 for (value=values; value; value=value->next) { |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
388 g_checksum_update(checksum, value->data, -1); |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
389 g_checksum_update(checksum, (guchar *)"<", -1); |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
390 } |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
391 } |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
392 } |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
393 } |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
394 g_list_free(fields); |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
395 } |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
396 } |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
397 g_list_free(forms); |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
398 |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
399 g_checksum_get_digest(checksum, digest, &digest_size); |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
400 local_hash = g_base64_encode(digest, digest_size); |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
401 g_checksum_free(checksum); |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
402 |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
403 match = !g_strcmp0(hash, local_hash); |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
404 |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
405 g_free(local_hash); |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
406 return match; |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
407 } |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
408 |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
409 static gchar* caps_get_filename(const char* hash) |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
410 { |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
411 gchar *hash_fs = g_strdup (hash); |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
412 gchar *dir = (gchar *) settings_opt_get ("caps_directory"); |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
413 gchar *file = NULL; |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
414 |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
415 if (!dir) |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
416 goto caps_filename_return; |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
417 |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
418 { |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
419 const gchar *valid_fs = |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
420 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+="; |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
421 g_strcanon(hash_fs, valid_fs, '-'); |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
422 } |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
423 |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
424 dir = expand_filename (dir); |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
425 file = g_strdup_printf ("%s/%s.ini", dir, hash_fs); |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
426 g_free(dir); |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
427 |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
428 caps_filename_return: |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
429 g_free(hash_fs); |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
430 return file; |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
431 } |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
432 |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
433 void caps_copy_to_persistent(const char* hash, char* xml) |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
434 { |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
435 gchar *file; |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
436 GList *features, *langs, *forms; |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
437 GKeyFile *key_file; |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
438 caps *c; |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
439 int fd; |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
440 |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
441 g_free (xml); |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
442 |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
443 c = g_hash_table_lookup (caps_cache, hash); |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
444 if (!c) |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
445 goto caps_copy_return; |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
446 |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
447 file = caps_get_filename (hash); |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
448 if (!file) |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
449 goto caps_copy_return; |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
450 |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
451 fd = open (file, O_WRONLY|O_CREAT|O_EXCL, S_IRUSR|S_IWUSR); |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
452 if (fd == -1) |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
453 goto caps_copy_exists; |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
454 |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
455 key_file = g_key_file_new (); |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
456 |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
457 langs = g_hash_table_get_keys (c->identities); |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
458 { |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
459 identity *i; |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
460 GList *lang; |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
461 gchar *group; |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
462 for (lang=langs; lang; lang=lang->next) { |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
463 i = g_hash_table_lookup (c->identities, lang->data); |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
464 group = g_strdup_printf("identity_%s", (gchar *)lang->data); |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
465 g_key_file_set_string (key_file, group, "category", i->category); |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
466 g_key_file_set_string (key_file, group, "type", i->type); |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
467 g_key_file_set_string (key_file, group, "name", i->name); |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
468 g_free (group); |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
469 } |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
470 } |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
471 g_list_free (langs); |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
472 |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
473 features = g_hash_table_get_values (c->features); |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
474 { |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
475 GList *feature; |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
476 gchar **string_list; |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
477 gint i; |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
478 |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
479 i = g_list_length (features); |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
480 string_list = g_new (gchar*, i + 1); |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
481 i = 0; |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
482 for (feature=features; feature; feature=feature->next) { |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
483 string_list[i] = g_strdup(feature->data); |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
484 ++i; |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
485 } |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
486 string_list[i] = NULL; |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
487 |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
488 g_key_file_set_string_list (key_file, "features", "features", |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
489 (const gchar**)string_list, i); |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
490 g_strfreev (string_list); |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
491 } |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
492 g_list_free (features); |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
493 |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
494 forms = g_hash_table_get_keys(c->forms); |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
495 { |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
496 dataform *d; |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
497 GList *form, *fields; |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
498 gchar *group; |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
499 for (form=forms; form; form=form->next) { |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
500 d = g_hash_table_lookup (c->forms, form->data); |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
501 group = g_strdup_printf ("form_%s", (gchar *)form->data); |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
502 fields = g_hash_table_get_keys(d->fields); |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
503 { |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
504 GList *field; |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
505 GList *values; |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
506 for (field=fields; field; field=field->next) { |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
507 values = g_hash_table_lookup (d->fields, field->data); |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
508 { |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
509 GList *value; |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
510 gchar **string_list; |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
511 gint i; |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
512 i = g_list_length (values); |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
513 string_list = g_new (gchar*, i + 1); |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
514 i = 0; |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
515 for (value=values; value; value=value->next) { |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
516 string_list[i] = g_strdup(value->data); |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
517 ++i; |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
518 } |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
519 string_list[i] = NULL; |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
520 |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
521 g_key_file_set_string_list (key_file, group, field->data, |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
522 (const gchar**)string_list, i); |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
523 |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
524 g_strfreev (string_list); |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
525 } |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
526 } |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
527 } |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
528 g_list_free(fields); |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
529 g_free (group); |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
530 } |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
531 } |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
532 g_list_free (forms); |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
533 |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
534 { |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
535 gchar *data; |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
536 gsize length; |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
537 data = g_key_file_to_data (key_file, &length, NULL); |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
538 write (fd, data, length); |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
539 g_free(data); |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
540 close (fd); |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
541 } |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
542 |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
543 g_key_file_free(key_file); |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
544 caps_copy_exists: |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
545 g_free(file); |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
546 caps_copy_return: |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
547 return; |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
548 } |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
549 |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
550 gboolean caps_restore_from_persistent (const char* hash) |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
551 { |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
552 gchar *file; |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
553 GKeyFile *key_file; |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
554 gchar **groups, **group; |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
555 gboolean restored = FALSE; |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
556 |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
557 file = caps_get_filename (hash); |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
558 if (!file) |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
559 goto caps_restore_no_file; |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
560 |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
561 key_file = g_key_file_new (); |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
562 if (!g_key_file_load_from_file (key_file, file, G_KEY_FILE_NONE, NULL)) |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
563 goto caps_restore_bad_file; |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
564 |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
565 caps_add(hash); |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
566 |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
567 groups = g_key_file_get_groups (key_file, NULL); |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
568 for (group = groups; *group; ++group) { |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
569 if (!g_strcmp0(*group, "features")) { |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
570 gchar **features, **feature; |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
571 features = g_key_file_get_string_list (key_file, *group, "features", |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
572 NULL, NULL); |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
573 for (feature = features; *feature; ++feature) { |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
574 caps_add_feature(hash, *feature); |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
575 } |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
576 |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
577 g_strfreev (features); |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
578 } else if (g_str_has_prefix (*group, "identity_")) { |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
579 gchar *category, *type, *name, *lang; |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
580 |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
581 category = g_key_file_get_string(key_file, *group, "category", NULL); |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
582 type = g_key_file_get_string(key_file, *group, "type", NULL); |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
583 name = g_key_file_get_string(key_file, *group, "name", NULL); |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
584 lang = *group + 9; /* "identity_" */ |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
585 |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
586 caps_add_identity(hash, category, name, type, lang); |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
587 g_free(category); |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
588 g_free(type); |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
589 g_free(name); |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
590 } else if (g_str_has_prefix (*group, "form_")) { |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
591 gchar *formtype; |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
592 gchar **fields, **field; |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
593 formtype = *group + 5; /* "form_" */ |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
594 caps_add_dataform (hash, formtype); |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
595 |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
596 fields = g_key_file_get_keys(key_file, *group, NULL, NULL); |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
597 for (field = fields; *field; ++field) { |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
598 gchar **values, **value; |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
599 values = g_key_file_get_string_list (key_file, *group, *field, |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
600 NULL, NULL); |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
601 for (value = values; *value; ++value) { |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
602 caps_add_dataform_field (hash, formtype, *field, *value); |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
603 } |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
604 g_strfreev (values); |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
605 } |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
606 g_strfreev (fields); |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
607 } |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
608 } |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
609 g_strfreev(groups); |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
610 restored = TRUE; |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
611 |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
612 caps_restore_bad_file: |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
613 g_key_file_free (key_file); |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
614 g_free (file); |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
615 caps_restore_no_file: |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
616 return restored; |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
617 } |
51f032d5ca22
Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents:
1877
diff
changeset
|
618 |
1811 | 619 /* vim: set expandtab cindent cinoptions=>2\:2(0 sw=2 ts=2: For Vim users... */ |