13 #include "inspircd_io.h"
19 // class type for holding an extended mode character - internal to core
21 class ExtMode : public classbase
30 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) { };
33 typedef std::vector<ExtMode> ExtModeList;
34 typedef ExtModeList::iterator ExtModeListIter;
38 // returns true if an extended mode character is in use
39 bool ModeDefined(char modechar, int type)
41 log(DEBUG,"Size of extmodes vector is %d",EMode.size());
42 for (ExtModeListIter i = EMode.begin(); i < EMode.end(); i++)
44 log(DEBUG,"i->modechar==%c, modechar=%c, i->type=%d, type=%d",i->modechar,modechar,i->type,type);
45 if ((i->modechar == modechar) && (i->type == type))
53 bool ModeIsListMode(char modechar, int type)
55 log(DEBUG,"Size of extmodes vector is %d",EMode.size());
56 for (ExtModeListIter i = EMode.begin(); i < EMode.end(); i++)
58 log(DEBUG,"i->modechar==%c, modechar=%c, i->type=%d, type=%d",i->modechar,modechar,i->type,type);
59 if ((i->modechar == modechar) && (i->type == type) && (i->list == true))
67 bool ModeDefinedOper(char modechar, int type)
69 log(DEBUG,"Size of extmodes vector is %d",EMode.size());
70 for (ExtModeListIter i = EMode.begin(); i < EMode.end(); i++)
72 log(DEBUG,"i->modechar==%c, modechar=%c, i->type=%d, type=%d",i->modechar,modechar,i->type,type);
73 if ((i->modechar == modechar) && (i->type == type) && (i->needsoper == true))
81 // returns number of parameters for a custom mode when it is switched on
82 int ModeDefinedOn(char modechar, int type)
84 for (ExtModeListIter i = EMode.begin(); i < EMode.end(); i++)
86 if ((i->modechar == modechar) && (i->type == type))
88 return i->params_when_on;
94 // returns number of parameters for a custom mode when it is switched on
95 int ModeDefinedOff(char modechar, int type)
97 for (ExtModeListIter i = EMode.begin(); i < EMode.end(); i++)
99 if ((i->modechar == modechar) && (i->type == type))
101 return i->params_when_off;
107 // returns true if an extended mode character is in use
108 bool DoAddExtendedMode(char modechar, int type, bool requires_oper, int params_on, int params_off)
110 if (ModeDefined(modechar,type)) {
113 EMode.push_back(ExtMode(modechar,type,requires_oper,params_on,params_off));
117 // turns a mode into a listmode
118 void ModeMakeList(char modechar)
120 for (ExtModeListIter i = EMode.begin(); i < EMode.end(); i++)
122 if ((i->modechar == modechar) && (i->type == MT_CHANNEL))
131 // version is a simple class for holding a modules version number
133 Version::Version(int major, int minor, int revision, int build) : Major(major), Minor(minor), Revision(revision), Build(build) { };
135 // admin is a simple class for holding a server's administrative info
137 Admin::Admin(std::string name, std::string email, std::string nick) : Name(name), Email(email), Nick(nick) { };
140 Module::~Module() { }
141 void Module::OnUserConnect(userrec* user) { }
142 void Module::OnUserQuit(userrec* user) { }
143 void Module::OnUserJoin(userrec* user, chanrec* channel) { }
144 void Module::OnUserPart(userrec* user, chanrec* channel) { }
145 void Module::OnPacketTransmit(char *p) { }
146 void Module::OnPacketReceive(char *p) { }
147 void Module::OnRehash() { }
148 void Module::OnServerRaw(std::string &raw, bool inbound, userrec* user) { }
149 int Module::OnUserPreJoin(userrec* user, chanrec* chan, const char* cname) { return 0; }
150 int Module::OnExtendedMode(userrec* user, void* target, char modechar, int type, bool mode_on, string_list ¶ms) { return false; }
151 Version Module::GetVersion() { return Version(1,0,0,0); }
152 void Module::OnOper(userrec* user) { };
153 void Module::OnInfo(userrec* user) { };
154 void Module::OnWhois(userrec* source, userrec* dest) { };
155 int Module::OnUserPreMessage(userrec* user,void* dest,int target_type, std::string text) { return 0; };
156 int Module::OnUserPreNotice(userrec* user,void* dest,int target_type, std::string text) { return 0; };
157 int Module::OnUserPreNick(userrec* user, std::string newnick) { return 0; };
158 int Module::OnAccessCheck(userrec* source,userrec* dest,chanrec* channel,int access_type) { return ACR_DEFAULT; };
160 // server is a wrapper class that provides methods to all of the C-style
161 // exports in the core
172 void Server::SendOpers(std::string s)
174 WriteOpers("%s",s.c_str());
177 bool Server::MatchText(std::string sliteral, std::string spattern)
179 char literal[MAXBUF],pattern[MAXBUF];
180 strncpy(literal,sliteral.c_str(),MAXBUF);
181 strncpy(pattern,spattern.c_str(),MAXBUF);
182 return match(literal,pattern);
185 void Server::SendToModeMask(std::string modes, int flags, std::string text)
187 WriteMode(modes.c_str(),flags,"%s",text.c_str());
190 chanrec* Server::JoinUserToChannel(userrec* user, std::string cname, std::string key)
192 return add_channel(user,cname.c_str(),key.c_str(),true);
195 chanrec* Server::PartUserFromChannel(userrec* user, std::string cname, std::string reason)
197 return del_channel(user,cname.c_str(),reason.c_str(),false);
200 void Server::ChangeUserNick(userrec* user, std::string nickname)
202 force_nickchange(user,nickname.c_str());
205 void Server::QuitUser(userrec* user, std::string reason)
207 send_network_quit(user->nick,reason.c_str());
208 kill_link(user,reason.c_str());
211 bool Server::IsUlined(std::string server)
213 return is_uline(server.c_str());
216 void Server::CallCommandHandler(std::string commandname, char** parameters, int pcnt, userrec* user)
218 call_handler(commandname.c_str(),parameters,pcnt,user);
221 void Server::Log(int level, std::string s)
223 log(level,"%s",s.c_str());
226 void Server::AddCommand(char* cmd, handlerfunc f, char flags, int minparams)
228 createcommand(cmd,f,flags,minparams);
231 void Server::SendMode(char **parameters, int pcnt, userrec *user)
233 server_mode(parameters,pcnt,user);
236 void Server::Send(int Socket, std::string s)
238 Write(Socket,"%s",s.c_str());
241 void Server::SendServ(int Socket, std::string s)
243 WriteServ(Socket,"%s",s.c_str());
246 void Server::SendFrom(int Socket, userrec* User, std::string s)
248 WriteFrom(Socket,User,"%s",s.c_str());
251 void Server::SendTo(userrec* Source, userrec* Dest, std::string s)
255 // if source is NULL, then the message originates from the local server
256 Write(Dest->fd,":%s %s",this->GetServerName().c_str(),s.c_str());
260 // otherwise it comes from the user specified
261 WriteTo(Source,Dest,"%s",s.c_str());
265 void Server::SendChannel(userrec* User, chanrec* Channel, std::string s,bool IncludeSender)
269 WriteChannel(Channel,User,"%s",s.c_str());
273 ChanExceptSender(Channel,User,"%s",s.c_str());
277 bool Server::CommonChannels(userrec* u1, userrec* u2)
279 return (common_channels(u1,u2) != 0);
282 void Server::SendCommon(userrec* User, std::string text,bool IncludeSender)
286 WriteCommon(User,"%s",text.c_str());
290 WriteCommonExcept(User,"%s",text.c_str());
294 void Server::SendWallops(userrec* User, std::string text)
296 WriteWallOps(User,false,"%s",text.c_str());
299 void Server::ChangeHost(userrec* user, std::string host)
301 ChangeDisplayedHost(user,host.c_str());
304 void Server::ChangeGECOS(userrec* user, std::string gecos)
306 ChangeName(user,gecos.c_str());
309 bool Server::IsNick(std::string nick)
311 return (isnick(nick.c_str()) != 0);
314 userrec* Server::FindNick(std::string nick)
319 chanrec* Server::FindChannel(std::string channel)
321 return FindChan(channel.c_str());
324 std::string Server::ChanMode(userrec* User, chanrec* Chan)
326 return cmode(User,Chan);
329 bool Server::IsOnChannel(userrec* User, chanrec* Chan)
331 return has_channel(User,Chan);
334 std::string Server::GetServerName()
336 return getservername();
339 std::string Server::GetNetworkName()
341 return getnetworkname();
344 Admin Server::GetAdmin()
346 return Admin(getadminname(),getadminemail(),getadminnick());
351 bool Server::AddExtendedMode(char modechar, int type, bool requires_oper, int params_when_on, int params_when_off)
353 if (type == MT_SERVER)
355 log(DEBUG,"*** API ERROR *** Modes of type MT_SERVER are reserved for future expansion");
358 if (((params_when_on>0) || (params_when_off>0)) && (type == MT_CLIENT))
360 log(DEBUG,"*** API ERROR *** Parameters on MT_CLIENT modes are not supported");
363 if ((params_when_on>1) || (params_when_off>1))
365 log(DEBUG,"*** API ERROR *** More than one parameter for an MT_CHANNEL mode is not yet supported");
368 return DoAddExtendedMode(modechar,type,requires_oper,params_when_on,params_when_off);
371 bool Server::AddExtendedListMode(char modechar)
373 bool res = DoAddExtendedMode(modechar,MT_CHANNEL,false,1,1);
375 ModeMakeList(modechar);
379 int Server::CountUsers(chanrec* c)
385 ConfigReader::ConfigReader()
387 this->cache = new std::stringstream(std::stringstream::in | std::stringstream::out);
388 this->error = LoadConf(CONFIG_FILE,this->cache);
392 ConfigReader::~ConfigReader()
399 ConfigReader::ConfigReader(std::string filename)
401 this->cache = new std::stringstream(std::stringstream::in | std::stringstream::out);
402 this->error = LoadConf(filename.c_str(),this->cache);
405 std::string ConfigReader::ReadValue(std::string tag, std::string name, int index)
410 strncpy(t,tag.c_str(),MAXBUF);
411 strncpy(n,name.c_str(),MAXBUF);
412 ReadConf(cache,t,n,index,val);
413 return std::string(val);
417 int ConfigReader::Enumerate(std::string tag)
419 return EnumConf(cache,tag.c_str());
422 int ConfigReader::EnumerateValues(std::string tag, int index)
424 return EnumValues(cache, tag.c_str(), index);
427 bool ConfigReader::Verify()
433 FileReader::FileReader(std::string filename)
436 readfile(c,filename.c_str());
440 FileReader::FileReader()
444 void FileReader::LoadFile(std::string filename)
447 readfile(c,filename.c_str());
452 FileReader::~FileReader()
456 bool FileReader::Exists()
468 std::string FileReader::GetLine(int x)
470 if ((x<0) || (x>fc.size()))
475 int FileReader::FileSize()
481 std::vector<Module*> modules(255);
482 std::vector<ircd_module*> factory(255);