Keep active and sticky chats visible and fix unread counts

This commit is contained in:
Johannes Findeisen 2026-12-02 01:31:20 +01:00
commit 847886a879
2 changed files with 89 additions and 38 deletions

119
irc9.c
View file

@ -92,6 +92,7 @@ typedef struct Window {
int query_offline;
int nlines;
int unread;
int sticky;
int scroll;
int syncing;
struct timespec join_ts;
@ -104,7 +105,7 @@ typedef struct AutojoinChannel {
char *commands;
const char *next_command;
int joined;
struct timespec command_at;
struct timespec due_at;
struct AutojoinChannel *next;
} AutojoinChannel;
#endif
@ -371,7 +372,7 @@ static int window_visible_in_tree(const Window *w)
{
if (w->type == W_SERVER) return 1;
return !w->srv->tree_folded &&
(!hide_read_windows || w->unread);
(!hide_read_windows || w == curwin || w->sticky || w->unread);
}
static Window *next_visible_window(Window *w)
@ -388,8 +389,7 @@ static void position_tree(int delta, int reveal_current)
int count = 0, current = -1;
for (Server *s = servers; s; s = s->next)
for (Window *w = s->windows; w; w = next_visible_window(w), count++)
if (curwin && (w == curwin ||
(current < 0 && w == curwin->srv->windows))) current = count;
if (w == curwin) current = count;
int offset = tree_offset + delta;
if (reveal_current && current >= 0) {
@ -498,7 +498,6 @@ static void win_append_raw(Window *w, const char *text, int keep_formatting, siz
w->lines[w->nlines++] = (ScrollLine){line, strlen(stamp) + body_offset};
free(clean);
if (w->scroll > 0 && w->scroll < scrollback_limit * MAXWRAPROWS) w->scroll++;
if (w != curwin) w->unread++;
dirty = 1;
}
@ -568,9 +567,9 @@ static void switch_window(int dir)
if (t->windows) { target = t->windows; break; }
} else {
for (Server *s = servers; s; s = s->next)
for (Window *w = s->windows; w; w = w->next) {
for (Window *w = s->windows; w; w = next_visible_window(w)) {
if (w == curwin) goto found;
if (window_visible_in_tree(w)) target = w;
target = w;
}
found:;
if (!target)
@ -1158,14 +1157,12 @@ static void send_autojoin(Server *s)
AutojoinChannel *channel = s->autojoin;
s->autojoin = channel->next;
sendf(s, "JOIN %s", channel->name);
if (channel->commands) {
AutojoinChannel **pending = &s->pending_autojoin;
while (*pending) pending = &(*pending)->next;
channel->next = NULL;
*pending = channel;
} else {
free(channel);
}
AutojoinChannel **pending = &s->pending_autojoin;
while (*pending) pending = &(*pending)->next;
channel->next = NULL;
channel->due_at = now;
channel->due_at.tv_sec += 30;
*pending = channel;
s->next_autojoin = now;
s->next_autojoin.tv_sec += 2;
}
@ -1297,6 +1294,12 @@ static int parse_irc(char *line, char **prefix, char **cmd, char **argv, int max
return n;
}
static void mark_unread_message(Window *w)
{
if (w != curwin && w->type != W_SERVER && w->unread < INT_MAX)
w->unread++;
}
/* Returns 1 when the message was CTCP and has been dealt with. */
static int handle_ctcp(Server *s, const char *nick, char *text,
Window *w, int is_reply)
@ -1318,6 +1321,7 @@ static int handle_ctcp(Server *s, const char *nick, char *text,
if (is_reply) {
append_nickf(w, "* ", nick, " ", "CTCP %s: %s", cmd, arg);
} else if (strcasecmp(cmd, "ACTION") == 0) {
mark_unread_message(w);
append_nickf(w, "* ", nick, " ", "%s", arg);
} else if (strcasecmp(cmd, "VERSION") == 0) {
sendf(s, "NOTICE %s :\001VERSION irc9\001", nick);
@ -1387,8 +1391,8 @@ static void run_autojoin_command(AutojoinChannel **link, Window *w)
take_configured_command(&channel->next_command, line);
if (channel->next_command) {
clock_gettime(CLOCK_MONOTONIC, &channel->command_at);
channel->command_at.tv_sec += 2;
clock_gettime(CLOCK_MONOTONIC, &channel->due_at);
channel->due_at.tv_sec += 2;
} else {
*link = channel->next;
free(channel->commands);
@ -1397,15 +1401,34 @@ static void run_autojoin_command(AutojoinChannel **link, Window *w)
do_command(w, line);
}
static void remove_pending_autojoin(AutojoinChannel **link)
{
AutojoinChannel *channel = *link;
*link = channel->next;
free(channel->commands);
free(channel);
dirty = 1;
}
static AutojoinChannel **find_pending_join(Server *s, const char *name)
{
AutojoinChannel **link = &s->pending_autojoin;
while (*link && ((*link)->joined || strcasecmp((*link)->name, name) != 0))
link = &(*link)->next;
return link;
}
static void confirm_autojoin(Window *w)
{
AutojoinChannel **link = &w->srv->pending_autojoin;
while (*link && ((*link)->joined || strcasecmp((*link)->name, w->name) != 0))
link = &(*link)->next;
AutojoinChannel **link = find_pending_join(w->srv, w->name);
if (!*link) return;
(*link)->joined = 1;
run_autojoin_command(link, w);
dirty = 1;
if ((*link)->next_command)
run_autojoin_command(link, w);
else
remove_pending_autojoin(link);
}
static void run_pending_autojoin_commands(Server *s)
@ -1415,15 +1438,13 @@ static void run_pending_autojoin_commands(Server *s)
clock_gettime(CLOCK_MONOTONIC, &now);
for (AutojoinChannel **link = &s->pending_autojoin; *link; link = &(*link)->next) {
AutojoinChannel *channel = *link;
if (!channel->joined || !command_due(now, channel->command_at)) continue;
if (!command_due(now, channel->due_at)) continue;
Window *w = window_find(s, channel->name);
Window *w = channel->joined ? window_find(s, channel->name) : NULL;
if (w) {
run_autojoin_command(link, w);
} else {
*link = channel->next;
free(channel->commands);
free(channel);
remove_pending_autojoin(link);
}
return;
}
@ -1517,6 +1538,7 @@ static void handle_line(Server *s, char *line)
if (w->type == W_QUERY && hostmask && strchr(hostmask, '!'))
snprintf(w->hostmask, sizeof w->hostmask, "%s", hostmask);
if (narg > 1 && handle_ctcp(s, nick, arg[1], w, notice)) return;
mark_unread_message(w);
if (notice) append_nickf(w, "-", *nick ? nick : "server", "- ", "%s", text);
else append_nickf(w, "<", *nick ? nick : "server", "> ", "%s", text);
return;
@ -1864,6 +1886,15 @@ static void handle_line(Server *s, char *line)
if (narg > 0) appendf(sw, "* %s", arg[narg - 1]);
return;
}
#ifdef USE_AUTOJOIN
if (narg >= 2 && (strcmp(cmd, "403") == 0 || strcmp(cmd, "405") == 0 ||
strcmp(cmd, "471") == 0 || strcmp(cmd, "473") == 0 ||
strcmp(cmd, "474") == 0 || strcmp(cmd, "475") == 0 ||
strcmp(cmd, "476") == 0 || strcmp(cmd, "477") == 0)) {
AutojoinChannel **link = find_pending_join(s, arg[1]);
if (*link) remove_pending_autojoin(link);
}
#endif
if (strcmp(cmd, "461") == 0 || strcmp(cmd, "403") == 0 ||
strcmp(cmd, "404") == 0 || strcmp(cmd, "405") == 0 ||
strcmp(cmd, "482") == 0 ||
@ -2535,8 +2566,11 @@ static void handle_key(int c)
if (c == '\n' || c == '\r') {
if (!*input && curwin && curwin->type == W_SERVER) {
curwin->srv->tree_folded = !curwin->srv->tree_folded;
if (!*input && curwin) {
if (curwin->type == W_SERVER)
curwin->srv->tree_folded = !curwin->srv->tree_folded;
else
curwin->sticky = !curwin->sticky;
hist_cur = -1;
position_tree(0, 1);
dirty = 1;
@ -2767,6 +2801,19 @@ static int irc_to_ansi_color(int color)
return color >= 0 && color < 16 ? map[color] : 9;
}
static long long server_unread_messages(const Server *s)
{
#ifdef USE_AUTOJOIN
if (s->autojoin) return 0;
for (const AutojoinChannel *channel = s->pending_autojoin; channel; channel = channel->next)
if (!channel->joined) return 0;
#endif
long long unread = 0;
for (const Window *w = s->windows; w; w = w->next)
unread += w->unread;
return unread;
}
static void draw_tree(void)
{
int bottom = lines - 3;
@ -2800,9 +2847,7 @@ static void draw_tree(void)
fputs(w == curwin ? (terminal_color ? "\033[1;4m" : "\033[7m") : "\033[1m", stdout);
putchar(s->connected ? '*' : ' ');
if (s->tree_folded) putchar('+');
long long unread = 0;
for (Window *child = s->windows; child; child = child->next)
unread += child->unread;
long long unread = server_unread_messages(s);
int columns = treew - 3 - s->tree_folded;
char count[32] = "";
int count_width = unread ? snprintf(count, sizeof count, " (%lld)", unread) : 0;
@ -2813,9 +2858,6 @@ static void draw_tree(void)
}
continue;
}
char label[CHANLEN + 16];
if (w->unread) snprintf(label, sizeof label, "%s(%d)", w->name, w->unread);
else snprintf(label, sizeof label, "%s", w->name);
if (treew > 5) {
terminal_move(y, 2);
terminal_accent();
@ -2824,7 +2866,14 @@ static void draw_tree(void)
if (w == curwin) fputs(terminal_color ? "\033[1;4m" : "\033[7m", stdout);
if (colored_nicks && terminal_color && w->type == W_QUERY)
printf("\033[%dm", 30 + irc_to_ansi_color(nick_color(s, w->name, strlen(w->name))));
terminal_text(label, treew - 6);
int columns = treew - 6;
char suffix[32] = "";
if (w->unread) snprintf(suffix, sizeof suffix, "%s(%d)", w->sticky ? "*" : "", w->unread);
else if (w->sticky) strcpy(suffix, "*");
int suffix_width = (int)strlen(suffix);
if (suffix_width > columns) suffix_width = w->sticky && columns > 0 ? 1 : 0;
terminal_text(w->name, columns - suffix_width);
if (suffix_width) fwrite(suffix, 1, (size_t)suffix_width, stdout);
fputs("\033[0m", stdout);
}
}