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