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