]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules.cpp
Added m_alias module which provides command aliases.
[user/henk/code/inspircd.git] / src / modules.cpp
1 /*
2
3
4 */
5
6
7
8 #include <typeinfo>
9 #include <iostream>
10 #include "globals.h"
11 #include "modules.h"
12 #include "ctables.h"
13 #include "inspircd_io.h"
14 #include "wildcard.h"
15 #include "mode.h"
16 #include "message.h"
17 #include "commands.h"
18
19 // class type for holding an extended mode character - internal to core
20
21 class ExtMode : public classbase
22 {
23 public:
24         char modechar;
25         int type;
26         int params_when_on;
27         int params_when_off;
28         bool needsoper;
29         ExtMode(char mc, int ty, bool oper, int p_on, int p_off) : modechar(mc), type(ty), needsoper(oper), params_when_on(p_on), params_when_off(p_off) { };
30 };                                     
31
32 typedef std::vector<ExtMode> ExtModeList;
33 typedef ExtModeList::iterator ExtModeListIter;
34
35 ExtModeList EMode;
36
37 // returns true if an extended mode character is in use
38 bool ModeDefined(char modechar, int type)
39 {
40         log(DEBUG,"Size of extmodes vector is %d",EMode.size());
41         for (ExtModeListIter i = EMode.begin(); i < EMode.end(); i++)
42         {
43                 log(DEBUG,"i->modechar==%c, modechar=%c, i->type=%d, type=%d",i->modechar,modechar,i->type,type);
44                 if ((i->modechar == modechar) && (i->type == type))
45                 {
46                         return true;
47                 }
48         }
49         return false;
50 }
51
52 bool ModeDefinedOper(char modechar, int type)
53 {
54         log(DEBUG,"Size of extmodes vector is %d",EMode.size());
55         for (ExtModeListIter i = EMode.begin(); i < EMode.end(); i++)
56         {
57                 log(DEBUG,"i->modechar==%c, modechar=%c, i->type=%d, type=%d",i->modechar,modechar,i->type,type);
58                 if ((i->modechar == modechar) && (i->type == type) && (i->needsoper == true))
59                 {
60                         return true;
61                 }
62         }
63         return false;
64 }
65
66 // returns number of parameters for a custom mode when it is switched on
67 int ModeDefinedOn(char modechar, int type)
68 {
69         for (ExtModeListIter i = EMode.begin(); i < EMode.end(); i++)
70         {
71                 if ((i->modechar == modechar) && (i->type == type))
72                 {
73                         return i->params_when_on;
74                 }
75         }
76         return 0;
77 }
78
79 // returns number of parameters for a custom mode when it is switched on
80 int ModeDefinedOff(char modechar, int type)
81 {
82         for (ExtModeListIter i = EMode.begin(); i < EMode.end(); i++)
83         {
84                 if ((i->modechar == modechar) && (i->type == type))
85                 {
86                         return i->params_when_off;
87                 }
88         }
89         return 0;
90 }
91
92 // returns true if an extended mode character is in use
93 bool DoAddExtendedMode(char modechar, int type, bool requires_oper, int params_on, int params_off)
94 {
95         if (ModeDefined(modechar,type)) {
96                 return false;
97         }
98         EMode.push_back(ExtMode(modechar,type,requires_oper,params_on,params_off));
99         return true;
100 }
101
102
103 // version is a simple class for holding a modules version number
104
105 Version::Version(int major, int minor, int revision, int build) : Major(major), Minor(minor), Revision(revision), Build(build) { };
106
107 // admin is a simple class for holding a server's administrative info
108
109 Admin::Admin(std::string name, std::string email, std::string nick) : Name(name), Email(email), Nick(nick) { };
110
111 Module::Module() { }
112 Module::~Module() { }
113 void Module::OnUserConnect(userrec* user) { }
114 void Module::OnUserQuit(userrec* user) { }
115 void Module::OnUserJoin(userrec* user, chanrec* channel) { }
116 void Module::OnUserPart(userrec* user, chanrec* channel) { }
117 void Module::OnPacketTransmit(char *p) { }
118 void Module::OnPacketReceive(char *p) { }
119 void Module::OnRehash() { }
120 void Module::OnServerRaw(std::string &raw, bool inbound, userrec* user) { }
121 int Module::OnUserPreJoin(userrec* user, chanrec* chan, const char* cname) { return 0; }
122 bool Module::OnExtendedMode(userrec* user, void* target, char modechar, int type, bool mode_on, string_list &params) { return false; }
123 Version Module::GetVersion() { return Version(1,0,0,0); }
124 void Module::OnOper(userrec* user) { };
125 void Module::OnInfo(userrec* user) { };
126 void Module::OnWhois(userrec* source, userrec* dest) { };
127 int Module::OnUserPreMessage(userrec* user,void* dest,int target_type, std::string text) { return 0; };
128 int Module::OnUserPreNotice(userrec* user,void* dest,int target_type, std::string text) { return 0; };
129 int Module::OnUserPreNick(userrec* user, std::string newnick) { return 0; };
130
131 // server is a wrapper class that provides methods to all of the C-style
132 // exports in the core
133 //
134
135 Server::Server()
136 {
137 }
138
139 Server::~Server()
140 {
141 }
142
143 void Server::SendOpers(std::string s)
144 {
145         WriteOpers("%s",s.c_str());
146 }
147
148 bool Server::MatchText(std::string sliteral, std::string spattern)
149 {
150         char literal[MAXBUF],pattern[MAXBUF];
151         strncpy(literal,sliteral.c_str(),MAXBUF);
152         strncpy(pattern,spattern.c_str(),MAXBUF);
153         return match(literal,pattern);
154 }
155
156 void Server::SendToModeMask(std::string modes, int flags, std::string text)
157 {
158         WriteMode(modes.c_str(),flags,"%s",text.c_str());
159 }
160
161 chanrec* Server::JoinUserToChannel(userrec* user, std::string cname, std::string key)
162 {
163         return add_channel(user,cname.c_str(),key.c_str(),true);
164 }
165
166 chanrec* Server::PartUserFromChannel(userrec* user, std::string cname, std::string reason)
167 {
168         return del_channel(user,cname.c_str(),reason.c_str(),false);
169 }
170
171 void Server::ChangeUserNick(userrec* user, std::string nickname)
172 {
173         force_nickchange(user,nickname.c_str());
174 }
175
176 void Server::QuitUser(userrec* user, std::string reason)
177 {
178         send_network_quit(user->nick,reason.c_str());
179         kill_link(user,reason.c_str());
180 }
181
182 bool Server::IsUlined(std::string server)
183 {
184         return is_uline(server.c_str());
185 }
186
187 void Server::CallCommandHandler(std::string commandname, char** parameters, int pcnt, userrec* user)
188 {
189         call_handler(commandname.c_str(),parameters,pcnt,user);
190 }
191
192 void Server::Log(int level, std::string s)
193 {
194         log(level,"%s",s.c_str());
195 }
196
197 void Server::AddCommand(char* cmd, handlerfunc f, char flags, int minparams)
198 {
199         createcommand(cmd,f,flags,minparams);
200 }
201
202 void Server::SendMode(char **parameters, int pcnt, userrec *user)
203 {
204         server_mode(parameters,pcnt,user);
205 }
206
207 void Server::Send(int Socket, std::string s)
208 {
209         Write(Socket,"%s",s.c_str());
210 }
211
212 void Server::SendServ(int Socket, std::string s)
213 {
214         WriteServ(Socket,"%s",s.c_str());
215 }
216
217 void Server::SendFrom(int Socket, userrec* User, std::string s)
218 {
219         WriteFrom(Socket,User,"%s",s.c_str());
220 }
221
222 void Server::SendTo(userrec* Source, userrec* Dest, std::string s)
223 {
224         if (!Source)
225         {
226                 // if source is NULL, then the message originates from the local server
227                 Write(Dest->fd,":%s %s",this->GetServerName().c_str(),s.c_str());
228         }
229         else
230         {
231                 // otherwise it comes from the user specified
232                 WriteTo(Source,Dest,"%s",s.c_str());
233         }
234 }
235
236 void Server::SendChannel(userrec* User, chanrec* Channel, std::string s,bool IncludeSender)
237 {
238         if (IncludeSender)
239         {
240                 WriteChannel(Channel,User,"%s",s.c_str());
241         }
242         else
243         {
244                 ChanExceptSender(Channel,User,"%s",s.c_str());
245         }
246 }
247
248 bool Server::CommonChannels(userrec* u1, userrec* u2)
249 {
250         return (common_channels(u1,u2) != 0);
251 }
252
253 void Server::SendCommon(userrec* User, std::string text,bool IncludeSender)
254 {
255         if (IncludeSender)
256         {
257                 WriteCommon(User,"%s",text.c_str());
258         }
259         else
260         {
261                 WriteCommonExcept(User,"%s",text.c_str());
262         }
263 }
264
265 void Server::SendWallops(userrec* User, std::string text)
266 {
267         WriteWallOps(User,false,"%s",text.c_str());
268 }
269
270 void Server::ChangeHost(userrec* user, std::string host)
271 {
272         ChangeDisplayedHost(user,host.c_str());
273 }
274
275 void Server::ChangeGECOS(userrec* user, std::string gecos)
276 {
277         ChangeName(user,gecos.c_str());
278 }
279
280 bool Server::IsNick(std::string nick)
281 {
282         return (isnick(nick.c_str()) != 0);
283 }
284
285 userrec* Server::FindNick(std::string nick)
286 {
287         return Find(nick);
288 }
289
290 chanrec* Server::FindChannel(std::string channel)
291 {
292         return FindChan(channel.c_str());
293 }
294
295 std::string Server::ChanMode(userrec* User, chanrec* Chan)
296 {
297         return cmode(User,Chan);
298 }
299
300 std::string Server::GetServerName()
301 {
302         return getservername();
303 }
304
305 std::string Server::GetNetworkName()
306 {
307         return getnetworkname();
308 }
309
310 Admin Server::GetAdmin()
311 {
312         return Admin(getadminname(),getadminemail(),getadminnick());
313 }
314
315
316
317 bool Server::AddExtendedMode(char modechar, int type, bool requires_oper, int params_when_on, int params_when_off)
318 {
319         if (type == MT_SERVER)
320         {
321                 log(DEBUG,"*** API ERROR *** Modes of type MT_SERVER are reserved for future expansion");
322                 return false;
323         }
324         if (((params_when_on>0) || (params_when_off>0)) && (type == MT_CLIENT))
325         {
326                 log(DEBUG,"*** API ERROR *** Parameters on MT_CLIENT modes are not supported");
327                 return false;
328         }
329         if ((params_when_on>1) || (params_when_off>1))
330         {
331                 log(DEBUG,"*** API ERROR *** More than one parameter for an MT_CHANNEL mode is not yet supported");
332                 return false;
333         }
334         return DoAddExtendedMode(modechar,type,requires_oper,params_when_on,params_when_off);
335 }
336
337 int Server::CountUsers(chanrec* c)
338 {
339         return usercount(c);
340 }
341
342
343 ConfigReader::ConfigReader()
344 {
345         this->cache = new std::stringstream(std::stringstream::in | std::stringstream::out);
346         this->error = LoadConf(CONFIG_FILE,this->cache);
347 }
348
349
350 ConfigReader::~ConfigReader()
351 {
352         if (this->cache)
353                 delete this->cache;
354 }
355
356
357 ConfigReader::ConfigReader(std::string filename)
358 {
359         this->cache = new std::stringstream(std::stringstream::in | std::stringstream::out);
360         this->error = LoadConf(filename.c_str(),this->cache);
361 };
362
363 std::string ConfigReader::ReadValue(std::string tag, std::string name, int index)
364 {
365         char val[MAXBUF];
366         char t[MAXBUF];
367         char n[MAXBUF];
368         strncpy(t,tag.c_str(),MAXBUF);
369         strncpy(n,name.c_str(),MAXBUF);
370         ReadConf(cache,t,n,index,val);
371         return std::string(val);
372 }
373
374
375 int ConfigReader::Enumerate(std::string tag)
376 {
377         return EnumConf(cache,tag.c_str());
378 }
379
380 int ConfigReader::EnumerateValues(std::string tag, int index)
381 {
382         return EnumValues(cache, tag.c_str(), index);
383 }
384
385 bool ConfigReader::Verify()
386 {
387         return this->error;
388 }
389
390
391 FileReader::FileReader(std::string filename)
392 {
393         file_cache c;
394         readfile(c,filename.c_str());
395         this->fc = c;
396 }
397
398 FileReader::FileReader()
399 {
400 }
401
402 void FileReader::LoadFile(std::string filename)
403 {
404         file_cache c;
405         readfile(c,filename.c_str());
406         this->fc = c;
407 }
408
409
410 FileReader::~FileReader()
411 {
412 }
413
414 bool FileReader::Exists()
415 {
416         if (fc.size() == 0)
417         {
418                 return(false);
419         }
420         else
421         {
422                 return(true);
423         }
424 }
425
426 std::string FileReader::GetLine(int x)
427 {
428         if ((x<0) || (x>fc.size()))
429                 return "";
430         return fc[x];
431 }
432
433 int FileReader::FileSize()
434 {
435         return fc.size();
436 }
437
438
439 std::vector<Module*> modules(255);
440 std::vector<ircd_module*> factory(255);
441
442 int MODCOUNT  = -1;
443
444