]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_alias.cpp
57c6834afb30304177613260f6233f80760fd54d
[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 : public classbase
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(ServerInstance);
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(InspIRCd* Me)
97                 : Module(Me)
98         {
99                 ReadAliases();
100                 Me->Modules->Attach(I_OnPreCommand, this);
101                 Me->Modules->Attach(I_OnRehash, this);
102                 Me->Modules->Attach(I_OnUserMessage, this);
103
104         }
105
106         virtual ~ModuleAlias()
107         {
108         }
109
110         virtual Version GetVersion()
111         {
112                 return Version("Provides aliases of commands.", VF_VENDOR,API_VERSION);
113         }
114
115         std::string GetVar(std::string varname, const std::string &original_line)
116         {
117                 irc::spacesepstream ss(original_line);
118                 varname.erase(varname.begin());
119                 int index = *(varname.begin()) - 48;
120                 varname.erase(varname.begin());
121                 bool everything_after = (varname == "-");
122                 std::string word;
123
124                 for (int j = 0; j < index; j++)
125                         ss.GetToken(word);
126
127                 if (everything_after)
128                 {
129                         std::string more;
130                         while (ss.GetToken(more))
131                         {
132                                 word.append(" ");
133                                 word.append(more);
134                         }
135                 }
136
137                 return word;
138         }
139
140         virtual ModResult OnPreCommand(std::string &command, std::vector<std::string> &parameters, User *user, bool validated, const std::string &original_line)
141         {
142                 std::multimap<std::string, Alias>::iterator i, upperbound;
143
144                 /* If theyre not registered yet, we dont want
145                  * to know.
146                  */
147                 if (user->registered != REG_ALL)
148                         return MOD_RES_PASSTHRU;
149
150                 /* We dont have any commands looking like this? Stop processing. */
151                 i = Aliases.find(command);
152                 if (i == Aliases.end())
153                         return MOD_RES_PASSTHRU;
154                 /* Avoid iterating on to different aliases if no patterns match. */
155                 upperbound = Aliases.upper_bound(command);
156
157                 irc::string c = command.c_str();
158                 /* The parameters for the command in their original form, with the command stripped off */
159                 std::string compare = original_line.substr(command.length());
160                 while (*(compare.c_str()) == ' ')
161                         compare.erase(compare.begin());
162
163                 while (i != upperbound)
164                 {
165                         if (i->second.UserCommand)
166                         {
167                                 if (DoAlias(user, NULL, &(i->second), compare, original_line))
168                                 {
169                                         return MOD_RES_DENY;
170                                 }
171                         }
172
173                         i++;
174                 }
175
176                 // If we made it here, no aliases actually matched.
177                 return MOD_RES_PASSTHRU;
178         }
179
180         virtual void OnUserMessage(User *user, void *dest, int target_type, const std::string &text, char status, const CUList &exempt_list)
181         {
182                 if (target_type != TYPE_CHANNEL)
183                 {
184                         return;
185                 }
186
187                 // fcommands are only for local users. Spanningtree will send them back out as their original cmd.
188                 if (!user || !IS_LOCAL(user))
189                 {
190                         return;
191                 }
192
193                 /* Stop here if the user is +B and allowbot is set to no. */
194                 if (!AllowBots && user->IsModeSet('B'))
195                 {
196                         return;
197                 }
198
199                 Channel *c = (Channel *)dest;
200                 std::string fcommand;
201
202                 // text is like "!moo cows bite me", we want "!moo" first
203                 irc::spacesepstream ss(text);
204                 ss.GetToken(fcommand);
205
206                 if (fcommand.empty())
207                 {
208                         return; // wtfbbq
209                 }
210
211                 // we don't want to touch non-fantasy stuff
212                 if (*fcommand.c_str() != fprefix)
213                 {
214                         return;
215                 }
216
217                 // nor do we give a shit about the prefix
218                 fcommand.erase(fcommand.begin());
219                 std::transform(fcommand.begin(), fcommand.end(), fcommand.begin(), ::toupper);
220
221                 std::multimap<std::string, Alias>::iterator i = Aliases.find(fcommand);
222
223                 if (i == Aliases.end())
224                         return;
225
226                 /* Avoid iterating on to other aliases if no patterns match */
227                 std::multimap<std::string, Alias>::iterator upperbound = Aliases.upper_bound(fcommand);
228
229
230                 /* The parameters for the command in their original form, with the command stripped off */
231                 std::string compare = text.substr(fcommand.length() + 1);
232                 while (*(compare.c_str()) == ' ')
233                         compare.erase(compare.begin());
234
235                 while (i != upperbound)
236                 {
237                         if (i->second.ChannelCommand)
238                         {
239                                 // We use substr(1) here to remove the fantasy prefix
240                                 if (DoAlias(user, c, &(i->second), compare, text.substr(1)))
241                                         return;
242                         }
243
244                         i++;
245                 }
246         }
247
248
249         int DoAlias(User *user, Channel *c, Alias *a, const std::string compare, const std::string safe)
250         {
251                 User *u = NULL;
252
253                 /* Does it match the pattern? */
254                 if (!a->format.empty())
255                 {
256                         if (a->CaseSensitive)
257                         {
258                                 if (!InspIRCd::Match(compare, a->format, rfc_case_sensitive_map))
259                                         return 0;
260                         }
261                         else
262                         {
263                                 if (!InspIRCd::Match(compare, a->format))
264                                         return 0;
265                         }
266                 }
267
268                 if ((a->OperOnly) && (!IS_OPER(user)))
269                         return 0;
270
271                 if (!a->RequiredNick.empty())
272                 {
273                         u = ServerInstance->FindNick(a->RequiredNick);
274                         if (!u)
275                         {
276                                 user->WriteNumeric(401, ""+std::string(user->nick)+" "+a->RequiredNick+" :is currently unavailable. Please try again later.");
277                                 return 1;
278                         }
279                 }
280                 if ((u != NULL) && (!a->RequiredNick.empty()) && (a->ULineOnly))
281                 {
282                         if (!ServerInstance->ULine(u->server))
283                         {
284                                 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!");
285                                 user->WriteNumeric(401, ""+std::string(user->nick)+" "+a->RequiredNick+" :is an imposter! Please inform an IRC operator as soon as possible.");
286                                 return 1;
287                         }
288                 }
289
290                 /* Now, search and replace in a copy of the original_line, replacing $1 through $9 and $1- etc */
291
292                 std::string::size_type crlf = a->ReplaceFormat.find('\n');
293
294                 if (crlf == std::string::npos)
295                 {
296                         DoCommand(a->ReplaceFormat, user, c, safe);
297                         return 1;
298                 }
299                 else
300                 {
301                         irc::sepstream commands(a->ReplaceFormat, '\n');
302                         std::string scommand;
303                         while (commands.GetToken(scommand))
304                         {
305                                 DoCommand(scommand, user, c, safe);
306                         }
307                         return 1;
308                 }
309         }
310
311         void DoCommand(std::string newline, User* user, Channel *c, const std::string &original_line)
312         {
313                 std::vector<std::string> pars;
314
315                 for (int v = 1; v < 10; v++)
316                 {
317                         std::string var = "$";
318                         var.append(ConvToStr(v));
319                         var.append("-");
320                         std::string::size_type x = newline.find(var);
321
322                         while (x != std::string::npos)
323                         {
324                                 newline.erase(x, var.length());
325                                 newline.insert(x, GetVar(var, original_line));
326                                 x = newline.find(var);
327                         }
328
329                         var = "$";
330                         var.append(ConvToStr(v));
331                         x = newline.find(var);
332
333                         while (x != std::string::npos)
334                         {
335                                 newline.erase(x, var.length());
336                                 newline.insert(x, GetVar(var, original_line));
337                                 x = newline.find(var);
338                         }
339                 }
340
341                 /* Special variables */
342                 SearchAndReplace(newline, std::string("$nick"), user->nick);
343                 SearchAndReplace(newline, std::string("$ident"), user->ident);
344                 SearchAndReplace(newline, std::string("$host"), user->host);
345                 SearchAndReplace(newline, std::string("$vhost"), user->dhost);
346
347                 if (c)
348                 {
349                         /* Channel specific variables */
350                         SearchAndReplace(newline, std::string("$chan"), c->name);
351                 }
352                 else
353                 {
354                         /* We don't want these in a user alias */
355                         SearchAndReplace(newline, std::string("$chan"), std::string(""));
356                 }
357
358                 irc::tokenstream ss(newline);
359                 pars.clear();
360                 std::string command, token;
361
362                 ss.GetToken(command);
363                 while (ss.GetToken(token) && (pars.size() <= MAXPARAMETERS))
364                 {
365                         pars.push_back(token);
366                 }
367                 ServerInstance->Parser->CallHandler(command, pars, user);
368         }
369
370         virtual void OnRehash(User* user)
371         {
372                 ReadAliases();
373         }
374
375         virtual void Prioritize()
376         {
377                 // Prioritise after spanningtree so that channel aliases show the alias before the effects.
378                 Module* linkmod = ServerInstance->Modules->Find("m_spanningtree.so");
379                 ServerInstance->Modules->SetPriority(this, I_OnUserMessage, PRIORITY_AFTER, &linkmod);
380         }
381 };
382
383 MODULE_INIT(ModuleAlias)