Wed, 23 Aug 2006 16:36:58 +0000
[gaim-migrate @ 17005]
a prpl's set_idle function can be called before the login function
this is because the signing-on signal is emitted, and there's a callback to check idle and update all the prpls attached to that signal
this meant that if you were idle, and got disconnected from jabber, upon attempting to reconnect, you'd segfault
I've changed how jabber handles idle updates to work around this. someone may want to audit the other prpls, to make sure their set_idle callbacks (if any) don't assume the connection is up
| 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
c62db98b7a2a
[gaim-migrate @ 7621]
Herman Bloggs <herman@bluedigits.com>
parents:
7014
diff
changeset
|
33 | #ifdef _WIN32 |
|
c62db98b7a2a
[gaim-migrate @ 7621]
Herman Bloggs <herman@bluedigits.com>
parents:
7014
diff
changeset
|
34 | #include "utsname.h" |
|
c62db98b7a2a
[gaim-migrate @ 7621]
Herman Bloggs <herman@bluedigits.com>
parents:
7014
diff
changeset
|
35 | #endif |
| 7014 | 36 | |
| 14356 | 37 | GHashTable *iq_handlers = NULL; |
| 38 | ||
| 39 | ||
| 7014 | 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"); | |
| 13808 | 84 | xmlnode_set_namespace(query, xmlns); |
| 7014 | 85 | |
| 86 | return iq; | |
| 87 | } | |
| 88 | ||
| 7395 | 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) | |
| 7014 | 96 | { |
| 97 | iq->callback = callback; | |
| 7395 | 98 | iq->callback_data = data; |
| 7014 | 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 | { | |
| 7395 | 117 | JabberCallbackData *jcd; |
| 7014 | 118 | g_return_if_fail(iq != NULL); |
| 119 | ||
| 120 | jabber_send(iq->js, iq->node); | |
| 121 | ||
| 7395 | 122 | if(iq->id && iq->callback) { |
| 123 | jcd = g_new0(JabberCallbackData, 1); | |
| 124 | jcd->callback = iq->callback; | |
| 125 | jcd->data = iq->callback_data; | |
| 8312 | 126 | g_hash_table_insert(iq->js->iq_callbacks, g_strdup(iq->id), jcd); |
| 7395 | 127 | } |
| 7014 | 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 | ||
| 8312 | 141 | static void jabber_iq_last_parse(JabberStream *js, xmlnode *packet) |
| 7014 | 142 | { |
| 143 | JabberIq *iq; | |
| 8006 | 144 | const char *type; |
| 7014 | 145 | const char *from; |
| 146 | const char *id; | |
| 147 | xmlnode *query; | |
| 14375 | 148 | GaimPresence *gpresence; |
| 7014 | 149 | |
| 8006 | 150 | type = xmlnode_get_attrib(packet, "type"); |
| 7014 | 151 | from = xmlnode_get_attrib(packet, "from"); |
| 152 | id = xmlnode_get_attrib(packet, "id"); | |
| 153 | ||
| 8006 | 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); | |
| 7014 | 158 | |
| 8006 | 159 | query = xmlnode_get_child(iq->node, "query"); |
| 7014 | 160 | |
| 14375 | 161 | gpresence = gaim_account_get_presence(js->gc->account); |
| 162 | ||
| 163 | if(gaim_presence_is_idle(gpresence)) { | |
| 164 | time_t idle_time = gaim_presence_get_idle_time(gpresence); | |
| 165 | char *idle_str; | |
| 166 | ||
| 167 | idle_str = g_strdup_printf("%ld", time(NULL) - idle_time); | |
| 168 | xmlnode_set_attrib(query, "seconds", idle_str); | |
| 169 | g_free(idle_str); | |
| 170 | } else { | |
| 171 | xmlnode_set_attrib(query, "seconds", "0"); | |
| 172 | } | |
| 7401 | 173 | |
| 8006 | 174 | jabber_iq_send(iq); |
| 175 | } | |
| 7014 | 176 | } |
| 177 | ||
| 8312 | 178 | static void jabber_iq_time_parse(JabberStream *js, xmlnode *packet) |
| 7014 | 179 | { |
| 8006 | 180 | const char *type, *from, *id; |
| 7014 | 181 | JabberIq *iq; |
| 182 | xmlnode *query; | |
| 183 | time_t now_t; | |
|
9709
2e73f176cc80
[gaim-migrate @ 10570]
Mark Doliner <markdoliner@pidgin.im>
parents:
8315
diff
changeset
|
184 | struct tm *now; |
|
9722
c1072806bcae
[gaim-migrate @ 10583]
Mark Doliner <markdoliner@pidgin.im>
parents:
9709
diff
changeset
|
185 | |
| 7014 | 186 | time(&now_t); |
|
9709
2e73f176cc80
[gaim-migrate @ 10570]
Mark Doliner <markdoliner@pidgin.im>
parents:
8315
diff
changeset
|
187 | now = localtime(&now_t); |
| 7014 | 188 | |
| 8006 | 189 | type = xmlnode_get_attrib(packet, "type"); |
| 7014 | 190 | from = xmlnode_get_attrib(packet, "from"); |
| 191 | id = xmlnode_get_attrib(packet, "id"); | |
| 192 | ||
| 8006 | 193 | if(type && !strcmp(type, "get")) { |
|
13105
8f9c66e4af87
[gaim-migrate @ 15466]
Richard Laager <rlaager@pidgin.im>
parents:
12284
diff
changeset
|
194 | const char *date; |
| 7014 | 195 | |
| 8006 | 196 | iq = jabber_iq_new_query(js, JABBER_IQ_RESULT, "jabber:iq:time"); |
| 197 | jabber_iq_set_id(iq, id); | |
| 198 | xmlnode_set_attrib(iq->node, "to", from); | |
| 199 | ||
| 200 | query = xmlnode_get_child(iq->node, "query"); | |
| 7014 | 201 | |
|
13105
8f9c66e4af87
[gaim-migrate @ 15466]
Richard Laager <rlaager@pidgin.im>
parents:
12284
diff
changeset
|
202 | date = gaim_utf8_strftime("%Y%m%dT%T", now); |
|
8f9c66e4af87
[gaim-migrate @ 15466]
Richard Laager <rlaager@pidgin.im>
parents:
12284
diff
changeset
|
203 | xmlnode_insert_data(xmlnode_new_child(query, "utc"), date, -1); |
| 10941 | 204 | |
|
13105
8f9c66e4af87
[gaim-migrate @ 15466]
Richard Laager <rlaager@pidgin.im>
parents:
12284
diff
changeset
|
205 | date = gaim_utf8_strftime("%Z", now); |
|
8f9c66e4af87
[gaim-migrate @ 15466]
Richard Laager <rlaager@pidgin.im>
parents:
12284
diff
changeset
|
206 | xmlnode_insert_data(xmlnode_new_child(query, "tz"), date, -1); |
| 10941 | 207 | |
|
13105
8f9c66e4af87
[gaim-migrate @ 15466]
Richard Laager <rlaager@pidgin.im>
parents:
12284
diff
changeset
|
208 | date = gaim_utf8_strftime("%d %b %Y %T", now); |
|
8f9c66e4af87
[gaim-migrate @ 15466]
Richard Laager <rlaager@pidgin.im>
parents:
12284
diff
changeset
|
209 | xmlnode_insert_data(xmlnode_new_child(query, "display"), date, -1); |
| 7014 | 210 | |
| 8006 | 211 | jabber_iq_send(iq); |
| 212 | } | |
| 7014 | 213 | } |
| 214 | ||
| 8312 | 215 | static void jabber_iq_version_parse(JabberStream *js, xmlnode *packet) |
| 7014 | 216 | { |
| 217 | JabberIq *iq; | |
| 8006 | 218 | const char *type, *from, *id; |
| 7014 | 219 | xmlnode *query; |
| 10941 | 220 | char *os = NULL; |
| 7014 | 221 | |
| 8006 | 222 | type = xmlnode_get_attrib(packet, "type"); |
| 223 | ||
| 224 | if(type && !strcmp(type, "get")) { | |
| 10941 | 225 | |
| 8006 | 226 | if(!gaim_prefs_get_bool("/plugins/prpl/jabber/hide_os")) { |
| 227 | struct utsname osinfo; | |
| 7014 | 228 | |
| 8006 | 229 | uname(&osinfo); |
| 230 | os = g_strdup_printf("%s %s %s", osinfo.sysname, osinfo.release, | |
| 231 | osinfo.machine); | |
| 232 | } | |
| 10941 | 233 | |
| 8006 | 234 | from = xmlnode_get_attrib(packet, "from"); |
| 235 | id = xmlnode_get_attrib(packet, "id"); | |
| 7014 | 236 | |
| 8006 | 237 | iq = jabber_iq_new_query(js, JABBER_IQ_RESULT, "jabber:iq:version"); |
| 238 | xmlnode_set_attrib(iq->node, "to", from); | |
| 239 | jabber_iq_set_id(iq, id); | |
| 7014 | 240 | |
| 8006 | 241 | query = xmlnode_get_child(iq->node, "query"); |
| 7014 | 242 | |
| 8006 | 243 | xmlnode_insert_data(xmlnode_new_child(query, "name"), PACKAGE, -1); |
| 244 | xmlnode_insert_data(xmlnode_new_child(query, "version"), VERSION, -1); | |
| 245 | if(os) { | |
| 246 | xmlnode_insert_data(xmlnode_new_child(query, "os"), os, -1); | |
| 247 | g_free(os); | |
| 248 | } | |
| 10941 | 249 | |
| 8006 | 250 | jabber_iq_send(iq); |
| 7014 | 251 | } |
| 252 | } | |
| 253 | ||
| 13794 | 254 | void jabber_iq_remove_callback_by_id(JabberStream *js, const char *id) |
| 255 | { | |
| 256 | g_hash_table_remove(js->iq_callbacks, id); | |
| 257 | } | |
| 258 | ||
| 7014 | 259 | void jabber_iq_parse(JabberStream *js, xmlnode *packet) |
| 260 | { | |
| 7395 | 261 | JabberCallbackData *jcd; |
| 8169 | 262 | xmlnode *query, *error, *x; |
| 7014 | 263 | const char *xmlns; |
| 8135 | 264 | const char *type, *id, *from; |
| 14356 | 265 | JabberIqHandler *jih; |
| 7014 | 266 | |
| 267 | query = xmlnode_get_child(packet, "query"); | |
| 8043 | 268 | type = xmlnode_get_attrib(packet, "type"); |
| 8135 | 269 | from = xmlnode_get_attrib(packet, "from"); |
| 8312 | 270 | id = xmlnode_get_attrib(packet, "id"); |
| 271 | ||
| 272 | /* First, lets see if a special callback got registered */ | |
| 273 | ||
| 274 | if(type && (!strcmp(type, "result") || !strcmp(type, "error"))) { | |
| 275 | if(id && *id && (jcd = g_hash_table_lookup(js->iq_callbacks, id))) { | |
| 276 | jcd->callback(js, packet, jcd->data); | |
| 13794 | 277 | jabber_iq_remove_callback_by_id(js, id); |
| 8314 | 278 | return; |
| 8312 | 279 | } |
| 280 | } | |
| 281 | ||
| 282 | /* Apparently not, so lets see if we have a pre-defined handler */ | |
| 7014 | 283 | |
| 13808 | 284 | if(type && query && (xmlns = xmlnode_get_namespace(query))) { |
| 14356 | 285 | if((jih = g_hash_table_lookup(iq_handlers, xmlns))) { |
| 286 | jih(js, packet); | |
| 8262 | 287 | return; |
| 288 | } | |
| 7395 | 289 | } |
| 290 | ||
| 14356 | 291 | |
| 292 | if(xmlnode_get_child_with_namespace(packet, "si", "http://jabber.org/protocol/si")) { | |
| 293 | jabber_si_parse(js, packet); | |
| 294 | return; | |
| 295 | } | |
| 296 | ||
| 8312 | 297 | /* If we get here, send the default error reply mandated by XMPP-CORE */ |
| 8315 | 298 | if(type && (!strcmp(type, "set") || !strcmp(type, "get"))) { |
| 299 | JabberIq *iq = jabber_iq_new(js, JABBER_IQ_ERROR); | |
| 8135 | 300 | |
| 8315 | 301 | xmlnode_free(iq->node); |
| 302 | iq->node = xmlnode_copy(packet); | |
| 303 | xmlnode_set_attrib(iq->node, "to", from); | |
|
11825
fe23dc05a8a6
[gaim-migrate @ 14116]
Mark Doliner <markdoliner@pidgin.im>
parents:
11822
diff
changeset
|
304 | xmlnode_remove_attrib(iq->node, "from"); |
| 8315 | 305 | xmlnode_set_attrib(iq->node, "type", "error"); |
| 306 | error = xmlnode_new_child(iq->node, "error"); | |
| 307 | xmlnode_set_attrib(error, "type", "cancel"); | |
| 308 | xmlnode_set_attrib(error, "code", "501"); | |
| 309 | x = xmlnode_new_child(error, "feature-not-implemented"); | |
| 13808 | 310 | xmlnode_set_namespace(x, "urn:ietf:params:xml:ns:xmpp-stanzas"); |
| 8169 | 311 | |
| 8315 | 312 | jabber_iq_send(iq); |
| 313 | } | |
| 7014 | 314 | } |
| 315 | ||
| 14356 | 316 | void jabber_iq_register_handler(const char *xmlns, JabberIqHandler handlerfunc) |
| 317 | { | |
| 318 | g_hash_table_replace(iq_handlers, g_strdup(xmlns), handlerfunc); | |
| 319 | } | |
| 320 | ||
| 321 | void jabber_iq_init(void) | |
| 322 | { | |
| 323 | iq_handlers = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, NULL); | |
| 324 | ||
| 325 | jabber_iq_register_handler("jabber:iq:roster", jabber_roster_parse); | |
| 326 | jabber_iq_register_handler("jabber:iq:oob", jabber_oob_parse); | |
| 327 | jabber_iq_register_handler("http://jabber.org/protocol/bytestreams", jabber_bytestreams_parse); | |
| 328 | jabber_iq_register_handler("jabber:iq:last", jabber_iq_last_parse); | |
| 329 | jabber_iq_register_handler("jabber:iq:time", jabber_iq_time_parse); | |
| 330 | jabber_iq_register_handler("jabber:iq:version", jabber_iq_version_parse); | |
| 331 | jabber_iq_register_handler("http://jabber.org/protocol/disco#info", jabber_disco_info_parse); | |
| 332 | jabber_iq_register_handler("http://jabber.org/protocol/disco#items", jabber_disco_items_parse); | |
| 333 | jabber_iq_register_handler("jabber:iq:register", jabber_register_parse); | |
| 334 | } | |
| 335 | ||
| 336 | void jabber_iq_uninit(void) | |
| 337 | { | |
| 338 | g_hash_table_destroy(iq_handlers); | |
| 339 | } | |
| 340 |