]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_abbreviation.cpp
Replace hardcoded mode letters, part 3
[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 init() CXX11_OVERRIDE
26         {
27                 ServerInstance->Modules->Attach(I_OnPreCommand, this);
28         }
29
30         void Prioritize()
31         {
32                 ServerInstance->Modules->SetPriority(this, I_OnPreCommand, PRIORITY_FIRST);
33         }
34
35         Version GetVersion() CXX11_OVERRIDE
36         {
37                 return Version("Provides the ability to abbreviate commands a-la BBC BASIC keywords.",VF_VENDOR);
38         }
39
40         ModResult OnPreCommand(std::string &command, std::vector<std::string> &parameters, LocalUser *user, bool validated, const std::string &original_line) CXX11_OVERRIDE
41         {
42                 /* Command is already validated, has a length of 0, or last character is not a . */
43                 if (validated || command.empty() || *command.rbegin() != '.')
44                         return MOD_RES_PASSTHRU;
45
46                 /* Whack the . off the end */
47                 command.erase(command.end() - 1);
48
49                 /* Look for any command that starts with the same characters, if it does, replace the command string with it */
50                 size_t clen = command.length();
51                 std::string foundcommand, matchlist;
52                 bool foundmatch = false;
53                 for (Commandtable::iterator n = ServerInstance->Parser->cmdlist.begin(); n != ServerInstance->Parser->cmdlist.end(); ++n)
54                 {
55                         if (n->first.length() < clen)
56                                 continue;
57
58                         if (command == n->first.substr(0, clen))
59                         {
60                                 if (matchlist.length() > 450)
61                                 {
62                                         user->WriteNumeric(420, "%s :Ambiguous abbreviation and too many possible matches.", user->nick.c_str());
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(420, "%s :Ambiguous abbreviation, posssible matches: %s%s", user->nick.c_str(), foundcommand.c_str(), matchlist.c_str());
81                         return MOD_RES_DENY;
82                 }
83
84                 if (foundcommand.empty())
85                 {
86                         /* No match, we have to put the . back again so that the invalid command numeric looks correct. */
87                         command += '.';
88                 }
89                 else
90                 {
91                         command = foundcommand;
92                 }
93
94                 return MOD_RES_PASSTHRU;
95         }
96 };
97
98 MODULE_INIT(ModuleAbbreviation)