#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) { 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; } }