2
* This program is free software; you can redistribute it and/or modify
3
* it under the terms of the GNU General Public License as published by
4
* the Free Software Foundation; either version 2 of the License, or
5
* (at your option) any later version.
7
* This program is distributed in the hope that it will be useful,
8
* but WITHOUT ANY WARRANTY; without even the implied warranty of
9
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
* GNU Library General Public License for more details.
12
* You should have received a copy of the GNU General Public License
13
* along with this program; if not, write to the Free Software
14
* Foundation, Inc., 51 Franklin Street, Fifth Floor Boston, MA 02110-1301, USA
28
static GHashTable *local_data_by_alt = NULL;
29
static GHashTable *local_data_by_cid = NULL;
30
static GHashTable *remote_data_by_cid = NULL;
33
jabber_data_create_from_data(gconstpointer rawdata, gsize size, const char *type,
36
JabberData *data = g_new0(JabberData, 1);
37
gchar *checksum = purple_util_get_image_checksum(rawdata, size);
40
g_snprintf(cid, sizeof(cid), "sha1+%s@bob.xmpp.org", checksum);
43
data->cid = g_strdup(cid);
44
data->type = g_strdup(type);
47
data->data = g_memdup(rawdata, size);
53
jabber_data_create_from_xml(xmlnode *tag)
55
JabberData *data = g_new0(JabberData, 1);
57
gpointer raw_data = NULL;
60
purple_debug_error("jabber", "Could not allocate data object\n");
65
/* check if this is a "data" tag */
66
if (strcmp(tag->name, "data") != 0) {
67
purple_debug_error("jabber", "Invalid data element");
72
data->cid = g_strdup(xmlnode_get_attrib(tag, "cid"));
73
data->type = g_strdup(xmlnode_get_attrib(tag, "type"));
75
raw_data = xmlnode_get_data(tag);
76
data->data = purple_base64_decode(raw_data, &size);
86
jabber_data_delete(gpointer cbdata)
88
JabberData *data = cbdata;
96
jabber_data_get_cid(const JabberData *data)
103
jabber_data_get_type(const JabberData *data)
109
jabber_data_get_size(const JabberData *data)
115
jabber_data_get_data(const JabberData *data)
121
jabber_data_get_xml_definition(const JabberData *data)
123
xmlnode *tag = xmlnode_new("data");
124
char *base64data = purple_base64_encode(data->data, data->size);
126
xmlnode_set_namespace(tag, XEP_0231_NAMESPACE);
127
xmlnode_set_attrib(tag, "cid", data->cid);
128
xmlnode_set_attrib(tag, "type", data->type);
130
xmlnode_insert_data(tag, base64data, -1);
138
jabber_data_get_xhtml_im(const JabberData *data, const gchar *alt)
140
xmlnode *img = xmlnode_new("img");
143
xmlnode_set_attrib(img, "alt", alt);
144
g_snprintf(src, sizeof(src), "cid:%s", data->cid);
145
xmlnode_set_attrib(img, "src", src);
151
jabber_data_get_xml_request(const gchar *cid)
153
xmlnode *tag = xmlnode_new("data");
155
xmlnode_set_namespace(tag, XEP_0231_NAMESPACE);
156
xmlnode_set_attrib(tag, "cid", cid);
162
jabber_data_find_local_by_alt(const gchar *alt)
164
purple_debug_info("jabber", "looking up local smiley with alt = %s\n", alt);
165
return g_hash_table_lookup(local_data_by_alt, alt);
169
jabber_data_find_local_by_cid(const gchar *cid)
171
purple_debug_info("jabber", "lookup local smiley with cid = %s\n", cid);
172
return g_hash_table_lookup(local_data_by_cid, cid);
176
jabber_data_find_remote_by_cid(const gchar *cid)
178
purple_debug_info("jabber", "lookup remote smiley with cid = %s\n", cid);
180
return g_hash_table_lookup(remote_data_by_cid, cid);
184
jabber_data_associate_local(JabberData *data, const gchar *alt)
186
purple_debug_info("jabber", "associating local smiley\n alt = %s, cid = %s\n",
187
alt, jabber_data_get_cid(data));
188
g_hash_table_insert(local_data_by_alt, g_strdup(alt), data);
189
g_hash_table_insert(local_data_by_cid, g_strdup(jabber_data_get_cid(data)),
194
jabber_data_associate_remote(JabberData *data)
196
purple_debug_info("jabber", "associating remote smiley, cid = %s\n",
197
jabber_data_get_cid(data));
198
g_hash_table_insert(remote_data_by_cid, g_strdup(jabber_data_get_cid(data)),
203
jabber_data_parse(JabberStream *js, xmlnode *packet)
205
JabberIq *result = NULL;
206
const char *who = xmlnode_get_attrib(packet, "from");
207
xmlnode *data_node = xmlnode_get_child(packet, "data");
208
const JabberData *data =
209
jabber_data_find_local_by_cid(xmlnode_get_attrib(data_node, "cid"));
212
xmlnode *item_not_found = xmlnode_new("item-not-found");
214
result = jabber_iq_new(js, JABBER_IQ_ERROR);
215
xmlnode_set_attrib(result->node, "to", who);
216
xmlnode_set_attrib(result->node, "id", xmlnode_get_attrib(packet, "id"));
217
xmlnode_insert_child(result->node, item_not_found);
219
result = jabber_iq_new(js, JABBER_IQ_RESULT);
220
xmlnode_set_attrib(result->node, "to", who);
221
xmlnode_set_attrib(result->node, "id", xmlnode_get_attrib(packet, "id"));
222
xmlnode_insert_child(result->node,
223
jabber_data_get_xml_definition(data));
225
jabber_iq_send(result);
229
jabber_data_init(void)
231
purple_debug_info("jabber", "creating hash tables for data objects\n");
232
local_data_by_alt = g_hash_table_new_full(g_str_hash, g_str_equal,
234
local_data_by_cid = g_hash_table_new_full(g_str_hash, g_str_equal,
235
g_free, jabber_data_delete);
236
remote_data_by_cid = g_hash_table_new_full(g_str_hash, g_str_equal,
237
g_free, jabber_data_delete);
241
jabber_data_uninit(void)
243
purple_debug_info("jabber", "destroying hash tables for data objects\n");
244
g_hash_table_destroy(local_data_by_alt);
245
g_hash_table_destroy(local_data_by_cid);
246
g_hash_table_destroy(remote_data_by_cid);