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