]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_abbreviation.cpp
Merge insp20
[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                 /* Whack the . off the end */
42                 command.erase(command.end() - 1);
43
44                 /* Look for any command that starts with the same characters, if it does, replace the command string with it */
45                 size_t clen = command.length();
46                 std::string foundcommand, matchlist;
47                 bool foundmatch = false;
48                 for (Commandtable::iterator n = ServerInstance->Parser->cmdlist.begin(); n != ServerInstance->Parser->cmdlist.end(); ++n)
49                 {
50                         if (n->first.length() < clen)
51                                 continue;
52
53                         if (command == n->first.substr(0, clen))
54                         {
55                                 if (matchlist.length() > 450)
56                                 {
57                                         user->WriteNumeric(420, ":Ambiguous abbreviation and too many possible matches.");
58                                         return MOD_RES_DENY;
59                                 }
60
61                                 if (!foundmatch)
62                                 {
63                                         /* Found the command */
64                                         foundcommand = n->first;
65                                         foundmatch = true;
66                                 }
67                                 else
68                                         matchlist.append(" ").append(n->first);
69                         }
70                 }
71
72                 /* Ambiguous command, list the matches */
73                 if (!matchlist.empty())
74                 {
75                         user->WriteNumeric(420, ":Ambiguous abbreviation, posssible matches: %s%s", foundcommand.c_str(), matchlist.c_str());
76                         return MOD_RES_DENY;
77                 }
78
79                 if (foundcommand.empty())
80                 {
81                         /* No match, we have to put the . back again so that the invalid command numeric looks correct. */
82                         command += '.';
83                 }
84                 else
85                 {
86                         command = foundcommand;
87                 }
88
89                 return MOD_RES_PASSTHRU;
90         }
91 };
92
93 MODULE_INIT(ModuleAbbreviation)