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