diff --git a/irc9.c b/irc9.c index bf19482..63fa3f7 100644 --- a/irc9.c +++ b/irc9.c @@ -89,6 +89,7 @@ typedef struct Window { struct Server *srv; struct Window *next; ScrollLine *lines; + int query_offline; int nlines; int unread; int scroll; @@ -119,6 +120,7 @@ typedef struct Server { char real[128]; int fd; int connected; + int monitor; int connecting; unsigned long connect_gen; time_t retry_at; @@ -319,6 +321,15 @@ static void window_insert(Server *s, Window *w) *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); @@ -328,6 +339,7 @@ static Window *window_new(Server *s, const char *name, Wintype type) w->srv = s; w->lines = xalloc_array(scrollback_limit, sizeof *w->lines); window_insert(s, w); + monitor_query(w, '+'); return w; } @@ -391,6 +403,7 @@ 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); @@ -1122,6 +1135,9 @@ 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->inlen = 0; s->cap_state = CAP_NONE; @@ -1363,6 +1379,7 @@ 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; @@ -1408,6 +1425,13 @@ 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; } @@ -1423,7 +1447,10 @@ static void handle_line(Server *s, char *line) if (!silent) append_nickf(w, "* ", nick, " ", "is now known as %s", arg[0]); in_channel = 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; @@ -1492,7 +1519,12 @@ static void handle_line(Server *s, char *line) if (strcmp(cmd, "005") == 0) { for (int i = 1; i < narg; i++) { - if (strncmp(arg[i], "PREFIX=(", 8) == 0) { + 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) { const char *modes = arg[i] + 8; const char *end = strchr(modes, ')'); if (end && (size_t)(end - modes) == strlen(end + 1) && @@ -1509,6 +1541,22 @@ 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; @@ -1592,7 +1640,13 @@ static void handle_line(Server *s, char *line) return; } if (strcmp(cmd, "401") == 0) { - if (narg >= 3) appendf(reply_window(s), "* %s: %s", arg[1], arg[2]); + 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]); + } s->reply_win = NULL; return; }