Mercurial > pidgin
annotate src/protocols/jabber/iq.c @ 8262:b5dbd1839716
[gaim-migrate @ 8985]
this is jabber new-style file transfer receive support. this doesn't do much error checking or handling, but I managed to send pictures to myself from Exodus on my laptop in the living room, which would have taken twice as long were it not for VNC. i said i was going to bed 1, 2, and 3 hours ago. i should go to bed.
committer: Tailor Script <tailor@pidgin.im>
author | Nathan Walp <nwalp@pidgin.im> |
---|---|
date | Sun, 15 Feb 2004 10:11:38 +0000 |
parents | a64774143a42 |
children | ba12d8b12ab0 |
rev | line source |
---|---|
7014 | 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 | |
7395 | 25 #include "buddy.h" |
7014 | 26 #include "iq.h" |
7170 | 27 #include "oob.h" |
7014 | 28 #include "roster.h" |
7395 | 29 #include "si.h" |
7014 | 30 |
7058
06e7697f3fae
[gaim-migrate @ 7621]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
7014
diff
changeset
|
31 #ifdef _WIN32 |
06e7697f3fae
[gaim-migrate @ 7621]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
7014
diff
changeset
|
32 #include "utsname.h" |
06e7697f3fae
[gaim-migrate @ 7621]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
7014
diff
changeset
|
33 #endif |
7014 | 34 |
35 JabberIq *jabber_iq_new(JabberStream *js, JabberIqType type) | |
36 { | |
37 JabberIq *iq; | |
38 | |
39 iq = g_new0(JabberIq, 1); | |
40 | |
41 iq->type = type; | |
42 | |
43 iq->node = xmlnode_new("iq"); | |
44 switch(iq->type) { | |
45 case JABBER_IQ_SET: | |
46 xmlnode_set_attrib(iq->node, "type", "set"); | |
47 break; | |
48 case JABBER_IQ_GET: | |
49 xmlnode_set_attrib(iq->node, "type", "get"); | |
50 break; | |
51 case JABBER_IQ_ERROR: | |
52 xmlnode_set_attrib(iq->node, "type", "error"); | |
53 break; | |
54 case JABBER_IQ_RESULT: | |
55 xmlnode_set_attrib(iq->node, "type", "result"); | |
56 break; | |
57 case JABBER_IQ_NONE: | |
58 /* this shouldn't ever happen */ | |
59 break; | |
60 } | |
61 | |
62 iq->js = js; | |
63 | |
64 if(type == JABBER_IQ_GET || type == JABBER_IQ_SET) { | |
65 iq->id = jabber_get_next_id(js); | |
66 xmlnode_set_attrib(iq->node, "id", iq->id); | |
67 } | |
68 | |
69 return iq; | |
70 } | |
71 | |
72 JabberIq *jabber_iq_new_query(JabberStream *js, JabberIqType type, | |
73 const char *xmlns) | |
74 { | |
75 JabberIq *iq = jabber_iq_new(js, type); | |
76 xmlnode *query; | |
77 | |
78 query = xmlnode_new_child(iq->node, "query"); | |
79 xmlnode_set_attrib(query, "xmlns", xmlns); | |
80 | |
81 return iq; | |
82 } | |
83 | |
7395 | 84 typedef struct _JabberCallbackData { |
85 JabberIqCallback *callback; | |
86 gpointer data; | |
87 } JabberCallbackData; | |
88 | |
89 void | |
90 jabber_iq_set_callback(JabberIq *iq, JabberIqCallback *callback, gpointer data) | |
7014 | 91 { |
92 iq->callback = callback; | |
7395 | 93 iq->callback_data = data; |
7014 | 94 } |
95 | |
96 void jabber_iq_set_id(JabberIq *iq, const char *id) | |
97 { | |
98 if(iq->id) | |
99 g_free(iq->id); | |
100 | |
101 if(id) { | |
102 xmlnode_set_attrib(iq->node, "id", id); | |
103 iq->id = g_strdup(id); | |
104 } else { | |
105 xmlnode_remove_attrib(iq->node, "id"); | |
106 iq->id = NULL; | |
107 } | |
108 } | |
109 | |
110 void jabber_iq_send(JabberIq *iq) | |
111 { | |
7395 | 112 JabberCallbackData *jcd; |
7014 | 113 g_return_if_fail(iq != NULL); |
114 | |
115 jabber_send(iq->js, iq->node); | |
116 | |
7395 | 117 if(iq->id && iq->callback) { |
118 jcd = g_new0(JabberCallbackData, 1); | |
119 jcd->callback = iq->callback; | |
120 jcd->data = iq->callback_data; | |
121 g_hash_table_insert(iq->js->callbacks, g_strdup(iq->id), jcd); | |
122 } | |
7014 | 123 |
124 jabber_iq_free(iq); | |
125 } | |
126 | |
127 void jabber_iq_free(JabberIq *iq) | |
128 { | |
129 g_return_if_fail(iq != NULL); | |
130 | |
131 g_free(iq->id); | |
132 xmlnode_free(iq->node); | |
133 g_free(iq); | |
134 } | |
135 | |
136 static void jabber_iq_handle_last(JabberStream *js, xmlnode *packet) | |
137 { | |
138 JabberIq *iq; | |
8006 | 139 const char *type; |
7014 | 140 const char *from; |
141 const char *id; | |
142 xmlnode *query; | |
143 char *idle_time; | |
144 | |
8006 | 145 type = xmlnode_get_attrib(packet, "type"); |
7014 | 146 from = xmlnode_get_attrib(packet, "from"); |
147 id = xmlnode_get_attrib(packet, "id"); | |
148 | |
8006 | 149 if(type && !strcmp(type, "get")) { |
150 iq = jabber_iq_new_query(js, JABBER_IQ_RESULT, "jabber:iq:last"); | |
151 jabber_iq_set_id(iq, id); | |
152 xmlnode_set_attrib(iq->node, "to", from); | |
7014 | 153 |
8006 | 154 query = xmlnode_get_child(iq->node, "query"); |
7014 | 155 |
8006 | 156 idle_time = g_strdup_printf("%ld", js->idle ? time(NULL) - js->idle : 0); |
157 xmlnode_set_attrib(query, "seconds", idle_time); | |
158 g_free(idle_time); | |
7401 | 159 |
8006 | 160 jabber_iq_send(iq); |
161 } | |
7014 | 162 } |
163 | |
164 static void jabber_iq_handle_time(JabberStream *js, xmlnode *packet) | |
165 { | |
8006 | 166 const char *type, *from, *id; |
7014 | 167 JabberIq *iq; |
168 char buf[1024]; | |
169 xmlnode *query; | |
170 time_t now_t; | |
171 struct tm now; | |
172 time(&now_t); | |
173 localtime_r(&now_t, &now); | |
174 | |
8006 | 175 type = xmlnode_get_attrib(packet, "type"); |
7014 | 176 from = xmlnode_get_attrib(packet, "from"); |
177 id = xmlnode_get_attrib(packet, "id"); | |
178 | |
8006 | 179 if(type && !strcmp(type, "get")) { |
7014 | 180 |
8006 | 181 iq = jabber_iq_new_query(js, JABBER_IQ_RESULT, "jabber:iq:time"); |
182 jabber_iq_set_id(iq, id); | |
183 xmlnode_set_attrib(iq->node, "to", from); | |
184 | |
185 query = xmlnode_get_child(iq->node, "query"); | |
7014 | 186 |
8006 | 187 strftime(buf, sizeof(buf), "%Y%m%dT%T", &now); |
188 xmlnode_insert_data(xmlnode_new_child(query, "utc"), buf, -1); | |
189 strftime(buf, sizeof(buf), "%Z", &now); | |
190 xmlnode_insert_data(xmlnode_new_child(query, "tz"), buf, -1); | |
191 strftime(buf, sizeof(buf), "%d %b %Y %T", &now); | |
192 xmlnode_insert_data(xmlnode_new_child(query, "display"), buf, -1); | |
7014 | 193 |
8006 | 194 jabber_iq_send(iq); |
195 } | |
7014 | 196 } |
197 | |
198 static void jabber_iq_handle_version(JabberStream *js, xmlnode *packet) | |
199 { | |
200 JabberIq *iq; | |
8006 | 201 const char *type, *from, *id; |
7014 | 202 xmlnode *query; |
203 char *os = NULL; | |
204 | |
8006 | 205 type = xmlnode_get_attrib(packet, "type"); |
206 | |
207 if(type && !strcmp(type, "get")) { | |
208 | |
209 if(!gaim_prefs_get_bool("/plugins/prpl/jabber/hide_os")) { | |
210 struct utsname osinfo; | |
7014 | 211 |
8006 | 212 uname(&osinfo); |
213 os = g_strdup_printf("%s %s %s", osinfo.sysname, osinfo.release, | |
214 osinfo.machine); | |
215 } | |
7014 | 216 |
8006 | 217 from = xmlnode_get_attrib(packet, "from"); |
218 id = xmlnode_get_attrib(packet, "id"); | |
7014 | 219 |
8006 | 220 iq = jabber_iq_new_query(js, JABBER_IQ_RESULT, "jabber:iq:version"); |
221 xmlnode_set_attrib(iq->node, "to", from); | |
222 jabber_iq_set_id(iq, id); | |
7014 | 223 |
8006 | 224 query = xmlnode_get_child(iq->node, "query"); |
7014 | 225 |
8006 | 226 xmlnode_insert_data(xmlnode_new_child(query, "name"), PACKAGE, -1); |
227 xmlnode_insert_data(xmlnode_new_child(query, "version"), VERSION, -1); | |
228 if(os) { | |
229 xmlnode_insert_data(xmlnode_new_child(query, "os"), os, -1); | |
230 g_free(os); | |
231 } | |
232 | |
233 jabber_iq_send(iq); | |
7014 | 234 } |
235 } | |
236 | |
7395 | 237 #define SUPPORT_FEATURE(x) \ |
238 feature = xmlnode_new_child(query, "feature"); \ | |
239 xmlnode_set_attrib(feature, "var", x); | |
240 | |
241 | |
242 void jabber_disco_info_parse(JabberStream *js, xmlnode *packet) { | |
243 const char *from = xmlnode_get_attrib(packet, "from"); | |
244 const char *type = xmlnode_get_attrib(packet, "type"); | |
245 | |
246 if(!from || !type) | |
247 return; | |
248 | |
249 if(!strcmp(type, "get")) { | |
250 xmlnode *query, *identity, *feature; | |
251 JabberIq *iq = jabber_iq_new_query(js, JABBER_IQ_RESULT, | |
252 "http://jabber.org/protocol/disco#info"); | |
253 | |
254 jabber_iq_set_id(iq, xmlnode_get_attrib(packet, "id")); | |
255 | |
256 xmlnode_set_attrib(iq->node, "to", from); | |
257 query = xmlnode_get_child(iq->node, "query"); | |
258 | |
259 identity = xmlnode_new_child(query, "identity"); | |
260 xmlnode_set_attrib(identity, "category", "client"); | |
261 xmlnode_set_attrib(identity, "type", "pc"); /* XXX: bot, console, | |
262 * handheld, pc, phone, | |
263 * web */ | |
264 | |
265 SUPPORT_FEATURE("jabber:iq:last") | |
266 SUPPORT_FEATURE("jabber:iq:oob") | |
267 SUPPORT_FEATURE("jabber:iq:time") | |
268 SUPPORT_FEATURE("jabber:iq:version") | |
269 SUPPORT_FEATURE("jabber:x:conference") | |
270 SUPPORT_FEATURE("http://jabber.org/protocol/bytestreams") | |
271 SUPPORT_FEATURE("http://jabber.org/protocol/disco#info") | |
272 SUPPORT_FEATURE("http://jabber.org/protocol/disco#items") | |
8262 | 273 #if 0 |
274 SUPPORT_FEATURE("http://jabber.org/protocol/ibb") | |
275 #endif | |
7395 | 276 SUPPORT_FEATURE("http://jabber.org/protocol/muc") |
277 SUPPORT_FEATURE("http://jabber.org/protocol/muc#user") | |
278 SUPPORT_FEATURE("http://jabber.org/protocol/si") | |
279 SUPPORT_FEATURE("http://jabber.org/protocol/si/profile/file-transfer") | |
8043 | 280 SUPPORT_FEATURE("http://jabber.org/protocol/xhtml-im") |
7395 | 281 |
282 jabber_iq_send(iq); | |
8006 | 283 } else if(!strcmp(type, "result")) { |
7395 | 284 xmlnode *query = xmlnode_get_child(packet, "query"); |
285 xmlnode *child; | |
286 JabberID *jid; | |
287 JabberBuddy *jb; | |
288 JabberBuddyResource *jbr = NULL; | |
289 | |
290 if(!(jid = jabber_id_new(from))) | |
291 return; | |
292 | |
293 if(jid->resource && (jb = jabber_buddy_find(js, from, TRUE))) | |
294 jbr = jabber_buddy_find_resource(jb, jid->resource); | |
7445 | 295 jabber_id_free(jid); |
7395 | 296 |
297 for(child = query->child; child; child = child->next) { | |
8135 | 298 if(child->type != XMLNODE_TYPE_TAG) |
7395 | 299 continue; |
300 | |
8043 | 301 if(!strcmp(child->name, "identity")) { |
302 const char *category = xmlnode_get_attrib(child, "category"); | |
303 const char *type = xmlnode_get_attrib(child, "type"); | |
304 if(!category || !type) | |
305 continue; | |
306 | |
307 /* we found a groupchat or MUC server, add it to the list */ | |
308 /* XXX: actually check for protocol/muc or gc-1.0 support */ | |
309 if(!strcmp(category, "conference") && !strcmp(type, "text")) | |
310 js->chat_servers = g_list_append(js->chat_servers, g_strdup(from)); | |
311 | |
312 } else if(!strcmp(child->name, "feature")) { | |
7395 | 313 const char *var = xmlnode_get_attrib(child, "var"); |
314 if(!var) | |
315 continue; | |
316 | |
8043 | 317 if(jbr && !strcmp(var, "http://jabber.org/protocol/si")) |
7395 | 318 jbr->capabilities |= JABBER_CAP_SI; |
8043 | 319 else if(jbr && !strcmp(var, |
7395 | 320 "http://jabber.org/protocol/si/profile/file-transfer")) |
321 jbr->capabilities |= JABBER_CAP_SI_FILE_XFER; | |
8043 | 322 else if(jbr && !strcmp(var, "http://jabber.org/protocol/bytestreams")) |
7395 | 323 jbr->capabilities |= JABBER_CAP_BYTESTREAMS; |
324 } | |
325 } | |
326 } | |
327 } | |
328 | |
329 void jabber_disco_items_parse(JabberStream *js, xmlnode *packet) { | |
330 const char *from = xmlnode_get_attrib(packet, "from"); | |
331 const char *type = xmlnode_get_attrib(packet, "type"); | |
332 | |
333 if(!strcmp(type, "get")) { | |
334 JabberIq *iq = jabber_iq_new_query(js, JABBER_IQ_RESULT, | |
335 "http://jabber.org/protocol/disco#items"); | |
336 | |
337 jabber_iq_set_id(iq, xmlnode_get_attrib(packet, "id")); | |
338 | |
339 xmlnode_set_attrib(iq->node, "to", from); | |
340 jabber_iq_send(iq); | |
341 } | |
342 } | |
343 | |
8043 | 344 static void |
345 jabber_iq_disco_server_result_cb(JabberStream *js, xmlnode *packet, gpointer data) | |
346 { | |
347 xmlnode *query, *child; | |
348 const char *from = xmlnode_get_attrib(packet, "from"); | |
349 const char *type = xmlnode_get_attrib(packet, "type"); | |
350 | |
351 if(!from || !type) | |
352 return; | |
353 | |
354 if(strcmp(from, js->user->domain)) | |
355 return; | |
356 | |
357 if(strcmp(type, "result")) | |
358 return; | |
359 | |
360 while(js->chat_servers) { | |
361 g_free(js->chat_servers->data); | |
362 js->chat_servers = g_list_delete_link(js->chat_servers, js->chat_servers); | |
363 } | |
364 | |
365 query = xmlnode_get_child(packet, "query"); | |
366 | |
8135 | 367 for(child = xmlnode_get_child(query, "item"); child; |
368 child = xmlnode_get_next_twin(child)) { | |
8043 | 369 JabberIq *iq; |
370 const char *jid; | |
371 | |
372 if(!(jid = xmlnode_get_attrib(child, "jid"))) | |
373 continue; | |
374 | |
375 iq = jabber_iq_new_query(js, JABBER_IQ_GET, "http://jabber.org/protocol/disco#info"); | |
376 xmlnode_set_attrib(iq->node, "to", jid); | |
377 jabber_iq_send(iq); | |
378 } | |
379 } | |
380 | |
381 void jabber_iq_disco_server(JabberStream *js) | |
382 { | |
383 JabberIq *iq = jabber_iq_new_query(js, JABBER_IQ_GET, | |
384 "http://jabber.org/protocol/disco#items"); | |
385 | |
386 xmlnode_set_attrib(iq->node, "to", js->user->domain); | |
387 | |
388 jabber_iq_set_callback(iq, jabber_iq_disco_server_result_cb, NULL); | |
389 jabber_iq_send(iq); | |
390 } | |
391 | |
392 | |
7014 | 393 void jabber_iq_parse(JabberStream *js, xmlnode *packet) |
394 { | |
7395 | 395 JabberCallbackData *jcd; |
8169 | 396 xmlnode *query, *error, *x; |
7014 | 397 const char *xmlns; |
8135 | 398 const char *type, *id, *from; |
399 JabberIq *iq; | |
7014 | 400 |
401 query = xmlnode_get_child(packet, "query"); | |
8043 | 402 type = xmlnode_get_attrib(packet, "type"); |
8135 | 403 from = xmlnode_get_attrib(packet, "from"); |
7014 | 404 |
8043 | 405 if(type && query && (xmlns = xmlnode_get_attrib(query, "xmlns"))) { |
406 if(!strcmp(type, "set")) { | |
407 if(!strcmp(xmlns, "jabber:iq:roster")) { | |
408 jabber_roster_parse(js, packet); | |
409 return; | |
410 } else if(!strcmp(xmlns, "jabber:iq:oob")) { | |
411 jabber_oob_parse(js, packet); | |
412 return; | |
8262 | 413 } else if(!strcmp(xmlns, "http://jabber.org/protocol/bytestreams")) { |
414 jabber_bytestreams_parse(js, packet); | |
415 return; | |
8043 | 416 } |
417 } else if(!strcmp(type, "get")) { | |
418 if(!strcmp(xmlns, "jabber:iq:last")) { | |
419 jabber_iq_handle_last(js, packet); | |
420 return; | |
421 } else if(!strcmp(xmlns, "jabber:iq:time")) { | |
422 jabber_iq_handle_time(js, packet); | |
423 return; | |
424 } else if(!strcmp(xmlns, "jabber:iq:version")) { | |
425 jabber_iq_handle_version(js, packet); | |
426 return; | |
427 } else if(!strcmp(xmlns, "http://jabber.org/protocol/disco#info")) { | |
428 jabber_disco_info_parse(js, packet); | |
429 return; | |
430 } else if(!strcmp(xmlns, "http://jabber.org/protocol/disco#items")) { | |
431 jabber_disco_items_parse(js, packet); | |
432 return; | |
433 } | |
434 } else if(!strcmp(type, "result")) { | |
435 if(!strcmp(xmlns, "jabber:iq:roster")) { | |
436 jabber_roster_parse(js, packet); | |
437 return; | |
438 } else if(!strcmp(xmlns, "jabber:iq:register")) { | |
439 jabber_register_parse(js, packet); | |
440 return; | |
441 } else if(!strcmp(xmlns, "http://jabber.org/protocol/disco#info")) { | |
442 jabber_disco_info_parse(js, packet); | |
443 return; | |
444 } | |
7395 | 445 } |
8262 | 446 } else { |
447 xmlnode *si; | |
448 if((si = xmlnode_get_child(packet, "si")) && (xmlns = xmlnode_get_attrib(si, "xmlns")) && | |
449 !strcmp(xmlns, "http://jabber.org/protocol/si")) { | |
450 jabber_si_parse(js, packet); | |
451 return; | |
452 } | |
7395 | 453 } |
454 | |
455 /* If we got here, no pre-defined handlers got it, lets see if a special | |
456 * callback got registered */ | |
457 | |
458 id = xmlnode_get_attrib(packet, "id"); | |
459 | |
8169 | 460 if(type && (!strcmp(type, "result") || !strcmp(type, "error"))) { |
461 if(id && *id && (jcd = g_hash_table_lookup(js->callbacks, id))) { | |
462 jcd->callback(js, packet, jcd->data); | |
463 g_hash_table_remove(js->callbacks, id); | |
464 } | |
8135 | 465 return; |
466 } | |
467 | |
468 /* Default error reply mandated by XMPP-CORE */ | |
469 | |
470 iq = jabber_iq_new(js, JABBER_IQ_ERROR); | |
471 | |
8169 | 472 xmlnode_free(iq->node); |
473 iq->node = xmlnode_copy(packet); | |
474 xmlnode_set_attrib(iq->node, "to", from); | |
475 xmlnode_set_attrib(iq->node, "type", "error"); | |
476 error = xmlnode_new_child(iq->node, "error"); | |
477 xmlnode_set_attrib(error, "type", "cancel"); | |
478 xmlnode_set_attrib(error, "code", "501"); | |
479 x = xmlnode_new_child(error, "feature-not-implemented"); | |
480 xmlnode_set_attrib(x, "xmlns", "urn:ietf:params:xml:ns:xmpp-stanzas"); | |
481 | |
482 jabber_iq_send(iq); | |
7014 | 483 } |
484 |