2018-08-04 17:35:29 +00:00
|
|
|
/* Copyright (C) 2018 Curtis McEnroe <june@causal.agency>
|
|
|
|
*
|
|
|
|
* 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/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <err.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sysexits.h>
|
|
|
|
|
|
|
|
#include "chat.h"
|
|
|
|
|
|
|
|
static int color(const char *s) {
|
|
|
|
if (!s) return 0;
|
|
|
|
int x = 0;
|
|
|
|
for (; s[0]; ++s) {
|
|
|
|
x ^= s[0];
|
|
|
|
}
|
|
|
|
x &= 15;
|
|
|
|
return (x == 1) ? 0 : x;
|
|
|
|
}
|
|
|
|
|
|
|
|
typedef void (*Handler)(char *prefix, char *params);
|
|
|
|
|
|
|
|
static char *prift(char **prefix) {
|
|
|
|
return strsep(prefix, "!@");
|
|
|
|
}
|
|
|
|
|
|
|
|
static char *shift(char **params) {
|
|
|
|
char *rest = *params;
|
|
|
|
if (!rest) errx(EX_PROTOCOL, "unexpected eol");
|
|
|
|
if (rest[0] == ':') {
|
|
|
|
*params = NULL;
|
|
|
|
return &rest[1];
|
|
|
|
}
|
|
|
|
return strsep(params, " ");
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
static void handle432(char *prefix, char *params) {
|
|
|
|
(void)prefix;
|
|
|
|
shift(¶ms);
|
|
|
|
shift(¶ms);
|
|
|
|
char *mesg = shift(¶ms);
|
2018-08-06 18:19:52 +00:00
|
|
|
uiLog(L"You can't use that name here");
|
2018-08-07 18:11:19 +00:00
|
|
|
uiFmt("Sheriff says, \"%s\"", mesg);
|
2018-08-06 18:19:52 +00:00
|
|
|
uiLog(L"Type /nick <name> to choose a new one");
|
2018-08-04 17:35:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void handle001(char *prefix, char *params) {
|
|
|
|
(void)prefix;
|
|
|
|
char *nick = shift(¶ms);
|
|
|
|
if (strcmp(nick, chat.nick)) {
|
|
|
|
free(chat.nick);
|
|
|
|
chat.nick = strdup(nick);
|
|
|
|
}
|
2018-08-04 21:54:46 +00:00
|
|
|
ircFmt("JOIN %s\r\n", chat.chan);
|
2018-08-04 17:35:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void handleJoin(char *prefix, char *params) {
|
|
|
|
char *nick = prift(&prefix);
|
|
|
|
char *user = prift(&prefix);
|
|
|
|
char *chan = shift(¶ms);
|
|
|
|
if (!strcmp(nick, chat.nick) && strcmp(user, chat.user)) {
|
|
|
|
free(chat.user);
|
|
|
|
chat.user = strdup(user);
|
|
|
|
}
|
2018-08-07 18:58:32 +00:00
|
|
|
tabTouch(nick);
|
2018-08-04 17:35:29 +00:00
|
|
|
uiFmt(
|
2018-08-07 18:11:19 +00:00
|
|
|
"\3%d%s\3 arrives in \3%d%s\3",
|
2018-08-04 17:35:29 +00:00
|
|
|
color(user), nick, color(chan), chan
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void handlePart(char *prefix, char *params) {
|
|
|
|
char *nick = prift(&prefix);
|
|
|
|
char *user = prift(&prefix);
|
|
|
|
char *chan = shift(¶ms);
|
2018-08-07 18:58:32 +00:00
|
|
|
tabRemove(nick);
|
2018-08-07 05:17:22 +00:00
|
|
|
if (params) {
|
|
|
|
char *mesg = shift(¶ms);
|
|
|
|
uiFmt(
|
2018-08-07 18:11:19 +00:00
|
|
|
"\3%d%s\3 leaves \3%d%s\3, \"%s\"",
|
2018-08-07 05:17:22 +00:00
|
|
|
color(user), nick, color(chan), chan, mesg
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
uiFmt(
|
2018-08-07 18:11:19 +00:00
|
|
|
"\3%d%s\3 leaves \3%d%s\3",
|
2018-08-07 05:17:22 +00:00
|
|
|
color(user), nick, color(chan), chan
|
|
|
|
);
|
|
|
|
}
|
2018-08-04 17:35:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void handleQuit(char *prefix, char *params) {
|
|
|
|
char *nick = prift(&prefix);
|
|
|
|
char *user = prift(&prefix);
|
2018-08-07 18:58:32 +00:00
|
|
|
tabRemove(nick);
|
2018-08-07 05:17:22 +00:00
|
|
|
if (params) {
|
|
|
|
char *mesg = shift(¶ms);
|
|
|
|
char *quot = (mesg[0] == '"') ? "" : "\"";
|
|
|
|
uiFmt(
|
2018-08-07 18:11:19 +00:00
|
|
|
"\3%d%s\3 leaves, %s%s%s",
|
2018-08-07 05:17:22 +00:00
|
|
|
color(user), nick, quot, mesg, quot
|
|
|
|
);
|
|
|
|
} else {
|
2018-08-07 18:11:19 +00:00
|
|
|
uiFmt("\3%d%s\3 leaves", color(user), nick);
|
2018-08-07 05:17:22 +00:00
|
|
|
}
|
2018-08-04 17:35:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void handleKick(char *prefix, char *params) {
|
|
|
|
char *nick = prift(&prefix);
|
|
|
|
char *user = prift(&prefix);
|
|
|
|
char *chan = shift(¶ms);
|
|
|
|
char *kick = shift(¶ms);
|
|
|
|
char *mesg = shift(¶ms);
|
2018-08-07 18:58:32 +00:00
|
|
|
tabRemove(nick);
|
2018-08-04 17:35:29 +00:00
|
|
|
uiFmt(
|
2018-08-07 18:11:19 +00:00
|
|
|
"\3%d%s\3 kicks \3%d%s\3 out of \3%d%s\3, \"%s\"",
|
2018-08-04 17:35:29 +00:00
|
|
|
color(user), nick, color(kick), kick, color(chan), chan, mesg
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void handle332(char *prefix, char *params) {
|
|
|
|
(void)prefix;
|
|
|
|
shift(¶ms);
|
|
|
|
char *chan = shift(¶ms);
|
|
|
|
char *topic = shift(¶ms);
|
|
|
|
uiFmt(
|
2018-08-07 18:11:19 +00:00
|
|
|
"The sign in \3%d%s\3 reads, \"%s\"",
|
2018-08-04 17:35:29 +00:00
|
|
|
color(chan), chan, topic
|
|
|
|
);
|
2018-08-06 18:19:52 +00:00
|
|
|
uiTopicStr(topic);
|
2018-08-04 17:35:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void handleTopic(char *prefix, char *params) {
|
|
|
|
char *nick = prift(&prefix);
|
|
|
|
char *user = prift(&prefix);
|
|
|
|
char *chan = shift(¶ms);
|
|
|
|
char *topic = shift(¶ms);
|
|
|
|
uiFmt(
|
2018-08-07 18:11:19 +00:00
|
|
|
"\3%d%s\3 places a new sign in \3%d%s\3, \"%s\"",
|
2018-08-04 17:35:29 +00:00
|
|
|
color(user), nick, color(chan), chan, topic
|
|
|
|
);
|
2018-08-06 18:19:52 +00:00
|
|
|
uiTopicStr(topic);
|
2018-08-04 17:35:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void handle366(char *prefix, char *params) {
|
|
|
|
(void)prefix;
|
|
|
|
shift(¶ms);
|
|
|
|
char *chan = shift(¶ms);
|
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
|
|
|
|
|
|
|
static void handle352(char *prefix, char *params) {
|
|
|
|
(void)prefix;
|
|
|
|
shift(¶ms);
|
|
|
|
shift(¶ms);
|
|
|
|
char *user = shift(¶ms);
|
|
|
|
shift(¶ms);
|
|
|
|
shift(¶ms);
|
|
|
|
char *nick = shift(¶ms);
|
2018-08-07 18:58:32 +00:00
|
|
|
tabTouch(nick);
|
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",
|
2018-08-07 04:12:08 +00:00
|
|
|
(who.len ? ", " : ""), color(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
|
|
|
}
|
|
|
|
|
|
|
|
static void handle315(char *prefix, char *params) {
|
|
|
|
(void)prefix;
|
|
|
|
shift(¶ms);
|
|
|
|
char *chan = shift(¶ms);
|
2018-08-07 04:12:08 +00:00
|
|
|
who.len = 0;
|
2018-08-04 17:35:29 +00:00
|
|
|
uiFmt(
|
2018-08-07 18:11:19 +00:00
|
|
|
"In \3%d%s\3 are %s",
|
2018-08-07 04:12:08 +00:00
|
|
|
color(chan), chan, who.buf
|
2018-08-04 17:35:29 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void handleNick(char *prefix, char *params) {
|
|
|
|
char *prev = prift(&prefix);
|
|
|
|
char *user = prift(&prefix);
|
|
|
|
char *next = shift(¶ms);
|
|
|
|
if (!strcmp(user, chat.user)) {
|
|
|
|
free(chat.nick);
|
|
|
|
chat.nick = strdup(next);
|
|
|
|
}
|
2018-08-07 18:58:32 +00:00
|
|
|
tabReplace(prev, next);
|
2018-08-04 17:35:29 +00:00
|
|
|
uiFmt(
|
2018-08-07 18:11:19 +00:00
|
|
|
"\3%d%s\3 is now known as \3%d%s\3",
|
2018-08-04 17:35:29 +00:00
|
|
|
color(user), prev, color(user), next
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void handlePrivmsg(char *prefix, char *params) {
|
|
|
|
char *nick = prift(&prefix);
|
|
|
|
char *user = prift(&prefix);
|
|
|
|
shift(¶ms);
|
|
|
|
char *mesg = shift(¶ms);
|
2018-08-07 18:58:32 +00:00
|
|
|
tabTouch(nick);
|
2018-08-04 17:35:29 +00:00
|
|
|
if (mesg[0] == '\1') {
|
|
|
|
strsep(&mesg, " ");
|
2018-08-07 18:34:26 +00:00
|
|
|
char *action = strsep(&mesg, "\1");
|
|
|
|
uiFmt("* \3%d%s\3 %s", color(user), nick, action);
|
2018-08-04 17:35:29 +00:00
|
|
|
} else {
|
2018-08-07 21:51:23 +00:00
|
|
|
bool ping = !strncasecmp(mesg, chat.nick, strlen(chat.nick));
|
|
|
|
if (ping) uiBeep();
|
|
|
|
uiFmt(
|
|
|
|
"<%s\3%d%s\17> %s",
|
|
|
|
(ping ? "\26" : ""), color(user), nick, mesg
|
|
|
|
);
|
2018-08-04 17:35:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void handleNotice(char *prefix, char *params) {
|
|
|
|
char *nick = prift(&prefix);
|
|
|
|
char *user = prift(&prefix);
|
|
|
|
char *chan = shift(¶ms);
|
|
|
|
char *mesg = shift(¶ms);
|
|
|
|
if (strcmp(chat.chan, chan)) return;
|
2018-08-07 18:58:32 +00:00
|
|
|
tabTouch(nick);
|
2018-08-07 18:11:19 +00:00
|
|
|
uiFmt("-\3%d%s\3- %s", color(user), nick, mesg);
|
2018-08-04 17:35:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static const struct {
|
|
|
|
const char *command;
|
|
|
|
Handler handler;
|
|
|
|
} HANDLERS[] = {
|
|
|
|
{ "001", handle001 },
|
|
|
|
{ "315", handle315 },
|
|
|
|
{ "332", handle332 },
|
|
|
|
{ "352", handle352 },
|
|
|
|
{ "366", handle366 },
|
|
|
|
{ "432", handle432 },
|
|
|
|
{ "433", handle432 },
|
|
|
|
{ "JOIN", handleJoin },
|
|
|
|
{ "KICK", handleKick },
|
|
|
|
{ "NICK", handleNick },
|
|
|
|
{ "NOTICE", handleNotice },
|
|
|
|
{ "PART", handlePart },
|
|
|
|
{ "PING", handlePing },
|
|
|
|
{ "PRIVMSG", handlePrivmsg },
|
|
|
|
{ "QUIT", handleQuit },
|
|
|
|
{ "TOPIC", handleTopic },
|
|
|
|
};
|
|
|
|
static const size_t HANDLERS_LEN = sizeof(HANDLERS) / sizeof(HANDLERS[0]);
|
|
|
|
|
|
|
|
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, " ");
|
|
|
|
for (size_t i = 0; i < HANDLERS_LEN; ++i) {
|
|
|
|
if (strcmp(command, HANDLERS[i].command)) continue;
|
|
|
|
HANDLERS[i].handler(prefix, line);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|