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