Thu, 04 Mar 2004 17:07:56 +0000
[gaim-migrate @ 9125]
fix a crash in jabber chats when you leave the chat before closing the
configuration dialog, and plug a memleak. Thanks deryni for pointing the
crash out
| 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 "multi.h" /* for proto_chat_entry */ | |
| 7310 | 24 | #include "notify.h" |
| 8113 | 25 | #include "request.h" |
| 26 | #include "roomlist.h" | |
| 7971 | 27 | #include "util.h" |
| 7014 | 28 | |
| 29 | #include "chat.h" | |
| 7895 | 30 | #include "iq.h" |
| 7014 | 31 | #include "message.h" |
| 7073 | 32 | #include "presence.h" |
| 7923 | 33 | #include "xdata.h" |
| 7014 | 34 | |
| 35 | GList *jabber_chat_info(GaimConnection *gc) | |
| 36 | { | |
| 37 | GList *m = NULL; | |
| 38 | struct proto_chat_entry *pce; | |
| 39 | JabberStream *js = gc->proto_data; | |
| 40 | ||
| 41 | pce = g_new0(struct proto_chat_entry, 1); | |
|
7841
0000a4c68bf8
[gaim-migrate @ 8494]
Mark Doliner <markdoliner@pidgin.im>
parents:
7400
diff
changeset
|
42 | pce->label = _("_Room:"); |
| 7014 | 43 | pce->identifier = "room"; |
| 44 | m = g_list_append(m, pce); | |
| 45 | ||
| 46 | pce = g_new0(struct proto_chat_entry, 1); | |
|
7841
0000a4c68bf8
[gaim-migrate @ 8494]
Mark Doliner <markdoliner@pidgin.im>
parents:
7400
diff
changeset
|
47 | pce->label = _("_Server:"); |
| 7014 | 48 | pce->identifier = "server"; |
| 8043 | 49 | pce->def = js->chat_servers ? js->chat_servers->data : "conference.jabber.org"; |
| 7014 | 50 | m = g_list_append(m, pce); |
| 51 | ||
| 52 | pce = g_new0(struct proto_chat_entry, 1); | |
|
7841
0000a4c68bf8
[gaim-migrate @ 8494]
Mark Doliner <markdoliner@pidgin.im>
parents:
7400
diff
changeset
|
53 | pce->label = _("_Handle:"); |
| 7014 | 54 | pce->identifier = "handle"; |
| 55 | pce->def = js->user->node; | |
| 56 | m = g_list_append(m, pce); | |
| 57 | ||
| 58 | pce = g_new0(struct proto_chat_entry, 1); | |
|
7841
0000a4c68bf8
[gaim-migrate @ 8494]
Mark Doliner <markdoliner@pidgin.im>
parents:
7400
diff
changeset
|
59 | pce->label = _("_Password:"); |
| 7014 | 60 | pce->identifier = "password"; |
| 61 | pce->secret = TRUE; | |
| 62 | m = g_list_append(m, pce); | |
| 63 | ||
| 64 | return m; | |
| 65 | } | |
| 66 | ||
| 67 | JabberChat *jabber_chat_find(JabberStream *js, const char *room, | |
| 68 | const char *server) | |
| 69 | { | |
| 70 | JabberChat *chat; | |
| 71 | char *room_jid; | |
| 72 | ||
| 73 | room_jid = g_strdup_printf("%s@%s", room, server); | |
| 74 | ||
| 7322 | 75 | chat = g_hash_table_lookup(js->chats, jabber_normalize(NULL, room_jid)); |
| 7014 | 76 | g_free(room_jid); |
| 77 | ||
| 78 | return chat; | |
| 79 | } | |
| 80 | ||
| 81 | struct _find_by_id_data { | |
| 82 | int id; | |
| 83 | JabberChat *chat; | |
| 84 | }; | |
| 85 | ||
| 86 | void find_by_id_foreach_cb(gpointer key, gpointer value, gpointer user_data) | |
| 87 | { | |
| 88 | JabberChat *chat = value; | |
| 89 | struct _find_by_id_data *fbid = user_data; | |
| 90 | ||
| 91 | if(chat->id == fbid->id) | |
| 92 | fbid->chat = chat; | |
| 93 | } | |
| 94 | ||
| 95 | JabberChat *jabber_chat_find_by_id(JabberStream *js, int id) | |
| 96 | { | |
| 97 | JabberChat *chat; | |
| 98 | struct _find_by_id_data *fbid = g_new0(struct _find_by_id_data, 1); | |
| 7073 | 99 | fbid->id = id; |
| 7014 | 100 | g_hash_table_foreach(js->chats, find_by_id_foreach_cb, fbid); |
| 101 | chat = fbid->chat; | |
| 102 | g_free(fbid); | |
| 103 | return chat; | |
| 104 | } | |
| 105 | ||
| 106 | void jabber_chat_invite(GaimConnection *gc, int id, const char *msg, | |
| 107 | const char *name) | |
| 108 | { | |
| 109 | JabberStream *js = gc->proto_data; | |
| 110 | JabberChat *chat; | |
| 111 | xmlnode *message, *body, *x, *invite; | |
| 112 | char *room_jid; | |
| 113 | ||
| 114 | chat = jabber_chat_find_by_id(js, id); | |
| 115 | if(!chat) | |
| 116 | return; | |
| 117 | ||
| 118 | message = xmlnode_new("message"); | |
| 119 | ||
| 120 | room_jid = g_strdup_printf("%s@%s", chat->room, chat->server); | |
| 121 | ||
| 122 | if(chat->muc) { | |
| 123 | xmlnode_set_attrib(message, "to", room_jid); | |
| 124 | x = xmlnode_new_child(message, "x"); | |
| 125 | xmlnode_set_attrib(x, "xmlns", "http://jabber.org/protocol/muc#user"); | |
| 126 | invite = xmlnode_new_child(x, "invite"); | |
| 127 | xmlnode_set_attrib(invite, "to", name); | |
| 128 | body = xmlnode_new_child(invite, "reason"); | |
| 129 | xmlnode_insert_data(body, msg, -1); | |
| 130 | } else { | |
| 131 | xmlnode_set_attrib(message, "to", name); | |
| 132 | body = xmlnode_new_child(message, "body"); | |
| 133 | xmlnode_insert_data(body, msg, -1); | |
| 134 | x = xmlnode_new_child(message, "x"); | |
| 135 | xmlnode_set_attrib(x, "jid", room_jid); | |
| 136 | xmlnode_set_attrib(x, "xmlns", "jabber:x:conference"); | |
| 137 | } | |
| 138 | ||
| 139 | jabber_send(js, message); | |
| 140 | xmlnode_free(message); | |
| 141 | g_free(room_jid); | |
| 142 | } | |
| 143 | ||
| 144 | void jabber_chat_join(GaimConnection *gc, GHashTable *data) | |
| 145 | { | |
| 146 | JabberChat *chat; | |
| 147 | char *room, *server, *handle, *passwd; | |
| 148 | xmlnode *presence, *x; | |
| 7262 | 149 | char *tmp, *room_jid, *full_jid; |
| 7014 | 150 | JabberStream *js = gc->proto_data; |
| 151 | ||
| 152 | room = g_hash_table_lookup(data, "room"); | |
| 153 | server = g_hash_table_lookup(data, "server"); | |
| 154 | handle = g_hash_table_lookup(data, "handle"); | |
| 155 | passwd = g_hash_table_lookup(data, "password"); | |
| 156 | ||
| 8113 | 157 | if(!room || !server) |
| 7014 | 158 | return; |
| 159 | ||
| 8113 | 160 | if(!handle) |
| 161 | handle = js->user->node; | |
| 162 | ||
| 7310 | 163 | if(!jabber_nodeprep_validate(room)) { |
| 164 | char *buf = g_strdup_printf(_("%s is not a valid room name"), room); | |
| 165 | gaim_notify_error(gc, _("Invalid Room Name"), _("Invalid Room Name"), | |
| 166 | buf); | |
| 167 | g_free(buf); | |
| 168 | return; | |
| 169 | } else if(!jabber_nameprep_validate(server)) { | |
| 170 | char *buf = g_strdup_printf(_("%s is not a valid server name"), server); | |
| 171 | gaim_notify_error(gc, _("Invalid Server Name"), | |
| 172 | _("Invalid Server Name"), buf); | |
| 173 | g_free(buf); | |
| 174 | return; | |
| 175 | } else if(!jabber_resourceprep_validate(handle)) { | |
| 176 | char *buf = g_strdup_printf(_("%s is not a valid room handle"), handle); | |
| 177 | gaim_notify_error(gc, _("Invalid Room Handle"), | |
| 178 | _("Invalid Room Handle"), buf); | |
| 179 | } | |
| 180 | ||
| 7014 | 181 | if(jabber_chat_find(js, room, server)) |
| 182 | return; | |
| 183 | ||
| 7262 | 184 | tmp = g_strdup_printf("%s@%s", room, server); |
| 7322 | 185 | room_jid = g_strdup(jabber_normalize(NULL, tmp)); |
| 7262 | 186 | g_free(tmp); |
| 7014 | 187 | |
| 188 | chat = g_new0(JabberChat, 1); | |
| 189 | chat->js = gc->proto_data; | |
| 190 | ||
| 191 | chat->room = g_strdup(room); | |
| 192 | chat->server = g_strdup(server); | |
| 193 | ||
| 194 | g_hash_table_insert(js->chats, room_jid, chat); | |
| 195 | ||
| 7073 | 196 | presence = jabber_presence_create(gc->away_state, gc->away); |
| 7014 | 197 | full_jid = g_strdup_printf("%s/%s", room_jid, handle); |
| 198 | xmlnode_set_attrib(presence, "to", full_jid); | |
| 199 | g_free(full_jid); | |
| 200 | ||
| 201 | x = xmlnode_new_child(presence, "x"); | |
| 202 | xmlnode_set_attrib(x, "xmlns", "http://jabber.org/protocol/muc"); | |
| 203 | ||
| 204 | if(passwd && *passwd) { | |
| 205 | xmlnode *password = xmlnode_new_child(x, "password"); | |
| 206 | xmlnode_insert_data(password, passwd, -1); | |
| 207 | } | |
| 208 | ||
| 209 | jabber_send(js, presence); | |
| 210 | xmlnode_free(presence); | |
| 211 | } | |
| 212 | ||
| 213 | void jabber_chat_leave(GaimConnection *gc, int id) | |
| 214 | { | |
| 215 | JabberStream *js = gc->proto_data; | |
| 216 | JabberChat *chat = jabber_chat_find_by_id(js, id); | |
| 7974 | 217 | |
| 7014 | 218 | |
| 219 | if(!chat) | |
| 220 | return; | |
| 221 | ||
| 7974 | 222 | jabber_chat_part(chat, NULL); |
| 7014 | 223 | } |
| 224 | ||
| 225 | void jabber_chat_destroy(JabberChat *chat) | |
| 226 | { | |
| 227 | JabberStream *js = chat->js; | |
| 228 | char *room_jid = g_strdup_printf("%s@%s", chat->room, chat->server); | |
| 229 | ||
| 7322 | 230 | g_hash_table_remove(js->chats, jabber_normalize(NULL, room_jid)); |
| 7014 | 231 | g_free(room_jid); |
| 8396 | 232 | } |
| 233 | ||
| 234 | void jabber_chat_free(JabberChat *chat) | |
| 235 | { | |
| 236 | if(chat->config_dialog_handle) | |
| 237 | gaim_request_close(chat->config_dialog_type, chat->config_dialog_handle); | |
| 7014 | 238 | |
| 239 | g_free(chat->room); | |
| 240 | g_free(chat->server); | |
| 241 | g_free(chat); | |
| 242 | } | |
| 243 | ||
| 244 | gboolean jabber_chat_find_buddy(GaimConversation *conv, const char *name) | |
| 245 | { | |
|
7118
280b3b85a28a
[gaim-migrate @ 7685]
Christian Hammond <chipx86@chipx86.com>
parents:
7073
diff
changeset
|
246 | GList *m = gaim_conv_chat_get_users(GAIM_CONV_CHAT(conv)); |
| 7014 | 247 | |
| 248 | while(m) { | |
| 249 | if(!strcmp(m->data, name)) | |
| 250 | return TRUE; | |
| 251 | m = m->next; | |
| 252 | } | |
| 253 | ||
| 254 | return FALSE; | |
| 255 | } | |
| 256 | ||
| 7398 | 257 | char *jabber_chat_buddy_real_name(GaimConnection *gc, int id, const char *who) |
| 258 | { | |
| 259 | JabberStream *js = gc->proto_data; | |
| 260 | JabberChat *chat; | |
| 261 | ||
| 262 | chat = jabber_chat_find_by_id(js, id); | |
| 263 | ||
| 264 | if(!chat) | |
| 265 | return NULL; | |
| 266 | ||
| 267 | return g_strdup_printf("%s@%s/%s", chat->room, chat->server, who); | |
| 268 | } | |
| 7895 | 269 | |
| 7923 | 270 | static void jabber_chat_room_configure_x_data_cb(JabberStream *js, xmlnode *result, gpointer data) |
| 271 | { | |
| 272 | JabberChat *chat = data; | |
| 273 | xmlnode *query; | |
| 274 | JabberIq *iq; | |
| 275 | char *to = g_strdup_printf("%s@%s", chat->room, chat->server); | |
| 276 | ||
| 277 | iq = jabber_iq_new_query(js, JABBER_IQ_SET, "http://jabber.org/protocol/muc#owner"); | |
| 278 | xmlnode_set_attrib(iq->node, "to", to); | |
| 279 | g_free(to); | |
| 280 | ||
| 281 | query = xmlnode_get_child(iq->node, "query"); | |
| 282 | ||
| 283 | xmlnode_insert_child(query, result); | |
| 284 | ||
| 285 | jabber_iq_send(iq); | |
| 286 | } | |
| 287 | ||
| 288 | static void jabber_chat_room_configure_cb(JabberStream *js, xmlnode *packet, gpointer data) | |
| 289 | { | |
| 290 | xmlnode *query, *x; | |
| 291 | const char *type = xmlnode_get_attrib(packet, "type"); | |
| 292 | const char *from = xmlnode_get_attrib(packet, "from"); | |
| 7926 | 293 | char *msg; |
| 7923 | 294 | JabberChat *chat; |
| 295 | JabberID *jid; | |
| 296 | ||
| 297 | if(!type || !from) | |
| 298 | return; | |
| 299 | ||
| 300 | ||
| 7926 | 301 | |
| 7923 | 302 | if(!strcmp(type, "result")) { |
| 303 | jid = jabber_id_new(from); | |
| 304 | ||
| 305 | if(!jid) | |
| 306 | return; | |
| 307 | ||
| 308 | chat = jabber_chat_find(js, jid->node, jid->domain); | |
| 309 | jabber_id_free(jid); | |
| 310 | ||
| 311 | if(!chat) | |
| 312 | return; | |
| 313 | ||
| 314 | if(!(query = xmlnode_get_child(packet, "query"))) | |
| 315 | return; | |
| 316 | ||
| 8135 | 317 | for(x = xmlnode_get_child(query, "x"); x; x = xmlnode_get_next_twin(x)) { |
| 7923 | 318 | const char *xmlns; |
| 319 | if(!(xmlns = xmlnode_get_attrib(x, "xmlns"))) | |
| 320 | continue; | |
| 321 | ||
| 322 | if(!strcmp(xmlns, "jabber:x:data")) { | |
| 8396 | 323 | chat->config_dialog_type = GAIM_REQUEST_FIELDS; |
| 324 | chat->config_dialog_handle = jabber_x_data_request(js, x, jabber_chat_room_configure_x_data_cb, chat); | |
| 7923 | 325 | return; |
| 326 | } | |
| 327 | } | |
| 7926 | 328 | } else if(!strcmp(type, "error")) { |
| 329 | xmlnode *errnode = xmlnode_get_child(packet, "error"); | |
| 330 | const char *code = NULL; | |
| 331 | char *code_txt = NULL; | |
| 332 | char *msg; | |
| 333 | char *text = NULL; | |
| 334 | ||
| 335 | if(errnode) { | |
| 336 | code = xmlnode_get_attrib(errnode, "code"); | |
| 337 | text = xmlnode_get_data(errnode); | |
| 338 | } | |
| 339 | ||
| 340 | if(code) | |
| 341 | code_txt = g_strdup_printf(_(" (Code %s)"), code); | |
| 342 | ||
| 343 | msg = g_strdup_printf("%s%s", text ? text : "", code_txt ? code_txt : ""); | |
| 344 | gaim_notify_error(js->gc, _("Configuration error"), _("Configuration error"), msg); | |
| 345 | ||
| 346 | g_free(msg); | |
| 347 | if(code_txt) | |
| 348 | g_free(code_txt); | |
| 349 | ||
| 350 | return; | |
| 7923 | 351 | } |
| 352 | ||
| 7926 | 353 | msg = g_strdup_printf("Unable to configure room %s", from); |
| 354 | ||
| 355 | gaim_notify_info(js->gc, _("Unable to configure"), _("Unable to configure"), msg); | |
| 356 | g_free(msg); | |
| 7923 | 357 | |
| 358 | } | |
| 359 | ||
| 360 | void jabber_chat_request_room_configure(JabberChat *chat) { | |
| 361 | JabberIq *iq; | |
| 362 | xmlnode *query; | |
| 363 | char *room_jid; | |
| 364 | ||
| 7895 | 365 | if(!chat) |
| 366 | return; | |
| 367 | ||
| 8396 | 368 | chat->config_dialog_handle = NULL; |
| 369 | ||
| 7955 | 370 | if(!chat->muc) { |
| 371 | gaim_notify_error(chat->js->gc, _("Room Configuration Error"), _("Room Configuration Error"), | |
| 372 | _("This room is not capable of being configured")); | |
| 373 | return; | |
| 374 | } | |
| 375 | ||
| 7923 | 376 | iq = jabber_iq_new_query(chat->js, JABBER_IQ_SET, |
| 377 | "http://jabber.org/protocol/muc#owner"); | |
| 378 | query = xmlnode_get_child(iq->node, "query"); | |
| 379 | room_jid = g_strdup_printf("%s@%s", chat->room, chat->server); | |
| 7895 | 380 | |
| 7923 | 381 | xmlnode_set_attrib(iq->node, "to", room_jid); |
| 382 | ||
| 383 | jabber_iq_set_callback(iq, jabber_chat_room_configure_cb, NULL); | |
| 384 | ||
| 385 | jabber_iq_send(iq); | |
| 386 | ||
| 387 | g_free(room_jid); | |
| 7895 | 388 | } |
| 389 | ||
| 390 | void jabber_chat_create_instant_room(JabberChat *chat) { | |
| 391 | JabberIq *iq; | |
| 392 | xmlnode *query, *x; | |
| 393 | char *room_jid; | |
| 394 | ||
| 395 | if(!chat) | |
| 396 | return; | |
| 397 | ||
| 8396 | 398 | chat->config_dialog_handle = NULL; |
| 399 | ||
| 7895 | 400 | iq = jabber_iq_new_query(chat->js, JABBER_IQ_SET, |
| 401 | "http://jabber.org/protocol/muc#owner"); | |
| 402 | query = xmlnode_get_child(iq->node, "query"); | |
| 403 | x = xmlnode_new_child(query, "x"); | |
| 404 | room_jid = g_strdup_printf("%s@%s", chat->room, chat->server); | |
| 405 | ||
| 406 | xmlnode_set_attrib(iq->node, "to", room_jid); | |
| 407 | xmlnode_set_attrib(x, "xmlns", "jabber:x:data"); | |
| 408 | xmlnode_set_attrib(x, "type", "submit"); | |
| 409 | ||
| 410 | jabber_iq_send(iq); | |
| 411 | ||
| 412 | g_free(room_jid); | |
| 413 | } | |
| 7955 | 414 | |
| 415 | static void jabber_chat_register_x_data_result_cb(JabberStream *js, xmlnode *packet, gpointer data) | |
| 416 | { | |
| 417 | const char *type = xmlnode_get_attrib(packet, "type"); | |
| 418 | ||
| 419 | if(type && !strcmp(type, "error")) { | |
| 420 | /* XXX: handle an error (you'll get a 409 if the nick is already registered) */ | |
| 421 | } | |
| 422 | } | |
| 423 | ||
| 424 | static void jabber_chat_register_x_data_cb(JabberStream *js, xmlnode *result, gpointer data) | |
| 425 | { | |
| 426 | JabberChat *chat = data; | |
| 427 | xmlnode *query; | |
| 428 | JabberIq *iq; | |
| 429 | char *to = g_strdup_printf("%s@%s", chat->room, chat->server); | |
| 430 | ||
| 431 | iq = jabber_iq_new_query(js, JABBER_IQ_SET, "jabber:iq:register"); | |
| 432 | xmlnode_set_attrib(iq->node, "to", to); | |
| 433 | g_free(to); | |
| 434 | ||
| 435 | query = xmlnode_get_child(iq->node, "query"); | |
| 436 | ||
| 437 | xmlnode_insert_child(query, result); | |
| 438 | ||
| 439 | jabber_iq_set_callback(iq, jabber_chat_register_x_data_result_cb, NULL); | |
| 440 | ||
| 441 | jabber_iq_send(iq); | |
| 442 | } | |
| 443 | ||
| 444 | static void jabber_chat_register_cb(JabberStream *js, xmlnode *packet, gpointer data) | |
| 445 | { | |
| 446 | xmlnode *query, *x; | |
| 447 | const char *type = xmlnode_get_attrib(packet, "type"); | |
| 448 | const char *from = xmlnode_get_attrib(packet, "from"); | |
| 449 | char *msg; | |
| 450 | JabberChat *chat; | |
| 451 | JabberID *jid; | |
| 452 | ||
| 453 | if(!type || !from) | |
| 454 | return; | |
| 455 | ||
| 456 | if(!strcmp(type, "result")) { | |
| 457 | jid = jabber_id_new(from); | |
| 458 | ||
| 459 | if(!jid) | |
| 460 | return; | |
| 461 | ||
| 462 | chat = jabber_chat_find(js, jid->node, jid->domain); | |
| 463 | jabber_id_free(jid); | |
| 464 | ||
| 465 | if(!chat) | |
| 466 | return; | |
| 467 | ||
| 468 | if(!(query = xmlnode_get_child(packet, "query"))) | |
| 469 | return; | |
| 470 | ||
| 8135 | 471 | for(x = xmlnode_get_child(query, "x"); x; x = xmlnode_get_next_twin(x)) { |
| 7955 | 472 | const char *xmlns; |
| 473 | ||
| 474 | if(!(xmlns = xmlnode_get_attrib(x, "xmlns"))) | |
| 475 | continue; | |
| 476 | ||
| 477 | if(!strcmp(xmlns, "jabber:x:data")) { | |
| 478 | jabber_x_data_request(js, x, jabber_chat_register_x_data_cb, chat); | |
| 479 | return; | |
| 480 | } | |
| 481 | } | |
| 482 | } else if(!strcmp(type, "error")) { | |
| 483 | /* XXX: how many places is this code duplicated? Fix it, immediately */ | |
| 484 | xmlnode *errnode = xmlnode_get_child(packet, "error"); | |
| 485 | const char *code = NULL; | |
| 486 | char *code_txt = NULL; | |
| 487 | char *msg; | |
| 488 | char *text = NULL; | |
| 489 | ||
| 490 | if(errnode) { | |
| 491 | code = xmlnode_get_attrib(errnode, "code"); | |
| 492 | text = xmlnode_get_data(errnode); | |
| 493 | } | |
| 494 | ||
| 495 | if(code) | |
| 496 | code_txt = g_strdup_printf(_(" (Code %s)"), code); | |
| 497 | ||
| 498 | msg = g_strdup_printf("%s%s", text ? text : "", code_txt ? code_txt : ""); | |
| 499 | gaim_notify_error(js->gc, _("Registration error"), _("Registration error"), msg); | |
| 500 | ||
| 501 | g_free(msg); | |
| 502 | if(code_txt) | |
| 503 | g_free(code_txt); | |
| 504 | ||
| 505 | return; | |
| 506 | } | |
| 507 | ||
| 508 | msg = g_strdup_printf("Unable to configure room %s", from); | |
| 509 | ||
| 510 | gaim_notify_info(js->gc, _("Unable to configure"), _("Unable to configure"), msg); | |
| 511 | g_free(msg); | |
| 512 | ||
| 513 | } | |
| 514 | ||
| 515 | void jabber_chat_register(JabberChat *chat) | |
| 516 | { | |
| 517 | JabberIq *iq; | |
| 518 | char *room_jid; | |
| 519 | ||
| 520 | if(!chat) | |
| 521 | return; | |
| 522 | ||
| 523 | room_jid = g_strdup_printf("%s@%s", chat->room, chat->server); | |
| 524 | ||
| 525 | iq = jabber_iq_new_query(chat->js, JABBER_IQ_GET, "jabber:iq:register"); | |
| 526 | xmlnode_set_attrib(iq->node, "to", room_jid); | |
| 527 | g_free(room_jid); | |
| 528 | ||
| 529 | jabber_iq_set_callback(iq, jabber_chat_register_cb, NULL); | |
| 530 | ||
| 531 | jabber_iq_send(iq); | |
| 532 | } | |
| 533 | ||
| 7971 | 534 | /* merge this with the function below when we get everyone on the same page wrt /commands */ |
| 535 | void jabber_chat_change_topic(JabberChat *chat, const char *topic) | |
| 536 | { | |
| 537 | if(topic && *topic) { | |
| 538 | JabberMessage *jm; | |
| 539 | jm = g_new0(JabberMessage, 1); | |
| 540 | jm->js = chat->js; | |
| 541 | jm->type = JABBER_MESSAGE_GROUPCHAT; | |
| 542 | jm->subject = gaim_markup_strip_html(topic); | |
| 543 | jm->to = g_strdup_printf("%s@%s", chat->room, chat->server); | |
| 544 | jabber_message_send(jm); | |
| 545 | jabber_message_free(jm); | |
| 546 | } else { | |
| 547 | const char *cur = gaim_conv_chat_get_topic(GAIM_CONV_CHAT(chat->conv)); | |
| 548 | char *buf; | |
| 7955 | 549 | |
| 7971 | 550 | if(cur) |
| 8261 | 551 | buf = g_strdup_printf(_("current topic is: %s"), cur); |
| 7971 | 552 | else |
| 553 | buf = g_strdup(_("No topic is set")); | |
| 554 | gaim_conv_chat_write(GAIM_CONV_CHAT(chat->conv), "", buf, | |
| 555 | GAIM_MESSAGE_SYSTEM | GAIM_MESSAGE_NO_LOG, time(NULL)); | |
| 556 | g_free(buf); | |
| 557 | } | |
| 558 | ||
| 559 | } | |
| 560 | ||
| 561 | void jabber_chat_set_topic(GaimConnection *gc, int id, const char *topic) | |
| 562 | { | |
| 563 | JabberStream *js = gc->proto_data; | |
| 564 | JabberChat *chat = jabber_chat_find_by_id(js, id); | |
| 565 | ||
| 566 | if(!chat) | |
| 567 | return; | |
| 568 | ||
| 569 | jabber_chat_change_topic(chat, topic); | |
| 570 | } | |
| 571 | ||
| 572 | ||
| 7972 | 573 | void jabber_chat_change_nick(JabberChat *chat, const char *nick) |
| 574 | { | |
| 575 | xmlnode *presence; | |
| 576 | char *full_jid; | |
| 577 | ||
| 578 | if(!chat->muc) { | |
| 579 | gaim_conv_chat_write(GAIM_CONV_CHAT(chat->conv), "", | |
| 580 | _("Nick changing not supported in non-MUC chatrooms"), | |
| 581 | GAIM_MESSAGE_SYSTEM, time(NULL)); | |
| 582 | return; | |
| 583 | } | |
| 584 | ||
| 585 | presence = jabber_presence_create(chat->js->gc->away_state, chat->js->gc->away); | |
| 586 | full_jid = g_strdup_printf("%s@%s/%s", chat->room, chat->server, nick); | |
| 587 | xmlnode_set_attrib(presence, "to", full_jid); | |
| 588 | g_free(full_jid); | |
| 589 | ||
| 590 | jabber_send(chat->js, presence); | |
| 591 | xmlnode_free(presence); | |
| 592 | } | |
| 593 | ||
| 7974 | 594 | void jabber_chat_part(JabberChat *chat, const char *msg) |
| 595 | { | |
| 596 | char *room_jid; | |
| 597 | xmlnode *presence; | |
| 7972 | 598 | |
| 7974 | 599 | room_jid = g_strdup_printf("%s@%s", chat->room, chat->server); |
| 600 | presence = xmlnode_new("presence"); | |
| 601 | xmlnode_set_attrib(presence, "to", room_jid); | |
| 602 | xmlnode_set_attrib(presence, "type", "unavailable"); | |
| 603 | if(msg) { | |
| 604 | xmlnode *status = xmlnode_new_child(presence, "status"); | |
| 605 | xmlnode_insert_data(status, msg, -1); | |
| 606 | } | |
| 607 | jabber_send(chat->js, presence); | |
| 608 | xmlnode_free(presence); | |
| 609 | g_free(room_jid); | |
| 610 | } | |
| 611 | ||
| 8113 | 612 | static void roomlist_disco_result_cb(JabberStream *js, xmlnode *packet, gpointer data) |
| 613 | { | |
| 614 | xmlnode *query; | |
| 615 | xmlnode *item; | |
| 616 | const char *type; | |
| 7974 | 617 | |
| 8113 | 618 | if(!js->roomlist) |
| 619 | return; | |
| 620 | ||
| 621 | if(!(type = xmlnode_get_attrib(packet, "type")) || strcmp(type, "result")) { | |
| 622 | /* XXX: error msg */ | |
| 623 | gaim_roomlist_set_in_progress(js->roomlist, FALSE); | |
| 8120 | 624 | gaim_roomlist_unref(js->roomlist); |
| 625 | js->roomlist = NULL; | |
| 8113 | 626 | return; |
| 627 | } | |
| 628 | ||
| 629 | if(!(query = xmlnode_get_child(packet, "query"))) { | |
| 630 | /* XXX: error msg */ | |
| 631 | gaim_roomlist_set_in_progress(js->roomlist, FALSE); | |
| 8120 | 632 | gaim_roomlist_unref(js->roomlist); |
| 633 | js->roomlist = NULL; | |
| 8113 | 634 | return; |
| 635 | } | |
| 636 | ||
| 8135 | 637 | for(item = xmlnode_get_child(query, "item"); item; |
| 638 | item = xmlnode_get_next_twin(item)) { | |
| 8113 | 639 | const char *name; |
| 640 | GaimRoomlistRoom *room; | |
| 641 | JabberID *jid; | |
| 642 | ||
| 643 | if(!(jid = jabber_id_new(xmlnode_get_attrib(item, "jid")))) | |
| 644 | continue; | |
| 645 | name = xmlnode_get_attrib(item, "name"); | |
| 646 | ||
| 647 | ||
| 648 | room = gaim_roomlist_room_new(GAIM_ROOMLIST_ROOMTYPE_ROOM, jid->node, NULL); | |
| 649 | gaim_roomlist_room_add_field(js->roomlist, room, jid->node); | |
| 650 | gaim_roomlist_room_add_field(js->roomlist, room, jid->domain); | |
| 651 | gaim_roomlist_room_add_field(js->roomlist, room, name ? name : ""); | |
| 652 | gaim_roomlist_room_add(js->roomlist, room); | |
| 653 | ||
| 654 | jabber_id_free(jid); | |
| 655 | } | |
| 656 | gaim_roomlist_set_in_progress(js->roomlist, FALSE); | |
| 657 | gaim_roomlist_unref(js->roomlist); | |
| 658 | js->roomlist = NULL; | |
| 659 | } | |
| 660 | ||
| 661 | static void roomlist_ok_cb(JabberStream *js, const char *server) | |
| 662 | { | |
| 663 | JabberIq *iq; | |
| 664 | ||
| 665 | if(!js->roomlist) | |
| 666 | return; | |
| 667 | ||
| 668 | if(!server || !*server) { | |
| 669 | gaim_notify_error(js->gc, _("Invalid Server"), _("Invalid Server"), NULL); | |
| 670 | return; | |
| 671 | } | |
| 672 | ||
| 673 | gaim_roomlist_set_in_progress(js->roomlist, TRUE); | |
| 674 | ||
| 675 | iq = jabber_iq_new_query(js, JABBER_IQ_GET, "http://jabber.org/protocol/disco#items"); | |
| 676 | ||
| 677 | xmlnode_set_attrib(iq->node, "to", server); | |
| 678 | ||
| 679 | jabber_iq_set_callback(iq, roomlist_disco_result_cb, NULL); | |
| 680 | ||
| 681 | jabber_iq_send(iq); | |
| 682 | } | |
| 683 | ||
| 684 | GaimRoomlist *jabber_roomlist_get_list(GaimConnection *gc) | |
| 685 | { | |
| 686 | JabberStream *js = gc->proto_data; | |
| 687 | GList *fields = NULL; | |
| 688 | GaimRoomlistField *f; | |
| 689 | ||
| 690 | if(js->roomlist) | |
| 691 | gaim_roomlist_unref(js->roomlist); | |
| 692 | ||
| 693 | js->roomlist = gaim_roomlist_new(gaim_connection_get_account(gc)); | |
| 694 | ||
| 695 | f = gaim_roomlist_field_new(GAIM_ROOMLIST_FIELD_STRING, "", "room", TRUE); | |
| 696 | fields = g_list_append(fields, f); | |
| 697 | ||
| 698 | f = gaim_roomlist_field_new(GAIM_ROOMLIST_FIELD_STRING, "", "server", TRUE); | |
| 699 | fields = g_list_append(fields, f); | |
| 700 | ||
| 701 | f = gaim_roomlist_field_new(GAIM_ROOMLIST_FIELD_STRING, _("Description"), "description", FALSE); | |
| 702 | fields = g_list_append(fields, f); | |
| 703 | ||
| 704 | gaim_roomlist_set_fields(js->roomlist, fields); | |
| 705 | ||
| 706 | gaim_request_input(gc, _("Enter a Conference Server"), _("Enter a Conference Server"), | |
| 707 | _("Select a conference server to query"), | |
| 708 | js->chat_servers ? js->chat_servers->data : "conference.jabber.org", | |
| 709 | FALSE, FALSE, _("Find Rooms"), G_CALLBACK(roomlist_ok_cb), _("Cancel"), NULL, js); | |
| 710 | ||
| 711 | return js->roomlist; | |
| 712 | } | |
| 713 | ||
| 714 | void jabber_roomlist_cancel(GaimRoomlist *list) | |
| 715 | { | |
| 716 | GaimConnection *gc; | |
| 717 | JabberStream *js; | |
| 718 | ||
| 719 | gc = gaim_account_get_connection(list->account); | |
| 720 | js = gc->proto_data; | |
| 721 | ||
| 722 | gaim_roomlist_set_in_progress(list, FALSE); | |
| 723 | ||
| 724 | if (js->roomlist == list) { | |
| 725 | js->roomlist = NULL; | |
| 726 | gaim_roomlist_unref(list); | |
| 727 | } | |
| 728 | } | |
| 729 | ||
| 730 | ||
| 731 |