]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules.cpp
caacd9afc89071a89a4797ada92be970a58c6b7a
[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
124 // server is a wrapper class that provides methods to all of the C-style
125 // exports in the core
126 //
127
128 Server::Server()
129 {
130 }
131
132 Server::~Server()
133 {
134 }
135
136 void Server::SendOpers(std::string s)
137 {
138         WriteOpers("%s",s.c_str());
139 }
140
141 void Server::SendToModeMask(std::string modes, int flags, std::string text)
142 {
143         WriteMode(modes.c_str(),flags,"%s",text.c_str());
144 }
145
146 chanrec* Server::JoinUserToChannel(userrec* user, std::string cname, std::string key)
147 {
148         return add_channel(user,cname.c_str(),key.c_str());
149 }
150
151 chanrec* Server::PartUserFromChannel(userrec* user, std::string cname, std::string reason)
152 {
153         return del_channel(user,cname.c_str(),reason.c_str());
154 }
155
156 void Server::ChangeUserNick(userrec* user, std::string nickname)
157 {
158         force_nickchange(user,nickname.c_str());
159 }
160
161 void Server::QuitUser(userrec* user, std::string reason)
162 {
163         kill_link(user,reason.c_str());
164 }
165
166
167 void Server::Log(int level, std::string s)
168 {
169         log(level,"%s",s.c_str());
170 }
171
172 void Server::AddCommand(char* cmd, handlerfunc f, char flags, int minparams)
173 {
174         createcommand(cmd,f,flags,minparams);
175 }
176
177 void Server::SendMode(char **parameters, int pcnt, userrec *user)
178 {
179         server_mode(parameters,pcnt,user);
180 }
181
182 void Server::Send(int Socket, std::string s)
183 {
184         Write(Socket,"%s",s.c_str());
185 }
186
187 void Server::SendServ(int Socket, std::string s)
188 {
189         WriteServ(Socket,"%s",s.c_str());
190 }
191
192 void Server::SendFrom(int Socket, userrec* User, std::string s)
193 {
194         WriteFrom(Socket,User,"%s",s.c_str());
195 }
196
197 void Server::SendTo(userrec* Source, userrec* Dest, std::string s)
198 {
199         WriteTo(Source,Dest,"%s",s.c_str());
200 }
201
202 void Server::SendChannel(userrec* User, chanrec* Channel, std::string s,bool IncludeSender)
203 {
204         if (IncludeSender)
205         {
206                 WriteChannel(Channel,User,"%s",s.c_str());
207         }
208         else
209         {
210                 ChanExceptSender(Channel,User,"%s",s.c_str());
211         }
212 }
213
214 bool Server::CommonChannels(userrec* u1, userrec* u2)
215 {
216         return (common_channels(u1,u2) != 0);
217 }
218
219 void Server::SendCommon(userrec* User, std::string text,bool IncludeSender)
220 {
221         if (IncludeSender)
222         {
223                 WriteCommon(User,"%s",text.c_str());
224         }
225         else
226         {
227                 WriteCommonExcept(User,"%s",text.c_str());
228         }
229 }
230
231 void Server::SendWallops(userrec* User, std::string text)
232 {
233         WriteWallOps(User,"%s",text.c_str());
234 }
235
236 bool Server::IsNick(std::string nick)
237 {
238         return (isnick(nick.c_str()) != 0);
239 }
240
241 userrec* Server::FindNick(std::string nick)
242 {
243         return Find(nick);
244 }
245
246 chanrec* Server::FindChannel(std::string channel)
247 {
248         return FindChan(channel.c_str());
249 }
250
251 std::string Server::ChanMode(userrec* User, chanrec* Chan)
252 {
253         return cmode(User,Chan);
254 }
255
256 std::string Server::GetServerName()
257 {
258         return getservername();
259 }
260
261 std::string Server::GetNetworkName()
262 {
263         return getnetworkname();
264 }
265
266 Admin Server::GetAdmin()
267 {
268         return Admin(getadminname(),getadminemail(),getadminnick());
269 }
270
271
272
273 bool Server::AddExtendedMode(char modechar, int type, bool requires_oper, int params_when_on, int params_when_off)
274 {
275         if (type == MT_SERVER)
276         {
277                 log(DEBUG,"*** API ERROR *** Modes of type MT_SERVER are reserved for future expansion");
278                 return false;
279         }
280         if (((params_when_on>0) || (params_when_off>0)) && (type == MT_CLIENT))
281         {
282                 log(DEBUG,"*** API ERROR *** Parameters on MT_CLIENT modes are not supported");
283                 return false;
284         }
285         if ((params_when_on>1) || (params_when_off>1))
286         {
287                 log(DEBUG,"*** API ERROR *** More than one parameter for an MT_CHANNEL mode is not yet supported");
288                 return false;
289         }
290         return DoAddExtendedMode(modechar,type,requires_oper,params_when_on,params_when_off);
291 }
292
293
294 ConfigReader::ConfigReader()
295 {
296         fname = CONFIG_FILE;
297 }
298
299
300 ConfigReader::~ConfigReader()
301 {
302 }
303
304
305 ConfigReader::ConfigReader(std::string filename) : fname(filename) { };
306
307 std::string ConfigReader::ReadValue(std::string tag, std::string name, int index)
308 {
309         char val[MAXBUF];
310         ReadConf(fname.c_str(),tag.c_str(),name.c_str(),index,val);
311         return val;
312 }
313
314
315 int ConfigReader::Enumerate(std::string tag)
316 {
317         return EnumConf(fname.c_str(),tag.c_str());
318 }
319
320
321 bool ConfigReader::Verify()
322 {
323         return true;
324 }
325
326
327 FileReader::FileReader(std::string filename)
328 {
329         file_cache c;
330         readfile(c,filename.c_str());
331         this->fc = c;
332 }
333
334 FileReader::FileReader()
335 {
336 }
337
338 void FileReader::LoadFile(std::string filename)
339 {
340         file_cache c;
341         readfile(c,filename.c_str());
342         this->fc = c;
343 }
344
345
346 FileReader::~FileReader()
347 {
348 }
349
350 bool FileReader::Exists()
351 {
352         if (fc.size() == 0)
353         {
354                 return(false);
355         }
356         else
357         {
358                 return(true);
359         }
360 }
361
362 std::string FileReader::GetLine(int x)
363 {
364         if ((x<0) || (x>fc.size()))
365                 return "";
366         return fc[x];
367 }
368
369 int FileReader::FileSize()
370 {
371         return fc.size();
372 }
373
374
375 std::vector<Module*> modules(255);
376 std::vector<ircd_module*> factory(255);
377
378 int MODCOUNT  = -1;
379
380