diff options
author | Sadie Powell <sadie@witchery.services> | 2024-06-24 14:29:55 +0100 |
---|---|---|
committer | Sadie Powell <sadie@witchery.services> | 2024-06-24 14:29:55 +0100 |
commit | 693eeed762eac490b1289f8f4428ff0b5bbf1672 (patch) | |
tree | 974cbad287da04fc9e137d7f51092efd2e474acd /src | |
parent | 6e5713d64a379fc64c7ff6658362d02b3618c3ca (diff) |
Rework how CTCP messages are sent and received.
Diffstat (limited to 'src')
-rw-r--r-- | src/messages.cpp | 14 | ||||
-rw-r--r-- | src/misc.cpp | 47 | ||||
-rw-r--r-- | src/protocol.cpp | 27 |
3 files changed, 53 insertions, 35 deletions
diff --git a/src/messages.cpp b/src/messages.cpp index 9a0a7a539..512da3e95 100644 --- a/src/messages.cpp +++ b/src/messages.cpp @@ -342,19 +342,17 @@ void Privmsg::Run(MessageSource &source, const std::vector<Anope::string> ¶m if (bi) { - if (message[0] == '\1' && message[message.length() - 1] == '\1') + Anope::string ctcpname, ctcpbody; + if (Anope::ParseCTCP(message, ctcpname, ctcpbody)) { - if (message.substr(0, 6).equals_ci("\1PING ")) + if (ctcpname.equals_ci("PING")) { - Anope::string buf = message; - buf.erase(buf.begin()); - buf.erase(buf.end() - 1); - IRCD->SendCTCP(bi, u->nick, "%s", buf.c_str()); + IRCD->SendNotice(bi, u->nick, Anope::FormatCTCP("PING", ctcpbody)); } - else if (message.substr(0, 9).equals_ci("\1VERSION\1")) + else if (ctcpname.equals_ci("VERSION")) { Module *enc = ModuleManager::FindFirstOf(ENCRYPTION); - IRCD->SendCTCP(bi, u->nick, "VERSION Anope-%s %s :%s - (%s) -- %s", Anope::Version().c_str(), Me->GetName().c_str(), IRCD->GetProtocolName().c_str(), enc ? enc->name.c_str() : "(none)", Anope::VersionBuildString().c_str()); + IRCD->SendNotice(bi, u->nick, Anope::FormatCTCP("VERSION", Anope::printf("Anope-%s %s :%s - (%s) -- %s", Anope::Version().c_str(), Me->GetName().c_str(), IRCD->GetProtocolName().c_str(), enc ? enc->name.c_str() : "(none)", Anope::VersionBuildString().c_str()))); } return; } diff --git a/src/misc.cpp b/src/misc.cpp index 8a0920fc1..0b760ea76 100644 --- a/src/misc.cpp +++ b/src/misc.cpp @@ -835,3 +835,50 @@ Anope::string Anope::Expand(const Anope::string &base, const Anope::string &frag return Anope::printf("%s%c%s", base.c_str(), separator, fragment.c_str()); } + +Anope::string Anope::FormatCTCP(const Anope::string &name, const Anope::string &value) +{ + if (value.empty()) + return Anope::printf("\1%s\1", name.c_str()); + + return Anope::printf("\1%s %s\1", name.c_str(), value.c_str()); +} + +bool Anope::ParseCTCP(const Anope::string &text, Anope::string &name, Anope::string &body) +{ + // According to draft-oakley-irc-ctcp-02 a valid CTCP must begin with SOH and + // contain at least one octet which is not NUL, SOH, CR, LF, or SPACE. As most + // of these are restricted at the protocol level we only need to check for SOH + // and SPACE. + if (text.length() < 2 || text[0] != '\x1' || text[1] == '\x1' || text[1] == ' ') + { + name.clear(); + body.clear(); + return false; + } + + auto end_of_name = text.find(' ', 2); + auto end_of_ctcp = *text.rbegin() == '\x1' ? 1 : 0; + if (end_of_name == std::string::npos) + { + // The CTCP only contains a name. + name = text.substr(1, text.length() - 1 - end_of_ctcp); + body.clear(); + return true; + } + + // The CTCP contains a name and a body. + name = text.substr(1, end_of_name - 1); + + auto start_of_body = text.find_first_not_of(' ', end_of_name + 1); + if (start_of_body == std::string::npos) + { + // The CTCP body is provided but empty. + body.clear(); + return true; + } + + // The CTCP body provided was non-empty. + body = text.substr(start_of_body, text.length() - start_of_body - end_of_ctcp); + return true; +} diff --git a/src/protocol.cpp b/src/protocol.cpp index 275721634..1c5edd2a4 100644 --- a/src/protocol.cpp +++ b/src/protocol.cpp @@ -166,12 +166,6 @@ void IRCDProto::SendGlobops(const MessageSource &source, const Anope::string &me Uplink::Send(source, "GLOBOPS", message); } -void IRCDProto::SendCTCPInternal(const MessageSource &source, const Anope::string &dest, const Anope::string &buf) -{ - Anope::string s = Anope::NormalizeBuffer(buf); - this->SendNotice(source, dest, "\1" + s + "\1"); -} - void IRCDProto::SendNumericInternal(int numeric, const Anope::string &dest, const std::vector<Anope::string> ¶ms) { Anope::string n = Anope::ToString(numeric); @@ -190,17 +184,6 @@ void IRCDProto::SendTopic(const MessageSource &source, Channel *c) Uplink::Send(source, "TOPIC", c->name, c->topic); } -void IRCDProto::SendAction(const MessageSource &source, const Anope::string &dest, const char *fmt, ...) -{ - va_list args; - char buf[BUFSIZE] = ""; - va_start(args, fmt); - vsnprintf(buf, BUFSIZE - 1, fmt, args); - va_end(args); - Anope::string actionbuf = Anope::string("\1ACTION ") + buf + '\1'; - SendPrivmsg(source, dest, actionbuf); -} - void IRCDProto::SendPing(const Anope::string &servname, const Anope::string &who) { if (servname.empty()) @@ -243,16 +226,6 @@ void IRCDProto::SendForceNickChange(User *u, const Anope::string &newnick, time_ Uplink::Send("SVSNICK", u->GetUID(), newnick, when); } -void IRCDProto::SendCTCP(const MessageSource &source, const Anope::string &dest, const char *fmt, ...) -{ - va_list args; - char buf[BUFSIZE] = ""; - va_start(args, fmt); - vsnprintf(buf, BUFSIZE - 1, fmt, args); - va_end(args); - SendCTCPInternal(source, dest, buf); -} - bool IRCDProto::IsNickValid(const Anope::string &nick) { /** |