Ignore ~ username prefixes

This simplifies a lot.
weechat-hashes
Curtis McEnroe 2018-12-03 00:08:24 -05:00
parent 452aeb5ecf
commit 26db6e6c10
No known key found for this signature in database
GPG Key ID: CEA2F97ADCFCD77C
4 changed files with 59 additions and 71 deletions

37
chat.c
View File

@ -25,17 +25,10 @@
#include "chat.h" #include "chat.h"
static void freedup(char **field, const char *str) { static char *dupe(const char *str) {
free(*field); char *dup = strdup(str);
*field = strdup(str); if (!dup) err(EX_OSERR, "strdup");
if (!*field) err(EX_OSERR, "strdup"); return dup;
}
void selfNick(const char *nick) {
freedup(&self.nick, nick);
}
void selfUser(const char *user) {
freedup(&self.user, user);
} }
static char *prompt(const char *prompt) { static char *prompt(const char *prompt) {
@ -60,25 +53,25 @@ int main(int argc, char *argv[]) {
while (0 < (opt = getopt(argc, argv, "NW:h:j:l:n:p:r:u:vw:"))) { while (0 < (opt = getopt(argc, argv, "NW:h:j:l:n:p:r:u:vw:"))) {
switch (opt) { switch (opt) {
break; case 'N': self.notify = true; break; case 'N': self.notify = true;
break; case 'W': freedup(&self.webp, optarg); break; case 'W': self.webp = dupe(optarg);
break; case 'h': freedup(&self.host, optarg); break; case 'h': self.host = dupe(optarg);
break; case 'j': freedup(&self.join, optarg); break; case 'j': self.join = dupe(optarg);
break; case 'l': logOpen(optarg); break; case 'l': logOpen(optarg);
break; case 'n': freedup(&self.nick, optarg); break; case 'n': self.nick = dupe(optarg);
break; case 'p': freedup(&self.port, optarg); break; case 'p': self.port = dupe(optarg);
break; case 'r': freedup(&self.real, optarg); break; case 'r': self.real = dupe(optarg);
break; case 'u': freedup(&self.user, optarg); break; case 'u': self.user = dupe(optarg);
break; case 'v': self.verbose = true; break; case 'v': self.verbose = true;
break; case 'w': freedup(&self.pass, optarg); break; case 'w': self.pass = dupe(optarg);
break; default: return EX_USAGE; break; default: return EX_USAGE;
} }
} }
if (!self.host) self.host = prompt("Host: "); if (!self.host) self.host = prompt("Host: ");
if (!self.port) freedup(&self.port, "6697"); if (!self.port) self.port = dupe("6697");
if (!self.nick) self.nick = prompt("Name: "); if (!self.nick) self.nick = prompt("Name: ");
if (!self.user) freedup(&self.user, self.nick); if (!self.user) self.user = dupe(self.nick);
if (!self.real) freedup(&self.real, self.nick); if (!self.real) self.real = dupe(self.nick);
inputTab(); inputTab();
uiInit(); uiInit();

3
chat.h
View File

@ -43,9 +43,6 @@ struct {
bool notify; bool notify;
} self; } self;
void selfNick(const char *nick);
void selfUser(const char *user);
void eventWait(const char *argv[static 2]); void eventWait(const char *argv[static 2]);
void eventPipe(const char *argv[static 2]); void eventPipe(const char *argv[static 2]);
void eventQuit(void); void eventQuit(void);

View File

@ -34,7 +34,7 @@ static char *paramField(char **params) {
return strsep(params, " "); return strsep(params, " ");
} }
static void shift( static void parse(
char *prefix, char **nick, char **user, char **host, char *prefix, char **nick, char **user, char **host,
char *params, size_t req, size_t opt, /* (char **) */ ... char *params, size_t req, size_t opt, /* (char **) */ ...
) { ) {
@ -43,7 +43,7 @@ static void shift(
field = strsep(&prefix, "!"); field = strsep(&prefix, "!");
if (nick) *nick = field; if (nick) *nick = field;
field = strsep(&prefix, "@"); field = strsep(&prefix, "@");
if (user) *user = field; if (user) *user = (field && field[0] == '~' ? &field[1] : field);
if (host) *host = prefix; if (host) *host = prefix;
} }
@ -66,13 +66,9 @@ static void shift(
va_end(ap); va_end(ap);
} }
static bool isSelf(const char *nick, const char *user) { static bool isSelf(const char *user) {
if (!user) return false; if (!user) return false;
if (!strcmp(nick, self.nick)) { return !strcmp(user, self.user);
if (strcmp(user, self.user)) selfUser(user);
return true;
}
return false;
} }
static bool isPing(const char *mesg) { static bool isPing(const char *mesg) {
@ -105,16 +101,20 @@ static void handlePing(char *prefix, char *params) {
static void handleErrorErroneousNickname(char *prefix, char *params) { static void handleErrorErroneousNickname(char *prefix, char *params) {
char *mesg; char *mesg;
shift(prefix, NULL, NULL, NULL, params, 3, 0, NULL, NULL, &mesg); parse(prefix, NULL, NULL, NULL, params, 3, 0, NULL, NULL, &mesg);
uiFmt(TagStatus, UIHot, "You can't use that name here: \"%s\"", mesg); uiFmt(TagStatus, UIHot, "You can't use that name here: \"%s\"", mesg);
uiLog(TagStatus, UICold, L"Type /nick <name> to choose a new one"); uiLog(TagStatus, UICold, L"Type /nick <name> to choose a new one");
} }
static void handleReplyWelcome(char *prefix, char *params) { static void handleReplyWelcome(char *prefix, char *params) {
char *nick; char *nick;
shift(prefix, NULL, NULL, NULL, params, 1, 0, &nick); parse(prefix, NULL, NULL, NULL, params, 1, 0, &nick);
if (strcmp(nick, self.nick)) selfNick(nick); if (strcmp(nick, self.nick)) {
free(self.nick);
self.nick = strdup(nick);
if (!self.nick) err(EX_OSERR, "strdup");
}
if (self.join) ircFmt("JOIN %s\r\n", self.join); if (self.join) ircFmt("JOIN %s\r\n", self.join);
tabTouch(TagStatus, self.nick); tabTouch(TagStatus, self.nick);
@ -123,7 +123,7 @@ static void handleReplyWelcome(char *prefix, char *params) {
static void handleReplyMOTD(char *prefix, char *params) { static void handleReplyMOTD(char *prefix, char *params) {
char *mesg; char *mesg;
shift(prefix, NULL, NULL, NULL, params, 2, 0, NULL, &mesg); parse(prefix, NULL, NULL, NULL, params, 2, 0, NULL, &mesg);
if (mesg[0] == '-' && mesg[1] == ' ') mesg = &mesg[2]; if (mesg[0] == '-' && mesg[1] == ' ') mesg = &mesg[2];
urlScan(TagStatus, mesg); urlScan(TagStatus, mesg);
@ -133,11 +133,11 @@ static void handleReplyMOTD(char *prefix, char *params) {
static enum IRCColor whoisColor; static enum IRCColor whoisColor;
static void handleReplyWhoisUser(char *prefix, char *params) { static void handleReplyWhoisUser(char *prefix, char *params) {
char *nick, *user, *host, *real; char *nick, *user, *host, *real;
shift( parse(
prefix, NULL, NULL, NULL, prefix, NULL, NULL, NULL,
params, 6, 0, NULL, &nick, &user, &host, NULL, &real params, 6, 0, NULL, &nick, &user, &host, NULL, &real
); );
whoisColor = formatColor(user); whoisColor = formatColor(user[0] == '~' ? &user[1] : user);
uiFmt( uiFmt(
TagStatus, UIWarm, TagStatus, UIWarm,
"\3%d%s\3 is %s@%s, \"%s\"", "\3%d%s\3 is %s@%s, \"%s\"",
@ -147,7 +147,7 @@ static void handleReplyWhoisUser(char *prefix, char *params) {
static void handleReplyWhoisServer(char *prefix, char *params) { static void handleReplyWhoisServer(char *prefix, char *params) {
char *nick, *serv, *info; char *nick, *serv, *info;
shift(prefix, NULL, NULL, NULL, params, 4, 0, NULL, &nick, &serv, &info); parse(prefix, NULL, NULL, NULL, params, 4, 0, NULL, &nick, &serv, &info);
uiFmt( uiFmt(
TagStatus, UIWarm, TagStatus, UIWarm,
"\3%d%s\3 is connected to %s, \"%s\"", "\3%d%s\3 is connected to %s, \"%s\"",
@ -157,13 +157,13 @@ static void handleReplyWhoisServer(char *prefix, char *params) {
static void handleReplyWhoisOperator(char *prefix, char *params) { static void handleReplyWhoisOperator(char *prefix, char *params) {
char *nick, *oper; char *nick, *oper;
shift(prefix, NULL, NULL, NULL, params, 3, 0, NULL, &nick, &oper); parse(prefix, NULL, NULL, NULL, params, 3, 0, NULL, &nick, &oper);
uiFmt(TagStatus, UIWarm, "\3%d%s\3 %s", whoisColor, nick, oper); uiFmt(TagStatus, UIWarm, "\3%d%s\3 %s", whoisColor, nick, oper);
} }
static void handleReplyWhoisIdle(char *prefix, char *params) { static void handleReplyWhoisIdle(char *prefix, char *params) {
char *nick, *idle, *sign; char *nick, *idle, *sign;
shift(prefix, NULL, NULL, NULL, params, 4, 0, NULL, &nick, &idle, &sign); parse(prefix, NULL, NULL, NULL, params, 4, 0, NULL, &nick, &idle, &sign);
time_t time = strtoul(sign, NULL, 10); time_t time = strtoul(sign, NULL, 10);
const char *at = ctime(&time); const char *at = ctime(&time);
unsigned long secs = strtoul(idle, NULL, 10); unsigned long secs = strtoul(idle, NULL, 10);
@ -178,22 +178,22 @@ static void handleReplyWhoisIdle(char *prefix, char *params) {
static void handleReplyWhoisChannels(char *prefix, char *params) { static void handleReplyWhoisChannels(char *prefix, char *params) {
char *nick, *chans; char *nick, *chans;
shift(prefix, NULL, NULL, NULL, params, 3, 0, NULL, &nick, &chans); parse(prefix, NULL, NULL, NULL, params, 3, 0, NULL, &nick, &chans);
uiFmt(TagStatus, UIWarm, "\3%d%s\3 is in %s", whoisColor, nick, chans); uiFmt(TagStatus, UIWarm, "\3%d%s\3 is in %s", whoisColor, nick, chans);
} }
static void handleErrorNoSuchNick(char *prefix, char *params) { static void handleErrorNoSuchNick(char *prefix, char *params) {
char *nick, *mesg; char *nick, *mesg;
shift(prefix, NULL, NULL, NULL, params, 3, 0, NULL, &nick, &mesg); parse(prefix, NULL, NULL, NULL, params, 3, 0, NULL, &nick, &mesg);
uiFmt(TagStatus, UIWarm, "%s, \"%s\"", mesg, nick); uiFmt(TagStatus, UIWarm, "%s, \"%s\"", mesg, nick);
} }
static void handleJoin(char *prefix, char *params) { static void handleJoin(char *prefix, char *params) {
char *nick, *user, *chan; char *nick, *user, *chan;
shift(prefix, &nick, &user, NULL, params, 1, 0, &chan); parse(prefix, &nick, &user, NULL, params, 1, 0, &chan);
struct Tag tag = tagFor(chan); struct Tag tag = tagFor(chan);
if (isSelf(nick, user)) { if (isSelf(user)) {
tabTouch(TagNone, chan); tabTouch(TagNone, chan);
uiViewTag(tag); uiViewTag(tag);
logReplay(tag); logReplay(tag);
@ -210,10 +210,10 @@ static void handleJoin(char *prefix, char *params) {
static void handlePart(char *prefix, char *params) { static void handlePart(char *prefix, char *params) {
char *nick, *user, *chan, *mesg; char *nick, *user, *chan, *mesg;
shift(prefix, &nick, &user, NULL, params, 1, 1, &chan, &mesg); parse(prefix, &nick, &user, NULL, params, 1, 1, &chan, &mesg);
struct Tag tag = tagFor(chan); struct Tag tag = tagFor(chan);
if (isSelf(nick, user)) { if (isSelf(user)) {
tabClear(tag); tabClear(tag);
} else { } else {
tabRemove(tag, nick); tabRemove(tag, nick);
@ -239,7 +239,7 @@ static void handlePart(char *prefix, char *params) {
static void handleKick(char *prefix, char *params) { static void handleKick(char *prefix, char *params) {
char *nick, *user, *chan, *kick, *mesg; char *nick, *user, *chan, *kick, *mesg;
shift(prefix, &nick, &user, NULL, params, 2, 1, &chan, &kick, &mesg); parse(prefix, &nick, &user, NULL, params, 2, 1, &chan, &kick, &mesg);
struct Tag tag = tagFor(chan); struct Tag tag = tagFor(chan);
bool kicked = !strcmp(kick, self.nick); bool kicked = !strcmp(kick, self.nick);
@ -277,7 +277,7 @@ static void handleKick(char *prefix, char *params) {
static void handleQuit(char *prefix, char *params) { static void handleQuit(char *prefix, char *params) {
char *nick, *user, *mesg; char *nick, *user, *mesg;
shift(prefix, &nick, &user, NULL, params, 0, 1, &mesg); parse(prefix, &nick, &user, NULL, params, 0, 1, &mesg);
struct Tag tag; struct Tag tag;
while (TagNone.id != (tag = tabTag(nick)).id) { while (TagNone.id != (tag = tabTag(nick)).id) {
@ -300,7 +300,7 @@ static void handleQuit(char *prefix, char *params) {
static void handleReplyTopic(char *prefix, char *params) { static void handleReplyTopic(char *prefix, char *params) {
char *chan, *topic; char *chan, *topic;
shift(prefix, NULL, NULL, NULL, params, 3, 0, NULL, &chan, &topic); parse(prefix, NULL, NULL, NULL, params, 3, 0, NULL, &chan, &topic);
struct Tag tag = tagFor(chan); struct Tag tag = tagFor(chan);
urlScan(tag, topic); urlScan(tag, topic);
@ -314,10 +314,10 @@ static void handleReplyTopic(char *prefix, char *params) {
static void handleTopic(char *prefix, char *params) { static void handleTopic(char *prefix, char *params) {
char *nick, *user, *chan, *topic; char *nick, *user, *chan, *topic;
shift(prefix, &nick, &user, NULL, params, 2, 0, &chan, &topic); parse(prefix, &nick, &user, NULL, params, 2, 0, &chan, &topic);
struct Tag tag = tagFor(chan); struct Tag tag = tagFor(chan);
if (!isSelf(nick, user)) tabTouch(tag, nick); if (!isSelf(user)) tabTouch(tag, nick);
urlScan(tag, topic); urlScan(tag, topic);
uiFmt( uiFmt(
@ -330,7 +330,7 @@ static void handleTopic(char *prefix, char *params) {
static void handleReplyEndOfNames(char *prefix, char *params) { static void handleReplyEndOfNames(char *prefix, char *params) {
char *chan; char *chan;
shift(prefix, NULL, NULL, NULL, params, 2, 0, NULL, &chan); parse(prefix, NULL, NULL, NULL, params, 2, 0, NULL, &chan);
ircFmt("WHO %s\r\n", chan); ircFmt("WHO %s\r\n", chan);
} }
@ -341,10 +341,11 @@ static struct {
static void handleReplyWho(char *prefix, char *params) { static void handleReplyWho(char *prefix, char *params) {
char *chan, *user, *nick; char *chan, *user, *nick;
shift( parse(
prefix, NULL, NULL, NULL, prefix, NULL, NULL, NULL,
params, 6, 0, NULL, &chan, &user, NULL, NULL, &nick params, 6, 0, NULL, &chan, &user, NULL, NULL, &nick
); );
if (user[0] == '~') user = &user[1];
struct Tag tag = tagFor(chan); struct Tag tag = tagFor(chan);
tabAdd(tag, nick); tabAdd(tag, nick);
@ -360,7 +361,7 @@ static void handleReplyWho(char *prefix, char *params) {
static void handleReplyEndOfWho(char *prefix, char *params) { static void handleReplyEndOfWho(char *prefix, char *params) {
char *chan; char *chan;
shift(prefix, NULL, NULL, NULL, params, 2, 0, NULL, &chan); parse(prefix, NULL, NULL, NULL, params, 2, 0, NULL, &chan);
struct Tag tag = tagFor(chan); struct Tag tag = tagFor(chan);
uiFmt( uiFmt(
@ -373,10 +374,12 @@ static void handleReplyEndOfWho(char *prefix, char *params) {
static void handleNick(char *prefix, char *params) { static void handleNick(char *prefix, char *params) {
char *prev, *user, *next; char *prev, *user, *next;
shift(prefix, &prev, &user, NULL, params, 1, 0, &next); parse(prefix, &prev, &user, NULL, params, 1, 0, &next);
if (isSelf(prev, user)) { if (isSelf(user)) {
selfNick(next); free(self.nick);
self.nick = strdup(next);
if (!self.nick) err(EX_OSERR, "strdup");
uiPrompt(); uiPrompt();
} }
@ -399,11 +402,10 @@ static void handleCTCP(struct Tag tag, char *nick, char *user, char *mesg) {
char *params = strsep(&mesg, "\1"); char *params = strsep(&mesg, "\1");
if (strcmp(ctcp, "ACTION")) return; if (strcmp(ctcp, "ACTION")) return;
bool self = isSelf(nick, user); if (!isSelf(user)) tabTouch(tag, nick);
if (!self) tabTouch(tag, nick);
urlScan(tag, params); urlScan(tag, params);
bool ping = !self && isPing(params); bool ping = !isSelf(user) && isPing(params);
uiFmt( uiFmt(
tag, (ping ? UIHot : UIWarm), tag, (ping ? UIHot : UIWarm),
"%c\3%d* %s\17 %s", "%c\3%d* %s\17 %s",
@ -414,7 +416,7 @@ static void handleCTCP(struct Tag tag, char *nick, char *user, char *mesg) {
static void handlePrivmsg(char *prefix, char *params) { static void handlePrivmsg(char *prefix, char *params) {
char *nick, *user, *chan, *mesg; char *nick, *user, *chan, *mesg;
shift(prefix, &nick, &user, NULL, params, 2, 0, &chan, &mesg); parse(prefix, &nick, &user, NULL, params, 2, 0, &chan, &mesg);
bool direct = !strcmp(chan, self.nick); bool direct = !strcmp(chan, self.nick);
struct Tag tag = (direct ? tagFor(nick) : tagFor(chan)); struct Tag tag = (direct ? tagFor(nick) : tagFor(chan));
if (mesg[0] == '\1') { if (mesg[0] == '\1') {
@ -422,7 +424,7 @@ static void handlePrivmsg(char *prefix, char *params) {
return; return;
} }
bool self = isSelf(nick, user); bool self = isSelf(user);
if (!self) tabTouch(tag, nick); if (!self) tabTouch(tag, nick);
urlScan(tag, mesg); urlScan(tag, mesg);
@ -438,15 +440,14 @@ static void handlePrivmsg(char *prefix, char *params) {
static void handleNotice(char *prefix, char *params) { static void handleNotice(char *prefix, char *params) {
char *nick, *user, *chan, *mesg; char *nick, *user, *chan, *mesg;
shift(prefix, &nick, &user, NULL, params, 2, 0, &chan, &mesg); parse(prefix, &nick, &user, NULL, params, 2, 0, &chan, &mesg);
struct Tag tag = TagStatus; struct Tag tag = TagStatus;
if (user) tag = (strcmp(chan, self.nick) ? tagFor(chan) : tagFor(nick)); if (user) tag = (strcmp(chan, self.nick) ? tagFor(chan) : tagFor(nick));
bool self = isSelf(nick, user); if (!isSelf(user)) tabTouch(tag, nick);
if (!self) tabTouch(tag, nick);
urlScan(tag, mesg); urlScan(tag, mesg);
bool ping = !self && isPing(mesg); bool ping = !isSelf(user) && isPing(mesg);
uiFmt( uiFmt(
tag, (ping ? UIHot : UIWarm), tag, (ping ? UIHot : UIWarm),
"%c\3%d-%s-\17 %s", "%c\3%d-%s-\17 %s",

3
irc.c
View File

@ -95,9 +95,6 @@ int ircConnect(void) {
); );
} }
/// FIXME
if (self.user[0] == '~') selfUser(&self.user[1]);
if (self.pass) ircFmt("PASS :%s\r\n", self.pass); if (self.pass) ircFmt("PASS :%s\r\n", self.pass);
ircFmt("NICK %s\r\n", self.nick); ircFmt("NICK %s\r\n", self.nick);
ircFmt("USER %s 0 * :%s\r\n", self.user, self.real); ircFmt("USER %s 0 * :%s\r\n", self.user, self.real);