Fri, 31 Oct 2003 02:43:58 +0000
[gaim-migrate @ 7990]
this would be the non-working start of file transfer (the real way) for jabber
also approximately eleventy billion jabber tweaks
| 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
c62db98b7a2a
[gaim-migrate @ 7621]
Herman Bloggs <herman@bluedigits.com>
parents:
7014
diff
changeset
|
31 | #ifdef _WIN32 |
|
c62db98b7a2a
[gaim-migrate @ 7621]
Herman Bloggs <herman@bluedigits.com>
parents:
7014
diff
changeset
|
32 | #include "utsname.h" |
|
c62db98b7a2a
[gaim-migrate @ 7621]
Herman Bloggs <herman@bluedigits.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; | |
| 139 | const char *from; | |
| 140 | const char *id; | |
| 141 | xmlnode *query; | |
| 142 | char *idle_time; | |
| 143 | ||
| 144 | from = xmlnode_get_attrib(packet, "from"); | |
| 145 | id = xmlnode_get_attrib(packet, "id"); | |
| 146 | ||
| 147 | iq = jabber_iq_new_query(js, JABBER_IQ_RESULT, "jabber:iq:last"); | |
| 148 | jabber_iq_set_id(iq, id); | |
| 149 | xmlnode_set_attrib(iq->node, "to", from); | |
| 150 | ||
| 151 | query = xmlnode_get_child(iq->node, "query"); | |
| 152 | ||
| 153 | idle_time = g_strdup_printf("%ld", js->idle ? time(NULL) - js->idle : 0); | |
| 154 | xmlnode_set_attrib(query, "seconds", idle_time); | |
| 155 | g_free(idle_time); | |
| 156 | } | |
| 157 | ||
| 158 | static void jabber_iq_handle_time(JabberStream *js, xmlnode *packet) | |
| 159 | { | |
| 160 | const char *from, *id; | |
| 161 | JabberIq *iq; | |
| 162 | char buf[1024]; | |
| 163 | xmlnode *query; | |
| 164 | time_t now_t; | |
| 165 | struct tm now; | |
| 166 | time(&now_t); | |
| 167 | localtime_r(&now_t, &now); | |
| 168 | ||
| 169 | from = xmlnode_get_attrib(packet, "from"); | |
| 170 | id = xmlnode_get_attrib(packet, "id"); | |
| 171 | ||
| 172 | iq = jabber_iq_new_query(js, JABBER_IQ_RESULT, "jabber:iq:time"); | |
| 173 | jabber_iq_set_id(iq, id); | |
| 174 | xmlnode_set_attrib(iq->node, "to", from); | |
| 175 | ||
| 176 | query = xmlnode_get_child(iq->node, "query"); | |
| 177 | ||
| 178 | strftime(buf, sizeof(buf), "%Y%m%dT%T", &now); | |
| 179 | xmlnode_insert_data(xmlnode_new_child(query, "utc"), buf, -1); | |
| 180 | strftime(buf, sizeof(buf), "%Z", &now); | |
| 181 | xmlnode_insert_data(xmlnode_new_child(query, "tz"), buf, -1); | |
| 182 | strftime(buf, sizeof(buf), "%d %b %Y %T", &now); | |
| 183 | xmlnode_insert_data(xmlnode_new_child(query, "display"), buf, -1); | |
| 184 | ||
| 185 | jabber_iq_send(iq); | |
| 186 | } | |
| 187 | ||
| 188 | static void jabber_iq_handle_version(JabberStream *js, xmlnode *packet) | |
| 189 | { | |
| 190 | JabberIq *iq; | |
| 191 | const char *from, *id; | |
| 192 | xmlnode *query; | |
| 193 | char *os = NULL; | |
| 194 | ||
| 195 | if(!gaim_prefs_get_bool("/plugins/prpl/jabber/hide_os")) { | |
| 196 | struct utsname osinfo; | |
| 197 | ||
| 198 | uname(&osinfo); | |
| 199 | os = g_strdup_printf("%s %s %s", osinfo.sysname, osinfo.release, | |
| 200 | osinfo.machine); | |
| 201 | } | |
| 202 | ||
| 203 | from = xmlnode_get_attrib(packet, "from"); | |
| 204 | id = xmlnode_get_attrib(packet, "id"); | |
| 205 | ||
| 206 | iq = jabber_iq_new_query(js, JABBER_IQ_RESULT, "jabber:iq:version"); | |
| 207 | xmlnode_set_attrib(iq->node, "to", from); | |
| 208 | jabber_iq_set_id(iq, id); | |
| 209 | ||
| 210 | query = xmlnode_get_child(iq->node, "query"); | |
| 211 | ||
| 212 | xmlnode_insert_data(xmlnode_new_child(query, "name"), PACKAGE, -1); | |
| 213 | xmlnode_insert_data(xmlnode_new_child(query, "version"), VERSION, -1); | |
| 214 | if(os) { | |
| 215 | xmlnode_insert_data(xmlnode_new_child(query, "os"), os, -1); | |
| 216 | g_free(os); | |
| 217 | } | |
| 218 | ||
| 219 | jabber_iq_send(iq); | |
| 220 | } | |
| 221 | ||
| 7395 | 222 | #define SUPPORT_FEATURE(x) \ |
| 223 | feature = xmlnode_new_child(query, "feature"); \ | |
| 224 | xmlnode_set_attrib(feature, "var", x); | |
| 225 | ||
| 226 | ||
| 227 | void jabber_disco_info_parse(JabberStream *js, xmlnode *packet) { | |
| 228 | const char *from = xmlnode_get_attrib(packet, "from"); | |
| 229 | const char *type = xmlnode_get_attrib(packet, "type"); | |
| 230 | ||
| 231 | if(!from || !type) | |
| 232 | return; | |
| 233 | ||
| 234 | if(!strcmp(type, "get")) { | |
| 235 | xmlnode *query, *identity, *feature; | |
| 236 | JabberIq *iq = jabber_iq_new_query(js, JABBER_IQ_RESULT, | |
| 237 | "http://jabber.org/protocol/disco#info"); | |
| 238 | ||
| 239 | jabber_iq_set_id(iq, xmlnode_get_attrib(packet, "id")); | |
| 240 | ||
| 241 | xmlnode_set_attrib(iq->node, "to", from); | |
| 242 | query = xmlnode_get_child(iq->node, "query"); | |
| 243 | ||
| 244 | identity = xmlnode_new_child(query, "identity"); | |
| 245 | xmlnode_set_attrib(identity, "category", "client"); | |
| 246 | xmlnode_set_attrib(identity, "type", "pc"); /* XXX: bot, console, | |
| 247 | * handheld, pc, phone, | |
| 248 | * web */ | |
| 249 | ||
| 250 | SUPPORT_FEATURE("jabber:iq:last") | |
| 251 | SUPPORT_FEATURE("jabber:iq:oob") | |
| 252 | SUPPORT_FEATURE("jabber:iq:time") | |
| 253 | SUPPORT_FEATURE("jabber:iq:version") | |
| 254 | SUPPORT_FEATURE("jabber:x:conference") | |
| 255 | SUPPORT_FEATURE("http://jabber.org/protocol/bytestreams") | |
| 256 | SUPPORT_FEATURE("http://jabber.org/protocol/disco#info") | |
| 257 | SUPPORT_FEATURE("http://jabber.org/protocol/disco#items") | |
| 258 | SUPPORT_FEATURE("http://jabber.org/protocol/muc") | |
| 259 | SUPPORT_FEATURE("http://jabber.org/protocol/muc#user") | |
| 260 | SUPPORT_FEATURE("http://jabber.org/protocol/si") | |
| 261 | SUPPORT_FEATURE("http://jabber.org/protocol/si/profile/file-transfer") | |
| 262 | ||
| 263 | jabber_iq_send(iq); | |
| 264 | }else if(!strcmp(type, "result")) { | |
| 265 | xmlnode *query = xmlnode_get_child(packet, "query"); | |
| 266 | xmlnode *child; | |
| 267 | JabberID *jid; | |
| 268 | JabberBuddy *jb; | |
| 269 | JabberBuddyResource *jbr = NULL; | |
| 270 | ||
| 271 | if(!(jid = jabber_id_new(from))) | |
| 272 | return; | |
| 273 | ||
| 274 | if(jid->resource && (jb = jabber_buddy_find(js, from, TRUE))) | |
| 275 | jbr = jabber_buddy_find_resource(jb, jid->resource); | |
| 276 | ||
| 277 | if(!jbr) { | |
| 278 | jabber_id_free(jid); | |
| 279 | return; | |
| 280 | } | |
| 281 | ||
| 282 | for(child = query->child; child; child = child->next) { | |
| 283 | if(child->type != NODE_TYPE_TAG) | |
| 284 | continue; | |
| 285 | ||
| 286 | if(!strcmp(child->name, "feature")) { | |
| 287 | const char *var = xmlnode_get_attrib(child, "var"); | |
| 288 | if(!var) | |
| 289 | continue; | |
| 290 | ||
| 291 | if(!strcmp(var, "http://jabber.org/protocol/si")) | |
| 292 | jbr->capabilities |= JABBER_CAP_SI; | |
| 293 | else if(!strcmp(var, | |
| 294 | "http://jabber.org/protocol/si/profile/file-transfer")) | |
| 295 | jbr->capabilities |= JABBER_CAP_SI_FILE_XFER; | |
| 296 | else if(!strcmp(var, "http://jabber.org/protocol/bytestreams")) | |
| 297 | jbr->capabilities |= JABBER_CAP_BYTESTREAMS; | |
| 298 | } | |
| 299 | } | |
| 300 | jabber_id_free(jid); | |
| 301 | } | |
| 302 | } | |
| 303 | ||
| 304 | void jabber_disco_items_parse(JabberStream *js, xmlnode *packet) { | |
| 305 | const char *from = xmlnode_get_attrib(packet, "from"); | |
| 306 | const char *type = xmlnode_get_attrib(packet, "type"); | |
| 307 | ||
| 308 | if(!strcmp(type, "get")) { | |
| 309 | JabberIq *iq = jabber_iq_new_query(js, JABBER_IQ_RESULT, | |
| 310 | "http://jabber.org/protocol/disco#items"); | |
| 311 | ||
| 312 | jabber_iq_set_id(iq, xmlnode_get_attrib(packet, "id")); | |
| 313 | ||
| 314 | xmlnode_set_attrib(iq->node, "to", from); | |
| 315 | jabber_iq_send(iq); | |
| 316 | } | |
| 317 | } | |
| 318 | ||
| 7014 | 319 | void jabber_iq_parse(JabberStream *js, xmlnode *packet) |
| 320 | { | |
| 7395 | 321 | JabberCallbackData *jcd; |
| 7014 | 322 | xmlnode *query; |
| 323 | const char *xmlns; | |
| 7395 | 324 | const char *type, *id; |
| 7014 | 325 | |
| 326 | query = xmlnode_get_child(packet, "query"); | |
| 327 | ||
| 7395 | 328 | if(query) { |
| 7014 | 329 | |
| 7395 | 330 | xmlns = xmlnode_get_attrib(query, "xmlns"); |
| 7014 | 331 | |
| 7395 | 332 | if(!xmlns) |
| 333 | return; | |
| 7014 | 334 | |
| 7395 | 335 | if(!strcmp(xmlns, "jabber:iq:roster")) { |
| 336 | jabber_roster_parse(js, packet); | |
| 337 | return; | |
| 338 | } else if(!strcmp(xmlns, "jabber:iq:last")) { | |
| 339 | jabber_iq_handle_last(js, packet); | |
| 340 | return; | |
| 341 | } else if(!strcmp(xmlns, "jabber:iq:time")) { | |
| 342 | jabber_iq_handle_time(js, packet); | |
| 343 | return; | |
| 344 | } else if(!strcmp(xmlns, "jabber:iq:version")) { | |
| 345 | jabber_iq_handle_version(js, packet); | |
| 346 | return; | |
| 347 | } else if(!strcmp(xmlns, "jabber:iq:register")) { | |
| 348 | jabber_register_parse(js, packet); | |
| 349 | return; | |
| 350 | } else if(!strcmp(xmlns, "jabber:iq:oob")) { | |
| 351 | jabber_oob_parse(js, packet); | |
| 352 | return; | |
| 353 | } else if(!strcmp(xmlns, "http://jabber.org/protocol/disco#info")) { | |
| 354 | jabber_disco_info_parse(js, packet); | |
| 355 | return; | |
| 356 | } else if(!strcmp(xmlns, "http://jabber.org/protocol/disco#items")) { | |
| 357 | jabber_disco_items_parse(js, packet); | |
| 358 | return; | |
| 359 | } | |
| 360 | } else if(xmlnode_get_child(packet, "si")) { | |
| 361 | jabber_si_parse(js, packet); | |
| 362 | return; | |
| 363 | } | |
| 364 | ||
| 365 | /* If we got here, no pre-defined handlers got it, lets see if a special | |
| 366 | * callback got registered */ | |
| 367 | ||
| 368 | type = xmlnode_get_attrib(packet, "type"); | |
| 369 | id = xmlnode_get_attrib(packet, "id"); | |
| 370 | ||
| 371 | if(type && (!strcmp(type, "result") || !strcmp(type, "error")) && id | |
| 372 | && *id && (jcd = g_hash_table_lookup(js->callbacks, id))) { | |
| 373 | jcd->callback(js, packet, jcd->data); | |
| 374 | g_free(jcd); | |
| 7014 | 375 | } |
| 376 | } | |
| 377 |