Allow setting fallback nicks and highlight on any

As a side-effect, even with only one nick set you'll still be
highlighted by it even if your current nick is different.
weechat-hashes
June McEnroe 2022-05-29 18:09:52 -04:00
parent a5162d83bd
commit 3475f03ec8
4 changed files with 39 additions and 17 deletions

View File

@ -312,12 +312,17 @@ Log chat events to files in paths
Set the user Set the user
.Ar mode . .Ar mode .
. .
.It Fl n Ar nick | Cm nick No = Ar nick .It Fl n Ar nick Oo Ar ... Oc | Cm nick No = Ar nick Oo Ar ... Oc
Set nickname to Set nickname to
.Ar nick . .Ar nick .
The default nickname is The default nickname is
the value of the environment variable the value of the environment variable
.Ev USER . .Ev USER .
Additional space-separated nicks
will be tried in order
if the first is not available,
and all nicks
are treated as highlight words.
. .
.It Fl o .It Fl o
Print the server certificate chain Print the server certificate chain

17
chat.c
View File

@ -237,7 +237,6 @@ int main(int argc, char *argv[]) {
bool log = false; bool log = false;
bool sasl = false; bool sasl = false;
char *pass = NULL; char *pass = NULL;
const char *nick = NULL;
const char *user = NULL; const char *user = NULL;
const char *real = NULL; const char *real = NULL;
@ -306,7 +305,11 @@ int main(int argc, char *argv[]) {
break; case 'k': priv = optarg; break; case 'k': priv = optarg;
break; case 'l': log = true; logOpen(); break; case 'l': log = true; logOpen();
break; case 'm': self.mode = optarg; break; case 'm': self.mode = optarg;
break; case 'n': nick = optarg; break; case 'n': {
for (uint i = 0; i < ARRAY_LEN(self.nicks); ++i) {
self.nicks[i] = strsep(&optarg, " ");
}
}
break; case 'o': printCert = true; break; case 'o': printCert = true;
break; case 'p': port = optarg; break; case 'p': port = optarg;
break; case 'q': windowThreshold = Warm; break; case 'q': windowThreshold = Warm;
@ -333,10 +336,10 @@ int main(int argc, char *argv[]) {
return EX_OK; return EX_OK;
} }
if (!nick) nick = getenv("USER"); if (!self.nicks[0]) self.nicks[0] = getenv("USER");
if (!nick) errx(EX_CONFIG, "USER unset"); if (!self.nicks[0]) errx(EX_CONFIG, "USER unset");
if (!user) user = nick; if (!user) user = self.nicks[0];
if (!real) real = nick; if (!real) real = self.nicks[0];
if (self.kiosk) { if (self.kiosk) {
char *hash; char *hash;
@ -404,7 +407,7 @@ int main(int argc, char *argv[]) {
} }
if (sasl) ircFormat("CAP REQ :sasl\r\n"); if (sasl) ircFormat("CAP REQ :sasl\r\n");
ircFormat("CAP LS\r\n"); ircFormat("CAP LS\r\n");
ircFormat("NICK :%s\r\n", nick); ircFormat("NICK %s\r\n", self.nicks[0]);
ircFormat("USER %s 0 * :%s\r\n", user, real); ircFormat("USER %s 0 * :%s\r\n", user, real);
// Avoid disabling VINTR until main loop. // Avoid disabling VINTR until main loop.

7
chat.h
View File

@ -193,10 +193,11 @@ extern struct Self {
bool restricted; bool restricted;
size_t pos; size_t pos;
enum Cap caps; enum Cap caps;
char *plainUser; const char *plainUser;
char *plainPass; char *plainPass;
char *mode; const char *nicks[8];
char *join; const char *mode;
const char *join;
char *nick; char *nick;
char *user; char *user;
char *host; char *host;

View File

@ -148,7 +148,12 @@ static void handleReplyGeneric(struct Message *msg) {
static void handleErrorNicknameInUse(struct Message *msg) { static void handleErrorNicknameInUse(struct Message *msg) {
require(msg, false, 2); require(msg, false, 2);
if (!strcmp(self.nick, "*")) { if (!strcmp(self.nick, "*")) {
ircFormat("NICK :%s_\r\n", msg->params[1]); static uint i = 1;
if (i < ARRAY_LEN(self.nicks) && self.nicks[i]) {
ircFormat("NICK %s\r\n", self.nicks[i++]);
} else {
ircFormat("NICK %s_\r\n", msg->params[1]);
}
} else { } else {
handleErrorGeneric(msg); handleErrorGeneric(msg);
} }
@ -1207,11 +1212,11 @@ static bool isAction(struct Message *msg) {
return true; return true;
} }
static bool isMention(const struct Message *msg) { static bool matchWord(const char *str, const char *word) {
size_t len = strlen(self.nick); size_t len = strlen(word);
const char *match = msg->params[1]; const char *match = str;
while (NULL != (match = strstr(match, self.nick))) { while (NULL != (match = strstr(match, word))) {
char a = (match > msg->params[1] ? match[-1] : ' '); char a = (match > str ? match[-1] : ' ');
char b = (match[len] ?: ' '); char b = (match[len] ?: ' ');
if ((isspace(a) || ispunct(a)) && (isspace(b) || ispunct(b))) { if ((isspace(a) || ispunct(a)) && (isspace(b) || ispunct(b))) {
return true; return true;
@ -1221,6 +1226,14 @@ static bool isMention(const struct Message *msg) {
return false; return false;
} }
static bool isMention(const struct Message *msg) {
if (matchWord(msg->params[1], self.nick)) return true;
for (uint i = 0; i < ARRAY_LEN(self.nicks) && self.nicks[i]; ++i) {
if (matchWord(msg->params[1], self.nicks[i])) return true;
}
return false;
}
static void colorMentions(char *buf, size_t cap, uint id, struct Message *msg) { static void colorMentions(char *buf, size_t cap, uint id, struct Message *msg) {
*buf = '\0'; *buf = '\0';