]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_abbreviation.cpp
Release v2.0.19
[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 /* $ModDesc: Provides the ability to abbreviate commands a-la BBC BASIC keywords. */
23
24 class ModuleAbbreviation : public Module
25 {
26  public:
27         void init()
28         {
29                 ServerInstance->Modules->Attach(I_OnPreCommand, this);
30         }
31
32         void Prioritize()
33         {
34                 ServerInstance->Modules->SetPriority(this, I_OnPreCommand, PRIORITY_FIRST);
35         }
36
37         virtual Version GetVersion()
38         {
39                 return Version("Provides the ability to abbreviate commands a-la BBC BASIC keywords.",VF_VENDOR);
40         }
41
42         virtual ModResult OnPreCommand(std::string &command, std::vector<std::string> &parameters, LocalUser *user, bool validated, const std::string &original_line)
43         {
44                 /* Command is already validated, has a length of 0, or last character is not a . */
45                 if (validated || command.empty() || *command.rbegin() != '.')
46                         return MOD_RES_PASSTHRU;
47
48                 /* Whack the . off the end */
49                 command.erase(command.end() - 1);
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();
53                 std::string foundcommand, matchlist;
54                 bool foundmatch = false;
55                 for (Commandtable::iterator n = ServerInstance->Parser->cmdlist.begin(); n != ServerInstance->Parser->cmdlist.end(); ++n)
56                 {
57                         if (n->first.length() < clen)
58                                 continue;
59
60                         if (command == n->first.substr(0, clen))
61                         {
62                                 if (matchlist.length() > 450)
63                                 {
64                                         user->WriteNumeric(420, "%s :Ambiguous abbreviation and too many possible matches.", user->nick.c_str());
65                                         return MOD_RES_DENY;
66                                 }
67
68                                 if (!foundmatch)
69                                 {
70                                         /* Found the command */
71                                         foundcommand = n->first;
72                                         foundmatch = true;
73                                 }
74                                 else
75                                         matchlist.append(" ").append(n->first);
76                         }
77                 }
78
79                 /* Ambiguous command, list the matches */
80                 if (!matchlist.empty())
81                 {
82                         user->WriteNumeric(420, "%s :Ambiguous abbreviation, possible matches: %s%s", user->nick.c_str(), foundcommand.c_str(), matchlist.c_str());
83                         return MOD_RES_DENY;
84                 }
85
86                 if (foundcommand.empty())
87                 {
88                         /* No match, we have to put the . back again so that the invalid command numeric looks correct. */
89                         command += '.';
90                 }
91                 else
92                 {
93                         command = foundcommand;
94                 }
95
96                 return MOD_RES_PASSTHRU;
97         }
98 };
99
100 MODULE_INIT(ModuleAbbreviation)