Fri, 30 Jan 2004 21:56:31 +0000
[gaim-migrate @ 8906]
take 1 of UberUglyHack
| 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 | ||
| 23 | #include "debug.h" | |
| 24 | #include "notify.h" | |
| 25 | #include "request.h" | |
| 26 | #include "server.h" | |
|
7095
17d2b54254f8
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7015
diff
changeset
|
27 | #include "util.h" |
| 7014 | 28 | |
| 29 | #include "buddy.h" | |
| 30 | #include "chat.h" | |
| 31 | #include "presence.h" | |
| 32 | #include "iq.h" | |
| 33 | #include "jutil.h" | |
| 34 | #include "xmlnode.h" | |
| 35 | ||
| 36 | ||
| 37 | static void chats_send_presence_foreach(gpointer key, gpointer val, | |
| 38 | gpointer user_data) | |
| 39 | { | |
| 40 | JabberChat *chat = val; | |
| 41 | xmlnode *presence = user_data; | |
| 42 | char *chat_jid = key; | |
| 43 | ||
| 44 | xmlnode_set_attrib(presence, "to", chat_jid); | |
| 45 | jabber_send(chat->js, presence); | |
| 46 | } | |
| 47 | ||
| 8185 | 48 | static int show_to_state(const char *show) { |
| 49 | if(!show) | |
| 50 | return 0; | |
| 51 | else if(!strcmp(show, "away") || !strcmp(show, _("Away"))) | |
| 52 | return JABBER_STATE_AWAY; | |
| 53 | else if(!strcmp(show, "chat") || !strcmp(show, _("Chatty"))) | |
| 54 | return JABBER_STATE_CHAT; | |
| 55 | else if(!strcmp(show, "xa") || !strcmp(show, _("Extended Away"))) | |
| 56 | return JABBER_STATE_XA; | |
| 57 | else if(!strcmp(show, "dnd") || !strcmp(show, _("Do Not Disturb"))) | |
| 58 | return JABBER_STATE_DND; | |
| 59 | return 0; | |
| 60 | } | |
| 61 | ||
| 62 | void jabber_presence_fake_to_self(JabberStream *js, const char *show, const char *status) { | |
| 63 | char *my_base_jid = g_strdup_printf("%s@%s", js->user->node, js->user->domain); | |
| 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))) { | |
| 68 | jabber_buddy_track_resource(jb, js->user->resource, 0, | |
| 69 | show_to_state(show), (status && *status) ? status : NULL); | |
| 70 | if((jbr = jabber_buddy_find_resource(jb, NULL))) | |
| 71 | serv_got_update(js->gc, my_base_jid, 1, 0, 0, 0, jbr->state); | |
| 72 | } | |
| 73 | } | |
| 74 | g_free(my_base_jid); | |
| 75 | } | |
| 76 | ||
| 7014 | 77 | |
| 78 | void jabber_presence_send(GaimConnection *gc, const char *state, | |
| 79 | const char *msg) | |
| 80 | { | |
| 81 | JabberStream *js = gc->proto_data; | |
| 82 | xmlnode *presence; | |
| 83 | char *stripped = NULL; | |
| 84 | ||
| 85 | if(msg) { | |
|
7095
17d2b54254f8
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7015
diff
changeset
|
86 | gaim_markup_html_to_xhtml(msg, NULL, &stripped); |
| 7249 | 87 | } else if(!state || strcmp(state, GAIM_AWAY_CUSTOM)) { |
| 7247 | 88 | /* i can't wait until someone re-writes the status/away stuff */ |
| 7014 | 89 | stripped = g_strdup(""); |
| 90 | } | |
| 91 | ||
| 7248 | 92 | if(gc->away) |
| 93 | g_free(gc->away); | |
| 7014 | 94 | gc->away = stripped; |
| 95 | ||
| 8185 | 96 | presence = jabber_presence_create(state, stripped); |
| 7014 | 97 | jabber_send(js, presence); |
| 98 | g_hash_table_foreach(js->chats, chats_send_presence_foreach, presence); | |
| 99 | xmlnode_free(presence); | |
| 8185 | 100 | |
| 101 | jabber_presence_fake_to_self(js, state, stripped); | |
| 7014 | 102 | } |
| 103 | ||
| 104 | xmlnode *jabber_presence_create(const char *state, const char *msg) | |
| 105 | { | |
| 106 | xmlnode *show, *status, *presence; | |
| 107 | ||
| 108 | ||
| 109 | presence = xmlnode_new("presence"); | |
| 110 | ||
| 111 | if(state) { | |
| 112 | const char *show_string = NULL; | |
| 113 | if(!strcmp(state, _("Chatty"))) | |
| 114 | show_string = "chat"; | |
| 115 | else if(!strcmp(state, _("Away")) || | |
| 116 | (msg && !strcmp(state, GAIM_AWAY_CUSTOM))) | |
| 117 | show_string = "away"; | |
| 118 | else if(!strcmp(state, _("Extended Away"))) | |
| 119 | show_string = "xa"; | |
| 120 | else if(!strcmp(state, _("Do Not Disturb"))) | |
| 121 | show_string = "dnd"; | |
| 122 | else if(!strcmp(state, _("Invisible"))) { | |
| 123 | xmlnode_set_attrib(presence, "type", "invisible"); | |
| 124 | } | |
| 125 | ||
| 126 | if(show_string) { | |
| 127 | show = xmlnode_new_child(presence, "show"); | |
| 128 | xmlnode_insert_data(show, show_string, -1); | |
| 129 | } | |
| 130 | } | |
| 131 | ||
| 132 | if(msg && *msg) { | |
| 133 | status = xmlnode_new_child(presence, "status"); | |
| 134 | xmlnode_insert_data(status, msg, -1); | |
| 135 | } | |
| 136 | ||
| 137 | return presence; | |
| 138 | } | |
| 139 | ||
| 140 | struct _jabber_add_permit { | |
| 141 | GaimConnection *gc; | |
| 142 | char *who; | |
| 143 | }; | |
| 144 | ||
| 145 | static void authorize_add_cb(struct _jabber_add_permit *jap) | |
| 146 | { | |
| 147 | if(g_list_find(gaim_connections_get_all(), jap->gc)) { | |
| 148 | jabber_presence_subscription_set(jap->gc->proto_data, jap->who, | |
| 149 | "subscribed"); | |
| 150 | ||
| 151 | if(!gaim_find_buddy(jap->gc->account, jap->who)) | |
|
7015
bea9111282b3
[gaim-migrate @ 7578]
Christian Hammond <chipx86@chipx86.com>
parents:
7014
diff
changeset
|
152 | gaim_account_notify_added(jap->gc->account, NULL, jap->who, NULL, NULL); |
| 7014 | 153 | } |
| 154 | ||
| 155 | g_free(jap->who); | |
| 156 | g_free(jap); | |
| 157 | } | |
| 158 | ||
| 159 | static void deny_add_cb(struct _jabber_add_permit *jap) | |
| 160 | { | |
| 161 | if(g_list_find(gaim_connections_get_all(), jap->gc)) { | |
| 162 | jabber_presence_subscription_set(jap->gc->proto_data, jap->who, | |
| 163 | "unsubscribed"); | |
| 164 | } | |
| 165 | ||
| 166 | g_free(jap->who); | |
| 167 | g_free(jap); | |
| 168 | } | |
| 169 | ||
| 170 | void jabber_presence_parse(JabberStream *js, xmlnode *packet) | |
| 171 | { | |
| 172 | const char *from = xmlnode_get_attrib(packet, "from"); | |
| 173 | const char *type = xmlnode_get_attrib(packet, "type"); | |
| 7944 | 174 | const char *real_jid = NULL; |
| 7014 | 175 | char *status = NULL; |
| 176 | int priority = 0; | |
| 177 | JabberID *jid; | |
| 178 | JabberChat *chat; | |
| 179 | JabberBuddy *jb; | |
| 7835 | 180 | JabberBuddyResource *jbr = NULL; |
| 7014 | 181 | GaimBuddy *b; |
| 182 | char *buddy_name; | |
| 183 | int state = 0; | |
| 184 | xmlnode *y; | |
| 185 | gboolean muc = FALSE; | |
| 186 | ||
| 187 | ||
| 8043 | 188 | if(!(jb = jabber_buddy_find(js, from, TRUE))) |
| 189 | return; | |
| 190 | ||
| 191 | if(!(jid = jabber_id_new(from))) | |
| 7280 | 192 | return; |
| 193 | ||
| 7014 | 194 | if(jb->error_msg) { |
| 195 | g_free(jb->error_msg); | |
| 196 | jb->error_msg = NULL; | |
| 197 | } | |
| 198 | ||
| 7813 | 199 | if(type && !strcmp(type, "error")) { |
| 7644 | 200 | const char *code = NULL; |
| 201 | char *err_txt = NULL; | |
| 202 | ||
| 7014 | 203 | state = JABBER_STATE_ERROR; |
| 204 | if((y = xmlnode_get_child(packet, "error")) != NULL) { | |
| 7184 | 205 | /* XXX: need to handle new XMPP-style errors */ |
| 7644 | 206 | code = xmlnode_get_attrib(y, "code"); |
| 207 | err_txt = xmlnode_get_data(y); | |
| 7014 | 208 | } |
| 7644 | 209 | jb->error_msg = g_strdup_printf("%s%s%s", code ? code : "", |
| 210 | code ? ": " : "", err_txt ? err_txt : | |
| 211 | _("Unknown Error in presence")); | |
| 212 | if(err_txt) | |
| 213 | g_free(err_txt); | |
| 7813 | 214 | } else if(type && !strcmp(type, "subscribe")) { |
| 7014 | 215 | struct _jabber_add_permit *jap = g_new0(struct _jabber_add_permit, 1); |
| 216 | char *msg = g_strdup_printf(_("The user %s wants to add you to their buddy list."), from); | |
| 217 | jap->gc = js->gc; | |
| 218 | jap->who = g_strdup(from); | |
| 219 | ||
| 220 | gaim_request_action(js->gc, NULL, msg, NULL, 0, jap, 2, | |
| 221 | _("Authorize"), G_CALLBACK(authorize_add_cb), | |
| 222 | _("Deny"), G_CALLBACK(deny_add_cb)); | |
| 223 | g_free(msg); | |
| 8043 | 224 | jabber_id_free(jid); |
| 7145 | 225 | return; |
| 7813 | 226 | } else if(type && !strcmp(type, "subscribed")) { |
| 7014 | 227 | /* we've been allowed to see their presence, but we don't care */ |
| 8043 | 228 | jabber_id_free(jid); |
| 7014 | 229 | return; |
| 230 | } else { | |
| 231 | if((y = xmlnode_get_child(packet, "show"))) { | |
| 232 | char *show = xmlnode_get_data(y); | |
| 8185 | 233 | state = show_to_state(show); |
| 7014 | 234 | g_free(show); |
| 235 | } else { | |
| 236 | state = 0; | |
| 237 | } | |
| 238 | } | |
| 239 | ||
| 7310 | 240 | |
| 7014 | 241 | for(y = packet->child; y; y = y->next) { |
| 8135 | 242 | if(y->type != XMLNODE_TYPE_TAG) |
| 7014 | 243 | continue; |
| 244 | ||
| 245 | if(!strcmp(y->name, "status")) { | |
| 7615 | 246 | g_free(status); |
| 7014 | 247 | status = xmlnode_get_data(y); |
| 248 | } else if(!strcmp(y->name, "priority")) { | |
| 249 | char *p = xmlnode_get_data(y); | |
| 250 | if(p) { | |
| 251 | priority = atoi(p); | |
| 252 | g_free(p); | |
| 253 | } | |
| 254 | } else if(!strcmp(y->name, "x")) { | |
| 255 | const char *xmlns = xmlnode_get_attrib(y, "xmlns"); | |
| 256 | if(xmlns && !strcmp(xmlns, "http://jabber.org/protocol/muc#user")) { | |
| 257 | /* this is where we'd normally get the "op" status of the | |
| 258 | * user, but since we don't have a good way to show that yet | |
| 259 | * we'll ignore it */ | |
| 7629 | 260 | xmlnode *z; |
| 261 | ||
| 7014 | 262 | muc = TRUE; |
| 7629 | 263 | if((z = xmlnode_get_child(y, "status"))) { |
| 264 | const char *code = xmlnode_get_attrib(z, "code"); | |
| 265 | if(code && !strcmp(code, "201")) { | |
| 7895 | 266 | chat = jabber_chat_find(js, jid->node, jid->domain); |
| 267 | gaim_request_action(js->gc, _("Create New Room"), | |
| 268 | _("Create New Room"), | |
| 269 | _("You are creating a new room. Would you like to " | |
| 270 | "configure it, or accept the default settings?"), | |
| 271 | 1, chat, 2, _("Configure Room"), | |
| 7923 | 272 | G_CALLBACK(jabber_chat_request_room_configure), |
| 7895 | 273 | _("Accept Defaults"), |
| 274 | G_CALLBACK(jabber_chat_create_instant_room)); | |
| 7629 | 275 | } |
| 276 | } | |
| 7944 | 277 | if((z = xmlnode_get_child(y, "item"))) { |
| 278 | real_jid = xmlnode_get_attrib(z, "jid"); | |
| 279 | } | |
| 7014 | 280 | } |
| 281 | } | |
| 282 | } | |
| 283 | ||
| 284 | ||
| 7322 | 285 | if(jid->node && (chat = jabber_chat_find(js, jid->node, jid->domain))) { |
| 7014 | 286 | static int i = 0; |
| 287 | char *room_jid = g_strdup_printf("%s@%s", jid->node, jid->domain); | |
| 288 | ||
| 289 | if(state == JABBER_STATE_ERROR) { | |
| 290 | const char *code = NULL; | |
| 291 | char *text = NULL; | |
| 292 | char *buf; | |
| 293 | xmlnode *error = xmlnode_get_child(packet, "error"); | |
| 294 | if(error) { | |
| 295 | /* I should make my own messages so they can be | |
| 296 | * translated, but i'm tired */ | |
| 297 | code = xmlnode_get_attrib(error, "code"); | |
| 298 | text = xmlnode_get_data(error); | |
| 299 | } | |
| 300 | ||
| 301 | if(!code) | |
| 302 | code = ""; | |
| 303 | ||
| 7321 | 304 | if(chat->conv) { |
| 305 | if(!text) | |
| 306 | text = g_strdup(_("Unknown error")); | |
| 307 | buf = g_strdup_printf("Error %s in chat %s: %s", | |
| 308 | code, from, text); | |
| 309 | serv_got_chat_left(js->gc, chat->id); | |
| 310 | } else { | |
| 311 | if(!text) | |
| 312 | text = g_strdup(_("Unable to join chat")); | |
| 313 | buf = g_strdup_printf("Error %s joining chat %s: %s", | |
| 314 | code, from, text); | |
| 315 | } | |
| 7014 | 316 | gaim_notify_error(js->gc, _("Error"), _("Error"), buf); |
| 317 | g_free(text); | |
| 318 | g_free(buf); | |
| 319 | ||
| 320 | jabber_chat_destroy(chat); | |
| 7310 | 321 | jabber_id_free(jid); |
| 7615 | 322 | g_free(status); |
| 8182 | 323 | g_free(room_jid); |
| 7014 | 324 | return; |
| 325 | } | |
| 326 | ||
| 327 | ||
| 7813 | 328 | if(type && !strcmp(type, "unavailable")) { |
| 7972 | 329 | gboolean nick_change = FALSE; |
| 7973 | 330 | |
| 8182 | 331 | /* If we haven't joined the chat yet, we don't care that someone left */ |
| 332 | if(!chat->conv) { | |
| 333 | jabber_id_free(jid); | |
| 334 | g_free(status); | |
| 335 | g_free(room_jid); | |
| 336 | return; | |
| 337 | } | |
| 338 | ||
| 7973 | 339 | jabber_buddy_remove_resource(jb, jid->resource); |
| 7972 | 340 | if(chat->muc) { |
| 341 | xmlnode *x; | |
| 8135 | 342 | for(x = xmlnode_get_child(packet, "x"); x; x = xmlnode_get_next_twin(x)) { |
| 7972 | 343 | const char *xmlns, *nick, *code; |
| 344 | xmlnode *stat, *item; | |
| 345 | if(!(xmlns = xmlnode_get_attrib(x, "xmlns")) || | |
| 346 | strcmp(xmlns, "http://jabber.org/protocol/muc#user")) | |
| 347 | continue; | |
| 348 | if(!(stat = xmlnode_get_child(x, "status"))) | |
| 349 | continue; | |
| 350 | if(!(code = xmlnode_get_attrib(stat, "code")) || strcmp(code, "303")) | |
| 351 | continue; | |
| 352 | if(!(item = xmlnode_get_child(x, "item"))) | |
| 353 | continue; | |
| 354 | if(!(nick = xmlnode_get_attrib(item, "nick"))) | |
| 355 | continue; | |
| 356 | nick_change = TRUE; | |
| 357 | gaim_conv_chat_rename_user(GAIM_CONV_CHAT(chat->conv), jid->resource, nick); | |
| 358 | break; | |
| 359 | } | |
| 360 | } | |
| 361 | if(!nick_change) { | |
| 8158 | 362 | if(!g_utf8_collate(jid->resource, gaim_conv_chat_get_nick(GAIM_CONV_CHAT(chat->conv)))) { |
| 7972 | 363 | serv_got_chat_left(js->gc, chat->id); |
| 364 | jabber_chat_destroy(chat); | |
| 365 | } else { | |
| 366 | gaim_conv_chat_remove_user(GAIM_CONV_CHAT(chat->conv), jid->resource, | |
| 7974 | 367 | status); |
| 7972 | 368 | } |
| 7014 | 369 | } |
| 370 | } else { | |
| 8182 | 371 | if(!chat->conv) { |
| 372 | chat->id = i++; | |
| 373 | chat->muc = muc; | |
| 374 | chat->conv = serv_got_joined_chat(js->gc, chat->id, room_jid); | |
| 375 | gaim_conv_chat_set_nick(GAIM_CONV_CHAT(chat->conv), jid->resource); | |
| 376 | } | |
| 377 | ||
| 7973 | 378 | jabber_buddy_track_resource(jb, jid->resource, priority, state, |
| 379 | status); | |
| 380 | ||
| 7014 | 381 | if(!jabber_chat_find_buddy(chat->conv, jid->resource)) |
|
7118
280b3b85a28a
[gaim-migrate @ 7685]
Christian Hammond <chipx86@chipx86.com>
parents:
7095
diff
changeset
|
382 | gaim_conv_chat_add_user(GAIM_CONV_CHAT(chat->conv), jid->resource, |
| 7944 | 383 | real_jid); |
| 7014 | 384 | } |
| 385 | g_free(room_jid); | |
| 386 | } else { | |
| 7124 | 387 | if(state != JABBER_STATE_ERROR && !(jb->subscription & JABBER_SUB_TO)) { |
| 7014 | 388 | gaim_debug(GAIM_DEBUG_INFO, "jabber", |
| 389 | "got unexpected presence from %s, ignoring\n", from); | |
| 390 | jabber_id_free(jid); | |
| 7615 | 391 | g_free(status); |
| 7014 | 392 | return; |
| 393 | } | |
| 394 | ||
| 7322 | 395 | buddy_name = g_strdup_printf("%s%s%s", jid->node ? jid->node : "", |
| 396 | jid->node ? "@" : "", jid->domain); | |
| 7014 | 397 | if((b = gaim_find_buddy(js->gc->account, buddy_name)) == NULL) { |
| 398 | jabber_id_free(jid); | |
| 399 | g_free(buddy_name); | |
| 7615 | 400 | g_free(status); |
| 7014 | 401 | return; |
| 402 | } | |
| 403 | ||
| 404 | if(state == JABBER_STATE_ERROR || | |
| 7813 | 405 | (type && (!strcmp(type, "unavailable") || |
| 406 | !strcmp(type, "unsubscribed")))) { | |
| 8043 | 407 | GaimConversation *conv; |
| 408 | ||
| 7014 | 409 | jabber_buddy_remove_resource(jb, jid->resource); |
| 8043 | 410 | if((conv = jabber_find_unnormalized_conv(from, js->gc->account))) |
| 411 | gaim_conversation_set_name(conv, buddy_name); | |
| 412 | ||
| 7395 | 413 | } else { |
| 7014 | 414 | jabber_buddy_track_resource(jb, jid->resource, priority, state, |
| 415 | status); | |
| 7395 | 416 | } |
| 7014 | 417 | |
| 7835 | 418 | if((jbr = jabber_buddy_find_resource(jb, NULL))) |
| 7014 | 419 | serv_got_update(js->gc, buddy_name, 1, 0, b->signon, b->idle, |
| 420 | jbr->state); | |
| 421 | else | |
| 422 | serv_got_update(js->gc, buddy_name, 0, 0, 0, 0, 0); | |
| 423 | ||
| 424 | g_free(buddy_name); | |
| 425 | } | |
| 426 | g_free(status); | |
| 427 | jabber_id_free(jid); | |
| 428 | } | |
| 429 | ||
| 430 | void jabber_presence_subscription_set(JabberStream *js, const char *who, const char *type) | |
| 431 | { | |
| 432 | xmlnode *presence = xmlnode_new("presence"); | |
| 433 | ||
| 434 | xmlnode_set_attrib(presence, "to", who); | |
| 435 | xmlnode_set_attrib(presence, "type", type); | |
| 436 | ||
| 437 | jabber_send(js, presence); | |
| 438 | xmlnode_free(presence); | |
| 439 | } |