2 * InspIRCd -- Internet Relay Chat Daemon
4 * Copyright (C) 2014 Attila Molnar <attilamolnar@hush.com>
5 * Copyright (C) 2013, 2017-2018 Sadie Powell <sadie@witchery.services>
6 * Copyright (C) 2012, 2019 Robby <robby@chatbelgie.be>
7 * Copyright (C) 2009-2010 Daniel De Graaf <danieldg@inspircd.org>
8 * Copyright (C) 2008 Craig Edwards <brain@inspircd.org>
10 * This file is part of InspIRCd. InspIRCd is free software: you can
11 * redistribute it and/or modify it under the terms of the GNU General Public
12 * License as published by the Free Software Foundation, version 2.
14 * This program is distributed in the hope that it will be useful, but WITHOUT
15 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
19 * You should have received a copy of the GNU General Public License
20 * along with this program. If not, see <http://www.gnu.org/licenses/>.
29 ERR_AMBIGUOUSCOMMAND = 420
32 class ModuleAbbreviation : public Module
35 void Prioritize() CXX11_OVERRIDE
37 ServerInstance->Modules->SetPriority(this, I_OnPreCommand, PRIORITY_FIRST);
40 Version GetVersion() CXX11_OVERRIDE
42 return Version("Allows commands to be abbreviated by appending a full stop.", VF_VENDOR);
45 ModResult OnPreCommand(std::string& command, CommandBase::Params& parameters, LocalUser* user, bool validated) CXX11_OVERRIDE
47 /* Command is already validated, has a length of 0, or last character is not a . */
48 if (validated || command.empty() || *command.rbegin() != '.')
49 return MOD_RES_PASSTHRU;
51 /* Look for any command that starts with the same characters, if it does, replace the command string with it */
52 size_t clen = command.length() - 1;
53 std::string foundcommand, matchlist;
54 bool foundmatch = false;
55 const CommandParser::CommandMap& commands = ServerInstance->Parser.GetCommands();
56 for (CommandParser::CommandMap::const_iterator n = commands.begin(); n != commands.end(); ++n)
58 if (!command.compare(0, clen, n->first, 0, clen))
60 if (matchlist.length() > 450)
62 user->WriteNumeric(ERR_AMBIGUOUSCOMMAND, "Ambiguous abbreviation and too many possible matches.");
68 /* Found the command */
69 foundcommand = n->first;
73 matchlist.append(" ").append(n->first);
77 /* Ambiguous command, list the matches */
78 if (!matchlist.empty())
80 user->WriteNumeric(ERR_AMBIGUOUSCOMMAND, InspIRCd::Format("Ambiguous abbreviation, possible matches: %s%s", foundcommand.c_str(), matchlist.c_str()));
84 if (!foundcommand.empty())
86 command = foundcommand;
89 return MOD_RES_PASSTHRU;
93 MODULE_INIT(ModuleAbbreviation)