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