diff --git a/AUTHORS b/AUTHORS deleted file mode 100644 index c5200d5..0000000 --- a/AUTHORS +++ /dev/null @@ -1,4 +0,0 @@ -murx authors - -Armin Jenewein - Founder -Johannes Findeisen - Contributor diff --git a/LICENSE b/LICENSE deleted file mode 100644 index 03f1a62..0000000 --- a/LICENSE +++ /dev/null @@ -1,13 +0,0 @@ -Copyright 2026 Armin Jenewein - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. diff --git a/README.md b/README.md index f13e950..d89f2ea 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ A deliberately minimal console IRC client in C. No network list, no settings commands, no plugins. Just servers, windows, timestamps, and two keys to move -around + some more keys you can use. +around. ## Build and run @@ -12,24 +12,16 @@ around + some more keys you can use. make ./irc9 # finds a config automatically (see below) ./irc9 myconfig.conf # or point at a config explicitly -./irc9 -s myconfig.conf # hide channel activity notices +./irc9 -s myconfig.conf # hide join and quit notices in channel windows ``` -`-v` and `--version` print the program version. `-h` and `--help` print the -version and command-line usage. - -`-s` and `--silent` hide channel join, quit, leave, and sync notices, as well as -nickname-change and mode-change notices. +`-s` and `--silent` hide JOIN and QUIT notices in channel windows. `-C` and `--colored` color nicknames in channel and private chats and in nicklists. Your own nick is always green in this mode, including in the status line. Use `-c RRGGBB` or `--color RRGGBB` to replace the default cyan accent on the channel tree and divider. For example, `./irc9 --color FF8800` uses orange on terminals with true-color support. -Use `--scrollback LINES` to set how many lines each window retains. The default -is 500 lines. For example, `./irc9 --scrollback 2000` keeps 2000 lines per window. -Use `--history` to save submitted slash commands to `~/.irc9_history` and -recall the 64 most recent commands with Up and Down across sessions. With no config path, irc9 looks next to the executable, in this order: `irc9rc`, `config`, then `~/.irc9rc`. Paths are relative to the binary, not to @@ -46,7 +38,7 @@ lines marked `ssl`/`tls` cannot connect. ## Configuration -One file, ten active keywords: +One file, nine active keywords: ``` nick irc9user @@ -56,22 +48,16 @@ real Happy irc9 User # Libera and OFTC use TLS on 6697. Use port 6667 without the ssl keyword for # plain TCP. The optional sasl line applies to the server above it; the SASL -# user is your account name. An optional autocommand runs after registration on -# the server above it, before startup channel joins. Separate slash commands with -# commas. irc9 waits two seconds between commands. +# user is your account name. An optional autocommand sends one raw IRC command +# after registration on the server above it, before startup channel joins. # libera server irc.libera.chat 6697 ssl name Libera Chat -# Suppress incoming messages from these nicknames on this server. -# blacklist user1,user2,user3 # sasl irc9user my-sasl-password -# autocommand /query user1,/query user2 +# 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. -# 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 @@ -93,34 +79,21 @@ server [port] [ssl|tls] An optional `name ` line below `server` sets its label in the channel tree. The label defaults to the hostname. -Add `blacklist user1,user2,user3` below a `server` line to suppress incoming -channel messages, private messages, and notices from those nicknames on that -server. Matching is case-insensitive. You can add multiple blacklist lines -for the same server. - The port defaults to `6667`. The optional third token `ssl` or `tls` enables TLS; use port `6697` for Libera and OFTC. A `sasl ` line applies to the most recent `server` line and enables SASL PLAIN for that server only. The SASL user is your account name (for many people this is the same as the nick). -Place an optional `autocommand` below the corresponding `server` line, -immediately after its `sasl` line when present. For example, -`autocommand /query user1,/query user2` opens two query windows, two seconds apart. -A comma followed by a slash, with optional spaces or tabs between them, starts -the next command. The first command can also use raw IRC syntax, as in -`autocommand PRIVMSG NickServ :STATUS mynick`. Use `/raw` for later raw commands. -The sequence runs once per successful registration, including reconnects, after -SASL negotiation when configured and before startup channel joins. +Place an optional `autocommand ` below the corresponding +`server` line, immediately after its `sasl` line when present. For example, +`autocommand PRIVMSG NickServ :STATUS mynick` sends that raw IRC command. +It runs once per successful registration, including reconnects, after SASL +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. -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. +irc9 sends the joins when that server first confirms registration. 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. @@ -134,23 +107,21 @@ Build yours by copying `irc9rc-example` to `irc9rc` next to the binary (or to ``` [irc9] irc.example / #general -/----------------------\/--------------------------------------\ -|*irc.example | General discussion -| |-#general |---------------------------------------| +/----------------------\ Topic: General discussion +|* irc.example | +| |-#general | | |-#help(3) | | \-alice | [10:21] hello there -|*irc.other | [10:22] hi -| | [10:22] * alice!u@host joined #help -\----------------------/\--------------------------------------/ +|* irc.other | [10:22] hi +\______________________/ [10:22] * alice!u@host joined #help +---------------------------------------------------------------- [10:23] (me@irc.example:6697 [TLS]) > your message here ``` The left column is a tree: each server and its windows (channels and private -message windows). Channels appear before private message windows, with -uppercase names sorting before lowercase names in each group. An unread window -shows `(n)` next to its name. Each server shows its total unread count after -its name, including when folded. `*` marks a connected server. The selected server or window is shown bold and underlined. Highlights use the +message windows). An unread window shows `(n)` next to its name. `*` marks a +connected server. The selected server or window is shown bold and underlined. Highlights use the terminal's default background so transparency is preserved where supported. Channel topics wrap to the chat pane width and stay at the top while you scroll. On short terminals, the topic leaves room for at least one chat line when possible. @@ -162,24 +133,18 @@ Channel privileges such as operator or voice appear as a trailing `*` (for examp CTCP `ACTION`, `VERSION`, `PING`, `TIME`, and `USERINFO` are understood; KICK, MODE, TOPIC, and INVITE events are shown in the relevant window. -## Screenshot - -![screenshot](screenshot.png) - ## Keys | Key | Action | | --- | --- | | `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+Up` / `Ctrl+Down` | scroll the channel tree by one row | -| `ctrl-n` / `ctrl-p` | next / previous visible window (tree order, wraps around) | -| Enter | fold or unfold the selected server when the input line is empty | +| `ctrl-n` / `ctrl-p` | next / previous window (tree order, wraps around) | | `PageUp` / `PageDown` | scroll the current window | | `ctrl-l` | clear the screen (history stays scrollable) | | `ctrl-t` | toggle copy view, which hides the tree, chat timestamps, and sender names | | `ctrl-s` | save the current window’s scrollback to a new plain-text file | -| Up / Down | browse slash command history with `--history` | +| Up / Down | command history | | Left / Right, Home / End | edit the input line | | `Del` | delete the character at the cursor | @@ -224,17 +189,3 @@ instead. Anything else typed without a leading `/` is sent to the current channel or private-message window. Typing in a server window tells you to pick a target. - -## Authors - -See [AUTHORS](AUTHORS) for project credits. - -- Armin Jenewein - - Founder -- Johannes Findeisen - - Contributor - -## License - -Copyright 2026 Armin Jenewein - -The code of murx is licensed under the terms of the Apache-2.0 license. See -LICENSE for details. diff --git a/irc9.c b/irc9.c index 025c416..2733948 100644 --- a/irc9.c +++ b/irc9.c @@ -1,12 +1,3 @@ -/* - * This file is part of irc9 - A very simple terminal IRC client. - * https://xw3.org/murx/irc9 - * - * Copyright 2026 Armin Jenewein - * Licensed under the terms of the Apache-2.0 license. - * https://opensource.org/license/apache-2-0 - */ - /* * irc9 - a deliberately minimal console IRC client. * @@ -49,8 +40,6 @@ #include #endif -#define IRC9_VERSION "1.0" - #define HOSTLEN 128 #define NICKLEN 64 #define CHANLEN 128 @@ -60,6 +49,7 @@ #define MAXHIST 64 #define MAXSCROLL 500 #define MAXWRAPROWS 128 +#define MAXSCROLLROWS (MAXSCROLL * MAXWRAPROWS) #define TREEW 24 #define RETRY 5 @@ -89,7 +79,6 @@ typedef struct Window { struct Server *srv; struct Window *next; ScrollLine *lines; - int query_offline; int nlines; int unread; int scroll; @@ -101,19 +90,10 @@ typedef struct Window { #ifdef USE_AUTOJOIN typedef struct AutojoinChannel { char name[CHANLEN]; - char *commands; - const char *next_command; - int joined; - struct timespec command_at; struct AutojoinChannel *next; } AutojoinChannel; #endif -typedef struct BlacklistedNick { - char nick[NICKLEN]; - struct BlacklistedNick *next; -} BlacklistedNick; - typedef struct Server { char host[HOSTLEN]; char *name; @@ -123,23 +103,18 @@ typedef struct Server { char real[128]; int fd; int connected; - int monitor; int connecting; unsigned long connect_gen; time_t retry_at; char inbuf[INBUF]; size_t inlen; Window *windows; - int tree_folded; Window *reply_win; /* where command replies are printed */ int tls; int sasl; char sasl_user[NICKLEN]; char sasl_pass[128]; char *autocommand; - const char *next_autocommand; - struct timespec autocommand_at; - BlacklistedNick *blacklist; int cap_state; char caps[512]; /* accumulated multi-line CAP LS reply */ char umode[32]; @@ -149,8 +124,6 @@ typedef struct Server { #endif #ifdef USE_AUTOJOIN AutojoinChannel *autojoin; - AutojoinChannel *pending_autojoin; - struct timespec next_autojoin; #endif struct Server *next; } Server; @@ -184,10 +157,8 @@ static volatile sig_atomic_t quit_flag; static int dirty = 1; static int input_dirty; static time_t status_minute; -static int scrollback_limit = MAXSCROLL; static int cols = 80, lines = 24; -static int tree_offset; -static int treew = TREEW, mainh = 1, chat_top = 1; +static int treew = TREEW, mainh = 1; static struct termios saved_terminal; static int terminal_active; static int terminal_color; @@ -197,7 +168,7 @@ static long accent_color = -1; enum { KEY_UP = 256, KEY_DOWN, KEY_LEFT, KEY_RIGHT, - KEY_WORD_LEFT, KEY_WORD_RIGHT, KEY_TREE_UP, KEY_TREE_DOWN, + KEY_WORD_LEFT, KEY_WORD_RIGHT, KEY_HOME, KEY_END, KEY_PPAGE, KEY_NPAGE, KEY_DELETE }; @@ -218,8 +189,6 @@ static char save_path[MAXINPUT]; static int save_len, save_pos; static char history[MAXHIST][MAXINPUT]; static int hist_n, hist_cur = -1; -static int history_enabled; -static char history_path[PATH_MAX]; static int comp_active, comp_index; static char comp_prefix[MAXINPUT], comp_last[NICKLEN]; #define MAXRECENT 16 @@ -319,35 +288,17 @@ static Window *reply_window(Server *s) /* ------------------------------------------------------------------ */ /* windows and scrollback */ -static void window_insert(Server *s, Window *w) -{ - Window **link = &s->windows; - while (*link && ((*link)->type < w->type || - ((*link)->type == w->type && strcmp((*link)->name, w->name) <= 0))) - link = &(*link)->next; - w->next = *link; - *link = w; -} - -static void sendf(Server *s, const char *fmt, ...); - -static void monitor_query(Window *w, char action) -{ - if (w->type == W_QUERY && w->srv->monitor && - w->name[0] && !strpbrk(w->name, " ,\t\r\n")) - sendf(w->srv, "MONITOR %c %s", action, w->name); -} - static Window *window_new(Server *s, const char *name, Wintype type) { Window *w = xmalloc(sizeof *w); + Window **tail = &s->windows; memset(w, 0, sizeof *w); snprintf(w->name, sizeof w->name, "%s", name); w->type = type; w->srv = s; - w->lines = xalloc_array(scrollback_limit, sizeof *w->lines); - window_insert(s, w); - monitor_query(w, '+'); + w->lines = xalloc_array(MAXSCROLL, sizeof *w->lines); + while (*tail) tail = &(*tail)->next; + *tail = w; return w; } @@ -366,36 +317,10 @@ static Window *window_get(Server *s, const char *name, Wintype type) return w; } -static Window *next_visible_window(Window *w) -{ - return w->srv->tree_folded ? NULL : w->next; -} - -static void position_tree(int delta, int reveal_current) -{ - int height = lines > 5 ? lines - 5 : 1; - int count = 0, current = -1; - for (Server *s = servers; s; s = s->next) - for (Window *w = s->windows; w; w = next_visible_window(w), count++) - if (w == curwin) current = count; - - int offset = tree_offset + delta; - if (reveal_current && current >= 0) { - if (current < offset) offset = current; - if (current >= offset + height) offset = current - height + 1; - } - int limit = count > height ? count - height : 0; - if (offset < 0) offset = 0; - if (offset > limit) offset = limit; - tree_offset = offset; -} - static void focus_window(Window *w) { if (!w || w == curwin) return; curwin = w; - if (w->type != W_SERVER) w->srv->tree_folded = 0; - position_tree(0, 1); curwin->scroll = 0; curwin->unread = 0; dirty = 1; @@ -417,7 +342,6 @@ static void window_close(Server *s, Window *w) Window **p = &s->windows; while (*p && *p != w) p = &(*p)->next; if (!*p) return; - monitor_query(w, '-'); *p = w->next; for (int i = 0; i < w->nlines; i++) free(w->lines[i].text); free(w->lines); @@ -471,9 +395,9 @@ static void set_channel_topic(Window *w, const char *topic) static void win_append_raw(Window *w, const char *text, int keep_formatting, size_t body_offset) { - if (w->nlines == scrollback_limit) { + if (w->nlines == MAXSCROLL) { free(w->lines[0].text); - memmove(w->lines, w->lines + 1, (scrollback_limit - 1) * sizeof *w->lines); + memmove(w->lines, w->lines + 1, (MAXSCROLL - 1) * sizeof *w->lines); w->nlines--; } char stamp[16], *line; @@ -485,7 +409,7 @@ static void win_append_raw(Window *w, const char *text, int keep_formatting, siz if (body_offset > strlen(clean)) body_offset = strlen(clean); w->lines[w->nlines++] = (ScrollLine){line, strlen(stamp) + body_offset}; free(clean); - if (w->scroll > 0 && w->scroll < scrollback_limit * MAXWRAPROWS) w->scroll++; + if (w->scroll > 0) w->scroll++; if (w != curwin) w->unread++; dirty = 1; } @@ -547,7 +471,7 @@ static void switch_window(int dir) Window *target = NULL; if (dir > 0) { - target = next_visible_window(curwin); + target = curwin->next; if (!target) for (Server *s = curwin->srv->next; s; s = s->next) if (s->windows) { target = s->windows; break; } @@ -556,7 +480,7 @@ static void switch_window(int dir) if (t->windows) { target = t->windows; break; } } else { for (Server *s = servers; s; s = s->next) - for (Window *w = s->windows; w; w = next_visible_window(w)) { + for (Window *w = s->windows; w; w = w->next) { if (w == curwin) goto found; target = w; } @@ -565,12 +489,16 @@ static void switch_window(int dir) for (Server *s = servers; s; s = s->next) if (s->windows) { Window *last = s->windows; - for (Window *w = next_visible_window(last); w; w = next_visible_window(w)) - last = w; + while (last->next) last = last->next; target = last; } } - focus_window(target); + if (target && target != curwin) { + curwin = target; + curwin->scroll = 0; + curwin->unread = 0; + dirty = 1; + } } /* ------------------------------------------------------------------ */ @@ -694,59 +622,6 @@ static int parse_port(const char *str) return (int)v; } -static void load_blacklist(Server *s, char *list) -{ - char *save = NULL; - for (char *nick = strtok_r(list, ",", &save); nick; - nick = strtok_r(NULL, ",", &save)) { - nick += strspn(nick, " \t"); - size_t len = strlen(nick); - while (len && (nick[len - 1] == ' ' || nick[len - 1] == '\t')) - nick[--len] = 0; - if (!len) continue; - if (len >= NICKLEN || strpbrk(nick, " \t\r")) { - fprintf(stderr, "irc9: ignoring invalid blacklist nickname: %s\n", nick); - continue; - } - BlacklistedNick *entry = xmalloc(sizeof *entry); - strcpy(entry->nick, nick); - entry->next = s->blacklist; - s->blacklist = entry; - } -} - -/* A comma starts another command only when followed by a slash. */ -static const char *configured_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_configured_commands(const char *command, int allow_raw) -{ - if (!*command || strchr(command, '\r')) return 0; - while (command) { - const char *next; - const char *end = configured_command_end(command, &next); - if (*command == '/') { - if (end - command >= MAXINPUT || - command + 1 + strspn(command + 1, " \t") >= end) - return 0; - } else if (!allow_raw || end - command > MAXLINE - 2) { - return 0; - } - command = next; - } - return 1; -} - static void load_config(const char *path) { FILE *f = fopen(path, "r"); @@ -772,11 +647,11 @@ static void load_config(const char *path) key = strtok_r(p, " \t", &save); if (!key) continue; if (strcasecmp(key, "name") == 0) { - if (save) save += strspn(save, " \t"); - size_t len = save ? strlen(save) : 0; + save += strspn(save, " \t"); + size_t len = strlen(save); while (len && (save[len - 1] == ' ' || save[len - 1] == '\t')) save[--len] = 0; - if (last && len) { + if (last && *save) { free(last->name); last->name = dupstr(save); } else { @@ -784,43 +659,19 @@ static void load_config(const char *path) } continue; } - if (strcasecmp(key, "blacklist") == 0) { - if (last && save && *save) - load_blacklist(last, save); - else - fprintf(stderr, "irc9: blacklist requires a server and a list of nicknames\n"); - continue; - } if (strcasecmp(key, "autocommand") == 0) { - if (save) save += strspn(save, " \t"); - if (last && save && valid_configured_commands(save, 1)) { + save += strspn(save, " \t"); + if (last && *save && strlen(save) <= MAXLINE - 2 && + !strchr(save, '\r')) { free(last->autocommand); last->autocommand = dupstr(save); } else { - fprintf(stderr, "irc9: autocommand requires a server and valid commands\n"); + fprintf(stderr, "irc9: autocommand requires a server and an IRC command of at most %d bytes\n", + MAXLINE - 2); } 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); - *channel = (AutojoinChannel){0}; - strcpy(channel->name, a1); - *channel_tail = channel; - channel_tail = &channel->next; - - const char *command = save ? save + strspn(save, " \t") : ""; - if (valid_configured_commands(command, 0)) { - channel->commands = dupstr(command); - channel->next_command = channel->commands; - } else if (*command) - 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); @@ -860,6 +711,16 @@ 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); } @@ -1131,44 +992,6 @@ 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->next_autocommand || !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->commands) { - 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) { - AutojoinChannel *channel = *head; - *head = channel->next; - free(channel->commands); - free(channel); - } -} -#endif - static void disconnect_server(Server *s) { s->connect_gen++; @@ -1179,19 +1002,11 @@ static void disconnect_server(Server *s) #endif close(s->fd); s->fd = -1; - s->monitor = 0; - for (Window *w = s->windows; w; w = w->next) - w->query_offline = 0; s->connected = 0; - s->next_autocommand = NULL; s->inlen = 0; s->cap_state = CAP_NONE; s->caps[0] = 0; s->retry_at = time(NULL) + RETRY; -#ifdef USE_AUTOJOIN - free_autojoin(&s->pending_autojoin); - s->next_autojoin = (struct timespec){0}; -#endif } /* ------------------------------------------------------------------ */ @@ -1323,101 +1138,6 @@ static int handle_ctcp(Server *s, const char *nick, char *text, return 1; } -static void do_command(Window *window, char *line); - -/* Copy one command into a mutable buffer for do_command. */ -static void take_configured_command(const char **cursor, char line[MAXINPUT]) -{ - const char *command = *cursor; - const char *end = configured_command_end(command, cursor); - if (*cursor) - while (end > command && (end[-1] == ' ' || end[-1] == '\t')) end--; - - size_t len = (size_t)(end - command); - memcpy(line, command, len); - line[len] = 0; -} - -static void run_autocommand(Server *s) -{ - char line[MAXINPUT]; - take_configured_command(&s->next_autocommand, line); - if (s->next_autocommand) { - clock_gettime(CLOCK_MONOTONIC, &s->autocommand_at); - s->autocommand_at.tv_sec += 2; - } - - if (*line == '/') - do_command(serverwin(s), line); - else - sendf(s, "%s", line); -} - -static int command_due(struct timespec now, struct timespec deadline) -{ - return now.tv_sec > deadline.tv_sec || - (now.tv_sec == deadline.tv_sec && now.tv_nsec >= deadline.tv_nsec); -} - -static void run_pending_autocommands(Server *s) -{ - if (!s->next_autocommand) return; - struct timespec now; - clock_gettime(CLOCK_MONOTONIC, &now); - if (command_due(now, s->autocommand_at)) run_autocommand(s); -} - -#ifdef USE_AUTOJOIN -static void run_autojoin_command(AutojoinChannel **link, Window *w) -{ - AutojoinChannel *channel = *link; - char line[MAXINPUT]; - take_configured_command(&channel->next_command, line); - - 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 && ((*link)->joined || strcasecmp((*link)->name, w->name) != 0)) - link = &(*link)->next; - if (!*link) return; - - (*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 || !command_due(now, channel->command_at)) 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 - static void handle_line(Server *s, char *line) { char *prefix, *cmd; @@ -1490,8 +1210,6 @@ static void handle_line(Server *s, char *line) int notice = (strcasecmp(cmd, "NOTICE") == 0); const char *text = narg > 1 ? arg[1] : ""; if (narg < 1) return; - for (BlacklistedNick *entry = s->blacklist; entry; entry = entry->next) - if (strcasecmp(entry->nick, nick) == 0) return; Window *w; if (is_channel(arg[0])) w = window_get(s, arg[0], W_CHANNEL); @@ -1501,7 +1219,6 @@ static void handle_line(Server *s, char *line) w = window_get(s, nick, W_QUERY); else w = sw; - if (w->type == W_QUERY) w->query_offline = 0; if (w->type == W_QUERY && hostmask && strchr(hostmask, '!')) snprintf(w->hostmask, sizeof w->hostmask, "%s", hostmask); if (narg > 1 && handle_ctcp(s, nick, arg[1], w, notice)) return; @@ -1515,9 +1232,6 @@ 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) confirm_autojoin(w); -#endif return; } @@ -1533,7 +1247,7 @@ static void handle_line(Server *s, char *line) w = window_get(s, arg[0], W_CHANNEL); member_remove(w, nick); } - if (!silent) append_nickf(w, "* ", hostmask ? hostmask : "someone", " ", "left %s%s", arg[0], why); + append_nickf(w, "* ", hostmask ? hostmask : "someone", " ", "left %s%s", arg[0], why); return; } @@ -1547,48 +1261,25 @@ static void handle_line(Server *s, char *line) member_remove(w, nick); shown = 1; } - Window *query = window_find(s, nick); - if (query && query->type == W_QUERY) { - if (!query->query_offline) - appendf(query, "* %s went offline%s", nick, why); - query->query_offline = 1; - shown = 1; - } if (!shown) appendf(sw, "* %s quit%s", hostmask ? hostmask : nick, why); return; } if (strcasecmp(cmd, "NICK") == 0) { if (narg < 1) return; - int in_channel = 0; - Window *renamed = NULL; - for (Window **link = &s->windows; *link; ) { - Window *w = *link; + int shown = 0; + for (Window *w = s->windows; w; w = w->next) { if (w->type == W_CHANNEL && member_find(w, nick)) { member_rename(w, nick, arg[0]); - if (!silent) append_nickf(w, "* ", nick, " ", "is now known as %s", arg[0]); - in_channel = 1; + append_nickf(w, "* ", nick, " ", "is now known as %s", arg[0]); + shown = 1; } else if (w->type == W_QUERY && strcasecmp(w->name, nick) == 0) { - monitor_query(w, '-'); snprintf(w->name, sizeof w->name, "%s", arg[0]); - w->query_offline = 0; - monitor_query(w, '+'); - *link = w->next; - w->next = renamed; - renamed = w; - continue; } - link = &w->next; - } - while (renamed) { - Window *w = renamed; - renamed = w->next; - window_insert(s, w); } if (strcasecmp(nick, s->nick) == 0) snprintf(s->nick, sizeof s->nick, "%s", arg[0]); - if (!silent && !in_channel) appendf(sw, "* %s is now known as %s", nick, arg[0]); - dirty = 1; + if (!shown) appendf(sw, "* %s is now known as %s", nick, arg[0]); return; } @@ -1605,19 +1296,17 @@ static void handle_line(Server *s, char *line) if (strcasecmp(cmd, "MODE") == 0 && narg >= 2) { Window *w = window_find(s, arg[0]); if (!w) w = is_channel(arg[0]) ? window_get(s, arg[0], W_CHANNEL) : sw; + char params[MAXLINE]; + params[0] = 0; + for (int i = 1; i < narg; i++) { + if (i > 1) strncat(params, " ", sizeof params - strlen(params) - 1); + strncat(params, arg[i], sizeof params - strlen(params) - 1); + } if (w->type == W_CHANNEL) member_modes(w, arg[1], arg + 2, narg - 2); else if (strcasecmp(arg[0], s->nick) == 0) snprintf(s->umode, sizeof s->umode, "%s", arg[1]); - if (!silent) { - char params[MAXLINE] = ""; - for (int i = 1; i < narg; i++) { - if (i > 1) strncat(params, " ", sizeof params - strlen(params) - 1); - strncat(params, arg[i], sizeof params - strlen(params) - 1); - } - append_nickf(w, "* ", *nick ? nick : "server", " ", "sets mode %s on %s", - params, arg[0]); - } - dirty = 1; + append_nickf(w, "* ", *nick ? nick : "server", " ", "sets mode %s on %s", + params, arg[0]); return; } @@ -1641,12 +1330,7 @@ static void handle_line(Server *s, char *line) if (strcmp(cmd, "005") == 0) { for (int i = 1; i < narg; i++) { - if (!s->monitor && (strcmp(arg[i], "MONITOR") == 0 || - strncmp(arg[i], "MONITOR=", 8) == 0)) { - s->monitor = 1; - for (Window *w = s->windows; w; w = w->next) - monitor_query(w, '+'); - } else if (strncmp(arg[i], "PREFIX=(", 8) == 0) { + if (strncmp(arg[i], "PREFIX=(", 8) == 0) { const char *modes = arg[i] + 8; const char *end = strchr(modes, ')'); if (end && (size_t)(end - modes) == strlen(end + 1) && @@ -1663,22 +1347,6 @@ static void handle_line(Server *s, char *line) } } - if ((strcmp(cmd, "730") == 0 || strcmp(cmd, "731") == 0) && narg >= 2) { - int offline = strcmp(cmd, "731") == 0; - char *save = NULL; - for (char *target = strtok_r(arg[1], ",", &save); target; - target = strtok_r(NULL, ",", &save)) { - char *host = strchr(target, '!'); - if (host) *host = 0; - Window *w = window_find(s, target); - if (!w || w->type != W_QUERY) continue; - if (offline && !w->query_offline) - appendf(w, "* %s is offline", w->name); - w->query_offline = offline; - } - return; - } - if (strcmp(cmd, "353") == 0) { /* 353 : */ if (narg < 4) return; @@ -1716,16 +1384,16 @@ static void handle_line(Server *s, char *line) if (!w) w = window_get(s, arg[1], W_CHANNEL); if (w->type == W_CHANNEL) { show_names(w); - if (!silent && w->syncing) { + if (w->syncing) { struct timespec now; clock_gettime(CLOCK_MONOTONIC, &now); double dt = (now.tv_sec - w->join_ts.tv_sec) + (now.tv_nsec - w->join_ts.tv_nsec) / 1e9; appendf(w, "* synced %s in %.2fs", w->name, dt); - } else if (!silent) { + w->syncing = 0; + } else { appendf(w, "* synced %s", w->name); } - w->syncing = 0; } } return; @@ -1762,13 +1430,7 @@ static void handle_line(Server *s, char *line) return; } if (strcmp(cmd, "401") == 0) { - if (narg >= 3) { - Window *w = window_find(s, arg[1]); - if (w && w->type == W_QUERY) - appendf(w, "* %s is offline or unavailable: %s", arg[1], arg[2]); - if (!w || w->type != W_QUERY || (s->reply_win && s->reply_win != w)) - appendf(reply_window(s), "* %s: %s", arg[1], arg[2]); - } + if (narg >= 3) appendf(reply_window(s), "* %s: %s", arg[1], arg[2]); s->reply_win = NULL; return; } @@ -1853,8 +1515,7 @@ static void handle_line(Server *s, char *line) return; } if (strcmp(cmd, "461") == 0 || strcmp(cmd, "403") == 0 || - strcmp(cmd, "404") == 0 || strcmp(cmd, "405") == 0 || - strcmp(cmd, "482") == 0 || + strcmp(cmd, "404") == 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]); @@ -1864,11 +1525,17 @@ static void handle_line(Server *s, char *line) if (strcmp(cmd, "001") == 0) { int first_welcome = !s->connected; s->connected = 1; - appendf(sw, "%s", narg > 0 ? arg[narg - 1] : cmd); - if (first_welcome && s->autocommand) { - s->next_autocommand = s->autocommand; - run_autocommand(s); + if (first_welcome && s->autocommand) + sendf(s, "%s", s->autocommand); +#ifdef USE_AUTOJOIN + while (s->autojoin) { + AutojoinChannel *channel = s->autojoin; + sendf(s, "JOIN %s", channel->name); + s->autojoin = channel->next; + free(channel); } +#endif + appendf(sw, "%s", narg > 0 ? arg[narg - 1] : cmd); return; } @@ -1906,7 +1573,6 @@ 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 { @@ -1933,7 +1599,7 @@ static void read_server(Server *s) static void history_add(const char *s) { - if (*s != '/') return; + if (!*s) return; if (hist_n == MAXHIST) { for (int i = 1; i < MAXHIST; i++) memcpy(history[i - 1], history[i], sizeof history[i]); @@ -1943,64 +1609,15 @@ static void history_add(const char *s) hist_cur = -1; } -static void history_load(void) -{ - const char *home = getenv("HOME"); - if (!home || !*home || - snprintf(history_path, sizeof history_path, "%s/.irc9_history", home) - >= (int)sizeof history_path) { - history_path[0] = 0; - fprintf(stderr, "irc9: cannot locate ~/.irc9_history\n"); - return; - } - - FILE *file = fopen(history_path, "r"); - if (!file) { - if (errno != ENOENT) - fprintf(stderr, "irc9: cannot read %s: %s\n", history_path, strerror(errno)); - return; - } - - char *line = NULL; - size_t capacity = 0; - ssize_t length; - while ((length = getline(&line, &capacity, file)) >= 0) { - if (length && line[length - 1] == '\n') line[--length] = 0; - if (length < MAXINPUT && !memchr(line, 0, (size_t)length)) history_add(line); - } - if (!feof(file)) - fprintf(stderr, "irc9: cannot read %s: %s\n", history_path, strerror(errno)); - free(line); - fclose(file); -} - -static void history_record(const char *line) -{ - if (hist_n && strcmp(history[hist_n - 1], line) == 0) return; - history_add(line); - if (!*history_path) return; - - int fd = open(history_path, O_WRONLY | O_CREAT | O_APPEND, 0600); - int error = 0; - if (fd < 0) { - error = errno; - } else { - char entry[MAXINPUT + 1]; - int length = snprintf(entry, sizeof entry, "%s\n", line); - ssize_t written; - do { - written = write(fd, entry, (size_t)length); - } while (written < 0 && errno == EINTR); - if (written != length) error = written < 0 ? errno : EIO; - if (close(fd) < 0 && !error) error = errno; - } - if (error && curwin) - appendf(curwin, "* cannot write %s: %s", history_path, strerror(error)); -} - static void history_browse(int dir) { - if (!history_enabled || (dir > 0 && hist_cur < 0)) return; + if (dir > 0 && hist_cur < 0) { + /* Down on a freshly typed line saves it, then clears the input. */ + history_add(input); + input[0] = 0; + input_len = input_pos = 0; + return; + } if (hist_n == 0) return; if (hist_cur < 0) { /* dir < 0: start at the most recent entry. */ @@ -2177,13 +1794,13 @@ static char *next_word(char **p) return s; } -static Window *input_target(Window *window) +static Window *input_target(void) { - if (!window || window->type == W_SERVER) return NULL; - return window; + if (!curwin || curwin->type == W_SERVER) return NULL; + return curwin; } -static void do_command(Window *window, char *line) +static void do_command(char *line) { char *p = line + 1; while (*p == ' ' || *p == '\t') p++; @@ -2196,26 +1813,26 @@ static void do_command(Window *window, char *line) if (strcasecmp(cmd, "join") == 0 || strcasecmp(cmd, "j") == 0) { char *chan = next_word(&q); char chanbuf[CHANLEN]; - if (!chan || !window) return; + if (!chan || !curwin) return; if (!is_channel(chan)) { snprintf(chanbuf, sizeof chanbuf, "#%s", chan); chan = chanbuf; } - Window *w = window_get(window->srv, chan, W_CHANNEL); + Window *w = window_get(curwin->srv, chan, W_CHANNEL); w->syncing = 1; clock_gettime(CLOCK_MONOTONIC, &w->join_ts); focus_window(w); - sendf(window->srv, "JOIN %s", chan); + sendf(curwin->srv, "JOIN %s", chan); } else if (strcasecmp(cmd, "n") == 0 || strcasecmp(cmd, "names") == 0) { - Window *t = window; + Window *t = curwin; char *name = next_word(&q); - if (name && window) { - t = window_find(window->srv, name); - if (!t) appendf(window, "* no such window: %s", name); + if (name && curwin) { + t = window_find(curwin->srv, name); + if (!t) appendf(curwin, "* 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); + Window *t = input_target(); char *chan = next_word(&q); if (!t || !t->srv) return; if (!chan && t->type == W_CHANNEL) chan = t->name; @@ -2224,37 +1841,37 @@ static void do_command(Window *window, char *line) else sendf(t->srv, "PART %s", chan); } else if (strcasecmp(cmd, "nick") == 0) { char *newnick = next_word(&q); - if (!newnick || !window) return; - sendf(window->srv, "NICK %s", newnick); - appendf(serverwin(window->srv), "* changing nick to %s", newnick); + if (!newnick || !curwin) return; + sendf(curwin->srv, "NICK %s", newnick); + appendf(serverwin(curwin->srv), "* changing nick to %s", newnick); } else if (strcasecmp(cmd, "msg") == 0) { char *target = next_word(&q); - if (!target || !window || !*q) return; - sendf(window->srv, "PRIVMSG %s :%s", target, q); - Window *w = window_get(window->srv, target, + if (!target || !curwin || !*q) return; + sendf(curwin->srv, "PRIVMSG %s :%s", target, q); + Window *w = window_get(curwin->srv, target, is_channel(target) ? W_CHANNEL : W_QUERY); - append_nickf(w, "<", window->srv->nick, "> ", "%s", q); + append_nickf(w, "<", curwin->srv->nick, "> ", "%s", q); } else if (strcasecmp(cmd, "whois") == 0) { char *nick = next_word(&q); - 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); + 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); } else if (strcasecmp(cmd, "me") == 0) { - Window *t = input_target(window); + Window *t = input_target(); 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 (!window) return; - sendf(window->srv, "AWAY :%s", *q ? q : ""); - if (*q) appendf(window, "* away: %s", q); - else appendf(window, "* not away"); + if (!curwin) return; + sendf(curwin->srv, "AWAY :%s", *q ? q : ""); + if (*q) appendf(curwin, "* away: %s", q); + else appendf(curwin, "* not away"); } else if (strcasecmp(cmd, "topic") == 0) { - Window *t = input_target(window); + Window *t = input_target(); if (!t || t->type != W_CHANNEL) { - if (window) appendf(window, "* no channel here"); + if (curwin) appendf(curwin, "* no channel here"); return; } t->srv->reply_win = t; @@ -2262,36 +1879,36 @@ static void do_command(Window *window, char *line) else sendf(t->srv, "TOPIC %s", t->name); } else if (strcasecmp(cmd, "invite") == 0) { char *nick = next_word(&q); - if (!nick || !window) return; + if (!nick || !curwin) return; char *chan = next_word(&q); - 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); + 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); } else if (strcasecmp(cmd, "kick") == 0) { - Window *t = input_target(window); + Window *t = input_target(); char *nick = next_word(&q); if (!t || t->type != W_CHANNEL || !nick) { - if (window) appendf(window, "* usage: /kick nick [reason] in a channel"); + if (curwin) appendf(curwin, "* 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 && 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); + 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); } 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); + Window *t = input_target(); char *who = next_word(&q); if (!t || t->type != W_CHANNEL || !who) { - if (window) appendf(window, "* usage: /%s nick|mask in a channel", cmd); + if (curwin) appendf(curwin, "* usage: /%s nick|mask in a channel", cmd); return; } const char *flag = !strcasecmp(cmd, "op") ? "+o" : @@ -2303,58 +1920,58 @@ static void do_command(Window *window, 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 || !window || !*q) return; - sendf(window->srv, "NOTICE %s :%s", target, q); - Window *w = window_get(window->srv, target, + if (!target || !curwin || !*q) return; + sendf(curwin->srv, "NOTICE %s :%s", target, q); + Window *w = window_get(curwin->srv, target, is_channel(target) ? W_CHANNEL : W_QUERY); - append_nickf(w, "-", window->srv->nick, "- ", "%s", q); + append_nickf(w, "-", curwin->srv->nick, "- ", "%s", q); } else if (strcasecmp(cmd, "query") == 0) { char *nick = next_word(&q); - 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); + 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); } else if (strcasecmp(cmd, "who") == 0) { - if (!window) return; - window->srv->reply_win = window; - sendf(window->srv, "WHO%s%s", *q ? " " : "", q); + if (!curwin) return; + curwin->srv->reply_win = curwin; + sendf(curwin->srv, "WHO%s%s", *q ? " " : "", q); } else if (strcasecmp(cmd, "list") == 0) { - if (!window) return; - window->srv->reply_win = window; - sendf(window->srv, "LIST%s%s", *q ? " " : "", q); + if (!curwin) return; + curwin->srv->reply_win = curwin; + sendf(curwin->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 (!window) return; - window->srv->reply_win = window; - sendf(window->srv, "%s%s%s", cmd, *q ? " " : "", q); - appendf(window, "* %s", cmd); + if (!curwin) return; + curwin->srv->reply_win = curwin; + sendf(curwin->srv, "%s%s%s", cmd, *q ? " " : "", q); + appendf(curwin, "* %s", cmd); } else if (strcasecmp(cmd, "ping") == 0) { char *nick = next_word(&q); - if (!nick || !window) return; - sendf(window->srv, "PING %s", nick); - appendf(window, "* pinging %s", nick); + if (!nick || !curwin) return; + sendf(curwin->srv, "PING %s", nick); + appendf(curwin, "* pinging %s", nick); } else if (strcasecmp(cmd, "ctcp") == 0) { char *nick = next_word(&q); - if (!nick || !window || !*q) return; - sendf(window->srv, "PRIVMSG %s :\001%s\001", nick, q); - appendf(window, "* ctcp %s %s", nick, q); + if (!nick || !curwin || !*q) return; + sendf(curwin->srv, "PRIVMSG %s :\001%s\001", nick, q); + appendf(curwin, "* ctcp %s %s", nick, q); } else if (strcasecmp(cmd, "raw") == 0) { - if (!window || !*q) return; - sendf(window->srv, "%s", q); - appendf(window, "* -> %s", q); + if (!curwin || !*q) return; + sendf(curwin->srv, "%s", q); + appendf(curwin, "* -> %s", q); } else if (strcasecmp(cmd, "close") == 0 || strcasecmp(cmd, "wc") == 0) { - if (!window) return; - if (window == window->srv->windows) { - appendf(window, "* cannot close the server window"); + if (!curwin) return; + if (curwin == curwin->srv->windows) { + appendf(curwin, "* cannot close the server window"); return; } - window_close(window->srv, window); + window_close(curwin->srv, curwin); } else if (strcasecmp(cmd, "clear") == 0) { - window_clear(window); + window_clear(curwin); } else if (strcasecmp(cmd, "reconnect") == 0) { - if (!window) return; - Server *s = window->srv; + if (!curwin) return; + Server *s = curwin->srv; appendf(serverwin(s), "* reconnecting to %s:%d", s->host, s->port); disconnect_server(s); s->retry_at = 0; @@ -2364,7 +1981,7 @@ static void do_command(Window *window, char *line) sendf(s, "QUIT :%s", *q ? q : "irc9"); quit_flag = 1; } else if (strcasecmp(cmd, "help") == 0) { - Window *t = window ? window : server_window_owner()->windows; + Window *t = curwin ? curwin : 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"); @@ -2372,10 +1989,9 @@ static void do_command(Window *window, char *line) appendf(t, " /voice /devoice /ban /unban /raw /close /wc /clear"); appendf(t, " /reconnect /quit /help"); 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-s save scrollback, ctrl-t toggle copy view"); } else { - if (window) appendf(window, "* unknown command: /%s", cmd); + if (curwin) appendf(curwin, "* unknown command: /%s", cmd); } } @@ -2383,13 +1999,12 @@ static void submit_input(void) { char line[MAXINPUT]; snprintf(line, sizeof line, "%s", input); - hist_cur = -1; + history_add(line); if (*line == '/') { - if (history_enabled) history_record(line); - do_command(curwin, line); + do_command(line); } else if (*line) { - Window *t = input_target(curwin); + Window *t = input_target(); if (!t) { if (curwin) appendf(curwin, "* no target here; use /join or /msg"); } else { @@ -2516,35 +2131,17 @@ static void handle_key(int c) } - if (c == '\n' || c == '\r') { - if (!*input && curwin && curwin->type == W_SERVER) { - curwin->srv->tree_folded = !curwin->srv->tree_folded; - hist_cur = -1; - position_tree(0, 1); - dirty = 1; - } else { - submit_input(); - } - return; - } + if (c == '\n' || c == '\r') { submit_input(); return; } if (c == KEY_UP) { history_browse(-1); input_dirty = 1; return; } if (c == KEY_DOWN) { history_browse(1); input_dirty = 1; return; } - if (c == KEY_TREE_UP || c == KEY_TREE_DOWN) { - if (treew >= 2 && lines >= 6) { - position_tree(c == KEY_TREE_UP ? -1 : 1, 0); - dirty = 1; - } - return; - } if (c == KEY_PPAGE) { if (curwin) { int height = mainh - layout_topic(0); int step = height > 1 ? height - 1 : 1; if (curwin->scroll < 0) curwin->scroll = 0; - int limit = scrollback_limit * MAXWRAPROWS; - if (curwin->scroll > limit - step) - curwin->scroll = limit; + if (curwin->scroll > MAXSCROLLROWS - step) + curwin->scroll = MAXSCROLLROWS; else curwin->scroll += step; dirty = 1; @@ -2571,8 +2168,7 @@ static void layout(void) treew = cols >= 60 ? TREEW : (cols >= 30 ? 16 : 8); if (copy_view) treew = 0; else if (treew > cols / 2) treew = cols / 2; - chat_top = lines >= 7 ? 2 : 1; - mainh = lines >= 5 ? lines - 3 - chat_top : 0; + mainh = lines > 4 ? lines - 4 : 0; } static void terminal_move(int row, int col) @@ -2691,9 +2287,7 @@ static void push_wrapped(char **rows, int *nr, int maxrows, const char *s, int w int curvis = 0; cur[0] = 0; - char *save = NULL; - for (char *tok = strtok_r(copy, " \t", &save); tok; - tok = strtok_r(NULL, " \t", &save)) { + for (char *tok = strtok(copy, " \t"); tok; tok = strtok(NULL, " \t")) { int tlvis = ircfmt_vislen(tok); size_t tlbytes = strlen(tok); if (curvis == 0) { @@ -2751,50 +2345,37 @@ static int irc_to_ansi_color(int color) static void draw_tree(void) { - int bottom = lines - 3; - if (treew < 2 || lines < 6) return; + if (treew < 2 || mainh < 2) return; terminal_accent(); terminal_move(1, 0); putchar('/'); terminal_repeat('-', treew - 2); putchar('\\'); - for (int row = 2; row < bottom; row++) { + for (int row = 2; row < mainh; row++) { terminal_move(row, 0); putchar('|'); terminal_move(row, treew - 1); putchar('|'); } - terminal_move(bottom, 0); + terminal_move(mainh, 0); putchar('\\'); - terminal_repeat('-', treew - 2); + terminal_repeat('_', treew - 2); putchar('/'); fputs("\033[0m", stdout); - position_tree(0, 0); - int index = 0; + int y = 1; for (Server *s = servers; s; s = s->next) { - for (Window *w = s->windows; w; w = next_visible_window(w), index++) { - if (index < tree_offset) continue; - int y = 2 + index - tree_offset; - if (y >= bottom) return; - if (w == s->windows) { - if (treew > 3) { - terminal_move(y, 1); - fputs(w == curwin ? (terminal_color ? "\033[1;4m" : "\033[7m") : "\033[1m", stdout); - putchar(s->connected ? '*' : ' '); - if (s->tree_folded) putchar('+'); - long long unread = 0; - for (Window *child = s->windows; child; child = child->next) - unread += child->unread; - int columns = treew - 3 - s->tree_folded; - char count[32] = ""; - int count_width = unread ? snprintf(count, sizeof count, " (%lld)", unread) : 0; - if (count_width > columns) count_width = 0; - terminal_text(s->name ? s->name : s->host, columns - count_width); - if (count_width) fputs(count, stdout); - fputs("\033[0m", stdout); - } - continue; - } + if (y >= mainh - 1) break; + int selected = (serverwin(s) == curwin); + if (treew > 4) { + terminal_move(y, 1); + fputs(selected ? (terminal_color ? "\033[1;4m" : "\033[7m") : "\033[1m", stdout); + printf("%c ", s->connected ? '*' : ' '); + terminal_text(s->name ? s->name : s->host, treew - 4); + fputs("\033[0m", stdout); + } + y++; + for (Window *w = s->windows->next; w; w = w->next) { + if (y >= mainh - 1) break; char label[CHANLEN + 16]; if (w->unread) snprintf(label, sizeof label, "%s(%d)", w->name, w->unread); else snprintf(label, sizeof label, "%s", w->name); @@ -2809,6 +2390,7 @@ static void draw_tree(void) terminal_text(label, treew - 6); fputs("\033[0m", stdout); } + y++; } } } @@ -2892,22 +2474,6 @@ static void draw_title(void) fputs("\033[0m", stdout); } -static void draw_pane_divider(int row, int opening, int closing) -{ - int left = lines > 5 && treew >= 2 ? treew : 0; - terminal_move(row, left); - terminal_accent(); - int width = cols - left; - if (width > 1) { - putchar(opening); - terminal_repeat('-', width - 2); - putchar(closing); - } else if (width == 1) { - putchar('-'); - } - fputs("\033[0m", stdout); -} - /* Include the topic divider when calculating the chat pane height. */ static int layout_topic(int draw) { @@ -2915,7 +2481,9 @@ static int layout_topic(int draw) if (!curwin || curwin->type != W_CHANNEL || mainh < 1 || width < 1) return 0; - const char *topic = curwin->topic[0] ? curwin->topic : "(no topic)"; + char topic[MAXLINE + sizeof "Topic: "]; + snprintf(topic, sizeof topic, "Topic: %s", + curwin->topic[0] ? curwin->topic : "(no topic)"); char *rows[MAXWRAPROWS]; int count = 0; push_wrapped(rows, &count, MAXWRAPROWS, topic, width); @@ -2926,10 +2494,15 @@ static int layout_topic(int draw) int height = count < limit ? count : limit; for (int row = 0; row < count; row++) { char *text = rows[count - 1 - row]; - if (draw && row < height) draw_irc_text(chat_top + row, left, text, width); + if (draw && row < height) draw_irc_text(row + 1, left, text, width); free(text); } - if (draw && divider) draw_pane_divider(chat_top + height, '-', '|'); + if (draw && divider) { + terminal_move(height + 1, left); + terminal_accent(); + terminal_repeat('-', width); + fputs("\033[0m", stdout); + } return height + divider; } @@ -2961,9 +2534,9 @@ static void draw_main(void) if (curwin->scroll > maxscroll) curwin->scroll = maxscroll; } for (int r = 0; r < h; r++) { - long long idx = (long long)curwin->scroll + h - 1 - r; + int idx = curwin->scroll + (h - 1 - r); if (idx >= 0 && idx < nr && rows[idx]) - draw_irc_text(chat_top + header + r, left, rows[idx], ww); + draw_irc_text(r + 1 + header, left, rows[idx], ww); } for (int i = 0; i < nr; i++) free(rows[i]); free(rows); @@ -2974,6 +2547,10 @@ static void draw_status(void) { status_minute = time(NULL) / 60; if (lines < 4) return; + terminal_move(lines - 3, 0); + terminal_accent(); + terminal_repeat('-', cols); + fputs("\033[0m", stdout); fputs(terminal_color ? "\033[1m" : "\033[7m", stdout); terminal_move(lines - 2, 0); terminal_repeat(' ', cols); @@ -3020,7 +2597,7 @@ static void draw_input(void) x += width; i += len ? (int)len : 1; } - while (x >= cols && start < pos) { + while (saving && x >= cols && start < pos) { int width; size_t len = terminal_character(text + start, (size_t)(pos - start), &width); start += len ? (int)len : 1; @@ -3037,9 +2614,7 @@ static void redraw(void) fputs("\033[?25l\033[0m\033[2J", stdout); draw_title(); draw_tree(); - if (chat_top > 1) draw_pane_divider(1, '/', '\\'); draw_main(); - if (lines >= 4) draw_pane_divider(lines - 3, '\\', '/'); draw_status(); draw_input(); fputs("\033[?25h", stdout); @@ -3136,8 +2711,8 @@ static void terminal_read_keys(void) if (sequence == -1) continue; int ctrl_arrow = sequence == '[' && number == 1 && modifier == 5; switch (c) { - case 'A': c = ctrl_arrow ? KEY_TREE_UP : KEY_UP; break; - case 'B': c = ctrl_arrow ? KEY_TREE_DOWN : KEY_DOWN; break; + case 'A': c = KEY_UP; break; + case 'B': c = KEY_DOWN; break; case 'C': c = ctrl_arrow ? KEY_WORD_RIGHT : KEY_RIGHT; break; case 'D': c = ctrl_arrow ? KEY_WORD_LEFT : KEY_LEFT; break; case 'H': c = KEY_HOME; break; @@ -3198,12 +2773,6 @@ static const char *find_config(void) return home_path; } -static void print_usage(FILE *out, const char *program) -{ - fprintf(out, "usage: %s [-h|--help] [-v|--version] [-s|--silent] " - "[-C|--colored] [-c|--color RRGGBB] [--scrollback LINES] [--history] [config]\n", program); -} - int main(int argc, char **argv) { const char *path = NULL; @@ -3212,13 +2781,6 @@ int main(int argc, char **argv) const char *arg = argv[i]; if (options && strcmp(arg, "--") == 0) { options = 0; - } else if (options && (strcmp(arg, "-v") == 0 || strcmp(arg, "--version") == 0)) { - puts("irc9 " IRC9_VERSION); - return 0; - } else if (options && (strcmp(arg, "-h") == 0 || strcmp(arg, "--help") == 0)) { - puts("irc9 " IRC9_VERSION); - print_usage(stdout, argv[0]); - return 0; } else if (options && (strcmp(arg, "-s") == 0 || strcmp(arg, "--silent") == 0)) { silent = 1; } else if (options && (strcmp(arg, "-C") == 0 || strcmp(arg, "--colored") == 0)) { @@ -3230,23 +2792,10 @@ int main(int argc, char **argv) return 1; } accent_color = strtol(argv[i], NULL, 16); - } else if (options && strcmp(arg, "--history") == 0) { - history_enabled = 1; - } else if (options && strcmp(arg, "--scrollback") == 0) { - const char *value = ++i < argc ? argv[i] : ""; - errno = 0; - long limit = strtol(value, NULL, 10); - if (!*value || value[strspn(value, "0123456789")] || errno == ERANGE || - limit < 1 || limit > INT_MAX / MAXWRAPROWS) { - fprintf(stderr, "irc9: --scrollback requires a line count from 1 to %d\n", - INT_MAX / MAXWRAPROWS); - return 1; - } - scrollback_limit = (int)limit; } else if (!path && (!options || arg[0] != '-' || arg[1] == 0)) { path = arg; } else { - print_usage(stderr, argv[0]); + fprintf(stderr, "usage: %s [-s|--silent] [-C|--colored] [-c|--color RRGGBB] [config]\n", argv[0]); return 1; } } @@ -3258,7 +2807,6 @@ int main(int argc, char **argv) setlocale(LC_ALL, ""); load_config(path); - if (history_enabled) history_load(); #ifdef USE_OPENSSL tls_ctx = SSL_CTX_new(TLS_client_method()); @@ -3302,18 +2850,9 @@ 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); - run_pending_autocommands(s); - if (quit_flag) break; -#ifdef USE_AUTOJOIN - send_autojoin(s); - run_pending_autojoin_commands(s); -#endif - } - - if (quit_flag) break; struct pollfd fds[64]; Server *owner[64]; @@ -3367,16 +2906,14 @@ int main(int argc, char **argv) #endif if (s->fd >= 0) close(s->fd); #ifdef USE_AUTOJOIN - free_autojoin(&s->autojoin); - free_autojoin(&s->pending_autojoin); + while (s->autojoin) { + AutojoinChannel *channel = s->autojoin; + s->autojoin = channel->next; + free(channel); + } #endif free(s->name); free(s->autocommand); - while (s->blacklist) { - BlacklistedNick *entry = s->blacklist; - s->blacklist = entry->next; - free(entry); - } free(s); s = sn; } diff --git a/irc9rc-example b/irc9rc-example index b104cf7..4923c70 100644 --- a/irc9rc-example +++ b/irc9rc-example @@ -8,22 +8,16 @@ real Happy irc9 User # Libera and OFTC use TLS on 6697. Use port 6667 without the ssl keyword for # plain TCP. The optional sasl line applies to the server above it; the SASL -# user is your account name. An optional autocommand runs after registration on -# the server above it, before startup channel joins. Separate slash commands with -# commas. irc9 waits two seconds between commands. +# user is your account name. An optional autocommand sends one raw IRC command +# after registration on the server above it, before startup channel joins. # libera server irc.libera.chat 6697 ssl name Libera Chat -# Suppress incoming messages from these nicknames on this server. -# blacklist user1,user2,user3 # sasl irc9user my-sasl-password -# autocommand /query user1,/query user2 +# 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. -# 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 diff --git a/screenshot.png b/screenshot.png deleted file mode 100644 index 8a4356b..0000000 Binary files a/screenshot.png and /dev/null differ