Sun, 11 Jul 2004 18:29:47 +0000
[gaim-migrate @ 10335]
Another victory for the request API. 3.14 penguin points for Chip!
| 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': | |
| 9428 | 123 | if (!(end = strchr(cur, ' '))) { |
| 124 | end = cur + strlen(cur); | |
| 125 | (*args)[i] = g_strndup(cur, end - cur); | |
| 126 | cur = end; | |
| 127 | } else { | |
| 128 | (*args)[i] = g_strndup(cur, end - cur); | |
| 129 | cur = end + 1; | |
| 130 | } | |
| 9130 | 131 | break; |
| 132 | case 'W': | |
| 9428 | 133 | if (!(end = strchr(cur, ' '))) { |
| 134 | end = cur + strlen(cur); | |
| 135 | (*args)[i] = gaim_markup_slice(m, g_utf8_pointer_to_offset(s, cur), g_utf8_pointer_to_offset(s, end)); | |
| 136 | cur = end; | |
| 137 | } else { | |
| 138 | (*args)[i] = gaim_markup_slice(m, g_utf8_pointer_to_offset(s, cur), g_utf8_pointer_to_offset(s, end)); | |
| 139 | cur = end +1; | |
| 140 | } | |
| 141 | break; | |
| 9130 | 142 | case 's': |
| 143 | (*args)[i] = g_strdup(cur); | |
| 144 | cur = cur + strlen(cur); | |
| 145 | break; | |
| 146 | case 'S': | |
| 9175 | 147 | (*args)[i] = gaim_markup_slice(m, g_utf8_pointer_to_offset(s, cur), g_utf8_strlen(cur, -1) + 1); |
| 9130 | 148 | cur = cur + strlen(cur); |
| 149 | break; | |
| 150 | } | |
| 151 | } | |
| 152 | ||
| 153 | if (*cur) | |
| 154 | return (cmd->flags & GAIM_CMD_FLAG_ALLOW_WRONG_ARGS); | |
| 155 | ||
| 156 | return TRUE; | |
| 157 | } | |
| 158 | ||
| 159 | static void gaim_cmd_free_args(gchar **args) | |
| 160 | { | |
| 161 | int i; | |
| 162 | ||
| 163 | for (i = 0; args[i]; i++) | |
| 164 | g_free(args[i]); | |
| 165 | g_free(args); | |
| 166 | } | |
| 167 | ||
| 9175 | 168 | static void gaim_cmd_strip_current_char(gunichar c, char *s, guint len) |
| 169 | { | |
| 170 | int bytes; | |
| 171 | ||
| 172 | bytes = g_unichar_to_utf8(c, NULL); | |
| 173 | memmove(s, s + bytes, len + 1 - bytes); | |
| 174 | } | |
| 175 | ||
| 176 | static void gaim_cmd_strip_cmd_from_markup(char *markup) | |
| 177 | { | |
| 178 | guint len = strlen(markup); | |
| 179 | char *s = markup; | |
| 180 | ||
| 181 | while (*s) { | |
| 182 | gunichar c = g_utf8_get_char(s); | |
| 183 | ||
| 184 | if (c == '<') { | |
| 185 | s = strchr(s, '>'); | |
| 186 | if (!s) | |
| 187 | return; | |
| 188 | } else if (g_unichar_isspace(c)) { | |
| 189 | gaim_cmd_strip_current_char(c, s, len - (s - markup)); | |
| 190 | return; | |
| 191 | } else { | |
| 192 | gaim_cmd_strip_current_char(c, s, len - (s - markup)); | |
| 193 | continue; | |
| 194 | } | |
| 195 | s = g_utf8_next_char(s); | |
| 196 | } | |
| 197 | } | |
| 198 | GaimCmdStatus gaim_cmd_do_command(GaimConversation *conv, const gchar *cmdline, | |
| 199 | const gchar *markup, gchar **error) | |
| 9130 | 200 | { |
| 201 | GaimCmd *c; | |
| 202 | GList *l; | |
| 203 | gchar *err = NULL; | |
| 204 | gboolean is_im; | |
| 205 | gboolean found = FALSE, tried_cmd = FALSE, right_type = FALSE, right_prpl = FALSE; | |
| 206 | const gchar *prpl_id; | |
| 207 | gchar **args = NULL; | |
| 9175 | 208 | gchar *cmd, *rest, *mrest; |
| 9130 | 209 | GaimCmdRet ret = GAIM_CMD_RET_CONTINUE; |
| 210 | ||
| 9149 | 211 | *error = NULL; |
| 9130 | 212 | prpl_id = gaim_account_get_protocol_id(gaim_conversation_get_account(conv)); |
| 213 | ||
| 214 | if (gaim_conversation_get_type(conv) == GAIM_CONV_IM) | |
| 215 | is_im = TRUE; | |
| 216 | else if (gaim_conversation_get_type(conv) == GAIM_CONV_CHAT) | |
| 217 | is_im = FALSE; | |
| 218 | else | |
| 219 | return GAIM_CMD_STATUS_FAILED; | |
| 220 | ||
| 221 | rest = strchr(cmdline, ' '); | |
| 222 | if (rest) { | |
| 223 | cmd = g_strndup(cmdline, rest - cmdline); | |
| 224 | rest++; | |
| 225 | } else { | |
| 226 | cmd = g_strdup(cmdline); | |
| 227 | rest = ""; | |
| 228 | } | |
| 229 | ||
| 9175 | 230 | mrest = g_strdup(markup); |
| 231 | gaim_cmd_strip_cmd_from_markup(mrest); | |
| 232 | ||
| 9130 | 233 | for (l = cmds; l; l = l->next) { |
| 234 | c = l->data; | |
| 235 | ||
| 236 | if (strcmp(c->cmd, cmd) != 0) | |
| 237 | continue; | |
| 238 | ||
| 239 | found = TRUE; | |
| 240 | ||
| 241 | if (is_im) | |
| 242 | if (!(c->flags & GAIM_CMD_FLAG_IM)) | |
| 243 | continue; | |
| 244 | if (!is_im) | |
| 245 | if (!(c->flags & GAIM_CMD_FLAG_CHAT)) | |
| 246 | continue; | |
| 247 | ||
| 248 | right_type = TRUE; | |
| 249 | ||
| 250 | if ((c->flags & GAIM_CMD_FLAG_PRPL_ONLY) && c->prpl_id && | |
| 251 | (strcmp(c->prpl_id, prpl_id) != 0)) | |
| 252 | continue; | |
| 253 | ||
| 254 | right_prpl = TRUE; | |
| 255 | ||
| 256 | /* this checks the allow bad args flag for us */ | |
| 9175 | 257 | if (!gaim_cmd_parse_args(c, rest, mrest, &args)) { |
| 9130 | 258 | gaim_cmd_free_args(args); |
| 259 | args = NULL; | |
| 260 | continue; | |
| 261 | } | |
| 262 | ||
| 263 | tried_cmd = TRUE; | |
| 264 | ret = c->func(conv, cmd, args, &err); | |
| 265 | if (ret == GAIM_CMD_RET_CONTINUE) { | |
| 266 | if (err) | |
| 267 | g_free(err); | |
| 9149 | 268 | err = NULL; |
| 9130 | 269 | gaim_cmd_free_args(args); |
| 270 | args = NULL; | |
| 271 | continue; | |
| 272 | } else { | |
| 273 | break; | |
| 274 | } | |
| 275 | ||
| 276 | } | |
| 277 | ||
| 278 | if (args) | |
| 279 | gaim_cmd_free_args(args); | |
| 280 | g_free(cmd); | |
| 9175 | 281 | g_free(mrest); |
| 9130 | 282 | |
| 283 | if (!found) | |
| 284 | return GAIM_CMD_STATUS_NOT_FOUND; | |
| 285 | ||
| 286 | if (!right_type) | |
| 287 | return GAIM_CMD_STATUS_WRONG_TYPE; | |
| 288 | if (!right_prpl) | |
| 289 | return GAIM_CMD_STATUS_WRONG_PRPL; | |
| 290 | if (!tried_cmd) | |
| 291 | return GAIM_CMD_STATUS_WRONG_ARGS; | |
| 292 | ||
| 293 | if (ret == GAIM_CMD_RET_OK) { | |
| 294 | return GAIM_CMD_STATUS_OK; | |
| 295 | } else { | |
| 296 | *error = err; | |
| 297 | if (ret == GAIM_CMD_RET_CONTINUE) | |
| 298 | return GAIM_CMD_STATUS_NOT_FOUND; | |
| 299 | else | |
| 300 | return GAIM_CMD_STATUS_FAILED; | |
| 301 | } | |
| 302 | ||
| 303 | } | |
| 304 | ||
| 305 | ||
| 306 | GList *gaim_cmd_list(GaimConversation *conv) | |
| 307 | { | |
| 308 | GList *ret = NULL; | |
| 309 | GaimCmd *c; | |
| 310 | GList *l; | |
| 311 | ||
| 312 | for (l = cmds; l; l = l->next) { | |
| 313 | c = l->data; | |
| 314 | ||
| 315 | if (conv && (gaim_conversation_get_type(conv) == GAIM_CONV_IM)) | |
| 316 | if (!(c->flags & GAIM_CMD_FLAG_IM)) | |
| 317 | continue; | |
| 318 | if (conv && (gaim_conversation_get_type(conv) == GAIM_CONV_CHAT)) | |
| 319 | if (!(c->flags & GAIM_CMD_FLAG_CHAT)) | |
| 320 | continue; | |
| 321 | ||
| 322 | if (conv && (c->flags & GAIM_CMD_FLAG_PRPL_ONLY) && c->prpl_id && | |
| 323 | (strcmp(c->prpl_id, gaim_account_get_protocol_id(gaim_conversation_get_account(conv))) != 0)) | |
| 324 | continue; | |
| 325 | ||
| 326 | ret = g_list_append(ret, c->cmd); | |
| 327 | } | |
| 328 | ||
| 329 | return ret; | |
| 330 | } | |
| 331 | ||
| 332 | ||
| 333 | GList *gaim_cmd_help(GaimConversation *conv, const gchar *cmd) | |
| 334 | { | |
| 335 | GList *ret = NULL; | |
| 336 | GaimCmd *c; | |
| 337 | GList *l; | |
| 338 | ||
| 339 | for (l = cmds; l; l = l->next) { | |
| 340 | c = l->data; | |
| 341 | ||
| 342 | if (cmd && (strcmp(cmd, c->cmd) != 0)) | |
| 343 | continue; | |
| 344 | ||
| 345 | if (conv && (gaim_conversation_get_type(conv) == GAIM_CONV_IM)) | |
| 346 | if (!(c->flags & GAIM_CMD_FLAG_IM)) | |
| 347 | continue; | |
| 348 | if (conv && (gaim_conversation_get_type(conv) == GAIM_CONV_CHAT)) | |
| 349 | if (!(c->flags & GAIM_CMD_FLAG_CHAT)) | |
| 350 | continue; | |
| 351 | ||
| 352 | if (conv && (c->flags & GAIM_CMD_FLAG_PRPL_ONLY) && c->prpl_id && | |
| 353 | (strcmp(c->prpl_id, gaim_account_get_protocol_id(gaim_conversation_get_account(conv))) != 0)) | |
| 354 | continue; | |
| 355 | ||
| 356 | ret = g_list_append(ret, c->help); | |
| 357 | } | |
| 358 | ||
| 359 | return ret; | |
| 360 | } | |
| 361 |