#include "CommandList.h" #include "Log.h" #include #include namespace core { CommandList::CommandList(std::string delimiter, int depth) : delimiter(delimiter), depth(depth) {} void CommandList::add(Command &command, std::string name) { // processRequest() below upper-cases the incoming keyword before // looking it up, but this previously stored whatever case the // caller passed in verbatim - any registration that wasn't // already uppercase (nearly everything outside SMTP/POP3/IMAP's // own RFC-driven uppercase convention) could then NEVER be // matched by any input, since upper-casing a non-uppercase string // can't produce a match against a non-uppercase key. Confirmed: // this alone made every Zephora game command unreachable, along // with this class's own base-level "publish"/"subscribe"/"event"/ // etc. registrations (TCPServer's constructor). Folding the same // way here keeps registration and lookup permanently in sync // regardless of what case any given add() call happens to use - // already-uppercase callers (SMTP, the BARANTMail admin console) // are unaffected, since upper-casing an uppercase string is a // no-op. std::transform(name.begin(), name.end(), name.begin(), [](unsigned char c) { return std::toupper(c); }); commands.insert(std::make_pair(name, &command)); } void CommandList::remove(Command &command) {} int CommandList::processRequest(coreutils::ZString &request, TCPSession &session) { if(session.grab != NULL) { return session.grab->processCommand(request, session); } else { if(request.equals("")) return 0; request.split(delimiter, depth); request.reset(); try { // Every protocol built on this (SMTP, POP3, IMAP - RFC 5321 // 2.4, RFC 1939 3, RFC 3501 2.2.1) specifies that command // keywords are case-insensitive, but commands are always // registered here in uppercase and this was an exact-match // lookup - "mail from:" or "Mail From:" (both legal on the // wire, and not unusual from real clients) silently failed // to dispatch at all. Only the lookup key is case-folded; // the token instances handlers actually receive are // untouched, so any handler doing its own case-sensitive // parsing on arguments is unaffected. std::string key = request[0].str(); std::transform(key.begin(), key.end(), key.begin(), [](unsigned char c) { return std::toupper(c); }); auto command = commands.at(key); return command->processCommand(request, session); } catch(...) { return 0; } } return true; } bool CommandList::grabInput(TCPSession &session, Command &command) { session.grab = &command; return true; } void CommandList::clearGrab(TCPSession &session) { session.grab = NULL; } int CommandList::processCommand(coreutils::ZString &request, TCPSession &session) { // The intent was always "typing help lists the available // commands" - this was left as a no-op stub (the commented-out // body even called a Command::getName() that was never added to // the base class) so every registered CommandList - including // the top-level one behind the "help" command itself - silently // did nothing. The map's own key IS the registered name (see // add()), so no getName() is needed at all - just walk it directly. for (auto &pair : commands) session.out << pair.first << std::string("\r\n"); return true; } }