]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_alias.cpp
Add config <options:disablehmac> to support disabling of HMAC, and tidy up to detect...
[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                 for (int j = 0; j < index; j++)
108                         word = ss.GetToken();
109
110                 if (everything_after)
111                 {
112                         std::string more = "*";
113                         while ((more = ss.GetToken()) != "")
114                         {
115                                 word.append(" ");
116                                 word.append(more);
117                         }
118                 }
119
120                 return word;
121         }
122
123         void SearchAndReplace(std::string& newline, const std::string &find, const std::string &replace)
124         {
125                 std::string::size_type x = newline.find(find);
126                 while (x != std::string::npos)
127                 {
128                         newline.erase(x, find.length());
129                         newline.insert(x, replace);
130                         x = newline.find(find);
131                 }
132         }
133
134         virtual int OnPreCommand(const std::string &command, const char** parameters, int pcnt, userrec *user, bool validated, const std::string &original_line)
135         {
136                 userrec *u = NULL;
137
138                 /* If theyre not registered yet, we dont want
139                  * to know.
140                  */
141                 if (user->registered != REG_ALL)
142                         return 0;
143
144                 /* We dont have any commands looking like this, dont bother with the loop */
145                 if (AliasMap.find(command) == AliasMap.end())
146                         return 0;
147
148                 irc::string c = command.c_str();
149                 /* The parameters for the command in their original form, with the command stripped off */
150                 std::string compare = original_line.substr(command.length());
151                 while (*(compare.c_str()) == ' ')
152                         compare.erase(compare.begin());
153
154                 std::string safe(original_line);
155
156                 /* Escape out any $ symbols in the user provided text */
157
158                 SearchAndReplace(safe, "$", "\r");
159
160                 for (unsigned int i = 0; i < Aliases.size(); i++)
161                 {
162                         if (Aliases[i].text == c)
163                         {
164                                 /* Does it match the pattern? */
165                                 if (!Aliases[i].format.empty())
166                                 {
167                                         if (!match(Aliases[i].case_sensitive, compare.c_str(), Aliases[i].format.c_str()))
168                                                 continue;
169                                 }
170
171                                 if ((Aliases[i].operonly) && (!*user->oper))
172                                         return 0;
173
174                                 if (Aliases[i].requires != "")
175                                 {
176                                         u = ServerInstance->FindNick(Aliases[i].requires);
177                                         if (!u)
178                                         {
179                                                 user->WriteServ("401 "+std::string(user->nick)+" "+Aliases[i].requires+" :is currently unavailable. Please try again later.");
180                                                 return 1;
181                                         }
182                                 }
183                                 if ((u != NULL) && (Aliases[i].requires != "") && (Aliases[i].uline))
184                                 {
185                                         if (!ServerInstance->ULine(u->server))
186                                         {
187                                                 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!"); 
188                                                 user->WriteServ("401 "+std::string(user->nick)+" "+Aliases[i].requires+" :is an imposter! Please inform an IRC operator as soon as possible.");
189                                                 return 1;
190                                         }
191                                 }
192
193                                 /* Now, search and replace in a copy of the original_line, replacing $1 through $9 and $1- etc */
194
195                                 std::string::size_type crlf = Aliases[i].replace_with.find('\n');
196
197                                 if (crlf == std::string::npos)
198                                 {
199                                         DoCommand(Aliases[i].replace_with, user, safe);
200                                         return 1;
201                                 }
202                                 else
203                                 {
204                                         irc::sepstream commands(Aliases[i].replace_with, '\n');
205                                         std::string command = "*";
206                                         while ((command = commands.GetToken()) != "")
207                                         {
208                                                 DoCommand(command, user, safe);
209                                         }
210                                         return 1;
211                                 }
212                         }
213                 }
214                 return 0;
215         }
216
217         void DoCommand(std::string newline, userrec* user, const std::string &original_line)
218         {
219                 for (int v = 1; v < 10; v++)
220                 {
221                         std::string var = "$";
222                         var.append(ConvToStr(v));
223                         var.append("-");
224                         std::string::size_type x = newline.find(var);
225
226                         while (x != std::string::npos)
227                         {
228                                 newline.erase(x, var.length());
229                                 newline.insert(x, GetVar(var, original_line));
230                                 x = newline.find(var);
231                         }
232
233                         var = "$";
234                         var.append(ConvToStr(v));
235                         x = newline.find(var);
236
237                         while (x != std::string::npos)
238                         {
239                                 newline.erase(x, var.length());
240                                 newline.insert(x, GetVar(var, original_line));
241                                 x = newline.find(var);
242                         }
243                 }
244
245                 /* Special variables */
246                 SearchAndReplace(newline, "$nick", user->nick);
247                 SearchAndReplace(newline, "$ident", user->ident);
248                 SearchAndReplace(newline, "$host", user->host);
249                 SearchAndReplace(newline, "$vhost", user->dhost);
250
251                 /* Unescape any variable names in the user text before sending */
252                 SearchAndReplace(newline, "\r", "$");
253
254                 irc::tokenstream ss(newline);
255                 const char* parv[127];
256                 int x = 0;
257
258                 while (ss.GetToken(pars[x]))
259                 {
260                         parv[x] = pars[x].c_str();
261                         x++;
262                 }
263
264                 ServerInstance->Parser->CallHandler(parv[0], &parv[1], x-1, user);
265         }
266  
267         virtual void OnRehash(userrec* user, const std::string &parameter)
268         {
269                 ReadAliases();
270         }
271 };
272
273
274 class ModuleAliasFactory : public ModuleFactory
275 {
276  public:
277         ModuleAliasFactory()
278         {
279         }
280
281         ~ModuleAliasFactory()
282         {
283         }
284
285                 virtual Module * CreateModule(InspIRCd* Me)
286         {
287                 return new ModuleAlias(Me);
288         }
289 };
290
291
292 extern "C" void * init_module( void )
293 {
294         return new ModuleAliasFactory;
295 }
296