Debian Codegraph @

src/core/xep/muc-commands.c

raw symbols SCIP
1 /*
2 * Copyright (C) 2007 Colin DIDIER
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
16 */
17
18 #include <stdlib.h>
19
20 #include "module.h"
21 #include "settings.h"
22 #include "signals.h"
23 #include "window-item-def.h"
24
25 #include "xmpp-servers.h"
26 #include "xmpp-commands.h"
27 #include "tools.h"
28 #include "rosters-tools.h"
29 #include "muc.h"
30 #include "disco.h"
31
32 /* SYNTAX: INVITE <jid>[/<resource>]|<name> [<channame>] */
33 static void
34 cmd_invite(const char *data, XMPP_SERVER_REC *server, WI_ITEM_REC *item)
35 {
36 MUC_REC *channel;
37 LmMessage *lmsg;
38 LmMessageNode *node, *invite_node;
39 GHashTable *optlist;
40 char *channame, *dest, *jid, *recoded;
41 void *free_arg;
42
43 CMD_XMPP_SERVER(server);
44 if (!cmd_get_params(data, &free_arg, 2 | PARAM_FLAG_OPTIONS,
45 "xmppinvite", &optlist, &dest, &channame))
46 return;
47 if (*dest == '\0')
48 cmd_param_error(CMDERR_NOT_ENOUGH_PARAMS);
49 if (*channame == '\0' || g_ascii_strcasecmp(channame, "*") == 0) {
50 if (!IS_MUC(item))
51 cmd_param_error(CMDERR_NOT_JOINED);
52 channame = MUC(item)->name;
53 }
54 if ((channel = muc_find(server, channame)) == NULL)
55 cmd_param_error(CMDERR_NOT_JOINED);
56 if ((jid = rosters_resolve_name(server, dest)) != NULL)
57 dest = jid;
58 recoded = xmpp_recode_out(channame);
59 lmsg = lm_message_new(recoded, LM_MESSAGE_TYPE_MESSAGE);
60 g_free(recoded);
61 node = lm_message_node_add_child(lmsg->node, "x", NULL);
62 lm_message_node_set_attribute(node, "xmlns", XMLNS_MUC_USER);
63 invite_node = lm_message_node_add_child(node, "invite", NULL);
64 recoded = xmpp_recode_out(dest);
65 lm_message_node_set_attribute(invite_node, "to", recoded);
66 g_free(recoded);
67 signal_emit("xmpp send message", 2, server, lmsg);
68 lm_message_unref(lmsg);
69 g_free(jid);
70 cmd_params_free(free_arg);
71 }
72
73 /* SYNTAX: PART [<channel] [<message>] */
74 static void
75 cmd_part(const char *data, XMPP_SERVER_REC *server, WI_ITEM_REC *item)
76 {
77 MUC_REC *channel;
78 char *channame, *reason;
79 void *free_arg;
80
81 CMD_XMPP_SERVER(server);
82 if (!cmd_get_params(data, &free_arg, 2 | PARAM_FLAG_GETREST |
83 PARAM_FLAG_OPTCHAN, item, &channame, &reason))
84 return;
85 if (*channame == '\0')
86 cmd_param_error(CMDERR_NOT_ENOUGH_PARAMS);
87 if ((channel = muc_find(server, channame)) == NULL)
88 cmd_param_error(CMDERR_NOT_JOINED);
89 if (*reason == '\0')
90 reason = (char *)settings_get_str("part_message");
91 muc_part(channel, reason);
92 cmd_params_free(free_arg);
93 }
94
95 /* SYNTAX: NICK [<channel>] <nick> */
96 static void
97 cmd_nick(const char *data, XMPP_SERVER_REC *server, WI_ITEM_REC *item)
98 {
99 MUC_REC *channel;
100 char *channame, *nick;
101 void *free_arg;
102
103 CMD_XMPP_SERVER(server);
104 if (!cmd_get_params(data, &free_arg, 2 | PARAM_FLAG_GETREST |
105 PARAM_FLAG_OPTCHAN, item, &channame, &nick))
106 return;
107 if (*channame == '\0' || *nick == '\0')
108 cmd_param_error(CMDERR_NOT_ENOUGH_PARAMS);
109 if ((channel = muc_find(server, channame)) == NULL)
110 cmd_param_error(CMDERR_NOT_JOINED);
111 muc_nick(channel, nick);
112 cmd_params_free(free_arg);
113 }
114
115 /* SYNTAX: TOPIC [-delete] [<channel>] [<topic>] */
116 static void
117 cmd_topic(const char *data, XMPP_SERVER_REC *server, WI_ITEM_REC *item)
118 {
119 GHashTable *optlist;
120 LmMessage *lmsg;
121 char *channame, *topic, *recoded;
122 void *free_arg;
123 gboolean delete;
124
125 CMD_XMPP_SERVER(server);
126 if (!cmd_get_params(data, &free_arg, 2 | PARAM_FLAG_OPTCHAN |
127 PARAM_FLAG_OPTIONS | PARAM_FLAG_GETREST, item, "topic", &optlist,
128 &channame, &topic))
129 return;
130 if (muc_find(server, channame) == NULL)
131 cmd_param_error(CMDERR_NOT_JOINED);
132 g_strstrip(topic);
133 delete = g_hash_table_lookup(optlist, "delete") != NULL;
134 if (*topic != '\0' || delete) {
135 recoded = xmpp_recode_out(channame);
136 lmsg = lm_message_new_with_sub_type(recoded,
137 LM_MESSAGE_TYPE_MESSAGE, LM_MESSAGE_SUB_TYPE_GROUPCHAT);
138 g_free(recoded);
139 if (delete)
140 lm_message_node_add_child(lmsg->node, "subject", NULL);
141 else {
142 recoded = xmpp_recode_out(topic);
143 lm_message_node_add_child(lmsg->node, "subject",
144 recoded);
145 g_free(recoded);
146 }
147 signal_emit("xmpp send message", 2, server, lmsg);
148 lm_message_unref(lmsg);
149 }
150 cmd_params_free(free_arg);
151 }
152
153 /* SYNTAX: AFFILIATION [<channel>] <type> [<jid>] [<reason>] */
154 static void
155 cmd_affiliation(const char *data, XMPP_SERVER_REC *server, WI_ITEM_REC *item)
156 {
157 MUC_REC *channel;
158 char *channame, *type, *jid, *reason;
159 void *free_arg;
160
161 CMD_XMPP_SERVER(server);
162 if (!cmd_get_params(data, &free_arg, 4 | PARAM_FLAG_OPTCHAN |
163 PARAM_FLAG_GETREST, item, &channame, &type, &jid, &reason))
164 return;
165 if (*channame == '\0' || *type == '\0')
166 cmd_param_error(CMDERR_NOT_ENOUGH_PARAMS);
167 if ((channel = muc_find(server, channame)) == NULL)
168 cmd_param_error(CMDERR_NOT_JOINED);
169 if (*jid == '\0') {
170 muc_get_affiliation(server, channel, type);
171 } else {
172 if (*reason == '\0')
173 reason = NULL;
174 muc_set_affiliation(server, channel, type, jid, reason);
175 }
176 cmd_params_free(free_arg);
177 }
178
179 /* SYNTAX: BAN [<channel>] <jid> [<reason>] */
180 static void
181 cmd_ban(const char *data, XMPP_SERVER_REC *server, WI_ITEM_REC *item)
182 {
183 MUC_REC *channel;
184 char *channame, *jid, *reason;
185 void *free_arg;
186
187 CMD_XMPP_SERVER(server);
188 if (!cmd_get_params(data, &free_arg, 3 | PARAM_FLAG_OPTCHAN |
189 PARAM_FLAG_GETREST, item, &channame, &jid, &reason))
190 return;
191 if (*channame == '\0' || *jid == '\0')
192 cmd_param_error(CMDERR_NOT_ENOUGH_PARAMS);
193 if ((channel = muc_find(server, channame)) == NULL)
194 cmd_param_error(CMDERR_NOT_JOINED);
195 if (*reason == '\0')
196 reason = NULL;
197
198 muc_set_affiliation(server, channel, "outcast", jid, reason);
199 cmd_params_free(free_arg);
200 }
201
202 /* SYNTAX: ROLE [<channel>] <type> [<nick>] [<reason>] */
203 static void
204 cmd_role(const char *data, XMPP_SERVER_REC *server, WI_ITEM_REC *item)
205 {
206 MUC_REC *channel;
207 char *channame, *type, *nick, *reason;
208 void *free_arg;
209
210 CMD_XMPP_SERVER(server);
211 if (!cmd_get_params(data, &free_arg, 4 | PARAM_FLAG_OPTCHAN |
212 PARAM_FLAG_GETREST, item, &channame, &type, &nick, &reason))
213 return;
214 if (*channame == '\0' || *type == '\0')
215 cmd_param_error(CMDERR_NOT_ENOUGH_PARAMS);
216 if ((channel = muc_find(server, channame)) == NULL)
217 cmd_param_error(CMDERR_NOT_JOINED);
218 if (*nick == '\0') {
219 muc_get_role(server, channel, type);
220 } else {
221 if (*reason == '\0')
222 reason = NULL;
223 muc_set_role(server, channel, type, nick, reason);
224 }
225 cmd_params_free(free_arg);
226 }
227
228 /* SYNTAX: kick [<channel>] <nick> [<reason>] */
229 static void
230 cmd_kick(const char *data, XMPP_SERVER_REC *server, WI_ITEM_REC *item)
231 {
232 MUC_REC *channel;
233 char *channame, *nick, *reason;
234 void *free_arg;
235
236 CMD_XMPP_SERVER(server);
237 if (!cmd_get_params(data, &free_arg, 3 | PARAM_FLAG_OPTCHAN |
238 PARAM_FLAG_GETREST, item, &channame, &nick, &reason))
239 return;
240 if (*channame == '\0' || *nick == '\0')
241 cmd_param_error(CMDERR_NOT_ENOUGH_PARAMS);
242 if ((channel = muc_find(server, channame)) == NULL)
243 cmd_param_error(CMDERR_NOT_JOINED);
244 if (*reason == '\0')
245 reason = NULL;
246 muc_set_role(server, channel, "none", nick, reason);
247 cmd_params_free(free_arg);
248 }
249
250 /* SYNTAX: MODE [<channel>] [<mode>] */
251 static void
252 cmd_mode(const char *data, XMPP_SERVER_REC *server, WI_ITEM_REC *item)
253 {
254 MUC_REC *channel;
255 char *channame, *mode;
256 void *free_arg;
257
258 CMD_XMPP_SERVER(server);
259 if (!cmd_get_params(data, &free_arg, 2 | PARAM_FLAG_OPTCHAN |
260 PARAM_FLAG_GETREST, item, &channame, &mode))
261 return;
262 if ((channel = muc_find(server, channame)) == NULL)
263 cmd_param_error(CMDERR_NOT_JOINED);
264 if (*mode == '\0')
265 disco_request(server, channel->name);
266 else
267 muc_set_mode(server, channel, mode);
268 cmd_params_free(free_arg);
269 }
270
271 /* SYNTAX: DESTROY [<channel>] [<alternate>] [<reason>]*/
272 static void
273 cmd_destroy(const char *data, XMPP_SERVER_REC *server, WI_ITEM_REC *item)
274 {
275 MUC_REC *channel;
276 char *channame, *reason, *alternate;
277 void *free_arg;
278
279 CMD_XMPP_SERVER(server);
280 if (!cmd_get_params(data, &free_arg, 3 | PARAM_FLAG_OPTCHAN |
281 PARAM_FLAG_GETREST, item, &channame, &alternate, &reason))
282 return;
283 if ((channel = muc_find(server, channame)) == NULL)
284 cmd_param_error(CMDERR_NOT_JOINED);
285 if (*alternate == '\0')
286 alternate = NULL;
287 if (*reason == '\0')
288 reason = NULL;
289 muc_destroy(server, channel, alternate, reason);
290 cmd_params_free(free_arg);
291 }
292
293 void
294 muc_commands_init(void)
295 {
296 command_bind_xmpp("invite", NULL, (SIGNAL_FUNC)cmd_invite);
297 command_set_options("invite", "yes");
298 command_bind_xmpp("part", NULL, (SIGNAL_FUNC)cmd_part);
299 command_bind_xmpp("nick", NULL, (SIGNAL_FUNC)cmd_nick);
300 command_bind_xmpp("topic", NULL, (SIGNAL_FUNC)cmd_topic);
301 command_bind_xmpp("affiliation", NULL, (SIGNAL_FUNC)cmd_affiliation);
302 command_bind_xmpp("ban", NULL, (SIGNAL_FUNC)cmd_ban);
303 command_bind_xmpp("role", NULL, (SIGNAL_FUNC)cmd_role);
304 command_bind_xmpp("kick", NULL, (SIGNAL_FUNC)cmd_kick);
305 command_bind_xmpp("mode", NULL, (SIGNAL_FUNC)cmd_mode);
306 command_bind_xmpp("destroy", NULL, (SIGNAL_FUNC)cmd_destroy);
307 }
308
309 void
310 muc_commands_deinit(void)
311 {
312 command_unbind("invite", (SIGNAL_FUNC)cmd_invite);
313 command_unbind("part", (SIGNAL_FUNC)cmd_part);
314 command_unbind("nick", (SIGNAL_FUNC)cmd_nick);
315 command_unbind("topic", (SIGNAL_FUNC)cmd_topic);
316 command_unbind("affiliation", (SIGNAL_FUNC)cmd_affiliation);
317 command_unbind("ban", (SIGNAL_FUNC)cmd_ban);
318 command_unbind("role", (SIGNAL_FUNC)cmd_role);
319 command_unbind("kick", (SIGNAL_FUNC)cmd_kick);
320 command_unbind("mode", (SIGNAL_FUNC)cmd_mode);
321 command_unbind("destroy", (SIGNAL_FUNC)cmd_destroy);
322 }