2000-04-26 04:03:38 -04:00
|
|
|
/*
|
|
|
|
fe-core-commands.c : irssi
|
|
|
|
|
2001-02-18 23:44:15 -05:00
|
|
|
Copyright (C) 1999-2001 Timo Sirainen
|
2000-04-26 04:03:38 -04:00
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation; either version 2 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "module.h"
|
|
|
|
#include "module-formats.h"
|
|
|
|
#include "signals.h"
|
|
|
|
#include "commands.h"
|
|
|
|
#include "levels.h"
|
2000-05-04 06:32:42 -04:00
|
|
|
#include "misc.h"
|
2000-04-26 04:03:38 -04:00
|
|
|
#include "line-split.h"
|
2000-06-10 19:40:47 -04:00
|
|
|
#include "settings.h"
|
2000-04-26 04:03:38 -04:00
|
|
|
#include "irssi-version.h"
|
|
|
|
|
2000-11-17 11:27:14 -05:00
|
|
|
#include "fe-windows.h"
|
2000-10-28 16:14:19 -04:00
|
|
|
#include "printtext.h"
|
2000-04-26 04:03:38 -04:00
|
|
|
|
2000-07-02 15:22:30 -04:00
|
|
|
#define PASTE_CHECK_SPEED 200 /* 0.2 sec */
|
|
|
|
|
2000-06-28 13:15:37 -04:00
|
|
|
static int ret_texts[] = {
|
2001-01-07 14:42:59 -05:00
|
|
|
TXT_OPTION_UNKNOWN,
|
|
|
|
TXT_OPTION_AMBIGUOUS,
|
|
|
|
TXT_OPTION_MISSING_ARG,
|
|
|
|
TXT_COMMAND_UNKNOWN,
|
|
|
|
TXT_COMMAND_AMBIGUOUS,
|
2000-06-28 13:15:37 -04:00
|
|
|
-1,
|
2001-01-07 14:42:59 -05:00
|
|
|
TXT_NOT_ENOUGH_PARAMS,
|
|
|
|
TXT_NOT_CONNECTED,
|
|
|
|
TXT_NOT_JOINED,
|
|
|
|
TXT_CHAN_NOT_FOUND,
|
|
|
|
TXT_CHAN_NOT_SYNCED,
|
|
|
|
TXT_NOT_GOOD_IDEA
|
2000-04-26 04:03:38 -04:00
|
|
|
};
|
|
|
|
|
2000-06-10 19:40:47 -04:00
|
|
|
/* keep the whole command line here temporarily. we need it in
|
|
|
|
"default command" event handler, but there we don't know if the start of
|
|
|
|
the line had one or two command chars, and which one.. */
|
|
|
|
static const char *current_cmdline;
|
2000-07-02 13:57:10 -04:00
|
|
|
static int hide_output;
|
2000-06-10 19:40:47 -04:00
|
|
|
|
2000-07-02 15:22:30 -04:00
|
|
|
static GTimeVal time_command_last, time_command_now;
|
|
|
|
static int last_command_cmd, command_cmd;
|
|
|
|
|
2000-12-29 19:42:47 -05:00
|
|
|
/* SYNTAX: ECHO [-current] [-window <name>] [-level <level>] <text> */
|
2000-04-26 04:03:38 -04:00
|
|
|
static void cmd_echo(const char *data, void *server, WI_ITEM_REC *item)
|
|
|
|
{
|
2000-11-25 22:52:23 -05:00
|
|
|
WINDOW_REC *window;
|
|
|
|
GHashTable *optlist;
|
|
|
|
char *msg, *levelstr, *winname;
|
|
|
|
void *free_arg;
|
|
|
|
int level;
|
|
|
|
|
2000-04-26 04:03:38 -04:00
|
|
|
g_return_if_fail(data != NULL);
|
|
|
|
|
2000-11-25 22:52:23 -05:00
|
|
|
if (!cmd_get_params(data, &free_arg, 1 | PARAM_FLAG_OPTIONS |
|
|
|
|
PARAM_FLAG_GETREST, "echo", &optlist, &msg))
|
|
|
|
return;
|
|
|
|
|
|
|
|
levelstr = g_hash_table_lookup(optlist, "level");
|
|
|
|
level = levelstr == NULL ? 0 :
|
|
|
|
level2bits(g_hash_table_lookup(optlist, "level"));
|
|
|
|
if (level == 0) level = MSGLEVEL_CRAP;
|
|
|
|
|
|
|
|
winname = g_hash_table_lookup(optlist, "window");
|
|
|
|
window = winname == NULL ? NULL :
|
|
|
|
is_numeric(winname, '\0') ?
|
|
|
|
window_find_refnum(atoi(winname)) :
|
|
|
|
window_find_item(NULL, winname);
|
|
|
|
if (window == NULL) window = active_win;
|
|
|
|
|
|
|
|
printtext_window(window, level, "%s", msg);
|
|
|
|
cmd_params_free(free_arg);
|
2000-04-26 04:03:38 -04:00
|
|
|
}
|
|
|
|
|
2000-07-23 19:19:22 -04:00
|
|
|
/* SYNTAX: VERSION */
|
2000-04-26 04:03:38 -04:00
|
|
|
static void cmd_version(char *data)
|
|
|
|
{
|
|
|
|
g_return_if_fail(data != NULL);
|
|
|
|
|
2001-01-07 08:53:58 -05:00
|
|
|
if (*data == '\0') {
|
|
|
|
printtext(NULL, NULL, MSGLEVEL_CLIENTNOTICE,
|
|
|
|
"Client: "PACKAGE" " IRSSI_VERSION);
|
|
|
|
}
|
2000-04-26 04:03:38 -04:00
|
|
|
}
|
|
|
|
|
2000-07-23 19:19:22 -04:00
|
|
|
/* SYNTAX: CAT <file> */
|
2000-05-04 06:32:42 -04:00
|
|
|
static void cmd_cat(const char *data)
|
|
|
|
{
|
|
|
|
LINEBUF_REC *buffer = NULL;
|
2000-06-17 21:18:12 -04:00
|
|
|
char *fname, *fposstr;
|
|
|
|
char tmpbuf[1024], *str;
|
|
|
|
void *free_arg;
|
2000-06-01 21:15:19 -04:00
|
|
|
int f, ret, recvlen, fpos;
|
|
|
|
|
2000-06-17 21:18:12 -04:00
|
|
|
if (!cmd_get_params(data, &free_arg, 2, &fname, &fposstr))
|
|
|
|
return;
|
|
|
|
|
2000-06-01 21:15:19 -04:00
|
|
|
fname = convert_home(fname);
|
|
|
|
fpos = atoi(fposstr);
|
2000-06-17 21:18:12 -04:00
|
|
|
cmd_params_free(free_arg);
|
2000-05-04 06:32:42 -04:00
|
|
|
|
|
|
|
f = open(fname, O_RDONLY);
|
|
|
|
g_free(fname);
|
2000-06-01 21:15:19 -04:00
|
|
|
|
2000-05-04 06:32:42 -04:00
|
|
|
if (f == -1) {
|
|
|
|
/* file not found */
|
2001-01-07 08:53:58 -05:00
|
|
|
printtext(NULL, NULL, MSGLEVEL_CLIENTERROR,
|
|
|
|
"%s", g_strerror(errno));
|
2000-05-04 06:32:42 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2000-06-01 21:15:19 -04:00
|
|
|
lseek(f, fpos, SEEK_SET);
|
2000-05-04 06:32:42 -04:00
|
|
|
do {
|
|
|
|
recvlen = read(f, tmpbuf, sizeof(tmpbuf));
|
|
|
|
|
|
|
|
ret = line_split(tmpbuf, recvlen, &str, &buffer);
|
2001-01-07 08:53:58 -05:00
|
|
|
if (ret > 0)
|
|
|
|
printtext(NULL, NULL, MSGLEVEL_CLIENTCRAP, "%s", str);
|
2000-07-04 17:45:54 -04:00
|
|
|
} while (ret > 0);
|
2000-05-04 06:32:42 -04:00
|
|
|
line_split_free(buffer);
|
|
|
|
|
|
|
|
close(f);
|
|
|
|
}
|
|
|
|
|
2000-07-23 19:19:22 -04:00
|
|
|
/* SYNTAX: BEEP */
|
2000-05-04 06:32:42 -04:00
|
|
|
static void cmd_beep(void)
|
|
|
|
{
|
2001-03-03 13:22:09 -05:00
|
|
|
signal_emit("beep", 0);
|
2000-05-04 06:32:42 -04:00
|
|
|
}
|
|
|
|
|
2000-07-02 13:57:10 -04:00
|
|
|
static void sig_stop(void)
|
|
|
|
{
|
|
|
|
signal_stop();
|
|
|
|
}
|
|
|
|
|
2000-06-10 19:40:47 -04:00
|
|
|
static void event_command(const char *data)
|
|
|
|
{
|
2000-07-02 13:57:10 -04:00
|
|
|
const char *cmdchar;
|
|
|
|
|
2000-07-02 15:22:30 -04:00
|
|
|
/* save current command line */
|
2000-07-02 13:57:10 -04:00
|
|
|
current_cmdline = data;
|
|
|
|
|
2000-07-02 15:22:30 -04:00
|
|
|
/* for detecting if we're pasting text */
|
|
|
|
time_command_last = time_command_now;
|
|
|
|
last_command_cmd = command_cmd;
|
|
|
|
|
|
|
|
g_get_current_time(&time_command_now);
|
|
|
|
command_cmd = strchr(settings_get_str("cmdchars"), *data) != NULL;
|
|
|
|
|
|
|
|
/* /^command hides the output of the command */
|
2000-07-02 13:57:10 -04:00
|
|
|
cmdchar = strchr(settings_get_str("cmdchars"), *data);
|
2001-01-07 08:53:58 -05:00
|
|
|
if (cmdchar != NULL && (data[1] == '^' ||
|
|
|
|
(data[1] == *cmdchar && data[2] == '^'))) {
|
2000-07-02 13:57:10 -04:00
|
|
|
hide_output = TRUE;
|
2000-12-04 06:01:37 -05:00
|
|
|
signal_add_first("print starting", (SIGNAL_FUNC) sig_stop);
|
|
|
|
signal_add_first("print format", (SIGNAL_FUNC) sig_stop);
|
2000-07-02 13:57:10 -04:00
|
|
|
signal_add_first("print text stripped", (SIGNAL_FUNC) sig_stop);
|
|
|
|
signal_add_first("print text", (SIGNAL_FUNC) sig_stop);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void event_command_last(const char *data)
|
|
|
|
{
|
|
|
|
if (hide_output) {
|
|
|
|
hide_output = FALSE;
|
2000-12-04 06:01:37 -05:00
|
|
|
signal_remove("print starting", (SIGNAL_FUNC) sig_stop);
|
|
|
|
signal_remove("print format", (SIGNAL_FUNC) sig_stop);
|
2000-07-02 13:57:10 -04:00
|
|
|
signal_remove("print text stripped", (SIGNAL_FUNC) sig_stop);
|
|
|
|
signal_remove("print text", (SIGNAL_FUNC) sig_stop);
|
|
|
|
}
|
2000-06-10 19:40:47 -04:00
|
|
|
}
|
|
|
|
|
2001-01-07 08:53:58 -05:00
|
|
|
static void event_default_command(const char *data, void *server,
|
|
|
|
WI_ITEM_REC *item)
|
2000-06-10 19:40:47 -04:00
|
|
|
{
|
2000-07-02 15:22:30 -04:00
|
|
|
const char *cmdchars, *ptr;
|
2000-06-28 13:15:37 -04:00
|
|
|
char *cmd, *p;
|
2000-07-02 15:22:30 -04:00
|
|
|
long diff;
|
|
|
|
|
|
|
|
cmdchars = settings_get_str("cmdchars");
|
2000-06-10 19:40:47 -04:00
|
|
|
|
2000-06-28 13:15:37 -04:00
|
|
|
ptr = data;
|
|
|
|
while (*ptr != '\0' && *ptr != ' ') {
|
2000-07-02 15:22:30 -04:00
|
|
|
if (strchr(cmdchars, *ptr)) {
|
2000-06-10 19:40:47 -04:00
|
|
|
/* command character inside command .. we probably
|
|
|
|
want to send this text to channel. for example
|
|
|
|
when pasting a path /usr/bin/xxx. */
|
|
|
|
signal_emit("send text", 3, current_cmdline, server, item);
|
|
|
|
return;
|
|
|
|
}
|
2000-06-28 13:15:37 -04:00
|
|
|
ptr++;
|
2000-06-10 19:40:47 -04:00
|
|
|
}
|
|
|
|
|
2000-07-02 15:22:30 -04:00
|
|
|
/* maybe we're copy+pasting text? check how long it was since the
|
|
|
|
last line */
|
|
|
|
diff = get_timeval_diff(&time_command_now, &time_command_last);
|
|
|
|
if (item != NULL && !last_command_cmd && diff < PASTE_CHECK_SPEED) {
|
|
|
|
signal_emit("send text", 3, current_cmdline, active_win->active_server, active_win->active);
|
|
|
|
command_cmd = FALSE;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* get the command part of the line, send "error command" signal */
|
2000-06-28 13:15:37 -04:00
|
|
|
cmd = g_strdup(data);
|
|
|
|
p = strchr(cmd, ' ');
|
|
|
|
if (p != NULL) *p = '\0';
|
|
|
|
|
|
|
|
signal_emit("error command", 2, GINT_TO_POINTER(CMDERR_UNKNOWN), cmd);
|
|
|
|
|
|
|
|
g_free(cmd);
|
2000-06-10 19:40:47 -04:00
|
|
|
}
|
|
|
|
|
2001-01-07 08:53:58 -05:00
|
|
|
static void event_cmderror(void *errorp, const char *arg)
|
2000-04-26 04:03:38 -04:00
|
|
|
{
|
2000-05-15 04:25:45 -04:00
|
|
|
int error;
|
|
|
|
|
|
|
|
error = GPOINTER_TO_INT(errorp);
|
2000-06-28 13:15:37 -04:00
|
|
|
if (error == CMDERR_ERRNO) {
|
|
|
|
/* errno is special */
|
2000-12-19 12:48:42 -05:00
|
|
|
printtext(NULL, NULL, MSGLEVEL_CLIENTERROR, "%s", g_strerror(errno));
|
2000-06-28 13:15:37 -04:00
|
|
|
} else {
|
|
|
|
/* others */
|
|
|
|
printformat(NULL, NULL, MSGLEVEL_CLIENTERROR, ret_texts[error + -CMDERR_OPTION_UNKNOWN], arg);
|
2000-06-17 21:18:12 -04:00
|
|
|
}
|
2000-04-26 04:03:38 -04:00
|
|
|
}
|
|
|
|
|
2001-01-07 08:53:58 -05:00
|
|
|
static void event_list_subcommands(const char *command)
|
|
|
|
{
|
|
|
|
GSList *tmp;
|
|
|
|
GString *str;
|
|
|
|
int len;
|
|
|
|
|
|
|
|
str = g_string_new(NULL);
|
|
|
|
|
|
|
|
len = strlen(command);
|
|
|
|
for (tmp = commands; tmp != NULL; tmp = tmp->next) {
|
|
|
|
COMMAND_REC *rec = tmp->data;
|
|
|
|
|
|
|
|
if (g_strncasecmp(rec->cmd, command, len) == 0 &&
|
|
|
|
rec->cmd[len] == ' ' &&
|
|
|
|
strchr(rec->cmd+len+1, ' ') == NULL) {
|
|
|
|
g_string_sprintfa(str, "%s ", rec->cmd+len+1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (str->len != 0) {
|
|
|
|
g_string_truncate(str, str->len-1);
|
|
|
|
printtext(NULL, NULL, MSGLEVEL_CLIENTERROR, "%s", str->str);
|
|
|
|
}
|
|
|
|
|
|
|
|
g_string_free(str, TRUE);
|
|
|
|
}
|
|
|
|
|
2000-04-26 04:03:38 -04:00
|
|
|
void fe_core_commands_init(void)
|
|
|
|
{
|
2000-07-02 13:57:10 -04:00
|
|
|
hide_output = FALSE;
|
|
|
|
|
2000-07-02 15:22:30 -04:00
|
|
|
command_cmd = FALSE;
|
|
|
|
memset(&time_command_now, 0, sizeof(GTimeVal));
|
|
|
|
|
2000-04-26 04:03:38 -04:00
|
|
|
command_bind("echo", NULL, (SIGNAL_FUNC) cmd_echo);
|
|
|
|
command_bind("version", NULL, (SIGNAL_FUNC) cmd_version);
|
2000-05-04 06:32:42 -04:00
|
|
|
command_bind("cat", NULL, (SIGNAL_FUNC) cmd_cat);
|
|
|
|
command_bind("beep", NULL, (SIGNAL_FUNC) cmd_beep);
|
2000-04-26 04:03:38 -04:00
|
|
|
|
2000-06-10 19:40:47 -04:00
|
|
|
signal_add("send command", (SIGNAL_FUNC) event_command);
|
2000-07-02 13:57:10 -04:00
|
|
|
signal_add_last("send command", (SIGNAL_FUNC) event_command_last);
|
2000-06-10 19:40:47 -04:00
|
|
|
signal_add("default command", (SIGNAL_FUNC) event_default_command);
|
2000-04-26 04:03:38 -04:00
|
|
|
signal_add("error command", (SIGNAL_FUNC) event_cmderror);
|
2001-01-07 08:53:58 -05:00
|
|
|
signal_add("list subcommands", (SIGNAL_FUNC) event_list_subcommands);
|
2000-11-25 22:52:23 -05:00
|
|
|
|
|
|
|
command_set_options("echo", "current +level +window");
|
2000-04-26 04:03:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void fe_core_commands_deinit(void)
|
|
|
|
{
|
|
|
|
command_unbind("echo", (SIGNAL_FUNC) cmd_echo);
|
|
|
|
command_unbind("version", (SIGNAL_FUNC) cmd_version);
|
2000-05-04 06:32:42 -04:00
|
|
|
command_unbind("cat", (SIGNAL_FUNC) cmd_cat);
|
|
|
|
command_unbind("beep", (SIGNAL_FUNC) cmd_beep);
|
2000-04-26 04:03:38 -04:00
|
|
|
|
2000-06-10 19:40:47 -04:00
|
|
|
signal_remove("send command", (SIGNAL_FUNC) event_command);
|
2000-07-02 13:57:10 -04:00
|
|
|
signal_remove("send command", (SIGNAL_FUNC) event_command_last);
|
2000-06-10 19:40:47 -04:00
|
|
|
signal_remove("default command", (SIGNAL_FUNC) event_default_command);
|
2000-04-26 04:03:38 -04:00
|
|
|
signal_remove("error command", (SIGNAL_FUNC) event_cmderror);
|
2001-01-07 08:53:58 -05:00
|
|
|
signal_remove("list subcommands", (SIGNAL_FUNC) event_list_subcommands);
|
2000-04-26 04:03:38 -04:00
|
|
|
}
|