14192
|
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;
|
14463
|
208 char *msg;
|
14192
|
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);
|
14463
|
268 g_free(msg);
|
14192
|
269
|
|
270 x = xmlnode_new_child(presence, "x");
|
|
271 xmlnode_set_namespace(x, "http://jabber.org/protocol/muc");
|
|
272
|
|
273 if(passwd && *passwd) {
|
|
274 xmlnode *password = xmlnode_new_child(x, "password");
|
|
275 xmlnode_insert_data(password, passwd, -1);
|
|
276 }
|
|
277
|
|
278 jabber_send(js, presence);
|
|
279 xmlnode_free(presence);
|
|
280 }
|
|
281
|
|
282 void jabber_chat_leave(GaimConnection *gc, int id)
|
|
283 {
|
|
284 JabberStream *js = gc->proto_data;
|
|
285 JabberChat *chat = jabber_chat_find_by_id(js, id);
|
|
286
|
|
287
|
|
288 if(!chat)
|
|
289 return;
|
|
290
|
|
291 jabber_chat_part(chat, NULL);
|
|
292
|
|
293 chat->conv = NULL;
|
|
294 }
|
|
295
|
|
296 void jabber_chat_destroy(JabberChat *chat)
|
|
297 {
|
|
298 JabberStream *js = chat->js;
|
|
299 char *room_jid = g_strdup_printf("%s@%s", chat->room, chat->server);
|
|
300
|
|
301 g_hash_table_remove(js->chats, jabber_normalize(NULL, room_jid));
|
|
302 g_free(room_jid);
|
|
303 }
|
|
304
|
|
305 void jabber_chat_free(JabberChat *chat)
|
|
306 {
|
|
307 if(chat->config_dialog_handle)
|
|
308 gaim_request_close(chat->config_dialog_type, chat->config_dialog_handle);
|
|
309
|
|
310 g_free(chat->room);
|
|
311 g_free(chat->server);
|
|
312 g_free(chat->handle);
|
|
313 g_hash_table_destroy(chat->members);
|
|
314 g_free(chat);
|
|
315 }
|
|
316
|
|
317 gboolean jabber_chat_find_buddy(GaimConversation *conv, const char *name)
|
|
318 {
|
|
319 return gaim_conv_chat_find_user(GAIM_CONV_CHAT(conv), name);
|
|
320 }
|
|
321
|
|
322 char *jabber_chat_buddy_real_name(GaimConnection *gc, int id, const char *who)
|
|
323 {
|
|
324 JabberStream *js = gc->proto_data;
|
|
325 JabberChat *chat;
|
|
326
|
|
327 chat = jabber_chat_find_by_id(js, id);
|
|
328
|
|
329 if(!chat)
|
|
330 return NULL;
|
|
331
|
|
332 return g_strdup_printf("%s@%s/%s", chat->room, chat->server, who);
|
|
333 }
|
|
334
|
|
335 static void jabber_chat_room_configure_x_data_cb(JabberStream *js, xmlnode *result, gpointer data)
|
|
336 {
|
|
337 JabberChat *chat = data;
|
|
338 xmlnode *query;
|
|
339 JabberIq *iq;
|
|
340 char *to = g_strdup_printf("%s@%s", chat->room, chat->server);
|
|
341
|
|
342 iq = jabber_iq_new_query(js, JABBER_IQ_SET, "http://jabber.org/protocol/muc#owner");
|
|
343 xmlnode_set_attrib(iq->node, "to", to);
|
|
344 g_free(to);
|
|
345
|
|
346 query = xmlnode_get_child(iq->node, "query");
|
|
347
|
|
348 xmlnode_insert_child(query, result);
|
|
349
|
|
350 jabber_iq_send(iq);
|
|
351 }
|
|
352
|
|
353 static void jabber_chat_room_configure_cb(JabberStream *js, xmlnode *packet, gpointer data)
|
|
354 {
|
|
355 xmlnode *query, *x;
|
|
356 const char *type = xmlnode_get_attrib(packet, "type");
|
|
357 const char *from = xmlnode_get_attrib(packet, "from");
|
|
358 char *msg;
|
|
359 JabberChat *chat;
|
|
360 JabberID *jid;
|
|
361
|
|
362 if(!type || !from)
|
|
363 return;
|
|
364
|
|
365
|
|
366
|
|
367 if(!strcmp(type, "result")) {
|
|
368 jid = jabber_id_new(from);
|
|
369
|
|
370 if(!jid)
|
|
371 return;
|
|
372
|
|
373 chat = jabber_chat_find(js, jid->node, jid->domain);
|
|
374 jabber_id_free(jid);
|
|
375
|
|
376 if(!chat)
|
|
377 return;
|
|
378
|
|
379 if(!(query = xmlnode_get_child(packet, "query")))
|
|
380 return;
|
|
381
|
|
382 for(x = xmlnode_get_child(query, "x"); x; x = xmlnode_get_next_twin(x)) {
|
|
383 const char *xmlns;
|
|
384 if(!(xmlns = xmlnode_get_namespace(x)))
|
|
385 continue;
|
|
386
|
|
387 if(!strcmp(xmlns, "jabber:x:data")) {
|
|
388 chat->config_dialog_type = GAIM_REQUEST_FIELDS;
|
|
389 chat->config_dialog_handle = jabber_x_data_request(js, x, jabber_chat_room_configure_x_data_cb, chat);
|
|
390 return;
|
|
391 }
|
|
392 }
|
|
393 } else if(!strcmp(type, "error")) {
|
|
394 char *msg = jabber_parse_error(js, packet);
|
|
395
|
|
396 gaim_notify_error(js->gc, _("Configuration error"), _("Configuration error"), msg);
|
|
397
|
|
398 if(msg)
|
|
399 g_free(msg);
|
|
400 return;
|
|
401 }
|
|
402
|
|
403 msg = g_strdup_printf("Unable to configure room %s", from);
|
|
404
|
|
405 gaim_notify_info(js->gc, _("Unable to configure"), _("Unable to configure"), msg);
|
|
406 g_free(msg);
|
|
407
|
|
408 }
|
|
409
|
|
410 void jabber_chat_request_room_configure(JabberChat *chat) {
|
|
411 JabberIq *iq;
|
|
412 char *room_jid;
|
|
413
|
|
414 if(!chat)
|
|
415 return;
|
|
416
|
|
417 chat->config_dialog_handle = NULL;
|
|
418
|
|
419 if(!chat->muc) {
|
|
420 gaim_notify_error(chat->js->gc, _("Room Configuration Error"), _("Room Configuration Error"),
|
|
421 _("This room is not capable of being configured"));
|
|
422 return;
|
|
423 }
|
|
424
|
|
425 iq = jabber_iq_new_query(chat->js, JABBER_IQ_GET,
|
|
426 "http://jabber.org/protocol/muc#owner");
|
|
427 room_jid = g_strdup_printf("%s@%s", chat->room, chat->server);
|
|
428
|
|
429 xmlnode_set_attrib(iq->node, "to", room_jid);
|
|
430
|
|
431 jabber_iq_set_callback(iq, jabber_chat_room_configure_cb, NULL);
|
|
432
|
|
433 jabber_iq_send(iq);
|
|
434
|
|
435 g_free(room_jid);
|
|
436 }
|
|
437
|
|
438 void jabber_chat_create_instant_room(JabberChat *chat) {
|
|
439 JabberIq *iq;
|
|
440 xmlnode *query, *x;
|
|
441 char *room_jid;
|
|
442
|
|
443 if(!chat)
|
|
444 return;
|
|
445
|
|
446 chat->config_dialog_handle = NULL;
|
|
447
|
|
448 iq = jabber_iq_new_query(chat->js, JABBER_IQ_SET,
|
|
449 "http://jabber.org/protocol/muc#owner");
|
|
450 query = xmlnode_get_child(iq->node, "query");
|
|
451 x = xmlnode_new_child(query, "x");
|
|
452 room_jid = g_strdup_printf("%s@%s", chat->room, chat->server);
|
|
453
|
|
454 xmlnode_set_attrib(iq->node, "to", room_jid);
|
|
455 xmlnode_set_namespace(x, "jabber:x:data");
|
|
456 xmlnode_set_attrib(x, "type", "submit");
|
|
457
|
|
458 jabber_iq_send(iq);
|
|
459
|
|
460 g_free(room_jid);
|
|
461 }
|
|
462
|
|
463 static void jabber_chat_register_x_data_result_cb(JabberStream *js, xmlnode *packet, gpointer data)
|
|
464 {
|
|
465 const char *type = xmlnode_get_attrib(packet, "type");
|
|
466
|
|
467 if(type && !strcmp(type, "error")) {
|
|
468 char *msg = jabber_parse_error(js, packet);
|
|
469
|
|
470 gaim_notify_error(js->gc, _("Registration error"), _("Registration error"), msg);
|
|
471
|
|
472 if(msg)
|
|
473 g_free(msg);
|
|
474 return;
|
|
475 }
|
|
476 }
|
|
477
|
|
478 static void jabber_chat_register_x_data_cb(JabberStream *js, xmlnode *result, gpointer data)
|
|
479 {
|
|
480 JabberChat *chat = data;
|
|
481 xmlnode *query;
|
|
482 JabberIq *iq;
|
|
483 char *to = g_strdup_printf("%s@%s", chat->room, chat->server);
|
|
484
|
|
485 iq = jabber_iq_new_query(js, JABBER_IQ_SET, "jabber:iq:register");
|
|
486 xmlnode_set_attrib(iq->node, "to", to);
|
|
487 g_free(to);
|
|
488
|
|
489 query = xmlnode_get_child(iq->node, "query");
|
|
490
|
|
491 xmlnode_insert_child(query, result);
|
|
492
|
|
493 jabber_iq_set_callback(iq, jabber_chat_register_x_data_result_cb, NULL);
|
|
494
|
|
495 jabber_iq_send(iq);
|
|
496 }
|
|
497
|
|
498 static void jabber_chat_register_cb(JabberStream *js, xmlnode *packet, gpointer data)
|
|
499 {
|
|
500 xmlnode *query, *x;
|
|
501 const char *type = xmlnode_get_attrib(packet, "type");
|
|
502 const char *from = xmlnode_get_attrib(packet, "from");
|
|
503 char *msg;
|
|
504 JabberChat *chat;
|
|
505 JabberID *jid;
|
|
506
|
|
507 if(!type || !from)
|
|
508 return;
|
|
509
|
|
510 if(!strcmp(type, "result")) {
|
|
511 jid = jabber_id_new(from);
|
|
512
|
|
513 if(!jid)
|
|
514 return;
|
|
515
|
|
516 chat = jabber_chat_find(js, jid->node, jid->domain);
|
|
517 jabber_id_free(jid);
|
|
518
|
|
519 if(!chat)
|
|
520 return;
|
|
521
|
|
522 if(!(query = xmlnode_get_child(packet, "query")))
|
|
523 return;
|
|
524
|
|
525 for(x = xmlnode_get_child(query, "x"); x; x = xmlnode_get_next_twin(x)) {
|
|
526 const char *xmlns;
|
|
527
|
|
528 if(!(xmlns = xmlnode_get_namespace(x)))
|
|
529 continue;
|
|
530
|
|
531 if(!strcmp(xmlns, "jabber:x:data")) {
|
|
532 jabber_x_data_request(js, x, jabber_chat_register_x_data_cb, chat);
|
|
533 return;
|
|
534 }
|
|
535 }
|
|
536 } else if(!strcmp(type, "error")) {
|
|
537 char *msg = jabber_parse_error(js, packet);
|
|
538
|
|
539 gaim_notify_error(js->gc, _("Registration error"), _("Registration error"), msg);
|
|
540
|
|
541 if(msg)
|
|
542 g_free(msg);
|
|
543 return;
|
|
544 }
|
|
545
|
|
546 msg = g_strdup_printf("Unable to configure room %s", from);
|
|
547
|
|
548 gaim_notify_info(js->gc, _("Unable to configure"), _("Unable to configure"), msg);
|
|
549 g_free(msg);
|
|
550
|
|
551 }
|
|
552
|
|
553 void jabber_chat_register(JabberChat *chat)
|
|
554 {
|
|
555 JabberIq *iq;
|
|
556 char *room_jid;
|
|
557
|
|
558 if(!chat)
|
|
559 return;
|
|
560
|
|
561 room_jid = g_strdup_printf("%s@%s", chat->room, chat->server);
|
|
562
|
|
563 iq = jabber_iq_new_query(chat->js, JABBER_IQ_GET, "jabber:iq:register");
|
|
564 xmlnode_set_attrib(iq->node, "to", room_jid);
|
|
565 g_free(room_jid);
|
|
566
|
|
567 jabber_iq_set_callback(iq, jabber_chat_register_cb, NULL);
|
|
568
|
|
569 jabber_iq_send(iq);
|
|
570 }
|
|
571
|
|
572 /* merge this with the function below when we get everyone on the same page wrt /commands */
|
|
573 void jabber_chat_change_topic(JabberChat *chat, const char *topic)
|
|
574 {
|
|
575 if(topic && *topic) {
|
|
576 JabberMessage *jm;
|
|
577 jm = g_new0(JabberMessage, 1);
|
|
578 jm->js = chat->js;
|
|
579 jm->type = JABBER_MESSAGE_GROUPCHAT;
|
|
580 jm->subject = gaim_markup_strip_html(topic);
|
|
581 jm->to = g_strdup_printf("%s@%s", chat->room, chat->server);
|
|
582 jabber_message_send(jm);
|
|
583 jabber_message_free(jm);
|
|
584 } else {
|
|
585 const char *cur = gaim_conv_chat_get_topic(GAIM_CONV_CHAT(chat->conv));
|
|
586 char *buf, *tmp, *tmp2;
|
|
587
|
|
588 if(cur) {
|
|
589 tmp = g_markup_escape_text(cur, -1);
|
|
590 tmp2 = gaim_markup_linkify(tmp);
|
|
591 buf = g_strdup_printf(_("current topic is: %s"), tmp2);
|
|
592 g_free(tmp);
|
|
593 g_free(tmp2);
|
|
594 } else
|
|
595 buf = g_strdup(_("No topic is set"));
|
|
596 gaim_conv_chat_write(GAIM_CONV_CHAT(chat->conv), "", buf,
|
|
597 GAIM_MESSAGE_SYSTEM | GAIM_MESSAGE_NO_LOG, time(NULL));
|
|
598 g_free(buf);
|
|
599 }
|
|
600
|
|
601 }
|
|
602
|
|
603 void jabber_chat_set_topic(GaimConnection *gc, int id, const char *topic)
|
|
604 {
|
|
605 JabberStream *js = gc->proto_data;
|
|
606 JabberChat *chat = jabber_chat_find_by_id(js, id);
|
|
607
|
|
608 if(!chat)
|
|
609 return;
|
|
610
|
|
611 jabber_chat_change_topic(chat, topic);
|
|
612 }
|
|
613
|
|
614
|
|
615 void jabber_chat_change_nick(JabberChat *chat, const char *nick)
|
|
616 {
|
|
617 xmlnode *presence;
|
|
618 char *full_jid;
|
|
619 GaimPresence *gpresence;
|
|
620 GaimStatus *status;
|
|
621 JabberBuddyState state;
|
14463
|
622 char *msg;
|
14192
|
623 int priority;
|
|
624
|
|
625 if(!chat->muc) {
|
|
626 gaim_conv_chat_write(GAIM_CONV_CHAT(chat->conv), "",
|
|
627 _("Nick changing not supported in non-MUC chatrooms"),
|
|
628 GAIM_MESSAGE_SYSTEM, time(NULL));
|
|
629 return;
|
|
630 }
|
|
631
|
|
632 gpresence = gaim_account_get_presence(chat->js->gc->account);
|
|
633 status = gaim_presence_get_active_status(gpresence);
|
|
634
|
|
635 gaim_status_to_jabber(status, &state, &msg, &priority);
|
|
636
|
|
637 presence = jabber_presence_create(state, msg, priority);
|
|
638 full_jid = g_strdup_printf("%s@%s/%s", chat->room, chat->server, nick);
|
|
639 xmlnode_set_attrib(presence, "to", full_jid);
|
|
640 g_free(full_jid);
|
14463
|
641 g_free(msg);
|
14192
|
642
|
|
643 jabber_send(chat->js, presence);
|
|
644 xmlnode_free(presence);
|
|
645 }
|
|
646
|
|
647 void jabber_chat_part(JabberChat *chat, const char *msg)
|
|
648 {
|
|
649 char *room_jid;
|
|
650 xmlnode *presence;
|
|
651
|
|
652 room_jid = g_strdup_printf("%s@%s/%s", chat->room, chat->server,
|
|
653 chat->handle);
|
|
654 presence = xmlnode_new("presence");
|
|
655 xmlnode_set_attrib(presence, "to", room_jid);
|
|
656 xmlnode_set_attrib(presence, "type", "unavailable");
|
|
657 if(msg) {
|
|
658 xmlnode *status = xmlnode_new_child(presence, "status");
|
|
659 xmlnode_insert_data(status, msg, -1);
|
|
660 }
|
|
661 jabber_send(chat->js, presence);
|
|
662 xmlnode_free(presence);
|
|
663 g_free(room_jid);
|
|
664 }
|
|
665
|
|
666 static void roomlist_disco_result_cb(JabberStream *js, xmlnode *packet, gpointer data)
|
|
667 {
|
|
668 xmlnode *query;
|
|
669 xmlnode *item;
|
|
670 const char *type;
|
|
671
|
|
672 if(!js->roomlist)
|
|
673 return;
|
|
674
|
|
675 if(!(type = xmlnode_get_attrib(packet, "type")) || strcmp(type, "result")) {
|
|
676 char *err = jabber_parse_error(js,packet);
|
|
677 gaim_notify_error(js->gc, _("Error"),
|
|
678 _("Error retrieving room list"), err);
|
|
679 gaim_roomlist_set_in_progress(js->roomlist, FALSE);
|
|
680 gaim_roomlist_unref(js->roomlist);
|
|
681 js->roomlist = NULL;
|
|
682 g_free(err);
|
|
683 return;
|
|
684 }
|
|
685
|
|
686 if(!(query = xmlnode_get_child(packet, "query"))) {
|
|
687 char *err = jabber_parse_error(js, packet);
|
|
688 gaim_notify_error(js->gc, _("Error"),
|
|
689 _("Error retrieving room list"), err);
|
|
690 gaim_roomlist_set_in_progress(js->roomlist, FALSE);
|
|
691 gaim_roomlist_unref(js->roomlist);
|
|
692 js->roomlist = NULL;
|
|
693 g_free(err);
|
|
694 return;
|
|
695 }
|
|
696
|
|
697 for(item = xmlnode_get_child(query, "item"); item;
|
|
698 item = xmlnode_get_next_twin(item)) {
|
|
699 const char *name;
|
|
700 GaimRoomlistRoom *room;
|
|
701 JabberID *jid;
|
|
702
|
|
703 if(!(jid = jabber_id_new(xmlnode_get_attrib(item, "jid"))))
|
|
704 continue;
|
|
705 name = xmlnode_get_attrib(item, "name");
|
|
706
|
|
707
|
|
708 room = gaim_roomlist_room_new(GAIM_ROOMLIST_ROOMTYPE_ROOM, jid->node, NULL);
|
|
709 gaim_roomlist_room_add_field(js->roomlist, room, jid->node);
|
|
710 gaim_roomlist_room_add_field(js->roomlist, room, jid->domain);
|
|
711 gaim_roomlist_room_add_field(js->roomlist, room, name ? name : "");
|
|
712 gaim_roomlist_room_add(js->roomlist, room);
|
|
713
|
|
714 jabber_id_free(jid);
|
|
715 }
|
|
716 gaim_roomlist_set_in_progress(js->roomlist, FALSE);
|
|
717 gaim_roomlist_unref(js->roomlist);
|
|
718 js->roomlist = NULL;
|
|
719 }
|
|
720
|
|
721 static void roomlist_cancel_cb(JabberStream *js, const char *server) {
|
|
722 if(js->roomlist) {
|
|
723 gaim_roomlist_set_in_progress(js->roomlist, FALSE);
|
|
724 gaim_roomlist_unref(js->roomlist);
|
|
725 js->roomlist = NULL;
|
|
726 }
|
|
727 }
|
|
728
|
|
729 static void roomlist_ok_cb(JabberStream *js, const char *server)
|
|
730 {
|
|
731 JabberIq *iq;
|
|
732
|
|
733 if(!js->roomlist)
|
|
734 return;
|
|
735
|
|
736 if(!server || !*server) {
|
|
737 gaim_notify_error(js->gc, _("Invalid Server"), _("Invalid Server"), NULL);
|
|
738 return;
|
|
739 }
|
|
740
|
|
741 gaim_roomlist_set_in_progress(js->roomlist, TRUE);
|
|
742
|
|
743 iq = jabber_iq_new_query(js, JABBER_IQ_GET, "http://jabber.org/protocol/disco#items");
|
|
744
|
|
745 xmlnode_set_attrib(iq->node, "to", server);
|
|
746
|
|
747 jabber_iq_set_callback(iq, roomlist_disco_result_cb, NULL);
|
|
748
|
|
749 jabber_iq_send(iq);
|
|
750 }
|
|
751
|
|
752 GaimRoomlist *jabber_roomlist_get_list(GaimConnection *gc)
|
|
753 {
|
|
754 JabberStream *js = gc->proto_data;
|
|
755 GList *fields = NULL;
|
|
756 GaimRoomlistField *f;
|
|
757
|
|
758 if(js->roomlist)
|
|
759 gaim_roomlist_unref(js->roomlist);
|
|
760
|
|
761 js->roomlist = gaim_roomlist_new(gaim_connection_get_account(js->gc));
|
|
762
|
|
763 f = gaim_roomlist_field_new(GAIM_ROOMLIST_FIELD_STRING, "", "room", TRUE);
|
|
764 fields = g_list_append(fields, f);
|
|
765
|
|
766 f = gaim_roomlist_field_new(GAIM_ROOMLIST_FIELD_STRING, "", "server", TRUE);
|
|
767 fields = g_list_append(fields, f);
|
|
768
|
|
769 f = gaim_roomlist_field_new(GAIM_ROOMLIST_FIELD_STRING, _("Description"), "description", FALSE);
|
|
770 fields = g_list_append(fields, f);
|
|
771
|
|
772 gaim_roomlist_set_fields(js->roomlist, fields);
|
|
773
|
|
774
|
|
775 gaim_request_input(gc, _("Enter a Conference Server"), _("Enter a Conference Server"),
|
|
776 _("Select a conference server to query"),
|
|
777 js->chat_servers ? js->chat_servers->data : "conference.jabber.org",
|
|
778 FALSE, FALSE, NULL,
|
|
779 _("Find Rooms"), GAIM_CALLBACK(roomlist_ok_cb),
|
|
780 _("Cancel"), GAIM_CALLBACK(roomlist_cancel_cb), js);
|
|
781
|
|
782 return js->roomlist;
|
|
783 }
|
|
784
|
|
785 void jabber_roomlist_cancel(GaimRoomlist *list)
|
|
786 {
|
|
787 GaimConnection *gc;
|
|
788 JabberStream *js;
|
|
789
|
|
790 gc = gaim_account_get_connection(list->account);
|
|
791 js = gc->proto_data;
|
|
792
|
|
793 gaim_roomlist_set_in_progress(list, FALSE);
|
|
794
|
|
795 if (js->roomlist == list) {
|
|
796 js->roomlist = NULL;
|
|
797 gaim_roomlist_unref(list);
|
|
798 }
|
|
799 }
|
|
800
|
|
801 void jabber_chat_member_free(JabberChatMember *jcm)
|
|
802 {
|
|
803 g_free(jcm->handle);
|
|
804 g_free(jcm->jid);
|
|
805 g_free(jcm);
|
|
806 }
|
|
807
|
|
808 void jabber_chat_track_handle(JabberChat *chat, const char *handle,
|
|
809 const char *jid, const char *affiliation, const char *role)
|
|
810 {
|
|
811 JabberChatMember *jcm = g_new0(JabberChatMember, 1);
|
|
812
|
|
813 jcm->handle = g_strdup(handle);
|
|
814 jcm->jid = g_strdup(jid);
|
|
815
|
|
816 g_hash_table_replace(chat->members, jcm->handle, jcm);
|
|
817
|
|
818 /* XXX: keep track of role and affiliation */
|
|
819 }
|
|
820
|
|
821 void jabber_chat_remove_handle(JabberChat *chat, const char *handle)
|
|
822 {
|
|
823 g_hash_table_remove(chat->members, handle);
|
|
824 }
|
|
825
|
|
826 gboolean jabber_chat_ban_user(JabberChat *chat, const char *who, const char *why)
|
|
827 {
|
|
828 JabberIq *iq;
|
|
829 JabberChatMember *jcm = g_hash_table_lookup(chat->members, who);
|
|
830 char *to;
|
|
831 xmlnode *query, *item, *reason;
|
|
832
|
|
833 if(!jcm || !jcm->jid)
|
|
834 return FALSE;
|
|
835
|
|
836 iq = jabber_iq_new_query(chat->js, JABBER_IQ_SET,
|
|
837 "http://jabber.org/protocol/muc#admin");
|
|
838
|
|
839 to = g_strdup_printf("%s@%s", chat->room, chat->server);
|
|
840 xmlnode_set_attrib(iq->node, "to", to);
|
|
841 g_free(to);
|
|
842
|
|
843 query = xmlnode_get_child(iq->node, "query");
|
|
844 item = xmlnode_new_child(query, "item");
|
|
845 xmlnode_set_attrib(item, "jid", jcm->jid);
|
|
846 xmlnode_set_attrib(item, "affiliation", "outcast");
|
|
847 if(why) {
|
|
848 reason = xmlnode_new_child(item, "reason");
|
|
849 xmlnode_insert_data(reason, why, -1);
|
|
850 }
|
|
851
|
|
852 jabber_iq_send(iq);
|
|
853
|
|
854 return TRUE;
|
|
855 }
|
|
856
|
|
857 gboolean jabber_chat_affiliate_user(JabberChat *chat, const char *who, const char *affiliation)
|
|
858 {
|
|
859 char *to;
|
|
860 JabberIq *iq;
|
|
861 xmlnode *query, *item;
|
|
862 JabberChatMember *jcm;
|
|
863
|
|
864 jcm = g_hash_table_lookup(chat->members, who);
|
|
865
|
|
866 if (!jcm || !jcm->jid)
|
|
867 return FALSE;
|
|
868
|
|
869 iq = jabber_iq_new_query(chat->js, JABBER_IQ_SET,
|
|
870 "http://jabber.org/protocol/muc#admin");
|
|
871
|
|
872 to = g_strdup_printf("%s@%s", chat->room, chat->server);
|
|
873 xmlnode_set_attrib(iq->node, "to", to);
|
|
874 g_free(to);
|
|
875
|
|
876 query = xmlnode_get_child(iq->node, "query");
|
|
877 item = xmlnode_new_child(query, "item");
|
|
878 xmlnode_set_attrib(item, "jid", jcm->jid);
|
|
879 xmlnode_set_attrib(item, "affiliation", affiliation);
|
|
880
|
|
881 jabber_iq_send(iq);
|
|
882
|
|
883 return TRUE;
|
|
884 }
|
|
885
|
|
886 gboolean jabber_chat_role_user(JabberChat *chat, const char *who, const char *role)
|
|
887 {
|
|
888 char *to;
|
|
889 JabberIq *iq;
|
|
890 xmlnode *query, *item;
|
|
891 JabberChatMember *jcm;
|
|
892
|
|
893 jcm = g_hash_table_lookup(chat->members, who);
|
|
894
|
|
895 if (!jcm || !jcm->handle)
|
|
896 return FALSE;
|
|
897
|
|
898 iq = jabber_iq_new_query(chat->js, JABBER_IQ_SET,
|
|
899 "http://jabber.org/protocol/muc#admin");
|
|
900
|
|
901 to = g_strdup_printf("%s@%s", chat->room, chat->server);
|
|
902 xmlnode_set_attrib(iq->node, "to", to);
|
|
903 g_free(to);
|
|
904
|
|
905 query = xmlnode_get_child(iq->node, "query");
|
|
906 item = xmlnode_new_child(query, "item");
|
|
907 xmlnode_set_attrib(item, "nick", jcm->handle);
|
|
908 xmlnode_set_attrib(item, "role", role);
|
|
909
|
|
910 jabber_iq_send(iq);
|
|
911
|
|
912 return TRUE;
|
|
913 }
|
|
914
|
|
915 gboolean jabber_chat_kick_user(JabberChat *chat, const char *who, const char *why)
|
|
916 {
|
|
917 JabberIq *iq;
|
|
918 JabberChatMember *jcm = g_hash_table_lookup(chat->members, who);
|
|
919 char *to;
|
|
920 xmlnode *query, *item, *reason;
|
|
921
|
|
922 if(!jcm || !jcm->jid)
|
|
923 return FALSE;
|
|
924
|
|
925 iq = jabber_iq_new_query(chat->js, JABBER_IQ_SET,
|
|
926 "http://jabber.org/protocol/muc#admin");
|
|
927
|
|
928 to = g_strdup_printf("%s@%s", chat->room, chat->server);
|
|
929 xmlnode_set_attrib(iq->node, "to", to);
|
|
930 g_free(to);
|
|
931
|
|
932 query = xmlnode_get_child(iq->node, "query");
|
|
933 item = xmlnode_new_child(query, "item");
|
|
934 xmlnode_set_attrib(item, "jid", jcm->jid);
|
|
935 xmlnode_set_attrib(item, "role", "none");
|
|
936 if(why) {
|
|
937 reason = xmlnode_new_child(item, "reason");
|
|
938 xmlnode_insert_data(reason, why, -1);
|
|
939 }
|
|
940
|
|
941 jabber_iq_send(iq);
|
|
942
|
|
943 return TRUE;
|
|
944 }
|
|
945
|
|
946 static void jabber_chat_disco_traffic_cb(JabberStream *js, xmlnode *packet, gpointer data)
|
|
947 {
|
|
948 JabberChat *chat;
|
|
949 xmlnode *query, *x;
|
|
950 int id = GPOINTER_TO_INT(data);
|
|
951
|
|
952 if(!(chat = jabber_chat_find_by_id(js, id)))
|
|
953 return;
|
|
954
|
|
955 /* defaults, in case the conference server doesn't
|
|
956 * support this request */
|
|
957 chat->xhtml = TRUE;
|
|
958
|
|
959 if(xmlnode_get_child(packet, "error")) {
|
|
960 return;
|
|
961 }
|
|
962
|
|
963 if(!(query = xmlnode_get_child(packet, "query")))
|
|
964 return;
|
|
965
|
|
966 chat->xhtml = FALSE;
|
|
967
|
|
968 for(x = xmlnode_get_child(query, "feature"); x; x = xmlnode_get_next_twin(x)) {
|
|
969 const char *var = xmlnode_get_attrib(x, "var");
|
|
970
|
|
971 if(var && !strcmp(var, "http://jabber.org/protocol/xhtml-im")) {
|
|
972 chat->xhtml = TRUE;
|
|
973 }
|
|
974 }
|
|
975 }
|
|
976
|
|
977 void jabber_chat_disco_traffic(JabberChat *chat)
|
|
978 {
|
|
979 JabberIq *iq;
|
|
980 xmlnode *query;
|
|
981 char *room_jid;
|
|
982
|
|
983 room_jid = g_strdup_printf("%s@%s", chat->room, chat->server);
|
|
984
|
|
985 iq = jabber_iq_new_query(chat->js, JABBER_IQ_GET,
|
|
986 "http://jabber.org/protocol/disco#info");
|
|
987
|
|
988 xmlnode_set_attrib(iq->node, "to", room_jid);
|
|
989
|
|
990 query = xmlnode_get_child(iq->node, "query");
|
|
991
|
|
992 xmlnode_set_attrib(query, "node", "http://jabber.org/protocol/muc#traffic");
|
|
993
|
|
994 jabber_iq_set_callback(iq, jabber_chat_disco_traffic_cb, GINT_TO_POINTER(chat->id));
|
|
995
|
|
996 jabber_iq_send(iq);
|
|
997
|
|
998 g_free(room_jid);
|
|
999 }
|
|
1000
|
|
1001
|
|
1002
|