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