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

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
}