Mercurial > pidgin
annotate src/protocols/jabber/iq.c @ 13104:e1e5462b7d81
[gaim-migrate @ 15466]
Rework lots of date parsing. I either introduced a whole lot of bugs, or I've made sure all dates are localized properly now. Only time will tell which it is... ;)
committer: Tailor Script <tailor@pidgin.im>
author | Richard Laager <rlaager@wiktel.com> |
---|---|
date | Thu, 02 Feb 2006 19:50:51 +0000 |
parents | ecd471d1eeec |
children | 02c7d18f5cc3 |
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 xmlnode *query; | |
171 time_t now_t; | |
9709 | 172 struct tm *now; |
9722 | 173 |
7014 | 174 time(&now_t); |
9709 | 175 now = localtime(&now_t); |
7014 | 176 |
8006 | 177 type = xmlnode_get_attrib(packet, "type"); |
7014 | 178 from = xmlnode_get_attrib(packet, "from"); |
179 id = xmlnode_get_attrib(packet, "id"); | |
180 | |
8006 | 181 if(type && !strcmp(type, "get")) { |
13104
e1e5462b7d81
[gaim-migrate @ 15466]
Richard Laager <rlaager@wiktel.com>
parents:
12284
diff
changeset
|
182 const char *date; |
7014 | 183 |
8006 | 184 iq = jabber_iq_new_query(js, JABBER_IQ_RESULT, "jabber:iq:time"); |
185 jabber_iq_set_id(iq, id); | |
186 xmlnode_set_attrib(iq->node, "to", from); | |
187 | |
188 query = xmlnode_get_child(iq->node, "query"); | |
7014 | 189 |
13104
e1e5462b7d81
[gaim-migrate @ 15466]
Richard Laager <rlaager@wiktel.com>
parents:
12284
diff
changeset
|
190 date = gaim_utf8_strftime("%Y%m%dT%T", now); |
e1e5462b7d81
[gaim-migrate @ 15466]
Richard Laager <rlaager@wiktel.com>
parents:
12284
diff
changeset
|
191 xmlnode_insert_data(xmlnode_new_child(query, "utc"), date, -1); |
10941 | 192 |
13104
e1e5462b7d81
[gaim-migrate @ 15466]
Richard Laager <rlaager@wiktel.com>
parents:
12284
diff
changeset
|
193 date = gaim_utf8_strftime("%Z", now); |
e1e5462b7d81
[gaim-migrate @ 15466]
Richard Laager <rlaager@wiktel.com>
parents:
12284
diff
changeset
|
194 xmlnode_insert_data(xmlnode_new_child(query, "tz"), date, -1); |
10941 | 195 |
13104
e1e5462b7d81
[gaim-migrate @ 15466]
Richard Laager <rlaager@wiktel.com>
parents:
12284
diff
changeset
|
196 date = gaim_utf8_strftime("%d %b %Y %T", now); |
e1e5462b7d81
[gaim-migrate @ 15466]
Richard Laager <rlaager@wiktel.com>
parents:
12284
diff
changeset
|
197 xmlnode_insert_data(xmlnode_new_child(query, "display"), date, -1); |
7014 | 198 |
8006 | 199 jabber_iq_send(iq); |
200 } | |
7014 | 201 } |
202 | |
8312 | 203 static void jabber_iq_version_parse(JabberStream *js, xmlnode *packet) |
7014 | 204 { |
205 JabberIq *iq; | |
8006 | 206 const char *type, *from, *id; |
7014 | 207 xmlnode *query; |
10941 | 208 char *os = NULL; |
7014 | 209 |
8006 | 210 type = xmlnode_get_attrib(packet, "type"); |
211 | |
212 if(type && !strcmp(type, "get")) { | |
10941 | 213 |
8006 | 214 if(!gaim_prefs_get_bool("/plugins/prpl/jabber/hide_os")) { |
215 struct utsname osinfo; | |
7014 | 216 |
8006 | 217 uname(&osinfo); |
218 os = g_strdup_printf("%s %s %s", osinfo.sysname, osinfo.release, | |
219 osinfo.machine); | |
220 } | |
10941 | 221 |
8006 | 222 from = xmlnode_get_attrib(packet, "from"); |
223 id = xmlnode_get_attrib(packet, "id"); | |
7014 | 224 |
8006 | 225 iq = jabber_iq_new_query(js, JABBER_IQ_RESULT, "jabber:iq:version"); |
226 xmlnode_set_attrib(iq->node, "to", from); | |
227 jabber_iq_set_id(iq, id); | |
7014 | 228 |
8006 | 229 query = xmlnode_get_child(iq->node, "query"); |
7014 | 230 |
8006 | 231 xmlnode_insert_data(xmlnode_new_child(query, "name"), PACKAGE, -1); |
232 xmlnode_insert_data(xmlnode_new_child(query, "version"), VERSION, -1); | |
233 if(os) { | |
234 xmlnode_insert_data(xmlnode_new_child(query, "os"), os, -1); | |
235 g_free(os); | |
236 } | |
10941 | 237 |
8006 | 238 jabber_iq_send(iq); |
7014 | 239 } |
240 } | |
241 | |
242 void jabber_iq_parse(JabberStream *js, xmlnode *packet) | |
243 { | |
7395 | 244 JabberCallbackData *jcd; |
8169 | 245 xmlnode *query, *error, *x; |
7014 | 246 const char *xmlns; |
8135 | 247 const char *type, *id, *from; |
7014 | 248 |
249 query = xmlnode_get_child(packet, "query"); | |
8043 | 250 type = xmlnode_get_attrib(packet, "type"); |
8135 | 251 from = xmlnode_get_attrib(packet, "from"); |
8312 | 252 id = xmlnode_get_attrib(packet, "id"); |
253 | |
254 /* First, lets see if a special callback got registered */ | |
255 | |
256 if(type && (!strcmp(type, "result") || !strcmp(type, "error"))) { | |
257 if(id && *id && (jcd = g_hash_table_lookup(js->iq_callbacks, id))) { | |
258 jcd->callback(js, packet, jcd->data); | |
259 g_hash_table_remove(js->iq_callbacks, id); | |
8314 | 260 return; |
8312 | 261 } |
262 } | |
263 | |
264 /* Apparently not, so lets see if we have a pre-defined handler */ | |
7014 | 265 |
8043 | 266 if(type && query && (xmlns = xmlnode_get_attrib(query, "xmlns"))) { |
267 if(!strcmp(type, "set")) { | |
268 if(!strcmp(xmlns, "jabber:iq:roster")) { | |
269 jabber_roster_parse(js, packet); | |
270 return; | |
271 } else if(!strcmp(xmlns, "jabber:iq:oob")) { | |
272 jabber_oob_parse(js, packet); | |
273 return; | |
8262 | 274 } else if(!strcmp(xmlns, "http://jabber.org/protocol/bytestreams")) { |
275 jabber_bytestreams_parse(js, packet); | |
276 return; | |
8043 | 277 } |
278 } else if(!strcmp(type, "get")) { | |
279 if(!strcmp(xmlns, "jabber:iq:last")) { | |
8312 | 280 jabber_iq_last_parse(js, packet); |
8043 | 281 return; |
282 } else if(!strcmp(xmlns, "jabber:iq:time")) { | |
8312 | 283 jabber_iq_time_parse(js, packet); |
8043 | 284 return; |
285 } else if(!strcmp(xmlns, "jabber:iq:version")) { | |
8312 | 286 jabber_iq_version_parse(js, packet); |
8043 | 287 return; |
288 } else if(!strcmp(xmlns, "http://jabber.org/protocol/disco#info")) { | |
289 jabber_disco_info_parse(js, packet); | |
290 return; | |
291 } else if(!strcmp(xmlns, "http://jabber.org/protocol/disco#items")) { | |
292 jabber_disco_items_parse(js, packet); | |
293 return; | |
294 } | |
295 } else if(!strcmp(type, "result")) { | |
296 if(!strcmp(xmlns, "jabber:iq:roster")) { | |
297 jabber_roster_parse(js, packet); | |
298 return; | |
299 } else if(!strcmp(xmlns, "jabber:iq:register")) { | |
300 jabber_register_parse(js, packet); | |
301 return; | |
302 } else if(!strcmp(xmlns, "http://jabber.org/protocol/disco#info")) { | |
303 jabber_disco_info_parse(js, packet); | |
304 return; | |
305 } | |
7395 | 306 } |
8262 | 307 } else { |
8312 | 308 if(xmlnode_get_child_with_namespace(packet, "si", "http://jabber.org/protocol/si")) { |
8262 | 309 jabber_si_parse(js, packet); |
310 return; | |
311 } | |
7395 | 312 } |
313 | |
8312 | 314 /* If we get here, send the default error reply mandated by XMPP-CORE */ |
8315 | 315 if(type && (!strcmp(type, "set") || !strcmp(type, "get"))) { |
316 JabberIq *iq = jabber_iq_new(js, JABBER_IQ_ERROR); | |
8135 | 317 |
8315 | 318 xmlnode_free(iq->node); |
319 iq->node = xmlnode_copy(packet); | |
320 xmlnode_set_attrib(iq->node, "to", from); | |
11825 | 321 xmlnode_remove_attrib(iq->node, "from"); |
8315 | 322 xmlnode_set_attrib(iq->node, "type", "error"); |
323 error = xmlnode_new_child(iq->node, "error"); | |
324 xmlnode_set_attrib(error, "type", "cancel"); | |
325 xmlnode_set_attrib(error, "code", "501"); | |
326 x = xmlnode_new_child(error, "feature-not-implemented"); | |
327 xmlnode_set_attrib(x, "xmlns", "urn:ietf:params:xml:ns:xmpp-stanzas"); | |
8169 | 328 |
8315 | 329 jabber_iq_send(iq); |
330 } | |
7014 | 331 } |
332 |