]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_alias.cpp
Restructuring to m_alias, this will be used for (optional) fantasy commands.
[user/henk/code/inspircd.git] / src / modules / m_alias.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 aliases of commands. */
17
18 /** An alias definition
19  */
20 class Alias : public classbase
21 {
22  public:
23         /** The text of the alias command */
24         irc::string text;
25         /** Text to replace with */
26         std::string replace_with;
27         /** Nickname required to perform alias */
28         std::string requires;
29         /** Alias requires ulined server */
30         bool uline;
31         /** Requires oper? */
32         bool operonly;
33         /* is case sensitive params */
34         bool case_sensitive;
35         /** Format that must be matched for use */
36         std::string format;
37 };
38
39 class ModuleAlias : public Module
40 {
41  private:
42         /* We cant use a map, there may be multiple aliases with the same name.
43          * We can, however, use a fancy invention: the multimap. Maps a key to one or more values.
44          *              -- w00t
45    */
46         std::multimap<std::string, Alias> Aliases;
47
48         virtual void ReadAliases()
49         {
50                 ConfigReader MyConf(ServerInstance);
51
52                 Aliases.clear();
53                 for (int i = 0; i < MyConf.Enumerate("alias"); i++)
54                 {
55                         Alias a;
56                         std::string txt;
57                         txt = MyConf.ReadValue("alias", "text", i);
58                         a.text = txt.c_str();
59                         a.replace_with = MyConf.ReadValue("alias", "replace", i, true);
60                         a.requires = MyConf.ReadValue("alias", "requires", i);
61                         a.uline = MyConf.ReadFlag("alias", "uline", i);
62                         a.operonly = MyConf.ReadFlag("alias", "operonly", i);
63                         a.format = MyConf.ReadValue("alias", "format", i);
64                         a.case_sensitive = MyConf.ReadFlag("alias", "matchcase", i);
65                         Aliases.insert(std::make_pair(txt, a));
66                 }
67         }
68
69  public:
70
71         ModuleAlias(InspIRCd* Me)
72                 : Module(Me)
73         {
74                 ReadAliases();
75                 Me->Modules->Attach(I_OnPreCommand, this);
76                 Me->Modules->Attach(I_OnRehash, this);
77
78         }
79
80         virtual ~ModuleAlias()
81         {
82         }
83
84         virtual Version GetVersion()
85         {
86                 return Version("$Id$", VF_VENDOR,API_VERSION);
87         }
88
89         std::string GetVar(std::string varname, const std::string &original_line)
90         {
91                 irc::spacesepstream ss(original_line);
92                 varname.erase(varname.begin());
93                 int index = *(varname.begin()) - 48;
94                 varname.erase(varname.begin());
95                 bool everything_after = (varname == "-");
96                 std::string word;
97
98                 for (int j = 0; j < index; j++)
99                         ss.GetToken(word);
100
101                 if (everything_after)
102                 {
103                         std::string more;
104                         while (ss.GetToken(more))
105                         {
106                                 word.append(" ");
107                                 word.append(more);
108                         }
109                 }
110
111                 return word;
112         }
113
114         void SearchAndReplace(std::string& newline, const std::string &find, const std::string &replace)
115         {
116                 std::string::size_type x = newline.find(find);
117                 while (x != std::string::npos)
118                 {
119                         newline.erase(x, find.length());
120                         newline.insert(x, replace);
121                         x = newline.find(find);
122                 }
123         }
124
125         virtual int OnPreCommand(std::string &command, std::vector<std::string> &parameters, User *user, bool validated, const std::string &original_line)
126         {
127                 std::multimap<std::string, Alias>::iterator i;
128
129                 /* If theyre not registered yet, we dont want
130                  * to know.
131                  */
132                 if (user->registered != REG_ALL)
133                         return 0;
134
135                 /* We dont have any commands looking like this? Stop processing. */
136                 i = Aliases.find(command);
137                 if (i == Aliases.end())
138                         return 0;
139
140                 irc::string c = command.c_str();
141                 /* The parameters for the command in their original form, with the command stripped off */
142                 std::string compare = original_line.substr(command.length());
143                 while (*(compare.c_str()) == ' ')
144                         compare.erase(compare.begin());
145
146                 std::string safe(original_line);
147
148                 /* Escape out any $ symbols in the user provided text */
149
150                 SearchAndReplace(safe, "$", "\r");
151
152                 while (i != Aliases.end())
153                 {
154                         DoAlias(user, &(i->second), compare, safe);
155
156                         i++;
157                 }
158
159                 // If aliases have been processed, aliases took it.
160                 return 1;
161         }
162
163         void DoAlias(User *user, Alias *a, const std::string compare, const std::string safe)
164         {
165                 User *u = NULL;
166                 /* Does it match the pattern? */
167                 if (!a->format.empty())
168                 {
169                         if (a->case_sensitive)
170                         {
171                                 if (InspIRCd::Match(compare, a->format, case_sensitive_map))
172                                         return;
173                         }
174                         else
175                         {
176                                 if (InspIRCd::Match(compare, a->format))
177                                         return;
178                         }
179                 }
180
181                 if ((a->operonly) && (!IS_OPER(user)))
182                         return;
183
184                 if (!a->requires.empty())
185                 {
186                         u = ServerInstance->FindNick(a->requires);
187                         if (!u)
188                         {
189                                 user->WriteNumeric(401, ""+std::string(user->nick)+" "+a->requires+" :is currently unavailable. Please try again later.");
190                                 return;
191                         }
192                 }
193                 if ((u != NULL) && (!a->requires.empty()) && (a->uline))
194                 {
195                         if (!ServerInstance->ULine(u->server))
196                         {
197                                 ServerInstance->SNO->WriteToSnoMask('A', "NOTICE -- Service "+a->requires+" required by alias "+std::string(a->text.c_str())+" is not on a u-lined server, possibly underhanded antics detected!");
198                                 user->WriteNumeric(401, ""+std::string(user->nick)+" "+a->requires+" :is an imposter! Please inform an IRC operator as soon as possible.");
199                                 return;
200                         }
201                 }
202
203                 /* Now, search and replace in a copy of the original_line, replacing $1 through $9 and $1- etc */
204
205                 std::string::size_type crlf = a->replace_with.find('\n');
206
207                 if (crlf == std::string::npos)
208                 {
209                         DoCommand(a->replace_with, user, safe);
210                         return;
211                 }
212                 else
213                 {
214                         irc::sepstream commands(a->replace_with, '\n');
215                         std::string scommand;
216                         while (commands.GetToken(scommand))
217                         {
218                                 DoCommand(scommand, user, safe);
219                         }
220                         return;
221                 }
222         }
223
224         void DoCommand(std::string newline, User* user, const std::string &original_line)
225         {
226                 std::vector<std::string> pars;
227
228                 for (int v = 1; v < 10; v++)
229                 {
230                         std::string var = "$";
231                         var.append(ConvToStr(v));
232                         var.append("-");
233                         std::string::size_type x = newline.find(var);
234
235                         while (x != std::string::npos)
236                         {
237                                 newline.erase(x, var.length());
238                                 newline.insert(x, GetVar(var, original_line));
239                                 x = newline.find(var);
240                         }
241
242                         var = "$";
243                         var.append(ConvToStr(v));
244                         x = newline.find(var);
245
246                         while (x != std::string::npos)
247                         {
248                                 newline.erase(x, var.length());
249                                 newline.insert(x, GetVar(var, original_line));
250                                 x = newline.find(var);
251                         }
252                 }
253
254                 /* Special variables */
255                 SearchAndReplace(newline, "$nick", user->nick);
256                 SearchAndReplace(newline, "$ident", user->ident);
257                 SearchAndReplace(newline, "$host", user->host);
258                 SearchAndReplace(newline, "$vhost", user->dhost);
259
260                 /* Unescape any variable names in the user text before sending */
261                 SearchAndReplace(newline, "\r", "$");
262
263                 irc::tokenstream ss(newline);
264                 pars.clear();
265                 std::string command, token;
266
267                 ss.GetToken(command);
268                 while (ss.GetToken(token) && (pars.size() <= MAXPARAMETERS))
269                 {
270                         pars.push_back(token);
271                 }
272                 ServerInstance->Parser->CallHandler(command, pars, user);
273         }
274
275         virtual void OnRehash(User* user, const std::string &parameter)
276         {
277                 ReadAliases();
278         }
279 };
280
281 MODULE_INIT(ModuleAlias)