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