Support multiple commands after channel autojoin

This commit is contained in:
Johannes Findeisen 2026-12-02 00:00:24 +01:00
commit a95f4f42b3
3 changed files with 106 additions and 24 deletions

View file

@ -68,8 +68,9 @@ name Libera Chat
# autocommand /msg NickServ IDENTIFY irc9user $PASSWORD
# Build with make -B AUTOJOIN=1 and uncomment channel lines to join at startup.
# Each channel line applies to the server above it.
# An optional slash command runs after the channel join succeeds.
# channel #channel /msg ChanServ OP #channel nickname
# Optional comma-separated slash commands run after the channel join succeeds.
# irc9 waits two seconds between commands on the same line.
# channel #channel /msg ChanServ OP #channel nickname,/mode #channel
channel #irc9
# channel #murx
# channel #zynk
@ -110,9 +111,12 @@ negotiation when configured and before startup channel joins.
To enable startup channel joins, build with `make -B AUTOJOIN=1`. Add one
`channel #name` line per channel below the corresponding `server` line.
After registration, irc9 sends one startup JOIN every two seconds per server.
To run a slash command once the server confirms the channel join, add it after the
channel name. For example, `channel #channel /msg ChanServ OP #channel nickname`.
Commands run in the joined channel's context. Default builds ignore `channel` lines. Use `make -B` to rebuild with autojoin disabled.
Add comma-separated slash commands after the channel name, for example
`channel #channel /msg ChanServ OP #channel nickname,/mode #channel`.
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.
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.
TLS certificates are verified against a system CA bundle when one is found.

111
irc9.c
View file

