]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_alias.cpp
e8fe736c4d0c02103e792c711496301135389d15
[user/henk/code/inspircd.git] / src / modules / m_alias.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2007 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 "users.h"
15 #include "channels.h"
16 #include "modules.h"
17 #include "inspircd.h"
18 #include "wildcard.h"
19 #include <vector>
20
21 /* $ModDesc: Provides aliases of commands. */
22
23 /** An alias definition
24  */
25 class Alias : public classbase
26 {
27  public:
28         /** The text of the alias command */
29         irc::string text;
30         /** Text to replace with */
31         std::string replace_with;
32         /** Nickname required to perform alias */
33         std::string requires;
34         /** Alias requires ulined server */
35         bool uline;
36         /** Requires oper? */
37         bool operonly;
38         /* is case sensitive params */
39         bool case_sensitive;
40         /** Format that must be matched for use */
41         std::string format;
42 };
43
44 class ModuleAlias : public Module
45 {
46  private:
47         /** We cant use a map, there may be multiple aliases with the same name */
48         std::vector<Alias> Aliases;
49         std::map<std::string, int> AliasMap;
50         std::vector<std::string> pars;
51
52         virtual void ReadAliases()
53         {
54                 ConfigReader MyConf(ServerInstance);
55
56                 Aliases.clear();
57                 AliasMap.clear();
58                 for (int i = 0; i < MyConf.Enumerate("alias"); i++)
59                 {
60                         Alias a;
61                         std::string txt;
62                         txt = MyConf.ReadValue("alias", "text", i);
63                         a.text = txt.c_str();
64                         a.replace_with = MyConf.ReadValue("alias", "replace", i, true);
65                         a.requires = MyConf.ReadValue("alias", "requires", i);
66                         a.uline = MyConf.ReadFlag("alias", "uline", i);
67                         a.operonly = MyConf.ReadFlag("alias", "operonly", i);
68                         a.format = MyConf.ReadValue("alias", "format", i);
69                         a.case_sensitive = MyConf.ReadFlag("alias", "matchcase", i);
70                         Aliases.push_back(a);
71                         AliasMap[txt] = 1;
72                 }
73         }
74
75  public:
76         
77         ModuleAlias(InspIRCd* Me)
78                 : Module::Module(Me)
79         {
80                 ReadAliases();
81                 pars.resize(127);
82         }
83
84         void Implements(char* List)
85         {
86                 List[I_OnPreCommand] = List[I_OnRehash] = 1;
87         }
88
89         virtual ~ModuleAlias()
90         {
91         }
92
93         virtual Version GetVersion()
94         {
95                 return Version(1,1,0,1,VF_VENDOR,API_VERSION);
96         }
97
98         std::string GetVar(std::string varname, const std::string &original_line)
99         {
100                 irc::spacesepstream ss(original_line);
101                 varname.erase(varname.begin());
102                 int index = *(varname.begin()) - 48;
103                 varname.erase(varname.begin());
104                 bool everything_after = (varname == "-");
105                 std::string word = "";
106
107                 ServerInstance->Log(DEBUG,"Get var %d%s", index , everything_after ? " and all after it" : "");
108
109                 for (int j = 0; j < index; j++)
110                         word = ss.GetToken();
111
112                 if (everything_after)
113                 {
114                         std::string more = "*";
115                         while ((more = ss.GetToken()) != "")
116                         {
117                                 word.append(" ");
118                                 word.append(more);
119                         }
120                 }
121
122                 ServerInstance->Log(DEBUG,"Var is '%s'", word.c_str());
123
124                 return word;
125         }
126
127         void SearchAndReplace(std::string& newline, const std::string &find, const std::string &replace)
128         {
129                 std::string::size_type x = newline.find(find);
130                 while (x != std::string::npos)
131                 {
132                         newline.erase(x, find.length());
133                         newline.insert(x, replace);
134                         x = newline.find(find);
135                 }
136         }
137
138         virtual int OnPreCommand(const std::string &command, const char** parameters, int pcnt, userrec *user, bool validated, const std::string &original_line)
139         {
140                 userrec *u = NULL;
141
142                 /* If theyre not registered yet, we dont want
143                  * to know.
144                  */
145                 if (user->registered != REG_ALL)
146                         return 0;
147
148                 /* We dont have any commands looking like this, dont bother with the loop */
149                 if (AliasMap.find(command) == AliasMap.end())
150                         return 0;
151
152                 irc::string c = command.c_str();
153                 /* The parameters for the command in their original form, with the command stripped off */
154                 std::string compare = original_line.substr(command.length());
155                 while (*(compare.c_str()) == ' ')
156                         compare.erase(compare.begin());
157
158                 std::string safe(original_line);
159
160                 /* Escape out any $ symbols in the user provided text */
161
162                 SearchAndReplace(safe, "$", "\r");
163
164                 for (unsigned int i = 0; i < Aliases.size(); i++)
165                 {
166                         if (Aliases[i].text == c)
167                         {
168                                 /* Does it match the pattern? */
169                                 if (!Aliases[i].format.empty())
170                                 {
171                                         if (!match(Aliases[i].case_sensitive, compare.c_str(), Aliases[i].format.c_str()))
172                                                 continue;
173                                 }
174
175                                 if ((Aliases[i].operonly) && (!*user->oper))
176                                         return 0;
177
178                                 if (Aliases[i].requires != "")
179                                 {
180                                         u = ServerInstance->FindNick(Aliases[i].requires);
181                                         if (!u)
182                                         {
183                                                 user->WriteServ("401 "+std::string(user->nick)+" "+Aliases[i].requires+" :is currently unavailable. Please try again later.");
184                                                 return 1;
185                                         }
186                                 }
187                                 if ((u != NULL) && (Aliases[i].requires != "") && (Aliases[i].uline))
188                                 {
189                                         if (!ServerInstance->ULine(u->server))
190                                         {
191                                                 ServerInstance->WriteOpers("*** NOTICE -- Service "+Aliases[i].requires+" required by alias "+std::string(Aliases[i].text.c_str())+" is not on a u-lined server, possibly underhanded antics detected!"); 
192                                                 user->WriteServ("401 "+std::string(user->nick)+" "+Aliases[i].requires+" :is an imposter! Please inform an IRC operator as soon as possible.");
193                                                 return 1;
194                                         }
195                                 }
196
197                                 /* Now, search and replace in a copy of the original_line, replacing $1 through $9 and $1- etc */
198
199                                 std::string::size_type crlf = Aliases[i].replace_with.find('\n');
200
201                                 if (crlf == std::string::npos)
202                                 {
203                                         ServerInstance->Log(DEBUG,"Single line alias: '%s'", Aliases[i].replace_with.c_str());
204                                         DoCommand(Aliases[i].replace_with, user, safe);
205                                         return 1;
206                                 }
207                                 else
208                                 {
209                                         ServerInstance->Log(DEBUG,"Multi line alias: '%s'", Aliases[i].replace_with.c_str());
210                                         irc::sepstream commands(Aliases[i].replace_with, '\n');
211                                         std::string command = "*";
212                                         while ((command = commands.GetToken()) != "")
213                                         {
214                                                 ServerInstance->Log(DEBUG,"Execute: '%s'", command.c_str());
215                                                 DoCommand(command, user, safe);
216                                         }
217                                         return 1;
218                                 }
219                         }
220                 }
221                 return 0;
222         }
223
224         void DoCommand(std::string newline, userrec* user, const std::string &original_line)
225         {
226                 for (int v = 1; v < 10; v++)
227                 {
228                         std::string var = "$";
229                         var.append(ConvToStr(v));
230                         var.append("-");
231                         std::string::size_type x = newline.find(var);
232
233                         while (x != std::string::npos)
234                         {
235                                 newline.erase(x, var.length());
236                                 newline.insert(x, GetVar(var, original_line));
237                                 x = newline.find(var);
238                         }
239
240                         var = "$";
241                         var.append(ConvToStr(v));
242                         x = newline.find(var);
243
244                         while (x != std::string::npos)
245                         {
246                                 newline.erase(x, var.length());
247                                 newline.insert(x, GetVar(var, original_line));
248                                 x = newline.find(var);
249                         }
250                 }
251
252                 /* Special variables */
253                 SearchAndReplace(newline, "$nick", user->nick);
254                 SearchAndReplace(newline, "$ident", user->ident);
255                 SearchAndReplace(newline, "$host", user->host);
256                 SearchAndReplace(newline, "$vhost", user->dhost);
257
258                 /* Unescape any variable names in the user text before sending */
259                 SearchAndReplace(newline, "\r", "$");
260
261                 irc::tokenstream ss(newline);
262                 const char* parv[127];
263                 int x = 0;
264
265                 while ((pars[x] = ss.GetToken()) != "")
266                 {
267                         parv[x] = pars[x].c_str();
268                         ServerInstance->Log(DEBUG,"Parameter %d: %s", x, parv[x]);
269                         x++;
270                 }
271
272                 ServerInstance->Log(DEBUG,"Call command handler on %s", parv[0]);
273
274                 if (ServerInstance->Parser->CallHandler(parv[0], &parv[1], x-1, user) == CMD_INVALID)
275                 {
276                         ServerInstance->Log(DEBUG,"Unknown command or not enough parameters");
277                 }
278                 else
279                 {
280                         ServerInstance->Log(DEBUG,"Command handler called successfully.");
281                 }
282         }
283  
284         virtual void OnRehash(userrec* user, const std::string &parameter)
285         {
286                 ReadAliases();
287         }
288 };
289
290
291 class ModuleAliasFactory : public ModuleFactory
292 {
293  public:
294         ModuleAliasFactory()
295         {
296         }
297
298         ~ModuleAliasFactory()
299         {
300         }
301
302                 virtual Module * CreateModule(InspIRCd* Me)
303         {
304                 return new ModuleAlias(Me);
305         }
306 };
307
308
309 extern "C" void * init_module( void )
310 {
311         return new ModuleAliasFactory;
312 }
313