]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_alias.cpp
FindNick, FindChan, ChanModes, UserList, CountInvisible, PurgeEmptyChannels, GetClass...
[user/henk/code/inspircd.git] / src / modules / m_alias.cpp
1 /*   +------------------------------------+
2  *   | Inspire Internet Relay Chat Daemon |
3  *   +------------------------------------+
4  *
5  *  InspIRCd is copyright (C) 2002-2006 ChatSpike-Dev.
6  *   E-mail:
7  *      <brain@chatspike.net>
8  *      <Craig@chatspike.net>
9  * 
10  * Written by Craig Edwards, Craig McLure, and others.
11  * This program is free but copyrighted software; see
12  *the file COPYING for details.
13  *
14  * ---------------------------------------------------
15  */
16
17 using namespace std;
18
19 #include "users.h"
20 #include "channels.h"
21 #include "modules.h"
22 #include "helperfuncs.h"
23 #include "inspircd.h"
24 #include <vector>
25
26 /* $ModDesc: Provides aliases of commands. */
27
28 extern InspIRCd* ServerInstance;
29
30 class Alias : public classbase
31 {
32         public:
33                 irc::string text;
34                 std::string replace_with;
35                 std::string requires;
36                 bool uline;
37 };
38
39 class ModuleAlias : public Module
40 {
41         private:
42                 Server *Srv;
43                 ConfigReader *MyConf;
44                 std::vector<Alias> Aliases;
45
46                 virtual void ReadAliases()
47                 {
48                         Aliases.clear();
49                 
50                         for (int i = 0; i < MyConf->Enumerate("alias"); i++)
51                         {
52                                 Alias a;
53                                 std::string txt;
54                                 txt = MyConf->ReadValue("alias", "text", i);
55                                 a.text = txt.c_str();
56                                 a.replace_with = MyConf->ReadValue("alias", "replace", i);
57                                 a.requires = MyConf->ReadValue("alias", "requires", i);
58                         
59                                 a.uline =       ((MyConf->ReadValue("alias", "uline", i) == "yes") ||
60                                                 (MyConf->ReadValue("alias", "uline", i) == "1") ||
61                                                 (MyConf->ReadValue("alias", "uline", i) == "true"));
62                                         
63                                 Aliases.push_back(a);
64                         }
65         
66                 }
67
68         public:
69         
70                 ModuleAlias(Server* Me)
71                         : Module::Module(Me)
72                 {
73                         Srv = Me;
74                         MyConf = new ConfigReader;
75                         ReadAliases();
76                 }
77
78                 void Implements(char* List)
79                 {
80                         List[I_OnPreCommand] = List[I_OnRehash] = 1;
81                 }
82         
83                 virtual ~ModuleAlias()
84                 {
85                         DELETE(MyConf);
86                 }
87         
88                 virtual Version GetVersion()
89                 {
90                         return Version(1,0,0,1,VF_VENDOR);
91                 }
92
93                 virtual int OnPreCommand(const std::string &command, const char** parameters, int pcnt, userrec *user, bool validated)
94                 {
95                         userrec *u = NULL;
96                         irc::string c = command.c_str();
97
98                         /* If the command is valid, we dont want to know,
99                          * and if theyre not registered yet, we dont want
100                          * to know either
101                          */
102                         if ((validated) || (user->registered != REG_ALL))
103                                 return 0;
104                         
105                         for (unsigned int i = 0; i < Aliases.size(); i++)
106                         {
107                                 if (Aliases[i].text == c)
108                                 {
109                                         if (Aliases[i].requires != "")
110                                         {
111                                                 u = ServerInstance->FindNick(Aliases[i].requires);
112                                                 if (!u)
113                                                 {
114                                                         user->WriteServ("401 "+std::string(user->nick)+" "+Aliases[i].requires+" :is currently unavailable. Please try again later.");
115                                                         return 1;
116                                                 }
117                                         }
118                                         if ((u != NULL) && (Aliases[i].requires != "") && (Aliases[i].uline))
119                                         {
120                                                 if (!Srv->IsUlined(u->server))
121                                                 {
122                                                         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!"); 
123                                                         user->WriteServ("401 "+std::string(user->nick)+" "+Aliases[i].requires+" :is an imposter! Please inform an IRC operator as soon as possible.");
124                                                         return 1;
125                                                 }
126                                         }
127
128                                         std::string n = "";
129                                         for (int j = 0; j < pcnt; j++)
130                                         {
131                                                 if (j)
132                                                         n = n + " ";
133                                                 n = n + parameters[j];
134                                         }
135                                         /* Final param now in n as one string */
136                                         std::stringstream stuff(Aliases[i].replace_with);
137
138                                         std::string cmd = "";
139                                         std::string target = "";
140                                         stuff >> cmd;
141                                         stuff >> target;
142
143                                         const char* para[2];
144                                         para[0] = target.c_str();
145                                         para[1] = n.c_str();
146
147                                         Srv->CallCommandHandler(cmd,para,2,user);
148                                         return 1;
149                                 }
150                         }
151                         return 0;
152                 }
153   
154                 virtual void OnRehash(const std::string &parameter)
155                 {
156                         DELETE(MyConf);
157                         MyConf = new ConfigReader;
158                 
159                         ReadAliases();
160                 }
161 };
162
163
164 class ModuleAliasFactory : public ModuleFactory
165 {
166         public:
167                 ModuleAliasFactory()
168                 {
169                 }
170         
171                 ~ModuleAliasFactory()
172                 {
173                 }
174         
175                 virtual Module * CreateModule(Server* Me)
176                 {
177                         return new ModuleAlias(Me);
178                 }
179 };
180
181
182 extern "C" void * init_module( void )
183 {
184         return new ModuleAliasFactory;
185 }
186