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