]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_alias.cpp
c2b26db5278281f071a4e8c8b725fb147f1f53cf
[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                                         DoCommand(Aliases[i].replace_with, user, original_line);
173                                         return 1;
174                                 }
175                                 else
176                                 {
177                                         irc::sepstream commands(Aliases[i].replace_with, '\n');
178                                         std::string command = "*";
179                                         while ((command = commands.GetToken()) != "")
180                                         {
181                                                 DoCommand(command, user, original_line);
182                                         }
183                                         return 1;
184                                 }
185                         }
186                 }
187                 return 0;
188         }
189
190         void DoCommand(std::string newline, userrec* user, const std::string &original_line)
191         {
192                 for (int v = 1; v < 10; v++)
193                 {
194                         std::string var = "$";
195                         var.append(ConvToStr(v));
196                         var.append("-");
197                         std::string::size_type x = newline.find(var);
198
199                         while (x != std::string::npos)
200                         {
201                                 newline.erase(x, var.length());
202                                 newline.insert(x, GetVar(var, original_line));
203                                 x = newline.find(var);
204                         }
205
206                         var = "$";
207                         var.append(ConvToStr(v));
208                         x = newline.find(var);
209
210                         while (x != std::string::npos)
211                         {
212                                 newline.erase(x, var.length());
213                                 newline.insert(x, GetVar(var, original_line));
214                                 x = newline.find(var);
215                         }
216                 }
217
218                 /* Special variables */
219                 SearchAndReplace(newline, "$nick", user->nick);
220                 SearchAndReplace(newline, "$ident", user->ident);
221                 SearchAndReplace(newline, "$host", user->host);
222                 SearchAndReplace(newline, "$vhost", user->dhost);
223
224                 irc::tokenstream ss(newline);
225                 const char* parv[127];
226                 int x = 0;
227
228                 while ((pars[x] = ss.GetToken()) != "")
229                 {
230                         parv[x] = pars[x].c_str();
231                         ServerInstance->Log(DEBUG,"Parameter %d: %s", x, parv[x]);
232                         x++;
233                 }
234
235                 ServerInstance->Log(DEBUG,"Call command handler on %s", parv[0]);
236
237                 if (ServerInstance->Parser->CallHandler(parv[0], &parv[1], x-1, user) == CMD_INVALID)
238                 {
239                         ServerInstance->Log(DEBUG,"Unknown command or not enough parameters");
240                 }
241                 else
242                 {
243                         ServerInstance->Log(DEBUG,"Command handler called successfully.");
244                 }
245         }
246  
247         virtual void OnRehash(const std::string &parameter)
248         {
249                 ReadAliases();
250         }
251 };
252
253
254 class ModuleAliasFactory : public ModuleFactory
255 {
256  public:
257         ModuleAliasFactory()
258         {
259         }
260
261         ~ModuleAliasFactory()
262         {
263         }
264
265                 virtual Module * CreateModule(InspIRCd* Me)
266         {
267                 return new ModuleAlias(Me);
268         }
269 };
270
271
272 extern "C" void * init_module( void )
273 {
274         return new ModuleAliasFactory;
275 }
276