9130
|
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
|
9149
|
171 *error = NULL;
|
9130
|
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);
|
9149
|
225 err = NULL;
|
9130
|
226 gaim_cmd_free_args(args);
|
|
227 args = NULL;
|
|
228 continue;
|
|
229 } else {
|
|
230 break;
|
|
231 }
|
|
232
|
|
233 }
|
|
234
|
|
235 if (args)
|
|
236 gaim_cmd_free_args(args);
|
|
237 g_free(cmd);
|
|
238
|
|
239 if (!found)
|
|
240 return GAIM_CMD_STATUS_NOT_FOUND;
|
|
241
|
|
242 if (!right_type)
|
|
243 return GAIM_CMD_STATUS_WRONG_TYPE;
|
|
244 if (!right_prpl)
|
|
245 return GAIM_CMD_STATUS_WRONG_PRPL;
|
|
246 if (!tried_cmd)
|
|
247 return GAIM_CMD_STATUS_WRONG_ARGS;
|
|
248
|
|
249 if (ret == GAIM_CMD_RET_OK) {
|
|
250 return GAIM_CMD_STATUS_OK;
|
|
251 } else {
|
|
252 *error = err;
|
|
253 if (ret == GAIM_CMD_RET_CONTINUE)
|
|
254 return GAIM_CMD_STATUS_NOT_FOUND;
|
|
255 else
|
|
256 return GAIM_CMD_STATUS_FAILED;
|
|
257 }
|
|
258
|
|
259 }
|
|
260
|
|
261
|
|
262 GList *gaim_cmd_list(GaimConversation *conv)
|
|
263 {
|
|
264 GList *ret = NULL;
|
|
265 GaimCmd *c;
|
|
266 GList *l;
|
|
267
|
|
268 for (l = cmds; l; l = l->next) {
|
|
269 c = l->data;
|
|
270
|
|
271 if (conv && (gaim_conversation_get_type(conv) == GAIM_CONV_IM))
|
|
272 if (!(c->flags & GAIM_CMD_FLAG_IM))
|
|
273 continue;
|
|
274 if (conv && (gaim_conversation_get_type(conv) == GAIM_CONV_CHAT))
|
|
275 if (!(c->flags & GAIM_CMD_FLAG_CHAT))
|
|
276 continue;
|
|
277
|
|
278 if (conv && (c->flags & GAIM_CMD_FLAG_PRPL_ONLY) && c->prpl_id &&
|
|
279 (strcmp(c->prpl_id, gaim_account_get_protocol_id(gaim_conversation_get_account(conv))) != 0))
|
|
280 continue;
|
|
281
|
|
282 ret = g_list_append(ret, c->cmd);
|
|
283 }
|
|
284
|
|
285 return ret;
|
|
286 }
|
|
287
|
|
288
|
|
289 GList *gaim_cmd_help(GaimConversation *conv, const gchar *cmd)
|
|
290 {
|
|
291 GList *ret = NULL;
|
|
292 GaimCmd *c;
|
|
293 GList *l;
|
|
294
|
|
295 for (l = cmds; l; l = l->next) {
|
|
296 c = l->data;
|
|
297
|
|
298 if (cmd && (strcmp(cmd, c->cmd) != 0))
|
|
299 continue;
|
|
300
|
|
301 if (conv && (gaim_conversation_get_type(conv) == GAIM_CONV_IM))
|
|
302 if (!(c->flags & GAIM_CMD_FLAG_IM))
|
|
303 continue;
|
|
304 if (conv && (gaim_conversation_get_type(conv) == GAIM_CONV_CHAT))
|
|
305 if (!(c->flags & GAIM_CMD_FLAG_CHAT))
|
|
306 continue;
|
|
307
|
|
308 if (conv && (c->flags & GAIM_CMD_FLAG_PRPL_ONLY) && c->prpl_id &&
|
|
309 (strcmp(c->prpl_id, gaim_account_get_protocol_id(gaim_conversation_get_account(conv))) != 0))
|
|
310 continue;
|
|
311
|
|
312 ret = g_list_append(ret, c->help);
|
|
313 }
|
|
314
|
|
315 return ret;
|
|
316 }
|
|
317
|