comparison src/cmds.c @ 9130:933a19e3a6b3

[gaim-migrate @ 9908] This puts the core in charge of irc-style /commands, which is way cool. Tim did most of the work, I've just been keeping it in sync with CVS, and slowly adding more commands to jabber. committer: Tailor Script <tailor@pidgin.im>
author Nathan Walp <nwalp@pidgin.im>
date Sun, 30 May 2004 19:34:21 +0000
parents
children 108a0300135d
comparison
equal deleted inserted replaced
9129:3e94a77ee0c7 9130:933a19e3a6b3
1 /**
2 * @file cmds.c Commands API
3 * @ingroup core
4 *
5 * Copyright (C) 2003-2004 Timothy Ringenbach <omarvo@hotmail.com
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 *
21 */
22
23 #include <string.h>
24
25 #include "account.h"
26 #include "cmds.h"
27
28 static GList *cmds = NULL;
29 static guint next_id = 1;
30
31 typedef struct _GaimCmd {
32 GaimCmdId id;
33 gchar *cmd;
34 gchar *args;
35 GaimCmdPriority priority;
36 GaimCmdFlag flags;
37 gchar *prpl_id;
38 GaimCmdFunc func;
39 gchar *help;
40 } GaimCmd;
41
42
43 static gint cmds_compare_func(const GaimCmd *a, const GaimCmd *b)
44 {
45 if (a->id > b->id)
46 return -1;
47 else if (a->id < b->id)
48 return 1;
49 else return 0;
50 }
51
52 GaimCmdId *gaim_cmd_register(const gchar *cmd, const gchar *args, GaimCmdPriority p, GaimCmdFlag f,
53 const gchar *prpl_id, GaimCmdFunc func, const gchar *helpstr)
54 {
55 GaimCmdId *id;
56 GaimCmd *c;
57
58 g_return_val_if_fail(cmd != NULL && *cmd != '\0', NULL);
59 g_return_val_if_fail(args != NULL, NULL);
60 g_return_val_if_fail(func != NULL, NULL);
61
62 id = g_new(GaimCmdId, 1);
63 *id = next_id++;
64
65 c = g_new0(GaimCmd, 1);
66 c->id = *id;
67 c->cmd = g_strdup(cmd);
68 c->args = g_strdup(args);
69 c->priority = p;
70 c->flags = f;
71 c->prpl_id = prpl_id ? g_strdup(prpl_id) : NULL;
72 c->func = func;
73 c->help = helpstr ? g_strdup(helpstr) : NULL;
74
75 cmds = g_list_insert_sorted(cmds, c, (GCompareFunc)cmds_compare_func);
76
77 return id;
78 }
79
80 static void gaim_cmd_free(GaimCmd *c)
81 {
82 g_free(c->cmd);
83 g_free(c->args);
84 if (c->prpl_id)
85 g_free(c->prpl_id);
86 if (c->help)
87 g_free(c->help);
88
89 g_free(c);
90 }
91
92 void gaim_cmd_unregister(GaimCmdId *id)
93 {
94 GaimCmd *c;
95 GList *l;
96
97 for (l = cmds; l; l = l->next) {
98 c = l->data;
99
100 if (c->id == *id) {
101 cmds = g_list_remove(cmds, c);
102 gaim_cmd_free(c);
103 g_free(id);
104 return;
105 }
106 }
107 }
108
109 static gboolean gaim_cmd_parse_args(GaimCmd *cmd, const gchar *s, gchar ***args)
110 {
111 int i;
112 const char *end, *cur;
113
114 *args = g_new0(char *, strlen(cmd->args) + 1);
115
116 cur = s;
117
118 for (i = 0; cmd->args[i]; i++) {
119 if (!*cur)
120 return (cmd->flags & GAIM_CMD_FLAG_ALLOW_WRONG_ARGS);
121
122 switch (cmd->args[i]) {
123 case 'w':
124 if (!(end = strchr(cur, ' '))) end = cur + strlen(cur);
125 (*args)[i] = g_strndup(cur, end - cur);
126 cur += end - cur;
127 break;
128 case 'W':
129 if (!(end = strchr(cur, ' '))) end = cur + strlen(cur);
130 (*args)[i] = g_strndup(cur, end - cur); /* XXX apply default formatting here */
131 cur += end - cur;
132 break;
133 case 's':
134 (*args)[i] = g_strdup(cur);
135 cur = cur + strlen(cur);
136 break;
137 case 'S':
138 (*args)[i] = g_strdup(cur); /* XXX apply default formatting here */
139 cur = cur + strlen(cur);
140 break;
141 }
142 }
143
144 if (*cur)
145 return (cmd->flags & GAIM_CMD_FLAG_ALLOW_WRONG_ARGS);
146
147 return TRUE;
148 }
149
150 static void gaim_cmd_free_args(gchar **args)
151 {
152 int i;
153
154 for (i = 0; args[i]; i++)
155 g_free(args[i]);
156 g_free(args);
157 }
158
159 GaimCmdStatus gaim_cmd_do_command(GaimConversation *conv, const gchar *cmdline, gchar **error)
160 {
161 GaimCmd *c;
162 GList *l;
163 gchar *err = NULL;
164 gboolean is_im;
165 gboolean found = FALSE, tried_cmd = FALSE, right_type = FALSE, right_prpl = FALSE;
166 const gchar *prpl_id;
167 gchar **args = NULL;
168 gchar *cmd, *rest;
169 GaimCmdRet ret = GAIM_CMD_RET_CONTINUE;
170
171 error = NULL;
172 prpl_id = gaim_account_get_protocol_id(gaim_conversation_get_account(conv));
173
174 if (gaim_conversation_get_type(conv) == GAIM_CONV_IM)
175 is_im = TRUE;
176 else if (gaim_conversation_get_type(conv) == GAIM_CONV_CHAT)
177 is_im = FALSE;
178 else
179 return GAIM_CMD_STATUS_FAILED;
180
181 rest = strchr(cmdline, ' ');
182 if (rest) {
183 cmd = g_strndup(cmdline, rest - cmdline);
184 rest++;
185 } else {
186 cmd = g_strdup(cmdline);
187 rest = "";
188 }
189
190 for (l = cmds; l; l = l->next) {
191 c = l->data;
192
193 if (strcmp(c->cmd, cmd) != 0)
194 continue;
195
196 found = TRUE;
197
198 if (is_im)
199 if (!(c->flags & GAIM_CMD_FLAG_IM))
200 continue;
201 if (!is_im)
202 if (!(c->flags & GAIM_CMD_FLAG_CHAT))
203 continue;
204
205 right_type = TRUE;
206
207 if ((c->flags & GAIM_CMD_FLAG_PRPL_ONLY) && c->prpl_id &&
208 (strcmp(c->prpl_id, prpl_id) != 0))
209 continue;
210
211 right_prpl = TRUE;
212
213 /* this checks the allow bad args flag for us */
214 if (!gaim_cmd_parse_args(c, rest, &args)) {
215 gaim_cmd_free_args(args);
216 args = NULL;
217 continue;
218 }
219
220 tried_cmd = TRUE;
221 ret = c->func(conv, cmd, args, &err);
222 if (ret == GAIM_CMD_RET_CONTINUE) {
223 if (err)
224 g_free(err);
225 gaim_cmd_free_args(args);
226 args = NULL;
227 continue;
228 } else {
229 break;
230 }
231
232 }
233
234 if (args)
235 gaim_cmd_free_args(args);
236 g_free(cmd);
237
238 if (!found)
239 return GAIM_CMD_STATUS_NOT_FOUND;
240
241 if (!right_type)
242 return GAIM_CMD_STATUS_WRONG_TYPE;
243 if (!right_prpl)
244 return GAIM_CMD_STATUS_WRONG_PRPL;
245 if (!tried_cmd)
246 return GAIM_CMD_STATUS_WRONG_ARGS;
247
248 if (ret == GAIM_CMD_RET_OK) {
249 return GAIM_CMD_STATUS_OK;
250 } else {
251 *error = err;
252 if (ret == GAIM_CMD_RET_CONTINUE)
253 return GAIM_CMD_STATUS_NOT_FOUND;
254 else
255 return GAIM_CMD_STATUS_FAILED;
256 }
257
258 }
259
260
261 GList *gaim_cmd_list(GaimConversation *conv)
262 {
263 GList *ret = NULL;
264 GaimCmd *c;
265 GList *l;
266
267 for (l = cmds; l; l = l->next) {
268 c = l->data;
269
270 if (conv && (gaim_conversation_get_type(conv) == GAIM_CONV_IM))
271 if (!(c->flags & GAIM_CMD_FLAG_IM))
272 continue;
273 if (conv && (gaim_conversation_get_type(conv) == GAIM_CONV_CHAT))
274 if (!(c->flags & GAIM_CMD_FLAG_CHAT))
275 continue;
276
277 if (conv && (c->flags & GAIM_CMD_FLAG_PRPL_ONLY) && c->prpl_id &&
278 (strcmp(c->prpl_id, gaim_account_get_protocol_id(gaim_conversation_get_account(conv))) != 0))
279 continue;
280
281 ret = g_list_append(ret, c->cmd);
282 }
283
284 return ret;
285 }
286
287
288 GList *gaim_cmd_help(GaimConversation *conv, const gchar *cmd)
289 {
290 GList *ret = NULL;
291 GaimCmd *c;
292 GList *l;
293
294 for (l = cmds; l; l = l->next) {
295 c = l->data;
296
297 if (cmd && (strcmp(cmd, c->cmd) != 0))
298 continue;
299
300 if (conv && (gaim_conversation_get_type(conv) == GAIM_CONV_IM))
301 if (!(c->flags & GAIM_CMD_FLAG_IM))
302 continue;
303 if (conv && (gaim_conversation_get_type(conv) == GAIM_CONV_CHAT))
304 if (!(c->flags & GAIM_CMD_FLAG_CHAT))
305 continue;
306
307 if (conv && (c->flags & GAIM_CMD_FLAG_PRPL_ONLY) && c->prpl_id &&
308 (strcmp(c->prpl_id, gaim_account_get_protocol_id(gaim_conversation_get_account(conv))) != 0))
309 continue;
310
311 ret = g_list_append(ret, c->help);
312 }
313
314 return ret;
315 }
316