]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_abbreviation.cpp
c81caf1429cc6b9f39d380d426f4ff96dc4a63fd
[user/henk/code/inspircd.git] / src / modules / m_abbreviation.cpp
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2008 Craig Edwards <craigedwards@brainbox.cc>
5  *
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.
9  *
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
13  * details.
14  *
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/>.
17  */
18
19
20 #include "inspircd.h"
21
22 enum
23 {
24         // InspIRCd-specific.
25         ERR_AMBIGUOUSCOMMAND = 420
26 };
27
28 class ModuleAbbreviation : public Module
29 {
30  public:
31         void Prioritize() CXX11_OVERRIDE
32         {
33                 ServerInstance->Modules->SetPriority(this, I_OnPreCommand, PRIORITY_FIRST);
34         }
35
36         Version GetVersion() CXX11_OVERRIDE
37         {
38                 return Version("Provides the ability to abbreviate commands a-la BBC BASIC keywords.",VF_VENDOR);
39         }
40
41         ModResult OnPreCommand(std::string &command, std::vector<std::string> &parameters, LocalUser *user, bool validated, const std::string &original_line) CXX11_OVERRIDE
42         {
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;
46
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)
53                 {
54                         if (!command.compare(0, clen, n->first, 0, clen))
55                         {
56                                 if (matchlist.length() > 450)
57                                 {
58                                         user->WriteNumeric(ERR_AMBIGUOUSCOMMAND, "Ambiguous abbreviation and too many possible matches.");
59                                         return MOD_RES_DENY;
60                                 }
61
62                                 if (!foundmatch)
63                                 {
64                                         /* Found the command */
65                                         foundcommand = n->first;
66                                         foundmatch = true;
67                                 }
68                                 else
69                                         matchlist.append(" ").append(n->first);
70                         }
71                 }
72
73                 /* Ambiguous command, list the matches */
74                 if (!matchlist.empty())
75                 {
76                         user->WriteNumeric(ERR_AMBIGUOUSCOMMAND, InspIRCd::Format("Ambiguous abbreviation, possible matches: %s%s", foundcommand.c_str(), matchlist.c_str()));
77                         return MOD_RES_DENY;
78                 }
79
80                 if (!foundcommand.empty())
81                 {
82                         command = foundcommand;
83                 }
84
85                 return MOD_RES_PASSTHRU;
86         }
87 };
88
89 MODULE_INIT(ModuleAbbreviation)