#include "commands.h"
#include "misc.h"
#include "special-vars.h"
+#include "window-item-def.h"
#include "servers.h"
-#include "servers-redirect.h"
#include "channels.h"
#include "lib-config/iconfig.h"
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);
for (tmp = rec->modules; tmp != NULL; tmp = tmp->next) {
COMMAND_MODULE_REC *rec = tmp->data;
- if (g_slist_find(rec->signals, 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;
return FALSE;
}
-static COMMAND_MODULE_REC *command_module_get(COMMAND_REC *rec,
- const char *module)
+static COMMAND_MODULE_REC *
+command_module_get(COMMAND_REC *rec, const char *module, int protocol)
{
COMMAND_MODULE_REC *modrec;
if (modrec == NULL) {
modrec = g_new0(COMMAND_MODULE_REC, 1);
modrec->name = g_strdup(module);
+ modrec->protocol = -1;
rec->modules = g_slist_append(rec->modules, modrec);
}
+ if (protocol != -1)
+ modrec->protocol = protocol;
+
return modrec;
}
-void command_bind_to(const char *module, int pos, const char *cmd,
- 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);
rec->category = category == NULL ? NULL : g_strdup(category);
commands = g_slist_append(commands, rec);
}
- modrec = command_module_get(rec, module);
+ modrec = command_module_get(rec, module, protocol);
- modrec->signals = g_slist_append(modrec->signals, 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);
}
{
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);
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);
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);
rec = command_find(cmd);
if (rec != NULL) {
- modrec = command_module_find_func(rec, func);
- modrec->signals = g_slist_remove(modrec->signals, func);
- if (modrec->signals == NULL)
+ modrec = command_module_find_and_remove(rec, func);
+ g_return_if_fail(modrec != 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);
}
g_return_if_fail(data != NULL);
+ while (*data == ' ') data++;
+
if (*data == '\0') {
/* no subcommand given - list the subcommands */
signal_emit("list subcommands", 2, cmd);
rec = command_find(cmd);
g_return_if_fail(rec != NULL);
- modrec = command_module_get(rec, module);
+ modrec = command_module_get(rec, module, -1);
reload = modrec->options != NULL;
if (reload) {
return pos;
}
-static char *cmd_get_quoted_param(char **data)
+char *cmd_get_quoted_param(char **data)
{
char *pos, quote;
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;
}
}
(*data)++;
- if (**data == '-' && isspace((*data)[1])) {
+ if (**data == '-' && (*data)[1] == ' ') {
/* -- option means end of options even
if next word starts with - */
(*data)++;
- while (isspace(**data)) (*data)++;
+ while (**data == ' ') (*data)++;
break;
}
- if (!isspace(**data))
+ if (**data == '\0')
+ option = "-";
+ else if (**data != ' ')
option = cmd_get_param(data);
else {
option = "-";
/* check if this option can have argument */
pos = optlist == NULL ? -1 :
option_find(optlist, option);
+
+ if (pos == -1 && optlist != NULL &&
+ is_numeric(option, '\0')) {
+ /* check if we want -<number> option */
+ pos = option_find(optlist, "#");
+ if (pos != -1) {
+ g_hash_table_insert(options, "#",
+ option);
+ pos = -3;
+ }
+ }
+
if (pos == -1 && !ignore_unknown) {
/* unknown option! */
*data = option;
option = optlist[pos] +
iscmdtype(*optlist[pos]);
}
- if (options != NULL)
+ if (options != NULL && pos != -3)
g_hash_table_insert(options, option, "");
if (pos < 0 || !iscmdtype(*optlist[pos]) ||
*optlist[pos] == '!')
option = NULL;
- while (isspace(**data)) (*data)++;
+ while (**data == ' ') (*data)++;
continue;
}
if (option == NULL)
break;
- if (*optlist[pos] == '@' && !isdigit(**data))
+ if (*optlist[pos] == '@' && !i_isdigit(**data))
break; /* expected a numeric argument */
/* save the argument */
}
option = NULL;
- while (isspace(**data)) (*data)++;
+ while (**data == ' ') (*data)++;
}
return 0;
GHashTable *options;
} CMD_TEMP_REC;
-static char *get_optional_channel(CHANNEL_REC *active_channel, char **data)
+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_channel == NULL) {
+ if (active_item == NULL) {
/* no active channel in window, channel required */
return cmd_get_param(data);
}
origtmp = tmp = g_strdup(*data);
channel = cmd_get_param(&tmp);
- if (strcmp(channel, "*") == 0 ||
- !active_channel->server->ischannel(channel))
- ret = active_channel->name;
- else {
+ if (strcmp(channel, "*") == 0 && !require_name) {
+ /* "*" means active channel */
+ cmd_get_param(data);
+ 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 = window_item_get_target(active_item);
+ } else {
/* Find the channel first and use it's name if found.
This allows automatic !channel -> !XXXXXchannel replaces. */
- chanrec = channel_find(active_channel->server, channel);
+ channel = cmd_get_param(data);
+
+ chanrec = channel_find(active_item->server, channel);
ret = chanrec == NULL ? channel : chanrec->name;
- cmd_get_param(data);
}
g_free(origtmp);
int cmd_get_params(const char *data, gpointer *free_me, int count, ...)
{
- CHANNEL_REC *chanrec;
+ WI_ITEM_REC *item;
CMD_TEMP_REC *rec;
GHashTable **opthash;
char **str, *arg, *datad;
va_list args;
- int cnt, error, ignore_unknown;
+ int cnt, error, ignore_unknown, require_name;
g_return_val_if_fail(data != NULL, FALSE);
datad = rec->data;
error = FALSE;
- chanrec = (count & PARAM_FLAG_OPTCHAN) == 0 ? NULL:
- (CHANNEL_REC *) va_arg(args, CHANNEL_REC *);
+ item = (count & PARAM_FLAG_OPTCHAN) == 0 ? NULL:
+ (WI_ITEM_REC *) va_arg(args, WI_ITEM_REC *);
if (count & PARAM_FLAG_OPTIONS) {
arg = (char *) va_arg(args, char *);
cnt = PARAM_WITHOUT_FLAGS(count);
if (count & PARAM_FLAG_OPTCHAN) {
/* optional channel as first parameter */
- arg = get_optional_channel(chanrec, &datad);
+ 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;
{
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, tmp->data);
+ command_unbind_full(rec->cmd, cb->func, cb->user_data);
}
if (g_slist_find(commands, rec) != NULL) {
}
}
+static int cmd_protocol_match(COMMAND_REC *cmd, SERVER_REC *server)
+{
+ GSList *tmp;
+
+ for (tmp = cmd->modules; tmp != NULL; tmp = tmp->next) {
+ COMMAND_MODULE_REC *rec = tmp->data;
+
+ if (rec->protocol == -1) {
+ /* at least one module accepts the command
+ without specific protocol */
+ return 1;
+ }
+
+ if (server != NULL && rec->protocol == server->chat_type) {
+ /* matching protocol found */
+ return 1;
+ }
+ }
+
+ return 0;
+}
+
#define alias_runstack_push(alias) \
alias_runstack = g_slist_append(alias_runstack, alias)
static void parse_command(const char *command, int expand_aliases,
SERVER_REC *server, void *item)
{
+ COMMAND_REC *rec;
const char *alias, *newcmd;
char *cmd, *orig, *args, *oldcmd;
return;
}
- cmd = g_strconcat("command ", newcmd, NULL);
- if (server != NULL)
- server_redirect_default(SERVER(server), cmd);
+ rec = command_find(newcmd);
+ if (rec != NULL && !cmd_protocol_match(rec, server)) {
+ g_free(orig);
+
+ signal_emit("error command", 2,
+ GINT_TO_POINTER(server == NULL ?
+ CMDERR_NOT_CONNECTED :
+ CMDERR_ILLEGAL_PROTO));
+ return;
+ }
+ cmd = g_strconcat("command ", newcmd, NULL);
g_strdown(cmd);
+
oldcmd = current_command;
current_command = cmd+8;
- if (!signal_emit(cmd, 3, args, server, item)) {
+ if (server != NULL) server_ref(server);
+ if (!signal_emit(cmd, 3, args, server, item)) {
signal_emit_id(signal_default_command, 3,
command, server, item);
}
+ if (server != NULL) {
+ if (server->connection_lost)
+ server_disconnect(server);
+ server_unref(server);
+ }
current_command = oldcmd;
g_free(cmd);
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;
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> */