Mercurial > pidgin.yaz
annotate src/protocols/jabber/chat.c @ 7895:ddd80ffd3815
[gaim-migrate @ 8555]
wanted to get this committed before I went home
committer: Tailor Script <tailor@pidgin.im>
author | Nathan Walp <nwalp@pidgin.im> |
---|---|
date | Fri, 19 Dec 2003 08:33:03 +0000 |
parents | 0555e59dfba9 |
children | e87e7d9d0132 |
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" |
7014 | 30 |
31 GList *jabber_chat_info(GaimConnection *gc) | |
32 { | |
33 GList *m = NULL; | |
34 struct proto_chat_entry *pce; | |
35 JabberStream *js = gc->proto_data; | |
36 | |
37 pce = g_new0(struct proto_chat_entry, 1); | |
7841 | 38 pce->label = _("_Room:"); |
7014 | 39 pce->identifier = "room"; |
40 m = g_list_append(m, pce); | |
41 | |
42 /* we're gonna default to a conference server I know is true, until | |
43 * I can figure out how to disco for a chat server */ | |
44 pce = g_new0(struct proto_chat_entry, 1); | |
7841 | 45 pce->label = _("_Server:"); |
7014 | 46 pce->identifier = "server"; |
47 pce->def = "conference.jabber.org"; | |
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 pce->def = js->user->node; | |
54 m = g_list_append(m, pce); | |
55 | |
56 pce = g_new0(struct proto_chat_entry, 1); | |
7841 | 57 pce->label = _("_Password:"); |
7014 | 58 pce->identifier = "password"; |
59 pce->secret = TRUE; | |
60 m = g_list_append(m, pce); | |
61 | |
62 return m; | |
63 } | |
64 | |
65 JabberChat *jabber_chat_find(JabberStream *js, const char *room, | |
66 const char *server) | |
67 { | |
68 JabberChat *chat; | |
69 char *room_jid; | |
70 | |
71 room_jid = g_strdup_printf("%s@%s", room, server); | |
72 | |
7322 | 73 chat = g_hash_table_lookup(js->chats, jabber_normalize(NULL, room_jid)); |
7014 | 74 g_free(room_jid); |
75 | |
76 return chat; | |
77 } | |
78 | |
79 struct _find_by_id_data { | |
80 int id; | |
81 JabberChat *chat; | |
82 }; | |
83 | |
84 void find_by_id_foreach_cb(gpointer key, gpointer value, gpointer user_data) | |
85 { | |
86 JabberChat *chat = value; | |
87 struct _find_by_id_data *fbid = user_data; | |
88 | |
89 if(chat->id == fbid->id) | |
90 fbid->chat = chat; | |
91 } | |
92 | |
93 JabberChat *jabber_chat_find_by_id(JabberStream *js, int id) | |
94 { | |
95 JabberChat *chat; | |
96 struct _find_by_id_data *fbid = g_new0(struct _find_by_id_data, 1); | |
7073 | 97 fbid->id = id; |
7014 | 98 g_hash_table_foreach(js->chats, find_by_id_foreach_cb, fbid); |
99 chat = fbid->chat; | |
100 g_free(fbid); | |
101 return chat; | |
102 } | |
103 | |
104 void jabber_chat_invite(GaimConnection *gc, int id, const char *msg, | |
105 const char *name) | |
106 { | |
107 JabberStream *js = gc->proto_data; | |
108 JabberChat *chat; | |
109 xmlnode *message, *body, *x, *invite; | |
110 char *room_jid; | |
111 | |
112 chat = jabber_chat_find_by_id(js, id); | |
113 if(!chat) | |
114 return; | |
115 | |
116 message = xmlnode_new("message"); | |
117 | |
118 room_jid = g_strdup_printf("%s@%s", chat->room, chat->server); | |
119 | |
120 if(chat->muc) { | |
121 xmlnode_set_attrib(message, "to", room_jid); | |
122 x = xmlnode_new_child(message, "x"); | |
123 xmlnode_set_attrib(x, "xmlns", "http://jabber.org/protocol/muc#user"); | |
124 invite = xmlnode_new_child(x, "invite"); | |
125 xmlnode_set_attrib(invite, "to", name); | |
126 body = xmlnode_new_child(invite, "reason"); | |
127 xmlnode_insert_data(body, msg, -1); | |
128 } else { | |
129 xmlnode_set_attrib(message, "to", name); | |
130 body = xmlnode_new_child(message, "body"); | |
131 xmlnode_insert_data(body, msg, -1); | |
132 x = xmlnode_new_child(message, "x"); | |
133 xmlnode_set_attrib(x, "jid", room_jid); | |
134 xmlnode_set_attrib(x, "xmlns", "jabber:x:conference"); | |
135 } | |
136 | |
137 jabber_send(js, message); | |
138 xmlnode_free(message); | |
139 g_free(room_jid); | |
140 } | |
141 | |
142 void jabber_chat_join(GaimConnection *gc, GHashTable *data) | |
143 { | |
144 JabberChat *chat; | |
145 char *room, *server, *handle, *passwd; | |
146 xmlnode *presence, *x; | |
7262 | 147 char *tmp, *room_jid, *full_jid; |
7014 | 148 JabberStream *js = gc->proto_data; |
149 | |
150 room = g_hash_table_lookup(data, "room"); | |
151 server = g_hash_table_lookup(data, "server"); | |
152 handle = g_hash_table_lookup(data, "handle"); | |
153 passwd = g_hash_table_lookup(data, "password"); | |
154 | |
155 if(!room || !server || !handle) | |
156 return; | |
157 | |
7310 | 158 if(!jabber_nodeprep_validate(room)) { |
159 char *buf = g_strdup_printf(_("%s is not a valid room name"), room); | |
160 gaim_notify_error(gc, _("Invalid Room Name"), _("Invalid Room Name"), | |
161 buf); | |
162 g_free(buf); | |
163 return; | |
164 } else if(!jabber_nameprep_validate(server)) { | |
165 char *buf = g_strdup_printf(_("%s is not a valid server name"), server); | |
166 gaim_notify_error(gc, _("Invalid Server Name"), | |
167 _("Invalid Server Name"), buf); | |
168 g_free(buf); | |
169 return; | |
170 } else if(!jabber_resourceprep_validate(handle)) { | |
171 char *buf = g_strdup_printf(_("%s is not a valid room handle"), handle); | |
172 gaim_notify_error(gc, _("Invalid Room Handle"), | |
173 _("Invalid Room Handle"), buf); | |
174 } | |
175 | |
7014 | 176 if(jabber_chat_find(js, room, server)) |
177 return; | |
178 | |
7262 | 179 tmp = g_strdup_printf("%s@%s", room, server); |
7322 | 180 room_jid = g_strdup(jabber_normalize(NULL, tmp)); |
7262 | 181 g_free(tmp); |
7014 | 182 |
183 chat = g_new0(JabberChat, 1); | |
184 chat->js = gc->proto_data; | |
185 | |
186 chat->room = g_strdup(room); | |
187 chat->server = g_strdup(server); | |
188 chat->nick = g_strdup(handle); | |
189 | |
190 g_hash_table_insert(js->chats, room_jid, chat); | |
191 | |
7073 | 192 presence = jabber_presence_create(gc->away_state, gc->away); |
7014 | 193 full_jid = g_strdup_printf("%s/%s", room_jid, handle); |
194 xmlnode_set_attrib(presence, "to", full_jid); | |
195 g_free(full_jid); | |
196 | |
197 x = xmlnode_new_child(presence, "x"); | |
198 xmlnode_set_attrib(x, "xmlns", "http://jabber.org/protocol/muc"); | |
199 | |
200 if(passwd && *passwd) { | |
201 xmlnode *password = xmlnode_new_child(x, "password"); | |
202 xmlnode_insert_data(password, passwd, -1); | |
203 } | |
204 | |
205 jabber_send(js, presence); | |
206 xmlnode_free(presence); | |
207 } | |
208 | |
209 void jabber_chat_leave(GaimConnection *gc, int id) | |
210 { | |
211 JabberStream *js = gc->proto_data; | |
212 JabberChat *chat = jabber_chat_find_by_id(js, id); | |
213 char *room_jid; | |
214 xmlnode *presence; | |
215 | |
216 if(!chat) | |
217 return; | |
218 | |
219 room_jid = g_strdup_printf("%s@%s", chat->room, chat->server); | |
220 gaim_debug(GAIM_DEBUG_INFO, "jabber", "%s is leaving chat %s\n", | |
221 chat->nick, room_jid); | |
222 presence = xmlnode_new("presence"); | |
223 xmlnode_set_attrib(presence, "to", room_jid); | |
224 xmlnode_set_attrib(presence, "type", "unavailable"); | |
225 jabber_send(js, presence); | |
226 xmlnode_free(presence); | |
227 } | |
228 | |
229 void jabber_chat_destroy(JabberChat *chat) | |
230 { | |
231 JabberStream *js = chat->js; | |
232 char *room_jid = g_strdup_printf("%s@%s", chat->room, chat->server); | |
233 | |
7322 | 234 g_hash_table_remove(js->chats, jabber_normalize(NULL, room_jid)); |
7014 | 235 g_free(room_jid); |
236 | |
237 g_free(chat->room); | |
238 g_free(chat->server); | |
239 g_free(chat->nick); | |
240 g_free(chat); | |
241 } | |
242 | |
243 gboolean jabber_chat_find_buddy(GaimConversation *conv, const char *name) | |
244 { | |
7118
bf630f7dfdcd
[gaim-migrate @ 7685]
Christian Hammond <chipx86@chipx86.com>
parents:
7073
diff
changeset
|
245 GList *m = gaim_conv_chat_get_users(GAIM_CONV_CHAT(conv)); |
7014 | 246 |
247 while(m) { | |
248 if(!strcmp(m->data, name)) | |
249 return TRUE; | |
250 m = m->next; | |
251 } | |
252 | |
253 return FALSE; | |
254 } | |
255 | |
7398 | 256 char *jabber_chat_buddy_real_name(GaimConnection *gc, int id, const char *who) |
257 { | |
258 JabberStream *js = gc->proto_data; | |
259 JabberChat *chat; | |
260 | |
261 chat = jabber_chat_find_by_id(js, id); | |
262 | |
263 if(!chat) | |
264 return NULL; | |
265 | |
266 return g_strdup_printf("%s@%s/%s", chat->room, chat->server, who); | |
267 } | |
7895 | 268 |
269 void jabber_chat_start_room_configure(JabberChat *chat) { | |
270 if(!chat) | |
271 return; | |
272 | |
273 /* XXX: implement me! */ | |
274 | |
275 /* XXX: for now... */ | |
276 jabber_chat_create_instant_room(chat); | |
277 } | |
278 | |
279 void jabber_chat_create_instant_room(JabberChat *chat) { | |
280 JabberIq *iq; | |
281 xmlnode *query, *x; | |
282 char *room_jid; | |
283 | |
284 if(!chat) | |
285 return; | |
286 | |
287 iq = jabber_iq_new_query(chat->js, JABBER_IQ_SET, | |
288 "http://jabber.org/protocol/muc#owner"); | |
289 query = xmlnode_get_child(iq->node, "query"); | |
290 x = xmlnode_new_child(query, "x"); | |
291 room_jid = g_strdup_printf("%s@%s", chat->room, chat->server); | |
292 | |
293 xmlnode_set_attrib(iq->node, "to", room_jid); | |
294 xmlnode_set_attrib(x, "xmlns", "jabber:x:data"); | |
295 xmlnode_set_attrib(x, "type", "submit"); | |
296 | |
297 jabber_iq_send(iq); | |
298 | |
299 g_free(room_jid); | |
300 } |