comparison src/protocols/jabber/chat.c @ 7014:67c4e9d39242

[gaim-migrate @ 7577] Here it is, the bulk of the new Jabber prpl. Left to do: - Implement registration - Implement password changing - Keep track of conversation threads (since I apparently have to) - Fix the bugs that always magically appear in code after I commit committer: Tailor Script <tailor@pidgin.im>
author Nathan Walp <nwalp@pidgin.im>
date Mon, 29 Sep 2003 15:23:19 +0000
parents
children 82e980962926
comparison
equal deleted inserted replaced
7013:859cafb6433f 7014:67c4e9d39242
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 */
24
25 #include "chat.h"
26 #include "message.h"
27
28 GList *jabber_chat_info(GaimConnection *gc)
29 {
30 GList *m = NULL;
31 struct proto_chat_entry *pce;
32 JabberStream *js = gc->proto_data;
33
34 pce = g_new0(struct proto_chat_entry, 1);
35 pce->label = _("Room:");
36 pce->identifier = "room";
37 m = g_list_append(m, pce);
38
39 /* we're gonna default to a conference server I know is true, until
40 * I can figure out how to disco for a chat server */
41 pce = g_new0(struct proto_chat_entry, 1);
42 pce->label = _("Server:");
43 pce->identifier = "server";
44 pce->def = "conference.jabber.org";
45 m = g_list_append(m, pce);
46
47 pce = g_new0(struct proto_chat_entry, 1);
48 pce->label = _("Handle:");
49 pce->identifier = "handle";
50 pce->def = js->user->node;
51 m = g_list_append(m, pce);
52
53 pce = g_new0(struct proto_chat_entry, 1);
54 pce->label = _("Password:");
55 pce->identifier = "password";
56 pce->secret = TRUE;
57 m = g_list_append(m, pce);
58
59 return m;
60 }
61
62 JabberChat *jabber_chat_find(JabberStream *js, const char *room,
63 const char *server)
64 {
65 JabberChat *chat;
66 char *room_jid;
67
68 room_jid = g_strdup_printf("%s@%s", room, server);
69
70 chat = g_hash_table_lookup(js->chats, room_jid);
71 g_free(room_jid);
72
73 return chat;
74 }
75
76 struct _find_by_id_data {
77 int id;
78 JabberChat *chat;
79 };
80
81 void find_by_id_foreach_cb(gpointer key, gpointer value, gpointer user_data)
82 {
83 JabberChat *chat = value;
84 struct _find_by_id_data *fbid = user_data;
85
86 if(chat->id == fbid->id)
87 fbid->chat = chat;
88 }
89
90 JabberChat *jabber_chat_find_by_id(JabberStream *js, int id)
91 {
92 JabberChat *chat;
93 struct _find_by_id_data *fbid = g_new0(struct _find_by_id_data, 1);
94 g_hash_table_foreach(js->chats, find_by_id_foreach_cb, fbid);
95 chat = fbid->chat;
96 g_free(fbid);
97 return chat;
98 }
99
100 void jabber_chat_invite(GaimConnection *gc, int id, const char *msg,
101 const char *name)
102 {
103 JabberStream *js = gc->proto_data;
104 JabberChat *chat;
105 xmlnode *message, *body, *x, *invite;
106 char *room_jid;
107
108 chat = jabber_chat_find_by_id(js, id);
109 if(!chat)
110 return;
111
112 message = xmlnode_new("message");
113
114 room_jid = g_strdup_printf("%s@%s", chat->room, chat->server);
115
116 if(chat->muc) {
117 xmlnode_set_attrib(message, "to", room_jid);
118 x = xmlnode_new_child(message, "x");
119 xmlnode_set_attrib(x, "xmlns", "http://jabber.org/protocol/muc#user");
120 invite = xmlnode_new_child(x, "invite");
121 xmlnode_set_attrib(invite, "to", name);
122 body = xmlnode_new_child(invite, "reason");
123 xmlnode_insert_data(body, msg, -1);
124 } else {
125 xmlnode_set_attrib(message, "to", name);
126 body = xmlnode_new_child(message, "body");
127 xmlnode_insert_data(body, msg, -1);
128 x = xmlnode_new_child(message, "x");
129 xmlnode_set_attrib(x, "jid", room_jid);
130 xmlnode_set_attrib(x, "xmlns", "jabber:x:conference");
131 }
132
133 jabber_send(js, message);
134 xmlnode_free(message);
135 g_free(room_jid);
136 }
137
138 void jabber_chat_whisper(GaimConnection *gc, int id, const char *who,
139 const char *message)
140 {
141 JabberStream *js = gc->proto_data;
142 JabberChat *chat;
143 char *full_jid;
144
145 chat = jabber_chat_find_by_id(js, id);
146
147 /* TODO: we get real Jabber IDs from MUC sometimes, we need to cache
148 * them eventually */
149 full_jid = g_strdup_printf("%s@%s/%s", chat->room, chat->server, who);
150
151 jabber_message_send_im(gc, full_jid, message, 0);
152
153 g_free(full_jid);
154 }
155
156 void jabber_chat_join(GaimConnection *gc, GHashTable *data)
157 {
158 JabberChat *chat;
159 char *room, *server, *handle, *passwd;
160 xmlnode *presence, *x;
161 char *room_jid, *full_jid;
162 JabberStream *js = gc->proto_data;
163
164 room = g_hash_table_lookup(data, "room");
165 server = g_hash_table_lookup(data, "server");
166 handle = g_hash_table_lookup(data, "handle");
167 passwd = g_hash_table_lookup(data, "password");
168
169 if(!room || !server || !handle)
170 return;
171
172 if(jabber_chat_find(js, room, server))
173 return;
174
175 room_jid = g_strdup_printf("%s@%s", room, server);
176
177 chat = g_new0(JabberChat, 1);
178 chat->js = gc->proto_data;
179
180 chat->room = g_strdup(room);
181 chat->server = g_strdup(server);
182 chat->nick = g_strdup(handle);
183
184 g_hash_table_insert(js->chats, room_jid, chat);
185
186 presence = xmlnode_new("presence");
187 full_jid = g_strdup_printf("%s/%s", room_jid, handle);
188 xmlnode_set_attrib(presence, "to", full_jid);
189 g_free(full_jid);
190
191 x = xmlnode_new_child(presence, "x");
192 xmlnode_set_attrib(x, "xmlns", "http://jabber.org/protocol/muc");
193
194 if(passwd && *passwd) {
195 xmlnode *password = xmlnode_new_child(x, "password");
196 xmlnode_insert_data(password, passwd, -1);
197 }
198
199 jabber_send(js, presence);
200 xmlnode_free(presence);
201 }
202
203 void jabber_chat_leave(GaimConnection *gc, int id)
204 {
205 JabberStream *js = gc->proto_data;
206 JabberChat *chat = jabber_chat_find_by_id(js, id);
207 char *room_jid;
208 xmlnode *presence;
209
210 if(!chat)
211 return;
212
213 room_jid = g_strdup_printf("%s@%s", chat->room, chat->server);
214 gaim_debug(GAIM_DEBUG_INFO, "jabber", "%s is leaving chat %s\n",
215 chat->nick, room_jid);
216 presence = xmlnode_new("presence");
217 xmlnode_set_attrib(presence, "to", room_jid);
218 xmlnode_set_attrib(presence, "type", "unavailable");
219 jabber_send(js, presence);
220 xmlnode_free(presence);
221 }
222
223 void jabber_chat_destroy(JabberChat *chat)
224 {
225 JabberStream *js = chat->js;
226 char *room_jid = g_strdup_printf("%s@%s", chat->room, chat->server);
227
228 g_hash_table_remove(js->chats, room_jid);
229 g_free(room_jid);
230
231 g_free(chat->room);
232 g_free(chat->server);
233 g_free(chat->nick);
234 g_free(chat);
235 }
236
237 gboolean jabber_chat_find_buddy(GaimConversation *conv, const char *name)
238 {
239 GList *m = gaim_chat_get_users(GAIM_CHAT(conv));
240
241 while(m) {
242 if(!strcmp(m->data, name))
243 return TRUE;
244 m = m->next;
245 }
246
247 return FALSE;
248 }
249