From 1b41af31351ed0dd4bcdceda5efbbae3e38c3fca Mon Sep 17 00:00:00 2001 From: Curtis McEnroe Date: Fri, 25 Oct 2019 18:24:47 -0400 Subject: Send PRIVMSG and NOTICE to other clients --- bounce.h | 1 + client.c | 21 ++++++++++++++++++++- state.c | 6 ++++++ 3 files changed, 27 insertions(+), 1 deletion(-) diff --git a/bounce.h b/bounce.h index 3307bac..e6efe10 100644 --- a/bounce.h +++ b/bounce.h @@ -87,3 +87,4 @@ void clientConsume(struct Client *client); bool stateReady(void); void stateParse(char *line); void stateSync(struct Client *client); +const char *stateSelf(void); diff --git a/client.c b/client.c index 7079d7a..2bfe7db 100644 --- a/client.c +++ b/client.c @@ -169,13 +169,31 @@ static void handleQuit(struct Client *client, struct Message *msg) { client->error = true; } +static void handlePrivmsg(struct Client *client, struct Message *msg) { + if (!msg->params[0] || !msg->params[1]) return; + // FIXME: Check against ISUPPORT CHANTYPES? + if (msg->params[0][0] == '#') { + char line[1024]; + snprintf( + line, sizeof(line), ":%s %s %s :%s", + stateSelf(), msg->cmd, msg->params[0], msg->params[1] + ); + size_t diff = ringDiff(client->consumer); + ringProduce(line); + if (!diff) ringConsume(NULL, client->consumer); + } + serverFormat("%s %s :%s\r\n", msg->cmd, msg->params[0], msg->params[1]); +} + static const struct { const char *cmd; Handler *fn; } Handlers[] = { { "CAP", handleCap }, { "NICK", handleNick }, + { "NOTICE", handlePrivmsg }, { "PASS", handlePass }, + { "PRIVMSG", handlePrivmsg }, { "QUIT", handleQuit }, { "USER", handleUser }, }; @@ -193,7 +211,8 @@ static void clientParse(struct Client *client, char *line) { static bool intercept(const char *line, size_t len) { if (len >= 4 && !memcmp(line, "CAP ", 4)) return true; if (len >= 5 && !memcmp(line, "QUIT ", 5)) return true; - // TODO: Intercept PRIVMSG and NOTICE to send to other clients. + if (len >= 7 && !memcmp(line, "NOTICE ", 7)) return true; + if (len >= 8 && !memcmp(line, "PRIVMSG ", 8)) return true; return false; } diff --git a/state.c b/state.c index a65b1c2..c46aa8c 100644 --- a/state.c +++ b/state.c @@ -94,6 +94,12 @@ bool stateReady(void) { && support.len; } +const char *stateSelf(void) { + if (self.origin) return self.origin; + if (self.nick) return self.nick; + return "*"; +} + typedef void Handler(struct Message *msg); static void handleCap(struct Message *msg) { -- cgit 1.4.1