summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorTimo Sirainen <cras@irssi.org>2001-01-28 15:46:00 +0000
committercras <cras@dbcabf3a-b0e7-0310-adc4-f8d773084564>2001-01-28 15:46:00 +0000
commit3927ef9713a5f1cd8adef0336382ff73a3c72df3 (patch)
tree1b74a25691959e94691a7c88e7562c5a404e94b3 /src
parent4339c9a7edaff30309d2572c419eb936bff371b2 (diff)
downloadirssi-3927ef9713a5f1cd8adef0336382ff73a3c72df3.zip
/HELP, /NAMES: using now the column helper function in misc.c. /NAMES
moved to core and is printed immediately instead of sending /NAMES request to server. git-svn-id: http://svn.irssi.org/repos/irssi/trunk@1162 dbcabf3a-b0e7-0310-adc4-f8d773084564
Diffstat (limited to 'src')
-rw-r--r--src/fe-common/core/fe-channels.c152
-rw-r--r--src/fe-common/core/fe-channels.h9
-rw-r--r--src/fe-common/core/fe-common-core.c4
-rw-r--r--src/fe-common/core/fe-core-commands.c103
-rw-r--r--src/fe-common/core/module-formats.c3
-rw-r--r--src/fe-common/core/module-formats.h3
-rw-r--r--src/fe-common/irc/fe-events-numeric.c108
-rw-r--r--src/fe-common/irc/module-formats.c3
-rw-r--r--src/fe-common/irc/module-formats.h3
9 files changed, 246 insertions, 142 deletions
diff --git a/src/fe-common/core/fe-channels.c b/src/fe-common/core/fe-channels.c
index 87409737..77c7b7e4 100644
--- a/src/fe-common/core/fe-channels.c
+++ b/src/fe-common/core/fe-channels.c
@@ -289,6 +289,156 @@ static void cmd_channel_remove(const char *data)
cmd_params_free(free_arg);
}
+static int get_nick_length(void *data)
+{
+ return strlen(((NICK_REC *) data)->nick);
+}
+
+static void display_sorted_nicks(CHANNEL_REC *channel, GSList *nicklist)
+{
+ WINDOW_REC *window;
+ TEXT_DEST_REC dest;
+ GString *str;
+ GSList *tmp;
+ char *format, *stripped;
+ char *linebuf, nickmode[2] = { 0, 0 };
+ int *columns, cols, rows, last_col_rows, col, row, max_width;
+ int item_extra;
+
+ window = window_find_closest(channel->server, channel->name,
+ MSGLEVEL_CLIENTCRAP);
+ max_width = window->width;
+
+ /* get the length of item extra stuff ("[ ] ") */
+ format = format_get_text(MODULE_NAME, NULL,
+ channel->server, channel->name,
+ TXT_NAMES_NICK, " ", "");
+ stripped = strip_codes(format);
+ item_extra = strlen(stripped);
+ g_free(stripped);
+ g_free(format);
+
+ /* remove width of timestamp from max_width */
+ format_create_dest(&dest, channel->server, channel->name,
+ MSGLEVEL_CLIENTCRAP, NULL);
+ format = format_get_line_start(current_theme, &dest, time(NULL));
+ if (format != NULL) {
+ stripped = strip_codes(format);
+ max_width -= strlen(stripped);
+ g_free(stripped);
+ g_free(format);
+ }
+
+ /* calculate columns */
+ cols = get_max_column_count(nicklist, get_nick_length, max_width,
+ item_extra, 3, &columns, &rows);
+ nicklist = columns_sort_list(nicklist, rows);
+
+ /* rows in last column */
+ last_col_rows = rows-(cols*rows-g_slist_length(nicklist));
+ if (last_col_rows == 0)
+ last_col_rows = rows;
+
+ str = g_string_new(NULL);
+ linebuf = g_malloc(max_width+1);
+
+ col = 0; row = 0;
+ for (tmp = nicklist; tmp != NULL; tmp = tmp->next) {
+ NICK_REC *rec = tmp->data;
+
+ nickmode[0] = rec->op ? '@' : rec->voice ? '+' : ' ';
+
+ memset(linebuf, ' ', columns[col]-item_extra);
+ linebuf[columns[col]-item_extra] = '\0';
+ memcpy(linebuf, rec->nick, strlen(rec->nick));
+
+ format = format_get_text(MODULE_NAME, NULL,
+ channel->server, channel->name,
+ TXT_NAMES_NICK, nickmode, linebuf);
+ g_string_append(str, format);
+ g_free(format);
+
+ if (++col == cols) {
+ printtext(channel->server, channel->name,
+ MSGLEVEL_CLIENTCRAP, "%s", str->str);
+ g_string_truncate(str, 0);
+ col = 0; row++;
+
+ if (row == last_col_rows)
+ cols--;
+ }
+ }
+
+ if (str->len != 0) {
+ printtext(channel->server, channel->name,
+ MSGLEVEL_CLIENTCRAP, "%s", str->str);
+ }
+
+ g_slist_free(nicklist);
+ g_string_free(str, TRUE);
+ g_free(columns);
+ g_free(linebuf);
+}
+
+void fe_channels_nicklist(CHANNEL_REC *channel)
+{
+ NICK_REC *nick;
+ GSList *tmp, *nicklist, *sorted;
+ int nicks, normal, voices, ops;
+
+ nicks = normal = voices = ops = 0;
+ nicklist = nicklist_getnicks(channel);
+ sorted = NULL;
+
+ /* sort the nicklist */
+ for (tmp = nicklist; tmp != NULL; tmp = tmp->next) {
+ nick = tmp->data;
+
+ sorted = g_slist_insert_sorted(sorted, nick, (GCompareFunc) nicklist_compare);
+ if (nick->op)
+ ops++;
+ else if (nick->voice)
+ voices++;
+ else
+ normal++;
+ nicks++;
+ }
+ g_slist_free(nicklist);
+
+ /* display the nicks */
+ printformat(channel->server, channel->name,
+ MSGLEVEL_CRAP, TXT_NAMES, channel->name, "");
+ display_sorted_nicks(channel, sorted);
+ g_slist_free(sorted);
+
+ printformat(channel->server, channel->name,
+ MSGLEVEL_CRAP, TXT_ENDOFNAMES,
+ channel->name, nicks, ops, voices, normal);
+}
+
+static void cmd_names(const char *data, SERVER_REC *server, WI_ITEM_REC *item)
+{
+ CHANNEL_REC *channel;
+
+ g_return_if_fail(data != NULL);
+
+ if (server == NULL || !server->connected)
+ cmd_return_error(CMDERR_NOT_CONNECTED);
+
+ if (strcmp(data, "*") == 0) {
+ if (!IS_CHANNEL(item))
+ cmd_return_error(CMDERR_NOT_JOINED);
+
+ data = item->name;
+ }
+
+ channel = channel_find(server, data);
+ if (channel != NULL) {
+ fe_channels_nicklist(channel);
+ signal_stop();
+ }
+}
+
void fe_channels_init(void)
{
settings_add_bool("lookandfeel", "autoclose_windows", TRUE);
@@ -306,6 +456,7 @@ void fe_channels_init(void)
command_bind("channel add", NULL, (SIGNAL_FUNC) cmd_channel_add);
command_bind("channel remove", NULL, (SIGNAL_FUNC) cmd_channel_remove);
command_bind("channel list", NULL, (SIGNAL_FUNC) cmd_channel_list);
+ command_bind("names", NULL, (SIGNAL_FUNC) cmd_names);
command_set_options("channel add", "auto noauto -bots -botcmd");
command_set_options("join", "window");
@@ -326,4 +477,5 @@ void fe_channels_deinit(void)
command_unbind("channel add", (SIGNAL_FUNC) cmd_channel_add);
command_unbind("channel remove", (SIGNAL_FUNC) cmd_channel_remove);
command_unbind("channel list", (SIGNAL_FUNC) cmd_channel_list);
+ command_unbind("names", (SIGNAL_FUNC) cmd_names);
}
diff --git a/src/fe-common/core/fe-channels.h b/src/fe-common/core/fe-channels.h
new file mode 100644
index 00000000..3d6d31ab
--- /dev/null
+++ b/src/fe-common/core/fe-channels.h
@@ -0,0 +1,9 @@
+#ifndef __FE_CHANNELS_H
+#define __FE_CHANNELS_H
+
+void fe_channels_nicklist(CHANNEL_REC *channel);
+
+void fe_channels_init(void);
+void fe_channels_deinit(void);
+
+#endif
diff --git a/src/fe-common/core/fe-common-core.c b/src/fe-common/core/fe-common-core.c
index d0c3e7c5..f01825bd 100644
--- a/src/fe-common/core/fe-common-core.c
+++ b/src/fe-common/core/fe-common-core.c
@@ -33,6 +33,7 @@
#include "formats.h"
#include "themes.h"
#include "translation.h"
+#include "fe-channels.h"
#include "fe-windows.h"
#include "window-items.h"
#include "window-save.h"
@@ -42,9 +43,6 @@
void autorun_init(void);
void autorun_deinit(void);
-void fe_channels_init(void);
-void fe_channels_deinit(void);
-
void fe_core_log_init(void);
void fe_core_log_deinit(void);
diff --git a/src/fe-common/core/fe-core-commands.c b/src/fe-common/core/fe-core-commands.c
index 40ded457..87e26a9c 100644
--- a/src/fe-common/core/fe-core-commands.c
+++ b/src/fe-common/core/fe-core-commands.c
@@ -67,45 +67,72 @@ static int commands_compare(COMMAND_REC *rec, COMMAND_REC *rec2)
return strcmp(rec->cmd, rec2->cmd);
}
-static void help_category(GSList *cmdlist, gint items, gint max)
+static int get_cmd_length(void *data)
{
- COMMAND_REC *rec;
- GString *str;
- GSList *tmp;
- gint lines, cols, line, col, skip;
- gchar *cmdbuf;
+ return strlen(((COMMAND_REC *) data)->cmd);
+}
- str = g_string_new(NULL);
+static void help_category(GSList *cmdlist, int items)
+{
+ WINDOW_REC *window;
+ TEXT_DEST_REC dest;
+ GString *str;
+ GSList *tmp;
+ int *columns, cols, rows, col, row, last_col_rows, max_width;
+ char *linebuf, *format, *stripped;
+
+ window = window_find_closest(NULL, NULL, MSGLEVEL_CLIENTCRAP);
+ max_width = window->width;
+
+ /* remove width of timestamp from max_width */
+ format_create_dest(&dest, NULL, NULL, MSGLEVEL_CLIENTCRAP, NULL);
+ format = format_get_line_start(current_theme, &dest, time(NULL));
+ if (format != NULL) {
+ stripped = strip_codes(format);
+ max_width -= strlen(stripped);
+ g_free(stripped);
+ g_free(format);
+ }
- cols = max > 65 ? 1 : (65 / max);
- lines = items <= cols ? 1 : items / cols+1;
+ /* calculate columns */
+ cols = get_max_column_count(cmdlist, get_cmd_length,
+ max_width, 1, 3, &columns, &rows);
+ cmdlist = columns_sort_list(cmdlist, rows);
- cmdbuf = g_malloc(max+1); cmdbuf[max] = '\0';
- for (line = 0, col = 0, skip = 1, tmp = cmdlist; line < lines; tmp = tmp->next)
- {
- rec = tmp->data;
+ /* rows in last column */
+ last_col_rows = rows-(cols*rows-g_slist_length(cmdlist));
+ if (last_col_rows == 0)
+ last_col_rows = rows;
- if (--skip == 0)
- {
- skip = lines;
- memset(cmdbuf, ' ', max);
- memcpy(cmdbuf, rec->cmd, strlen(rec->cmd));
- g_string_sprintfa(str, "%s ", cmdbuf);
- cols++;
- }
+ str = g_string_new(NULL);
+ linebuf = g_malloc(max_width+1);
- if (col == cols || tmp->next == NULL)
- {
- printtext_string(NULL, NULL, MSGLEVEL_CLIENTCRAP, str->str);
- g_string_truncate(str, 0);
- col = 0; line++;
- tmp = g_slist_nth(cmdlist, line-1); skip = 1;
+ col = 0; row = 0;
+ for (tmp = cmdlist; tmp != NULL; tmp = tmp->next) {
+ COMMAND_REC *rec = tmp->data;
+
+ memset(linebuf, ' ', columns[col]);
+ linebuf[columns[col]] = '\0';
+ memcpy(linebuf, rec->cmd, strlen(rec->cmd));
+ g_string_append(str, linebuf);
+
+ if (++col == cols) {
+ printtext(NULL, NULL,
+ MSGLEVEL_CLIENTCRAP, "%s", str->str);
+ g_string_truncate(str, 0);
+ col = 0; row++;
+
+ if (row == last_col_rows)
+ cols--;
+ }
}
- }
- if (str->len != 0)
- printtext_string(NULL, NULL, MSGLEVEL_CLIENTCRAP, str->str);
- g_string_free(str, TRUE);
- g_free(cmdbuf);
+ if (str->len != 0)
+ printtext(NULL, NULL, MSGLEVEL_CLIENTCRAP, "%s", str->str);
+
+ g_slist_free(cmdlist);
+ g_string_free(str, TRUE);
+ g_free(columns);
+ g_free(linebuf);
}
static int show_help_rec(COMMAND_REC *cmd)
@@ -148,7 +175,7 @@ static void show_help(const char *data)
{
COMMAND_REC *rec, *last, *helpitem;
GSList *tmp, *cmdlist;
- gint len, max, items, findlen;
+ gint items, findlen;
gboolean header, found;
g_return_if_fail(data != NULL);
@@ -158,7 +185,7 @@ static void show_help(const char *data)
/* print command, sort by category */
cmdlist = NULL; last = NULL; header = FALSE; helpitem = NULL;
- max = items = 0; findlen = strlen(data); found = FALSE;
+ items = 0; findlen = strlen(data); found = FALSE;
for (tmp = commands; tmp != NULL; last = rec, tmp = tmp->next)
{
rec = tmp->data;
@@ -179,11 +206,11 @@ static void show_help(const char *data)
printtext(NULL, NULL, MSGLEVEL_CLIENTCRAP, "");
printtext(NULL, NULL, MSGLEVEL_CLIENTCRAP, "%s:", last->category);
}
- help_category(cmdlist, items, max);
+ help_category(cmdlist, items);
}
g_slist_free(cmdlist); cmdlist = NULL;
- items = 0; max = 0;
+ items = 0;
}
if (last != NULL && g_strcasecmp(rec->cmd, last->cmd) == 0)
@@ -201,8 +228,6 @@ static void show_help(const char *data)
else if (strchr(rec->cmd+findlen+1, ' ') == NULL)
{
/* not a subcommand (and matches the query) */
- len = strlen(rec->cmd);
- if (max < len) max = len;
items++;
cmdlist = g_slist_append(cmdlist, rec);
found = TRUE;
@@ -236,7 +261,7 @@ static void show_help(const char *data)
printtext(NULL, NULL, MSGLEVEL_CLIENTCRAP, "");
printtext(NULL, NULL, MSGLEVEL_CLIENTCRAP, "%s:", last->category);
}
- help_category(cmdlist, items, max);
+ help_category(cmdlist, items);
g_slist_free(cmdlist);
}
}
diff --git a/src/fe-common/core/module-formats.c b/src/fe-common/core/module-formats.c
index 1762712c..04a2fe42 100644
--- a/src/fe-common/core/module-formats.c
+++ b/src/fe-common/core/module-formats.c
@@ -77,6 +77,9 @@ FORMAT_REC fecommon_core_formats[] = {
{ "talking_in", "You are now talking in {channel $0}", 1, { 0 } },
{ "not_in_channels", "You are not on any channels", 0 },
{ "current_channel", "Current channel {channel $0}", 1, { 0 } },
+ { "names", "{names_users Users {names_channel $0}} $1", 2, { 0, 0 } },
+ { "names_nick", "{names_nick $0 $1}", 2, { 0, 0 } },
+ { "endofnames", "{channel $0}: Total of {hilight $1} nicks {comment {hilight $2} ops, {hilight $3} voices, {hilight $4} normal}", 5, { 0, 1, 1, 1, 1 } },
{ "chanlist_header", "You are on the following channels:", 0 },
{ "chanlist_line", "{channel $[-10]0} %|+$1 ($2): $3", 4, { 0, 0, 0, 0 } },
{ "chansetup_not_found", "Channel {channel $0} not found", 2, { 0, 0 } },
diff --git a/src/fe-common/core/module-formats.h b/src/fe-common/core/module-formats.h
index 0ead5f9b..38c57abd 100644
--- a/src/fe-common/core/module-formats.h
+++ b/src/fe-common/core/module-formats.h
@@ -53,6 +53,9 @@ enum {
TXT_TALKING_IN,
TXT_NOT_IN_CHANNELS,
TXT_CURRENT_CHANNEL,
+ TXT_NAMES,
+ TXT_NAMES_NICK,
+ TXT_ENDOFNAMES,
TXT_CHANLIST_HEADER,
TXT_CHANLIST_LINE,
TXT_CHANSETUP_NOT_FOUND,
diff --git a/src/fe-common/irc/fe-events-numeric.c b/src/fe-common/irc/fe-events-numeric.c
index 64ec6a1f..01ec72ac 100644
--- a/src/fe-common/irc/fe-events-numeric.c
+++ b/src/fe-common/irc/fe-events-numeric.c
@@ -30,7 +30,9 @@
#include "irc-channels.h"
#include "nicklist.h"
+#include "../core/module-formats.h"
#include "printtext.h"
+#include "fe-channels.h"
static void event_received(IRC_SERVER_REC *server, const char *data);
@@ -69,97 +71,12 @@ static void event_names_list(IRC_SERVER_REC *server, const char *data)
g_return_if_fail(data != NULL);
params = event_get_params(data, 4, NULL, NULL, &channel, &names);
- if (irc_channel_find(server, channel) == NULL)
- printformat(server, channel, MSGLEVEL_CRAP, IRCTXT_NAMES, channel, names);
- g_free(params);
-}
-
-static void display_sorted_nicks(CHANNEL_REC *channel, GSList *nicklist, gint items, gint max)
-{
- NICK_REC *rec;
- GString *str;
- GSList *tmp;
- gint lines, cols, line, col, skip;
- gchar *linebuf;
-
- max++; /* op/voice */
- str = g_string_new(NULL);
-
- cols = max > 65 ? 1 : (65 / (max+3)); /* "[] " */
- lines = items <= cols ? 1 : items/cols + 1;
- if (lines > items) lines = items;
-
- linebuf = g_malloc(max+1); linebuf[max] = '\0';
- for (line = 0, col = 0, skip = 1, tmp = nicklist; line < lines; tmp = tmp->next)
- {
- rec = tmp->data;
-
- if (--skip == 0)
- {
- char *ret, nickmode[2] = { 0, 0 };
- skip = lines;
- memset(linebuf, ' ', max);
- nickmode[0] = rec->op ? '@' : rec->voice ? '+' : ' ';
- memcpy(linebuf, rec->nick, strlen(rec->nick));
- ret = format_get_text(MODULE_NAME, NULL,
- channel->server, channel->name,
- IRCTXT_NAMES_NICK, nickmode, linebuf);
- g_string_append(str, ret);
- g_free(ret);
- cols++;
+ if (irc_channel_find(server, channel) == NULL) {
+ printformat_module("fe-common/core", server, channel,
+ MSGLEVEL_CRAP, TXT_NAMES,
+ channel, names);
}
-
- if (col == cols || tmp->next == NULL)
- {
- printtext(channel->server, channel->name, MSGLEVEL_CLIENTCRAP, "%s", str->str);
- g_string_truncate(str, 0);
- col = 0; line++;
- tmp = g_slist_nth(nicklist, line-1); skip = 1;
- }
- }
- if (str->len != 0)
- printtext(channel->server, channel->name, MSGLEVEL_CLIENTCRAP, "%s", str->str);
- g_string_free(str, TRUE);
- g_free(linebuf);
-}
-
-static void display_nicks(CHANNEL_REC *channel)
-{
- NICK_REC *nick;
- GSList *tmp, *nicklist, *sorted;
- gint nicks, normal, voices, ops, len, max;
-
- nicks = normal = voices = ops = 0;
- nicklist = nicklist_getnicks(channel);
- sorted = NULL;
-
- /* sort the nicklist */
- max = 0;
- for (tmp = nicklist; tmp != NULL; tmp = tmp->next)
- {
- nick = tmp->data;
-
- sorted = g_slist_insert_sorted(sorted, nick, (GCompareFunc) nicklist_compare);
- if (nick->op)
- ops++;
- else if (nick->voice)
- voices++;
- else
- normal++;
- nicks++;
-
- len = strlen(nick->nick);
- if (len > max) max = len;
- }
- g_slist_free(nicklist);
-
- /* display the nicks */
- printformat(channel->server, channel->name, MSGLEVEL_CRAP, IRCTXT_NAMES, channel->name, "");
- display_sorted_nicks(channel, sorted, nicks, max);
- g_slist_free(sorted);
-
- printformat(channel->server, channel->name, MSGLEVEL_CRAP, IRCTXT_ENDOFNAMES,
- channel->name, nicks, ops, voices, normal);
+ g_free(params);
}
static void event_end_of_names(IRC_SERVER_REC *server, const char *data)
@@ -172,10 +89,13 @@ static void event_end_of_names(IRC_SERVER_REC *server, const char *data)
params = event_get_params(data, 2, NULL, &channel);
chanrec = irc_channel_find(server, channel);
- if (chanrec == NULL)
- printformat(server, channel, MSGLEVEL_CRAP, IRCTXT_ENDOFNAMES, channel, 0, 0, 0, 0);
- else
- display_nicks(CHANNEL(chanrec));
+ if (chanrec != NULL)
+ fe_channels_nicklist(CHANNEL(chanrec));
+ else {
+ printformat_module("fe-common/core", server, channel,
+ MSGLEVEL_CRAP, TXT_ENDOFNAMES,
+ channel, 0, 0, 0, 0);
+ }
g_free(params);
}
diff --git a/src/fe-common/irc/module-formats.c b/src/fe-common/irc/module-formats.c
index acf4fea1..3181be96 100644
--- a/src/fe-common/irc/module-formats.c
+++ b/src/fe-common/irc/module-formats.c
@@ -59,9 +59,6 @@ FORMAT_REC fecommon_irc_formats[] = {
{ "channel_rejoin", "Channel {channel $0} is temporarily unavailable, this is normally because of netsplits. Irssi will now automatically try to rejoin back to this channel until the join is successful. Use /RMREJOINS command if you wish to abort this.", 1, { 0 } },
{ "inviting", "Inviting {nick $0} to {channel $1}", 2, { 0, 0 } },
{ "not_invited", "You have not been invited to a channel!", 0 },
- { "names", "{names_users Users {names_channel $0}} $1", 2, { 0, 0 } },
- { "names_nick", "{names_nick $0 $1}", 2, { 0, 0 } },
- { "endofnames", "{channel $0}: Total of {hilight $1} nicks {comment {hilight $2} ops, {hilight $3} voices, {hilight $4} normal}", 5, { 0, 1, 1, 1, 1 } },
{ "channel_created", "Channel {channelhilight $0} created $1", 2, { 0, 0 } },
{ "url", "Home page for {channelhilight $0}: $1", 2, { 0, 0 } },
{ "topic", "Topic for {channelhilight $0}: $1", 2, { 0, 0 } },
diff --git a/src/fe-common/irc/module-formats.h b/src/fe-common/irc/module-formats.h
index a6886332..26ec3e4b 100644
--- a/src/fe-common/irc/module-formats.h
+++ b/src/fe-common/irc/module-formats.h
@@ -36,9 +36,6 @@ enum {
IRCTXT_CHANNEL_REJOIN,
IRCTXT_INVITING,
IRCTXT_NOT_INVITED,
- IRCTXT_NAMES,
- IRCTXT_NAMES_NICK,
- IRCTXT_ENDOFNAMES,
IRCTXT_CHANNEL_CREATED,
IRCTXT_CHANNEL_URL,
IRCTXT_TOPIC,