summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/fe-common/core/completion.c96
-rw-r--r--src/fe-common/irc/fe-irc-commands.c7
-rw-r--r--src/fe-common/irc/irc-completion.c82
-rw-r--r--src/irc/core/Makefile.am1
-rw-r--r--src/irc/core/irc-commands.c43
-rw-r--r--src/irc/core/irc-commands.h8
-rw-r--r--src/irc/core/irc.c19
-rw-r--r--src/irc/core/irc.h4
8 files changed, 181 insertions, 79 deletions
diff --git a/src/fe-common/core/completion.c b/src/fe-common/core/completion.c
index 6ed04964..33cc8a84 100644
--- a/src/fe-common/core/completion.c
+++ b/src/fe-common/core/completion.c
@@ -34,8 +34,8 @@
iconfig_list_find("completions", "short", completion, "long")
static GList *complist; /* list of commands we're currently completing */
-static char *last_linestart;
-static int last_want_space;
+static char *last_line;
+static int last_want_space, last_line_pos;
#define isseparator_notspace(c) \
((c) == ',')
@@ -106,30 +106,23 @@ static void free_completions(void)
g_list_free(complist);
complist = NULL;
- g_free_and_null(last_linestart);
+ g_free_and_null(last_line);
}
/* manual word completion - called when TAB is pressed */
char *word_complete(WINDOW_REC *window, const char *line, int *pos)
{
+ static int startpos = 0, wordlen = 0;
+
GString *result;
char *word, *wordstart, *linestart, *ret;
- int startpos, wordlen, want_space;
+ int want_space;
g_return_val_if_fail(line != NULL, NULL);
g_return_val_if_fail(pos != NULL, NULL);
- /* get the word we want to complete */
- word = get_word_at(line, *pos, &wordstart);
- startpos = (int) (wordstart-line);
- wordlen = strlen(word);
-
- /* get the start of line until the word we're completing */
- while (wordstart > line && isseparator(wordstart[-1])) wordstart--;
- linestart = g_strndup(line, (int) (wordstart-line));
-
- if (complist != NULL && strcmp(linestart, last_linestart) == 0 &&
- g_strcasecmp(complist->data, word) == 0) {
+ if (complist != NULL && *pos == last_line_pos &&
+ strcmp(line, last_line) == 0) {
/* complete from old list */
complist = complist->next != NULL ? complist->next :
g_list_first(complist);
@@ -138,33 +131,66 @@ char *word_complete(WINDOW_REC *window, const char *line, int *pos)
/* get new completion list */
free_completions();
- last_linestart = g_strdup(linestart);
+ /* get the word we want to complete */
+ word = get_word_at(line, *pos, &wordstart);
+ startpos = (int) (wordstart-line);
+ wordlen = strlen(word);
+
+ /* get the start of line until the word we're completing */
+ while (wordstart > line && isseparator(wordstart[-1])) wordstart--;
+ linestart = g_strndup(line, (int) (wordstart-line));
+
+ /* completions usually add space after the word, that makes
+ things a bit harder. When continuing a completion
+ "/msg nick1 "<tab> we have to cycle to nick2, etc.
+ BUT if we start completion with "/msg "<tab>, we don't
+ want to complete the /msg word, but instead complete empty
+ word with /msg being in linestart. */
+ if (pos > 0 && line[*pos-1] == ' ') {
+ char *old;
+
+ old = linestart;
+ linestart = *linestart == '\0' ?
+ g_strdup(word) :
+ g_strconcat(linestart, " ", word, NULL);
+ g_free(old);
+
+ g_free(word);
+ word = g_strdup("");
+ startpos = (int) (wordstart-line)+wordlen+1;
+ wordlen = 0;
+ }
+
want_space = TRUE;
signal_emit("complete word", 5, &complist, window, word, linestart, &want_space);
last_want_space = want_space;
+
+ g_free(linestart);
+ g_free(word);
}
if (complist == NULL)
- ret = NULL;
- else {
- /* word completed */
- *pos = startpos+strlen(complist->data)+1;
+ return NULL;
- /* replace the word in line - we need to return
- a full new line */
- result = g_string_new(line);
- g_string_erase(result, startpos, wordlen);
- g_string_insert(result, startpos, complist->data);
+ /* word completed */
+ *pos = startpos+strlen(complist->data)+1;
- if (want_space && !isseparator(result->str[*pos-1]))
- g_string_insert_c(result, *pos-1, ' ');
+ /* replace the word in line - we need to return
+ a full new line */
+ result = g_string_new(line);
+ g_string_erase(result, startpos, wordlen);
+ g_string_insert(result, startpos, complist->data);
- ret = result->str;
- g_string_free(result, FALSE);
- }
+ if (want_space && !isseparator(result->str[*pos-1]))
+ g_string_insert_c(result, *pos-1, ' ');
- g_free(linestart);
- g_free(word);
+ wordlen = strlen(complist->data);
+ last_line_pos = *pos;
+ g_free_not_null(last_line);
+ last_line = g_strdup(result->str);
+
+ ret = result->str;
+ g_string_free(result, FALSE);
return ret;
}
@@ -339,7 +365,7 @@ static GList *completion_get_subcommands(const char *cmd)
/* get the number of chars to skip at the start of command. */
spacepos = strrchr(cmd, ' ');
- skip = spacepos == NULL ? 0 :
+ skip = spacepos == NULL ? strlen(cmd)+1 :
((int) (spacepos-cmd) + 1);
len = strlen(cmd);
@@ -370,7 +396,7 @@ GList *completion_get_options(const char *cmd, const char *option)
g_return_val_if_fail(option != NULL, NULL);
rec = command_find(cmd);
- if (rec == NULL) return NULL;
+ if (rec == NULL || rec->options == NULL) return NULL;
list = NULL;
len = strlen(option);
@@ -566,7 +592,7 @@ static void sig_complete_filename(GList **list, WINDOW_REC *window,
void completion_init(void)
{
complist = NULL;
- last_linestart = NULL;
+ last_line = NULL; last_line_pos = -1;
signal_add("complete word", (SIGNAL_FUNC) sig_complete_word);
signal_add("complete command set", (SIGNAL_FUNC) sig_complete_set);
diff --git a/src/fe-common/irc/fe-irc-commands.c b/src/fe-common/irc/fe-irc-commands.c
index 33850798..1852954b 100644
--- a/src/fe-common/irc/fe-irc-commands.c
+++ b/src/fe-common/irc/fe-irc-commands.c
@@ -27,6 +27,7 @@
#include "levels.h"
#include "irc.h"
+#include "irc-commands.h"
#include "server.h"
#include "mode-lists.h"
#include "nicklist.h"
@@ -90,6 +91,7 @@ static void cmd_query(gchar *data, IRC_SERVER_REC *server, WI_IRC_REC *item)
static void cmd_msg(gchar *data, IRC_SERVER_REC *server, WI_ITEM_REC *item)
{
+ GHashTable *optlist;
WINDOW_REC *window;
CHANNEL_REC *channel;
NICK_REC *nickrec;
@@ -99,9 +101,12 @@ static void cmd_msg(gchar *data, IRC_SERVER_REC *server, WI_ITEM_REC *item)
g_return_if_fail(data != NULL);
- if (!cmd_get_params(data, &free_arg, 2 | PARAM_FLAG_GETREST, &target, &msg))
+ if (!cmd_get_params(data, &free_arg, 2 | PARAM_FLAG_OPTIONS |
+ PARAM_FLAG_UNKNOWN_OPTIONS | PARAM_FLAG_GETREST,
+ "msg", &optlist, &target, &msg))
return;
if (*target == '\0' || *msg == '\0') cmd_param_error(CMDERR_NOT_ENOUGH_PARAMS);
+ server = irccmd_options_get_server(optlist, server);
if (*target == '=')
{
diff --git a/src/fe-common/irc/irc-completion.c b/src/fe-common/irc/irc-completion.c
index 362dd84c..b92ff42c 100644
--- a/src/fe-common/irc/irc-completion.c
+++ b/src/fe-common/irc/irc-completion.c
@@ -201,12 +201,15 @@ static void event_privmsg(const char *data, IRC_SERVER_REC *server, const char *
static void cmd_msg(const char *data, IRC_SERVER_REC *server)
{
+ GHashTable *optlist;
char *target, *msg;
void *free_arg;
g_return_if_fail(data != NULL);
- if (!cmd_get_params(data, &free_arg, 2 | PARAM_FLAG_GETREST, &target, &msg))
+ if (!cmd_get_params(data, &free_arg, 2 | PARAM_FLAG_OPTIONS |
+ PARAM_FLAG_UNKNOWN_OPTIONS | PARAM_FLAG_GETREST,
+ "msg", &optlist, &target, &msg))
return;
if (*target != '\0' && *msg != '\0') {
if (!ischannel(*target) && *target != '=' && server != NULL)
@@ -244,12 +247,15 @@ static void sig_nick_changed(CHANNEL_REC *channel, NICK_REC *nick, const char *o
}
}
-static GList *completion_msg(IRC_SERVER_REC *server, const char *nick, const char *prefix)
+/* Complete /MSG from specified server */
+static GList *completion_msg_server(IRC_SERVER_REC *server, const char *nick, const char *prefix)
{
GSList *tmp;
GList *list;
int len;
+ g_return_val_if_fail(nick != NULL, NULL);
+
list = NULL; len = strlen(nick);
for (tmp = server->lastmsgs; tmp != NULL; tmp = tmp->next) {
if (len == 0 || g_strncasecmp(tmp->data, nick, len) == 0) {
@@ -263,6 +269,41 @@ static GList *completion_msg(IRC_SERVER_REC *server, const char *nick, const cha
return list;
}
+/* Complete /MSG - if `server' is NULL, complete nicks from all servers */
+static GList *completion_msg(IRC_SERVER_REC *win_server, IRC_SERVER_REC *find_server,
+ const char *nick, const char *prefix)
+{
+ GSList *tmp;
+ GList *list, *tmplist;
+ char *newprefix;
+
+ g_return_val_if_fail(nick != NULL, NULL);
+ if (servers == NULL) return NULL;
+
+ if (find_server != NULL)
+ return completion_msg_server(find_server, nick, prefix);
+
+ list = NULL;
+ for (tmp = servers; tmp != NULL; tmp = tmp->next) {
+ IRC_SERVER_REC *rec = tmp->data;
+
+ if (rec == win_server)
+ newprefix = g_strdup(prefix);
+ else {
+ newprefix = prefix == NULL ?
+ g_strdup_printf("-%s", rec->tag) :
+ g_strdup_printf("%s -%s", prefix, rec->tag);
+ }
+
+ tmplist = completion_msg_server(rec, nick, newprefix);
+ list = g_list_concat(list, tmplist);
+
+ g_free_not_null(newprefix);
+ }
+
+ return list;
+}
+
static void complete_from_nicklist(GList **outlist, GSList *list,
const char *nick, const char *prefix)
{
@@ -336,6 +377,31 @@ static GList *completion_joinlist(GList *list1, GList *list2)
return list1;
}
+static IRC_SERVER_REC *line_get_server(const char *line)
+{
+ IRC_SERVER_REC *server;
+ const char *ptr;
+ char *tag, *p;
+
+ g_return_val_if_fail(line != NULL, NULL);
+
+ ptr = strchr(line, ' ');
+ if (ptr == NULL) return NULL;
+
+ while (*ptr == ' ') ptr++;
+ if (*ptr != '-') return NULL;
+
+ /* -option found - should be server tag */
+ tag = g_strdup(ptr+1);
+ p = strchr(tag, ' ');
+ if (p != NULL) *p = '\0';
+
+ server = (IRC_SERVER_REC *) server_find_tag(tag);
+
+ g_free(tag);
+ return server;
+}
+
static void sig_complete_word(GList **list, WINDOW_REC *window,
const char *word, const char *linestart)
{
@@ -364,12 +430,13 @@ static void sig_complete_word(GList **list, WINDOW_REC *window,
/* check for /MSG completion */
cmdchars = settings_get_str("cmdchars");
- if (*word == '\0' || (*linestart == '\0' && strchr(cmdchars, *word) != NULL &&
- g_strcasecmp(word+1, "msg") == 0)) {
+ if ((*linestart == '\0' && *word == '\0') ||
+ (*linestart == '\0' && strchr(cmdchars, *word) != NULL &&
+ g_strcasecmp(word+1, "msg") == 0)) {
/* pressed TAB at the start of line - add /MSG
... or ... trying to complete /MSG command */
prefix = g_strdup_printf("%cmsg", *cmdchars);
- *list = completion_msg(server, "", prefix);
+ *list = completion_msg(server, NULL, "", prefix);
if (*list == NULL) *list = g_list_append(*list, g_strdup(prefix));
g_free(prefix);
@@ -380,7 +447,10 @@ static void sig_complete_word(GList **list, WINDOW_REC *window,
if (strchr(cmdchars, *linestart) != NULL &&
g_strcasecmp(linestart+1, "msg") == 0) {
/* completing /MSG nick */
- *list = completion_msg(server, word, NULL);
+ IRC_SERVER_REC *msgserver;
+
+ msgserver = line_get_server(linestart);
+ *list = completion_msg(server, msgserver, word, NULL);
}
/* nick completion .. we could also be completing a nick after /MSG
diff --git a/src/irc/core/Makefile.am b/src/irc/core/Makefile.am
index aa0b87ea..86148ed3 100644
--- a/src/irc/core/Makefile.am
+++ b/src/irc/core/Makefile.am
@@ -41,6 +41,7 @@ noinst_HEADERS = \
channels-setup.h \
ignore.h \
irc.h \
+ irc-commands.h \
irc-server.h \
ircnet-setup.h \
masks.h \
diff --git a/src/irc/core/irc-commands.c b/src/irc/core/irc-commands.c
index fb4e338b..aa7f9a27 100644
--- a/src/irc/core/irc-commands.c
+++ b/src/irc/core/irc-commands.c
@@ -45,6 +45,29 @@ typedef struct {
static GString *tmpstr;
static int knockout_tag;
+/* `optlist' should contain only one key - the server tag.
+ returns NULL if there was unknown -option */
+IRC_SERVER_REC *irccmd_options_get_server(GHashTable *optlist, IRC_SERVER_REC *defserver)
+{
+ SERVER_REC *server;
+ GSList *list;
+
+ /* -<server tag> */
+ list = hashtable_get_keys(optlist);
+ if (list == NULL) return defserver;
+
+ server = server_find_tag(list->data);
+ if (server == NULL || list->next != NULL) {
+ /* unknown option (not server tag) */
+ signal_emit("error command", 2, GINT_TO_POINTER(CMDERR_OPTION_UNKNOWN), list->data);
+
+ server = NULL;
+ }
+
+ g_slist_free(list);
+ return (IRC_SERVER_REC *) server;
+}
+
static IRC_SERVER_REC *connect_server(const char *data)
{
IRC_SERVER_CONNECT_REC *conn;
@@ -206,16 +229,20 @@ static void cmd_quit(const char *data)
static void cmd_msg(const char *data, IRC_SERVER_REC *server, WI_IRC_REC *item)
{
+ GHashTable *optlist;
char *target, *msg;
void *free_arg;
int free_ret;
g_return_if_fail(data != NULL);
- if (!cmd_get_params(data, &free_arg, 2 | PARAM_FLAG_GETREST, &target, &msg))
+ if (!cmd_get_params(data, &free_arg, 2 | PARAM_FLAG_OPTIONS |
+ PARAM_FLAG_UNKNOWN_OPTIONS | PARAM_FLAG_GETREST,
+ "msg", &optlist, &target, &msg))
return;
if (*target == '\0' || *msg == '\0') cmd_param_error(CMDERR_NOT_ENOUGH_PARAMS);
+ server = irccmd_options_get_server(optlist, server);
if (server == NULL || !server->connected || !irc_server_check(server))
cmd_param_error(CMDERR_NOT_CONNECTED);
@@ -300,7 +327,6 @@ static void cmd_nctcp(const char *data, IRC_SERVER_REC *server)
static void cmd_join(const char *data, IRC_SERVER_REC *server)
{
GHashTable *optlist;
- GSList *list;
char *channels;
void *free_arg;
@@ -317,18 +343,7 @@ static void cmd_join(const char *data, IRC_SERVER_REC *server)
channels_join(server, server->last_invite, FALSE);
} else {
/* -<server tag> */
- list = hashtable_get_keys(optlist);
- if (list != NULL) {
- server = (IRC_SERVER_REC *) server_find_tag(list->data);
-
- if (server == NULL) {
- /* unknown option (not server tag) */
- signal_emit("error command", 2, GINT_TO_POINTER(CMDERR_OPTION_UNKNOWN), list->data);
- signal_stop();
- }
- g_slist_free(list);
- }
-
+ server = irccmd_options_get_server(optlist, server);
if (server != NULL) channels_join(server, channels, FALSE);
}
diff --git a/src/irc/core/irc-commands.h b/src/irc/core/irc-commands.h
new file mode 100644
index 00000000..6d4535a0
--- /dev/null
+++ b/src/irc/core/irc-commands.h
@@ -0,0 +1,8 @@
+#ifndef __IRC_COMMANDS_H
+#define __IRC_COMMANDS_H
+
+/* `optlist' should contain only one key - the server tag.
+ returns NULL if there was unknown -option */
+IRC_SERVER_REC *irccmd_options_get_server(GHashTable *optlist, IRC_SERVER_REC *defserver);
+
+#endif
diff --git a/src/irc/core/irc.c b/src/irc/core/irc.c
index 042da35c..d464b098 100644
--- a/src/irc/core/irc.c
+++ b/src/irc/core/irc.c
@@ -183,25 +183,6 @@ void irc_send_cmd_split(IRC_SERVER_REC *server, const char *cmd,
g_free(str);
}
-/* Nick can be in format "servertag/nick" - Update `nick' to
- position "nick" and return "servertag" which you need to free */
-char *irc_nick_get_server(char **nick)
-{
- char *ptr, *tag;
-
- ptr = strchr(*nick, '/');
- if (ptr == NULL) return NULL;
- if (ptr == *nick) {
- (*nick)++;
- return NULL;
- }
-
- tag = g_strndup(*nick, (int) (ptr-*nick));
- *nick = ptr+1;
-
- return tag;
-}
-
/* Get next parameter */
char *event_get_param(char **data)
{
diff --git a/src/irc/core/irc.h b/src/irc/core/irc.h
index 13e1dfca..29560e28 100644
--- a/src/irc/core/irc.h
+++ b/src/irc/core/irc.h
@@ -70,10 +70,6 @@ void irc_send_cmd_split(IRC_SERVER_REC *server, const char *cmd,
and queues. */
void irc_send_cmd_now(IRC_SERVER_REC *server, const char *cmd);
-/* Nick can be in format "servertag/nick" - Update `nick' to
- position "nick" and return "servertag" which you need to free */
-char *irc_nick_get_server(char **nick);
-
#include "commands.h" /* contains the generic PARAM_FLAG_xxx defines */
/* IRC specific: optional channel in first argument */