comparison libgaim/protocols/jabber/chat.c @ 14192:60b1bc8dbf37

[gaim-migrate @ 16863] Renamed 'core' to 'libgaim' committer: Tailor Script <tailor@pidgin.im>
author Evan Schoenberg <evan.s@dreskin.net>
date Sat, 19 Aug 2006 01:50:10 +0000
parents
children 7df4ab213577
comparison
equal deleted inserted replaced
14191:009db0b357b5 14192:60b1bc8dbf37
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 "prpl.h" /* for proto_chat_entry */
24 #include "notify.h"
25 #include "request.h"
26 #include "roomlist.h"
27 #include "util.h"
28
29 #include "chat.h"
30 #include "iq.h"
31 #include "message.h"
32 #include "presence.h"
33 #include "xdata.h"
34
35 GList *jabber_chat_info(GaimConnection *gc)
36 {
37 GList *m = NULL;
38 struct proto_chat_entry *pce;
39
40 pce = g_new0(struct proto_chat_entry, 1);
41 pce->label = _("_Room:");
42 pce->identifier = "room";
43 pce->required = TRUE;
44 m = g_list_append(m, pce);
45
46 pce = g_new0(struct proto_chat_entry, 1);
47 pce->label = _("_Server:");
48 pce->identifier = "server";
49 pce->required = TRUE;
50 m = g_list_append(m, pce);
51
52 pce = g_new0(struct proto_chat_entry, 1);
53 pce->label = _("_Handle:");
54 pce->identifier = "handle";
55 pce->required = TRUE;
56 m = g_list_append(m, pce);
57
58 pce = g_new0(struct proto_chat_entry, 1);
59 pce->label = _("_Password:");
60 pce->identifier = "password";
61 pce->secret = TRUE;
62 m = g_list_append(m, pce);
63
64 return m;
65 }
66
67 GHashTable *jabber_chat_info_defaults(GaimConnection *gc, const char *chat_name)
68 {
69 GHashTable *defaults;
70 JabberStream *js = gc->proto_data;
71
72 defaults = g_hash_table_new_full(g_str_hash, g_str_equal, NULL, g_free);
73
74 g_hash_table_insert(defaults, "handle", g_strdup(js->user->node));
75
76 if (js->chat_servers)
77 g_hash_table_insert(defaults, "server", g_strdup(js->chat_servers->data));
78 else
79 g_hash_table_insert(defaults, "server", g_strdup("conference.jabber.org"));
80
81 if (chat_name != NULL) {
82 JabberID *jid = jabber_id_new(chat_name);
83 if(jid) {
84 g_hash_table_insert(defaults, "room", g_strdup(jid->node));
85 if(jid->domain)
86 g_hash_table_replace(defaults, "server", g_strdup(jid->domain));
87 jabber_id_free(jid);
88 }
89 }
90
91 return defaults;
92 }
93
94 JabberChat *jabber_chat_find(JabberStream *js, const char *room,
95 const char *server)
96 {
97 JabberChat *chat = NULL;
98 char *room_jid;
99
100 if(NULL != js->chats)
101 {
102 room_jid = g_strdup_printf("%s@%s", room, server);
103
104 chat = g_hash_table_lookup(js->chats, jabber_normalize(NULL, room_jid));
105 g_free(room_jid);
106 }
107
108 return chat;
109 }
110
111 struct _find_by_id_data {
112 int id;
113 JabberChat *chat;
114 };
115
116 static void find_by_id_foreach_cb(gpointer key, gpointer value, gpointer user_data)
117 {
118 JabberChat *chat = value;
119 struct _find_by_id_data *fbid = user_data;
120
121 if(chat->id == fbid->id)
122 fbid->chat = chat;
123 }
124
125 JabberChat *jabber_chat_find_by_id(JabberStream *js, int id)
126 {
127 JabberChat *chat;
128 struct _find_by_id_data *fbid = g_new0(struct _find_by_id_data, 1);
129 fbid->id = id;
130 g_hash_table_foreach(js->chats, find_by_id_foreach_cb, fbid);
131 chat = fbid->chat;
132 g_free(fbid);
133 return chat;
134 }
135
136 JabberChat *jabber_chat_find_by_conv(GaimConversation *conv)
137 {
138 GaimAccount *account = gaim_conversation_get_account(conv);
139 GaimConnection *gc = gaim_account_get_connection(account);
140 JabberStream *js = gc->proto_data;
141 int id = gaim_conv_chat_get_id(GAIM_CONV_CHAT(conv));
142
143 return jabber_chat_find_by_id(js, id);
144 }
145
146 void jabber_chat_invite(GaimConnection *gc, int id, const char *msg,
147 const char *name)
148 {
149 JabberStream *js = gc->proto_data;
150 JabberChat *chat;
151 xmlnode *message, *body, *x, *invite;
152 char *room_jid;
153
154 chat = jabber_chat_find_by_id(js, id);
155 if(!chat)
156 return;
157
158 message = xmlnode_new("message");
159
160 room_jid = g_strdup_printf("%s@%s", chat->room, chat->server);
161
162 if(chat->muc) {
163 xmlnode_set_attrib(message, "to", room_jid);
164 x = xmlnode_new_child(message, "x");
165 xmlnode_set_namespace(x, "http://jabber.org/protocol/muc#user");
166 invite = xmlnode_new_child(x, "invite");
167 xmlnode_set_attrib(invite, "to", name);
168 body = xmlnode_new_child(invite, "reason");
169 xmlnode_insert_data(body, msg, -1);
170 } else {
171 xmlnode_set_attrib(message, "to", name);
172 body = xmlnode_new_child(message, "body");
173 xmlnode_insert_data(body, msg, -1);
174 x = xmlnode_new_child(message, "x");
175 xmlnode_set_attrib(x, "jid", room_jid);
176 xmlnode_set_namespace(x, "jabber:x:conference");
177 }
178
179 jabber_send(js, message);
180 xmlnode_free(message);
181 g_free(room_jid);
182 }
183
184 void jabber_chat_member_free(JabberChatMember *jcm);
185
186 char *jabber_get_chat_name(GHashTable *data) {
187 char *room, *server, *chat_name = NULL;
188
189 room = g_hash_table_lookup(data, "room");
190 server = g_hash_table_lookup(data, "server");
191
192 if (room && server) {
193 chat_name = g_strdup_printf("%s@%s", room, server);
194 }
195 return chat_name;
196 }
197
198 void jabber_chat_join(GaimConnection *gc, GHashTable *data)
199 {
200 JabberChat *chat;
201 char *room, *server, *handle, *passwd;
202 xmlnode *presence, *x;
203 char *tmp, *room_jid, *full_jid;
204 JabberStream *js = gc->proto_data;
205 GaimPresence *gpresence;
206 GaimStatus *status;
207 JabberBuddyState state;
208 const char *msg;
209 int priority;
210
211 room = g_hash_table_lookup(data, "room");
212 server = g_hash_table_lookup(data, "server");
213 handle = g_hash_table_lookup(data, "handle");
214 passwd = g_hash_table_lookup(data, "password");
215
216 if(!room || !server)
217 return;
218
219 if(!handle)
220 handle = js->user->node;
221
222 if(!jabber_nodeprep_validate(room)) {
223 char *buf = g_strdup_printf(_("%s is not a valid room name"), room);
224 gaim_notify_error(gc, _("Invalid Room Name"), _("Invalid Room Name"),
225 buf);
226 g_free(buf);
227 return;
228 } else if(!jabber_nameprep_validate(server)) {
229 char *buf = g_strdup_printf(_("%s is not a valid server name"), server);
230 gaim_notify_error(gc, _("Invalid Server Name"),
231 _("Invalid Server Name"), buf);
232 g_free(buf);
233 return;
234 } else if(!jabber_resourceprep_validate(handle)) {
235 char *buf = g_strdup_printf(_("%s is not a valid room handle"), handle);
236 gaim_notify_error(gc, _("Invalid Room Handle"),
237 _("Invalid Room Handle"), buf);
238 }
239
240 if(jabber_chat_find(js, room, server))
241 return;
242
243 tmp = g_strdup_printf("%s@%s", room, server);
244 room_jid = g_strdup(jabber_normalize(NULL, tmp));
245 g_free(tmp);
246
247 chat = g_new0(JabberChat, 1);
248 chat->js = gc->proto_data;
249
250 chat->room = g_strdup(room);
251 chat->server = g_strdup(server);
252 chat->handle = g_strdup(handle);
253
254 chat->members = g_hash_table_new_full(g_str_hash, g_str_equal, NULL,
255 (GDestroyNotify)jabber_chat_member_free);
256
257 g_hash_table_insert(js->chats, room_jid, chat);
258
259 gpresence = gaim_account_get_presence(gc->account);
260 status = gaim_presence_get_active_status(gpresence);
261
262 gaim_status_to_jabber(status, &state, &msg, &priority);
263
264 presence = jabber_presence_create(state, msg, priority);
265 full_jid = g_strdup_printf("%s/%s", room_jid, handle);
266 xmlnode_set_attrib(presence, "to", full_jid);
267 g_free(full_jid);
268
269 x = xmlnode_new_child(presence, "x");
270 xmlnode_set_namespace(x, "http://jabber.org/protocol/muc");
271
272 if(passwd && *passwd) {
273 xmlnode *password = xmlnode_new_child(x, "password");
274 xmlnode_insert_data(password, passwd, -1);
275 }
276
277 jabber_send(js, presence);
278 xmlnode_free(presence);
279 }
280
281 void jabber_chat_leave(GaimConnection *gc, int id)
282 {
283 JabberStream *js = gc->proto_data;
284 JabberChat *chat = jabber_chat_find_by_id(js, id);
285
286
287 if(!chat)
288 return;
289
290 jabber_chat_part(chat, NULL);
291
292 chat->conv = NULL;
293 }
294
295 void jabber_chat_destroy(JabberChat *chat)
296 {
297 JabberStream *js = chat->js;
298 char *room_jid = g_strdup_printf("%s@%s", chat->room, chat->server);
299
300 g_hash_table_remove(js->chats, jabber_normalize(NULL, room_jid));
301 g_free(room_jid);
302 }
303
304 void jabber_chat_free(JabberChat *chat)
305 {
306 if(chat->config_dialog_handle)
307 gaim_request_close(chat->config_dialog_type, chat->config_dialog_handle);
308
309 g_free(chat->room);
310 g_free(chat->server);
311 g_free(chat->handle);
312 g_hash_table_destroy(chat->members);
313 g_free(chat);
314 }
315
316 gboolean jabber_chat_find_buddy(GaimConversation *conv, const char *name)
317 {
318 return gaim_conv_chat_find_user(GAIM_CONV_CHAT(conv), name);
319 }
320
321 char *jabber_chat_buddy_real_name(GaimConnection *gc, int id, const char *who)
322 {
323 JabberStream *js = gc->proto_data;
324 JabberChat *chat;
325
326 chat = jabber_chat_find_by_id(js, id);
327
328 if(!chat)
329 return NULL;
330
331 return g_strdup_printf("%s@%s/%s", chat->room, chat->server, who);
332 }
333
334 static void jabber_chat_room_configure_x_data_cb(JabberStream *js, xmlnode *result, gpointer data)
335 {
336 JabberChat *chat = data;
337 xmlnode *query;
338 JabberIq *iq;
339 char *to = g_strdup_printf("%s@%s", chat->room, chat->server);
340
341 iq = jabber_iq_new_query(js, JABBER_IQ_SET, "http://jabber.org/protocol/muc#owner");
342 xmlnode_set_attrib(iq->node, "to", to);
343 g_free(to);
344
345 query = xmlnode_get_child(iq->node, "query");
346
347 xmlnode_insert_child(query, result);
348
349 jabber_iq_send(iq);
350 }
351
352 static void jabber_chat_room_configure_cb(JabberStream *js, xmlnode *packet, gpointer data)
353 {
354 xmlnode *query, *x;
355 const char *type = xmlnode_get_attrib(packet, "type");
356 const char *from = xmlnode_get_attrib(packet, "from");
357 char *msg;
358 JabberChat *chat;
359 JabberID *jid;
360
361 if(!type || !from)
362 return;
363
364
365
366 if(!strcmp(type, "result")) {
367 jid = jabber_id_new(from);
368
369 if(!jid)
370 return;
371
372 chat = jabber_chat_find(js, jid->node, jid->domain);
373 jabber_id_free(jid);
374
375 if(!chat)
376 return;
377
378 if(!(query = xmlnode_get_child(packet, "query")))
379 return;
380
381 for(x = xmlnode_get_child(query, "x"); x; x = xmlnode_get_next_twin(x)) {
382 const char *xmlns;
383 if(!(xmlns = xmlnode_get_namespace(x)))
384 continue;
385
386 if(!strcmp(xmlns, "jabber:x:data")) {
387 chat->config_dialog_type = GAIM_REQUEST_FIELDS;
388 chat->config_dialog_handle = jabber_x_data_request(js, x, jabber_chat_room_configure_x_data_cb, chat);
389 return;
390 }
391 }
392 } else if(!strcmp(type, "error")) {
393 char *msg = jabber_parse_error(js, packet);
394
395 gaim_notify_error(js->gc, _("Configuration error"), _("Configuration error"), msg);
396
397 if(msg)
398 g_free(msg);
399 return;
400 }
401
402 msg = g_strdup_printf("Unable to configure room %s", from);
403
404 gaim_notify_info(js->gc, _("Unable to configure"), _("Unable to configure"), msg);
405 g_free(msg);
406
407 }
408
409 void jabber_chat_request_room_configure(JabberChat *chat) {
410 JabberIq *iq;
411 char *room_jid;
412
413 if(!chat)
414 return;
415
416 chat->config_dialog_handle = NULL;
417
418 if(!chat->muc) {
419 gaim_notify_error(chat->js->gc, _("Room Configuration Error"), _("Room Configuration Error"),
420 _("This room is not capable of being configured"));
421 return;
422 }
423
424 iq = jabber_iq_new_query(chat->js, JABBER_IQ_GET,
425 "http://jabber.org/protocol/muc#owner");
426 room_jid = g_strdup_printf("%s@%s", chat->room, chat->server);
427
428 xmlnode_set_attrib(iq->node, "to", room_jid);
429
430 jabber_iq_set_callback(iq, jabber_chat_room_configure_cb, NULL);
431
432 jabber_iq_send(iq);
433
434 g_free(room_jid);
435 }
436
437 void jabber_chat_create_instant_room(JabberChat *chat) {
438 JabberIq *iq;
439 xmlnode *query, *x;
440 char *room_jid;
441
442 if(!chat)
443 return;
444
445 chat->config_dialog_handle = NULL;
446
447 iq = jabber_iq_new_query(chat->js, JABBER_IQ_SET,
448 "http://jabber.org/protocol/muc#owner");
449 query = xmlnode_get_child(iq->node, "query");
450 x = xmlnode_new_child(query, "x");
451 room_jid = g_strdup_printf("%s@%s", chat->room, chat->server);
452
453 xmlnode_set_attrib(iq->node, "to", room_jid);
454 xmlnode_set_namespace(x, "jabber:x:data");
455 xmlnode_set_attrib(x, "type", "submit");
456
457 jabber_iq_send(iq);
458
459 g_free(room_jid);
460 }
461
462 static void jabber_chat_register_x_data_result_cb(JabberStream *js, xmlnode *packet, gpointer data)
463 {
464 const char *type = xmlnode_get_attrib(packet, "type");
465
466 if(type && !strcmp(type, "error")) {
467 char *msg = jabber_parse_error(js, packet);
468
469 gaim_notify_error(js->gc, _("Registration error"), _("Registration error"), msg);
470
471 if(msg)
472 g_free(msg);
473 return;
474 }
475 }
476
477 static void jabber_chat_register_x_data_cb(JabberStream *js, xmlnode *result, gpointer data)
478 {
479 JabberChat *chat = data;
480 xmlnode *query;
481 JabberIq *iq;
482 char *to = g_strdup_printf("%s@%s", chat->room, chat->server);
483
484 iq = jabber_iq_new_query(js, JABBER_IQ_SET, "jabber:iq:register");
485 xmlnode_set_attrib(iq->node, "to", to);
486 g_free(to);
487
488 query = xmlnode_get_child(iq->node, "query");
489
490 xmlnode_insert_child(query, result);
491
492 jabber_iq_set_callback(iq, jabber_chat_register_x_data_result_cb, NULL);
493
494 jabber_iq_send(iq);
495 }
496
497 static void jabber_chat_register_cb(JabberStream *js, xmlnode *packet, gpointer data)
498 {
499 xmlnode *query, *x;
500 const char *type = xmlnode_get_attrib(packet, "type");
501 const char *from = xmlnode_get_attrib(packet, "from");
502 char *msg;
503 JabberChat *chat;
504 JabberID *jid;
505
506 if(!type || !from)
507 return;
508
509 if(!strcmp(type, "result")) {
510 jid = jabber_id_new(from);
511
512 if(!jid)
513 return;
514
515 chat = jabber_chat_find(js, jid->node, jid->domain);
516 jabber_id_free(jid);
517
518 if(!chat)
519 return;
520
521 if(!(query = xmlnode_get_child(packet, "query")))
522 return;
523
524 for(x = xmlnode_get_child(query, "x"); x; x = xmlnode_get_next_twin(x)) {
525 const char *xmlns;
526
527 if(!(xmlns = xmlnode_get_namespace(x)))
528 continue;
529
530 if(!strcmp(xmlns, "jabber:x:data")) {
531 jabber_x_data_request(js, x, jabber_chat_register_x_data_cb, chat);
532 return;
533 }
534 }
535 } else if(!strcmp(type, "error")) {
536 char *msg = jabber_parse_error(js, packet);
537
538 gaim_notify_error(js->gc, _("Registration error"), _("Registration error"), msg);
539
540 if(msg)
541 g_free(msg);
542 return;
543 }
544
545 msg = g_strdup_printf("Unable to configure room %s", from);
546
547 gaim_notify_info(js->gc, _("Unable to configure"), _("Unable to configure"), msg);
548 g_free(msg);
549
550 }
551
552 void jabber_chat_register(JabberChat *chat)
553 {
554 JabberIq *iq;
555 char *room_jid;
556
557 if(!chat)
558 return;
559
560 room_jid = g_strdup_printf("%s@%s", chat->room, chat->server);
561
562 iq = jabber_iq_new_query(chat->js, JABBER_IQ_GET, "jabber:iq:register");
563 xmlnode_set_attrib(iq->node, "to", room_jid);
564 g_free(room_jid);
565
566 jabber_iq_set_callback(iq, jabber_chat_register_cb, NULL);
567
568 jabber_iq_send(iq);
569 }
570
571 /* merge this with the function below when we get everyone on the same page wrt /commands */
572 void jabber_chat_change_topic(JabberChat *chat, const char *topic)
573 {
574 if(topic && *topic) {
575 JabberMessage *jm;
576 jm = g_new0(JabberMessage, 1);
577 jm->js = chat->js;
578 jm->type = JABBER_MESSAGE_GROUPCHAT;
579 jm->subject = gaim_markup_strip_html(topic);
580 jm->to = g_strdup_printf("%s@%s", chat->room, chat->server);
581 jabber_message_send(jm);
582 jabber_message_free(jm);
583 } else {
584 const char *cur = gaim_conv_chat_get_topic(GAIM_CONV_CHAT(chat->conv));
585 char *buf, *tmp, *tmp2;
586
587 if(cur) {
588 tmp = g_markup_escape_text(cur, -1);
589 tmp2 = gaim_markup_linkify(tmp);
590 buf = g_strdup_printf(_("current topic is: %s"), tmp2);
591 g_free(tmp);
592 g_free(tmp2);
593 } else
594 buf = g_strdup(_("No topic is set"));
595 gaim_conv_chat_write(GAIM_CONV_CHAT(chat->conv), "", buf,
596 GAIM_MESSAGE_SYSTEM | GAIM_MESSAGE_NO_LOG, time(NULL));
597 g_free(buf);
598 }
599
600 }
601
602 void jabber_chat_set_topic(GaimConnection *gc, int id, const char *topic)
603 {
604 JabberStream *js = gc->proto_data;
605 JabberChat *chat = jabber_chat_find_by_id(js, id);
606
607 if(!chat)
608 return;
609
610 jabber_chat_change_topic(chat, topic);
611 }
612
613
614 void jabber_chat_change_nick(JabberChat *chat, const char *nick)
615 {
616 xmlnode *presence;
617 char *full_jid;
618 GaimPresence *gpresence;
619 GaimStatus *status;
620 JabberBuddyState state;
621 const char *msg;
622 int priority;
623
624 if(!chat->muc) {
625 gaim_conv_chat_write(GAIM_CONV_CHAT(chat->conv), "",
626 _("Nick changing not supported in non-MUC chatrooms"),
627 GAIM_MESSAGE_SYSTEM, time(NULL));
628 return;
629 }
630
631 gpresence = gaim_account_get_presence(chat->js->gc->account);
632 status = gaim_presence_get_active_status(gpresence);
633
634 gaim_status_to_jabber(status, &state, &msg, &priority);
635
636 presence = jabber_presence_create(state, msg, priority);
637 full_jid = g_strdup_printf("%s@%s/%s", chat->room, chat->server, nick);
638 xmlnode_set_attrib(presence, "to", full_jid);
639 g_free(full_jid);
640
641 jabber_send(chat->js, presence);
642 xmlnode_free(presence);
643 }
644
645 void jabber_chat_part(JabberChat *chat, const char *msg)
646 {
647 char *room_jid;
648 xmlnode *presence;
649
650 room_jid = g_strdup_printf("%s@%s/%s", chat->room, chat->server,
651 chat->handle);
652 presence = xmlnode_new("presence");
653 xmlnode_set_attrib(presence, "to", room_jid);
654 xmlnode_set_attrib(presence, "type", "unavailable");
655 if(msg) {
656 xmlnode *status = xmlnode_new_child(presence, "status");
657 xmlnode_insert_data(status, msg, -1);
658 }
659 jabber_send(chat->js, presence);
660 xmlnode_free(presence);
661 g_free(room_jid);
662 }
663
664 static void roomlist_disco_result_cb(JabberStream *js, xmlnode *packet, gpointer data)
665 {
666 xmlnode *query;
667 xmlnode *item;
668 const char *type;
669
670 if(!js->roomlist)
671 return;
672
673 if(!(type = xmlnode_get_attrib(packet, "type")) || strcmp(type, "result")) {
674 char *err = jabber_parse_error(js,packet);
675 gaim_notify_error(js->gc, _("Error"),
676 _("Error retrieving room list"), err);
677 gaim_roomlist_set_in_progress(js->roomlist, FALSE);
678 gaim_roomlist_unref(js->roomlist);
679 js->roomlist = NULL;
680 g_free(err);
681 return;
682 }
683
684 if(!(query = xmlnode_get_child(packet, "query"))) {
685 char *err = jabber_parse_error(js, packet);
686 gaim_notify_error(js->gc, _("Error"),
687 _("Error retrieving room list"), err);
688 gaim_roomlist_set_in_progress(js->roomlist, FALSE);
689 gaim_roomlist_unref(js->roomlist);
690 js->roomlist = NULL;
691 g_free(err);
692 return;
693 }
694
695 for(item = xmlnode_get_child(query, "item"); item;
696 item = xmlnode_get_next_twin(item)) {
697 const char *name;
698 GaimRoomlistRoom *room;
699 JabberID *jid;
700
701 if(!(jid = jabber_id_new(xmlnode_get_attrib(item, "jid"))))
702 continue;
703 name = xmlnode_get_attrib(item, "name");
704
705
706 room = gaim_roomlist_room_new(GAIM_ROOMLIST_ROOMTYPE_ROOM, jid->node, NULL);
707 gaim_roomlist_room_add_field(js->roomlist, room, jid->node);
708 gaim_roomlist_room_add_field(js->roomlist, room, jid->domain);
709 gaim_roomlist_room_add_field(js->roomlist, room, name ? name : "");
710 gaim_roomlist_room_add(js->roomlist, room);
711
712 jabber_id_free(jid);
713 }
714 gaim_roomlist_set_in_progress(js->roomlist, FALSE);
715 gaim_roomlist_unref(js->roomlist);
716 js->roomlist = NULL;
717 }
718
719 static void roomlist_cancel_cb(JabberStream *js, const char *server) {
720 if(js->roomlist) {
721 gaim_roomlist_set_in_progress(js->roomlist, FALSE);
722 gaim_roomlist_unref(js->roomlist);
723 js->roomlist = NULL;
724 }
725 }
726
727 static void roomlist_ok_cb(JabberStream *js, const char *server)
728 {
729 JabberIq *iq;
730
731 if(!js->roomlist)
732 return;
733
734 if(!server || !*server) {
735 gaim_notify_error(js->gc, _("Invalid Server"), _("Invalid Server"), NULL);
736 return;
737 }
738
739 gaim_roomlist_set_in_progress(js->roomlist, TRUE);
740
741 iq = jabber_iq_new_query(js, JABBER_IQ_GET, "http://jabber.org/protocol/disco#items");
742
743 xmlnode_set_attrib(iq->node, "to", server);
744
745 jabber_iq_set_callback(iq, roomlist_disco_result_cb, NULL);
746
747 jabber_iq_send(iq);
748 }
749
750 GaimRoomlist *jabber_roomlist_get_list(GaimConnection *gc)
751 {
752 JabberStream *js = gc->proto_data;
753 GList *fields = NULL;
754 GaimRoomlistField *f;
755
756 if(js->roomlist)
757 gaim_roomlist_unref(js->roomlist);
758
759 js->roomlist = gaim_roomlist_new(gaim_connection_get_account(js->gc));
760
761 f = gaim_roomlist_field_new(GAIM_ROOMLIST_FIELD_STRING, "", "room", TRUE);
762 fields = g_list_append(fields, f);
763
764 f = gaim_roomlist_field_new(GAIM_ROOMLIST_FIELD_STRING, "", "server", TRUE);
765 fields = g_list_append(fields, f);
766
767 f = gaim_roomlist_field_new(GAIM_ROOMLIST_FIELD_STRING, _("Description"), "description", FALSE);
768 fields = g_list_append(fields, f);
769
770 gaim_roomlist_set_fields(js->roomlist, fields);
771
772
773 gaim_request_input(gc, _("Enter a Conference Server"), _("Enter a Conference Server"),
774 _("Select a conference server to query"),
775 js->chat_servers ? js->chat_servers->data : "conference.jabber.org",
776 FALSE, FALSE, NULL,
777 _("Find Rooms"), GAIM_CALLBACK(roomlist_ok_cb),
778 _("Cancel"), GAIM_CALLBACK(roomlist_cancel_cb), js);
779
780 return js->roomlist;
781 }
782
783 void jabber_roomlist_cancel(GaimRoomlist *list)
784 {
785 GaimConnection *gc;
786 JabberStream *js;
787
788 gc = gaim_account_get_connection(list->account);
789 js = gc->proto_data;
790
791 gaim_roomlist_set_in_progress(list, FALSE);
792
793 if (js->roomlist == list) {
794 js->roomlist = NULL;
795 gaim_roomlist_unref(list);
796 }
797 }
798
799 void jabber_chat_member_free(JabberChatMember *jcm)
800 {
801 g_free(jcm->handle);
802 g_free(jcm->jid);
803 g_free(jcm);
804 }
805
806 void jabber_chat_track_handle(JabberChat *chat, const char *handle,
807 const char *jid, const char *affiliation, const char *role)
808 {
809 JabberChatMember *jcm = g_new0(JabberChatMember, 1);
810
811 jcm->handle = g_strdup(handle);
812 jcm->jid = g_strdup(jid);
813
814 g_hash_table_replace(chat->members, jcm->handle, jcm);
815
816 /* XXX: keep track of role and affiliation */
817 }
818
819 void jabber_chat_remove_handle(JabberChat *chat, const char *handle)
820 {
821 g_hash_table_remove(chat->members, handle);
822 }
823
824 gboolean jabber_chat_ban_user(JabberChat *chat, const char *who, const char *why)
825 {
826 JabberIq *iq;
827 JabberChatMember *jcm = g_hash_table_lookup(chat->members, who);
828 char *to;
829 xmlnode *query, *item, *reason;
830
831 if(!jcm || !jcm->jid)
832 return FALSE;
833
834 iq = jabber_iq_new_query(chat->js, JABBER_IQ_SET,
835 "http://jabber.org/protocol/muc#admin");
836
837 to = g_strdup_printf("%s@%s", chat->room, chat->server);
838 xmlnode_set_attrib(iq->node, "to", to);
839 g_free(to);
840
841 query = xmlnode_get_child(iq->node, "query");
842 item = xmlnode_new_child(query, "item");
843 xmlnode_set_attrib(item, "jid", jcm->jid);
844 xmlnode_set_attrib(item, "affiliation", "outcast");
845 if(why) {
846 reason = xmlnode_new_child(item, "reason");
847 xmlnode_insert_data(reason, why, -1);
848 }
849
850 jabber_iq_send(iq);
851
852 return TRUE;
853 }
854
855 gboolean jabber_chat_affiliate_user(JabberChat *chat, const char *who, const char *affiliation)
856 {
857 char *to;
858 JabberIq *iq;
859 xmlnode *query, *item;
860 JabberChatMember *jcm;
861
862 jcm = g_hash_table_lookup(chat->members, who);
863
864 if (!jcm || !jcm->jid)
865 return FALSE;
866
867 iq = jabber_iq_new_query(chat->js, JABBER_IQ_SET,
868 "http://jabber.org/protocol/muc#admin");
869
870 to = g_strdup_printf("%s@%s", chat->room, chat->server);
871 xmlnode_set_attrib(iq->node, "to", to);
872 g_free(to);
873
874 query = xmlnode_get_child(iq->node, "query");
875 item = xmlnode_new_child(query, "item");
876 xmlnode_set_attrib(item, "jid", jcm->jid);
877 xmlnode_set_attrib(item, "affiliation", affiliation);
878
879 jabber_iq_send(iq);
880
881 return TRUE;
882 }
883
884 gboolean jabber_chat_role_user(JabberChat *chat, const char *who, const char *role)
885 {
886 char *to;
887 JabberIq *iq;
888 xmlnode *query, *item;
889 JabberChatMember *jcm;
890
891 jcm = g_hash_table_lookup(chat->members, who);
892
893 if (!jcm || !jcm->handle)
894 return FALSE;
895
896 iq = jabber_iq_new_query(chat->js, JABBER_IQ_SET,
897 "http://jabber.org/protocol/muc#admin");
898
899 to = g_strdup_printf("%s@%s", chat->room, chat->server);
900 xmlnode_set_attrib(iq->node, "to", to);
901 g_free(to);
902
903 query = xmlnode_get_child(iq->node, "query");
904 item = xmlnode_new_child(query, "item");
905 xmlnode_set_attrib(item, "nick", jcm->handle);
906 xmlnode_set_attrib(item, "role", role);
907
908 jabber_iq_send(iq);
909
910 return TRUE;
911 }
912
913 gboolean jabber_chat_kick_user(JabberChat *chat, const char *who, const char *why)
914 {
915 JabberIq *iq;
916 JabberChatMember *jcm = g_hash_table_lookup(chat->members, who);
917 char *to;
918 xmlnode *query, *item, *reason;
919
920 if(!jcm || !jcm->jid)
921 return FALSE;
922
923 iq = jabber_iq_new_query(chat->js, JABBER_IQ_SET,
924 "http://jabber.org/protocol/muc#admin");
925
926 to = g_strdup_printf("%s@%s", chat->room, chat->server);
927 xmlnode_set_attrib(iq->node, "to", to);
928 g_free(to);
929
930 query = xmlnode_get_child(iq->node, "query");
931 item = xmlnode_new_child(query, "item");
932 xmlnode_set_attrib(item, "jid", jcm->jid);
933 xmlnode_set_attrib(item, "role", "none");
934 if(why) {
935 reason = xmlnode_new_child(item, "reason");
936 xmlnode_insert_data(reason, why, -1);
937 }
938
939 jabber_iq_send(iq);
940
941 return TRUE;
942 }
943
944 static void jabber_chat_disco_traffic_cb(JabberStream *js, xmlnode *packet, gpointer data)
945 {
946 JabberChat *chat;
947 xmlnode *query, *x;
948 int id = GPOINTER_TO_INT(data);
949
950 if(!(chat = jabber_chat_find_by_id(js, id)))
951 return;
952
953 /* defaults, in case the conference server doesn't
954 * support this request */
955 chat->xhtml = TRUE;
956
957 if(xmlnode_get_child(packet, "error")) {
958 return;
959 }
960
961 if(!(query = xmlnode_get_child(packet, "query")))
962 return;
963
964 chat->xhtml = FALSE;
965
966 for(x = xmlnode_get_child(query, "feature"); x; x = xmlnode_get_next_twin(x)) {
967 const char *var = xmlnode_get_attrib(x, "var");
968
969 if(var && !strcmp(var, "http://jabber.org/protocol/xhtml-im")) {
970 chat->xhtml = TRUE;
971 }
972 }
973 }
974
975 void jabber_chat_disco_traffic(JabberChat *chat)
976 {
977 JabberIq *iq;
978 xmlnode *query;
979 char *room_jid;
980
981 room_jid = g_strdup_printf("%s@%s", chat->room, chat->server);
982
983 iq = jabber_iq_new_query(chat->js, JABBER_IQ_GET,
984 "http://jabber.org/protocol/disco#info");
985
986 xmlnode_set_attrib(iq->node, "to", room_jid);
987
988 query = xmlnode_get_child(iq->node, "query");
989
990 xmlnode_set_attrib(query, "node", "http://jabber.org/protocol/muc#traffic");
991
992 jabber_iq_set_callback(iq, jabber_chat_disco_traffic_cb, GINT_TO_POINTER(chat->id));
993
994 jabber_iq_send(iq);
995
996 g_free(room_jid);
997 }
998
999
1000