Mercurial > pidgin.yaz
annotate src/protocols/jabber/chat.c @ 7955:119a22025818
[gaim-migrate @ 8630]
fun jabber stuff, which isn't done yet. mostly harmless, and committing this now makes the next
commit that much easier for a lazy person such as myself to type.
committer: Tailor Script <tailor@pidgin.im>
author | Nathan Walp <nwalp@pidgin.im> |
---|---|
date | Mon, 29 Dec 2003 08:59:22 +0000 |
parents | 68900f9cb540 |
children | 6fca0d9cc98b |
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" | |
23 #include "multi.h" /* for proto_chat_entry */ | |
7310 | 24 #include "notify.h" |
7014 | 25 |
26 #include "chat.h" | |
7895 | 27 #include "iq.h" |
7014 | 28 #include "message.h" |
7073 | 29 #include "presence.h" |
7923 | 30 #include "xdata.h" |
7014 | 31 |
32 GList *jabber_chat_info(GaimConnection *gc) | |
33 { | |
34 GList *m = NULL; | |
35 struct proto_chat_entry *pce; | |
36 JabberStream *js = gc->proto_data; | |
37 | |
38 pce = g_new0(struct proto_chat_entry, 1); | |
7841 | 39 pce->label = _("_Room:"); |
7014 | 40 pce->identifier = "room"; |
41 m = g_list_append(m, pce); | |
42 | |
43 /* we're gonna default to a conference server I know is true, until | |
44 * I can figure out how to disco for a chat server */ | |
45 pce = g_new0(struct proto_chat_entry, 1); | |
7841 | 46 pce->label = _("_Server:"); |
7014 | 47 pce->identifier = "server"; |
48 pce->def = "conference.jabber.org"; | |
49 m = g_list_append(m, pce); | |
50 | |
51 pce = g_new0(struct proto_chat_entry, 1); | |
7841 | 52 pce->label = _("_Handle:"); |
7014 | 53 pce->identifier = "handle"; |
54 pce->def = js->user->node; | |
55 m = g_list_append(m, pce); | |
56 | |
57 pce = g_new0(struct proto_chat_entry, 1); | |
7841 | 58 pce->label = _("_Password:"); |
7014 | 59 pce->identifier = "password"; |
60 pce->secret = TRUE; | |
61 m = g_list_append(m, pce); | |
62 | |
63 return m; | |
64 } | |
65 | |
66 JabberChat *jabber_chat_find(JabberStream *js, const char *room, | |
67 const char *server) | |
68 { | |
69 JabberChat *chat; | |
70 char *room_jid; | |
71 | |
72 room_jid = g_strdup_printf("%s@%s", room, server); | |
73 | |
7322 | 74 chat = g_hash_table_lookup(js->chats, jabber_normalize(NULL, room_jid)); |
7014 | 75 g_free(room_jid); |
76 | |
77 return chat; | |
78 } | |
79 | |
80 struct _find_by_id_data { | |
81 int id; | |
82 JabberChat *chat; | |
83 }; | |
84 | |
85 void find_by_id_foreach_cb(gpointer key, gpointer value, gpointer user_data) | |
86 { | |
87 JabberChat *chat = value; | |
88 struct _find_by_id_data *fbid = user_data; | |
89 | |
90 if(chat->id == fbid->id) | |
91 fbid->chat = chat; | |
92 } | |
93 | |
94 JabberChat *jabber_chat_find_by_id(JabberStream *js, int id) | |
95 { | |
96 JabberChat *chat; | |
97 struct _find_by_id_data *fbid = g_new0(struct _find_by_id_data, 1); | |
7073 | 98 fbid->id = id; |
7014 | 99 g_hash_table_foreach(js->chats, find_by_id_foreach_cb, fbid); |
100 chat = fbid->chat; | |
101 g_free(fbid); | |
102 return chat; | |
103 } | |
104 | |
105 void jabber_chat_invite(GaimConnection *gc, int id, const char *msg, | |
106 const char *name) | |
107 { | |
108 JabberStream *js = gc->proto_data; | |
109 JabberChat *chat; | |
110 xmlnode *message, *body, *x, *invite; | |
111 char *room_jid; | |
112 | |
113 chat = jabber_chat_find_by_id(js, id); | |
114 if(!chat) | |
115 return; | |
116 | |
117 message = xmlnode_new("message"); | |
118 | |
119 room_jid = g_strdup_printf("%s@%s", chat->room, chat->server); | |
120 | |
121 if(chat->muc) { | |
122 xmlnode_set_attrib(message, "to", room_jid); | |
123 x = xmlnode_new_child(message, "x"); | |
124 xmlnode_set_attrib(x, "xmlns", "http://jabber.org/protocol/muc#user"); | |
125 invite = xmlnode_new_child(x, "invite"); | |
126 xmlnode_set_attrib(invite, "to", name); | |
127 body = xmlnode_new_child(invite, "reason"); | |
128 xmlnode_insert_data(body, msg, -1); | |
129 } else { | |
130 xmlnode_set_attrib(message, "to", name); | |
131 body = xmlnode_new_child(message, "body"); | |
132 xmlnode_insert_data(body, msg, -1); | |
133 x = xmlnode_new_child(message, "x"); | |
134 xmlnode_set_attrib(x, "jid", room_jid); | |
135 xmlnode_set_attrib(x, "xmlns", "jabber:x:conference"); | |
136 } | |
137 | |
138 jabber_send(js, message); | |
139 xmlnode_free(message); | |
140 g_free(room_jid); | |
141 } | |
142 | |
143 void jabber_chat_join(GaimConnection *gc, GHashTable *data) | |
144 { | |
145 JabberChat *chat; | |
146 char *room, *server, *handle, *passwd; | |
147 xmlnode *presence, *x; | |
7262 | 148 char *tmp, *room_jid, *full_jid; |
7014 | 149 JabberStream *js = gc->proto_data; |
150 | |
151 room = g_hash_table_lookup(data, "room"); | |
152 server = g_hash_table_lookup(data, "server"); | |
153 handle = g_hash_table_lookup(data, "handle"); | |
154 passwd = g_hash_table_lookup(data, "password"); | |
155 | |
156 if(!room || !server || !handle) | |
157 return; | |
158 | |
7310 | 159 if(!jabber_nodeprep_validate(room)) { |
160 char *buf = g_strdup_printf(_("%s is not a valid room name"), room); | |
161 gaim_notify_error(gc, _("Invalid Room Name"), _("Invalid Room Name"), | |
162 buf); | |
163 g_free(buf); | |
164 return; | |
165 } else if(!jabber_nameprep_validate(server)) { | |
166 char *buf = g_strdup_printf(_("%s is not a valid server name"), server); | |
167 gaim_notify_error(gc, _("Invalid Server Name"), | |
168 _("Invalid Server Name"), buf); | |
169 g_free(buf); | |
170 return; | |
171 } else if(!jabber_resourceprep_validate(handle)) { | |
172 char *buf = g_strdup_printf(_("%s is not a valid room handle"), handle); | |
173 gaim_notify_error(gc, _("Invalid Room Handle"), | |
174 _("Invalid Room Handle"), buf); | |
175 } | |
176 | |
7014 | 177 if(jabber_chat_find(js, room, server)) |
178 return; | |
179 | |
7262 | 180 tmp = g_strdup_printf("%s@%s", room, server); |
7322 | 181 room_jid = g_strdup(jabber_normalize(NULL, tmp)); |
7262 | 182 g_free(tmp); |
7014 | 183 |
184 chat = g_new0(JabberChat, 1); | |
185 chat->js = gc->proto_data; | |
186 | |
187 chat->room = g_strdup(room); | |
188 chat->server = g_strdup(server); | |
189 chat->nick = g_strdup(handle); | |
190 | |
191 g_hash_table_insert(js->chats, room_jid, chat); | |
192 | |
7073 | 193 presence = jabber_presence_create(gc->away_state, gc->away); |
7014 | 194 full_jid = g_strdup_printf("%s/%s", room_jid, handle); |
195 xmlnode_set_attrib(presence, "to", full_jid); | |
196 g_free(full_jid); | |
197 | |
198 x = xmlnode_new_child(presence, "x"); | |
199 xmlnode_set_attrib(x, "xmlns", "http://jabber.org/protocol/muc"); | |
200 | |
201 if(passwd && *passwd) { | |
202 xmlnode *password = xmlnode_new_child(x, "password"); | |
203 xmlnode_insert_data(password, passwd, -1); | |
204 } | |
205 | |
206 jabber_send(js, presence); | |
207 xmlnode_free(presence); | |
208 } | |
209 | |
210 void jabber_chat_leave(GaimConnection *gc, int id) | |
211 { | |
212 JabberStream *js = gc->proto_data; | |
213 JabberChat *chat = jabber_chat_find_by_id(js, id); | |
214 char *room_jid; | |
215 xmlnode *presence; | |
216 | |
217 if(!chat) | |
218 return; | |
219 | |
220 room_jid = g_strdup_printf("%s@%s", chat->room, chat->server); | |
221 gaim_debug(GAIM_DEBUG_INFO, "jabber", "%s is leaving chat %s\n", | |
222 chat->nick, room_jid); | |
223 presence = xmlnode_new("presence"); | |
224 xmlnode_set_attrib(presence, "to", room_jid); | |
225 xmlnode_set_attrib(presence, "type", "unavailable"); | |
226 jabber_send(js, presence); | |
227 xmlnode_free(presence); | |
228 } | |
229 | |
230 void jabber_chat_destroy(JabberChat *chat) | |
231 { | |
232 JabberStream *js = chat->js; | |
233 char *room_jid = g_strdup_printf("%s@%s", chat->room, chat->server); | |
234 | |
7322 | 235 g_hash_table_remove(js->chats, jabber_normalize(NULL, room_jid)); |
7014 | 236 g_free(room_jid); |
237 | |
238 g_free(chat->room); | |
239 g_free(chat->server); | |
240 g_free(chat->nick); | |
241 g_free(chat); | |
242 } | |
243 | |
244 gboolean jabber_chat_find_buddy(GaimConversation *conv, const char *name) | |
245 { | |
7118
bf630f7dfdcd
[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 | |
317 for(x = query->child; x; x = x->next) { | |
318 const char *xmlns; | |
319 if(strcmp(x->name, "x")) | |
320 continue; | |
321 | |
322 if(!(xmlns = xmlnode_get_attrib(x, "xmlns"))) | |
323 continue; | |
324 | |
325 if(!strcmp(xmlns, "jabber:x:data")) { | |
326 jabber_x_data_request(js, x, jabber_chat_room_configure_x_data_cb, chat); | |
327 return; | |
328 } | |
329 } | |
7926 | 330 } else if(!strcmp(type, "error")) { |
331 xmlnode *errnode = xmlnode_get_child(packet, "error"); | |
332 const char *code = NULL; | |
333 char *code_txt = NULL; | |
334 char *msg; | |
335 char *text = NULL; | |
336 | |
337 if(errnode) { | |
338 code = xmlnode_get_attrib(errnode, "code"); | |
339 text = xmlnode_get_data(errnode); | |
340 } | |
341 | |
342 if(code) | |
343 code_txt = g_strdup_printf(_(" (Code %s)"), code); | |
344 | |
345 msg = g_strdup_printf("%s%s", text ? text : "", code_txt ? code_txt : ""); | |
346 gaim_notify_error(js->gc, _("Configuration error"), _("Configuration error"), msg); | |
347 | |
348 g_free(msg); | |
349 if(code_txt) | |
350 g_free(code_txt); | |
351 | |
352 return; | |
7923 | 353 } |
354 | |
7926 | 355 msg = g_strdup_printf("Unable to configure room %s", from); |
356 | |
357 gaim_notify_info(js->gc, _("Unable to configure"), _("Unable to configure"), msg); | |
358 g_free(msg); | |
7923 | 359 |
360 } | |
361 | |
362 void jabber_chat_request_room_configure(JabberChat *chat) { | |
363 JabberIq *iq; | |
364 xmlnode *query; | |
365 char *room_jid; | |
366 | |
7895 | 367 if(!chat) |
368 return; | |
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 | |
398 iq = jabber_iq_new_query(chat->js, JABBER_IQ_SET, | |
399 "http://jabber.org/protocol/muc#owner"); | |
400 query = xmlnode_get_child(iq->node, "query"); | |
401 x = xmlnode_new_child(query, "x"); | |
402 room_jid = g_strdup_printf("%s@%s", chat->room, chat->server); | |
403 | |
404 xmlnode_set_attrib(iq->node, "to", room_jid); | |
405 xmlnode_set_attrib(x, "xmlns", "jabber:x:data"); | |
406 xmlnode_set_attrib(x, "type", "submit"); | |
407 | |
408 jabber_iq_send(iq); | |
409 | |
410 g_free(room_jid); | |
411 } | |
7955 | 412 |
413 static void jabber_chat_register_x_data_result_cb(JabberStream *js, xmlnode *packet, gpointer data) | |
414 { | |
415 const char *type = xmlnode_get_attrib(packet, "type"); | |
416 | |
417 if(type && !strcmp(type, "error")) { | |
418 /* XXX: handle an error (you'll get a 409 if the nick is already registered) */ | |
419 } | |
420 } | |
421 | |
422 static void jabber_chat_register_x_data_cb(JabberStream *js, xmlnode *result, gpointer data) | |
423 { | |
424 JabberChat *chat = data; | |
425 xmlnode *query; | |
426 JabberIq *iq; | |
427 char *to = g_strdup_printf("%s@%s", chat->room, chat->server); | |
428 | |
429 iq = jabber_iq_new_query(js, JABBER_IQ_SET, "jabber:iq:register"); | |
430 xmlnode_set_attrib(iq->node, "to", to); | |
431 g_free(to); | |
432 | |
433 query = xmlnode_get_child(iq->node, "query"); | |
434 | |
435 xmlnode_insert_child(query, result); | |
436 | |
437 jabber_iq_set_callback(iq, jabber_chat_register_x_data_result_cb, NULL); | |
438 | |
439 jabber_iq_send(iq); | |
440 } | |
441 | |
442 static void jabber_chat_register_cb(JabberStream *js, xmlnode *packet, gpointer data) | |
443 { | |
444 xmlnode *query, *x; | |
445 const char *type = xmlnode_get_attrib(packet, "type"); | |
446 const char *from = xmlnode_get_attrib(packet, "from"); | |
447 char *msg; | |
448 JabberChat *chat; | |
449 JabberID *jid; | |
450 | |
451 if(!type || !from) | |
452 return; | |
453 | |
454 if(!strcmp(type, "result")) { | |
455 jid = jabber_id_new(from); | |
456 | |
457 if(!jid) | |
458 return; | |
459 | |
460 chat = jabber_chat_find(js, jid->node, jid->domain); | |
461 jabber_id_free(jid); | |
462 | |
463 if(!chat) | |
464 return; | |
465 | |
466 if(!(query = xmlnode_get_child(packet, "query"))) | |
467 return; | |
468 | |
469 for(x = query->child; x; x = x->next) { | |
470 const char *xmlns; | |
471 if(strcmp(x->name, "x")) | |
472 continue; | |
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 | |
534 |