Enable configured channel autojoin by default

This commit is contained in:
Johannes Findeisen 2026-12-03 01:11:14 +01:00
commit 4ad0f4987e
4 changed files with 16 additions and 47 deletions

View file

@ -11,10 +11,6 @@ CFLAGS += -DUSE_OPENSSL $(shell pkg-config --cflags openssl)
LDLIBS += $(shell pkg-config --libs openssl) LDLIBS += $(shell pkg-config --libs openssl)
endif endif
ifeq ($(AUTOJOIN),1)
CPPFLAGS += -DUSE_AUTOJOIN
endif
irc9: irc9.c irc9: irc9.c
$(CC) $(CPPFLAGS) $(CFLAGS) -D_DARWIN_C_SOURCE -o $@ $< $(LDLIBS) $(CC) $(CPPFLAGS) $(CFLAGS) -D_DARWIN_C_SOURCE -o $@ $< $(LDLIBS)

View file

@ -67,7 +67,7 @@ name Libera Chat
# blacklist user1,user2,user3 # blacklist user1,user2,user3
# sasl irc9user my-sasl-password # sasl irc9user my-sasl-password
# autocommand /query user1,/query user2 # autocommand /query user1,/query user2
# Build with make -B AUTOJOIN=1 and uncomment channel lines to join at startup. # Uncomment channel lines to join at startup.
# Each channel line applies to the server above it. # Each channel line applies to the server above it.
# Optional comma-separated slash commands run after the channel join succeeds. # Optional comma-separated slash commands run after the channel join succeeds.
# irc9 waits two seconds between commands on the same line. # irc9 waits two seconds between commands on the same line.
@ -112,7 +112,7 @@ the next command. The first command can also use raw IRC syntax, as in
The sequence runs once per successful registration, including reconnects, after The sequence runs once per successful registration, including reconnects, after
SASL negotiation when configured and before startup channel joins. SASL negotiation when configured and before startup channel joins.
To enable startup channel joins, build with `make -B AUTOJOIN=1`. Add one To join channels automatically at startup, add one
`channel #name` line per channel below the corresponding `server` line. `channel #name` line per channel below the corresponding `server` line.
After registration, irc9 sends one startup JOIN every two seconds per server. After registration, irc9 sends one startup JOIN every two seconds per server.
Add comma-separated slash commands after the channel name, for example Add comma-separated slash commands after the channel name, for example
@ -120,7 +120,6 @@ Add comma-separated slash commands after the channel name, for example
The first command runs when the server confirms the join. irc9 waits two seconds The first command runs when the server confirms the join. irc9 waits two seconds
between commands on the same line and runs them in the joined channel's context. between commands on the same line and runs them in the joined channel's context.
A comma followed by a slash, with optional spaces or tabs between them, starts the next command. A comma followed by a slash, with optional spaces or tabs between them, starts the next command.
Default builds ignore `channel` lines. Use `make -B` to rebuild with autojoin disabled.
Blank lines and whole-line comments starting with `#` are ignored. Blank lines and whole-line comments starting with `#` are ignored.
TLS certificates are verified against a system CA bundle when one is found. TLS certificates are verified against a system CA bundle when one is found.
@ -174,8 +173,8 @@ MODE, TOPIC, and INVITE events are shown in the relevant window.
| --- | --- | | --- | --- |
| `Tab` | complete a `/` command or a nick; press again to cycle matches (recently completed nicks come first) | | `Tab` | complete a `/` command or a nick; press again to cycle matches (recently completed nicks come first) |
| `Ctrl+Left` / `Ctrl+Right` | move to the previous / next word in the input line | | `Ctrl+Left` / `Ctrl+Right` | move to the previous / next word in the input line |
| `Ctrl+Y` | hide or show the channel tree and channel topic |
| `Ctrl+PgUp` / `Ctrl+PgDn` | scroll the channel tree by one row | | `Ctrl+PgUp` / `Ctrl+PgDn` | scroll the channel tree by one row |
| `Ctrl+Y` | hide or show the channel tree and channel topic |
| `Ctrl+N` or `Ctrl+Down` / `Ctrl+P` or `Ctrl+Up` | next / previous visible window (tree order, wraps around) | | `Ctrl+N` or `Ctrl+Down` / `Ctrl+P` or `Ctrl+Up` | next / previous visible window (tree order, wraps around) |
| Enter | with empty input, fold or unfold a server, or toggle a chat’s sticky status (`*` after its name) | | Enter | with empty input, fold or unfold a server, or toggle a chat’s sticky status (`*` after its name) |
| `Ctrl+O` | toggle hiding inactive channels and private chats with no unread messages. Active and sticky chats stay visible when their server is unfolded | | `Ctrl+O` | toggle hiding inactive channels and private chats with no unread messages. Active and sticky chats stay visible when their server is unfolded |

