]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_alias.cpp
62e091bb65fa00836ee396f312d7520315ba0081
[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 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         /* We cant use a map, there may be multiple aliases with the same name.
55          * We can, however, use a fancy invention: the multimap. Maps a key to one or more values.
56          *              -- w00t
57    */
58         std::multimap<std::string, Alias> Aliases;
59
60         virtual void ReadAliases()
61         {
62                 ConfigReader MyConf(ServerInstance);
63
64                 Aliases.clear();
65                 for (int i = 0; i < MyConf.Enumerate("alias"); i++)
66                 {
67                         Alias a;
68                         std::string txt;
69                         txt = MyConf.ReadValue("alias", "text", i);
70                         a.AliasedCommand = txt.c_str();
71                         a.ReplaceFormat = MyConf.ReadValue("alias", "replace", i, true);
72                         a.RequiredNick = MyConf.ReadValue("alias", "requires", i);
73                         a.ULineOnly = MyConf.ReadFlag("alias", "uline", i);
74                         a.ChannelCommand = MyConf.ReadFlag("alias", "channelcommand", "no", i);
75                         a.UserCommand = MyConf.ReadFlag("alias", "usercommand", "yes", i);
76                         a.OperOnly = MyConf.ReadFlag("alias", "operonly", i);
77                         a.format = MyConf.ReadValue("alias", "format", i);
78                         a.CaseSensitive = MyConf.ReadFlag("alias", "matchcase", i);
79                         Aliases.insert(std::make_pair(txt, a));
80                 }
81         }
82
83  public:
84
85         ModuleAlias(InspIRCd* Me)
86                 : Module(Me)
87         {
88                 ReadAliases();
89                 Me->Modules->Attach(I_OnPreCommand, this);
90                 Me->Modules->Attach(I_OnRehash, this);
91                 Me->Modules->Attach(I_OnUserPreMessage, this);
92
93         }
94
95         virtual ~ModuleAlias()
96         {
97         }
98
99         virtual Version GetVersion()
100         {
101                 return Version("$Id$", VF_VENDOR,API_VERSION);
102         }
103
104         std::string GetVar(std::string varname, const std::string &original_line)
105         {
106                 irc::spacesepstream ss(original_line);
107                 varname.erase(varname.begin());
108                 int index = *(varname.begin()) - 48;
109                 varname.erase(varname.begin());
110                 bool everything_after = (varname == "-");
111                 std::string word;
112
113                 for (int j = 0; j < index; j++)
114                         ss.GetToken(word);
115
116                 if (everything_after)
117                 {
118                         std::string more;
119                         while (ss.GetToken(more))
120                         {
121                                 word.append(" ");
122                                 word.append(more);
123                         }
124                 }
125
126                 return word;
127         }
128
129         void SearchAndReplace(std::string& newline, const std::string &find, const std::string &replace)
130         {
131                 std::string::size_type x = newline.find(find);
132                 while (x != std::string::npos)
133                 {
134                         newline.erase(x, find.length());
135                         newline.insert(x, replace);
136                         x = newline.find(find);
137                 }
138         }
139
140         virtual int 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;
143
144                 /* If theyre not registered yet, we dont want
145                  * to know.
146                  */
147                 if (user->registered != REG_ALL)
148                         return 0;
149
150                 /* We dont have any commands looking like this? Stop processing. */
151                 i = Aliases.find(command);
152                 if (i == Aliases.end())
153                         return 0;
154
155                 irc::string c = command.c_str();
156                 /* The parameters for the command in their original form, with the command stripped off */
157                 std::string compare = original_line.substr(command.length());
158                 while (*(compare.c_str()) == ' ')
159                         compare.erase(compare.begin());
160
161                 std::string safe(original_line);
162
163                 /* Escape out any $ symbols in the user provided text */
164
165                 SearchAndReplace(safe, "$", "\r");
166
167                 while (i != Aliases.end())
168                 {
169                         if (i->second.UserCommand)
170                                 DoAlias(user, NULL, &(i->second), compare, safe);
171
172                         i++;
173                 }
174
175                 // If aliases have been processed, aliases took it.
176                 return 1;
177         }
178
179         virtual int OnUserPreMessage(User *user, void *dest, int target_type, std::string &text, char status, CUList &exempt_list)
180         {
181                 if (target_type != TYPE_CHANNEL)
182                 {
183                         ServerInstance->Logs->Log("FANTASY", DEBUG, "fantasy: not a channel msg");
184                         return 0;
185                 }
186
187                 // fcommands are only for local users. Spanningtree will send them back out as their original cmd.
188                 if (!IS_LOCAL(user))
189                 {
190                         ServerInstance->Logs->Log("FANTASY", DEBUG, "fantasy: not local");
191                         return 0;
192                 }
193
194                 Channel *c = (Channel *)dest;
195                 std::string fcommand;
196
197                 // text is like "!moo cows bite me", we want "!moo" first
198                 irc::spacesepstream ss(text);
199                 ss.GetToken(fcommand);
200
201                 if (fcommand.empty())
202                 {
203                         ServerInstance->Logs->Log("FANTASY", DEBUG, "fantasy: empty (?)");
204                         return 0; // wtfbbq
205                 }
206
207                 ServerInstance->Logs->Log("FANTASY", DEBUG, "fantasy: looking at fcommand %s", fcommand.c_str());
208
209                 // we don't want to touch non-fantasy stuff
210                 if (*fcommand.c_str() != '!')
211                 {
212                         ServerInstance->Logs->Log("FANTASY", DEBUG, "fantasy: not a fcommand");
213                         return 0;
214                 }
215
216                 // nor do we give a shit about the !
217                 fcommand.erase(fcommand.begin());
218                 std::transform(fcommand.begin(), fcommand.end(), fcommand.begin(), ::toupper);
219                 ServerInstance->Logs->Log("FANTASY", DEBUG, "fantasy: now got %s", fcommand.c_str());
220
221
222                 std::multimap<std::string, Alias>::iterator i = Aliases.find(fcommand);
223
224                 if (i == Aliases.end())
225                         return 0;
226
227
228                 /* The parameters for the command in their original form, with the command stripped off */
229                 std::string compare = text.substr(fcommand.length() + 1);
230                 while (*(compare.c_str()) == ' ')
231                         compare.erase(compare.begin());
232
233                 std::string safe(compare);
234
235                 /* Escape out any $ symbols in the user provided text (ugly, but better than crashy) */
236                 SearchAndReplace(safe, "$", "\r");
237
238                 ServerInstance->Logs->Log("FANTASY", DEBUG, "fantasy: compare is %s and safe is %s", compare.c_str(), safe.c_str());
239
240                 while (i != Aliases.end())
241                 {
242                         if (i->second.ChannelCommand)
243                                 DoAlias(user, c, &(i->second), compare, safe);
244
245                         i++;
246                 }
247                 
248                 return 0;
249         }
250
251
252         void DoAlias(User *user, Channel *c, Alias *a, const std::string compare, const std::string safe)
253         {
254                 User *u = NULL;
255                 /* Does it match the pattern? */
256                 if (!a->format.empty())
257                 {
258                         if (a->CaseSensitive)
259                         {
260                                 if (InspIRCd::Match(compare, a->format, case_sensitive_map))
261                                         return;
262                         }
263                         else
264                         {
265                                 if (InspIRCd::Match(compare, a->format))
266                                         return;
267                         }
268                 }
269
270                 if ((a->OperOnly) && (!IS_OPER(user)))
271                         return;
272
273                 if (!a->RequiredNick.empty())
274                 {
275                         u = ServerInstance->FindNick(a->RequiredNick);
276                         if (!u)
277                         {
278                                 user->WriteNumeric(401, ""+std::string(user->nick)+" "+a->RequiredNick+" :is currently unavailable. Please try again later.");
279                                 return;
280                         }
281                 }
282                 if ((u != NULL) && (!a->RequiredNick.empty()) && (a->ULineOnly))
283                 {
284                         if (!ServerInstance->ULine(u->server))
285                         {
286                                 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!");
287                                 user->WriteNumeric(401, ""+std::string(user->nick)+" "+a->RequiredNick+" :is an imposter! Please inform an IRC operator as soon as possible.");
288                                 return;
289                         }
290                 }
291
292                 /* Now, search and replace in a copy of the original_line, replacing $1 through $9 and $1- etc */
293
294                 std::string::size_type crlf = a->ReplaceFormat.find('\n');
295
296                 if (crlf == std::string::npos)
297                 {
298                         DoCommand(a->ReplaceFormat, user, c, safe);
299                         return;
300                 }
301                 else
302                 {
303                         irc::sepstream commands(a->ReplaceFormat, '\n');
304                         std::string scommand;
305                         while (commands.GetToken(scommand))
306                         {
307                                 DoCommand(scommand, user, c, safe);
308                         }
309                         return;
310                 }
311         }
312
313         void DoCommand(std::string newline, User* user, Channel *c, const std::string &original_line)
314         {
315                 std::vector<std::string> pars;
316
317                 for (int v = 1; v < 10; v++)
318                 {
319                         std::string var = "$";
320                         var.append(ConvToStr(v));
321                         var.append("-");
322                         std::string::size_type x = newline.find(var);
323
324                         while (x != std::string::npos)
325                         {
326                                 newline.erase(x, var.length());
327                                 newline.insert(x, GetVar(var, original_line));
328                                 x = newline.find(var);
329                         }
330
331                         var = "$";
332                         var.append(ConvToStr(v));
333                         x = newline.find(var);
334
335                         while (x != std::string::npos)
336                         {
337                                 newline.erase(x, var.length());
338                                 newline.insert(x, GetVar(var, original_line));
339                                 x = newline.find(var);
340                         }
341                 }
342
343                 /* Special variables */
344                 SearchAndReplace(newline, "$nick", user->nick);
345                 SearchAndReplace(newline, "$ident", user->ident);
346                 SearchAndReplace(newline, "$host", user->host);
347                 SearchAndReplace(newline, "$vhost", user->dhost);
348
349                 if (c)
350                 {
351                         /* Channel specific variables */
352                         SearchAndReplace(newline, "$chan", c->name);                    
353                 }
354
355                 /* Unescape any variable names in the user text before sending */
356                 SearchAndReplace(newline, "\r", "$");
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, const std::string &parameter)
371         {
372                 ReadAliases();
373         }
374 };
375
376 MODULE_INIT(ModuleAlias)