@ -101,7 +101,10 @@ typedef struct Window {
#ifdef USE_AUTOJOIN
typedef struct AutojoinChannel {
char name[CHANLEN];
char *command;
char *commands;
const char *next_command;
int joined;
struct timespec command_at;
struct AutojoinChannel *next;
} AutojoinChannel;
#endif
@ -702,6 +705,36 @@ static void load_blacklist(Server *s, char *list)
}
}
#ifdef USE_AUTOJOIN
/* A comma starts another command only when followed by a slash. */
static const char *autojoin_command_end(const char *command, const char **next)
{
for (const char *comma = strchr(command, ','); comma; comma = strchr(comma + 1, ',')) {
const char *following = comma + 1 + strspn(comma + 1, " \t");
if (*following == '/') {
*next = following;
return comma;
}
}
*next = NULL;
return command + strlen(command);
}
static int valid_autojoin_commands(const char *command)
{
if (*command != '/' || strchr(command, '\r')) return 0;
while (command) {
const char *next;
const char *end = autojoin_command_end(command, &next);
if (end - command >= MAXINPUT ||
command + 1 + strspn(command + 1, " \t") >= end)
return 0;
command = next;
}
return 1;
}
#endif
static void load_config(const char *path)
{
FILE *f = fopen(path, "r");
@ -763,17 +796,16 @@ static void load_config(const char *path)
#ifdef USE_AUTOJOIN
if (channel_tail && a1 && strlen(a1) < CHANLEN) {
AutojoinChannel *channel = xmalloc(sizeof *channel);
*channel = (AutojoinChannel){0};
strcpy(channel->name, a1);
channel->command = NULL;
channel->next = NULL;
*channel_tail = channel;
channel_tail = &channel->next;
const char *command = save ? save + strspn(save, " \t") : "";
if (*command == '/' && command[1] && strlen(command) < MAXINPUT &&
!strchr(command, '\r'))
channel->command = dupstr(command);
else if (*command)
if (valid_autojoin_commands(command)) {
channel->commands = dupstr(command);
channel->next_command = channel->commands;
} else if (*command)
fprintf(stderr, "irc9: ignoring invalid command for channel %s\n", a1);
}
#endif
@ -1104,7 +1136,7 @@ static void send_autojoin(Server *s)
AutojoinChannel *channel = s->autojoin;
s->autojoin = channel->next;
sendf(s, "JOIN %s", channel->name);
if (channel->command) {
if (channel->commands) {
AutojoinChannel **pending = &s->pending_autojoin;
while (*pending) pending = &(*pending)->next;
channel->next = NULL;
@ -1121,7 +1153,7 @@ static void free_autojoin(AutojoinChannel **head)
while (*head) {
AutojoinChannel *channel = *head;
*head = channel->next;
free(channel->command);
free(channel->commands);
free(channel);
}
}
@ -1283,18 +1315,62 @@ static int handle_ctcp(Server *s, const char *nick, char *text,
#ifdef USE_AUTOJOIN
static void do_command(Window *window, char *line);
static void run_autojoin_command(Window *w)
static void run_autojoin_command(AutojoinChannel **link, Window *w)
{
AutojoinChannel *channel = *link;
const char *command = channel->next_command;
const char *end = autojoin_command_end(command, &channel->next_command);
if (channel->next_command)
while (end > command && (end[-1] == ' ' || end[-1] == '\t')) end--;
char line[MAXINPUT];
size_t len = (size_t)(end - command);
memcpy(line, command, len);
line[len] = 0;
if (channel->next_command) {
clock_gettime(CLOCK_MONOTONIC, &channel->command_at);
channel->command_at.tv_sec += 2;
} else {
*link = channel->next;
free(channel->commands);
free(channel);
}
do_command(w, line);
}
static void confirm_autojoin(Window *w)
{
AutojoinChannel **link = &w->srv->pending_autojoin;
while (*link && strcasecmp((*link)->name, w->name) != 0)
while (*link && ((*link)->joined || strcasecmp((*link)->name, w->name) != 0))
link = &(*link)->next;
if (!*link) return;
AutojoinChannel *channel = *link;
*link = channel->next;
do_command(w, channel->command);
free(channel->command);
free(channel);
(*link)->joined = 1;
run_autojoin_command(link, w);
}
static void run_pending_autojoin_commands(Server *s)
{
if (!s->pending_autojoin) return;
struct timespec now;
clock_gettime(CLOCK_MONOTONIC, &now);
for (AutojoinChannel **link = &s->pending_autojoin; *link; link = &(*link)->next) {
AutojoinChannel *channel = *link;
if (!channel->joined || now.tv_sec < channel->command_at.tv_sec ||
(now.tv_sec == channel->command_at.tv_sec &&
now.tv_nsec < channel->command_at.tv_nsec)) continue;
Window *w = window_find(s, channel->name);
if (w) {
run_autojoin_command(link, w);
} else {
*link = channel->next;
free(channel->commands);
free(channel);
}
return;
}
}
#endif
@ -1396,7 +1472,7 @@ static void handle_line(Server *s, char *line)
member_add(w, nick);
if (!silent) append_nickf(w, "* ", hostmask ? hostmask : "someone", " ", "joined %s", arg[0]);
#ifdef USE_AUTOJOIN
if (strcasecmp(nick, s->nick) == 0) run_autojoin_command(w);
if (strcasecmp(nick, s->nick) == 0) confirm_autojoin(w);
#endif
return;
}
@ -3165,6 +3241,7 @@ int main(int argc, char **argv)
connect_server(s);
#ifdef USE_AUTOJOIN
send_autojoin(s);
run_pending_autojoin_commands(s);
#endif
}

View file

@ -20,8 +20,9 @@ name Libera Chat
# autocommand /msg NickServ IDENTIFY irc9user $PASSWORD
# Build with make -B AUTOJOIN=1 and uncomment channel lines to join at startup.
# Each channel line applies to the server above it.
# An optional slash command runs after the channel join succeeds.
# channel #channel /msg ChanServ OP #channel nickname
# Optional comma-separated slash commands run after the channel join succeeds.
# irc9 waits two seconds between commands on the same line.
# channel #channel /msg ChanServ OP #channel nickname,/mode #channel
channel #irc9
# channel #murx
# channel #zynk