]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_abbreviation.cpp
Sync helpop chmodes s and p with docs
[user/henk/code/inspircd.git] / src / modules / m_abbreviation.cpp
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
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>
9  *
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.
13  *
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
17  * details.
18  *
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/>.
21  */
22
23
24 #include "inspircd.h"
25
26 enum
27 {
28         // InspIRCd-specific.
29         ERR_AMBIGUOUSCOMMAND = 420
30 };
31
32 class ModuleAbbreviation : public Module
33 {
34  public:
35         void Prioritize() CXX11_OVERRIDE
36         {
37                 ServerInstance->Modules->SetPriority(this, I_OnPreCommand, PRIORITY_FIRST);
38         }
39
40         Version GetVersion() CXX11_OVERRIDE
41         {
42                 return Version("Allows commands to be abbreviated by appending a full stop.", VF_VENDOR);
43         }
44
45         ModResult OnPreCommand(std::string& command, CommandBase::Params& parameters, LocalUser* user, bool validated) CXX11_OVERRIDE
46         {
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;
50
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)
57                 {
58                         if (!command.compare(0, clen, n->first, 0, clen))
59                         {
60                                 if (matchlist.length() > 450)
61                                 {
62                                         user->WriteNumeric(ERR_AMBIGUOUSCOMMAND, "Ambiguous abbreviation and too many possible matches.");
63                                         return MOD_RES_DENY;
64                                 }
65
66                                 if (!foundmatch)
67                                 {
68                                         /* Found the command */
69                                         foundcommand = n->first;
70                                         foundmatch = true;
71                                 }
72                                 else
73                                         matchlist.append(" ").append(n->first);
74                         }
75                 }
76
77                 /* Ambiguous command, list the matches */
78                 if (!matchlist.empty())
79                 {
80                         user->WriteNumeric(ERR_AMBIGUOUSCOMMAND, InspIRCd::Format("Ambiguous abbreviation, possible matches: %s%s", foundcommand.c_str(), matchlist.c_str()));
81                         return MOD_RES_DENY;
82                 }
83
84                 if (!foundcommand.empty())
85                 {
86                         command = foundcommand;
87                 }
88
89                 return MOD_RES_PASSTHRU;
90         }
91 };
92
93 MODULE_INIT(ModuleAbbreviation)