Thu, 26 May 2005 04:13:06 +0000
[gaim-migrate @ 12731]
sick of having my tree be so far out of sync...here's all my jabber stuff
most notable is the "iChat" buddy icon support
| 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 | ||
|
10684
0325b164a7eb
[gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
10646
diff
changeset
|
23 | #include "cipher.h" |
| 7014 | 24 | #include "debug.h" |
| 25 | #include "notify.h" | |
| 26 | #include "request.h" | |
| 27 | #include "server.h" | |
| 9954 | 28 | #include "status.h" |
|
7095
17d2b54254f8
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7015
diff
changeset
|
29 | #include "util.h" |
| 7014 | 30 | |
| 31 | #include "buddy.h" | |
| 32 | #include "chat.h" | |
| 33 | #include "presence.h" | |
| 34 | #include "iq.h" | |
| 35 | #include "jutil.h" | |
| 36 | #include "xmlnode.h" | |
| 37 | ||
| 38 | ||
| 39 | static void chats_send_presence_foreach(gpointer key, gpointer val, | |
| 40 | gpointer user_data) | |
| 41 | { | |
| 42 | JabberChat *chat = val; | |
| 43 | xmlnode *presence = user_data; | |
| 8577 | 44 | char *chat_full_jid; |
| 45 | ||
| 46 | if(!chat->conv) | |
| 47 | return; | |
| 48 | ||
| 49 | chat_full_jid = g_strdup_printf("%s@%s/%s", chat->room, chat->server, | |
| 8401 | 50 | chat->handle); |
| 7014 | 51 | |
| 8401 | 52 | xmlnode_set_attrib(presence, "to", chat_full_jid); |
| 7014 | 53 | jabber_send(chat->js, presence); |
| 8401 | 54 | g_free(chat_full_jid); |
| 7014 | 55 | } |
| 56 | ||
| 9954 | 57 | void jabber_presence_fake_to_self(JabberStream *js, const GaimStatus *gstatus) { |
| 10286 | 58 | char *my_base_jid; |
| 59 | ||
| 60 | if(!js->user) | |
| 61 | return; | |
| 62 | ||
| 63 | my_base_jid = g_strdup_printf("%s@%s", js->user->node, js->user->domain); | |
| 8185 | 64 | if(gaim_find_buddy(js->gc->account, my_base_jid)) { |
| 65 | JabberBuddy *jb; | |
| 66 | JabberBuddyResource *jbr; | |
| 67 | if((jb = jabber_buddy_find(js, my_base_jid, TRUE))) { | |
| 9954 | 68 | JabberBuddyState state; |
| 69 | const char *msg; | |
| 70 | int priority; | |
| 71 | ||
| 72 | gaim_status_to_jabber(gstatus, &state, &msg, &priority); | |
| 73 | ||
| 10490 | 74 | if (state == JABBER_BUDDY_STATE_UNAVAILABLE || state == JABBER_BUDDY_STATE_UNKNOWN) { |
|
9744
c2b450de1fc0
[gaim-migrate @ 10609]
Daniel Atallah <datallah@pidgin.im>
parents:
9743
diff
changeset
|
75 | jabber_buddy_remove_resource(jb, js->user->resource); |
|
c2b450de1fc0
[gaim-migrate @ 10609]
Daniel Atallah <datallah@pidgin.im>
parents:
9743
diff
changeset
|
76 | } else { |
| 9954 | 77 | jabber_buddy_track_resource(jb, js->user->resource, priority, state, msg); |
|
9744
c2b450de1fc0
[gaim-migrate @ 10609]
Daniel Atallah <datallah@pidgin.im>
parents:
9743
diff
changeset
|
78 | } |
| 9954 | 79 | if((jbr = jabber_buddy_find_resource(jb, NULL))) { |
| 9990 | 80 | gaim_prpl_got_user_status(js->gc->account, my_base_jid, jabber_buddy_state_get_status_id(jbr->state), "priority", jbr->priority, jbr->status ? "message" : NULL, jbr->status, NULL); |
| 9954 | 81 | } else { |
| 9990 | 82 | gaim_prpl_got_user_status(js->gc->account, my_base_jid, "offline", msg ? "message" : NULL, msg, NULL); |
| 9954 | 83 | } |
| 8185 | 84 | } |
| 85 | } | |
| 86 | g_free(my_base_jid); | |
| 87 | } | |
| 88 | ||
| 7014 | 89 | |
| 10216 | 90 | void jabber_presence_send(GaimAccount *account, GaimStatus *status) |
| 7014 | 91 | { |
|
10382
32e07712e224
[gaim-migrate @ 11608]
Luke Schierer <lschiere@pidgin.im>
parents:
10286
diff
changeset
|
92 | GaimConnection *gc = NULL; |
|
32e07712e224
[gaim-migrate @ 11608]
Luke Schierer <lschiere@pidgin.im>
parents:
10286
diff
changeset
|
93 | JabberStream *js = NULL; |
|
10755
a336a5d3102d
[gaim-migrate @ 12358]
Mark Doliner <markdoliner@pidgin.im>
parents:
10738
diff
changeset
|
94 | gboolean connected; |
|
a336a5d3102d
[gaim-migrate @ 12358]
Mark Doliner <markdoliner@pidgin.im>
parents:
10738
diff
changeset
|
95 | int primitive; |
| 10189 | 96 | xmlnode *presence, *x, *photo; |
| 7014 | 97 | char *stripped = NULL; |
| 9954 | 98 | const char *msg; |
| 99 | JabberBuddyState state; | |
| 100 | int priority; | |
| 7014 | 101 | |
| 10486 | 102 | if(!gaim_status_is_active(status)) |
| 103 | return; | |
| 104 | ||
|
10755
a336a5d3102d
[gaim-migrate @ 12358]
Mark Doliner <markdoliner@pidgin.im>
parents:
10738
diff
changeset
|
105 | connected = gaim_account_is_connected(account); |
|
a336a5d3102d
[gaim-migrate @ 12358]
Mark Doliner <markdoliner@pidgin.im>
parents:
10738
diff
changeset
|
106 | primitive = gaim_status_type_get_primitive(gaim_status_get_type(status)); |
|
a336a5d3102d
[gaim-migrate @ 12358]
Mark Doliner <markdoliner@pidgin.im>
parents:
10738
diff
changeset
|
107 | |
|
a336a5d3102d
[gaim-migrate @ 12358]
Mark Doliner <markdoliner@pidgin.im>
parents:
10738
diff
changeset
|
108 | if(primitive != GAIM_STATUS_OFFLINE && !connected) { |
|
10738
63ca8277c234
[gaim-migrate @ 12340]
Mark Doliner <markdoliner@pidgin.im>
parents:
10722
diff
changeset
|
109 | gaim_account_connect(account); |
|
10755
a336a5d3102d
[gaim-migrate @ 12358]
Mark Doliner <markdoliner@pidgin.im>
parents:
10738
diff
changeset
|
110 | } else if(primitive == GAIM_STATUS_OFFLINE && connected) { |
|
a336a5d3102d
[gaim-migrate @ 12358]
Mark Doliner <markdoliner@pidgin.im>
parents:
10738
diff
changeset
|
111 | gaim_account_disconnect(account); |
|
a336a5d3102d
[gaim-migrate @ 12358]
Mark Doliner <markdoliner@pidgin.im>
parents:
10738
diff
changeset
|
112 | } |
|
a336a5d3102d
[gaim-migrate @ 12358]
Mark Doliner <markdoliner@pidgin.im>
parents:
10738
diff
changeset
|
113 | |
|
a336a5d3102d
[gaim-migrate @ 12358]
Mark Doliner <markdoliner@pidgin.im>
parents:
10738
diff
changeset
|
114 | if(!connected) |
|
a336a5d3102d
[gaim-migrate @ 12358]
Mark Doliner <markdoliner@pidgin.im>
parents:
10738
diff
changeset
|
115 | return; |
|
a336a5d3102d
[gaim-migrate @ 12358]
Mark Doliner <markdoliner@pidgin.im>
parents:
10738
diff
changeset
|
116 | |
|
a336a5d3102d
[gaim-migrate @ 12358]
Mark Doliner <markdoliner@pidgin.im>
parents:
10738
diff
changeset
|
117 | gc = gaim_account_get_connection(account); |
|
a336a5d3102d
[gaim-migrate @ 12358]
Mark Doliner <markdoliner@pidgin.im>
parents:
10738
diff
changeset
|
118 | js = gc->proto_data; |
|
a336a5d3102d
[gaim-migrate @ 12358]
Mark Doliner <markdoliner@pidgin.im>
parents:
10738
diff
changeset
|
119 | |
| 9954 | 120 | gaim_status_to_jabber(status, &state, &msg, &priority); |
| 121 | ||
| 122 | if(msg) | |
|
7095
17d2b54254f8
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7015
diff
changeset
|
123 | gaim_markup_html_to_xhtml(msg, NULL, &stripped); |
| 7014 | 124 | |
| 9954 | 125 | presence = jabber_presence_create(state, stripped, priority); |
| 126 | g_free(stripped); | |
| 7014 | 127 | |
| 128 | jabber_send(js, presence); | |
| 10189 | 129 | |
| 130 | if(js->avatar_hash) { | |
| 131 | x = xmlnode_new_child(presence, "x"); | |
| 132 | xmlnode_set_attrib(x, "xmlns", "vcard-temp:x:update"); | |
| 133 | photo = xmlnode_new_child(x, "photo"); | |
| 134 | xmlnode_insert_data(photo, js->avatar_hash, -1); | |
| 135 | } | |
| 136 | ||
| 7014 | 137 | g_hash_table_foreach(js->chats, chats_send_presence_foreach, presence); |
| 138 | xmlnode_free(presence); | |
| 8185 | 139 | |
| 9954 | 140 | jabber_presence_fake_to_self(js, status); |
| 141 | ||
| 7014 | 142 | } |
| 143 | ||
| 9954 | 144 | xmlnode *jabber_presence_create(JabberBuddyState state, const char *msg, int priority) |
| 7014 | 145 | { |
| 146 | xmlnode *show, *status, *presence; | |
| 9954 | 147 | const char *show_string = NULL; |
| 7014 | 148 | |
| 149 | ||
| 150 | presence = xmlnode_new("presence"); | |
| 151 | ||
| 152 | ||
| 9954 | 153 | if(state == JABBER_BUDDY_STATE_UNAVAILABLE) |
| 154 | xmlnode_set_attrib(presence, "type", "unavailable"); | |
| 155 | else if(state != JABBER_BUDDY_STATE_ONLINE && | |
| 156 | state != JABBER_BUDDY_STATE_UNKNOWN && | |
| 157 | state != JABBER_BUDDY_STATE_ERROR) | |
| 158 | show_string = jabber_buddy_state_get_status_id(state); | |
| 159 | ||
| 160 | if(show_string) { | |
| 161 | show = xmlnode_new_child(presence, "show"); | |
| 162 | xmlnode_insert_data(show, show_string, -1); | |
| 7014 | 163 | } |
| 164 | ||
| 9954 | 165 | if(msg) { |
| 7014 | 166 | status = xmlnode_new_child(presence, "status"); |
| 167 | xmlnode_insert_data(status, msg, -1); | |
| 168 | } | |
| 169 | ||
| 170 | return presence; | |
| 171 | } | |
| 172 | ||
| 173 | struct _jabber_add_permit { | |
| 174 | GaimConnection *gc; | |
| 175 | char *who; | |
| 176 | }; | |
| 177 | ||
| 178 | static void authorize_add_cb(struct _jabber_add_permit *jap) | |
| 179 | { | |
| 180 | if(g_list_find(gaim_connections_get_all(), jap->gc)) { | |
| 181 | jabber_presence_subscription_set(jap->gc->proto_data, jap->who, | |
| 182 | "subscribed"); | |
| 183 | ||
| 184 | if(!gaim_find_buddy(jap->gc->account, jap->who)) | |
|
7015
bea9111282b3
[gaim-migrate @ 7578]
Christian Hammond <chipx86@chipx86.com>
parents:
7014
diff
changeset
|
185 | gaim_account_notify_added(jap->gc->account, NULL, jap->who, NULL, NULL); |
| 7014 | 186 | } |
| 187 | ||
| 188 | g_free(jap->who); | |
| 189 | g_free(jap); | |
| 190 | } | |
| 191 | ||
| 192 | static void deny_add_cb(struct _jabber_add_permit *jap) | |
| 193 | { | |
| 194 | if(g_list_find(gaim_connections_get_all(), jap->gc)) { | |
| 195 | jabber_presence_subscription_set(jap->gc->proto_data, jap->who, | |
| 196 | "unsubscribed"); | |
| 197 | } | |
| 198 | ||
| 199 | g_free(jap->who); | |
| 200 | g_free(jap); | |
| 201 | } | |
| 202 | ||
| 10189 | 203 | static void jabber_vcard_parse_avatar(JabberStream *js, xmlnode *packet, gpointer blah) |
| 204 | { | |
| 10941 | 205 | JabberBuddy *jb = NULL; |
| 10189 | 206 | GaimBuddy *b = NULL; |
| 10941 | 207 | xmlnode *vcard, *photo, *binval; |
| 10189 | 208 | char *text, *data; |
| 209 | int size; | |
| 210 | const char *from = xmlnode_get_attrib(packet, "from"); | |
| 211 | ||
| 212 | if(!from) | |
| 213 | return; | |
| 214 | ||
| 10941 | 215 | jb = jabber_buddy_find(js, from, TRUE); |
| 216 | ||
| 217 | js->pending_avatar_requests = g_slist_remove(js->pending_avatar_requests, jb); | |
| 218 | ||
| 10189 | 219 | if((vcard = xmlnode_get_child(packet, "vCard")) || |
| 220 | (vcard = xmlnode_get_child_with_namespace(packet, "query", "vcard-temp"))) { | |
| 10941 | 221 | if((photo = xmlnode_get_child(vcard, "PHOTO")) && |
| 222 | (binval = xmlnode_get_child(photo, "BINVAL")) && | |
| 223 | (text = xmlnode_get_data(binval))) { | |
| 224 | gaim_base64_decode(text, &data, &size); | |
| 10189 | 225 | |
| 10941 | 226 | gaim_buddy_icons_set_for_user(js->gc->account, from, data, size); |
| 227 | if((b = gaim_find_buddy(js->gc->account, from))) { | |
| 228 | unsigned char hashval[20]; | |
| 229 | char hash[41], *p; | |
| 230 | int i; | |
| 10189 | 231 | |
| 10941 | 232 | gaim_cipher_digest_region("sha1", (guint8 *)data, size, |
| 233 | sizeof(hashval), hashval, NULL); | |
| 234 | p = hash; | |
| 235 | for(i=0; i<20; i++, p+=2) | |
| 236 | snprintf(p, 3, "%02x", hashval[i]); | |
| 237 | gaim_blist_node_set_string((GaimBlistNode*)b, "avatar_hash", hash); | |
| 10189 | 238 | } |
| 10941 | 239 | g_free(text); |
| 10189 | 240 | } |
| 241 | } | |
| 242 | } | |
| 243 | ||
| 7014 | 244 | void jabber_presence_parse(JabberStream *js, xmlnode *packet) |
| 245 | { | |
| 246 | const char *from = xmlnode_get_attrib(packet, "from"); | |
| 247 | const char *type = xmlnode_get_attrib(packet, "type"); | |
| 7944 | 248 | const char *real_jid = NULL; |
| 9152 | 249 | const char *affiliation = NULL; |
| 250 | const char *role = NULL; | |
| 7014 | 251 | char *status = NULL; |
| 252 | int priority = 0; | |
| 253 | JabberID *jid; | |
| 254 | JabberChat *chat; | |
| 255 | JabberBuddy *jb; | |
| 9954 | 256 | JabberBuddyResource *jbr = NULL, *found_jbr = NULL; |
|
9554
1609ba3612c3
[gaim-migrate @ 10387]
Stu Tomlinson <nosnilmot@pidgin.im>
parents:
9320
diff
changeset
|
257 | GaimConvChatBuddyFlags flags = GAIM_CBFLAGS_NONE; |
|
9846
61f7349c153a
[gaim-migrate @ 10724]
Nathan Fredrickson <nathan@silverorange.com>
parents:
9745
diff
changeset
|
258 | gboolean delayed = FALSE; |
| 10189 | 259 | GaimBuddy *b = NULL; |
| 7014 | 260 | char *buddy_name; |
| 9954 | 261 | JabberBuddyState state = JABBER_BUDDY_STATE_UNKNOWN; |
| 7014 | 262 | xmlnode *y; |
| 263 | gboolean muc = FALSE; | |
| 10189 | 264 | char *avatar_hash = NULL; |
| 7014 | 265 | |
| 266 | ||
| 8043 | 267 | if(!(jb = jabber_buddy_find(js, from, TRUE))) |
| 268 | return; | |
| 269 | ||
| 270 | if(!(jid = jabber_id_new(from))) | |
| 7280 | 271 | return; |
| 272 | ||
| 7014 | 273 | if(jb->error_msg) { |
| 274 | g_free(jb->error_msg); | |
| 275 | jb->error_msg = NULL; | |
| 276 | } | |
| 277 | ||
| 7813 | 278 | if(type && !strcmp(type, "error")) { |
| 8401 | 279 | char *msg = jabber_parse_error(js, packet); |
| 7644 | 280 | |
| 9954 | 281 | state = JABBER_BUDDY_STATE_ERROR; |
| 8401 | 282 | jb->error_msg = msg ? msg : g_strdup(_("Unknown Error in presence")); |
| 7813 | 283 | } else if(type && !strcmp(type, "subscribe")) { |
| 7014 | 284 | struct _jabber_add_permit *jap = g_new0(struct _jabber_add_permit, 1); |
| 285 | char *msg = g_strdup_printf(_("The user %s wants to add you to their buddy list."), from); | |
| 286 | jap->gc = js->gc; | |
| 287 | jap->who = g_strdup(from); | |
| 288 | ||
| 10189 | 289 | gaim_request_action(js->gc, NULL, msg, NULL, GAIM_DEFAULT_ACTION_NONE, |
|
10116
054b064145a1
[gaim-migrate @ 11153]
Stu Tomlinson <nosnilmot@pidgin.im>
parents:
9990
diff
changeset
|
290 | jap, 2, |
| 7014 | 291 | _("Authorize"), G_CALLBACK(authorize_add_cb), |
| 292 | _("Deny"), G_CALLBACK(deny_add_cb)); | |
| 293 | g_free(msg); | |
| 8043 | 294 | jabber_id_free(jid); |
| 7145 | 295 | return; |
| 7813 | 296 | } else if(type && !strcmp(type, "subscribed")) { |
| 7014 | 297 | /* we've been allowed to see their presence, but we don't care */ |
| 8043 | 298 | jabber_id_free(jid); |
| 7014 | 299 | return; |
| 300 | } else { | |
| 301 | if((y = xmlnode_get_child(packet, "show"))) { | |
| 302 | char *show = xmlnode_get_data(y); | |
| 9954 | 303 | state = jabber_buddy_status_id_get_state(show); |
| 7014 | 304 | g_free(show); |
| 305 | } else { | |
| 9954 | 306 | state = JABBER_BUDDY_STATE_ONLINE; |
| 7014 | 307 | } |
| 308 | } | |
| 309 | ||
| 7310 | 310 | |
| 7014 | 311 | for(y = packet->child; y; y = y->next) { |
| 8135 | 312 | if(y->type != XMLNODE_TYPE_TAG) |
| 7014 | 313 | continue; |
| 314 | ||
| 315 | if(!strcmp(y->name, "status")) { | |
| 7615 | 316 | g_free(status); |
| 7014 | 317 | status = xmlnode_get_data(y); |
| 318 | } else if(!strcmp(y->name, "priority")) { | |
| 319 | char *p = xmlnode_get_data(y); | |
| 320 | if(p) { | |
| 321 | priority = atoi(p); | |
| 322 | g_free(p); | |
| 323 | } | |
| 324 | } else if(!strcmp(y->name, "x")) { | |
| 325 | const char *xmlns = xmlnode_get_attrib(y, "xmlns"); | |
|
9846
61f7349c153a
[gaim-migrate @ 10724]
Nathan Fredrickson <nathan@silverorange.com>
parents:
9745
diff
changeset
|
326 | if(xmlns && !strcmp(xmlns, "jabber:x:delay")) { |
| 9847 | 327 | /* XXX: compare the time. jabber:x:delay can happen on presence packets that aren't really and truly delayed */ |
|
9846
61f7349c153a
[gaim-migrate @ 10724]
Nathan Fredrickson <nathan@silverorange.com>
parents:
9745
diff
changeset
|
328 | delayed = TRUE; |
|
61f7349c153a
[gaim-migrate @ 10724]
Nathan Fredrickson <nathan@silverorange.com>
parents:
9745
diff
changeset
|
329 | } else if(xmlns && !strcmp(xmlns, "http://jabber.org/protocol/muc#user")) { |
| 7629 | 330 | xmlnode *z; |
| 331 | ||
| 7014 | 332 | muc = TRUE; |
| 7629 | 333 | if((z = xmlnode_get_child(y, "status"))) { |
| 334 | const char *code = xmlnode_get_attrib(z, "code"); | |
| 335 | if(code && !strcmp(code, "201")) { | |
| 7895 | 336 | chat = jabber_chat_find(js, jid->node, jid->domain); |
| 8396 | 337 | chat->config_dialog_type = GAIM_REQUEST_ACTION; |
| 338 | chat->config_dialog_handle = | |
| 339 | gaim_request_action(js->gc, _("Create New Room"), | |
| 7895 | 340 | _("Create New Room"), |
| 341 | _("You are creating a new room. Would you like to " | |
| 342 | "configure it, or accept the default settings?"), | |
| 343 | 1, chat, 2, _("Configure Room"), | |
| 7923 | 344 | G_CALLBACK(jabber_chat_request_room_configure), |
| 7895 | 345 | _("Accept Defaults"), |
| 346 | G_CALLBACK(jabber_chat_create_instant_room)); | |
| 7629 | 347 | } |
| 348 | } | |
| 7944 | 349 | if((z = xmlnode_get_child(y, "item"))) { |
| 350 | real_jid = xmlnode_get_attrib(z, "jid"); | |
| 9152 | 351 | affiliation = xmlnode_get_attrib(z, "affiliation"); |
| 352 | role = xmlnode_get_attrib(z, "role"); | |
| 9931 | 353 | if(affiliation != NULL && !strcmp(affiliation, "owner")) |
| 354 | flags |= GAIM_CBFLAGS_FOUNDER; | |
|
9743
839b2bce3853
[gaim-migrate @ 10608]
Stu Tomlinson <nosnilmot@pidgin.im>
parents:
9559
diff
changeset
|
355 | if (role != NULL) { |
|
839b2bce3853
[gaim-migrate @ 10608]
Stu Tomlinson <nosnilmot@pidgin.im>
parents:
9559
diff
changeset
|
356 | if (!strcmp(role, "moderator")) |
| 9931 | 357 | flags |= GAIM_CBFLAGS_OP; |
|
9743
839b2bce3853
[gaim-migrate @ 10608]
Stu Tomlinson <nosnilmot@pidgin.im>
parents:
9559
diff
changeset
|
358 | else if (!strcmp(role, "participant")) |
| 9931 | 359 | flags |= GAIM_CBFLAGS_VOICE; |
|
9743
839b2bce3853
[gaim-migrate @ 10608]
Stu Tomlinson <nosnilmot@pidgin.im>
parents:
9559
diff
changeset
|
360 | } |
| 7944 | 361 | } |
| 10189 | 362 | } else if(xmlns && !strcmp(xmlns, "vcard-temp:x:update")) { |
| 363 | xmlnode *photo = xmlnode_get_child(y, "photo"); | |
| 364 | if(photo) { | |
| 365 | if(avatar_hash) | |
| 366 | g_free(avatar_hash); | |
| 367 | avatar_hash = xmlnode_get_data(photo); | |
| 368 | } | |
| 7014 | 369 | } |
| 370 | } | |
| 371 | } | |
| 372 | ||
| 373 | ||
| 7322 | 374 | if(jid->node && (chat = jabber_chat_find(js, jid->node, jid->domain))) { |
| 8462 | 375 | static int i = 1; |
| 7014 | 376 | char *room_jid = g_strdup_printf("%s@%s", jid->node, jid->domain); |
| 377 | ||
| 9954 | 378 | if(state == JABBER_BUDDY_STATE_ERROR) { |
| 8401 | 379 | char *title, *msg = jabber_parse_error(js, packet); |
| 7014 | 380 | |
| 7321 | 381 | if(chat->conv) { |
| 8401 | 382 | title = g_strdup_printf(_("Error in chat %s"), from); |
| 7321 | 383 | serv_got_chat_left(js->gc, chat->id); |
| 384 | } else { | |
| 8401 | 385 | title = g_strdup_printf(_("Error joining chat %s"), from); |
| 7321 | 386 | } |
| 8401 | 387 | gaim_notify_error(js->gc, title, title, msg); |
| 388 | g_free(title); | |
| 389 | g_free(msg); | |
| 7014 | 390 | |
| 391 | jabber_chat_destroy(chat); | |
| 7310 | 392 | jabber_id_free(jid); |
| 7615 | 393 | g_free(status); |
| 8182 | 394 | g_free(room_jid); |
| 10189 | 395 | if(avatar_hash) |
| 396 | g_free(avatar_hash); | |
| 7014 | 397 | return; |
| 398 | } | |
| 399 | ||
| 400 | ||
| 7813 | 401 | if(type && !strcmp(type, "unavailable")) { |
| 7972 | 402 | gboolean nick_change = FALSE; |
| 7973 | 403 | |
| 9152 | 404 | /* If we haven't joined the chat yet, we don't care that someone |
| 405 | * left, or it was us leaving after we closed the chat */ | |
| 8182 | 406 | if(!chat->conv) { |
| 10558 | 407 | if(jid->resource && chat->handle && !strcmp(jid->resource, chat->handle)) |
| 9152 | 408 | jabber_chat_destroy(chat); |
| 8182 | 409 | jabber_id_free(jid); |
| 410 | g_free(status); | |
| 411 | g_free(room_jid); | |
| 10189 | 412 | if(avatar_hash) |
| 413 | g_free(avatar_hash); | |
| 8182 | 414 | return; |
| 415 | } | |
| 416 | ||
| 7973 | 417 | jabber_buddy_remove_resource(jb, jid->resource); |
| 7972 | 418 | if(chat->muc) { |
| 419 | xmlnode *x; | |
| 8135 | 420 | for(x = xmlnode_get_child(packet, "x"); x; x = xmlnode_get_next_twin(x)) { |
| 7972 | 421 | const char *xmlns, *nick, *code; |
| 422 | xmlnode *stat, *item; | |
| 423 | if(!(xmlns = xmlnode_get_attrib(x, "xmlns")) || | |
| 424 | strcmp(xmlns, "http://jabber.org/protocol/muc#user")) | |
| 425 | continue; | |
| 426 | if(!(stat = xmlnode_get_child(x, "status"))) | |
| 427 | continue; | |
| 9152 | 428 | if(!(code = xmlnode_get_attrib(stat, "code"))) |
| 7972 | 429 | continue; |
| 9152 | 430 | if(!strcmp(code, "301")) { |
| 431 | /* XXX: we got banned */ | |
| 432 | } else if(!strcmp(code, "303")) { | |
| 433 | if(!(item = xmlnode_get_child(x, "item"))) | |
| 434 | continue; | |
| 435 | if(!(nick = xmlnode_get_attrib(item, "nick"))) | |
| 436 | continue; | |
| 437 | nick_change = TRUE; | |
| 438 | if(!strcmp(jid->resource, chat->handle)) { | |
| 439 | g_free(chat->handle); | |
| 440 | chat->handle = g_strdup(nick); | |
| 441 | } | |
| 442 | gaim_conv_chat_rename_user(GAIM_CONV_CHAT(chat->conv), jid->resource, nick); | |
| 443 | jabber_chat_remove_handle(chat, jid->resource); | |
| 444 | break; | |
| 445 | } else if(!strcmp(code, "307")) { | |
| 446 | /* XXX: we got kicked */ | |
| 447 | } else if(!strcmp(code, "321")) { | |
| 448 | /* XXX: removed due to an affiliation change */ | |
| 449 | } else if(!strcmp(code, "322")) { | |
| 450 | /* XXX: removed because room is now members-only */ | |
| 451 | } else if(!strcmp(code, "332")) { | |
| 452 | /* XXX: removed due to system shutdown */ | |
| 8401 | 453 | } |
| 7972 | 454 | } |
| 455 | } | |
| 456 | if(!nick_change) { | |
| 9152 | 457 | if(!g_utf8_collate(jid->resource, chat->handle)) { |
| 7972 | 458 | serv_got_chat_left(js->gc, chat->id); |
| 459 | jabber_chat_destroy(chat); | |
| 460 | } else { | |
| 461 | gaim_conv_chat_remove_user(GAIM_CONV_CHAT(chat->conv), jid->resource, | |
| 7974 | 462 | status); |
| 9152 | 463 | jabber_chat_remove_handle(chat, jid->resource); |
| 7972 | 464 | } |
| 7014 | 465 | } |
| 466 | } else { | |
| 8182 | 467 | if(!chat->conv) { |
| 468 | chat->id = i++; | |
| 469 | chat->muc = muc; | |
| 470 | chat->conv = serv_got_joined_chat(js->gc, chat->id, room_jid); | |
| 10722 | 471 | gaim_conv_chat_set_nick(GAIM_CONV_CHAT(chat->conv), chat->handle); |
| 10486 | 472 | |
| 10941 | 473 | jabber_chat_disco_traffic(chat); |
| 8182 | 474 | } |
| 475 | ||
| 7973 | 476 | jabber_buddy_track_resource(jb, jid->resource, priority, state, |
| 477 | status); | |
| 478 | ||
| 9152 | 479 | jabber_chat_track_handle(chat, jid->resource, real_jid, affiliation, role); |
| 480 | ||
| 7014 | 481 | if(!jabber_chat_find_buddy(chat->conv, jid->resource)) |
|
7118
280b3b85a28a
[gaim-migrate @ 7685]
Christian Hammond <chipx86@chipx86.com>
parents:
7095
diff
changeset
|
482 | gaim_conv_chat_add_user(GAIM_CONV_CHAT(chat->conv), jid->resource, |
|
9846
61f7349c153a
[gaim-migrate @ 10724]
Nathan Fredrickson <nathan@silverorange.com>
parents:
9745
diff
changeset
|
483 | real_jid, flags, !delayed); |
|
9554
1609ba3612c3
[gaim-migrate @ 10387]
Stu Tomlinson <nosnilmot@pidgin.im>
parents:
9320
diff
changeset
|
484 | else |
|
1609ba3612c3
[gaim-migrate @ 10387]
Stu Tomlinson <nosnilmot@pidgin.im>
parents:
9320
diff
changeset
|
485 | gaim_conv_chat_user_set_flags(GAIM_CONV_CHAT(chat->conv), jid->resource, |
|
1609ba3612c3
[gaim-migrate @ 10387]
Stu Tomlinson <nosnilmot@pidgin.im>
parents:
9320
diff
changeset
|
486 | flags); |
| 7014 | 487 | } |
| 488 | g_free(room_jid); | |
| 489 | } else { | |
| 10486 | 490 | if(state != JABBER_BUDDY_STATE_ERROR && !(jb->subscription & JABBER_SUB_TO || jb->subscription & JABBER_SUB_PENDING)) { |
| 7014 | 491 | gaim_debug(GAIM_DEBUG_INFO, "jabber", |
| 492 | "got unexpected presence from %s, ignoring\n", from); | |
| 493 | jabber_id_free(jid); | |
| 7615 | 494 | g_free(status); |
| 10189 | 495 | if(avatar_hash) |
| 496 | g_free(avatar_hash); | |
| 7014 | 497 | return; |
| 498 | } | |
| 499 | ||
| 7322 | 500 | buddy_name = g_strdup_printf("%s%s%s", jid->node ? jid->node : "", |
| 501 | jid->node ? "@" : "", jid->domain); | |
| 7014 | 502 | if((b = gaim_find_buddy(js->gc->account, buddy_name)) == NULL) { |
| 503 | jabber_id_free(jid); | |
| 10189 | 504 | if(avatar_hash) |
| 505 | g_free(avatar_hash); | |
| 7014 | 506 | g_free(buddy_name); |
| 7615 | 507 | g_free(status); |
| 7014 | 508 | return; |
| 509 | } | |
| 510 | ||
| 10189 | 511 | if(avatar_hash) { |
| 512 | const char *avatar_hash2 = gaim_blist_node_get_string((GaimBlistNode*)b, "avatar_hash"); | |
| 513 | if(!avatar_hash2 || strcmp(avatar_hash, avatar_hash2)) { | |
| 514 | JabberIq *iq; | |
| 515 | xmlnode *vcard; | |
| 516 | ||
| 10941 | 517 | /* XXX this is a crappy way of trying to prevent |
| 518 | * someone from spamming us with presence packets | |
| 519 | * and causing us to DoS ourselves...what we really | |
| 520 | * need is a queue system that can throttle itself, | |
| 521 | * but i'm too tired to write that right now */ | |
| 522 | if(!g_slist_find(js->pending_avatar_requests, jb)) { | |
| 523 | ||
| 524 | js->pending_avatar_requests = g_slist_prepend(js->pending_avatar_requests, jb); | |
| 10189 | 525 | |
| 10941 | 526 | iq = jabber_iq_new(js, JABBER_IQ_GET); |
| 527 | xmlnode_set_attrib(iq->node, "to", buddy_name); | |
| 528 | vcard = xmlnode_new_child(iq->node, "vCard"); | |
| 529 | xmlnode_set_attrib(vcard, "xmlns", "vcard-temp"); | |
| 530 | ||
| 531 | jabber_iq_set_callback(iq, jabber_vcard_parse_avatar, NULL); | |
| 532 | jabber_iq_send(iq); | |
| 533 | } | |
| 10189 | 534 | } |
| 535 | } | |
| 536 | ||
| 9954 | 537 | if(state == JABBER_BUDDY_STATE_ERROR || |
| 7813 | 538 | (type && (!strcmp(type, "unavailable") || |
| 539 | !strcmp(type, "unsubscribed")))) { | |
| 8043 | 540 | GaimConversation *conv; |
| 541 | ||
| 7014 | 542 | jabber_buddy_remove_resource(jb, jid->resource); |
| 8043 | 543 | if((conv = jabber_find_unnormalized_conv(from, js->gc->account))) |
| 544 | gaim_conversation_set_name(conv, buddy_name); | |
| 545 | ||
| 7395 | 546 | } else { |
| 9954 | 547 | jbr = jabber_buddy_track_resource(jb, jid->resource, priority, |
| 548 | state, status); | |
| 7395 | 549 | } |
| 7014 | 550 | |
| 9954 | 551 | if((found_jbr = jabber_buddy_find_resource(jb, NULL))) { |
| 552 | if(!jbr || jbr == found_jbr) { | |
| 9990 | 553 | gaim_prpl_got_user_status(js->gc->account, buddy_name, jabber_buddy_state_get_status_id(state), "priority", found_jbr->priority, found_jbr->status ? "message" : NULL, found_jbr->status, NULL); |
| 9954 | 554 | } |
| 555 | } else { | |
| 9990 | 556 | gaim_prpl_got_user_status(js->gc->account, buddy_name, "offline", status ? "message" : NULL, status, NULL); |
| 9954 | 557 | } |
| 7014 | 558 | g_free(buddy_name); |
| 559 | } | |
| 560 | g_free(status); | |
| 561 | jabber_id_free(jid); | |
| 10189 | 562 | if(avatar_hash) |
| 563 | g_free(avatar_hash); | |
| 7014 | 564 | } |
| 565 | ||
| 566 | void jabber_presence_subscription_set(JabberStream *js, const char *who, const char *type) | |
| 567 | { | |
| 568 | xmlnode *presence = xmlnode_new("presence"); | |
| 569 | ||
| 570 | xmlnode_set_attrib(presence, "to", who); | |
| 571 | xmlnode_set_attrib(presence, "type", type); | |
| 572 | ||
| 573 | jabber_send(js, presence); | |
| 574 | xmlnode_free(presence); | |
| 575 | } | |
| 9954 | 576 | |
| 577 | void gaim_status_to_jabber(const GaimStatus *status, JabberBuddyState *state, const char **msg, int *priority) | |
| 578 | { | |
| 10216 | 579 | const char *status_id = NULL; |
| 580 | ||
| 581 | *state = JABBER_BUDDY_STATE_UNKNOWN; | |
| 582 | *msg = NULL; | |
| 583 | *priority = 0; | |
| 9954 | 584 | |
| 585 | if(!status) { | |
| 10216 | 586 | *state = JABBER_BUDDY_STATE_UNAVAILABLE; |
| 587 | } else { | |
| 588 | if(state) { | |
| 589 | status_id = gaim_status_get_id(status); | |
| 590 | *state = jabber_buddy_status_id_get_state(status_id); | |
| 591 | } | |
| 592 | ||
| 593 | if(msg) | |
| 594 | *msg = gaim_status_get_attr_string(status, "message"); | |
| 595 | ||
| 596 | if(priority) | |
| 597 | *priority = gaim_status_get_attr_int(status, "priority"); | |
| 9954 | 598 | } |
| 599 | ||
| 600 | } |