]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_abbreviation.cpp
Remove InspIRCd* parameters and fields
[user/henk/code/inspircd.git] / src / modules / m_abbreviation.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2009 InspIRCd Development Team
6  * See: http://wiki.inspircd.org/Credits
7  *
8  * This program is free but copyrighted software; see
9  *            the file COPYING for details.
10  *
11  * ---------------------------------------------------
12  */
13
14 #include "inspircd.h"
15
16 /* $ModDesc: Provides the ability to abbreviate commands a-la BBC BASIC keywords. */
17
18 class ModuleAbbreviation : public Module
19 {
20
21  public:
22
23         ModuleAbbreviation()
24                         {
25                 ServerInstance->Modules->Attach(I_OnPreCommand, this);
26                 /* Must do this first */
27                 ServerInstance->Modules->SetPriority(this, I_OnPreCommand, PRIORITY_FIRST);
28         }
29
30         virtual Version GetVersion()
31         {
32                 return Version("Provides the ability to abbreviate commands a-la BBC BASIC keywords.",VF_VENDOR,API_VERSION);
33         }
34
35         virtual ModResult OnPreCommand(std::string &command, std::vector<std::string> &parameters, User *user, bool validated, const std::string &original_line)
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, "%s :Ambiguous abbreviation and too many possible matches.", user->nick.c_str());
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, "%s :Ambiguous abbreviation, posssible matches: %s%s", user->nick.c_str(), 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)