50
irc9.c
View file

@ -99,7 +99,6 @@ typedef struct Window {
Member *members; Member *members;
} Window; } Window;
#ifdef USE_AUTOJOIN
typedef struct AutojoinChannel { typedef struct AutojoinChannel {
char name[CHANLEN]; char name[CHANLEN];
char *commands; char *commands;
@ -108,7 +107,6 @@ typedef struct AutojoinChannel {
struct timespec due_at; struct timespec due_at;
struct AutojoinChannel *next; struct AutojoinChannel *next;
} AutojoinChannel; } AutojoinChannel;
#endif
typedef struct BlacklistedNick { typedef struct BlacklistedNick {
char nick[NICKLEN]; char nick[NICKLEN];
@ -155,11 +153,9 @@ typedef struct Server {
#ifdef USE_OPENSSL #ifdef USE_OPENSSL
SSL *ssl; SSL *ssl;
#endif #endif
#ifdef USE_AUTOJOIN
AutojoinChannel *autojoin; AutojoinChannel *autojoin;
AutojoinChannel *pending_autojoin; AutojoinChannel *pending_autojoin;
struct timespec next_autojoin; struct timespec next_autojoin;
#endif
struct Server *next; struct Server *next;
} Server; } Server;
@ -198,11 +194,11 @@ static int tree_offset;
static int hide_read_windows; static int hide_read_windows;
static int treew = TREEW, mainh = 1, chat_top = 1; static int treew = TREEW, mainh = 1, chat_top = 1;
static struct termios saved_terminal; static struct termios saved_terminal;
static int tree_hidden;
static int terminal_active; static int terminal_active;
static int terminal_color; static int terminal_color;
static int colored_nicks; static int colored_nicks;
static int copy_view; static int copy_view;
static int tree_hidden;
static long accent_color = -1; static long accent_color = -1;
enum { enum {
@ -814,9 +810,7 @@ static void load_config(const char *path)
char line[1024]; char line[1024];
Server *last = NULL; Server *last = NULL;
#ifdef USE_AUTOJOIN
AutojoinChannel **channel_tail = NULL; AutojoinChannel **channel_tail = NULL;
#endif
while (fgets(line, sizeof line, f)) { while (fgets(line, sizeof line, f)) {
size_t n = strlen(line); size_t n = strlen(line);
while (n && (line[n - 1] == '\n' || line[n - 1] == '\r')) line[--n] = 0; while (n && (line[n - 1] == '\n' || line[n - 1] == '\r')) line[--n] = 0;
@ -860,7 +854,6 @@ static void load_config(const char *path)
} }
a1 = strtok_r(NULL, " \t", &save); a1 = strtok_r(NULL, " \t", &save);
if (strcasecmp(key, "channel") == 0) { if (strcasecmp(key, "channel") == 0) {
#ifdef USE_AUTOJOIN
if (channel_tail && a1 && strlen(a1) < CHANLEN) { if (channel_tail && a1 && strlen(a1) < CHANLEN) {
AutojoinChannel *channel = xmalloc(sizeof *channel); AutojoinChannel *channel = xmalloc(sizeof *channel);
*channel = (AutojoinChannel){0}; *channel = (AutojoinChannel){0};
@ -875,7 +868,6 @@ static void load_config(const char *path)
} else if (*command) } else if (*command)
fprintf(stderr, "irc9: ignoring invalid command for channel %s\n", a1); fprintf(stderr, "irc9: ignoring invalid command for channel %s\n", a1);
} }
#endif
continue; continue;
} }
a2 = strtok_r(NULL, " \t", &save); a2 = strtok_r(NULL, " \t", &save);
@ -905,9 +897,7 @@ static void load_config(const char *path)
s->retry_at = 0; s->retry_at = 0;
if (!servers) servers = s; else last->next = s; if (!servers) servers = s; else last->next = s;
last = s; last = s;
#ifdef USE_AUTOJOIN
channel_tail = &s->autojoin; channel_tail = &s->autojoin;
#endif
window_new(s, s->host, W_SERVER); window_new(s, s->host, W_SERVER);
} else if (strcasecmp(key, "sasl") == 0 && a1 && a2 && last) { } else if (strcasecmp(key, "sasl") == 0 && a1 && a2 && last) {
snprintf(last->sasl_user, sizeof last->sasl_user, "%s", a1); snprintf(last->sasl_user, sizeof last->sasl_user, "%s", a1);
@ -1188,7 +1178,6 @@ static void handle_connect_results(void)
} }
} }
#ifdef USE_AUTOJOIN
/* Pace each server's startup JOINs to avoid flooding the connection. */ /* Pace each server's startup JOINs to avoid flooding the connection. */
static void send_autojoin(Server *s) static void send_autojoin(Server *s)
{ {
@ -1222,7 +1211,6 @@ static void free_autojoin(AutojoinChannel **head)
free(channel); free(channel);
} }
} }
#endif
static void disconnect_server(Server *s) static void disconnect_server(Server *s)
{ {
@ -1243,10 +1231,8 @@ static void disconnect_server(Server *s)
s->cap_state = CAP_NONE; s->cap_state = CAP_NONE;
s->caps[0] = 0; s->caps[0] = 0;
s->retry_at = time(NULL) + RETRY; s->retry_at = time(NULL) + RETRY;
#ifdef USE_AUTOJOIN
free_autojoin(&s->pending_autojoin); free_autojoin(&s->pending_autojoin);
s->next_autojoin = (struct timespec){0}; s->next_autojoin = (struct timespec){0};
#endif
} }
/* ------------------------------------------------------------------ */ /* ------------------------------------------------------------------ */
@ -1429,7 +1415,6 @@ static void run_pending_autocommands(Server *s)
if (command_due(now, s->autocommand_at)) run_autocommand(s); if (command_due(now, s->autocommand_at)) run_autocommand(s);
} }
#ifdef USE_AUTOJOIN
static void run_autojoin_command(AutojoinChannel **link, Window *w) static void run_autojoin_command(AutojoinChannel **link, Window *w)
{ {
AutojoinChannel *channel = *link; AutojoinChannel *channel = *link;
@ -1495,7 +1480,6 @@ static void run_pending_autojoin_commands(Server *s)
return; return;
} }
} }
#endif
static void handle_line(Server *s, char *line) static void handle_line(Server *s, char *line)
{ {
@ -1597,9 +1581,7 @@ static void handle_line(Server *s, char *line)
Window *w = window_get(s, arg[0], W_CHANNEL); Window *w = window_get(s, arg[0], W_CHANNEL);
member_add(w, nick); member_add(w, nick);
if (!silent) append_nickf(w, "* ", hostmask ? hostmask : "someone", " ", "joined %s", arg[0]); if (!silent) append_nickf(w, "* ", hostmask ? hostmask : "someone", " ", "joined %s", arg[0]);
#ifdef USE_AUTOJOIN
if (strcasecmp(nick, s->nick) == 0) confirm_autojoin(w); if (strcasecmp(nick, s->nick) == 0) confirm_autojoin(w);
#endif
return; return;
} }
@ -1935,7 +1917,6 @@ static void handle_line(Server *s, char *line)
if (narg > 0) appendf(sw, "* %s", arg[narg - 1]); if (narg > 0) appendf(sw, "* %s", arg[narg - 1]);
return; return;
} }
#ifdef USE_AUTOJOIN
if (narg >= 2 && (strcmp(cmd, "403") == 0 || strcmp(cmd, "405") == 0 || if (narg >= 2 && (strcmp(cmd, "403") == 0 || strcmp(cmd, "405") == 0 ||
strcmp(cmd, "471") == 0 || strcmp(cmd, "473") == 0 || strcmp(cmd, "471") == 0 || strcmp(cmd, "473") == 0 ||
strcmp(cmd, "474") == 0 || strcmp(cmd, "475") == 0 || strcmp(cmd, "474") == 0 || strcmp(cmd, "475") == 0 ||
@ -1943,7 +1924,6 @@ static void handle_line(Server *s, char *line)
AutojoinChannel **link = find_pending_join(s, arg[1]); AutojoinChannel **link = find_pending_join(s, arg[1]);
if (*link) remove_pending_autojoin(link); if (*link) remove_pending_autojoin(link);
} }
#endif
if (strcmp(cmd, "461") == 0 || strcmp(cmd, "403") == 0 || if (strcmp(cmd, "461") == 0 || strcmp(cmd, "403") == 0 ||
strcmp(cmd, "404") == 0 || strcmp(cmd, "405") == 0 || strcmp(cmd, "404") == 0 || strcmp(cmd, "405") == 0 ||
strcmp(cmd, "482") == 0 || strcmp(cmd, "482") == 0 ||
@ -2464,7 +2444,6 @@ static void do_command(Window *window, char *line)
window_clear(window); window_clear(window);
} else if (strcasecmp(cmd, "reconnect") == 0) { } else if (strcasecmp(cmd, "reconnect") == 0) {
if (!window) return; if (!window) return;
appendf(t, " ctrl-y hide or show the channel tree and topic");
Server *s = window->srv; Server *s = window->srv;
appendf(serverwin(s), "* reconnecting to %s:%d", s->host, s->port); appendf(serverwin(s), "* reconnecting to %s:%d", s->host, s->port);
disconnect_server(s); disconnect_server(s);
@ -2485,6 +2464,7 @@ static void do_command(Window *window, char *line)
appendf(t, "keys: ctrl-n / ctrl-p switch windows, pgup/pgdn scroll, ctrl-l clear screen"); appendf(t, "keys: ctrl-n / ctrl-p switch windows, pgup/pgdn scroll, ctrl-l clear screen");
appendf(t, " ctrl-up / ctrl-down scroll the channel tree"); appendf(t, " ctrl-up / ctrl-down scroll the channel tree");
appendf(t, " ctrl-s save scrollback, ctrl-t toggle copy view"); appendf(t, " ctrl-s save scrollback, ctrl-t toggle copy view");
appendf(t, " ctrl-y hide or show the channel tree and topic");
} else { } else {
if (window) appendf(window, "* unknown command: /%s", cmd); if (window) appendf(window, "* unknown command: /%s", cmd);
} }
@ -2684,12 +2664,6 @@ static void handle_key(int c)
if (c == 0x13 && curwin) { /* ctrl-s */ if (c == 0x13 && curwin) { /* ctrl-s */
saving = 1; saving = 1;
save_path[0] = 0; save_path[0] = 0;
if (c == 0x19) { /* ctrl-y */
tree_hidden = !tree_hidden;
layout();
dirty = 1;
return;
}
save_len = save_pos = 0; save_len = save_pos = 0;
dirty = 1; dirty = 1;
return; return;
@ -2710,6 +2684,12 @@ static void handle_key(int c)
dirty = 1; dirty = 1;
return; return;
} }
if (c == 0x19) { /* ctrl-y */
tree_hidden = !tree_hidden;
layout();
dirty = 1;
return;
}
if (c == 0x0c) { /* ctrl-l */ if (c == 0x0c) { /* ctrl-l */
if (curwin) { curwin->scroll = -(mainh - layout_topic(0)); dirty = 1; } if (curwin) { curwin->scroll = -(mainh - layout_topic(0)); dirty = 1; }
return; return;
@ -2954,11 +2934,9 @@ static int irc_to_ansi_color(int color)
static long long server_unread_messages(const Server *s) static long long server_unread_messages(const Server *s)
{ {
#ifdef USE_AUTOJOIN
if (s->autojoin) return 0; if (s->autojoin) return 0;
for (const AutojoinChannel *channel = s->pending_autojoin; channel; channel = channel->next) for (const AutojoinChannel *channel = s->pending_autojoin; channel; channel = channel->next)
if (!channel->joined) return 0; if (!channel->joined) return 0;
#endif
long long unread = 0; long long unread = 0;
for (const Window *w = s->windows; w; w = w->next) for (const Window *w = s->windows; w; w = w->next)
unread += w->unread; unread += w->unread;
@ -3279,10 +3257,6 @@ static void redraw(void)
input_dirty = 0; input_dirty = 0;
} }
#ifdef VDSUSP
/* Allow Ctrl+Y to reach the key handler on terminals with delayed suspend. */
mode.c_cc[VDSUSP] = _POSIX_VDISABLE;
#endif
static void check_resize(void) static void check_resize(void)
{ {
struct winsize size; struct winsize size;
@ -3305,6 +3279,10 @@ static void terminal_start(void)
struct termios mode = saved_terminal; struct termios mode = saved_terminal;
mode.c_lflag &= ~(ICANON | ECHO | IEXTEN); mode.c_lflag &= ~(ICANON | ECHO | IEXTEN);
mode.c_iflag &= ~(ICRNL | INLCR | IGNCR | ISTRIP | IXON | IXOFF); mode.c_iflag &= ~(ICRNL | INLCR | IGNCR | ISTRIP | IXON | IXOFF);
#ifdef VDSUSP
/* Allow Ctrl+Y to reach the key handler on terminals with delayed suspend. */
mode.c_cc[VDSUSP] = _POSIX_VDISABLE;
#endif
mode.c_cc[VMIN] = 0; mode.c_cc[VMIN] = 0;
mode.c_cc[VTIME] = 0; mode.c_cc[VTIME] = 0;
if (tcsetattr(STDIN_FILENO, TCSANOW, &mode) < 0) if (tcsetattr(STDIN_FILENO, TCSANOW, &mode) < 0)
@ -3539,10 +3517,8 @@ int main(int argc, char **argv)
connect_server(s); connect_server(s);
run_pending_autocommands(s); run_pending_autocommands(s);
if (quit_flag) break; if (quit_flag) break;
#ifdef USE_AUTOJOIN
send_autojoin(s); send_autojoin(s);
run_pending_autojoin_commands(s); run_pending_autojoin_commands(s);
#endif
} }
if (quit_flag) break; if (quit_flag) break;
@ -3598,10 +3574,8 @@ int main(int argc, char **argv)
if (s->ssl) SSL_free(s->ssl); if (s->ssl) SSL_free(s->ssl);
#endif #endif
if (s->fd >= 0) close(s->fd); if (s->fd >= 0) close(s->fd);
#ifdef USE_AUTOJOIN
free_autojoin(&s->autojoin); free_autojoin(&s->autojoin);
free_autojoin(&s->pending_autojoin); free_autojoin(&s->pending_autojoin);
#endif
free(s->name); free(s->name);
free(s->autocommand); free(s->autocommand);
while (s->blacklist) { while (s->blacklist) {

View file

@ -19,7 +19,7 @@ name Libera Chat
# blacklist user1,user2,user3 # blacklist user1,user2,user3
# sasl irc9user my-sasl-password # sasl irc9user my-sasl-password
# autocommand /query user1,/query user2 # autocommand /query user1,/query user2
# Build with make -B AUTOJOIN=1 and uncomment channel lines to join at startup. # Uncomment channel lines to join at startup.
# Each channel line applies to the server above it. # Each channel line applies to the server above it.
# Optional comma-separated slash commands run after the channel join succeeds. # Optional comma-separated slash commands run after the channel join succeeds.
# irc9 waits two seconds between commands on the same line. # irc9 waits two seconds between commands on the same line.