diff --git a/README.md b/README.md index 85cc73f..505452e 100644 --- a/README.md +++ b/README.md @@ -16,6 +16,9 @@ make ``` `-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. diff --git a/irc9.c b/irc9.c index 68a30fe..ec40c61 100644 --- a/irc9.c +++ b/irc9.c @@ -154,6 +154,7 @@ static int treew = TREEW, mainh = 1; static struct termios saved_terminal; static int terminal_active; static int terminal_color; +static int colored_nicks; static long accent_color = -1; enum { @@ -382,7 +383,7 @@ static void set_channel_topic(Window *w, const char *topic) dirty = 1; } -static void win_append_raw(Window *w, const char *text) +static void win_append_raw(Window *w, const char *text, int keep_formatting) { if (w->nlines == MAXSCROLL) { free(w->lines[0]); @@ -390,7 +391,7 @@ static void win_append_raw(Window *w, const char *text) w->nlines--; } char stamp[16], *line; - char *clean = strip_formatting ? strip_ircfmt(text) : dupstr(text); + char *clean = strip_formatting && !keep_formatting ? strip_ircfmt(text) : dupstr(text); fmttime(stamp, sizeof stamp, "[%H:%M] "); size_t sz = size_add(size_add(strlen(stamp), strlen(clean)), 1); line = xmalloc(sz); @@ -409,7 +410,46 @@ static void appendf(Window *w, const char *fmt, ...) va_start(ap, fmt); vsnprintf(buf, sizeof buf, fmt, ap); va_end(ap); - win_append_raw(w, buf); + win_append_raw(w, buf, 0); +} + +/* Keep green reserved for our nick and assign others a stable palette color. */ +static int nick_color(const Server *s, const char *nick, size_t len) +{ + if (strlen(s->nick) == len && strncasecmp(s->nick, nick, len) == 0) return 3; + static const int palette[] = {4, 6, 7, 10, 12}; + uint32_t hash = 0; + for (size_t i = 0; i < len; i++) + hash = hash * 33 + (unsigned char)tolower((unsigned char)nick[i]); + return palette[hash % (sizeof palette / sizeof *palette)]; +} + +static void append_nickf(Window *w, const char *before, const char *identity, + const char *fmt, ...) +{ + char after[MAXLINE * 2]; + va_list ap; + va_start(ap, fmt); + vsnprintf(after, sizeof after, fmt, ap); + va_end(ap); + + if (!colored_nicks || !terminal_color || w->type == W_SERVER) { + appendf(w, "%s%s%s", before, identity, after); + return; + } + + size_t len = strcspn(identity, "!@"); + char *plain = strip_formatting ? strip_ircfmt(after) : NULL; + const char *suffix = plain ? plain : after; + size_t size = size_add(size_add(strlen(before), strlen(identity)), + size_add(strlen(suffix), 5)); + char *line = xmalloc(size); + snprintf(line, size, "%s\003%02d%.*s\003%s%s", + before, nick_color(w->srv, identity, len), (int)len, identity, + identity + len, suffix); + free(plain); + win_append_raw(w, line, 1); + free(line); } static void switch_window(int dir) @@ -542,12 +582,18 @@ static void show_names(Window *w) for (Member *m = w->members; m; m = m->next) { size_t room = sizeof line - (size_t)len - 1; - int add = snprintf(line + len, room, " %s%s", m->nick, m->status ? "*" : ""); + int add; + if (colored_nicks && terminal_color) + add = snprintf(line + len, room, " \003%02d%s\003%s", + nick_color(w->srv, m->nick, strlen(m->nick)), m->nick, + m->status ? "*" : ""); + else + add = snprintf(line + len, room, " %s%s", m->nick, m->status ? "*" : ""); if (add <= 0 || (size_t)add >= room) break; len += add; } line[len] = 0; - win_append_raw(w, line); + win_append_raw(w, line, colored_nicks); } /* ------------------------------------------------------------------ */ @@ -1060,9 +1106,9 @@ static int handle_ctcp(Server *s, const char *nick, char *text, const char *arg = argp ? argp : ""; if (is_reply) { - appendf(w, "* %s CTCP %s: %s", nick, cmd, arg); + append_nickf(w, "* ", nick, " CTCP %s: %s", cmd, arg); } else if (strcasecmp(cmd, "ACTION") == 0) { - appendf(w, "* %s %s", nick, arg); + append_nickf(w, "* ", nick, " %s", arg); } else if (strcasecmp(cmd, "VERSION") == 0) { sendf(s, "NOTICE %s :\001VERSION irc9\001", nick); } else if (strcasecmp(cmd, "PING") == 0) { @@ -1074,7 +1120,7 @@ static int handle_ctcp(Server *s, const char *nick, char *text, } else if (strcasecmp(cmd, "USERINFO") == 0) { sendf(s, "NOTICE %s :\001USERINFO %s\001", nick, g_real); } else { - appendf(w, "* %s CTCP %s", nick, cmd); + append_nickf(w, "* ", nick, " CTCP %s", cmd); } return 1; } @@ -1163,8 +1209,8 @@ static void handle_line(Server *s, char *line) 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; - if (notice) appendf(w, "-%s- %s", *nick ? nick : "server", text); - else appendf(w, "<%s> %s", *nick ? nick : "server", text); + if (notice) append_nickf(w, "-", *nick ? nick : "server", "- %s", text); + else append_nickf(w, "<", *nick ? nick : "server", "> %s", text); return; } @@ -1172,7 +1218,7 @@ static void handle_line(Server *s, char *line) if (narg < 1) return; Window *w = window_get(s, arg[0], W_CHANNEL); member_add(w, nick); - if (!silent) appendf(w, "* %s joined %s", hostmask ? hostmask : "someone", arg[0]); + if (!silent) append_nickf(w, "* ", hostmask ? hostmask : "someone", " joined %s", arg[0]); return; } @@ -1188,7 +1234,7 @@ static void handle_line(Server *s, char *line) w = window_get(s, arg[0], W_CHANNEL); member_remove(w, nick); } - appendf(w, "* %s left %s%s", hostmask ? hostmask : "someone", arg[0], why); + append_nickf(w, "* ", hostmask ? hostmask : "someone", " left %s%s", arg[0], why); return; } @@ -1198,7 +1244,7 @@ static void handle_line(Server *s, char *line) int shown = 0; for (Window *w = s->windows; w; w = w->next) if (w->type == W_CHANNEL && member_find(w, nick)) { - if (!silent) appendf(w, "* %s quit%s", hostmask ? hostmask : nick, why); + if (!silent) append_nickf(w, "* ", hostmask ? hostmask : nick, " quit%s", why); member_remove(w, nick); shown = 1; } @@ -1212,7 +1258,7 @@ static void handle_line(Server *s, char *line) for (Window *w = s->windows; w; w = w->next) { if (w->type == W_CHANNEL && member_find(w, nick)) { member_rename(w, nick, arg[0]); - appendf(w, "* %s is now known as %s", nick, arg[0]); + append_nickf(w, "* ", nick, " is now known as %s", arg[0]); shown = 1; } else if (w->type == W_QUERY && strcasecmp(w->name, nick) == 0) { snprintf(w->name, sizeof w->name, "%s", arg[0]); @@ -1228,8 +1274,8 @@ static void handle_line(Server *s, char *line) Window *w = window_get(s, arg[0], W_CHANNEL); char why[MAXLINE]; if (narg > 2) snprintf(why, sizeof why, " (%s)", arg[2]); else why[0] = 0; - appendf(w, "* %s was kicked from %s by %s%s", - arg[1], arg[0], *nick ? nick : "server", why); + append_nickf(w, "* ", arg[1], " was kicked from %s by %s%s", + arg[0], *nick ? nick : "server", why); member_remove(w, arg[1]); return; } @@ -1246,7 +1292,7 @@ static void handle_line(Server *s, char *line) 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]); - appendf(w, "* %s sets mode %s on %s", *nick ? nick : "server", + append_nickf(w, "* ", *nick ? nick : "server", " sets mode %s on %s", params, arg[0]); return; } @@ -1254,7 +1300,7 @@ static void handle_line(Server *s, char *line) if (strcasecmp(cmd, "TOPIC") == 0 && narg >= 2) { Window *w = window_get(s, arg[0], W_CHANNEL); set_channel_topic(w, arg[1]); - appendf(w, "* %s changed topic of %s: %s", *nick ? nick : "server", + append_nickf(w, "* ", *nick ? nick : "server", " changed topic of %s: %s", arg[0], arg[1]); return; } @@ -1791,7 +1837,7 @@ static void do_command(char *line) sendf(curwin->srv, "PRIVMSG %s :%s", target, q); Window *w = window_get(curwin->srv, target, is_channel(target) ? W_CHANNEL : W_QUERY); - appendf(w, "<%s> %s", curwin->srv->nick, q); + append_nickf(w, "<", curwin->srv->nick, "> %s", q); } else if (strcasecmp(cmd, "whois") == 0) { char *nick = next_word(&q); if (!curwin) return; @@ -1803,7 +1849,7 @@ static void do_command(char *line) Window *t = input_target(); if (!t || !*q) return; sendf(t->srv, "PRIVMSG %s :\001ACTION %s\001", t->name, q); - appendf(t, "* %s %s", t->srv->nick, 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 : ""); @@ -1865,7 +1911,7 @@ static void do_command(char *line) sendf(curwin->srv, "NOTICE %s :%s", target, q); Window *w = window_get(curwin->srv, target, is_channel(target) ? W_CHANNEL : W_QUERY); - appendf(w, "-%s- %s", curwin->srv->nick, q); + append_nickf(w, "-", curwin->srv->nick, "- %s", q); } else if (strcasecmp(cmd, "query") == 0) { char *nick = next_word(&q); if (!curwin) return; @@ -1950,7 +1996,7 @@ static void submit_input(void) if (curwin) appendf(curwin, "* no target here; use /join or /msg"); } else { sendf(t->srv, "PRIVMSG %s :%s", t->name, line); - appendf(t, "<%s> %s", t->srv->nick, line); + append_nickf(t, "<", t->srv->nick, "> %s", line); } } @@ -2247,6 +2293,12 @@ static void terminal_accent(void) } } +static int irc_to_ansi_color(int color) +{ + static const int map[] = {7, 0, 4, 2, 1, 1, 5, 3, 3, 2, 6, 6, 4, 5, 0, 7}; + return color >= 0 && color < 16 ? map[color] : 9; +} + static void draw_tree(void) { if (treew < 2 || mainh < 2) return; @@ -2289,6 +2341,8 @@ static void draw_tree(void) fputs(w->next ? "|-" : "\\-", stdout); fputs("\033[0m", stdout); if (w == curwin) fputs(terminal_color ? "\033[1;4m" : "\033[7m", stdout); + if (colored_nicks && terminal_color && w->type == W_QUERY) + printf("\033[%dm", 30 + irc_to_ansi_color(nick_color(s, w->name, strlen(w->name)))); terminal_text(label, treew - 6); fputs("\033[0m", stdout); } @@ -2297,12 +2351,6 @@ static void draw_tree(void) } } -static int irc_to_ansi_color(int color) -{ - static const int map[] = {7, 0, 4, 2, 1, 1, 5, 3, 3, 2, 6, 6, 4, 5, 0, 7}; - return color >= 0 && color < 16 ? map[color] : 9; -} - static void draw_irc_text(int y, int x, const char *s, int maxvis) { int vis = 0; @@ -2441,13 +2489,19 @@ static void draw_input(void) snprintf(umode, sizeof umode, "[%s]", curwin->srv->umode); else umode[0] = 0; - snprintf(status, sizeof status, "[%s] (%s@%s:%d%s)%s%s", - now, curwin->srv->nick, curwin->srv->host, curwin->srv->port, + int color_nick = colored_nicks && terminal_color; + snprintf(status, sizeof status, "%s[%s] (%s%s%s@%s:%d%s)%s%s", + color_nick ? "\002" : "", now, color_nick ? "\00303" : "", + curwin->srv->nick, color_nick ? "\003" : "", + curwin->srv->host, curwin->srv->port, curwin->srv->tls ? " [TLS]" : "", umode[0] ? " " : "", umode); if (cols > 2) { - terminal_move(lines - 2, 1); - terminal_text(status, cols - 2); + if (color_nick) draw_irc_text(lines - 2, 1, status, cols - 2); + else { + terminal_move(lines - 2, 1); + terminal_text(status, cols - 2); + } } } fputs("\033[0m", stdout); @@ -2641,6 +2695,8 @@ int main(int argc, char **argv) options = 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)) { + colored_nicks = 1; } else if (options && (strcmp(arg, "-c") == 0 || strcmp(arg, "--color") == 0)) { if (++i == argc || strlen(argv[i]) != 6 || strspn(argv[i], "0123456789abcdefABCDEF") != 6) { @@ -2651,7 +2707,7 @@ int main(int argc, char **argv) } else if (!path && (!options || arg[0] != '-' || arg[1] == 0)) { path = arg; } else { - fprintf(stderr, "usage: %s [-s|--silent] [-c|--color RRGGBB] [config]\n", argv[0]); + fprintf(stderr, "usage: %s [-s|--silent] [-C|--colored] [-c|--color RRGGBB] [config]\n", argv[0]); return 1; } }