2019-07-03 02:12:07 +00:00
|
|
|
/* Copyright (C) 2018, 2019 Curtis McEnroe <june@causal.agency>
|
2018-08-04 17:35:29 +00:00
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2018-08-13 18:37:37 +00:00
|
|
|
#include <ctype.h>
|
2018-08-04 17:35:29 +00:00
|
|
|
#include <err.h>
|
2018-08-09 21:37:14 +00:00
|
|
|
#include <stdarg.h>
|
2018-08-04 17:35:29 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sysexits.h>
|
2018-11-29 10:37:10 +00:00
|
|
|
#include <time.h>
|
2018-08-04 17:35:29 +00:00
|
|
|
|
|
|
|
#include "chat.h"
|
|
|
|
|
2018-08-09 21:37:14 +00:00
|
|
|
static char *paramField(char **params) {
|
2018-08-04 17:35:29 +00:00
|
|
|
char *rest = *params;
|
|
|
|
if (rest[0] == ':') {
|
|
|
|
*params = NULL;
|
|
|
|
return &rest[1];
|
|
|
|
}
|
|
|
|
return strsep(params, " ");
|
|
|
|
}
|
|
|
|
|
2018-12-03 05:08:24 +00:00
|
|
|
static void parse(
|
2018-08-09 21:37:14 +00:00
|
|
|
char *prefix, char **nick, char **user, char **host,
|
|
|
|
char *params, size_t req, size_t opt, /* (char **) */ ...
|
|
|
|
) {
|
|
|
|
char *field;
|
|
|
|
if (prefix) {
|
|
|
|
field = strsep(&prefix, "!");
|
|
|
|
if (nick) *nick = field;
|
|
|
|
field = strsep(&prefix, "@");
|
2019-07-03 01:41:52 +00:00
|
|
|
if (user) *user = field;
|
2018-08-09 21:37:14 +00:00
|
|
|
if (host) *host = prefix;
|
|
|
|
}
|
|
|
|
|
|
|
|
va_list ap;
|
|
|
|
va_start(ap, opt);
|
|
|
|
for (size_t i = 0; i < req; ++i) {
|
2018-08-10 16:45:37 +00:00
|
|
|
if (!params) errx(EX_PROTOCOL, "%zu params required, found %zu", req, i);
|
2018-08-09 21:37:14 +00:00
|
|
|
field = paramField(¶ms);
|
|
|
|
char **param = va_arg(ap, char **);
|
|
|
|
if (param) *param = field;
|
|
|
|
}
|
|
|
|
for (size_t i = 0; i < opt; ++i) {
|
|
|
|
char **param = va_arg(ap, char **);
|
|
|
|
if (params) {
|
|
|
|
*param = paramField(¶ms);
|
|
|
|
} else {
|
|
|
|
*param = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
va_end(ap);
|
|
|
|
}
|
|
|
|
|
2018-08-17 18:00:08 +00:00
|
|
|
static bool isPing(const char *mesg) {
|
2018-08-13 18:37:37 +00:00
|
|
|
size_t len = strlen(self.nick);
|
|
|
|
const char *match = mesg;
|
|
|
|
while (NULL != (match = strcasestr(match, self.nick))) {
|
|
|
|
char b = (match > mesg ? *(match - 1) : ' ');
|
|
|
|
char a = (match[len] ? match[len] : ' ');
|
|
|
|
match = &match[len];
|
|
|
|
if (!isspace(b) && !ispunct(b)) continue;
|
|
|
|
if (!isspace(a) && !ispunct(a)) continue;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-08-17 18:00:08 +00:00
|
|
|
static char *dequote(char *mesg) {
|
|
|
|
if (mesg[0] == '"') mesg = &mesg[1];
|
|
|
|
size_t len = strlen(mesg);
|
|
|
|
if (mesg[len - 1] == '"') mesg[len - 1] = '\0';
|
|
|
|
return mesg;
|
|
|
|
}
|
|
|
|
|
2018-12-30 06:53:30 +00:00
|
|
|
typedef void Handler(char *prefix, char *params);
|
2018-08-09 21:37:14 +00:00
|
|
|
|
2018-08-04 17:35:29 +00:00
|
|
|
static void handlePing(char *prefix, char *params) {
|
|
|
|
(void)prefix;
|
2018-08-04 21:54:46 +00:00
|
|
|
ircFmt("PONG %s\r\n", params);
|
2018-08-04 17:35:29 +00:00
|
|
|
}
|
|
|
|
|
2018-12-04 19:58:14 +00:00
|
|
|
static void handleError(char *prefix, char *params) {
|
2018-12-04 20:56:10 +00:00
|
|
|
char *mesg;
|
|
|
|
parse(prefix, NULL, NULL, NULL, params, 1, 0, &mesg);
|
|
|
|
if (self.quit) {
|
2019-02-22 19:11:50 +00:00
|
|
|
uiExit(EX_OK);
|
2018-12-04 20:56:10 +00:00
|
|
|
} else {
|
|
|
|
errx(EX_PROTOCOL, "%s", mesg);
|
|
|
|
}
|
2018-12-04 19:58:14 +00:00
|
|
|
}
|
|
|
|
|
2018-12-05 03:13:44 +00:00
|
|
|
static void handleCap(char *prefix, char *params) {
|
|
|
|
char *subc, *list;
|
|
|
|
parse(prefix, NULL, NULL, NULL, params, 3, 0, NULL, &subc, &list);
|
|
|
|
if (!strcmp(subc, "ACK") && self.auth) {
|
|
|
|
size_t len = strlen(self.auth);
|
|
|
|
byte plain[1 + len];
|
|
|
|
plain[0] = 0;
|
|
|
|
for (size_t i = 0; i < len; ++i) {
|
|
|
|
plain[1 + i] = (self.auth[i] == ':' ? 0 : self.auth[i]);
|
|
|
|
}
|
2018-12-16 21:21:58 +00:00
|
|
|
char b64[base64Size(sizeof(plain))];
|
|
|
|
base64(b64, plain, sizeof(plain));
|
2018-12-05 03:13:44 +00:00
|
|
|
ircFmt("AUTHENTICATE PLAIN\r\n");
|
|
|
|
ircFmt("AUTHENTICATE %s\r\n", b64);
|
|
|
|
}
|
|
|
|
ircFmt("CAP END\r\n");
|
|
|
|
}
|
|
|
|
|
2018-11-29 10:52:27 +00:00
|
|
|
static void handleErrorErroneousNickname(char *prefix, char *params) {
|
2018-08-09 21:37:14 +00:00
|
|
|
char *mesg;
|
2018-12-03 05:08:24 +00:00
|
|
|
parse(prefix, NULL, NULL, NULL, params, 3, 0, NULL, NULL, &mesg);
|
2018-09-02 20:13:00 +00:00
|
|
|
uiFmt(TagStatus, UIHot, "You can't use that name here: \"%s\"", mesg);
|
|
|
|
uiLog(TagStatus, UICold, L"Type /nick <name> to choose a new one");
|
2018-08-04 17:35:29 +00:00
|
|
|
}
|
|
|
|
|
2018-08-11 23:30:30 +00:00
|
|
|
static void handleReplyWelcome(char *prefix, char *params) {
|
2018-08-09 21:37:14 +00:00
|
|
|
char *nick;
|
2018-12-03 05:08:24 +00:00
|
|
|
parse(prefix, NULL, NULL, NULL, params, 1, 0, &nick);
|
2018-08-17 18:00:08 +00:00
|
|
|
|
2018-12-03 05:08:24 +00:00
|
|
|
if (strcmp(nick, self.nick)) {
|
|
|
|
free(self.nick);
|
|
|
|
self.nick = strdup(nick);
|
|
|
|
if (!self.nick) err(EX_OSERR, "strdup");
|
|
|
|
}
|
2019-07-02 22:40:06 +00:00
|
|
|
if (self.join && self.keys) {
|
|
|
|
ircFmt("JOIN %s %s\r\n", self.join, self.keys);
|
|
|
|
} else if (self.join) {
|
|
|
|
ircFmt("JOIN %s\r\n", self.join);
|
|
|
|
}
|
2018-09-02 20:13:00 +00:00
|
|
|
tabTouch(TagStatus, self.nick);
|
2018-08-17 18:00:08 +00:00
|
|
|
|
2018-12-03 03:56:33 +00:00
|
|
|
uiLog(TagStatus, UICold, L"You have arrived");
|
2018-08-11 03:31:20 +00:00
|
|
|
}
|
|
|
|
|
2018-08-11 23:30:30 +00:00
|
|
|
static void handleReplyMOTD(char *prefix, char *params) {
|
2018-08-11 03:31:20 +00:00
|
|
|
char *mesg;
|
2018-12-03 05:08:24 +00:00
|
|
|
parse(prefix, NULL, NULL, NULL, params, 2, 0, NULL, &mesg);
|
2018-08-11 03:31:20 +00:00
|
|
|
if (mesg[0] == '-' && mesg[1] == ' ') mesg = &mesg[2];
|
2018-08-17 18:00:08 +00:00
|
|
|
|
2018-09-02 20:13:00 +00:00
|
|
|
urlScan(TagStatus, mesg);
|
|
|
|
uiFmt(TagStatus, UICold, "%s", mesg);
|
2018-08-04 17:35:29 +00:00
|
|
|
}
|
|
|
|
|
2019-07-03 02:12:07 +00:00
|
|
|
static void handleReplyList(char *prefix, char *params) {
|
|
|
|
char *chan, *count, *topic;
|
|
|
|
parse(prefix, NULL, NULL, NULL, params, 4, 0, NULL, &chan, &count, &topic);
|
|
|
|
if (topic[0] == '[') {
|
|
|
|
char *skip = strstr(topic, "] ");
|
|
|
|
if (skip) topic = &skip[2];
|
|
|
|
}
|
|
|
|
const char *people = (strcmp(count, "1") ? "people" : "person");
|
|
|
|
if (topic[0]) {
|
|
|
|
uiFmt(
|
|
|
|
TagStatus, UIWarm,
|
|
|
|
"You see %s %s in \3%d%s\3 under the banner, \"%s\"",
|
|
|
|
count, people, colorGen(chan), chan, topic
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
uiFmt(
|
|
|
|
TagStatus, UIWarm,
|
|
|
|
"You see %s %s in \3%d%s\3",
|
|
|
|
count, people, colorGen(chan), chan
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void handleReplyListEnd(char *prefix, char *params) {
|
|
|
|
(void)prefix;
|
|
|
|
(void)params;
|
|
|
|
uiLog(TagStatus, UICold, L"You don't see anyone else");
|
|
|
|
}
|
|
|
|
|
2018-11-29 10:37:10 +00:00
|
|
|
static enum IRCColor whoisColor;
|
|
|
|
static void handleReplyWhoisUser(char *prefix, char *params) {
|
|
|
|
char *nick, *user, *host, *real;
|
2018-12-03 05:08:24 +00:00
|
|
|
parse(
|
2018-11-29 10:37:10 +00:00
|
|
|
prefix, NULL, NULL, NULL,
|
|
|
|
params, 6, 0, NULL, &nick, &user, &host, NULL, &real
|
|
|
|
);
|
2019-07-03 01:41:52 +00:00
|
|
|
whoisColor = colorGen(user);
|
2018-11-29 10:37:10 +00:00
|
|
|
uiFmt(
|
|
|
|
TagStatus, UIWarm,
|
2018-11-29 10:52:27 +00:00
|
|
|
"\3%d%s\3 is %s@%s, \"%s\"",
|
2018-11-29 10:37:10 +00:00
|
|
|
whoisColor, nick, user, host, real
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void handleReplyWhoisServer(char *prefix, char *params) {
|
|
|
|
char *nick, *serv, *info;
|
2018-12-03 05:08:24 +00:00
|
|
|
parse(prefix, NULL, NULL, NULL, params, 4, 0, NULL, &nick, &serv, &info);
|
2018-11-29 10:37:10 +00:00
|
|
|
uiFmt(
|
|
|
|
TagStatus, UIWarm,
|
|
|
|
"\3%d%s\3 is connected to %s, \"%s\"",
|
|
|
|
whoisColor, nick, serv, info
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void handleReplyWhoisOperator(char *prefix, char *params) {
|
|
|
|
char *nick, *oper;
|
2018-12-03 05:08:24 +00:00
|
|
|
parse(prefix, NULL, NULL, NULL, params, 3, 0, NULL, &nick, &oper);
|
2018-11-29 10:37:10 +00:00
|
|
|
uiFmt(TagStatus, UIWarm, "\3%d%s\3 %s", whoisColor, nick, oper);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void handleReplyWhoisIdle(char *prefix, char *params) {
|
|
|
|
char *nick, *idle, *sign;
|
2018-12-03 05:08:24 +00:00
|
|
|
parse(prefix, NULL, NULL, NULL, params, 4, 0, NULL, &nick, &idle, &sign);
|
2018-11-29 10:37:10 +00:00
|
|
|
time_t time = strtoul(sign, NULL, 10);
|
|
|
|
const char *at = ctime(&time);
|
|
|
|
unsigned long secs = strtoul(idle, NULL, 10);
|
|
|
|
unsigned long mins = secs / 60; secs %= 60;
|
|
|
|
unsigned long hours = mins / 60; mins %= 60;
|
|
|
|
uiFmt(
|
|
|
|
TagStatus, UIWarm,
|
|
|
|
"\3%d%s\3 signed on at %.24s and has been idle for %02lu:%02lu:%02lu",
|
|
|
|
whoisColor, nick, at, hours, mins, secs
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void handleReplyWhoisChannels(char *prefix, char *params) {
|
|
|
|
char *nick, *chans;
|
2018-12-03 05:08:24 +00:00
|
|
|
parse(prefix, NULL, NULL, NULL, params, 3, 0, NULL, &nick, &chans);
|
2018-11-29 10:37:10 +00:00
|
|
|
uiFmt(TagStatus, UIWarm, "\3%d%s\3 is in %s", whoisColor, nick, chans);
|
|
|
|
}
|
|
|
|
|
2018-11-29 10:52:27 +00:00
|
|
|
static void handleErrorNoSuchNick(char *prefix, char *params) {
|
|
|
|
char *nick, *mesg;
|
2018-12-03 05:08:24 +00:00
|
|
|
parse(prefix, NULL, NULL, NULL, params, 3, 0, NULL, &nick, &mesg);
|
2018-11-29 10:52:27 +00:00
|
|
|
uiFmt(TagStatus, UIWarm, "%s, \"%s\"", mesg, nick);
|
|
|
|
}
|
|
|
|
|
2018-08-04 17:35:29 +00:00
|
|
|
static void handleJoin(char *prefix, char *params) {
|
2018-08-09 21:37:14 +00:00
|
|
|
char *nick, *user, *chan;
|
2018-12-03 05:08:24 +00:00
|
|
|
parse(prefix, &nick, &user, NULL, params, 1, 0, &chan);
|
2019-02-26 04:10:40 +00:00
|
|
|
struct Tag tag = colorTag(tagFor(chan), chan);
|
2018-08-17 18:00:08 +00:00
|
|
|
|
2018-12-03 15:33:04 +00:00
|
|
|
if (!strcmp(nick, self.nick)) {
|
2018-09-02 20:13:00 +00:00
|
|
|
tabTouch(TagNone, chan);
|
2019-02-22 19:11:50 +00:00
|
|
|
uiShowTag(tag);
|
2018-11-29 23:20:24 +00:00
|
|
|
logReplay(tag);
|
2018-08-13 17:49:03 +00:00
|
|
|
}
|
2018-08-11 23:30:30 +00:00
|
|
|
tabTouch(tag, nick);
|
2018-08-17 18:00:08 +00:00
|
|
|
|
2018-08-09 21:37:14 +00:00
|
|
|
uiFmt(
|
2018-09-02 20:13:00 +00:00
|
|
|
tag, UICold,
|
2018-08-17 18:00:08 +00:00
|
|
|
"\3%d%s\3 arrives in \3%d%s\3",
|
2019-02-26 04:10:40 +00:00
|
|
|
colorGen(user), nick, colorGen(chan), chan
|
2018-08-09 21:37:14 +00:00
|
|
|
);
|
2018-08-18 01:50:45 +00:00
|
|
|
logFmt(tag, NULL, "%s arrives in %s", nick, chan);
|
2018-08-04 17:35:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void handlePart(char *prefix, char *params) {
|
2018-08-09 21:37:14 +00:00
|
|
|
char *nick, *user, *chan, *mesg;
|
2018-12-03 05:08:24 +00:00
|
|
|
parse(prefix, &nick, &user, NULL, params, 1, 1, &chan, &mesg);
|
2019-02-26 04:10:40 +00:00
|
|
|
struct Tag tag = colorTag(tagFor(chan), chan);
|
2018-08-17 18:00:08 +00:00
|
|
|
|
2018-12-03 15:33:04 +00:00
|
|
|
if (!strcmp(nick, self.nick)) {
|
2018-08-17 18:00:08 +00:00
|
|
|
tabClear(tag);
|
|
|
|
} else {
|
|
|
|
tabRemove(tag, nick);
|
|
|
|
}
|
|
|
|
|
2018-08-09 21:37:14 +00:00
|
|
|
if (mesg) {
|
2018-08-11 23:30:30 +00:00
|
|
|
urlScan(tag, mesg);
|
2018-08-07 05:17:22 +00:00
|
|
|
uiFmt(
|
2018-09-02 20:13:00 +00:00
|
|
|
tag, UICold,
|
2018-08-17 18:00:08 +00:00
|
|
|
"\3%d%s\3 leaves \3%d%s\3, \"%s\"",
|
2019-02-26 04:10:40 +00:00
|
|
|
colorGen(user), nick, colorGen(chan), chan, dequote(mesg)
|
2018-08-07 05:17:22 +00:00
|
|
|
);
|
2018-08-18 01:50:45 +00:00
|
|
|
logFmt(tag, NULL, "%s leaves %s, \"%s\"", nick, chan, dequote(mesg));
|
2018-08-07 05:17:22 +00:00
|
|
|
} else {
|
|
|
|
uiFmt(
|
2018-09-02 20:13:00 +00:00
|
|
|
tag, UICold,
|
2018-08-17 18:00:08 +00:00
|
|
|
"\3%d%s\3 leaves \3%d%s\3",
|
2019-02-26 04:10:40 +00:00
|
|
|
colorGen(user), nick, colorGen(chan), chan
|
2018-08-07 05:17:22 +00:00
|
|
|
);
|
2018-08-18 01:50:45 +00:00
|
|
|
logFmt(tag, NULL, "%s leaves %s", nick, chan);
|
2018-08-07 05:17:22 +00:00
|
|
|
}
|
2018-08-04 17:35:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void handleKick(char *prefix, char *params) {
|
2018-08-09 21:37:14 +00:00
|
|
|
char *nick, *user, *chan, *kick, *mesg;
|
2018-12-03 05:08:24 +00:00
|
|
|
parse(prefix, &nick, &user, NULL, params, 2, 1, &chan, &kick, &mesg);
|
2019-02-26 04:10:40 +00:00
|
|
|
struct Tag tag = colorTag(tagFor(chan), chan);
|
2018-08-17 18:00:08 +00:00
|
|
|
bool kicked = !strcmp(kick, self.nick);
|
|
|
|
|
|
|
|
if (kicked) {
|
|
|
|
tabClear(tag);
|
|
|
|
} else {
|
|
|
|
tabRemove(tag, kick);
|
|
|
|
}
|
|
|
|
|
2018-08-09 21:37:14 +00:00
|
|
|
if (mesg) {
|
2018-08-11 23:30:30 +00:00
|
|
|
urlScan(tag, mesg);
|
2018-08-09 21:37:14 +00:00
|
|
|
uiFmt(
|
2018-09-02 20:13:00 +00:00
|
|
|
tag, (kicked ? UIHot : UICold),
|
2018-08-17 18:00:08 +00:00
|
|
|
"\3%d%s\3 kicks \3%d%s\3 out of \3%d%s\3, \"%s\"",
|
2019-02-26 04:10:40 +00:00
|
|
|
colorGen(user), nick,
|
|
|
|
colorGen(kick), kick,
|
|
|
|
colorGen(chan), chan,
|
2018-08-17 18:00:08 +00:00
|
|
|
dequote(mesg)
|
2018-08-09 21:37:14 +00:00
|
|
|
);
|
2018-08-18 01:50:45 +00:00
|
|
|
logFmt(
|
|
|
|
tag, NULL,
|
|
|
|
"%s kicks %s out of %s, \"%s\"", nick, kick, chan, dequote(mesg)
|
|
|
|
);
|
2018-08-09 21:37:14 +00:00
|
|
|
} else {
|
|
|
|
uiFmt(
|
2018-09-02 20:13:00 +00:00
|
|
|
tag, (kicked ? UIHot : UICold),
|
2018-08-17 18:00:08 +00:00
|
|
|
"\3%d%s\3 kicks \3%d%s\3 out of \3%d%s\3",
|
2019-02-26 04:10:40 +00:00
|
|
|
colorGen(user), nick,
|
|
|
|
colorGen(kick), kick,
|
|
|
|
colorGen(chan), chan
|
2018-08-09 21:37:14 +00:00
|
|
|
);
|
2018-08-18 01:50:45 +00:00
|
|
|
logFmt(tag, NULL, "%s kicks %s out of %s", nick, kick, chan);
|
2018-08-09 21:37:14 +00:00
|
|
|
}
|
2018-08-11 03:31:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void handleQuit(char *prefix, char *params) {
|
|
|
|
char *nick, *user, *mesg;
|
2018-12-03 05:08:24 +00:00
|
|
|
parse(prefix, &nick, &user, NULL, params, 0, 1, &mesg);
|
2018-08-17 18:00:08 +00:00
|
|
|
|
2018-08-11 23:30:30 +00:00
|
|
|
struct Tag tag;
|
2018-09-02 20:13:00 +00:00
|
|
|
while (TagNone.id != (tag = tabTag(nick)).id) {
|
2018-08-11 23:30:30 +00:00
|
|
|
tabRemove(tag, nick);
|
2018-08-17 18:00:08 +00:00
|
|
|
|
2018-08-11 23:30:30 +00:00
|
|
|
if (mesg) {
|
|
|
|
urlScan(tag, mesg);
|
|
|
|
uiFmt(
|
2018-09-02 20:13:00 +00:00
|
|
|
tag, UICold,
|
2018-08-17 18:00:08 +00:00
|
|
|
"\3%d%s\3 leaves, \"%s\"",
|
2019-02-26 04:10:40 +00:00
|
|
|
colorGen(user), nick, dequote(mesg)
|
2018-08-11 23:30:30 +00:00
|
|
|
);
|
2018-08-18 01:50:45 +00:00
|
|
|
logFmt(tag, NULL, "%s leaves, \"%s\"", nick, dequote(mesg));
|
2018-08-11 23:30:30 +00:00
|
|
|
} else {
|
2019-02-26 04:10:40 +00:00
|
|
|
uiFmt(tag, UICold, "\3%d%s\3 leaves", colorGen(user), nick);
|
2018-08-18 01:50:45 +00:00
|
|
|
logFmt(tag, NULL, "%s leaves", nick);
|
2018-08-11 23:30:30 +00:00
|
|
|
}
|
2018-08-11 03:31:20 +00:00
|
|
|
}
|
2018-08-04 17:35:29 +00:00
|
|
|
}
|
|
|
|
|
2018-08-11 23:30:30 +00:00
|
|
|
static void handleReplyTopic(char *prefix, char *params) {
|
2018-08-09 21:37:14 +00:00
|
|
|
char *chan, *topic;
|
2018-12-03 05:08:24 +00:00
|
|
|
parse(prefix, NULL, NULL, NULL, params, 3, 0, NULL, &chan, &topic);
|
2019-02-26 04:10:40 +00:00
|
|
|
struct Tag tag = colorTag(tagFor(chan), chan);
|
2018-08-17 18:00:08 +00:00
|
|
|
|
2018-08-11 03:31:20 +00:00
|
|
|
urlScan(tag, topic);
|
2018-08-04 17:35:29 +00:00
|
|
|
uiFmt(
|
2018-09-02 20:13:00 +00:00
|
|
|
tag, UICold,
|
2018-08-17 18:00:08 +00:00
|
|
|
"The sign in \3%d%s\3 reads, \"%s\"",
|
2019-02-26 04:10:40 +00:00
|
|
|
colorGen(chan), chan, topic
|
2018-08-04 17:35:29 +00:00
|
|
|
);
|
2018-08-18 01:50:45 +00:00
|
|
|
logFmt(tag, NULL, "The sign in %s reads, \"%s\"", chan, topic);
|
2018-08-04 17:35:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void handleTopic(char *prefix, char *params) {
|
2018-08-09 21:37:14 +00:00
|
|
|
char *nick, *user, *chan, *topic;
|
2018-12-03 05:08:24 +00:00
|
|
|
parse(prefix, &nick, &user, NULL, params, 2, 0, &chan, &topic);
|
2019-02-26 04:10:40 +00:00
|
|
|
struct Tag tag = colorTag(tagFor(chan), chan);
|
2018-08-17 18:00:08 +00:00
|
|
|
|
2018-12-03 15:33:04 +00:00
|
|
|
if (strcmp(nick, self.nick)) tabTouch(tag, nick);
|
2018-08-17 18:00:08 +00:00
|
|
|
|
2018-08-11 03:31:20 +00:00
|
|
|
urlScan(tag, topic);
|
2018-08-04 17:35:29 +00:00
|
|
|
uiFmt(
|
2018-09-02 20:13:00 +00:00
|
|
|
tag, UICold,
|
2018-08-17 18:00:08 +00:00
|
|
|
"\3%d%s\3 places a new sign in \3%d%s\3, \"%s\"",
|
2019-02-26 04:10:40 +00:00
|
|
|
colorGen(user), nick, colorGen(chan), chan, topic
|
2018-08-04 17:35:29 +00:00
|
|
|
);
|
2018-08-18 01:50:45 +00:00
|
|
|
logFmt(tag, NULL, "%s places a new sign in %s, \"%s\"", nick, chan, topic);
|
2018-08-04 17:35:29 +00:00
|
|
|
}
|
|
|
|
|
2018-08-11 23:30:30 +00:00
|
|
|
static void handleReplyEndOfNames(char *prefix, char *params) {
|
2018-08-09 21:37:14 +00:00
|
|
|
char *chan;
|
2018-12-03 05:08:24 +00:00
|
|
|
parse(prefix, NULL, NULL, NULL, params, 2, 0, NULL, &chan);
|
2018-08-04 21:54:46 +00:00
|
|
|
ircFmt("WHO %s\r\n", chan);
|
2018-08-04 17:35:29 +00:00
|
|
|
}
|
|
|
|
|
2018-08-07 04:12:08 +00:00
|
|
|
static struct {
|
|
|
|
char buf[4096];
|
|
|
|
size_t len;
|
|
|
|
} who;
|
2018-08-04 17:35:29 +00:00
|
|
|
|
2018-08-11 23:30:30 +00:00
|
|
|
static void handleReplyWho(char *prefix, char *params) {
|
2018-08-11 03:31:20 +00:00
|
|
|
char *chan, *user, *nick;
|
2018-12-03 05:08:24 +00:00
|
|
|
parse(
|
2018-08-09 21:37:14 +00:00
|
|
|
prefix, NULL, NULL, NULL,
|
2018-08-11 03:31:20 +00:00
|
|
|
params, 6, 0, NULL, &chan, &user, NULL, NULL, &nick
|
2018-08-09 21:37:14 +00:00
|
|
|
);
|
2019-02-26 04:10:40 +00:00
|
|
|
struct Tag tag = colorTag(tagFor(chan), chan);
|
2018-08-17 18:00:08 +00:00
|
|
|
|
2018-08-20 23:11:44 +00:00
|
|
|
tabAdd(tag, nick);
|
2018-08-17 18:00:08 +00:00
|
|
|
|
2018-08-07 04:12:08 +00:00
|
|
|
size_t cap = sizeof(who.buf) - who.len;
|
|
|
|
int len = snprintf(
|
|
|
|
&who.buf[who.len], cap,
|
2018-08-04 17:35:29 +00:00
|
|
|
"%s\3%d%s\3",
|
2019-02-26 04:10:40 +00:00
|
|
|
(who.len ? ", " : ""), colorGen(user), nick
|
2018-08-04 17:35:29 +00:00
|
|
|
);
|
2018-08-07 04:12:08 +00:00
|
|
|
if ((size_t)len < cap) who.len += len;
|
2018-08-04 17:35:29 +00:00
|
|
|
}
|
|
|
|
|
2018-08-11 23:30:30 +00:00
|
|
|
static void handleReplyEndOfWho(char *prefix, char *params) {
|
2018-08-09 21:37:14 +00:00
|
|
|
char *chan;
|
2018-12-03 05:08:24 +00:00
|
|
|
parse(prefix, NULL, NULL, NULL, params, 2, 0, NULL, &chan);
|
2019-02-26 04:10:40 +00:00
|
|
|
struct Tag tag = colorTag(tagFor(chan), chan);
|
2018-08-17 18:00:08 +00:00
|
|
|
|
2018-08-04 17:35:29 +00:00
|
|
|
uiFmt(
|
2018-09-02 20:13:00 +00:00
|
|
|
tag, UICold,
|
2018-08-17 18:00:08 +00:00
|
|
|
"In \3%d%s\3 are %s",
|
2019-02-26 04:10:40 +00:00
|
|
|
colorGen(chan), chan, who.buf
|
2018-08-04 17:35:29 +00:00
|
|
|
);
|
2018-08-09 21:37:14 +00:00
|
|
|
who.len = 0;
|
2018-08-04 17:35:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void handleNick(char *prefix, char *params) {
|
2018-08-09 21:37:14 +00:00
|
|
|
char *prev, *user, *next;
|
2018-12-03 05:08:24 +00:00
|
|
|
parse(prefix, &prev, &user, NULL, params, 1, 0, &next);
|
2018-08-17 18:00:08 +00:00
|
|
|
|
2018-12-03 15:33:04 +00:00
|
|
|
if (!strcmp(prev, self.nick)) {
|
2018-12-03 05:08:24 +00:00
|
|
|
free(self.nick);
|
|
|
|
self.nick = strdup(next);
|
|
|
|
if (!self.nick) err(EX_OSERR, "strdup");
|
2019-02-22 19:11:50 +00:00
|
|
|
uiPrompt(true);
|
2018-09-15 03:37:09 +00:00
|
|
|
}
|
2018-08-17 18:00:08 +00:00
|
|
|
|
2018-08-11 23:30:30 +00:00
|
|
|
struct Tag tag;
|
2018-09-02 20:13:00 +00:00
|
|
|
while (TagNone.id != (tag = tabTag(prev)).id) {
|
2018-08-11 23:30:30 +00:00
|
|
|
tabReplace(tag, prev, next);
|
2018-08-17 18:00:08 +00:00
|
|
|
|
2018-08-11 23:30:30 +00:00
|
|
|
uiFmt(
|
2018-09-02 20:13:00 +00:00
|
|
|
tag, UICold,
|
2018-08-17 18:00:08 +00:00
|
|
|
"\3%d%s\3 is now known as \3%d%s\3",
|
2019-02-26 04:10:40 +00:00
|
|
|
colorGen(user), prev, colorGen(user), next
|
2018-08-11 23:30:30 +00:00
|
|
|
);
|
2018-08-18 01:50:45 +00:00
|
|
|
logFmt(tag, NULL, "%s is now known as %s", prev, next);
|
2018-08-11 23:30:30 +00:00
|
|
|
}
|
2018-08-09 21:37:14 +00:00
|
|
|
}
|
|
|
|
|
2018-08-11 03:31:20 +00:00
|
|
|
static void handleCTCP(struct Tag tag, char *nick, char *user, char *mesg) {
|
2018-08-09 21:37:14 +00:00
|
|
|
mesg = &mesg[1];
|
|
|
|
char *ctcp = strsep(&mesg, " ");
|
|
|
|
char *params = strsep(&mesg, "\1");
|
|
|
|
if (strcmp(ctcp, "ACTION")) return;
|
2018-08-17 18:00:08 +00:00
|
|
|
|
2018-12-03 15:33:04 +00:00
|
|
|
if (strcmp(nick, self.nick)) tabTouch(tag, nick);
|
2018-08-17 18:00:08 +00:00
|
|
|
|
2018-08-11 03:31:20 +00:00
|
|
|
urlScan(tag, params);
|
2018-12-03 15:33:04 +00:00
|
|
|
bool ping = strcmp(nick, self.nick) && isPing(params);
|
2018-08-04 17:35:29 +00:00
|
|
|
uiFmt(
|
2018-09-02 20:13:00 +00:00
|
|
|
tag, (ping ? UIHot : UIWarm),
|
2018-08-17 18:00:08 +00:00
|
|
|
"%c\3%d* %s\17 %s",
|
2019-02-26 04:10:40 +00:00
|
|
|
ping["\17\26"], colorGen(user), nick, params
|
2018-08-04 17:35:29 +00:00
|
|
|
);
|
2018-08-18 01:50:45 +00:00
|
|
|
logFmt(tag, NULL, "* %s %s", nick, params);
|
2018-08-04 17:35:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void handlePrivmsg(char *prefix, char *params) {
|
2018-08-11 03:31:20 +00:00
|
|
|
char *nick, *user, *chan, *mesg;
|
2018-12-03 05:08:24 +00:00
|
|
|
parse(prefix, &nick, &user, NULL, params, 2, 0, &chan, &mesg);
|
2018-09-02 05:06:45 +00:00
|
|
|
bool direct = !strcmp(chan, self.nick);
|
2019-02-26 04:10:40 +00:00
|
|
|
struct Tag tag = tagFor(direct ? nick : chan);
|
|
|
|
colorTag(tag, direct ? user : chan);
|
2018-08-09 21:37:14 +00:00
|
|
|
if (mesg[0] == '\1') {
|
2018-08-11 03:31:20 +00:00
|
|
|
handleCTCP(tag, nick, user, mesg);
|
2018-08-09 21:37:14 +00:00
|
|
|
return;
|
|
|
|
}
|
2018-08-17 18:00:08 +00:00
|
|
|
|
2018-12-03 15:33:04 +00:00
|
|
|
bool me = !strcmp(nick, self.nick);
|
|
|
|
if (!me) tabTouch(tag, nick);
|
2018-08-17 18:00:08 +00:00
|
|
|
|
|
|
|
urlScan(tag, mesg);
|
2018-12-03 15:33:04 +00:00
|
|
|
bool hot = !me && (direct || isPing(mesg));
|
|
|
|
bool ping = !me && isPing(mesg);
|
2018-08-09 21:37:14 +00:00
|
|
|
uiFmt(
|
2018-09-11 18:45:39 +00:00
|
|
|
tag, (hot ? UIHot : UIWarm),
|
2018-12-15 06:09:56 +00:00
|
|
|
"%c%c\3%d<%s>%c %s",
|
|
|
|
(me ? IRCUnderline : IRCColor), (ping ? IRCReverse : IRCColor),
|
2019-02-26 04:10:40 +00:00
|
|
|
colorGen(user), nick, IRCReset, mesg
|
2018-08-09 21:37:14 +00:00
|
|
|
);
|
2018-08-18 01:50:45 +00:00
|
|
|
logFmt(tag, NULL, "<%s> %s", nick, mesg);
|
2018-08-04 17:35:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void handleNotice(char *prefix, char *params) {
|
2018-08-09 21:37:14 +00:00
|
|
|
char *nick, *user, *chan, *mesg;
|
2018-12-03 05:08:24 +00:00
|
|
|
parse(prefix, &nick, &user, NULL, params, 2, 0, &chan, &mesg);
|
2019-02-26 04:10:40 +00:00
|
|
|
bool direct = !strcmp(chan, self.nick);
|
2018-09-02 20:13:00 +00:00
|
|
|
struct Tag tag = TagStatus;
|
2019-02-25 21:02:41 +00:00
|
|
|
if (user) {
|
2019-02-26 04:10:40 +00:00
|
|
|
tag = tagFor(direct ? nick : chan);
|
|
|
|
colorTag(tag, direct ? user : chan);
|
2019-02-25 21:02:41 +00:00
|
|
|
}
|
2018-08-17 18:00:08 +00:00
|
|
|
|
2018-12-03 15:33:04 +00:00
|
|
|
if (strcmp(nick, self.nick)) tabTouch(tag, nick);
|
2018-08-17 18:00:08 +00:00
|
|
|
|
2018-08-11 03:31:20 +00:00
|
|
|
urlScan(tag, mesg);
|
2018-12-03 15:33:04 +00:00
|
|
|
bool ping = strcmp(nick, self.nick) && isPing(mesg);
|
2018-08-09 21:37:14 +00:00
|
|
|
uiFmt(
|
2018-09-02 20:13:00 +00:00
|
|
|
tag, (ping ? UIHot : UIWarm),
|
2018-08-17 18:00:08 +00:00
|
|
|
"%c\3%d-%s-\17 %s",
|
2019-02-26 04:10:40 +00:00
|
|
|
ping["\17\26"], colorGen(user), nick, mesg
|
2018-08-09 21:37:14 +00:00
|
|
|
);
|
2018-08-18 01:50:45 +00:00
|
|
|
logFmt(tag, NULL, "-%s- %s", nick, mesg);
|
2018-08-04 17:35:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static const struct {
|
|
|
|
const char *command;
|
2018-12-30 06:53:30 +00:00
|
|
|
Handler *handler;
|
2018-09-02 20:13:00 +00:00
|
|
|
} Handlers[] = {
|
2018-08-11 23:30:30 +00:00
|
|
|
{ "001", handleReplyWelcome },
|
2018-11-29 10:37:10 +00:00
|
|
|
{ "311", handleReplyWhoisUser },
|
|
|
|
{ "312", handleReplyWhoisServer },
|
|
|
|
{ "313", handleReplyWhoisOperator },
|
2018-08-11 23:30:30 +00:00
|
|
|
{ "315", handleReplyEndOfWho },
|
2018-11-29 10:37:10 +00:00
|
|
|
{ "317", handleReplyWhoisIdle },
|
|
|
|
{ "319", handleReplyWhoisChannels },
|
2019-07-03 02:12:07 +00:00
|
|
|
{ "322", handleReplyList },
|
|
|
|
{ "323", handleReplyListEnd },
|
2018-08-11 23:30:30 +00:00
|
|
|
{ "332", handleReplyTopic },
|
|
|
|
{ "352", handleReplyWho },
|
|
|
|
{ "366", handleReplyEndOfNames },
|
|
|
|
{ "372", handleReplyMOTD },
|
|
|
|
{ "375", handleReplyMOTD },
|
2018-11-29 10:52:27 +00:00
|
|
|
{ "401", handleErrorNoSuchNick },
|
|
|
|
{ "432", handleErrorErroneousNickname },
|
|
|
|
{ "433", handleErrorErroneousNickname },
|
2018-12-05 03:13:44 +00:00
|
|
|
{ "CAP", handleCap },
|
2018-12-04 19:58:14 +00:00
|
|
|
{ "ERROR", handleError },
|
2018-08-04 17:35:29 +00:00
|
|
|
{ "JOIN", handleJoin },
|
|
|
|
{ "KICK", handleKick },
|
|
|
|
{ "NICK", handleNick },
|
|
|
|
{ "NOTICE", handleNotice },
|
|
|
|
{ "PART", handlePart },
|
|
|
|
{ "PING", handlePing },
|
|
|
|
{ "PRIVMSG", handlePrivmsg },
|
|
|
|
{ "QUIT", handleQuit },
|
|
|
|
{ "TOPIC", handleTopic },
|
|
|
|
};
|
2018-09-02 20:13:00 +00:00
|
|
|
static const size_t HandlersLen = sizeof(Handlers) / sizeof(Handlers[0]);
|
2018-08-04 17:35:29 +00:00
|
|
|
|
|
|
|
void handle(char *line) {
|
|
|
|
char *prefix = NULL;
|
|
|
|
if (line[0] == ':') {
|
|
|
|
prefix = strsep(&line, " ") + 1;
|
|
|
|
if (!line) errx(EX_PROTOCOL, "unexpected eol");
|
|
|
|
}
|
|
|
|
char *command = strsep(&line, " ");
|
2018-09-02 20:13:00 +00:00
|
|
|
for (size_t i = 0; i < HandlersLen; ++i) {
|
|
|
|
if (strcmp(command, Handlers[i].command)) continue;
|
|
|
|
Handlers[i].handler(prefix, line);
|
2018-08-04 17:35:29 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|