Pace startup channel joins per server

This commit is contained in:
Johannes Findeisen 2026-11-29 20:02:14 +01:00
commit a5c12b1cfe
2 changed files with 37 additions and 19 deletions

View file

@ -93,8 +93,8 @@ 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.
irc9 sends the joins when that server first confirms registration. To run a
slash command once the server confirms the channel join, add it after the
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.
Blank lines and whole-line comments starting with `#` are ignored.

52
irc9.c
View file

@ -126,6 +126,7 @@ typedef struct Server {
#ifdef USE_AUTOJOIN
AutojoinChannel *autojoin;
AutojoinChannel *pending_autojoin;
struct timespec next_autojoin;
#endif
struct Server *next;
} Server;
@ -1029,6 +1030,32 @@ static void handle_connect_results(void)
}
#ifdef USE_AUTOJOIN
/* Pace each server's startup JOINs to avoid flooding the connection. */
static void send_autojoin(Server *s)
{
if (!s->connected || !s->autojoin) return;
struct timespec now;
clock_gettime(CLOCK_MONOTONIC, &now);
if (now.tv_sec < s->next_autojoin.tv_sec ||
(now.tv_sec == s->next_autojoin.tv_sec &&
now.tv_nsec < s->next_autojoin.tv_nsec)) return;
AutojoinChannel *channel = s->autojoin;
s->autojoin = channel->next;
sendf(s, "JOIN %s", channel->name);
if (channel->command) {
AutojoinChannel **pending = &s->pending_autojoin;
while (*pending) pending = &(*pending)->next;
channel->next = NULL;
*pending = channel;
} else {
free(channel);
}
s->next_autojoin = now;
s->next_autojoin.tv_sec += 2;
}
static void free_autojoin(AutojoinChannel **head)
{
while (*head) {
@ -1057,6 +1084,7 @@ static void disconnect_server(Server *s)
s->retry_at = time(NULL) + RETRY;
#ifdef USE_AUTOJOIN
free_autojoin(&s->pending_autojoin);
s->next_autojoin = (struct timespec){0};
#endif
}
@ -1602,7 +1630,8 @@ static void handle_line(Server *s, char *line)
return;
}
if (strcmp(cmd, "461") == 0 || strcmp(cmd, "403") == 0 ||
strcmp(cmd, "404") == 0 || strcmp(cmd, "482") == 0 ||
strcmp(cmd, "404") == 0 || strcmp(cmd, "405") == 0 ||
strcmp(cmd, "482") == 0 ||
strcmp(cmd, "471") == 0 || strcmp(cmd, "473") == 0 ||
strcmp(cmd, "474") == 0 || strcmp(cmd, "475") == 0) {
if (narg > 0) appendf(reply_window(s), "* error: %s", arg[narg - 1]);
@ -1614,21 +1643,6 @@ static void handle_line(Server *s, char *line)
s->connected = 1;
if (first_welcome && s->autocommand)
sendf(s, "%s", s->autocommand);
#ifdef USE_AUTOJOIN
AutojoinChannel **pending = &s->pending_autojoin;
while (s->autojoin) {
AutojoinChannel *channel = s->autojoin;
s->autojoin = channel->next;
sendf(s, "JOIN %s", channel->name);
if (channel->command) {
channel->next = NULL;
*pending = channel;
pending = &channel->next;
} else {
free(channel);
}
}
#endif
appendf(sw, "%s", narg > 0 ? arg[narg - 1] : cmd);
return;
}
@ -2964,9 +2978,13 @@ int main(int argc, char **argv)
check_resize();
time_t now = time(NULL);
for (Server *s = servers; s; s = s->next)
for (Server *s = servers; s; s = s->next) {
if (s->fd < 0 && !s->connecting && now >= s->retry_at)
connect_server(s);
#ifdef USE_AUTOJOIN
send_autojoin(s);
#endif
}
struct pollfd fds[64];
Server *owner[64];