Tue, 15 Jun 2004 02:37:27 +0000
[gaim-migrate @ 10088]
Ok I'm done. This started out as shx's patch to make add/remove
buddy/buddies take GaimBuddy and GaimGroup's in various places.
I think his diff was like 2000 lines and mine is like 5000. I
tried to clean up blist.c a bit and make it more uniform. There
are some more g_return_if_fail() checks. Removed some code that
was deprecated--it's probably been long enough. Removed some
#include <multi.h>'s. Make blist.xml saving happen on a timer,
like prefs.xml and accounts.xml.
Sorry if this doesn't merge cleanly with whatever you're doing.
People should really test this a lot.
| 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 | ||
| 53 | GaimCmdId *gaim_cmd_register(const gchar *cmd, const gchar *args, GaimCmdPriority p, GaimCmdFlag f, | |
| 54 | const gchar *prpl_id, GaimCmdFunc func, const gchar *helpstr) | |
| 55 | { | |
| 56 | GaimCmdId *id; | |
| 57 | GaimCmd *c; | |
| 58 | ||
| 59 | g_return_val_if_fail(cmd != NULL && *cmd != '\0', NULL); | |
| 60 | g_return_val_if_fail(args != NULL, NULL); | |
| 61 | g_return_val_if_fail(func != NULL, NULL); | |
| 62 | ||
| 63 | id = g_new(GaimCmdId, 1); | |
| 64 | *id = next_id++; | |
| 65 | ||
| 66 | c = g_new0(GaimCmd, 1); | |
| 67 | c->id = *id; | |
| 68 | c->cmd = g_strdup(cmd); | |
| 69 | c->args = g_strdup(args); | |
| 70 | c->priority = p; | |
| 71 | c->flags = f; | |
| 72 | c->prpl_id = prpl_id ? g_strdup(prpl_id) : NULL; | |
| 73 | c->func = func; | |
| 74 | c->help = helpstr ? g_strdup(helpstr) : NULL; | |
| 75 | ||
| 76 | cmds = g_list_insert_sorted(cmds, c, (GCompareFunc)cmds_compare_func); | |
| 77 | ||
| 78 | return id; | |
| 79 | } | |
| 80 | ||
| 81 | static void gaim_cmd_free(GaimCmd *c) | |
| 82 | { | |
| 83 | g_free(c->cmd); | |
| 84 | g_free(c->args); | |
| 85 | if (c->prpl_id) | |
| 86 | g_free(c->prpl_id); | |
| 87 | if (c->help) | |
| 88 | g_free(c->help); | |
| 89 | ||
| 90 | g_free(c); | |
| 91 | } | |
| 92 | ||
| 93 | void gaim_cmd_unregister(GaimCmdId *id) | |
| 94 | { | |
| 95 | GaimCmd *c; | |
| 96 | GList *l; | |
| 97 | ||
| 98 | for (l = cmds; l; l = l->next) { | |
| 99 | c = l->data; | |
| 100 | ||
| 101 | if (c->id == *id) { | |
| 102 | cmds = g_list_remove(cmds, c); | |
| 103 | gaim_cmd_free(c); | |
| 104 | g_free(id); | |
| 105 | return; | |
| 106 | } | |
| 107 | } | |
| 108 | } | |
| 109 | ||
| 9175 | 110 | static gboolean gaim_cmd_parse_args(GaimCmd *cmd, const gchar *s, const gchar *m, gchar ***args) |
| 9130 | 111 | { |
| 112 | int i; | |
| 113 | const char *end, *cur; | |
| 114 | ||
| 115 | *args = g_new0(char *, strlen(cmd->args) + 1); | |
| 116 | ||
| 117 | cur = s; | |
| 118 | ||
| 119 | for (i = 0; cmd->args[i]; i++) { | |
| 120 | if (!*cur) | |
| 121 | return (cmd->flags & GAIM_CMD_FLAG_ALLOW_WRONG_ARGS); | |
| 122 | ||
| 123 | switch (cmd->args[i]) { | |
| 124 | case 'w': | |
| 125 | if (!(end = strchr(cur, ' '))) end = cur + strlen(cur); | |
| 126 | (*args)[i] = g_strndup(cur, end - cur); | |
| 127 | cur += end - cur; | |
| 128 | break; | |
| 129 | case 'W': | |
| 130 | if (!(end = strchr(cur, ' '))) end = cur + strlen(cur); | |
| 9175 | 131 | (*args)[i] = gaim_markup_slice(m, g_utf8_pointer_to_offset(s, cur), g_utf8_pointer_to_offset(s, end)); |
| 9130 | 132 | cur += end - cur; |
| 133 | break; | |
| 134 | case 's': | |
| 135 | (*args)[i] = g_strdup(cur); | |
| 136 | cur = cur + strlen(cur); | |
| 137 | break; | |
| 138 | case 'S': | |
| 9175 | 139 | (*args)[i] = gaim_markup_slice(m, g_utf8_pointer_to_offset(s, cur), g_utf8_strlen(cur, -1) + 1); |
| 9130 | 140 | cur = cur + strlen(cur); |
| 141 | break; | |
| 142 | } | |
| 143 | } | |
| 144 | ||
| 145 | if (*cur) | |
| 146 | return (cmd->flags & GAIM_CMD_FLAG_ALLOW_WRONG_ARGS); | |
| 147 | ||
| 148 | return TRUE; | |
| 149 | } | |
| 150 | ||
| 151 | static void gaim_cmd_free_args(gchar **args) | |
| 152 | { | |
| 153 | int i; | |
| 154 | ||
| 155 | for (i = 0; args[i]; i++) | |
| 156 | g_free(args[i]); | |
| 157 | g_free(args); | |
| 158 | } | |
| 159 | ||
| 9175 | 160 | static void gaim_cmd_strip_current_char(gunichar c, char *s, guint len) |
| 161 | { | |
| 162 | int bytes; | |
| 163 | ||
| 164 | bytes = g_unichar_to_utf8(c, NULL); | |
| 165 | memmove(s, s + bytes, len + 1 - bytes); | |
| 166 | } | |
| 167 | ||
| 168 | static void gaim_cmd_strip_cmd_from_markup(char *markup) | |
| 169 | { | |
| 170 | guint len = strlen(markup); | |
| 171 | char *s = markup; | |
| 172 | ||
| 173 | while (*s) { | |
| 174 | gunichar c = g_utf8_get_char(s); | |
| 175 | ||
| 176 | if (c == '<') { | |
| 177 | s = strchr(s, '>'); | |
| 178 | if (!s) | |
| 179 | return; | |
| 180 | } else if (g_unichar_isspace(c)) { | |
| 181 | gaim_cmd_strip_current_char(c, s, len - (s - markup)); | |
| 182 | return; | |
| 183 | } else { | |
| 184 | gaim_cmd_strip_current_char(c, s, len - (s - markup)); | |
| 185 | continue; | |
| 186 | } | |
| 187 | s = g_utf8_next_char(s); | |
| 188 | } | |
| 189 | } | |
| 190 | GaimCmdStatus gaim_cmd_do_command(GaimConversation *conv, const gchar *cmdline, | |
| 191 | const gchar *markup, gchar **error) | |
| 9130 | 192 | { |
| 193 | GaimCmd *c; | |
| 194 | GList *l; | |
| 195 | gchar *err = NULL; | |
| 196 | gboolean is_im; | |
| 197 | gboolean found = FALSE, tried_cmd = FALSE, right_type = FALSE, right_prpl = FALSE; | |
| 198 | const gchar *prpl_id; | |
| 199 | gchar **args = NULL; | |
| 9175 | 200 | gchar *cmd, *rest, *mrest; |
| 9130 | 201 | GaimCmdRet ret = GAIM_CMD_RET_CONTINUE; |
| 202 | ||
| 9149 | 203 | *error = NULL; |
| 9130 | 204 | prpl_id = gaim_account_get_protocol_id(gaim_conversation_get_account(conv)); |
| 205 | ||
| 206 | if (gaim_conversation_get_type(conv) == GAIM_CONV_IM) | |
| 207 | is_im = TRUE; | |
| 208 | else if (gaim_conversation_get_type(conv) == GAIM_CONV_CHAT) | |
| 209 | is_im = FALSE; | |
| 210 | else | |
| 211 | return GAIM_CMD_STATUS_FAILED; | |
| 212 | ||
| 213 | rest = strchr(cmdline, ' '); | |
| 214 | if (rest) { | |
| 215 | cmd = g_strndup(cmdline, rest - cmdline); | |
| 216 | rest++; | |
| 217 | } else { | |
| 218 | cmd = g_strdup(cmdline); | |
| 219 | rest = ""; | |
| 220 | } | |
| 221 | ||
| 9175 | 222 | mrest = g_strdup(markup); |
| 223 | gaim_cmd_strip_cmd_from_markup(mrest); | |
| 224 | ||
| 9130 | 225 | for (l = cmds; l; l = l->next) { |
| 226 | c = l->data; | |
| 227 | ||
| 228 | if (strcmp(c->cmd, cmd) != 0) | |
| 229 | continue; | |
| 230 | ||
| 231 | found = TRUE; | |
| 232 | ||
| 233 | if (is_im) | |
| 234 | if (!(c->flags & GAIM_CMD_FLAG_IM)) | |
| 235 | continue; | |
| 236 | if (!is_im) | |
| 237 | if (!(c->flags & GAIM_CMD_FLAG_CHAT)) | |
| 238 | continue; | |
| 239 | ||
| 240 | right_type = TRUE; | |
| 241 | ||
| 242 | if ((c->flags & GAIM_CMD_FLAG_PRPL_ONLY) && c->prpl_id && | |
| 243 | (strcmp(c->prpl_id, prpl_id) != 0)) | |
| 244 | continue; | |
| 245 | ||
| 246 | right_prpl = TRUE; | |
| 247 | ||
| 248 | /* this checks the allow bad args flag for us */ | |
| 9175 | 249 | if (!gaim_cmd_parse_args(c, rest, mrest, &args)) { |
| 9130 | 250 | gaim_cmd_free_args(args); |
| 251 | args = NULL; | |
| 252 | continue; | |
| 253 | } | |
| 254 | ||
| 255 | tried_cmd = TRUE; | |
| 256 | ret = c->func(conv, cmd, args, &err); | |
| 257 | if (ret == GAIM_CMD_RET_CONTINUE) { | |
| 258 | if (err) | |
| 259 | g_free(err); | |
| 9149 | 260 | err = NULL; |
| 9130 | 261 | gaim_cmd_free_args(args); |
| 262 | args = NULL; | |
| 263 | continue; | |
| 264 | } else { | |
| 265 | break; | |
| 266 | } | |
| 267 | ||
| 268 | } | |
| 269 | ||
| 270 | if (args) | |
| 271 | gaim_cmd_free_args(args); | |
| 272 | g_free(cmd); | |
| 9175 | 273 | g_free(mrest); |
| 9130 | 274 | |
| 275 | if (!found) | |
| 276 | return GAIM_CMD_STATUS_NOT_FOUND; | |
| 277 | ||
| 278 | if (!right_type) | |
| 279 | return GAIM_CMD_STATUS_WRONG_TYPE; | |
| 280 | if (!right_prpl) | |
| 281 | return GAIM_CMD_STATUS_WRONG_PRPL; | |
| 282 | if (!tried_cmd) | |
| 283 | return GAIM_CMD_STATUS_WRONG_ARGS; | |
| 284 | ||
| 285 | if (ret == GAIM_CMD_RET_OK) { | |
| 286 | return GAIM_CMD_STATUS_OK; | |
| 287 | } else { | |
| 288 | *error = err; | |
| 289 | if (ret == GAIM_CMD_RET_CONTINUE) | |
| 290 | return GAIM_CMD_STATUS_NOT_FOUND; | |
| 291 | else | |
| 292 | return GAIM_CMD_STATUS_FAILED; | |
| 293 | } | |
| 294 | ||
| 295 | } | |
| 296 | ||
| 297 | ||
| 298 | GList *gaim_cmd_list(GaimConversation *conv) | |
| 299 | { | |
| 300 | GList *ret = NULL; | |
| 301 | GaimCmd *c; | |
| 302 | GList *l; | |
| 303 | ||
| 304 | for (l = cmds; l; l = l->next) { | |
| 305 | c = l->data; | |
| 306 | ||
| 307 | if (conv && (gaim_conversation_get_type(conv) == GAIM_CONV_IM)) | |
| 308 | if (!(c->flags & GAIM_CMD_FLAG_IM)) | |
| 309 | continue; | |
| 310 | if (conv && (gaim_conversation_get_type(conv) == GAIM_CONV_CHAT)) | |
| 311 | if (!(c->flags & GAIM_CMD_FLAG_CHAT)) | |
| 312 | continue; | |
| 313 | ||
| 314 | if (conv && (c->flags & GAIM_CMD_FLAG_PRPL_ONLY) && c->prpl_id && | |
| 315 | (strcmp(c->prpl_id, gaim_account_get_protocol_id(gaim_conversation_get_account(conv))) != 0)) | |
| 316 | continue; | |
| 317 | ||
| 318 | ret = g_list_append(ret, c->cmd); | |
| 319 | } | |
| 320 | ||
| 321 | return ret; | |
| 322 | } | |
| 323 | ||
| 324 | ||
| 325 | GList *gaim_cmd_help(GaimConversation *conv, const gchar *cmd) | |
| 326 | { | |
| 327 | GList *ret = NULL; | |
| 328 | GaimCmd *c; | |
| 329 | GList *l; | |
| 330 | ||
| 331 | for (l = cmds; l; l = l->next) { | |
| 332 | c = l->data; | |
| 333 | ||
| 334 | if (cmd && (strcmp(cmd, c->cmd) != 0)) | |
| 335 | continue; | |
| 336 | ||
| 337 | if (conv && (gaim_conversation_get_type(conv) == GAIM_CONV_IM)) | |
| 338 | if (!(c->flags & GAIM_CMD_FLAG_IM)) | |
| 339 | continue; | |
| 340 | if (conv && (gaim_conversation_get_type(conv) == GAIM_CONV_CHAT)) | |
| 341 | if (!(c->flags & GAIM_CMD_FLAG_CHAT)) | |
| 342 | continue; | |
| 343 | ||
| 344 | if (conv && (c->flags & GAIM_CMD_FLAG_PRPL_ONLY) && c->prpl_id && | |
| 345 | (strcmp(c->prpl_id, gaim_account_get_protocol_id(gaim_conversation_get_account(conv))) != 0)) | |
| 346 | continue; | |
| 347 | ||
| 348 | ret = g_list_append(ret, c->help); | |
| 349 | } | |
| 350 | ||
| 351 | return ret; | |
| 352 | } | |
| 353 |