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"
|
9175
|
26 #include "util.h"
|
9130
|
27 #include "cmds.h"
|
|
28
|
|
29 static GList *cmds = NULL;
|
|
30 static guint next_id = 1;
|
|
31
|
|
32 typedef struct _GaimCmd {
|
|
33 GaimCmdId id;
|
|
34 gchar *cmd;
|
|
35 gchar *args;
|
|
36 GaimCmdPriority priority;
|
|
37 GaimCmdFlag flags;
|
|
38 gchar *prpl_id;
|
|
39 GaimCmdFunc func;
|
|
40 gchar *help;
|
9597
|
41 void *data;
|
9130
|
42 } GaimCmd;
|
|
43
|
|
44
|
|
45 static gint cmds_compare_func(const GaimCmd *a, const GaimCmd *b)
|
|
46 {
|
10504
|
47 if (a->priority > b->priority)
|
9130
|
48 return -1;
|
10504
|
49 else if (a->priority < b->priority)
|
9130
|
50 return 1;
|
|
51 else return 0;
|
|
52 }
|
|
53
|
9597
|
54 GaimCmdId gaim_cmd_register(const gchar *cmd, const gchar *args,
|
|
55 GaimCmdPriority p, GaimCmdFlag f,
|
|
56 const gchar *prpl_id, GaimCmdFunc func,
|
|
57 const gchar *helpstr, void *data)
|
9130
|
58 {
|
9343
|
59 GaimCmdId id;
|
9130
|
60 GaimCmd *c;
|
|
61
|
9343
|
62 g_return_val_if_fail(cmd != NULL && *cmd != '\0', 0);
|
|
63 g_return_val_if_fail(args != NULL, 0);
|
|
64 g_return_val_if_fail(func != NULL, 0);
|
9130
|
65
|
9343
|
66 id = next_id++;
|
9130
|
67
|
|
68 c = g_new0(GaimCmd, 1);
|
9343
|
69 c->id = id;
|
9130
|
70 c->cmd = g_strdup(cmd);
|
|
71 c->args = g_strdup(args);
|
|
72 c->priority = p;
|
|
73 c->flags = f;
|
|
74 c->prpl_id = prpl_id ? g_strdup(prpl_id) : NULL;
|
|
75 c->func = func;
|
|
76 c->help = helpstr ? g_strdup(helpstr) : NULL;
|
9597
|
77 c->data = data;
|
9130
|
78
|
|
79 cmds = g_list_insert_sorted(cmds, c, (GCompareFunc)cmds_compare_func);
|
|
80
|
|
81 return id;
|
|
82 }
|
|
83
|
|
84 static void gaim_cmd_free(GaimCmd *c)
|
|
85 {
|
|
86 g_free(c->cmd);
|
|
87 g_free(c->args);
|
|
88 if (c->prpl_id)
|
|
89 g_free(c->prpl_id);
|
|
90 if (c->help)
|
|
91 g_free(c->help);
|
|
92
|
|
93 g_free(c);
|
|
94 }
|
|
95
|
9343
|
96 void gaim_cmd_unregister(GaimCmdId id)
|
9130
|
97 {
|
|
98 GaimCmd *c;
|
|
99 GList *l;
|
|
100
|
|
101 for (l = cmds; l; l = l->next) {
|
|
102 c = l->data;
|
|
103
|
9343
|
104 if (c->id == id) {
|
9130
|
105 cmds = g_list_remove(cmds, c);
|
|
106 gaim_cmd_free(c);
|
|
107 return;
|
|
108 }
|
|
109 }
|
|
110 }
|
|
111
|
9175
|
112 static gboolean gaim_cmd_parse_args(GaimCmd *cmd, const gchar *s, const gchar *m, gchar ***args)
|
9130
|
113 {
|
|
114 int i;
|
|
115 const char *end, *cur;
|
|
116
|
|
117 *args = g_new0(char *, strlen(cmd->args) + 1);
|
|
118
|
|
119 cur = s;
|
|
120
|
|
121 for (i = 0; cmd->args[i]; i++) {
|
|
122 if (!*cur)
|
|
123 return (cmd->flags & GAIM_CMD_FLAG_ALLOW_WRONG_ARGS);
|
|
124
|
|
125 switch (cmd->args[i]) {
|
|
126 case 'w':
|
9428
|
127 if (!(end = strchr(cur, ' '))) {
|
|
128 end = cur + strlen(cur);
|
|
129 (*args)[i] = g_strndup(cur, end - cur);
|
|
130 cur = end;
|
|
131 } else {
|
|
132 (*args)[i] = g_strndup(cur, end - cur);
|
|
133 cur = end + 1;
|
|
134 }
|
9130
|
135 break;
|
|
136 case 'W':
|
9428
|
137 if (!(end = strchr(cur, ' '))) {
|
|
138 end = cur + strlen(cur);
|
|
139 (*args)[i] = gaim_markup_slice(m, g_utf8_pointer_to_offset(s, cur), g_utf8_pointer_to_offset(s, end));
|
|
140 cur = end;
|
|
141 } else {
|
|
142 (*args)[i] = gaim_markup_slice(m, g_utf8_pointer_to_offset(s, cur), g_utf8_pointer_to_offset(s, end));
|
|
143 cur = end +1;
|
|
144 }
|
|
145 break;
|
9130
|
146 case 's':
|
|
147 (*args)[i] = g_strdup(cur);
|
|
148 cur = cur + strlen(cur);
|
|
149 break;
|
|
150 case 'S':
|
9175
|
151 (*args)[i] = gaim_markup_slice(m, g_utf8_pointer_to_offset(s, cur), g_utf8_strlen(cur, -1) + 1);
|
9130
|
152 cur = cur + strlen(cur);
|
|
153 break;
|
|
154 }
|
|
155 }
|
|
156
|
|
157 if (*cur)
|
|
158 return (cmd->flags & GAIM_CMD_FLAG_ALLOW_WRONG_ARGS);
|
|
159
|
|
160 return TRUE;
|
|
161 }
|
|
162
|
|
163 static void gaim_cmd_free_args(gchar **args)
|
|
164 {
|
|
165 int i;
|
|
166
|
|
167 for (i = 0; args[i]; i++)
|
|
168 g_free(args[i]);
|
|
169 g_free(args);
|
|
170 }
|
|
171
|
9175
|
172 static void gaim_cmd_strip_current_char(gunichar c, char *s, guint len)
|
|
173 {
|
|
174 int bytes;
|
|
175
|
|
176 bytes = g_unichar_to_utf8(c, NULL);
|
|
177 memmove(s, s + bytes, len + 1 - bytes);
|
|
178 }
|
|
179
|
|
180 static void gaim_cmd_strip_cmd_from_markup(char *markup)
|
|
181 {
|
|
182 guint len = strlen(markup);
|
|
183 char *s = markup;
|
|
184
|
|
185 while (*s) {
|
|
186 gunichar c = g_utf8_get_char(s);
|
|
187
|
|
188 if (c == '<') {
|
|
189 s = strchr(s, '>');
|
|
190 if (!s)
|
|
191 return;
|
|
192 } else if (g_unichar_isspace(c)) {
|
|
193 gaim_cmd_strip_current_char(c, s, len - (s - markup));
|
|
194 return;
|
|
195 } else {
|
|
196 gaim_cmd_strip_current_char(c, s, len - (s - markup));
|
|
197 continue;
|
|
198 }
|
|
199 s = g_utf8_next_char(s);
|
|
200 }
|
|
201 }
|
9597
|
202
|
9175
|
203 GaimCmdStatus gaim_cmd_do_command(GaimConversation *conv, const gchar *cmdline,
|
|
204 const gchar *markup, gchar **error)
|
9130
|
205 {
|
|
206 GaimCmd *c;
|
|
207 GList *l;
|
|
208 gchar *err = NULL;
|
|
209 gboolean is_im;
|
|
210 gboolean found = FALSE, tried_cmd = FALSE, right_type = FALSE, right_prpl = FALSE;
|
|
211 const gchar *prpl_id;
|
|
212 gchar **args = NULL;
|
9175
|
213 gchar *cmd, *rest, *mrest;
|
9130
|
214 GaimCmdRet ret = GAIM_CMD_RET_CONTINUE;
|
|
215
|
9149
|
216 *error = NULL;
|
9130
|
217 prpl_id = gaim_account_get_protocol_id(gaim_conversation_get_account(conv));
|
|
218
|
|
219 if (gaim_conversation_get_type(conv) == GAIM_CONV_IM)
|
|
220 is_im = TRUE;
|
|
221 else if (gaim_conversation_get_type(conv) == GAIM_CONV_CHAT)
|
|
222 is_im = FALSE;
|
|
223 else
|
|
224 return GAIM_CMD_STATUS_FAILED;
|
|
225
|
|
226 rest = strchr(cmdline, ' ');
|
|
227 if (rest) {
|
|
228 cmd = g_strndup(cmdline, rest - cmdline);
|
|
229 rest++;
|
|
230 } else {
|
|
231 cmd = g_strdup(cmdline);
|
|
232 rest = "";
|
|
233 }
|
|
234
|
9175
|
235 mrest = g_strdup(markup);
|
|
236 gaim_cmd_strip_cmd_from_markup(mrest);
|
|
237
|
9130
|
238 for (l = cmds; l; l = l->next) {
|
|
239 c = l->data;
|
|
240
|
|
241 if (strcmp(c->cmd, cmd) != 0)
|
|
242 continue;
|
|
243
|
|
244 found = TRUE;
|
|
245
|
|
246 if (is_im)
|
|
247 if (!(c->flags & GAIM_CMD_FLAG_IM))
|
|
248 continue;
|
|
249 if (!is_im)
|
|
250 if (!(c->flags & GAIM_CMD_FLAG_CHAT))
|
|
251 continue;
|
|
252
|
|
253 right_type = TRUE;
|
|
254
|
|
255 if ((c->flags & GAIM_CMD_FLAG_PRPL_ONLY) && c->prpl_id &&
|
|
256 (strcmp(c->prpl_id, prpl_id) != 0))
|
|
257 continue;
|
|
258
|
|
259 right_prpl = TRUE;
|
|
260
|
|
261 /* this checks the allow bad args flag for us */
|
9175
|
262 if (!gaim_cmd_parse_args(c, rest, mrest, &args)) {
|
9130
|
263 gaim_cmd_free_args(args);
|
|
264 args = NULL;
|
|
265 continue;
|
|
266 }
|
|
267
|
|
268 tried_cmd = TRUE;
|
9597
|
269 ret = c->func(conv, cmd, args, &err, c->data);
|
9130
|
270 if (ret == GAIM_CMD_RET_CONTINUE) {
|
|
271 if (err)
|
|
272 g_free(err);
|
9149
|
273 err = NULL;
|
9130
|
274 gaim_cmd_free_args(args);
|
|
275 args = NULL;
|
|
276 continue;
|
|
277 } else {
|
|
278 break;
|
|
279 }
|
|
280
|
|
281 }
|
|
282
|
|
283 if (args)
|
|
284 gaim_cmd_free_args(args);
|
|
285 g_free(cmd);
|
9175
|
286 g_free(mrest);
|
9130
|
287
|
|
288 if (!found)
|
|
289 return GAIM_CMD_STATUS_NOT_FOUND;
|
|
290
|
|
291 if (!right_type)
|
|
292 return GAIM_CMD_STATUS_WRONG_TYPE;
|
|
293 if (!right_prpl)
|
|
294 return GAIM_CMD_STATUS_WRONG_PRPL;
|
|
295 if (!tried_cmd)
|
|
296 return GAIM_CMD_STATUS_WRONG_ARGS;
|
|
297
|
|
298 if (ret == GAIM_CMD_RET_OK) {
|
|
299 return GAIM_CMD_STATUS_OK;
|
|
300 } else {
|
|
301 *error = err;
|
|
302 if (ret == GAIM_CMD_RET_CONTINUE)
|
|
303 return GAIM_CMD_STATUS_NOT_FOUND;
|
|
304 else
|
|
305 return GAIM_CMD_STATUS_FAILED;
|
|
306 }
|
|
307
|
|
308 }
|
|
309
|
|
310
|
|
311 GList *gaim_cmd_list(GaimConversation *conv)
|
|
312 {
|
|
313 GList *ret = NULL;
|
|
314 GaimCmd *c;
|
|
315 GList *l;
|
|
316
|
|
317 for (l = cmds; l; l = l->next) {
|
|
318 c = l->data;
|
|
319
|
|
320 if (conv && (gaim_conversation_get_type(conv) == GAIM_CONV_IM))
|
|
321 if (!(c->flags & GAIM_CMD_FLAG_IM))
|
|
322 continue;
|
|
323 if (conv && (gaim_conversation_get_type(conv) == GAIM_CONV_CHAT))
|
|
324 if (!(c->flags & GAIM_CMD_FLAG_CHAT))
|
|
325 continue;
|
|
326
|
|
327 if (conv && (c->flags & GAIM_CMD_FLAG_PRPL_ONLY) && c->prpl_id &&
|
|
328 (strcmp(c->prpl_id, gaim_account_get_protocol_id(gaim_conversation_get_account(conv))) != 0))
|
|
329 continue;
|
|
330
|
|
331 ret = g_list_append(ret, c->cmd);
|
|
332 }
|
|
333
|
|
334 return ret;
|
|
335 }
|
|
336
|
|
337
|
|
338 GList *gaim_cmd_help(GaimConversation *conv, const gchar *cmd)
|
|
339 {
|
|
340 GList *ret = NULL;
|
|
341 GaimCmd *c;
|
|
342 GList *l;
|
|
343
|
|
344 for (l = cmds; l; l = l->next) {
|
|
345 c = l->data;
|
|
346
|
|
347 if (cmd && (strcmp(cmd, c->cmd) != 0))
|
|
348 continue;
|
|
349
|
|
350 if (conv && (gaim_conversation_get_type(conv) == GAIM_CONV_IM))
|
|
351 if (!(c->flags & GAIM_CMD_FLAG_IM))
|
|
352 continue;
|
|
353 if (conv && (gaim_conversation_get_type(conv) == GAIM_CONV_CHAT))
|
|
354 if (!(c->flags & GAIM_CMD_FLAG_CHAT))
|
|
355 continue;
|
|
356
|
|
357 if (conv && (c->flags & GAIM_CMD_FLAG_PRPL_ONLY) && c->prpl_id &&
|
|
358 (strcmp(c->prpl_id, gaim_account_get_protocol_id(gaim_conversation_get_account(conv))) != 0))
|
|
359 continue;
|
|
360
|
|
361 ret = g_list_append(ret, c->help);
|
|
362 }
|
|
363
|
|
364 return ret;
|
|
365 }
|
|
366
|