]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_abbreviation.cpp
Fix off by one in ping timeout.
[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 class ModuleAbbreviation : public Module
23 {
24  public:
25         void Prioritize()
26         {
27                 ServerInstance->Modules->SetPriority(this, I_OnPreCommand, PRIORITY_FIRST);
28         }
29
30         Version GetVersion() CXX11_OVERRIDE
31         {
32                 return Version("Provides the ability to abbreviate commands a-la BBC BASIC keywords.",VF_VENDOR);
33         }
34
35         ModResult OnPreCommand(std::string &command, std::vector<std::string> &parameters, LocalUser *user, bool validated, const std::string &original_line) CXX11_OVERRIDE
36         {
37                 /* Command is already validated, has a length of 0, or last character is not a . */
38                 if (validated || command.empty() || *command.rbegin() != '.')
39                         return MOD_RES_PASSTHRU;
40
41                 /* Look for any command that starts with the same characters, if it does, replace the command string with it */
42                 size_t clen = command.length() - 1;
43                 std::string foundcommand, matchlist;
44                 bool foundmatch = false;
45                 const CommandParser::CommandMap& commands = ServerInstance->Parser.GetCommands();
46                 for (CommandParser::CommandMap::const_iterator n = commands.begin(); n != commands.end(); ++n)
47                 {
48                         if (!command.compare(0, clen, n->first, 0, clen))
49                         {
50                                 if (matchlist.length() > 450)
51                                 {
52                                         user->WriteNumeric(420, ":Ambiguous abbreviation and too many possible matches.");
53                                         return MOD_RES_DENY;
54                                 }
55
56                                 if (!foundmatch)
57                                 {
58                                         /* Found the command */
59                                         foundcommand = n->first;
60                                         foundmatch = true;
61                                 }
62                                 else
63                                         matchlist.append(" ").append(n->first);
64                         }
65                 }
66
67                 /* Ambiguous command, list the matches */
68                 if (!matchlist.empty())
69                 {
70                         user->WriteNumeric(420, ":Ambiguous abbreviation, posssible matches: %s%s", foundcommand.c_str(), matchlist.c_str());
71                         return MOD_RES_DENY;
72                 }
73
74                 if (!foundcommand.empty())
75                 {
76                         command = foundcommand;
77                 }
78
79                 return MOD_RES_PASSTHRU;
80         }
81 };
82
83 MODULE_INIT(ModuleAbbreviation)