2 * InspIRCd -- Internet Relay Chat Daemon
4 * Copyright (C) 2008 Craig Edwards <craigedwards@brainbox.cc>
6 * This file is part of InspIRCd. InspIRCd is free software: you can
7 * redistribute it and/or modify it under the terms of the GNU General Public
8 * License as published by the Free Software Foundation, version 2.
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
25 ERR_AMBIGUOUSCOMMAND = 420
28 class ModuleAbbreviation : public Module
31 void Prioritize() CXX11_OVERRIDE
33 ServerInstance->Modules->SetPriority(this, I_OnPreCommand, PRIORITY_FIRST);
36 Version GetVersion() CXX11_OVERRIDE
38 return Version("Provides the ability to abbreviate commands a-la BBC BASIC keywords.",VF_VENDOR);
41 ModResult OnPreCommand(std::string& command, CommandBase::Params& parameters, LocalUser* user, bool validated) CXX11_OVERRIDE
43 /* Command is already validated, has a length of 0, or last character is not a . */
44 if (validated || command.empty() || *command.rbegin() != '.')
45 return MOD_RES_PASSTHRU;
47 /* Look for any command that starts with the same characters, if it does, replace the command string with it */
48 size_t clen = command.length() - 1;
49 std::string foundcommand, matchlist;
50 bool foundmatch = false;
51 const CommandParser::CommandMap& commands = ServerInstance->Parser.GetCommands();
52 for (CommandParser::CommandMap::const_iterator n = commands.begin(); n != commands.end(); ++n)
54 if (!command.compare(0, clen, n->first, 0, clen))
56 if (matchlist.length() > 450)
58 user->WriteNumeric(ERR_AMBIGUOUSCOMMAND, "Ambiguous abbreviation and too many possible matches.");
64 /* Found the command */
65 foundcommand = n->first;
69 matchlist.append(" ").append(n->first);
73 /* Ambiguous command, list the matches */
74 if (!matchlist.empty())
76 user->WriteNumeric(ERR_AMBIGUOUSCOMMAND, InspIRCd::Format("Ambiguous abbreviation, possible matches: %s%s", foundcommand.c_str(), matchlist.c_str()));
80 if (!foundcommand.empty())
82 command = foundcommand;
85 return MOD_RES_PASSTHRU;
89 MODULE_INIT(ModuleAbbreviation)