Assign nickname colors on activity
This commit is contained in:
parent
5de878113a
commit
e5a7eba0c6
1 changed files with 59 additions and 6 deletions
65
irc9.c
65
irc9.c
|
|
@ -115,6 +115,12 @@ typedef struct BlacklistedNick {
|
|||
struct BlacklistedNick *next;
|
||||
} BlacklistedNick;
|
||||
|
||||
typedef struct ActiveNick {
|
||||
char nick[NICKLEN];
|
||||
int color;
|
||||
struct ActiveNick *next;
|
||||
} ActiveNick;
|
||||
|
||||
typedef struct Server {
|
||||
char host[HOSTLEN];
|
||||
char *name;
|
||||
|
|
@ -141,6 +147,7 @@ typedef struct Server {
|
|||
const char *next_autocommand;
|
||||
struct timespec autocommand_at;
|
||||
BlacklistedNick *blacklist;
|
||||
ActiveNick *active_nicks;
|
||||
int cap_state;
|
||||
char caps[512]; /* accumulated multi-line CAP LS reply */
|
||||
char umode[32];
|
||||
|
|
@ -511,15 +518,53 @@ static void appendf(Window *w, const char *fmt, ...)
|
|||
win_append_raw(w, buf, 0, 0);
|
||||
}
|
||||
|
||||
/* Keep green reserved for our nick and assign others a stable palette color. */
|
||||
static ActiveNick **active_nick_slot(Server *s, const char *nick)
|
||||
{
|
||||
ActiveNick **slot = &s->active_nicks;
|
||||
while (*slot && strcasecmp((*slot)->nick, nick) != 0)
|
||||
slot = &(*slot)->next;
|
||||
return slot;
|
||||
}
|
||||
|
||||
static void activate_nick(Server *s, const char *nick)
|
||||
{
|
||||
if (!*nick || strcasecmp(s->nick, nick) == 0) return;
|
||||
ActiveNick **slot = active_nick_slot(s, nick);
|
||||
if (*slot) return;
|
||||
|
||||
static const int palette[] = {4, 6, 7, 10, 12};
|
||||
uint32_t hash = 0;
|
||||
for (const unsigned char *c = (const unsigned char *)nick; *c; c++)
|
||||
hash = hash * 33 + (unsigned char)tolower(*c);
|
||||
|
||||
ActiveNick *entry = xmalloc(sizeof *entry);
|
||||
snprintf(entry->nick, sizeof entry->nick, "%s", nick);
|
||||
entry->color = palette[hash % (sizeof palette / sizeof *palette)];
|
||||
entry->next = s->active_nicks;
|
||||
s->active_nicks = entry;
|
||||
}
|
||||
|
||||
static void rename_active_nick(Server *s, const char *oldnick, const char *newnick)
|
||||
{
|
||||
ActiveNick *entry = *active_nick_slot(s, oldnick);
|
||||
if (!entry || strcasecmp(oldnick, newnick) == 0) return;
|
||||
|
||||
ActiveNick **slot = active_nick_slot(s, newnick);
|
||||
if (*slot) {
|
||||
ActiveNick *previous = *slot;
|
||||
*slot = previous->next;
|
||||
free(previous);
|
||||
}
|
||||
snprintf(entry->nick, sizeof entry->nick, "%s", newnick);
|
||||
}
|
||||
|
||||
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)];
|
||||
for (const ActiveNick *entry = s->active_nicks; entry; entry = entry->next)
|
||||
if (strlen(entry->nick) == len && strncasecmp(entry->nick, nick, len) == 0)
|
||||
return entry->color;
|
||||
return 15;
|
||||
}
|
||||
|
||||
static void append_nickf(Window *w, const char *before, const char *identity,
|
||||
|
|
@ -1537,6 +1582,8 @@ static void handle_line(Server *s, char *line)
|
|||
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 && w->type != W_SERVER)
|
||||
activate_nick(s, nick);
|
||||
if (narg > 1 && handle_ctcp(s, nick, arg[1], w, notice)) return;
|
||||
mark_unread_message(w);
|
||||
if (notice) append_nickf(w, "-", *nick ? nick : "server", "- ", "%s", text);
|
||||
|
|
@ -1619,6 +1666,7 @@ static void handle_line(Server *s, char *line)
|
|||
renamed = w->next;
|
||||
window_insert(s, w);
|
||||
}
|
||||
rename_active_nick(s, nick, arg[0]);
|
||||
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]);
|
||||
|
|
@ -3549,6 +3597,11 @@ int main(int argc, char **argv)
|
|||
s->blacklist = entry->next;
|
||||
free(entry);
|
||||
}
|
||||
while (s->active_nicks) {
|
||||
ActiveNick *entry = s->active_nicks;
|
||||
s->active_nicks = entry->next;
|
||||
free(entry);
|
||||
}
|
||||
free(s);
|
||||
s = sn;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue