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