14192
|
1 /*
|
|
2 * gaim - Jabber Protocol Plugin
|
|
3 *
|
|
4 * Copyright (C) 2003, Nathan Walp <faceprint@faceprint.com>
|
|
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
|
|
9 * (at your option) any later version.
|
|
10 *
|
|
11 * This program is distributed in the hope that it will be useful,
|
|
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
14 * GNU 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 USA
|
|
19 *
|
|
20 */
|
|
21 #include "internal.h"
|
|
22 #include "debug.h"
|
|
23 #include "prefs.h"
|
|
24 #include "util.h"
|
|
25
|
|
26 #include "buddy.h"
|
|
27 #include "disco.h"
|
|
28 #include "iq.h"
|
|
29 #include "oob.h"
|
|
30 #include "roster.h"
|
|
31 #include "si.h"
|
|
32
|
|
33 #ifdef _WIN32
|
|
34 #include "utsname.h"
|
|
35 #endif
|
|
36
|
14294
|
37 GHashTable *iq_handlers = NULL;
|
|
38
|
|
39
|
14192
|
40 JabberIq *jabber_iq_new(JabberStream *js, JabberIqType type)
|
|
41 {
|
|
42 JabberIq *iq;
|
|
43
|
|
44 iq = g_new0(JabberIq, 1);
|
|
45
|
|
46 iq->type = type;
|
|
47
|
|
48 iq->node = xmlnode_new("iq");
|
|
49 switch(iq->type) {
|
|
50 case JABBER_IQ_SET:
|
|
51 xmlnode_set_attrib(iq->node, "type", "set");
|
|
52 break;
|
|
53 case JABBER_IQ_GET:
|
|
54 xmlnode_set_attrib(iq->node, "type", "get");
|
|
55 break;
|
|
56 case JABBER_IQ_ERROR:
|
|
57 xmlnode_set_attrib(iq->node, "type", "error");
|
|
58 break;
|
|
59 case JABBER_IQ_RESULT:
|
|
60 xmlnode_set_attrib(iq->node, "type", "result");
|
|
61 break;
|
|
62 case JABBER_IQ_NONE:
|
|
63 /* this shouldn't ever happen */
|
|
64 break;
|
|
65 }
|
|
66
|
|
67 iq->js = js;
|
|
68
|
|
69 if(type == JABBER_IQ_GET || type == JABBER_IQ_SET) {
|
|
70 iq->id = jabber_get_next_id(js);
|
|
71 xmlnode_set_attrib(iq->node, "id", iq->id);
|
|
72 }
|
|
73
|
|
74 return iq;
|
|
75 }
|
|
76
|
|
77 JabberIq *jabber_iq_new_query(JabberStream *js, JabberIqType type,
|
|
78 const char *xmlns)
|
|
79 {
|
|
80 JabberIq *iq = jabber_iq_new(js, type);
|
|
81 xmlnode *query;
|
|
82
|
|
83 query = xmlnode_new_child(iq->node, "query");
|
|
84 xmlnode_set_namespace(query, xmlns);
|
|
85
|
|
86 return iq;
|
|
87 }
|
|
88
|
|
89 typedef struct _JabberCallbackData {
|
|
90 JabberIqCallback *callback;
|
|
91 gpointer data;
|
|
92 } JabberCallbackData;
|
|
93
|
|
94 void
|
|
95 jabber_iq_set_callback(JabberIq *iq, JabberIqCallback *callback, gpointer data)
|
|
96 {
|
|
97 iq->callback = callback;
|
|
98 iq->callback_data = data;
|
|
99 }
|
|
100
|
|
101 void jabber_iq_set_id(JabberIq *iq, const char *id)
|
|
102 {
|
|
103 if(iq->id)
|
|
104 g_free(iq->id);
|
|
105
|
|
106 if(id) {
|
|
107 xmlnode_set_attrib(iq->node, "id", id);
|
|
108 iq->id = g_strdup(id);
|
|
109 } else {
|
|
110 xmlnode_remove_attrib(iq->node, "id");
|
|
111 iq->id = NULL;
|
|
112 }
|
|
113 }
|
|
114
|
|
115 void jabber_iq_send(JabberIq *iq)
|
|
116 {
|
|
117 JabberCallbackData *jcd;
|
|
118 g_return_if_fail(iq != NULL);
|
|
119
|
|
120 jabber_send(iq->js, iq->node);
|
|
121
|
|
122 if(iq->id && iq->callback) {
|
|
123 jcd = g_new0(JabberCallbackData, 1);
|
|
124 jcd->callback = iq->callback;
|
|
125 jcd->data = iq->callback_data;
|
|
126 g_hash_table_insert(iq->js->iq_callbacks, g_strdup(iq->id), jcd);
|
|
127 }
|
|
128
|
|
129 jabber_iq_free(iq);
|
|
130 }
|
|
131
|
|
132 void jabber_iq_free(JabberIq *iq)
|
|
133 {
|
|
134 g_return_if_fail(iq != NULL);
|
|
135
|
|
136 g_free(iq->id);
|
|
137 xmlnode_free(iq->node);
|
|
138 g_free(iq);
|
|
139 }
|
|
140
|
|
141 static void jabber_iq_last_parse(JabberStream *js, xmlnode *packet)
|
|
142 {
|
|
143 JabberIq *iq;
|
|
144 const char *type;
|
|
145 const char *from;
|
|
146 const char *id;
|
|
147 xmlnode *query;
|
14391
|
148 char *idle_time;
|
14192
|
149
|
|
150 type = xmlnode_get_attrib(packet, "type");
|
|
151 from = xmlnode_get_attrib(packet, "from");
|
|
152 id = xmlnode_get_attrib(packet, "id");
|
|
153
|
|
154 if(type && !strcmp(type, "get")) {
|
|
155 iq = jabber_iq_new_query(js, JABBER_IQ_RESULT, "jabber:iq:last");
|
|
156 jabber_iq_set_id(iq, id);
|
|
157 xmlnode_set_attrib(iq->node, "to", from);
|
|
158
|
|
159 query = xmlnode_get_child(iq->node, "query");
|
|
160
|
14391
|
161 idle_time = g_strdup_printf("%ld", js->idle ? time(NULL) - js->idle : 0);
|
|
162 xmlnode_set_attrib(query, "seconds", idle_time);
|
|
163 g_free(idle_time);
|
14192
|
164
|
|
165 jabber_iq_send(iq);
|
|
166 }
|
|
167 }
|
|
168
|
|
169 static void jabber_iq_time_parse(JabberStream *js, xmlnode *packet)
|
|
170 {
|
|
171 const char *type, *from, *id;
|
|
172 JabberIq *iq;
|
|
173 xmlnode *query;
|
|
174 time_t now_t;
|
|
175 struct tm *now;
|
|
176
|
|
177 time(&now_t);
|
|
178 now = localtime(&now_t);
|
|
179
|
|
180 type = xmlnode_get_attrib(packet, "type");
|
|
181 from = xmlnode_get_attrib(packet, "from");
|
|
182 id = xmlnode_get_attrib(packet, "id");
|
|
183
|
|
184 if(type && !strcmp(type, "get")) {
|
|
185 const char *date;
|
|
186
|
|
187 iq = jabber_iq_new_query(js, JABBER_IQ_RESULT, "jabber:iq:time");
|
|
188 jabber_iq_set_id(iq, id);
|
|
189 xmlnode_set_attrib(iq->node, "to", from);
|
|
190
|
|
191 query = xmlnode_get_child(iq->node, "query");
|
|
192
|
|
193 date = gaim_utf8_strftime("%Y%m%dT%T", now);
|
|
194 xmlnode_insert_data(xmlnode_new_child(query, "utc"), date, -1);
|
|
195
|
|
196 date = gaim_utf8_strftime("%Z", now);
|
|
197 xmlnode_insert_data(xmlnode_new_child(query, "tz"), date, -1);
|
|
198
|
|
199 date = gaim_utf8_strftime("%d %b %Y %T", now);
|
|
200 xmlnode_insert_data(xmlnode_new_child(query, "display"), date, -1);
|
|
201
|
|
202 jabber_iq_send(iq);
|
|
203 }
|
|
204 }
|
|
205
|
|
206 static void jabber_iq_version_parse(JabberStream *js, xmlnode *packet)
|
|
207 {
|
|
208 JabberIq *iq;
|
|
209 const char *type, *from, *id;
|
|
210 xmlnode *query;
|
|
211 char *os = NULL;
|
|
212
|
|
213 type = xmlnode_get_attrib(packet, "type");
|
|
214
|
|
215 if(type && !strcmp(type, "get")) {
|
|
216
|
|
217 if(!gaim_prefs_get_bool("/plugins/prpl/jabber/hide_os")) {
|
|
218 struct utsname osinfo;
|
|
219
|
|
220 uname(&osinfo);
|
|
221 os = g_strdup_printf("%s %s %s", osinfo.sysname, osinfo.release,
|
|
222 osinfo.machine);
|
|
223 }
|
|
224
|
|
225 from = xmlnode_get_attrib(packet, "from");
|
|
226 id = xmlnode_get_attrib(packet, "id");
|
|
227
|
|
228 iq = jabber_iq_new_query(js, JABBER_IQ_RESULT, "jabber:iq:version");
|
|
229 xmlnode_set_attrib(iq->node, "to", from);
|
|
230 jabber_iq_set_id(iq, id);
|
|
231
|
|
232 query = xmlnode_get_child(iq->node, "query");
|
|
233
|
|
234 xmlnode_insert_data(xmlnode_new_child(query, "name"), PACKAGE, -1);
|
|
235 xmlnode_insert_data(xmlnode_new_child(query, "version"), VERSION, -1);
|
|
236 if(os) {
|
|
237 xmlnode_insert_data(xmlnode_new_child(query, "os"), os, -1);
|
|
238 g_free(os);
|
|
239 }
|
|
240
|
|
241 jabber_iq_send(iq);
|
|
242 }
|
|
243 }
|
|
244
|
|
245 void jabber_iq_remove_callback_by_id(JabberStream *js, const char *id)
|
|
246 {
|
|
247 g_hash_table_remove(js->iq_callbacks, id);
|
|
248 }
|
|
249
|
|
250 void jabber_iq_parse(JabberStream *js, xmlnode *packet)
|
|
251 {
|
|
252 JabberCallbackData *jcd;
|
|
253 xmlnode *query, *error, *x;
|
|
254 const char *xmlns;
|
|
255 const char *type, *id, *from;
|
14294
|
256 JabberIqHandler *jih;
|
14192
|
257
|
|
258 query = xmlnode_get_child(packet, "query");
|
|
259 type = xmlnode_get_attrib(packet, "type");
|
|
260 from = xmlnode_get_attrib(packet, "from");
|
|
261 id = xmlnode_get_attrib(packet, "id");
|
|
262
|
|
263 /* First, lets see if a special callback got registered */
|
|
264
|
|
265 if(type && (!strcmp(type, "result") || !strcmp(type, "error"))) {
|
|
266 if(id && *id && (jcd = g_hash_table_lookup(js->iq_callbacks, id))) {
|
|
267 jcd->callback(js, packet, jcd->data);
|
|
268 jabber_iq_remove_callback_by_id(js, id);
|
|
269 return;
|
|
270 }
|
|
271 }
|
|
272
|
|
273 /* Apparently not, so lets see if we have a pre-defined handler */
|
|
274
|
|
275 if(type && query && (xmlns = xmlnode_get_namespace(query))) {
|
14294
|
276 if((jih = g_hash_table_lookup(iq_handlers, xmlns))) {
|
|
277 jih(js, packet);
|
14192
|
278 return;
|
|
279 }
|
|
280 }
|
|
281
|
14294
|
282
|
|
283 if(xmlnode_get_child_with_namespace(packet, "si", "http://jabber.org/protocol/si")) {
|
|
284 jabber_si_parse(js, packet);
|
|
285 return;
|
|
286 }
|
|
287
|
14192
|
288 /* If we get here, send the default error reply mandated by XMPP-CORE */
|
|
289 if(type && (!strcmp(type, "set") || !strcmp(type, "get"))) {
|
|
290 JabberIq *iq = jabber_iq_new(js, JABBER_IQ_ERROR);
|
|
291
|
|
292 xmlnode_free(iq->node);
|
|
293 iq->node = xmlnode_copy(packet);
|
|
294 xmlnode_set_attrib(iq->node, "to", from);
|
|
295 xmlnode_remove_attrib(iq->node, "from");
|
|
296 xmlnode_set_attrib(iq->node, "type", "error");
|
|
297 error = xmlnode_new_child(iq->node, "error");
|
|
298 xmlnode_set_attrib(error, "type", "cancel");
|
|
299 xmlnode_set_attrib(error, "code", "501");
|
|
300 x = xmlnode_new_child(error, "feature-not-implemented");
|
|
301 xmlnode_set_namespace(x, "urn:ietf:params:xml:ns:xmpp-stanzas");
|
|
302
|
|
303 jabber_iq_send(iq);
|
|
304 }
|
|
305 }
|
|
306
|
14294
|
307 void jabber_iq_register_handler(const char *xmlns, JabberIqHandler handlerfunc)
|
|
308 {
|
|
309 g_hash_table_replace(iq_handlers, g_strdup(xmlns), handlerfunc);
|
|
310 }
|
|
311
|
|
312 void jabber_iq_init(void)
|
|
313 {
|
|
314 iq_handlers = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, NULL);
|
|
315
|
|
316 jabber_iq_register_handler("jabber:iq:roster", jabber_roster_parse);
|
|
317 jabber_iq_register_handler("jabber:iq:oob", jabber_oob_parse);
|
|
318 jabber_iq_register_handler("http://jabber.org/protocol/bytestreams", jabber_bytestreams_parse);
|
|
319 jabber_iq_register_handler("jabber:iq:last", jabber_iq_last_parse);
|
|
320 jabber_iq_register_handler("jabber:iq:time", jabber_iq_time_parse);
|
|
321 jabber_iq_register_handler("jabber:iq:version", jabber_iq_version_parse);
|
|
322 jabber_iq_register_handler("http://jabber.org/protocol/disco#info", jabber_disco_info_parse);
|
|
323 jabber_iq_register_handler("http://jabber.org/protocol/disco#items", jabber_disco_items_parse);
|
|
324 jabber_iq_register_handler("jabber:iq:register", jabber_register_parse);
|
|
325 }
|
|
326
|
|
327 void jabber_iq_uninit(void)
|
|
328 {
|
|
329 g_hash_table_destroy(iq_handlers);
|
|
330 }
|
|
331
|