]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_alias.cpp
Make classbase and refcountbase uncopyable; expand comments on their indended uses
[user/henk/code/inspircd.git] / src / modules / m_alias.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 aliases of commands. */
17
18 /** An alias definition
19  */
20 class Alias
21 {
22  public:
23         /** The text of the alias command */
24         irc::string AliasedCommand;
25
26         /** Text to replace with */
27         std::string ReplaceFormat;
28
29         /** Nickname required to perform alias */
30         std::string RequiredNick;
31
32         /** Alias requires ulined server */
33         bool ULineOnly;
34
35         /** Requires oper? */
36         bool OperOnly;
37
38         /* is case sensitive params */
39         bool CaseSensitive;
40
41         /* whether or not it may be executed via fantasy (default OFF) */
42         bool ChannelCommand;
43
44         /* whether or not it may be executed via /command (default ON) */
45         bool UserCommand;
46
47         /** Format that must be matched for use */
48         std::string format;
49 };
50
51 class ModuleAlias : public Module
52 {
53  private:
54
55         char fprefix;
56
57         /* We cant use a map, there may be multiple aliases with the same name.
58          * We can, however, use a fancy invention: the multimap. Maps a key to one or more values.
59          *              -- w00t
60    */
61         std::multimap<std::string, Alias> Aliases;
62
63         /* whether or not +B users are allowed to use fantasy commands */
64         bool AllowBots;
65
66         virtual void ReadAliases()
67         {
68                 ConfigReader MyConf;
69
70                 AllowBots = MyConf.ReadFlag("fantasy", "allowbots", "no", 0);
71
72                 std::string fpre = MyConf.ReadValue("fantasy","prefix",0);
73                 fprefix = fpre.empty() ? '!' : fpre[0];
74
75                 Aliases.clear();
76                 for (int i = 0; i < MyConf.Enumerate("alias"); i++)
77                 {
78                         Alias a;
79                         std::string txt;
80                         txt = MyConf.ReadValue("alias", "text", i);
81                         a.AliasedCommand = txt.c_str();
82                         a.ReplaceFormat = MyConf.ReadValue("alias", "replace", i, true);
83                         a.RequiredNick = MyConf.ReadValue("alias", "requires", i);
84                         a.ULineOnly = MyConf.ReadFlag("alias", "uline", i);
85                         a.ChannelCommand = MyConf.ReadFlag("alias", "channelcommand", "no", i);
86                         a.UserCommand = MyConf.ReadFlag("alias", "usercommand", "yes", i);
87                         a.OperOnly = MyConf.ReadFlag("alias", "operonly", i);
88                         a.format = MyConf.ReadValue("alias", "format", i);
89                         a.CaseSensitive = MyConf.ReadFlag("alias", "matchcase", i);
90                         Aliases.insert(std::make_pair(txt, a));
91                 }
92         }
93
94  public:
95
96         ModuleAlias()
97                         {
98                 ReadAliases();
99                 ServerInstance->Modules->Attach(I_OnPreCommand, this);
100                 ServerInstance->Modules->Attach(I_OnRehash, this);
101                 ServerInstance->Modules->Attach(I_OnUserMessage, this);
102
103         }
104
105         virtual ~ModuleAlias()
106         {
107         }
108
109         virtual Version GetVersion()
110         {
111                 return Version("Provides aliases of commands.", VF_VENDOR);
112         }
113
114         std::string GetVar(std::string varname, const std::string &original_line)
115         {
116                 irc::spacesepstream ss(original_line);
117                 varname.erase(varname.begin());
118                 int index = *(varname.begin()) - 48;
119                 varname.erase(varname.begin());
120                 bool everything_after = (varname == "-");
121                 std::string word;
122
123                 for (int j = 0; j < index; j++)
124                         ss.GetToken(word);
125
126                 if (everything_after)
127                 {
128                         std::string more;
129                         while (ss.GetToken(more))
130                         {
131                                 word.append(" ");
132                                 word.append(more);
133                         }
134                 }
135
136                 return word;
137         }
138
139         virtual ModResult OnPreCommand(std::string &command, std::vector<std::string> &parameters, User *user, bool validated, const std::string &original_line)
140         {
141                 std::multimap<std::string, Alias>::iterator i, upperbound;
142
143                 /* If theyre not registered yet, we dont want
144                  * to know.
145                  */
146                 if (user->registered != REG_ALL)
147                         return MOD_RES_PASSTHRU;
148
149                 /* We dont have any commands looking like this? Stop processing. */
150                 i = Aliases.find(command);
151                 if (i == Aliases.end())
152                         return MOD_RES_PASSTHRU;
153                 /* Avoid iterating on to different aliases if no patterns match. */
154                 upperbound = Aliases.upper_bound(command);
155
156                 irc::string c = command.c_str();
157                 /* The parameters for the command in their original form, with the command stripped off */
158                 std::string compare = original_line.substr(command.length());
159                 while (*(compare.c_str()) == ' ')
160                         compare.erase(compare.begin());
161
162                 while (i != upperbound)
163                 {
164                         if (i->second.UserCommand)
165                         {
166                                 if (DoAlias(user, NULL, &(i->second), compare, original_line))
167                                 {
168                                         return MOD_RES_DENY;
169                                 }
170                         }
171
172                         i++;
173                 }
174
175                 // If we made it here, no aliases actually matched.
176                 return MOD_RES_PASSTHRU;
177         }
178
179         virtual void OnUserMessage(User *user, void *dest, int target_type, const std::string &text, char status, const CUList &exempt_list)
180         {
181                 if (target_type != TYPE_CHANNEL)
182                 {
183                         return;
184                 }
185
186                 // fcommands are only for local users. Spanningtree will send them back out as their original cmd.
187                 if (!user || !IS_LOCAL(user))
188                 {
189                         return;
190                 }
191
192                 /* Stop here if the user is +B and allowbot is set to no. */
193                 if (!AllowBots && user->IsModeSet('B'))
194                 {
195                         return;
196                 }
197
198                 Channel *c = (Channel *)dest;
199                 std::string fcommand;
200
201                 // text is like "!moo cows bite me", we want "!moo" first
202                 irc::spacesepstream ss(text);
203                 ss.GetToken(fcommand);
204
205                 if (fcommand.empty())
206                 {
207                         return; // wtfbbq
208                 }
209
210                 // we don't want to touch non-fantasy stuff
211                 if (*fcommand.c_str() != fprefix)
212                 {
213                         return;
214                 }
215
216                 // nor do we give a shit about the prefix
217                 fcommand.erase(fcommand.begin());
218                 std::transform(fcommand.begin(), fcommand.end(), fcommand.begin(), ::toupper);
219
220                 std::multimap<std::string, Alias>::iterator i = Aliases.find(fcommand);
221
222                 if (i == Aliases.end())
223                         return;
224
225                 /* Avoid iterating on to other aliases if no patterns match */
226                 std::multimap<std::string, Alias>::iterator upperbound = Aliases.upper_bound(fcommand);
227
228
229                 /* The parameters for the command in their original form, with the command stripped off */
230                 std::string compare = text.substr(fcommand.length() + 1);
231                 while (*(compare.c_str()) == ' ')
232                         compare.erase(compare.begin());
233
234                 while (i != upperbound)
235                 {
236                         if (i->second.ChannelCommand)
237                         {
238                                 // We use substr(1) here to remove the fantasy prefix
239                                 if (DoAlias(user, c, &(i->second), compare, text.substr(1)))
240                                         return;
241                         }
242
243                         i++;
244                 }
245         }
246
247
248         int DoAlias(User *user, Channel *c, Alias *a, const std::string compare, const std::string safe)
249         {
250                 User *u = NULL;
251
252                 /* Does it match the pattern? */
253                 if (!a->format.empty())
254                 {
255                         if (a->CaseSensitive)
256                         {
257                                 if (!InspIRCd::Match(compare, a->format, rfc_case_sensitive_map))
258                                         return 0;
259                         }
260                         else
261                         {
262                                 if (!InspIRCd::Match(compare, a->format))
263                                         return 0;
264                         }
265                 }
266
267                 if ((a->OperOnly) && (!IS_OPER(user)))
268                         return 0;
269
270                 if (!a->RequiredNick.empty())
271                 {
272                         u = ServerInstance->FindNick(a->RequiredNick);
273                         if (!u)
274                         {
275                                 user->WriteNumeric(401, ""+std::string(user->nick)+" "+a->RequiredNick+" :is currently unavailable. Please try again later.");
276                                 return 1;
277                         }
278                 }
279                 if ((u != NULL) && (!a->RequiredNick.empty()) && (a->ULineOnly))
280                 {
281                         if (!ServerInstance->ULine(u->server))
282                         {
283                                 ServerInstance->SNO->WriteToSnoMask('a', "NOTICE -- Service "+a->RequiredNick+" required by alias "+std::string(a->AliasedCommand.c_str())+" is not on a u-lined server, possibly underhanded antics detected!");
284                                 user->WriteNumeric(401, ""+std::string(user->nick)+" "+a->RequiredNick+" :is an imposter! Please inform an IRC operator as soon as possible.");
285                                 return 1;
286                         }
287                 }
288
289                 /* Now, search and replace in a copy of the original_line, replacing $1 through $9 and $1- etc */
290
291                 std::string::size_type crlf = a->ReplaceFormat.find('\n');
292
293                 if (crlf == std::string::npos)
294                 {
295                         DoCommand(a->ReplaceFormat, user, c, safe);
296                         return 1;
297                 }
298                 else
299                 {
300                         irc::sepstream commands(a->ReplaceFormat, '\n');
301                         std::string scommand;
302                         while (commands.GetToken(scommand))
303                         {
304                                 DoCommand(scommand, user, c, safe);
305                         }
306                         return 1;
307                 }
308         }
309
310         void DoCommand(std::string newline, User* user, Channel *c, const std::string &original_line)
311         {
312                 std::vector<std::string> pars;
313
314                 for (int v = 1; v < 10; v++)
315                 {
316                         std::string var = "$";
317                         var.append(ConvToStr(v));
318                         var.append("-");
319                         std::string::size_type x = newline.find(var);
320
321                         while (x != std::string::npos)
322                         {
323                                 newline.erase(x, var.length());
324                                 newline.insert(x, GetVar(var, original_line));
325                                 x = newline.find(var);
326                         }
327
328                         var = "$";
329                         var.append(ConvToStr(v));
330                         x = newline.find(var);
331
332                         while (x != std::string::npos)
333                         {
334                                 newline.erase(x, var.length());
335                                 newline.insert(x, GetVar(var, original_line));
336                                 x = newline.find(var);
337                         }
338                 }
339
340                 /* Special variables */
341                 SearchAndReplace(newline, std::string("$nick"), user->nick);
342                 SearchAndReplace(newline, std::string("$ident"), user->ident);
343                 SearchAndReplace(newline, std::string("$host"), user->host);
344                 SearchAndReplace(newline, std::string("$vhost"), user->dhost);
345
346                 if (c)
347                 {
348                         /* Channel specific variables */
349                         SearchAndReplace(newline, std::string("$chan"), c->name);
350                 }
351                 else
352                 {
353                         /* We don't want these in a user alias */
354                         SearchAndReplace(newline, std::string("$chan"), std::string(""));
355                 }
356
357                 irc::tokenstream ss(newline);
358                 pars.clear();
359                 std::string command, token;
360
361                 ss.GetToken(command);
362                 while (ss.GetToken(token) && (pars.size() <= MAXPARAMETERS))
363                 {
364                         pars.push_back(token);
365                 }
366                 ServerInstance->Parser->CallHandler(command, pars, user);
367         }
368
369         virtual void OnRehash(User* user)
370         {
371                 ReadAliases();
372         }
373
374         virtual void Prioritize()
375         {
376                 // Prioritise after spanningtree so that channel aliases show the alias before the effects.
377                 Module* linkmod = ServerInstance->Modules->Find("m_spanningtree.so");
378                 ServerInstance->Modules->SetPriority(this, I_OnUserMessage, PRIORITY_AFTER, &linkmod);
379         }
380 };
381
382 MODULE_INIT(ModuleAlias)