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