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