Added SILC Thread Queue API
[crypto.git] / apps / irssi / src / core / commands.c
index a96b5fa009d48ee1f749490b1819b39835130c68..d9dc67af4e9d79b752a80f565aa21446045f8895 100644 (file)
@@ -72,10 +72,10 @@ static COMMAND_MODULE_REC *command_module_find(COMMAND_REC *rec,
        return NULL;
 }
 
-static COMMAND_MODULE_REC *command_module_find_func(COMMAND_REC *rec,
-                                                   SIGNAL_FUNC func)
+static COMMAND_MODULE_REC *
+command_module_find_and_remove(COMMAND_REC *rec, SIGNAL_FUNC func)
 {
-       GSList *tmp;
+       GSList *tmp, *tmp2;
 
        g_return_val_if_fail(rec != NULL, NULL);
        g_return_val_if_fail(func != NULL, NULL);
@@ -83,8 +83,16 @@ static COMMAND_MODULE_REC *command_module_find_func(COMMAND_REC *rec,
        for (tmp = rec->modules; tmp != NULL; tmp = tmp->next) {
                COMMAND_MODULE_REC *rec = tmp->data;
 
-                if (g_slist_find(rec->signals, (void *) func) != NULL)
-                       return rec;
+               for (tmp2 = rec->callbacks; tmp2 != NULL; tmp2 = tmp2->next) {
+                       COMMAND_CALLBACK_REC *cb = tmp2->data;
+
+                       if (cb->func == func) {
+                               rec->callbacks =
+                                       g_slist_remove(rec->callbacks, cb);
+                               g_free(cb);
+                               return rec;
+                       }
+               }
        }
 
        return NULL;
@@ -131,11 +139,13 @@ command_module_get(COMMAND_REC *rec, const char *module, int protocol)
         return modrec;
 }
 
-void command_bind_to(const char *module, int pos, const char *cmd,
-                    int protocol, const char *category, SIGNAL_FUNC func)
+void command_bind_full(const char *module, int priority, const char *cmd,
+                      int protocol, const char *category, SIGNAL_FUNC func,
+                      void *user_data)
 {
        COMMAND_REC *rec;
-        COMMAND_MODULE_REC *modrec;
+       COMMAND_MODULE_REC *modrec;
+        COMMAND_CALLBACK_REC *cb;
        char *str;
 
        g_return_if_fail(module != NULL);
@@ -150,11 +160,14 @@ void command_bind_to(const char *module, int pos, const char *cmd,
        }
         modrec = command_module_get(rec, module, protocol);
 
-        modrec->signals = g_slist_append(modrec->signals, (void *) func);
+       cb = g_new0(COMMAND_CALLBACK_REC, 1);
+       cb->func = func;
+       cb->user_data = user_data;
+       modrec->callbacks = g_slist_append(modrec->callbacks, cb);
 
        if (func != NULL) {
                str = g_strconcat("command ", cmd, NULL);
-               signal_add_to(module, pos, str, func);
+               signal_add_full(module, priority, str, func, user_data);
                g_free(str);
        }
 
@@ -176,7 +189,8 @@ static void command_module_free(COMMAND_MODULE_REC *modrec, COMMAND_REC *rec)
 {
        rec->modules = g_slist_remove(rec->modules, modrec);
 
-       g_slist_free(modrec->signals);
+       g_slist_foreach(modrec->callbacks, (GFunc) g_free, NULL);
+       g_slist_free(modrec->callbacks);
         g_free(modrec->name);
         g_free_not_null(modrec->options);
         g_free(modrec);
@@ -196,7 +210,7 @@ static void command_module_destroy(COMMAND_REC *rec,
        for (tmp = rec->modules; tmp != NULL; tmp = tmp->next) {
                COMMAND_MODULE_REC *rec = tmp->data;
 
-               if (rec->signals == NULL)
+               if (rec->callbacks == NULL)
                        freelist = g_slist_append(freelist, rec);
                else {
                         g_slist_free(freelist);
@@ -212,10 +226,10 @@ static void command_module_destroy(COMMAND_REC *rec,
                command_free(rec);
 }
 
-void command_unbind(const char *cmd, SIGNAL_FUNC func)
+void command_unbind_full(const char *cmd, SIGNAL_FUNC func, void *user_data)
 {
        COMMAND_REC *rec;
-        COMMAND_MODULE_REC *modrec;
+       COMMAND_MODULE_REC *modrec;
        char *str;
 
        g_return_if_fail(cmd != NULL);
@@ -223,17 +237,15 @@ void command_unbind(const char *cmd, SIGNAL_FUNC func)
 
        rec = command_find(cmd);
        if (rec != NULL) {
-               modrec = command_module_find_func(rec, func);
+               modrec = command_module_find_and_remove(rec, func);
                g_return_if_fail(modrec != NULL);
 
-               modrec->signals =
-                       g_slist_remove(modrec->signals, (void *) func);
-               if (modrec->signals == NULL)
+               if (modrec->callbacks == NULL)
                        command_module_destroy(rec, modrec);
        }
 
        str = g_strconcat("command ", cmd, NULL);
-       signal_remove(str, func);
+       signal_remove_data(str, func, user_data);
        g_free(str);
 }
 
@@ -472,7 +484,7 @@ char *cmd_get_param(char **data)
        return pos;
 }
 
-static char *cmd_get_quoted_param(char **data)
+char *cmd_get_quoted_param(char **data)
 {
        char *pos, quote;
 
@@ -486,13 +498,18 @@ static char *cmd_get_quoted_param(char **data)
        quote = **data; (*data)++;
 
        pos = *data;
-       while (**data != '\0' && **data != quote) {
+       while (**data != '\0' && (**data != quote ||
+                                 ((*data)[1] != ' ' && (*data)[1] != '\0'))) {
                if (**data == '\\' && (*data)[1] != '\0')
                         g_memmove(*data, (*data)+1, strlen(*data));
                (*data)++;
        }
 
-       if (**data != '\0') *(*data)++ = '\0';
+       if (**data == quote) {
+               *(*data)++ = '\0';
+               if (**data == ' ')
+                       (*data)++;
+       }
 
        return pos;
 }
@@ -558,17 +575,17 @@ static int get_cmd_options(char **data, int ignore_unknown,
                        }
 
                        (*data)++;
-                       if (**data == '-' && i_isspace((*data)[1])) {
+                       if (**data == '-' && (*data)[1] == ' ') {
                                /* -- option means end of options even
                                   if next word starts with - */
                                (*data)++;
-                               while (i_isspace(**data)) (*data)++;
+                               while (**data == ' ') (*data)++;
                                break;
                        }
 
                        if (**data == '\0')
                                option = "-";
-                       else if (!i_isspace(**data))
+                       else if (**data != ' ')
                                option = cmd_get_param(data);
                        else {
                                option = "-";
@@ -613,7 +630,7 @@ static int get_cmd_options(char **data, int ignore_unknown,
                            *optlist[pos] == '!')
                                option = NULL;
 
-                       while (i_isspace(**data)) (*data)++;
+                       while (**data == ' ') (*data)++;
                        continue;
                }
 
@@ -631,7 +648,7 @@ static int get_cmd_options(char **data, int ignore_unknown,
                }
                option = NULL;
 
-               while (i_isspace(**data)) (*data)++;
+               while (**data == ' ') (*data)++;
        }
 
        return 0;
@@ -642,11 +659,12 @@ typedef struct {
         GHashTable *options;
 } CMD_TEMP_REC;
 
-static char *get_optional_channel(WI_ITEM_REC *active_item, char **data,
-                                 int require_name)
+static const char *
+get_optional_channel(WI_ITEM_REC *active_item, char **data, int require_name)
 {
         CHANNEL_REC *chanrec;
-       char *tmp, *origtmp, *channel, *ret;
+       const char *ret;
+       char *tmp, *origtmp, *channel;
 
        if (active_item == NULL) {
                 /* no active channel in window, channel required */
@@ -659,10 +677,10 @@ static char *get_optional_channel(WI_ITEM_REC *active_item, char **data,
        if (strcmp(channel, "*") == 0 && !require_name) {
                 /* "*" means active channel */
                cmd_get_param(data);
-               ret = active_item->name;
+               ret = window_item_get_target(active_item);
        } else if (!server_ischannel(active_item->server, channel)) {
                 /* we don't have channel parameter - use active channel */
-               ret = active_item->name;
+               ret = window_item_get_target(active_item);
        } else {
                /* Find the channel first and use it's name if found.
                   This allows automatic !channel -> !XXXXXchannel replaces. */
@@ -717,8 +735,9 @@ int cmd_get_params(const char *data, gpointer *free_me, int count, ...)
                cnt = PARAM_WITHOUT_FLAGS(count);
                if (count & PARAM_FLAG_OPTCHAN) {
                        /* optional channel as first parameter */
-                        require_name = (count & PARAM_FLAG_OPTCHAN_NAME);
-                       arg = get_optional_channel(item, &datad, require_name);
+                       require_name = (count & PARAM_FLAG_OPTCHAN_NAME) ==
+                               PARAM_FLAG_OPTCHAN_NAME;
+                       arg = (char *) get_optional_channel(item, &datad, require_name);
 
                        str = (char **) va_arg(args, char **);
                        if (str != NULL) *str = arg;
@@ -766,10 +785,11 @@ static void command_module_unbind_all(COMMAND_REC *rec,
 {
        GSList *tmp, *next;
 
-       for (tmp = modrec->signals; tmp != NULL; tmp = next) {
+       for (tmp = modrec->callbacks; tmp != NULL; tmp = next) {
+               COMMAND_CALLBACK_REC *cb = tmp->data;
                next = tmp->next;
 
-               command_unbind(rec->cmd, (SIGNAL_FUNC) tmp->data);
+               command_unbind_full(rec->cmd, cb->func, cb->user_data);
        }
 
        if (g_slist_find(commands, rec) != NULL) {
@@ -898,13 +918,8 @@ static void event_command(const char *line, SERVER_REC *server, void *item)
 
        g_return_if_fail(line != NULL);
 
-       if (*line == '\0') {
-               /* empty line, forget it. */
-                signal_stop();
-               return;
-       }
-
-       cmdchar = strchr(settings_get_str("cmdchars"), *line);
+       cmdchar = *line == '\0' ? NULL :
+               strchr(settings_get_str("cmdchars"), *line);
        if (cmdchar != NULL && line[1] == ' ') {
                /* "/ text" = same as sending "text" to active channel. */
                line += 2;
@@ -930,12 +945,18 @@ static void event_command(const char *line, SERVER_REC *server, void *item)
        parse_command(line, expand_aliases, server, item);
 }
 
+static int eval_recursion_depth=0;
 /* SYNTAX: EVAL <command(s)> */
 static void cmd_eval(const char *data, SERVER_REC *server, void *item)
 {
        g_return_if_fail(data != NULL);
+       if (eval_recursion_depth > 100) 
+               cmd_return_error(CMDERR_EVAL_MAX_RECURSE);
 
+       eval_recursion_depth++;
        eval_special_string(data, "", server, item);
+       eval_recursion_depth--;
 }
 
 /* SYNTAX: CD <directory> */