Mercurial > pidgin
annotate src/protocols/jabber/chat.c @ 7616:a4acf41898c1
[gaim-migrate @ 8240]
wouldn't it be cool if logging just worked? If it didn't leak, and didn't
crash, and sort-by-log-size worked as an added bonus?
i should have gotten a bounty declared on this first ;-)
committer: Tailor Script <tailor@pidgin.im>
author | Nathan Walp <nwalp@pidgin.im> |
---|---|
date | Sun, 23 Nov 2003 22:55:41 +0000 |
parents | 08cef5988410 |
children | 0555e59dfba9 |
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 | |
7322 | 72 chat = g_hash_table_lookup(js->chats, jabber_normalize(NULL, 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_join(GaimConnection *gc, GHashTable *data) | |
142 { | |
143 JabberChat *chat; | |
144 char *room, *server, *handle, *passwd; | |
145 xmlnode *presence, *x; | |
7262 | 146 char *tmp, *room_jid, *full_jid; |
7014 | 147 JabberStream *js = gc->proto_data; |
148 | |
149 room = g_hash_table_lookup(data, "room"); | |
150 server = g_hash_table_lookup(data, "server"); | |
151 handle = g_hash_table_lookup(data, "handle"); | |
152 passwd = g_hash_table_lookup(data, "password"); | |
153 | |
154 if(!room || !server || !handle) | |
155 return; | |
156 | |
7310 | 157 if(!jabber_nodeprep_validate(room)) { |
158 char *buf = g_strdup_printf(_("%s is not a valid room name"), room); | |
159 gaim_notify_error(gc, _("Invalid Room Name"), _("Invalid Room Name"), | |
160 buf); | |
161 g_free(buf); | |
162 return; | |
163 } else if(!jabber_nameprep_validate(server)) { | |
164 char *buf = g_strdup_printf(_("%s is not a valid server name"), server); | |
165 gaim_notify_error(gc, _("Invalid Server Name"), | |
166 _("Invalid Server Name"), buf); | |
167 g_free(buf); | |
168 return; | |
169 } else if(!jabber_resourceprep_validate(handle)) { | |
170 char *buf = g_strdup_printf(_("%s is not a valid room handle"), handle); | |
171 gaim_notify_error(gc, _("Invalid Room Handle"), | |
172 _("Invalid Room Handle"), buf); | |
173 } | |
174 | |
7014 | 175 if(jabber_chat_find(js, room, server)) |
176 return; | |
177 | |
7262 | 178 tmp = g_strdup_printf("%s@%s", room, server); |
7322 | 179 room_jid = g_strdup(jabber_normalize(NULL, tmp)); |
7262 | 180 g_free(tmp); |
7014 | 181 |
182 chat = g_new0(JabberChat, 1); | |
183 chat->js = gc->proto_data; | |
184 | |
185 chat->room = g_strdup(room); | |
186 chat->server = g_strdup(server); | |
187 chat->nick = g_strdup(handle); | |
188 | |
189 g_hash_table_insert(js->chats, room_jid, chat); | |
190 | |
7073 | 191 presence = jabber_presence_create(gc->away_state, gc->away); |
7014 | 192 full_jid = g_strdup_printf("%s/%s", room_jid, handle); |
193 xmlnode_set_attrib(presence, "to", full_jid); | |
194 g_free(full_jid); | |
195 | |
196 x = xmlnode_new_child(presence, "x"); | |
197 xmlnode_set_attrib(x, "xmlns", "http://jabber.org/protocol/muc"); | |
198 | |
199 if(passwd && *passwd) { | |
200 xmlnode *password = xmlnode_new_child(x, "password"); | |
201 xmlnode_insert_data(password, passwd, -1); | |
202 } | |
203 | |
204 jabber_send(js, presence); | |
205 xmlnode_free(presence); | |
206 } | |
207 | |
208 void jabber_chat_leave(GaimConnection *gc, int id) | |
209 { | |
210 JabberStream *js = gc->proto_data; | |
211 JabberChat *chat = jabber_chat_find_by_id(js, id); | |
212 char *room_jid; | |
213 xmlnode *presence; | |
214 | |
215 if(!chat) | |
216 return; | |
217 | |
218 room_jid = g_strdup_printf("%s@%s", chat->room, chat->server); | |
219 gaim_debug(GAIM_DEBUG_INFO, "jabber", "%s is leaving chat %s\n", | |
220 chat->nick, room_jid); | |
221 presence = xmlnode_new("presence"); | |
222 xmlnode_set_attrib(presence, "to", room_jid); | |
223 xmlnode_set_attrib(presence, "type", "unavailable"); | |
224 jabber_send(js, presence); | |
225 xmlnode_free(presence); | |
226 } | |
227 | |
228 void jabber_chat_destroy(JabberChat *chat) | |
229 { | |
230 JabberStream *js = chat->js; | |
231 char *room_jid = g_strdup_printf("%s@%s", chat->room, chat->server); | |
232 | |
7322 | 233 g_hash_table_remove(js->chats, jabber_normalize(NULL, room_jid)); |
7014 | 234 g_free(room_jid); |
235 | |
236 g_free(chat->room); | |
237 g_free(chat->server); | |
238 g_free(chat->nick); | |
239 g_free(chat); | |
240 } | |
241 | |
242 gboolean jabber_chat_find_buddy(GaimConversation *conv, const char *name) | |
243 { | |
7118
bf630f7dfdcd
[gaim-migrate @ 7685]
Christian Hammond <chipx86@chipx86.com>
parents:
7073
diff
changeset
|
244 GList *m = gaim_conv_chat_get_users(GAIM_CONV_CHAT(conv)); |
7014 | 245 |
246 while(m) { | |
247 if(!strcmp(m->data, name)) | |
248 return TRUE; | |
249 m = m->next; | |
250 } | |
251 | |
252 return FALSE; | |
253 } | |
254 | |
7398 | 255 char *jabber_chat_buddy_real_name(GaimConnection *gc, int id, const char *who) |
256 { | |
257 JabberStream *js = gc->proto_data; | |
258 JabberChat *chat; | |
259 | |
260 chat = jabber_chat_find_by_id(js, id); | |
261 | |
262 if(!chat) | |
263 return NULL; | |
264 | |
265 return g_strdup_printf("%s@%s/%s", chat->room, chat->server, who); | |
266 } |