Mercurial > pidgin
annotate src/protocols/jabber/chat.c @ 7923:e87e7d9d0132
[gaim-migrate @ 8592]
you can now configure MUC chat rooms. yay!
committer: Tailor Script <tailor@pidgin.im>
author | Nathan Walp <nwalp@pidgin.im> |
---|---|
date | Tue, 23 Dec 2003 17:36:07 +0000 |
parents | ddd80ffd3815 |
children | 68900f9cb540 |
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"); | |
293 JabberChat *chat; | |
294 JabberID *jid; | |
295 | |
296 if(!type || !from) | |
297 return; | |
298 | |
299 | |
300 if(!strcmp(type, "result")) { | |
301 jid = jabber_id_new(from); | |
302 | |
303 if(!jid) | |
304 return; | |
305 | |
306 chat = jabber_chat_find(js, jid->node, jid->domain); | |
307 jabber_id_free(jid); | |
308 | |
309 if(!chat) | |
310 return; | |
311 | |
312 if(!(query = xmlnode_get_child(packet, "query"))) | |
313 return; | |
314 | |
315 for(x = query->child; x; x = x->next) { | |
316 const char *xmlns; | |
317 if(strcmp(x->name, "x")) | |
318 continue; | |
319 | |
320 if(!(xmlns = xmlnode_get_attrib(x, "xmlns"))) | |
321 continue; | |
322 | |
323 if(!strcmp(xmlns, "jabber:x:data")) { | |
324 jabber_x_data_request(js, x, jabber_chat_room_configure_x_data_cb, chat); | |
325 return; | |
326 } | |
327 } | |
328 } else { | |
329 /* XXX: handle errors */ | |
330 } | |
331 | |
332 | |
333 } | |
334 | |
335 void jabber_chat_request_room_configure(JabberChat *chat) { | |
336 JabberIq *iq; | |
337 xmlnode *query; | |
338 char *room_jid; | |
339 | |
7895 | 340 if(!chat) |
341 return; | |
342 | |
7923 | 343 iq = jabber_iq_new_query(chat->js, JABBER_IQ_SET, |
344 "http://jabber.org/protocol/muc#owner"); | |
345 query = xmlnode_get_child(iq->node, "query"); | |
346 room_jid = g_strdup_printf("%s@%s", chat->room, chat->server); | |
7895 | 347 |
7923 | 348 xmlnode_set_attrib(iq->node, "to", room_jid); |
349 | |
350 jabber_iq_set_callback(iq, jabber_chat_room_configure_cb, NULL); | |
351 | |
352 jabber_iq_send(iq); | |
353 | |
354 g_free(room_jid); | |
7895 | 355 } |
356 | |
357 void jabber_chat_create_instant_room(JabberChat *chat) { | |
358 JabberIq *iq; | |
359 xmlnode *query, *x; | |
360 char *room_jid; | |
361 | |
362 if(!chat) | |
363 return; | |
364 | |
365 iq = jabber_iq_new_query(chat->js, JABBER_IQ_SET, | |
366 "http://jabber.org/protocol/muc#owner"); | |
367 query = xmlnode_get_child(iq->node, "query"); | |
368 x = xmlnode_new_child(query, "x"); | |
369 room_jid = g_strdup_printf("%s@%s", chat->room, chat->server); | |
370 | |
371 xmlnode_set_attrib(iq->node, "to", room_jid); | |
372 xmlnode_set_attrib(x, "xmlns", "jabber:x:data"); | |
373 xmlnode_set_attrib(x, "type", "submit"); | |
374 | |
375 jabber_iq_send(iq); | |
376 | |
377 g_free(room_jid); | |
378 } |