]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_abbreviation.cpp
Whoops eat command not allow it
[user/henk/code/inspircd.git] / src / modules / m_abbreviation.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2008 InspIRCd Development Team
6  * See: http://www.inspircd.org/wiki/index.php/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(InspIRCd* Me)
24                 : Module(Me)
25         {
26                 Me->Modules->Attach(I_OnPreCommand, this);
27                 /* Must do this first */
28                 Me->Modules->SetPriority(this, I_OnPreCommand, PRIO_FIRST);
29         }
30
31         virtual Version GetVersion()
32         {
33                 return Version(1,2,0,0,VF_VENDOR,API_VERSION);
34         }
35
36         virtual int OnPreCommand(std::string &command, std::vector<std::string> &parameters, User *user, bool validated, const std::string &original_line)
37         {
38                 /* Command is already validated, has a length of 0, or last character is not a . */
39                 if (validated || command.empty() || *command.rbegin() != '.')
40                         return 0;
41
42                 /* Whack the . off the end */
43                 command.erase(command.end() - 1);
44
45                 /* Look for any command that starts with the same characters, if it does, replace the command string with it */
46                 size_t clen = command.length();
47                 std::string foundcommand, matchlist;
48                 size_t matches = 0;
49                 bool foundmatch = false;
50                 for (Commandtable::iterator n = ServerInstance->Parser->cmdlist.begin(); n != ServerInstance->Parser->cmdlist.end(); ++n)
51                 {
52                         if (n->first.length() < clen)
53                                 continue;
54
55                         if (command == n->first.substr(0, clen))
56                         {
57                                 if (++matches > 10)
58                                 {
59                                         user->WriteNumeric(420, "%s :Ambiguous abbreviation and too many possible matches.", user->nick.c_str());
60                                         return true;
61                                 }
62
63                                 if (!foundmatch)
64                                 {
65                                         /* Found the command */
66                                         foundcommand = n->first;
67                                         foundmatch = true;
68                                 }
69                                 else
70                                         matchlist.append(" ").append(n->first);
71                         }
72                 }
73
74                 /* Ambiguous command, list the matches */
75                 if (!matchlist.empty())
76                 {
77                         user->WriteNumeric(420, "%s :Ambiguous abbreviation, posssible matches: %s%s", user->nick.c_str(), foundcommand.c_str(), matchlist.c_str());
78                         return true;
79                 }
80
81                 if (foundcommand.empty())
82                 {
83                         /* No match, we have to put the . back again so that the invalid command numeric looks correct. */
84                         command += '.';
85                 }
86                 else
87                 {
88                         command = foundcommand;
89                 }
90
91                 return false;
92         }
93 };
94
95 MODULE_INIT(ModuleAbbreviation)