Mercurial > pidgin
annotate src/protocols/jabber/iq.c @ 11407:f9c74bf11ae0
[gaim-migrate @ 13642]
sf patch #1252769, from Federico Schwindt
Fix crashes due to non terminate strings
committer: Tailor Script <tailor@pidgin.im>
author | Mark Doliner <mark@kingant.net> |
---|---|
date | Thu, 01 Sep 2005 04:21:29 +0000 |
parents | cef48e318125 |
children | cd20599f220b |
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" | |
10941 | 24 #include "util.h" |
7014 | 25 |
7395 | 26 #include "buddy.h" |
8312 | 27 #include "disco.h" |
7014 | 28 #include "iq.h" |
7170 | 29 #include "oob.h" |
7014 | 30 #include "roster.h" |
7395 | 31 #include "si.h" |
7014 | 32 |
7058
06e7697f3fae
[gaim-migrate @ 7621]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
7014
diff
changeset
|
33 #ifdef _WIN32 |
06e7697f3fae
[gaim-migrate @ 7621]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
7014
diff
changeset
|
34 #include "utsname.h" |
06e7697f3fae
[gaim-migrate @ 7621]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
7014
diff
changeset
|
35 #endif |
7014 | 36 |
37 JabberIq *jabber_iq_new(JabberStream *js, JabberIqType type) | |
38 { | |
39 JabberIq *iq; | |
40 | |
41 iq = g_new0(JabberIq, 1); | |
42 | |
43 iq->type = type; | |
44 | |
45 iq->node = xmlnode_new("iq"); | |
46 switch(iq->type) { | |
47 case JABBER_IQ_SET: | |
48 xmlnode_set_attrib(iq->node, "type", "set"); | |
49 break; | |
50 case JABBER_IQ_GET: | |
51 xmlnode_set_attrib(iq->node, "type", "get"); | |
52 break; | |
53 case JABBER_IQ_ERROR: | |
54 xmlnode_set_attrib(iq->node, "type", "error"); | |
55 break; | |
56 case JABBER_IQ_RESULT: | |
57 xmlnode_set_attrib(iq->node, "type", "result"); | |
58 break; | |
59 case JABBER_IQ_NONE: | |
60 /* this shouldn't ever happen */ | |
61 break; | |
62 } | |
63 | |
64 iq->js = js; | |
65 | |
66 if(type == JABBER_IQ_GET || type == JABBER_IQ_SET) { | |
67 iq->id = jabber_get_next_id(js); | |
68 xmlnode_set_attrib(iq->node, "id", iq->id); | |
69 } | |
70 | |
71 return iq; | |
72 } | |
73 | |
74 JabberIq *jabber_iq_new_query(JabberStream *js, JabberIqType type, | |
75 const char *xmlns) | |
76 { | |
77 JabberIq *iq = jabber_iq_new(js, type); | |
78 xmlnode *query; | |
79 | |
80 query = xmlnode_new_child(iq->node, "query"); | |
81 xmlnode_set_attrib(query, "xmlns", xmlns); | |
82 | |
83 return iq; | |
84 } | |
85 | |
7395 | 86 typedef struct _JabberCallbackData { |
87 JabberIqCallback *callback; | |
88 gpointer data; | |
89 } JabberCallbackData; | |
90 | |
91 void | |
92 jabber_iq_set_callback(JabberIq *iq, JabberIqCallback *callback, gpointer data) | |
7014 | 93 { |
94 iq->callback = callback; | |
7395 | 95 iq->callback_data = data; |
7014 | 96 } |
97 | |
98 void jabber_iq_set_id(JabberIq *iq, const char *id) | |
99 { | |
100 if(iq->id) | |
101 g_free(iq->id); | |
102 | |
103 if(id) { | |
104 xmlnode_set_attrib(iq->node, "id", id); | |
105 iq->id = g_strdup(id); | |
106 } else { | |
107 xmlnode_remove_attrib(iq->node, "id"); | |
108 iq->id = NULL; | |
109 } | |
110 } | |
111 | |
112 void jabber_iq_send(JabberIq *iq) | |
113 { | |
7395 | 114 JabberCallbackData *jcd; |
7014 | 115 g_return_if_fail(iq != NULL); |
116 | |
117 jabber_send(iq->js, iq->node); | |
118 | |
7395 | 119 if(iq->id && iq->callback) { |
120 jcd = g_new0(JabberCallbackData, 1); | |
121 jcd->callback = iq->callback; | |
122 jcd->data = iq->callback_data; | |
8312 | 123 g_hash_table_insert(iq->js->iq_callbacks, g_strdup(iq->id), jcd); |
7395 | 124 } |
7014 | 125 |
126 jabber_iq_free(iq); | |
127 } | |
128 | |
129 void jabber_iq_free(JabberIq *iq) | |
130 { | |
131 g_return_if_fail(iq != NULL); | |
132 | |
133 g_free(iq->id); | |
134 xmlnode_free(iq->node); | |
135 g_free(iq); | |
136 } | |
137 | |
8312 | 138 static void jabber_iq_last_parse(JabberStream *js, xmlnode *packet) |
7014 | 139 { |
140 JabberIq *iq; | |
8006 | 141 const char *type; |
7014 | 142 const char *from; |
143 const char *id; | |
144 xmlnode *query; | |
145 char *idle_time; | |
146 | |
8006 | 147 type = xmlnode_get_attrib(packet, "type"); |
7014 | 148 from = xmlnode_get_attrib(packet, "from"); |
149 id = xmlnode_get_attrib(packet, "id"); | |
150 | |
8006 | 151 if(type && !strcmp(type, "get")) { |
152 iq = jabber_iq_new_query(js, JABBER_IQ_RESULT, "jabber:iq:last"); | |
153 jabber_iq_set_id(iq, id); | |
154 xmlnode_set_attrib(iq->node, "to", from); | |
7014 | 155 |
8006 | 156 query = xmlnode_get_child(iq->node, "query"); |
7014 | 157 |
8006 | 158 idle_time = g_strdup_printf("%ld", js->idle ? time(NULL) - js->idle : 0); |
159 xmlnode_set_attrib(query, "seconds", idle_time); | |
160 g_free(idle_time); | |
7401 | 161 |
8006 | 162 jabber_iq_send(iq); |
163 } | |
7014 | 164 } |
165 | |
8312 | 166 static void jabber_iq_time_parse(JabberStream *js, xmlnode *packet) |
7014 | 167 { |
8006 | 168 const char *type, *from, *id; |
7014 | 169 JabberIq *iq; |
170 char buf[1024]; | |
171 xmlnode *query; | |
172 time_t now_t; | |
9709 | 173 struct tm *now; |
9722 | 174 |
7014 | 175 time(&now_t); |
9709 | 176 now = localtime(&now_t); |
7014 | 177 |
8006 | 178 type = xmlnode_get_attrib(packet, "type"); |
7014 | 179 from = xmlnode_get_attrib(packet, "from"); |
180 id = xmlnode_get_attrib(packet, "id"); | |
181 | |
8006 | 182 if(type && !strcmp(type, "get")) { |
10941 | 183 char *utf8; |
7014 | 184 |
8006 | 185 iq = jabber_iq_new_query(js, JABBER_IQ_RESULT, "jabber:iq:time"); |
186 jabber_iq_set_id(iq, id); | |
187 xmlnode_set_attrib(iq->node, "to", from); | |
188 | |
189 query = xmlnode_get_child(iq->node, "query"); | |
7014 | 190 |
9709 | 191 strftime(buf, sizeof(buf), "%Y%m%dT%T", now); |
8006 | 192 xmlnode_insert_data(xmlnode_new_child(query, "utc"), buf, -1); |
10941 | 193 |
9709 | 194 strftime(buf, sizeof(buf), "%Z", now); |
10941 | 195 if((utf8 = gaim_utf8_try_convert(buf))) { |
196 xmlnode_insert_data(xmlnode_new_child(query, "tz"), utf8, -1); | |
197 g_free(utf8); | |
198 } | |
199 | |
9709 | 200 strftime(buf, sizeof(buf), "%d %b %Y %T", now); |
10941 | 201 if((utf8 = gaim_utf8_try_convert(buf))) { |
202 xmlnode_insert_data(xmlnode_new_child(query, "display"), utf8, -1); | |
203 g_free(utf8); | |
204 } | |
7014 | 205 |
8006 | 206 jabber_iq_send(iq); |
207 } | |
7014 | 208 } |
209 | |
8312 | 210 static void jabber_iq_version_parse(JabberStream *js, xmlnode *packet) |
7014 | 211 { |
212 JabberIq *iq; | |
8006 | 213 const char *type, *from, *id; |
7014 | 214 xmlnode *query; |
10941 | 215 char *os = NULL; |
7014 | 216 |
8006 | 217 type = xmlnode_get_attrib(packet, "type"); |
218 | |
219 if(type && !strcmp(type, "get")) { | |
10941 | 220 |
8006 | 221 if(!gaim_prefs_get_bool("/plugins/prpl/jabber/hide_os")) { |
222 struct utsname osinfo; | |
7014 | 223 |
8006 | 224 uname(&osinfo); |
225 os = g_strdup_printf("%s %s %s", osinfo.sysname, osinfo.release, | |
226 osinfo.machine); | |
227 } | |
10941 | 228 |
8006 | 229 from = xmlnode_get_attrib(packet, "from"); |
230 id = xmlnode_get_attrib(packet, "id"); | |
7014 | 231 |
8006 | 232 iq = jabber_iq_new_query(js, JABBER_IQ_RESULT, "jabber:iq:version"); |
233 xmlnode_set_attrib(iq->node, "to", from); | |
234 jabber_iq_set_id(iq, id); | |
7014 | 235 |
8006 | 236 query = xmlnode_get_child(iq->node, "query"); |
7014 | 237 |
8006 | 238 xmlnode_insert_data(xmlnode_new_child(query, "name"), PACKAGE, -1); |
239 xmlnode_insert_data(xmlnode_new_child(query, "version"), VERSION, -1); | |
240 if(os) { | |
241 xmlnode_insert_data(xmlnode_new_child(query, "os"), os, -1); | |
242 g_free(os); | |
243 } | |
10941 | 244 |
8006 | 245 jabber_iq_send(iq); |
7014 | 246 } |
247 } | |
248 | |
249 void jabber_iq_parse(JabberStream *js, xmlnode *packet) | |
250 { | |
7395 | 251 JabberCallbackData *jcd; |
8169 | 252 xmlnode *query, *error, *x; |
7014 | 253 const char *xmlns; |
8135 | 254 const char *type, *id, *from; |
7014 | 255 |
256 query = xmlnode_get_child(packet, "query"); | |
8043 | 257 type = xmlnode_get_attrib(packet, "type"); |
8135 | 258 from = xmlnode_get_attrib(packet, "from"); |
8312 | 259 id = xmlnode_get_attrib(packet, "id"); |
260 | |
261 /* First, lets see if a special callback got registered */ | |
262 | |
263 if(type && (!strcmp(type, "result") || !strcmp(type, "error"))) { | |
264 if(id && *id && (jcd = g_hash_table_lookup(js->iq_callbacks, id))) { | |
265 jcd->callback(js, packet, jcd->data); | |
266 g_hash_table_remove(js->iq_callbacks, id); | |
8314 | 267 return; |
8312 | 268 } |
269 } | |
270 | |
271 /* Apparently not, so lets see if we have a pre-defined handler */ | |
7014 | 272 |
8043 | 273 if(type && query && (xmlns = xmlnode_get_attrib(query, "xmlns"))) { |
274 if(!strcmp(type, "set")) { | |
275 if(!strcmp(xmlns, "jabber:iq:roster")) { | |
276 jabber_roster_parse(js, packet); | |
277 return; | |
278 } else if(!strcmp(xmlns, "jabber:iq:oob")) { | |
279 jabber_oob_parse(js, packet); | |
280 return; | |
8262 | 281 } else if(!strcmp(xmlns, "http://jabber.org/protocol/bytestreams")) { |
282 jabber_bytestreams_parse(js, packet); | |
283 return; | |
8043 | 284 } |
285 } else if(!strcmp(type, "get")) { | |
286 if(!strcmp(xmlns, "jabber:iq:last")) { | |
8312 | 287 jabber_iq_last_parse(js, packet); |
8043 | 288 return; |
289 } else if(!strcmp(xmlns, "jabber:iq:time")) { | |
8312 | 290 jabber_iq_time_parse(js, packet); |
8043 | 291 return; |
292 } else if(!strcmp(xmlns, "jabber:iq:version")) { | |
8312 | 293 jabber_iq_version_parse(js, packet); |
8043 | 294 return; |
295 } else if(!strcmp(xmlns, "http://jabber.org/protocol/disco#info")) { | |
296 jabber_disco_info_parse(js, packet); | |
297 return; | |
298 } else if(!strcmp(xmlns, "http://jabber.org/protocol/disco#items")) { | |
299 jabber_disco_items_parse(js, packet); | |
300 return; | |
301 } | |
302 } else if(!strcmp(type, "result")) { | |
303 if(!strcmp(xmlns, "jabber:iq:roster")) { | |
304 jabber_roster_parse(js, packet); | |
305 return; | |
306 } else if(!strcmp(xmlns, "jabber:iq:register")) { | |
307 jabber_register_parse(js, packet); | |
308 return; | |
309 } else if(!strcmp(xmlns, "http://jabber.org/protocol/disco#info")) { | |
310 jabber_disco_info_parse(js, packet); | |
311 return; | |
312 } | |
7395 | 313 } |
8262 | 314 } else { |
8312 | 315 if(xmlnode_get_child_with_namespace(packet, "si", "http://jabber.org/protocol/si")) { |
8262 | 316 jabber_si_parse(js, packet); |
317 return; | |
318 } | |
7395 | 319 } |
320 | |
321 | |
8312 | 322 /* If we get here, send the default error reply mandated by XMPP-CORE */ |
8135 | 323 |
8315 | 324 if(type && (!strcmp(type, "set") || !strcmp(type, "get"))) { |
325 JabberIq *iq = jabber_iq_new(js, JABBER_IQ_ERROR); | |
8135 | 326 |
8315 | 327 xmlnode_free(iq->node); |
328 iq->node = xmlnode_copy(packet); | |
329 xmlnode_set_attrib(iq->node, "to", from); | |
330 xmlnode_set_attrib(iq->node, "type", "error"); | |
331 error = xmlnode_new_child(iq->node, "error"); | |
332 xmlnode_set_attrib(error, "type", "cancel"); | |
333 xmlnode_set_attrib(error, "code", "501"); | |
334 x = xmlnode_new_child(error, "feature-not-implemented"); | |
335 xmlnode_set_attrib(x, "xmlns", "urn:ietf:params:xml:ns:xmpp-stanzas"); | |
8169 | 336 |
8315 | 337 jabber_iq_send(iq); |
338 } | |
7014 | 339 } |
340 |