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