These changes are reuired to make the Windows installer build.
[pidgin-git.git] / libpurple / cmds.c
blob6a495acd068c22191cbc70aed1d42547ba30a30c
1 /**
2 * @file cmds.c Commands API
3 * @ingroup core
4 */
6 /* Copyright (C) 2003-2004 Timothy Ringenbach <omarvo@hotmail.com
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA
24 #include "internal.h"
26 #include "account.h"
27 #include "util.h"
28 #include "cmds.h"
30 static GList *cmds = NULL;
31 static guint next_id = 1;
33 typedef struct _PurpleCmd {
34 PurpleCmdId id;
35 gchar *cmd;
36 gchar *args;
37 PurpleCmdPriority priority;
38 PurpleCmdFlag flags;
39 gchar *prpl_id;
40 PurpleCmdFunc func;
41 gchar *help;
42 void *data;
43 } PurpleCmd;
46 static gint cmds_compare_func(const PurpleCmd *a, const PurpleCmd *b)
48 if (a->priority > b->priority)
49 return -1;
50 else if (a->priority < b->priority)
51 return 1;
52 else return 0;
55 PurpleCmdId purple_cmd_register(const gchar *cmd, const gchar *args,
56 PurpleCmdPriority p, PurpleCmdFlag f,
57 const gchar *prpl_id, PurpleCmdFunc func,
58 const gchar *helpstr, void *data)
60 PurpleCmdId id;
61 PurpleCmd *c;
63 g_return_val_if_fail(cmd != NULL && *cmd != '\0', 0);
64 g_return_val_if_fail(args != NULL, 0);
65 g_return_val_if_fail(func != NULL, 0);
67 id = next_id++;
69 c = g_new0(PurpleCmd, 1);
70 c->id = id;
71 c->cmd = g_strdup(cmd);
72 c->args = g_strdup(args);
73 c->priority = p;
74 c->flags = f;
75 c->prpl_id = g_strdup(prpl_id);
76 c->func = func;
77 c->help = g_strdup(helpstr);
78 c->data = data;
80 cmds = g_list_insert_sorted(cmds, c, (GCompareFunc)cmds_compare_func);
82 purple_signal_emit(purple_cmds_get_handle(), "cmd-added", cmd, p, f);
84 return id;
87 static void purple_cmd_free(PurpleCmd *c)
89 g_free(c->cmd);
90 g_free(c->args);
91 g_free(c->prpl_id);
92 g_free(c->help);
93 g_free(c);
96 void purple_cmd_unregister(PurpleCmdId id)
98 PurpleCmd *c;
99 GList *l;
101 for (l = cmds; l; l = l->next) {
102 c = l->data;
104 if (c->id == id) {
105 cmds = g_list_remove(cmds, c);
106 purple_signal_emit(purple_cmds_get_handle(), "cmd-removed", c->cmd);
107 purple_cmd_free(c);
108 return;
114 * This sets args to a NULL-terminated array of strings. It should
115 * be freed using g_strfreev().
117 static gboolean purple_cmd_parse_args(PurpleCmd *cmd, const gchar *s, const gchar *m, gchar ***args)
119 int i;
120 const char *end, *cur;
122 *args = g_new0(char *, strlen(cmd->args) + 1);
124 cur = s;
126 for (i = 0; cmd->args[i]; i++) {
127 if (!*cur)
128 return (cmd->flags & PURPLE_CMD_FLAG_ALLOW_WRONG_ARGS);
130 switch (cmd->args[i]) {
131 case 'w':
132 if (!(end = strchr(cur, ' '))) {
133 end = cur + strlen(cur);
134 (*args)[i] = g_strndup(cur, end - cur);
135 cur = end;
136 } else {
137 (*args)[i] = g_strndup(cur, end - cur);
138 cur = end + 1;
140 break;
141 case 'W':
142 if (!(end = strchr(cur, ' '))) {
143 end = cur + strlen(cur);
144 (*args)[i] = purple_markup_slice(m, g_utf8_pointer_to_offset(s, cur), g_utf8_pointer_to_offset(s, end));
145 cur = end;
146 } else {
147 (*args)[i] = purple_markup_slice(m, g_utf8_pointer_to_offset(s, cur), g_utf8_pointer_to_offset(s, end));
148 cur = end +1;
150 break;
151 case 's':
152 (*args)[i] = g_strdup(cur);
153 cur = cur + strlen(cur);
154 break;
155 case 'S':
156 (*args)[i] = purple_markup_slice(m, g_utf8_pointer_to_offset(s, cur), g_utf8_strlen(cur, -1) + 1);
157 cur = cur + strlen(cur);
158 break;
162 if (*cur)
163 return (cmd->flags & PURPLE_CMD_FLAG_ALLOW_WRONG_ARGS);
165 return TRUE;
168 static void purple_cmd_strip_current_char(gunichar c, char *s, guint len)
170 int bytes;
172 bytes = g_unichar_to_utf8(c, NULL);
173 memmove(s, s + bytes, len + 1 - bytes);
176 static void purple_cmd_strip_cmd_from_markup(char *markup)
178 guint len = strlen(markup);
179 char *s = markup;
181 while (*s) {
182 gunichar c = g_utf8_get_char(s);
184 if (c == '<') {
185 s = strchr(s, '>');
186 if (!s)
187 return;
188 } else if (g_unichar_isspace(c)) {
189 purple_cmd_strip_current_char(c, s, len - (s - markup));
190 return;
191 } else {
192 purple_cmd_strip_current_char(c, s, len - (s - markup));
193 continue;
195 s = g_utf8_next_char(s);
199 PurpleCmdStatus purple_cmd_do_command(PurpleConversation *conv, const gchar *cmdline,
200 const gchar *markup, gchar **error)
202 PurpleCmd *c;
203 GList *l;
204 gchar *err = NULL;
205 gboolean is_im;
206 gboolean found = FALSE, tried_cmd = FALSE, right_type = FALSE, right_prpl = FALSE;
207 const gchar *prpl_id;
208 gchar **args = NULL;
209 gchar *cmd, *rest, *mrest;
210 PurpleCmdRet ret = PURPLE_CMD_RET_CONTINUE;
212 *error = NULL;
213 prpl_id = purple_account_get_protocol_id(purple_conversation_get_account(conv));
215 if (purple_conversation_get_type(conv) == PURPLE_CONV_TYPE_IM)
216 is_im = TRUE;
217 else if (purple_conversation_get_type(conv) == PURPLE_CONV_TYPE_CHAT)
218 is_im = FALSE;
219 else
220 return PURPLE_CMD_STATUS_FAILED;
222 rest = strchr(cmdline, ' ');
223 if (rest) {
224 cmd = g_strndup(cmdline, rest - cmdline);
225 rest++;
226 } else {
227 cmd = g_strdup(cmdline);
228 rest = "";
231 mrest = g_strdup(markup);
232 purple_cmd_strip_cmd_from_markup(mrest);
234 for (l = cmds; l; l = l->next) {
235 c = l->data;
237 if (!purple_strequal(c->cmd, cmd))
238 continue;
240 found = TRUE;
242 if (is_im)
243 if (!(c->flags & PURPLE_CMD_FLAG_IM))
244 continue;
245 if (!is_im)
246 if (!(c->flags & PURPLE_CMD_FLAG_CHAT))
247 continue;
249 right_type = TRUE;
251 if ((c->flags & PURPLE_CMD_FLAG_PRPL_ONLY) &&
252 !purple_strequal(c->prpl_id, prpl_id))
253 continue;
255 right_prpl = TRUE;
257 /* this checks the allow bad args flag for us */
258 if (!purple_cmd_parse_args(c, rest, mrest, &args)) {
259 g_strfreev(args);
260 args = NULL;
261 continue;
264 tried_cmd = TRUE;
265 ret = c->func(conv, cmd, args, &err, c->data);
266 if (ret == PURPLE_CMD_RET_CONTINUE) {
267 g_free(err);
268 err = NULL;
269 g_strfreev(args);
270 args = NULL;
271 continue;
272 } else {
273 break;
278 g_strfreev(args);
279 g_free(cmd);
280 g_free(mrest);
282 if (!found)
283 return PURPLE_CMD_STATUS_NOT_FOUND;
285 if (!right_type)
286 return PURPLE_CMD_STATUS_WRONG_TYPE;
287 if (!right_prpl)
288 return PURPLE_CMD_STATUS_WRONG_PRPL;
289 if (!tried_cmd)
290 return PURPLE_CMD_STATUS_WRONG_ARGS;
292 if (ret == PURPLE_CMD_RET_OK) {
293 return PURPLE_CMD_STATUS_OK;
294 } else {
295 *error = err;
296 if (ret == PURPLE_CMD_RET_CONTINUE)
297 return PURPLE_CMD_STATUS_NOT_FOUND;
298 else
299 return PURPLE_CMD_STATUS_FAILED;
305 GList *purple_cmd_list(PurpleConversation *conv)
307 GList *ret = NULL;
308 PurpleCmd *c;
309 GList *l;
311 for (l = cmds; l; l = l->next) {
312 c = l->data;
314 if (conv && (purple_conversation_get_type(conv) == PURPLE_CONV_TYPE_IM))
315 if (!(c->flags & PURPLE_CMD_FLAG_IM))
316 continue;
317 if (conv && (purple_conversation_get_type(conv) == PURPLE_CONV_TYPE_CHAT))
318 if (!(c->flags & PURPLE_CMD_FLAG_CHAT))
319 continue;
321 if (conv && (c->flags & PURPLE_CMD_FLAG_PRPL_ONLY) &&
322 !purple_strequal(c->prpl_id, purple_account_get_protocol_id(purple_conversation_get_account(conv))))
323 continue;
325 ret = g_list_append(ret, c->cmd);
328 ret = g_list_sort(ret, (GCompareFunc)strcmp);
330 return ret;
334 GList *purple_cmd_help(PurpleConversation *conv, const gchar *cmd)
336 GList *ret = NULL;
337 PurpleCmd *c;
338 GList *l;
340 for (l = cmds; l; l = l->next) {
341 c = l->data;
343 if (cmd && !purple_strequal(cmd, c->cmd))
344 continue;
346 if (conv && (purple_conversation_get_type(conv) == PURPLE_CONV_TYPE_IM))
347 if (!(c->flags & PURPLE_CMD_FLAG_IM))
348 continue;
349 if (conv && (purple_conversation_get_type(conv) == PURPLE_CONV_TYPE_CHAT))
350 if (!(c->flags & PURPLE_CMD_FLAG_CHAT))
351 continue;
353 if (conv && (c->flags & PURPLE_CMD_FLAG_PRPL_ONLY) &&
354 !purple_strequal(c->prpl_id, purple_account_get_protocol_id(purple_conversation_get_account(conv))))
355 continue;
357 ret = g_list_append(ret, c->help);
360 ret = g_list_sort(ret, (GCompareFunc)strcmp);
362 return ret;
365 gpointer purple_cmds_get_handle(void)
367 static int handle;
368 return &handle;
371 void purple_cmds_init(void)
373 gpointer handle = purple_cmds_get_handle();
375 purple_signal_register(handle, "cmd-added",
376 purple_marshal_VOID__POINTER_INT_INT, NULL, 3,
377 purple_value_new(PURPLE_TYPE_STRING),
378 purple_value_new(PURPLE_TYPE_INT),
379 purple_value_new(PURPLE_TYPE_INT));
380 purple_signal_register(handle, "cmd-removed",
381 purple_marshal_VOID__POINTER, NULL, 1,
382 purple_value_new(PURPLE_TYPE_STRING));
385 void purple_cmds_uninit(void)
387 purple_signals_unregister_by_instance(purple_cmds_get_handle());