AmendHub

Download:

jcs

/

wallops

/

amendments

/

127

irc: Do more comprehensive nick-highlight checking

Highlight if our nick is later in a line, but only if it has a word
boundary with a leading space and a trailing punctuation, space, or
end-of-line. Also avoid having to strlen our nick every time and do
this at connection time and nick change time.
 
Handle printing unknown CTCP messages.
 
Also do better malloc failure checking.

jcs made amendment 127 20 days ago
--- irc.c Wed Sep 18 10:07:39 2024 +++ irc.c Fri Sep 20 21:08:45 2024 @@ -72,20 +72,38 @@ irc_connect(struct chatter *chatter, const char *serve panic("TCPInit failed (%d)", err); conn = xmalloczero(sizeof(struct irc_connection)); + if (conn == NULL) + goto conn_fail; SLIST_APPEND(&irc_connections_list, conn, irc_connection, list); SLIST_INIT(&conn->channels_list); conn->chatter = chatter; conn->state = IRC_STATE_DISCONNECTED; conn->hostname = xstrdup(server); + if (conn->hostname == NULL) + goto conn_fail; conn->port = port; - if (password && password[0]) + if (password && password[0]) { conn->password = xstrdup(password); + if (conn->password == NULL) + goto conn_fail; + } + + conn->nick_len = strlen(nick); conn->nick = xstrdup(nick); + if (conn->nick == NULL) + goto conn_fail; conn->ident = xstrdup(ident); + if (conn->ident == NULL) + goto conn_fail; conn->realname = xstrdup(realname); + if (conn->realname == NULL) + goto conn_fail; conn->hide_motd = hide_motd; - if (channel && channel[0]) + if (channel && channel[0]) { conn->channel_autojoin = xstrdup(channel); + if (conn->channel_autojoin == NULL) + goto conn_fail; + } chatter_printf(conn->chatter, conn, NULL, "$B***$0 Connecting to $B%s$0 port $B%d$0...", conn->hostname, @@ -123,6 +141,12 @@ irc_connect(struct chatter *chatter, const char *serve conn->state = IRC_STATE_UNREGISTERED; return conn; + +conn_fail: + warn("Out of memory for new connection"); + if (conn != NULL) + irc_dealloc_connection(conn); + return NULL; } void @@ -521,7 +545,11 @@ irc_process_server(struct irc_connection *conn) gestalt_machine_type()); } } else { - goto unknown; + chatter_printf(conn->chatter, conn, msg.arg[0], + "$B*** %s ($0%s@%s$B)$0 requested unknown CTCP " + "$B%s$0 from $B%s$0", + user->nick, user->username, user->hostname, + msg.arg[0], msg.msg); } } else if (strcmp(msg.arg[0], conn->nick) == 0) { /* message to us */ @@ -538,11 +566,24 @@ irc_process_server(struct irc_connection *conn) notify(); } else { /* message to channel we're in */ - size = strlen(conn->nick); - if (strncmp(msg.msg, conn->nick, size) == 0 && - (msg.msg[size] == ' ' || msg.msg[size] == ':' || - msg.msg[size] == ',' || msg.msg[size] == '\0')) { - /* highlight message directed to us */ + char *us; + + us = strcasestr(msg.msg, conn->nick); + if (us == msg.msg || us[-1] == ' ') { + if (us[conn->nick_len] == ' ' || + us[conn->nick_len] == ':' || + us[conn->nick_len] == ',' || + us[conn->nick_len] == '!' || + us[conn->nick_len] == '?' || + us[conn->nick_len] == '\0') { + /* /^nick[ :,?!$]/ or / nick[ :,?!$]/ */ + } else + us = NULL; + } else + us = NULL; + + if (us) { + /* mention to us, highlight */ chatter_printf(conn->chatter, conn, msg.arg[0], "<$U%s$0>$/ %s", user->nick, msg.msg); @@ -1591,7 +1632,10 @@ irc_change_user_nick(struct irc_connection *conn, if (strcasecmp(conn->nick, user->nick) == 0) { xfree(&conn->nick); + conn->nick_len = strlen(nick); conn->nick = xstrdup(nick); + if (conn->nick == NULL) + panic("Out of memory changing nick"); chatter_update_titlebar(conn->chatter); } } --- irc.h Tue Sep 17 15:18:17 2024 +++ irc.h Fri Sep 20 20:07:53 2024 @@ -76,6 +76,7 @@ struct irc_connection { short port; char *password; char *nick; + short nick_len; char *ident; char *realname; char *channel_autojoin;