diff options
-rw-r--r-- | src/core/commands.c | 102 | ||||
-rw-r--r-- | src/core/commands.h | 16 | ||||
-rw-r--r-- | src/irc/core/irc.c | 36 | ||||
-rw-r--r-- | src/irc/core/irc.h | 6 |
4 files changed, 53 insertions, 107 deletions
diff --git a/src/core/commands.c b/src/core/commands.c index 614358b8..f6d71397 100644 --- a/src/core/commands.c +++ b/src/core/commands.c @@ -22,9 +22,11 @@ #include "signals.h" #include "commands.h" #include "misc.h" +#include "special-vars.h" + #include "servers.h" #include "servers-redirect.h" -#include "special-vars.h" +#include "channels.h" #include "lib-config/iconfig.h" #include "settings.h" @@ -32,7 +34,6 @@ GSList *commands; char *current_command; -static GSList *cmdget_funcs; static int signal_default_command; static GSList *alias_runstack; @@ -613,69 +614,56 @@ static int get_cmd_options(char **data, int ignore_unknown, return 0; } -char *cmd_get_callfuncs(const char *data, int *count, va_list *args) +typedef struct { + char *data; + GHashTable *options; +} CMD_TEMP_REC; + +static char *get_optional_channel(CHANNEL_REC *active_channel, char **data) { - CMD_GET_FUNC func; - GSList *tmp; - char *ret, *old; + CHANNEL_REC *chanrec; + char *tmp, *origtmp, *channel, *ret; - ret = g_strdup(data); - for (tmp = cmdget_funcs; tmp != NULL; tmp = tmp->next) { - func = (CMD_GET_FUNC) tmp->data; + if (active_channel == NULL) { + /* no active channel in window, channel required */ + return cmd_get_param(data); + } + + origtmp = tmp = g_strdup(*data); + channel = cmd_get_param(&tmp); - old = ret; - ret = func(ret, count, args); - g_free(old); + if (strcmp(channel, "*") == 0 || + !active_channel->server->ischannel(channel)) + ret = active_channel->name; + 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); + ret = chanrec == NULL ? channel : chanrec->name; + cmd_get_param(data); } - return ret; + g_free(origtmp); + return ret; } -typedef struct { - char *data; - GHashTable *options; -} CMD_TEMP_REC; - int cmd_get_params(const char *data, gpointer *free_me, int count, ...) { CMD_TEMP_REC *rec; GHashTable **opthash; - char **str, *arg, *datad, *old; + char **str, *arg, *datad; va_list args; - int cnt, error, len, ignore_unknown; + int cnt, error, ignore_unknown; g_return_val_if_fail(data != NULL, FALSE); va_start(args, count); - /* get the length of the options in string */ - if ((count & PARAM_FLAG_OPTIONS) == 0) - len = 0; - else { - old = datad = g_strdup(data); - get_cmd_options(&datad, TRUE, NULL, NULL); - len = (int) (datad-old); - g_free(old); - } - - /* send the text to custom functions to handle - skip options */ - old = datad = cmd_get_callfuncs(data+len, &count, &args); - - if (len > 0) { - /* put the options + the new data to one string */ - datad = g_malloc(len+1 + strlen(old)+1); - memcpy(datad, data, len); - datad[len] = ' '; - memcpy(datad+len+1, old, strlen(old)+1); - g_free(old); - - old = datad; - } - rec = g_new0(CMD_TEMP_REC, 1); - rec->data = old; + rec->data = g_strdup(data); *free_me = rec; + datad = rec->data; error = FALSE; if (count & PARAM_FLAG_OPTIONS) { arg = (char *) va_arg(args, char *); @@ -693,6 +681,18 @@ int cmd_get_params(const char *data, gpointer *free_me, int count, ...) if (!error) { /* and now handle the string */ cnt = PARAM_WITHOUT_FLAGS(count); + if (count & PARAM_FLAG_OPTCHAN) { + /* optional channel as first parameter */ + CHANNEL_REC *chanrec; + + chanrec = (CHANNEL_REC *) va_arg(args, CHANNEL_REC *); + arg = get_optional_channel(chanrec, &datad); + + str = (char **) va_arg(args, char **); + if (str != NULL) *str = arg; + cnt--; + } + while (cnt-- > 0) { if (cnt == 0 && count & PARAM_FLAG_GETREST) { /* get rest */ @@ -729,16 +729,6 @@ void cmd_params_free(void *free_me) g_free(rec); } -void cmd_get_add_func(CMD_GET_FUNC func) -{ - cmdget_funcs = g_slist_prepend(cmdget_funcs, (void *) func); -} - -void cmd_get_remove_func(CMD_GET_FUNC func) -{ - cmdget_funcs = g_slist_prepend(cmdget_funcs, (void *) func); -} - static void command_module_unbind_all(COMMAND_REC *rec, COMMAND_MODULE_REC *modrec) { @@ -894,7 +884,6 @@ static void cmd_cd(const char *data) void commands_init(void) { commands = NULL; - cmdget_funcs = NULL; current_command = NULL; alias_runstack = NULL; @@ -910,7 +899,6 @@ void commands_init(void) void commands_deinit(void) { g_free_not_null(current_command); - g_slist_free(cmdget_funcs); signal_remove("send command", (SIGNAL_FUNC) event_command); diff --git a/src/core/commands.h b/src/core/commands.h index 6acf11f3..9dee2e80 100644 --- a/src/core/commands.h +++ b/src/core/commands.h @@ -97,11 +97,11 @@ void command_set_options_module(const char *module, int command_have_option(const char *cmd, const char *option); /* count can have these flags: */ -#define PARAM_WITHOUT_FLAGS(a) ((a) & 0x00ffffff) +#define PARAM_WITHOUT_FLAGS(a) ((a) & 0x00000fff) /* don't check for quotes - "arg1 arg2" is NOT treated as one argument */ -#define PARAM_FLAG_NOQUOTES 0x01000000 +#define PARAM_FLAG_NOQUOTES 0x00001000 /* final argument gets all the rest of the arguments */ -#define PARAM_FLAG_GETREST 0x02000000 +#define PARAM_FLAG_GETREST 0x00002000 /* command contains options - first you need to specify them with command_set_options() function. Example: @@ -124,9 +124,11 @@ int command_have_option(const char *cmd, const char *option); "cmd2" = "another arg" "optnumarg" = "" - this is because "rest" isn't a numeric value */ -#define PARAM_FLAG_OPTIONS 0x04000000 +#define PARAM_FLAG_OPTIONS 0x00004000 /* don't complain about unknown options */ -#define PARAM_FLAG_UNKNOWN_OPTIONS 0x08000000 +#define PARAM_FLAG_UNKNOWN_OPTIONS 0x00008000 +/* optional channel in first argument */ +#define PARAM_FLAG_OPTCHAN 0x00010000 char *cmd_get_param(char **data); /* get parameters from command - you should point free_me somewhere and @@ -137,10 +139,6 @@ int cmd_get_params(const char *data, gpointer *free_me, int count, ...); void cmd_params_free(void *free_me); -typedef char* (*CMD_GET_FUNC) (const char *data, int *count, va_list *args); -void cmd_get_add_func(CMD_GET_FUNC func); -void cmd_get_remove_func(CMD_GET_FUNC func); - void commands_remove_module(const char *module); void commands_init(void); diff --git a/src/irc/core/irc.c b/src/irc/core/irc.c index 7af48fe2..e3ba24a2 100644 --- a/src/irc/core/irc.c +++ b/src/irc/core/irc.c @@ -377,44 +377,8 @@ static void irc_init_server(IRC_SERVER_REC *server) (GInputFunction) irc_parse_incoming, server); } -#define isoptchan(a) \ - (ischannel((a)[0]) || ((a)[0] == '*' && ((a)[1] == '\0' || (a)[1] == ' '))) - -static char *irc_cmd_get_func(const char *data, int *count, va_list *vargs) -{ - IRC_CHANNEL_REC *channel; - char *ret, *args, *chan, *p; - - if ((*count & PARAM_FLAG_OPTCHAN) == 0) - return g_strdup(data); - - *count &= ~PARAM_FLAG_OPTCHAN; - channel = (void *) va_arg(*vargs, void *); - channel = IRC_CHANNEL(channel); - - /* change first argument in data to full channel name. */ - p = args = g_strdup(data); - - chan = isoptchan(args) ? cmd_get_param(&args) : NULL; - if (chan != NULL && *chan == '!' && channel != NULL) { - /* whenever trying to send something to !channel, - change it to the real joined !XXXXXchannel */ - channel = irc_channel_find(channel->server, chan); - if (channel != NULL) chan = channel->name; - } - - if (chan == NULL || strcmp(chan, "*") == 0) - chan = channel == NULL ? "*" : channel->name; - - ret = g_strconcat(chan, " ", args, NULL); - g_free(p); - return ret; -} - void irc_irc_init(void) { - cmd_get_add_func(irc_cmd_get_func); - signal_add("server event", (SIGNAL_FUNC) irc_server_event); signal_add("server connected", (SIGNAL_FUNC) irc_init_server); signal_add("server incoming", (SIGNAL_FUNC) irc_parse_incoming_line); diff --git a/src/irc/core/irc.h b/src/irc/core/irc.h index c4e4ed10..17a18635 100644 --- a/src/irc/core/irc.h +++ b/src/irc/core/irc.h @@ -50,12 +50,8 @@ void irc_send_cmd_now(IRC_SERVER_REC *server, const char *cmd); void irc_send_cmd_full(IRC_SERVER_REC *server, const char *cmd, int send_now, int immediate, int raw); -#include "commands.h" /* contains the generic PARAM_FLAG_xxx defines */ - -/* IRC specific: optional channel in first argument */ -#define PARAM_FLAG_OPTCHAN 0x10000000 - /* Get count parameters from data */ +#include "commands.h" char *event_get_param(char **data); char *event_get_params(const char *data, int count, ...); |