]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_alias.cpp
Next part of Development/Hooking (see wiki)
[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 "inspircd.h"
15 #include "wildcard.h"
16
17 /* $ModDesc: Provides aliases of commands. */
18
19 /** An alias definition
20  */
21 class Alias : public classbase
22 {
23  public:
24         /** The text of the alias command */
25         irc::string text;
26         /** Text to replace with */
27         std::string replace_with;
28         /** Nickname required to perform alias */
29         std::string requires;
30         /** Alias requires ulined server */
31         bool uline;
32         /** Requires oper? */
33         bool operonly;
34         /* is case sensitive params */
35         bool case_sensitive;
36         /** Format that must be matched for use */
37         std::string format;
38 };
39
40 class ModuleAlias : public Module
41 {
42  private:
43         /** We cant use a map, there may be multiple aliases with the same name */
44         std::vector<Alias> Aliases;
45         std::map<std::string, int> AliasMap;
46         std::vector<std::string> pars;
47
48         virtual void ReadAliases()
49         {
50                 ConfigReader MyConf(ServerInstance);
51
52                 Aliases.clear();
53                 AliasMap.clear();
54                 for (int i = 0; i < MyConf.Enumerate("alias"); i++)
55                 {
56                         Alias a;
57                         std::string txt;
58                         txt = MyConf.ReadValue("alias", "text", i);
59                         a.text = txt.c_str();
60                         a.replace_with = MyConf.ReadValue("alias", "replace", i, true);
61                         a.requires = MyConf.ReadValue("alias", "requires", i);
62                         a.uline = MyConf.ReadFlag("alias", "uline", i);
63                         a.operonly = MyConf.ReadFlag("alias", "operonly", i);
64                         a.format = MyConf.ReadValue("alias", "format", i);
65                         a.case_sensitive = MyConf.ReadFlag("alias", "matchcase", i);
66                         Aliases.push_back(a);
67                         AliasMap[txt] = 1;
68                 }
69         }
70
71  public:
72         
73         ModuleAlias(InspIRCd* Me)
74                 : Module(Me)
75         {
76                 ReadAliases();
77                 pars.resize(MAXPARAMETERS);
78
79                 Me->Modules->Attach(I_OnPreCommand, this);
80                 Me->Modules->Attach(I_OnRehash, this);
81         }
82
83         virtual ~ModuleAlias()
84         {
85         }
86
87         virtual Version GetVersion()
88         {
89                 return Version(1,1,0,1,VF_VENDOR,API_VERSION);
90         }
91
92         std::string GetVar(std::string varname, const std::string &original_line)
93         {
94                 irc::spacesepstream ss(original_line);
95                 varname.erase(varname.begin());
96                 int index = *(varname.begin()) - 48;
97                 varname.erase(varname.begin());
98                 bool everything_after = (varname == "-");
99                 std::string word;
100
101                 for (int j = 0; j < index; j++)
102                         ss.GetToken(word);
103
104                 if (everything_after)
105                 {
106                         std::string more;
107                         while (ss.GetToken(more))
108                         {
109                                 word.append(" ");
110                                 word.append(more);
111                         }
112                 }
113
114                 return word;
115         }
116
117         void SearchAndReplace(std::string& newline, const std::string &find, const std::string &replace)
118         {
119                 std::string::size_type x = newline.find(find);
120                 while (x != std::string::npos)
121                 {
122                         newline.erase(x, find.length());
123                         newline.insert(x, replace);
124                         x = newline.find(find);
125                 }
126         }
127
128         virtual int OnPreCommand(const std::string &command, const char** parameters, int pcnt, User *user, bool validated, const std::string &original_line)
129         {
130                 User *u = NULL;
131
132                 /* If theyre not registered yet, we dont want
133                  * to know.
134                  */
135                 if (user->registered != REG_ALL)
136                         return 0;
137
138                 /* We dont have any commands looking like this, dont bother with the loop */
139                 if (AliasMap.find(command) == AliasMap.end())
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())
160                                 {
161                                         if (!match(Aliases[i].case_sensitive, compare.c_str(), Aliases[i].format.c_str()))
162                                                 continue;
163                                 }
164
165                                 if ((Aliases[i].operonly) && (!IS_OPER(user)))
166                                         return 0;
167
168                                 if (!Aliases[i].requires.empty())
169                                 {
170                                         u = ServerInstance->FindNick(Aliases[i].requires);
171                                         if (!u)
172                                         {
173                                                 user->WriteServ("401 "+std::string(user->nick)+" "+Aliases[i].requires+" :is currently unavailable. Please try again later.");
174                                                 return 1;
175                                         }
176                                 }
177                                 if ((u != NULL) && (!Aliases[i].requires.empty()) && (Aliases[i].uline))
178                                 {
179                                         if (!ServerInstance->ULine(u->server))
180                                         {
181                                                 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!"); 
182                                                 user->WriteServ("401 "+std::string(user->nick)+" "+Aliases[i].requires+" :is an imposter! Please inform an IRC operator as soon as possible.");
183                                                 return 1;
184                                         }
185                                 }
186
187                                 /* Now, search and replace in a copy of the original_line, replacing $1 through $9 and $1- etc */
188
189                                 std::string::size_type crlf = Aliases[i].replace_with.find('\n');
190
191                                 if (crlf == std::string::npos)
192                                 {
193                                         DoCommand(Aliases[i].replace_with, user, safe);
194                                         return 1;
195                                 }
196                                 else
197                                 {
198                                         irc::sepstream commands(Aliases[i].replace_with, '\n');
199                                         std::string command;
200                                         while (commands.GetToken(command))
201                                         {
202                                                 DoCommand(command, user, safe);
203                                         }
204                                         return 1;
205                                 }
206                         }
207                 }
208                 return 0;
209         }
210
211         void DoCommand(std::string newline, User* user, const std::string &original_line)
212         {
213                 for (int v = 1; v < 10; v++)
214                 {
215                         std::string var = "$";
216                         var.append(ConvToStr(v));
217                         var.append("-");
218                         std::string::size_type x = newline.find(var);
219
220                         while (x != std::string::npos)
221                         {
222                                 newline.erase(x, var.length());
223                                 newline.insert(x, GetVar(var, original_line));
224                                 x = newline.find(var);
225                         }
226
227                         var = "$";
228                         var.append(ConvToStr(v));
229                         x = newline.find(var);
230
231                         while (x != std::string::npos)
232                         {
233                                 newline.erase(x, var.length());
234                                 newline.insert(x, GetVar(var, original_line));
235                                 x = newline.find(var);
236                         }
237                 }
238
239                 /* Special variables */
240                 SearchAndReplace(newline, "$nick", user->nick);
241                 SearchAndReplace(newline, "$ident", user->ident);
242                 SearchAndReplace(newline, "$host", user->host);
243                 SearchAndReplace(newline, "$vhost", user->dhost);
244
245                 /* Unescape any variable names in the user text before sending */
246                 SearchAndReplace(newline, "\r", "$");
247
248                 irc::tokenstream ss(newline);
249                 const char* parv[MAXPARAMETERS];
250                 int x = 0;
251
252                 while (ss.GetToken(pars[x]) && x < MAXPARAMETERS)
253                 {
254                         parv[x] = pars[x].c_str();
255                         x++;
256                 }
257
258                 ServerInstance->Parser->CallHandler(parv[0], &parv[1], x-1, user);
259         }
260  
261         virtual void OnRehash(User* user, const std::string &parameter)
262         {
263                 ReadAliases();
264         }
265 };
266
267 MODULE_INIT(ModuleAlias)