1
0
mirror of https://github.com/weechat/weechat.git synced 2026-07-08 02:33:12 +02:00

Add new command /version, add count of /upgrade, fix bugs with command exec

New (core) command /version displays version, and number of upgrades done with
first start date (if # /upgrade > 0).
The number of upgrades is displayed at startup (if > 0).
This commit is contained in:
Sebastien Helleu
2009-03-21 16:09:18 +01:00
parent 8a68adbf3f
commit 2dc13ebd21
17 changed files with 499 additions and 279 deletions
+90 -22
View File
@@ -766,7 +766,7 @@ command_command (void *data, struct t_gui_buffer *buffer,
if (argc > 2)
{
ptr_plugin = NULL;
if (string_strcasecmp (argv[1], "weechat") != 0)
if (string_strcasecmp (argv[1], PLUGIN_CORE) != 0)
{
ptr_plugin = plugin_search (argv[1]);
if (!ptr_plugin)
@@ -1243,22 +1243,16 @@ command_help (void *data, struct t_gui_buffer *buffer,
&& HOOK_COMMAND(ptr_hook, command)
&& HOOK_COMMAND(ptr_hook, command)[0])
{
gui_chat_printf (NULL, " %s%s%s%s%s%s%s%s",
gui_chat_printf (NULL, " %s%s%s%s%s",
GUI_COLOR(GUI_COLOR_CHAT_BUFFER),
(HOOK_COMMAND(ptr_hook, level) > 0) ?
"(" : "",
HOOK_COMMAND(ptr_hook, command),
(HOOK_COMMAND(ptr_hook, level) > 0) ?
")" : "",
GUI_COLOR(GUI_COLOR_CHAT),
(HOOK_COMMAND(ptr_hook, description)
&& HOOK_COMMAND(ptr_hook, description)[0]) ?
" - " : "",
(HOOK_COMMAND(ptr_hook, description)
&& HOOK_COMMAND(ptr_hook, description)[0]) ?
_(HOOK_COMMAND(ptr_hook, description)) : "",
(HOOK_COMMAND(ptr_hook, level) > 0) ?
_(" (used by a plugin)") : "");
_(HOOK_COMMAND(ptr_hook, description)) : "");
}
}
gui_chat_printf (NULL, "");
@@ -1271,22 +1265,16 @@ command_help (void *data, struct t_gui_buffer *buffer,
&& HOOK_COMMAND(ptr_hook, command)
&& HOOK_COMMAND(ptr_hook, command)[0])
{
gui_chat_printf (NULL, " %s%s%s%s%s%s%s%s",
gui_chat_printf (NULL, " %s%s%s%s%s",
GUI_COLOR(GUI_COLOR_CHAT_BUFFER),
(HOOK_COMMAND(ptr_hook, level) > 0) ?
"(" : "",
HOOK_COMMAND(ptr_hook, command),
(HOOK_COMMAND(ptr_hook, level) > 0) ?
")" : "",
GUI_COLOR(GUI_COLOR_CHAT),
(HOOK_COMMAND(ptr_hook, description)
&& HOOK_COMMAND(ptr_hook, description)[0]) ?
" - " : "",
(HOOK_COMMAND(ptr_hook, description)
&& HOOK_COMMAND(ptr_hook, description)[0]) ?
_(HOOK_COMMAND(ptr_hook, description)) : "",
(HOOK_COMMAND(ptr_hook, level) > 0) ?
_(" (masked by a plugin)") : "");
_(HOOK_COMMAND(ptr_hook, description)) : "");
}
}
@@ -1300,7 +1288,6 @@ command_help (void *data, struct t_gui_buffer *buffer,
if (!ptr_hook->deleted
&& HOOK_COMMAND(ptr_hook, command)
&& HOOK_COMMAND(ptr_hook, command)[0]
&& (HOOK_COMMAND(ptr_hook, level) == 0)
&& (string_strcasecmp (HOOK_COMMAND(ptr_hook, command),
argv[1]) == 0))
{
@@ -3209,7 +3196,7 @@ command_uptime (void *data, struct t_gui_buffer *buffer,
{
time_t running_time;
int day, hour, min, sec;
char string[256];
char string[512];
/* make C compiler happy */
(void) data;
@@ -3221,7 +3208,7 @@ command_uptime (void *data, struct t_gui_buffer *buffer,
min = ((running_time % (60 * 60 * 24)) % (60 * 60)) / 60;
sec = ((running_time % (60 * 60 * 24)) % (60 * 60)) % 60;
if ((argc == 2) && (string_strcasecmp (argv[1], "-o") == 0))
if ((argc >= 2) && (string_strcasecmp (argv[1], "-o") == 0))
{
snprintf (string, sizeof (string),
_("WeeChat uptime: %d %s %02d:%02d:%02d, started on %s"),
@@ -3260,6 +3247,81 @@ command_uptime (void *data, struct t_gui_buffer *buffer,
return WEECHAT_RC_OK;
}
/*
* command_version_display: display WeeChat version
*/
void
command_version_display (struct t_gui_buffer *buffer,
int send_to_buffer_as_input)
{
char string[512];
if (send_to_buffer_as_input)
{
snprintf (string, sizeof (string),
"WeeChat %s [%s %s %s]",
PACKAGE_VERSION,
_("compiled on"),
__DATE__,
__TIME__);
input_data (buffer, string);
if (weechat_upgrade_count > 0)
{
snprintf (string, sizeof (string),
_("Upgraded %d %s, first start: %s"),
weechat_upgrade_count,
NG_("time", "times", weechat_upgrade_count),
ctime (&weechat_start_time));
string[strlen (string) - 1] = '\0';
input_data (buffer, string);
}
}
else
{
gui_chat_printf (NULL, "%sWeeChat %s %s[%s%s %s %s%s]",
GUI_COLOR(GUI_COLOR_CHAT_BUFFER),
PACKAGE_VERSION,
GUI_COLOR(GUI_COLOR_CHAT_DELIMITERS),
GUI_COLOR(GUI_COLOR_CHAT_HOST),
_("compiled on"),
__DATE__,
__TIME__,
GUI_COLOR(GUI_COLOR_CHAT_DELIMITERS));
if (weechat_upgrade_count > 0)
{
gui_chat_printf (NULL,
_("Upgraded %d %s, first start: %s"),
weechat_upgrade_count,
/* TRANSLATORS: text is: "upgraded xx times" */
NG_("time", "times", weechat_upgrade_count),
ctime (&weechat_start_time));
}
}
}
/*
* command_version: display WeeChat version
*/
int
command_version (void *data, struct t_gui_buffer *buffer,
int argc, char **argv, char **argv_eol)
{
int send_to_buffer_as_input;
/* make C compiler happy */
(void) data;
(void) argv_eol;
send_to_buffer_as_input = ((argc >= 2)
&& (string_strcasecmp (argv[1], "-o") == 0));
command_version_display (buffer, send_to_buffer_as_input);
return WEECHAT_RC_OK;
}
/*
* command_window: manage windows
*/
@@ -3633,7 +3695,7 @@ command_init ()
"command)\n"
"command: command to execute (a '/' is automatically "
"added if not found at beginning of command)"),
"%p|weechat %P",
"%p|" PLUGIN_CORE " %P",
&command_command, NULL);
hook_command (NULL, "debug",
N_("control debug for core/plugins"),
@@ -3850,9 +3912,15 @@ command_init ()
hook_command (NULL, "uptime",
N_("show WeeChat uptime"),
N_("[-o]"),
N_("-o: send uptime on current channel as an IRC message"),
N_("-o: send uptime to current buffer as input"),
"-o",
&command_uptime, NULL);
hook_command (NULL, "version",
N_("show WeeChat version and compilation date"),
N_("[-o]"),
N_("-o: send version to current buffer as input"),
"-o",
&command_version, NULL);
hook_command (NULL, "window",
N_("manage windows"),
N_("[list | -1 | +1 | b# | up | down | left | right | "
+2
View File
@@ -26,5 +26,7 @@ extern int command_reload (void *data, struct t_gui_buffer *buffer,
int argc, char **argv, char **argv_eol);
extern void command_init ();
extern void command_startup (int plugins_looaded);
extern void command_version_display (struct t_gui_buffer *buffer,
int send_to_buffer_as_input);
#endif /* wee-command.h */
+37 -67
View File
@@ -304,7 +304,7 @@ hook_exec_end ()
*/
struct t_hook *
hook_search_command (const char *command)
hook_search_command (struct t_weechat_plugin *plugin, const char *command)
{
struct t_hook *ptr_hook;
@@ -312,11 +312,12 @@ hook_search_command (const char *command)
ptr_hook = ptr_hook->next_hook)
{
if (!ptr_hook->deleted
&& (ptr_hook->plugin == plugin)
&& (string_strcasecmp (HOOK_COMMAND(ptr_hook, command), command) == 0))
return ptr_hook;
}
/* command hook not found */
/* command hook not found for plugin */
return NULL;
}
@@ -331,24 +332,18 @@ hook_command (struct t_weechat_plugin *plugin, const char *command,
const char *completion,
t_hook_callback_command *callback, void *callback_data)
{
struct t_hook *ptr_hook, *new_hook;
struct t_hook *new_hook;
struct t_hook_command *new_hook_command;
if ((string_strcasecmp (command, "builtin") == 0)
&& hook_search_command (command))
return NULL;
/* increase level for command hooks with same command name
so that these commands will not be used any more, until this
one is removed */
for (ptr_hook = weechat_hooks[HOOK_TYPE_COMMAND]; ptr_hook;
ptr_hook = ptr_hook->next_hook)
if (hook_search_command (plugin, command))
{
if (!ptr_hook->deleted
&& (string_strcasecmp (HOOK_COMMAND(ptr_hook, command), command) == 0))
{
HOOK_COMMAND(ptr_hook, level)++;
}
gui_chat_printf (NULL,
_("%sError: another command \"%s\" already exists "
"for plugin \"%s\""),
gui_chat_prefix[GUI_CHAT_PREFIX_ERROR],
command,
plugin_get_name (plugin));
return NULL;
}
new_hook = malloc (sizeof (*new_hook));
@@ -367,7 +362,6 @@ hook_command (struct t_weechat_plugin *plugin, const char *command,
new_hook_command->callback = callback;
new_hook_command->command = (command) ?
strdup (command) : strdup ("");
new_hook_command->level = 0;
new_hook_command->description = (description) ?
strdup (description) : strdup ("");
new_hook_command->args = (args) ?
@@ -400,7 +394,7 @@ hook_command_exec (struct t_gui_buffer *buffer, int any_plugin,
struct t_hook *ptr_hook, *next_hook;
struct t_hook *hook_for_plugin, *hook_for_other_plugin;
char **argv, **argv_eol;
int argc, rc, command_is_running;
int argc, rc, number_for_other_plugin;
if (!buffer || !string || !string[0])
return -1;
@@ -423,31 +417,28 @@ hook_command_exec (struct t_gui_buffer *buffer, int any_plugin,
hook_for_plugin = NULL;
hook_for_other_plugin = NULL;
command_is_running = 0;
number_for_other_plugin = 0;
ptr_hook = weechat_hooks[HOOK_TYPE_COMMAND];
while (ptr_hook)
{
next_hook = ptr_hook->next_hook;
if (!ptr_hook->deleted
&& ((!any_plugin || HOOK_COMMAND(ptr_hook, level) == 0))
&& ((argv[0][0] == '/') && (string_strcasecmp (argv[0] + 1,
HOOK_COMMAND(ptr_hook, command)) == 0)))
{
if (ptr_hook->running > 0)
command_is_running = ptr_hook->running;
if (ptr_hook->running < HOOK_COMMAND_MAX_CALLS)
if (ptr_hook->plugin == plugin)
{
if (ptr_hook->plugin == plugin)
{
if (!hook_for_plugin)
hook_for_plugin = ptr_hook;
}
else
if (!hook_for_plugin)
hook_for_plugin = ptr_hook;
}
else
{
if (any_plugin)
{
if (!hook_for_other_plugin)
hook_for_other_plugin = ptr_hook;
number_for_other_plugin++;
}
}
}
@@ -455,20 +446,28 @@ hook_command_exec (struct t_gui_buffer *buffer, int any_plugin,
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)
if (!hook_for_plugin && !hook_for_other_plugin)
{
rc = -2;
/* command not found */
rc = -1;
}
else
{
if (any_plugin || hook_for_plugin)
if (!hook_for_plugin && (number_for_other_plugin > 1))
{
/* ambiguous: no command for current plugin, but more than one
command was found for other plugins, we don't know which one to
run! */
rc = -2;
}
else
{
ptr_hook = (hook_for_plugin) ?
hook_for_plugin : hook_for_other_plugin;
if (ptr_hook)
if (ptr_hook->running >= HOOK_COMMAND_MAX_CALLS)
rc = -3;
else
{
ptr_hook->running++;
rc = (int) (HOOK_COMMAND(ptr_hook, callback))
@@ -479,16 +478,6 @@ hook_command_exec (struct t_gui_buffer *buffer, int any_plugin,
else
rc = 1;
}
else
{
if (command_is_running)
rc = -3;
}
}
else
{
if (command_is_running)
rc = -3;
}
}
@@ -1948,8 +1937,6 @@ hook_infolist_get (struct t_weechat_plugin *plugin, const char *infolist_name,
void
unhook (struct t_hook *hook)
{
struct t_hook *ptr_hook;
/* invalid hook? */
if (!hook_valid (hook))
return;
@@ -1972,20 +1959,6 @@ unhook (struct t_hook *hook)
switch (hook->type)
{
case HOOK_TYPE_COMMAND:
/* decrease level for command hooks with same command name
and level higher than this one */
for (ptr_hook = weechat_hooks[HOOK_TYPE_COMMAND]; ptr_hook;
ptr_hook = ptr_hook->next_hook)
{
if (!ptr_hook->deleted
&& (ptr_hook != hook)
&& (string_strcasecmp (HOOK_COMMAND(ptr_hook, command),
HOOK_COMMAND(hook, command)) == 0)
&& (HOOK_COMMAND(ptr_hook, level) > HOOK_COMMAND(hook, level)))
{
HOOK_COMMAND(ptr_hook, level)--;
}
}
if (HOOK_COMMAND(hook, command))
free (HOOK_COMMAND(hook, command));
if (HOOK_COMMAND(hook, description))
@@ -2189,8 +2162,6 @@ hook_add_to_infolist_type (struct t_infolist *infolist,
return 0;
if (!infolist_new_var_string (ptr_item, "command", HOOK_COMMAND(ptr_hook, command)))
return 0;
if (!infolist_new_var_integer (ptr_item, "level", HOOK_COMMAND(ptr_hook, level)))
return 0;
if (!infolist_new_var_string (ptr_item, "description",
HOOK_COMMAND(ptr_hook, description)))
return 0;
@@ -2472,7 +2443,6 @@ hook_print_log ()
log_printf (" command data:");
log_printf (" callback . . . . . . : 0x%lx", HOOK_COMMAND(ptr_hook, callback));
log_printf (" command. . . . . . . : '%s'", HOOK_COMMAND(ptr_hook, command));
log_printf (" level. . . . . . . . : %d", HOOK_COMMAND(ptr_hook, level));
log_printf (" description. . . . . : '%s'", HOOK_COMMAND(ptr_hook, description));
log_printf (" args . . . . . . . . : '%s'", HOOK_COMMAND(ptr_hook, args));
log_printf (" args_description . . : '%s'", HOOK_COMMAND(ptr_hook, args_description));
-2
View File
@@ -99,8 +99,6 @@ struct t_hook_command
{
t_hook_callback_command *callback; /* command callback */
char *command; /* name of command (without '/') */
int level; /* when many commands with same name */
/* exist: lower level= high priority */
char *description; /* (for /help) short cmd description */
char *args; /* (for /help) command arguments */
char *args_description; /* (for /help) args long description */
+32 -41
View File
@@ -135,6 +135,14 @@ input_exec_command (struct t_gui_buffer *buffer,
break;
case 1: /* command hooked, OK (executed) */
break;
case -2: /* command is ambigous (exists for other plugins) */
gui_chat_printf (NULL,
_("%sError: ambigous command \"%s\": it exists "
"in many plugins and not in \"%s\" plugin"),
gui_chat_prefix[GUI_CHAT_PREFIX_ERROR],
command + 1,
plugin_get_name (plugin));
break;
case -3: /* command is running */
gui_chat_printf (NULL,
_("%sError: too much calls to command \"%s\" "
@@ -152,8 +160,8 @@ input_exec_command (struct t_gui_buffer *buffer,
else
{
gui_chat_printf (NULL,
_("%sError: unknown command \"%s\" (type /help "
"for help)"),
_("%sError: unknown command \"%s\" (type "
"/help for help)"),
gui_chat_prefix[GUI_CHAT_PREFIX_ERROR],
command + 1);
}
@@ -170,53 +178,36 @@ input_exec_command (struct t_gui_buffer *buffer,
void
input_data (struct t_gui_buffer *buffer, const char *data)
{
char *new_data, *pos;
char *pos;
const char *ptr_data;
if (!buffer || !data || !data[0] || (data[0] == '\r') || (data[0] == '\n'))
return;
/* TODO: modifier for input */
/*new_data = plugin_modifier_exec (PLUGIN_MODIFIER_IRC_USER,
"", data);*/
new_data = strdup (data);
/* no changes in new data */
if (new_data && (strcmp (data, new_data) == 0))
/* use new data (returned by plugin) */
ptr_data = data;
while (ptr_data && ptr_data[0])
{
free (new_data);
new_data = NULL;
}
/* message not dropped? */
if (!new_data || new_data[0])
{
/* use new data (returned by plugin) */
ptr_data = (new_data) ? new_data : data;
pos = strchr (ptr_data, '\n');
if (pos)
pos[0] = '\0';
while (ptr_data && ptr_data[0])
if (input_is_command (ptr_data))
{
pos = strchr (ptr_data, '\n');
if (pos)
pos[0] = '\0';
if (input_is_command (ptr_data))
{
/* WeeChat or plugin command */
(void) input_exec_command (buffer, 1, buffer->plugin, ptr_data);
}
else
{
input_exec_data (buffer, ptr_data);
}
if (pos)
{
pos[0] = '\n';
ptr_data = pos + 1;
}
else
ptr_data = NULL;
/* WeeChat or plugin command */
(void) input_exec_command (buffer, 1, buffer->plugin, ptr_data);
}
else
{
input_exec_data (buffer, ptr_data);
}
if (pos)
{
pos[0] = '\n';
ptr_data = pos + 1;
}
else
ptr_data = NULL;
}
}
+6
View File
@@ -204,6 +204,11 @@ upgrade_weechat_save_uptime (struct t_upgrade_file *upgrade_file)
infolist_free (ptr_infolist);
return 0;
}
if (!infolist_new_var_integer (ptr_item, "upgrade_count", weechat_upgrade_count))
{
infolist_free (ptr_infolist);
return 0;
}
rc = upgrade_file_write_object (upgrade_file,
UPGRADE_WEECHAT_TYPE_UPTIME,
@@ -504,6 +509,7 @@ upgrade_weechat_read_cb (void *data,
break;
case UPGRADE_WEECHAT_TYPE_UPTIME:
weechat_start_time = infolist_time (infolist, "start_time");
weechat_upgrade_count = infolist_integer (infolist, "upgrade_count");
break;
case UPGRADE_WEECHAT_TYPE_HOTLIST:
if (!hotlist_reset)
+6 -11
View File
@@ -74,6 +74,7 @@ int weechat_debug_core = 0; /* debug level for core */
char *weechat_argv0 = NULL; /* WeeChat binary file name (argv[0])*/
int weechat_upgrading = 0; /* =1 if WeeChat is upgrading */
time_t weechat_start_time = 0; /* start time (used by /uptime cmd) */
int weechat_upgrade_count = 0; /* number of /upgrade done */
int weechat_quit = 0; /* = 1 if quit request from user */
int weechat_sigsegv = 0; /* SIGSEGV received? */
char *weechat_home = NULL; /* home dir. (default: ~/.weechat) */
@@ -313,23 +314,14 @@ weechat_welcome_message ()
if (CONFIG_STRING(config_startup_weechat_slogan)
&& CONFIG_STRING(config_startup_weechat_slogan)[0])
{
gui_chat_printf (NULL, _("Welcome to %s%s%s, %s"),
gui_chat_printf (NULL, _("Welcome to %sWeeChat%s, %s"),
GUI_COLOR(GUI_COLOR_CHAT_BUFFER),
PACKAGE_NAME,
GUI_NO_COLOR,
CONFIG_STRING(config_startup_weechat_slogan));
}
if (CONFIG_BOOLEAN(config_startup_display_version))
{
gui_chat_printf (NULL, "%sWeeChat %s %s[%s%s %s %s%s]",
GUI_COLOR(GUI_COLOR_CHAT_BUFFER),
PACKAGE_VERSION,
GUI_COLOR(GUI_COLOR_CHAT_DELIMITERS),
GUI_COLOR(GUI_COLOR_CHAT_HOST),
_("compiled on"),
__DATE__,
__TIME__,
GUI_COLOR(GUI_COLOR_CHAT_DELIMITERS));
command_version_display (NULL, 0);
}
if (CONFIG_BOOLEAN(config_startup_display_logo) ||
(CONFIG_STRING(config_startup_weechat_slogan)
@@ -406,7 +398,10 @@ main (int argc, char *argv[])
exit (EXIT_FAILURE);
gui_main_init (); /* init WeeChat interface */
if (weechat_upgrading)
{
upgrade_weechat_load (); /* upgrade with session file */
weechat_upgrade_count++; /* increase /upgrade count */
}
weechat_welcome_message (); /* display WeeChat welcome message */
command_startup (0); /* command executed before plugins */
plugin_init (weechat_auto_load_plugins, /* init plugin interface(s) */
+1
View File
@@ -102,6 +102,7 @@ extern int weechat_debug_core;
extern char *weechat_argv0;
extern int weechat_upgrading;
extern time_t weechat_start_time;
extern int weechat_upgrade_count;
extern int weechat_quit;
extern char *weechat_home;
extern char *weechat_local_charset;
+3 -5
View File
@@ -219,7 +219,6 @@ gui_completion_search_command (struct t_gui_completion *completion)
if (!ptr_hook->deleted
&& HOOK_COMMAND(ptr_hook, command)
&& HOOK_COMMAND(ptr_hook, command)[0]
&& (HOOK_COMMAND(ptr_hook, level) == 0)
&& (string_strcasecmp (HOOK_COMMAND(ptr_hook, command),
completion->base_command) == 0))
return ptr_hook;
@@ -691,9 +690,9 @@ gui_completion_list_add_plugin_commands (struct t_gui_completion *completion)
if (plugin_name)
{
ptr_plugin = NULL;
if (string_strcasecmp (plugin_name, "weechat") != 0)
if (string_strcasecmp (plugin_name, PLUGIN_CORE) != 0)
{
/* plugin name is different from "weechat", then search it in
/* plugin name is different from "core", then search it in
plugin list */
ptr_plugin = plugin_search (plugin_name);
if (!ptr_plugin)
@@ -1600,8 +1599,7 @@ gui_completion_command (struct t_gui_completion *completion)
{
if (!ptr_hook->deleted
&& HOOK_COMMAND(ptr_hook, command)
&& HOOK_COMMAND(ptr_hook, command)[0]
&& (HOOK_COMMAND(ptr_hook, level) == 0))
&& HOOK_COMMAND(ptr_hook, command)[0])
{
gui_completion_list_add (completion,
HOOK_COMMAND(ptr_hook, command),
+1 -1
View File
@@ -36,7 +36,7 @@ struct t_irc_channel;
{ \
weechat_printf (NULL, \
_("%s%s: command \"%s\" must be executed on " \
"irc buffer"), \
"irc buffer (server or channel)"), \
weechat_prefix ("error"), IRC_PLUGIN_NAME, \
__command); \
return WEECHAT_RC_OK; \