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