diff --git a/README.md b/README.md index 1017246..14ce077 100644 --- a/README.md +++ b/README.md @@ -92,8 +92,10 @@ 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. Default -builds ignore `channel` lines. Use `make -B` to rebuild with autojoin disabled. +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 +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. TLS certificates are verified against a system CA bundle when one is found. diff --git a/irc9.c b/irc9.c index d803cf6..77f1f38 100644 --- a/irc9.c +++ b/irc9.c @@ -90,6 +90,7 @@ typedef struct Window { #ifdef USE_AUTOJOIN typedef struct AutojoinChannel { char name[CHANLEN]; + char *command; struct AutojoinChannel *next; } AutojoinChannel; #endif @@ -124,6 +125,7 @@ typedef struct Server { #endif #ifdef USE_AUTOJOIN AutojoinChannel *autojoin; + AutojoinChannel *pending_autojoin; #endif struct Server *next; } Server; @@ -672,6 +674,26 @@ static void load_config(const char *path) continue; } a1 = strtok_r(NULL, " \t", &save); + if (strcasecmp(key, "channel") == 0) { +#ifdef USE_AUTOJOIN + if (channel_tail && a1 && strlen(a1) < CHANLEN) { + AutojoinChannel *channel = xmalloc(sizeof *channel); + strcpy(channel->name, a1); + channel->command = NULL; + channel->next = NULL; + *channel_tail = channel; + channel_tail = &channel->next; + + save += strspn(save, " \t"); + if (*save == '/' && save[1] && strlen(save) < MAXINPUT && + !strchr(save, '\r')) + channel->command = dupstr(save); + else if (*save) + fprintf(stderr, "irc9: ignoring invalid command for channel %s\n", a1); + } +#endif + continue; + } a2 = strtok_r(NULL, " \t", &save); a3 = strtok_r(NULL, " \t", &save); @@ -711,16 +733,6 @@ static void load_config(const char *path) strcasecmp(key, "stripfmt") == 0) && a1) { strip_formatting = !strcasecmp(a1, "on") || !strcasecmp(a1, "true") || !strcasecmp(a1, "yes") || strcmp(a1, "1") == 0; - } else if (strcasecmp(key, "channel") == 0) { -#ifdef USE_AUTOJOIN - if (channel_tail && a1 && strlen(a1) < CHANLEN) { - AutojoinChannel *channel = xmalloc(sizeof *channel); - strcpy(channel->name, a1); - channel->next = NULL; - *channel_tail = channel; - channel_tail = &channel->next; - } -#endif } else { fprintf(stderr, "irc9: ignoring config line: %s\n", line); } @@ -992,6 +1004,18 @@ static void handle_connect_results(void) } } +#ifdef USE_AUTOJOIN +static void free_autojoin(AutojoinChannel **head) +{ + while (*head) { + AutojoinChannel *channel = *head; + *head = channel->next; + free(channel->command); + free(channel); + } +} +#endif + static void disconnect_server(Server *s) { s->connect_gen++; @@ -1007,6 +1031,9 @@ static void disconnect_server(Server *s) s->cap_state = CAP_NONE; s->caps[0] = 0; s->retry_at = time(NULL) + RETRY; +#ifdef USE_AUTOJOIN + free_autojoin(&s->pending_autojoin); +#endif } /* ------------------------------------------------------------------ */ @@ -1138,6 +1165,24 @@ static int handle_ctcp(Server *s, const char *nick, char *text, return 1; } +#ifdef USE_AUTOJOIN +static void do_command(Window *window, char *line); + +static void run_autojoin_command(Window *w) +{ + AutojoinChannel **link = &w->srv->pending_autojoin; + while (*link && 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); +} +#endif + static void handle_line(Server *s, char *line) { char *prefix, *cmd; @@ -1232,6 +1277,9 @@ static void handle_line(Server *s, char *line) Window *w = window_get(s, arg[0], W_CHANNEL); 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); +#endif return; } @@ -1528,11 +1576,18 @@ static void handle_line(Server *s, char *line) 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; - sendf(s, "JOIN %s", channel->name); s->autojoin = channel->next; - free(channel); + 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); @@ -1573,6 +1628,7 @@ static void read_server(Server *s) s->inbuf[s->inlen] = 0; if (s->inlen) handle_line(s, s->inbuf); s->inlen = 0; + if (s->fd < 0 || quit_flag) return; } else if (s->inlen < sizeof s->inbuf - 1) { s->inbuf[s->inlen++] = c; } else { @@ -1794,13 +1850,13 @@ static char *next_word(char **p) return s; } -static Window *input_target(void) +static Window *input_target(Window *window) { - if (!curwin || curwin->type == W_SERVER) return NULL; - return curwin; + if (!window || window->type == W_SERVER) return NULL; + return window; } -static void do_command(char *line) +static void do_command(Window *window, char *line) { char *p = line + 1; while (*p == ' ' || *p == '\t') p++; @@ -1813,26 +1869,26 @@ static void do_command(char *line) if (strcasecmp(cmd, "join") == 0 || strcasecmp(cmd, "j") == 0) { char *chan = next_word(&q); char chanbuf[CHANLEN]; - if (!chan || !curwin) return; + if (!chan || !window) return; if (!is_channel(chan)) { snprintf(chanbuf, sizeof chanbuf, "#%s", chan); chan = chanbuf; } - Window *w = window_get(curwin->srv, chan, W_CHANNEL); + Window *w = window_get(window->srv, chan, W_CHANNEL); w->syncing = 1; clock_gettime(CLOCK_MONOTONIC, &w->join_ts); focus_window(w); - sendf(curwin->srv, "JOIN %s", chan); + sendf(window->srv, "JOIN %s", chan); } else if (strcasecmp(cmd, "n") == 0 || strcasecmp(cmd, "names") == 0) { - Window *t = curwin; + Window *t = window; char *name = next_word(&q); - if (name && curwin) { - t = window_find(curwin->srv, name); - if (!t) appendf(curwin, "* no such window: %s", name); + if (name && window) { + t = window_find(window->srv, name); + if (!t) appendf(window, "* no such window: %s", name); } if (t) show_names(t); } else if (strcasecmp(cmd, "part") == 0 || strcasecmp(cmd, "leave") == 0) { - Window *t = input_target(); + Window *t = input_target(window); char *chan = next_word(&q); if (!t || !t->srv) return; if (!chan && t->type == W_CHANNEL) chan = t->name; @@ -1841,37 +1897,37 @@ static void do_command(char *line) else sendf(t->srv, "PART %s", chan); } else if (strcasecmp(cmd, "nick") == 0) { char *newnick = next_word(&q); - if (!newnick || !curwin) return; - sendf(curwin->srv, "NICK %s", newnick); - appendf(serverwin(curwin->srv), "* changing nick to %s", newnick); + if (!newnick || !window) return; + sendf(window->srv, "NICK %s", newnick); + appendf(serverwin(window->srv), "* changing nick to %s", newnick); } else if (strcasecmp(cmd, "msg") == 0) { char *target = next_word(&q); - if (!target || !curwin || !*q) return; - sendf(curwin->srv, "PRIVMSG %s :%s", target, q); - Window *w = window_get(curwin->srv, target, + if (!target || !window || !*q) return; + sendf(window->srv, "PRIVMSG %s :%s", target, q); + Window *w = window_get(window->srv, target, is_channel(target) ? W_CHANNEL : W_QUERY); - append_nickf(w, "<", curwin->srv->nick, "> ", "%s", q); + append_nickf(w, "<", window->srv->nick, "> ", "%s", q); } else if (strcasecmp(cmd, "whois") == 0) { char *nick = next_word(&q); - if (!curwin) return; - if (!nick) { appendf(curwin, "* usage: /whois nick"); return; } - curwin->srv->reply_win = curwin; - sendf(curwin->srv, "WHOIS %s", nick); - appendf(curwin, "* whois %s", nick); + if (!window) return; + if (!nick) { appendf(window, "* usage: /whois nick"); return; } + window->srv->reply_win = window; + sendf(window->srv, "WHOIS %s", nick); + appendf(window, "* whois %s", nick); } else if (strcasecmp(cmd, "me") == 0) { - Window *t = input_target(); + Window *t = input_target(window); if (!t || !*q) return; sendf(t->srv, "PRIVMSG %s :\001ACTION %s\001", t->name, q); append_nickf(t, "* ", t->srv->nick, " ", "%s", q); } else if (strcasecmp(cmd, "away") == 0) { - if (!curwin) return; - sendf(curwin->srv, "AWAY :%s", *q ? q : ""); - if (*q) appendf(curwin, "* away: %s", q); - else appendf(curwin, "* not away"); + if (!window) return; + sendf(window->srv, "AWAY :%s", *q ? q : ""); + if (*q) appendf(window, "* away: %s", q); + else appendf(window, "* not away"); } else if (strcasecmp(cmd, "topic") == 0) { - Window *t = input_target(); + Window *t = input_target(window); if (!t || t->type != W_CHANNEL) { - if (curwin) appendf(curwin, "* no channel here"); + if (window) appendf(window, "* no channel here"); return; } t->srv->reply_win = t; @@ -1879,36 +1935,36 @@ static void do_command(char *line) else sendf(t->srv, "TOPIC %s", t->name); } else if (strcasecmp(cmd, "invite") == 0) { char *nick = next_word(&q); - if (!nick || !curwin) return; + if (!nick || !window) return; char *chan = next_word(&q); - if (!chan && curwin->type == W_CHANNEL) chan = curwin->name; - if (!chan) { appendf(curwin, "* usage: /invite nick [#chan]"); return; } - curwin->srv->reply_win = curwin; - sendf(curwin->srv, "INVITE %s %s", nick, chan); - appendf(curwin, "* inviting %s to %s", nick, chan); + if (!chan && window->type == W_CHANNEL) chan = window->name; + if (!chan) { appendf(window, "* usage: /invite nick [#chan]"); return; } + window->srv->reply_win = window; + sendf(window->srv, "INVITE %s %s", nick, chan); + appendf(window, "* inviting %s to %s", nick, chan); } else if (strcasecmp(cmd, "kick") == 0) { - Window *t = input_target(); + Window *t = input_target(window); char *nick = next_word(&q); if (!t || t->type != W_CHANNEL || !nick) { - if (curwin) appendf(curwin, "* usage: /kick nick [reason] in a channel"); + if (window) appendf(window, "* usage: /kick nick [reason] in a channel"); return; } if (*q) sendf(t->srv, "KICK %s %s :%s", t->name, nick, q); else sendf(t->srv, "KICK %s %s", t->name, nick); } else if (strcasecmp(cmd, "mode") == 0) { char *target = next_word(&q); - if (!target && curwin && curwin->type != W_SERVER) target = curwin->name; - if (!target || !curwin) return; - curwin->srv->reply_win = curwin; - if (*q) sendf(curwin->srv, "MODE %s %s", target, q); - else sendf(curwin->srv, "MODE %s", target); + if (!target && window && window->type != W_SERVER) target = window->name; + if (!target || !window) return; + window->srv->reply_win = window; + if (*q) sendf(window->srv, "MODE %s %s", target, q); + else sendf(window->srv, "MODE %s", target); } else if (strcasecmp(cmd, "op") == 0 || strcasecmp(cmd, "deop") == 0 || strcasecmp(cmd, "voice") == 0 || strcasecmp(cmd, "devoice") == 0 || strcasecmp(cmd, "ban") == 0 || strcasecmp(cmd, "unban") == 0) { - Window *t = input_target(); + Window *t = input_target(window); char *who = next_word(&q); if (!t || t->type != W_CHANNEL || !who) { - if (curwin) appendf(curwin, "* usage: /%s nick|mask in a channel", cmd); + if (window) appendf(window, "* usage: /%s nick|mask in a channel", cmd); return; } const char *flag = !strcasecmp(cmd, "op") ? "+o" : @@ -1920,58 +1976,58 @@ static void do_command(char *line) appendf(t, "* mode %s %s %s", t->name, flag, who); } else if (strcasecmp(cmd, "notice") == 0) { char *target = next_word(&q); - if (!target || !curwin || !*q) return; - sendf(curwin->srv, "NOTICE %s :%s", target, q); - Window *w = window_get(curwin->srv, target, + if (!target || !window || !*q) return; + sendf(window->srv, "NOTICE %s :%s", target, q); + Window *w = window_get(window->srv, target, is_channel(target) ? W_CHANNEL : W_QUERY); - append_nickf(w, "-", curwin->srv->nick, "- ", "%s", q); + append_nickf(w, "-", window->srv->nick, "- ", "%s", q); } else if (strcasecmp(cmd, "query") == 0) { char *nick = next_word(&q); - if (!curwin) return; - if (nick) focus_window(window_get(curwin->srv, nick, W_QUERY)); - else if (curwin->type == W_QUERY) window_close(curwin->srv, curwin); + if (!window) return; + if (nick) focus_window(window_get(window->srv, nick, W_QUERY)); + else if (window->type == W_QUERY) window_close(window->srv, window); } else if (strcasecmp(cmd, "who") == 0) { - if (!curwin) return; - curwin->srv->reply_win = curwin; - sendf(curwin->srv, "WHO%s%s", *q ? " " : "", q); + if (!window) return; + window->srv->reply_win = window; + sendf(window->srv, "WHO%s%s", *q ? " " : "", q); } else if (strcasecmp(cmd, "list") == 0) { - if (!curwin) return; - curwin->srv->reply_win = curwin; - sendf(curwin->srv, "LIST%s%s", *q ? " " : "", q); + if (!window) return; + window->srv->reply_win = window; + sendf(window->srv, "LIST%s%s", *q ? " " : "", q); } else if (strcasecmp(cmd, "motd") == 0 || strcasecmp(cmd, "time") == 0 || strcasecmp(cmd, "version") == 0 || strcasecmp(cmd, "admin") == 0 || strcasecmp(cmd, "info") == 0 || strcasecmp(cmd, "links") == 0 || strcasecmp(cmd, "lusers") == 0 || strcasecmp(cmd, "stats") == 0) { - if (!curwin) return; - curwin->srv->reply_win = curwin; - sendf(curwin->srv, "%s%s%s", cmd, *q ? " " : "", q); - appendf(curwin, "* %s", cmd); + if (!window) return; + window->srv->reply_win = window; + sendf(window->srv, "%s%s%s", cmd, *q ? " " : "", q); + appendf(window, "* %s", cmd); } else if (strcasecmp(cmd, "ping") == 0) { char *nick = next_word(&q); - if (!nick || !curwin) return; - sendf(curwin->srv, "PING %s", nick); - appendf(curwin, "* pinging %s", nick); + if (!nick || !window) return; + sendf(window->srv, "PING %s", nick); + appendf(window, "* pinging %s", nick); } else if (strcasecmp(cmd, "ctcp") == 0) { char *nick = next_word(&q); - if (!nick || !curwin || !*q) return; - sendf(curwin->srv, "PRIVMSG %s :\001%s\001", nick, q); - appendf(curwin, "* ctcp %s %s", nick, q); + if (!nick || !window || !*q) return; + sendf(window->srv, "PRIVMSG %s :\001%s\001", nick, q); + appendf(window, "* ctcp %s %s", nick, q); } else if (strcasecmp(cmd, "raw") == 0) { - if (!curwin || !*q) return; - sendf(curwin->srv, "%s", q); - appendf(curwin, "* -> %s", q); + if (!window || !*q) return; + sendf(window->srv, "%s", q); + appendf(window, "* -> %s", q); } else if (strcasecmp(cmd, "close") == 0 || strcasecmp(cmd, "wc") == 0) { - if (!curwin) return; - if (curwin == curwin->srv->windows) { - appendf(curwin, "* cannot close the server window"); + if (!window) return; + if (window == window->srv->windows) { + appendf(window, "* cannot close the server window"); return; } - window_close(curwin->srv, curwin); + window_close(window->srv, window); } else if (strcasecmp(cmd, "clear") == 0) { - window_clear(curwin); + window_clear(window); } else if (strcasecmp(cmd, "reconnect") == 0) { - if (!curwin) return; - Server *s = curwin->srv; + if (!window) return; + Server *s = window->srv; appendf(serverwin(s), "* reconnecting to %s:%d", s->host, s->port); disconnect_server(s); s->retry_at = 0; @@ -1981,7 +2037,7 @@ static void do_command(char *line) sendf(s, "QUIT :%s", *q ? q : "irc9"); quit_flag = 1; } else if (strcasecmp(cmd, "help") == 0) { - Window *t = curwin ? curwin : server_window_owner()->windows; + Window *t = window ? window : server_window_owner()->windows; appendf(t, "commands: /join /part /leave /nick /msg /me /whois /n /names"); appendf(t, " /away /topic /invite /kick /mode /notice /query"); appendf(t, " /who /list /motd /time /version /admin /info"); @@ -1991,7 +2047,7 @@ static void do_command(char *line) appendf(t, "keys: ctrl-n / ctrl-p switch windows, pgup/pgdn scroll, ctrl-l clear screen"); appendf(t, " ctrl-s save scrollback, ctrl-t toggle copy view"); } else { - if (curwin) appendf(curwin, "* unknown command: /%s", cmd); + if (window) appendf(window, "* unknown command: /%s", cmd); } } @@ -2002,9 +2058,9 @@ static void submit_input(void) history_add(line); if (*line == '/') { - do_command(line); + do_command(curwin, line); } else if (*line) { - Window *t = input_target(); + Window *t = input_target(curwin); if (!t) { if (curwin) appendf(curwin, "* no target here; use /join or /msg"); } else { @@ -2906,11 +2962,8 @@ int main(int argc, char **argv) #endif if (s->fd >= 0) close(s->fd); #ifdef USE_AUTOJOIN - while (s->autojoin) { - AutojoinChannel *channel = s->autojoin; - s->autojoin = channel->next; - free(channel); - } + free_autojoin(&s->autojoin); + free_autojoin(&s->pending_autojoin); #endif free(s->name); free(s->autocommand); diff --git a/irc9rc-example b/irc9rc-example index 4923c70..0643962 100644 --- a/irc9rc-example +++ b/irc9rc-example @@ -18,6 +18,8 @@ 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 channel #irc9 # channel #murx # channel #zynk