1
0
mirror of https://github.com/weechat/weechat.git synced 2026-07-07 02:03:13 +02:00

Replaced /builtin command by /command, and can now be used to launch commands with same name from different plugins

This commit is contained in:
Sebastien Helleu
2008-04-18 15:39:24 +02:00
parent 22c619040b
commit 1486429cf1
16 changed files with 578 additions and 515 deletions
+32 -18
View File
@@ -561,33 +561,46 @@ command_buffer (void *data, struct t_gui_buffer *buffer,
}
/*
* command_builtin: launch WeeChat builtin command
* command_command: launch explicit WeeChat or plugin command
*/
int
command_builtin (void *data, struct t_gui_buffer *buffer,
command_command (void *data, struct t_gui_buffer *buffer,
int argc, char **argv, char **argv_eol)
{
char *command;
int length;
char *command;
struct t_weechat_plugin *ptr_plugin;
/* make C compiler happy */
(void) data;
if (argc > 1)
if (argc > 2)
{
if (argv[1][0] == '/')
ptr_plugin = NULL;
if (string_strcasecmp (argv[1], "weechat") != 0)
{
input_data (buffer, argv_eol[1], 1);
ptr_plugin = plugin_search (argv[1]);
if (!ptr_plugin)
{
gui_chat_printf (NULL, _("%sPlugin \"%s\" not found"),
gui_chat_prefix[GUI_CHAT_PREFIX_ERROR],
argv[1]);
return WEECHAT_RC_ERROR;
}
}
if (argv_eol[2][0] == '/')
{
input_exec_command (buffer, 0, ptr_plugin, argv_eol[2]);
}
else
{
length = strlen (argv_eol[1]) + 2;
length = strlen (argv_eol[2]) + 2;
command = malloc (length);
if (command)
{
snprintf (command, length, "/%s", argv_eol[1]);
input_data (buffer, command, 1);
snprintf (command, length, "/%s", argv_eol[2]);
input_exec_command (buffer, 0, ptr_plugin, command);
free (command);
}
}
@@ -2142,7 +2155,7 @@ command_uptime (void *data, struct t_gui_buffer *buffer,
sec,
ctime (&weechat_start_time));
string[strlen (string) - 1] = '\0';
input_data (buffer, string, 0);
input_data (buffer, string);
}
else
{
@@ -2504,14 +2517,15 @@ command_init ()
" jump to #weechat: /buffer #weechat"),
"clear|move|close|list|notify|scroll|set|%b|%c %b|%c",
&command_buffer, NULL);
hook_command (NULL, "builtin",
N_("launch WeeChat builtin command (do not look at commands "
"hooked)"),
N_("command"),
N_("command: command to execute (a '/' is automatically "
hook_command (NULL, "command",
N_("launch explicit WeeChat or plugin command"),
N_("plugin command"),
N_(" plugin: plugin name ('weechat' for WeeChat internal "
"command)\n"
"command: command to execute (a '/' is automatically "
"added if not found at beginning of command)"),
"%w",
&command_builtin, NULL);
"%p|weechat %P",
&command_command, NULL);
hook_command (NULL, "filter",
N_("filter messages in buffers, to hide/show them according "
"to tags or regex"),
@@ -2681,7 +2695,7 @@ command_startup (int plugins_loaded)
weechat_buffer = gui_buffer_search_main ();
for (ptr_cmd = commands; *ptr_cmd; ptr_cmd++)
{
input_data (weechat_buffer, *ptr_cmd, 0);
input_data (weechat_buffer, *ptr_cmd);
}
string_free_splitted_command (commands);
}
+46 -18
View File
@@ -366,15 +366,22 @@ hook_command (struct t_weechat_plugin *plugin, char *command, char *description,
* return: 0 if command executed and failed
* 1 if command executed successfully
* -1 if command not found
* -2 if command is ambigous (same command exists
* for another plugin, and we don't know which
* one to run)
*/
int
hook_command_exec (struct t_gui_buffer *buffer, char *string, int only_builtin)
hook_command_exec (struct t_gui_buffer *buffer, int any_plugin,
struct t_weechat_plugin *plugin, char *string)
{
struct t_hook *ptr_hook, *next_hook;
struct t_hook *hook_for_plugin, *hook_for_other_plugin;
char **argv, **argv_eol;
int argc, rc;
rc = -1;
if (!buffer || !string || !string[0])
return -1;
@@ -388,6 +395,8 @@ hook_command_exec (struct t_gui_buffer *buffer, char *string, int only_builtin)
hook_exec_start ();
hook_for_plugin = NULL;
hook_for_other_plugin = NULL;
ptr_hook = weechat_hooks[HOOK_TYPE_COMMAND];
while (ptr_hook)
{
@@ -395,37 +404,56 @@ hook_command_exec (struct t_gui_buffer *buffer, char *string, int only_builtin)
if (!ptr_hook->deleted
&& !ptr_hook->running
&& (HOOK_COMMAND(ptr_hook, level) == 0)
&& (!only_builtin || !ptr_hook->plugin)
/*&& (!ptr_hook->plugin
|| !buffer->plugin
|| (buffer->plugin == ptr_hook->plugin))*/
&& ((!any_plugin || HOOK_COMMAND(ptr_hook, level) == 0))
&& (string_strcasecmp (argv[0] + 1,
HOOK_COMMAND(ptr_hook, command)) == 0))
{
ptr_hook->running = 1;
rc = (int) (HOOK_COMMAND(ptr_hook, callback))
(ptr_hook->callback_data, buffer, argc, argv, argv_eol);
ptr_hook->running = 0;
string_free_exploded (argv);
string_free_exploded (argv_eol);
hook_exec_end ();
if (rc == WEECHAT_RC_ERROR)
return 0;
if (ptr_hook->plugin == plugin)
{
if (!hook_for_plugin)
hook_for_plugin = ptr_hook;
}
else
return 1;
{
if (!hook_for_other_plugin)
hook_for_other_plugin = ptr_hook;
}
}
ptr_hook = next_hook;
}
/* ambiguous: command found for current plugin and other one, we don't know
which one to run! */
if (any_plugin && hook_for_plugin && hook_for_other_plugin)
rc = -2;
else
{
if (any_plugin || hook_for_plugin)
{
ptr_hook = (hook_for_plugin) ?
hook_for_plugin : hook_for_other_plugin;
if (ptr_hook)
{
ptr_hook->running = 1;
rc = (int) (HOOK_COMMAND(ptr_hook, callback))
(ptr_hook->callback_data, buffer, argc, argv, argv_eol);
ptr_hook->running = 0;
if (rc == WEECHAT_RC_ERROR)
rc = 0;
else
rc = 1;
}
}
}
string_free_exploded (argv);
string_free_exploded (argv_eol);
hook_exec_end ();
/* no hook found */
return -1;
return rc;
}
/*
+2 -2
View File
@@ -173,8 +173,8 @@ extern struct t_hook *hook_command (struct t_weechat_plugin *plugin,
char *completion,
t_hook_callback_command *callback,
void *callback_data);
extern int hook_command_exec (struct t_gui_buffer *buffer, char *string,
int only_builtin);
extern int hook_command_exec (struct t_gui_buffer *buffer, int any_plugin,
struct t_weechat_plugin *plugin, char *string);
extern struct t_hook *hook_timer (struct t_weechat_plugin *plugin,
long interval, int align_second,
int max_calls,
+14 -51
View File
@@ -66,17 +66,20 @@ input_is_command (char *line)
*/
int
input_exec_command (struct t_gui_buffer *buffer, char *string,
int only_builtin)
input_exec_command (struct t_gui_buffer *buffer,
int any_plugin,
struct t_weechat_plugin *plugin,
char *string)
{
int rc, argc;
int rc;
char *command, *pos, *ptr_args;
char **argv, **argv_eol;
if ((!string) || (!string[0]) || (string[0] != '/'))
return 0;
command = strdup (string);
if (!command)
return 0;
/* look for end of command */
ptr_args = NULL;
@@ -89,12 +92,7 @@ input_exec_command (struct t_gui_buffer *buffer, char *string,
pos[1] = '\0';
}
rc = hook_command_exec (buffer, command, only_builtin);
/*vars_replaced = alias_replace_vars (window, ptr_args);
rc = plugin_cmd_handler_exec (window->buffer->protocol, command + 1,
(vars_replaced) ? vars_replaced : ptr_args);
if (vars_replaced)
free (vars_replaced);*/
rc = hook_command_exec (buffer, any_plugin, plugin, command);
pos = strchr (command, ' ');
if (pos)
@@ -115,57 +113,23 @@ input_exec_command (struct t_gui_buffer *buffer, char *string,
case 1: /* command hooked, OK (executed) */
break;
default: /* no command hooked */
argv = string_explode (ptr_args, " ", 0, 0, &argc);
argv_eol = string_explode (ptr_args, " ", 1, 0, NULL);
/* should we send unknown command to IRC server? */
/*if (cfg_irc_send_unknown_commands)
{
if (ptr_args)
unknown_command = malloc (strlen (command + 1) + 1 + strlen (ptr_args) + 1);
else
unknown_command = malloc (strlen (command + 1) + 1);
if (unknown_command)
{
strcpy (unknown_command, command + 1);
if (ptr_args)
{
strcat (unknown_command, " ");
strcat (unknown_command, ptr_args);
}
irc_send_cmd_quote (server, channel, unknown_command);
free (unknown_command);
}
}
else
{
gui_chat_printf_error (NULL,
_("Error: unknown command \"%s\" (type /help for help). "
"To send unknown commands to IRC server, enable option "
"irc_send_unknown_commands."),
command + 1);
}*/
gui_chat_printf (NULL,
_("%sError: unknown command \"%s\" (type /help "
"for help)"),
gui_chat_prefix[GUI_CHAT_PREFIX_ERROR],
command + 1);
string_free_exploded (argv);
string_free_exploded (argv_eol);
break;
}
free (command);
return 0;
}
/*
* input_data: read user input and send data to protocol
* input_data: read user input and send data to buffer callback
*/
void
input_data (struct t_gui_buffer *buffer, char *data, int only_builtin)
input_data (struct t_gui_buffer *buffer, char *data)
{
char *new_data, *ptr_data, *pos;
@@ -199,15 +163,14 @@ input_data (struct t_gui_buffer *buffer, char *data, int only_builtin)
if (input_is_command (ptr_data))
{
/* WeeChat or plugin command */
(void) input_exec_command (buffer, ptr_data,
only_builtin);
(void) input_exec_command (buffer, 1, buffer->plugin, ptr_data);
}
else
{
if ((ptr_data[0] == '/') && (ptr_data[1] == '/'))
ptr_data++;
hook_command_exec (buffer, ptr_data, 0);
hook_command_exec (buffer, 1, buffer->plugin, ptr_data);
if (buffer->input_callback)
{
+6 -2
View File
@@ -21,8 +21,12 @@
#define __WEECHAT_INPUT_H 1
struct t_gui_buffer;
struct t_weechat_plugin;
extern void input_data (struct t_gui_buffer *buffer, char *data,
int only_builtin);
extern int input_exec_command (struct t_gui_buffer *buffer,
int any_plugin,
struct t_weechat_plugin *plugin,
char *string);
extern void input_data (struct t_gui_buffer *buffer, char *data);
#endif /* wee-input.h */