1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * Inspire is copyright (C) 2002-2004 ChatSpike-Dev.
7 * <brain@chatspike.net>
8 * <Craig@chatspike.net>
10 * Written by Craig Edwards, Craig McLure, and others.
11 * This program is free but copyrighted software; see
12 * the file COPYING for details.
14 * ---------------------------------------------------
19 #include "inspircd_config.h"
21 #include "inspircd_io.h"
23 #include <sys/errno.h>
27 #include <ext/hash_map>
45 #include "inspstring.h"
46 #include "helperfuncs.h"
49 #include "socketengine.h"
52 #include "command_parse.h"
54 extern ServerConfig *Config;
55 extern InspIRCd* ServerInstance;
57 extern std::vector<Module*> modules;
58 extern std::vector<ircd_module*> factory;
59 extern std::vector<InspSocket*> module_sockets;
62 extern userrec* fd_ref_table[65536];
64 extern user_hash clientlist;
65 extern chan_hash chanlist;
66 extern command_table cmdlist;
69 // returns true if an extended mode character is in use
70 bool ModeDefined(char modechar, int type)
72 for (ExtModeListIter i = EMode.begin(); i < EMode.end(); i++)
74 if ((i->modechar == modechar) && (i->type == type))
82 bool ModeIsListMode(char modechar, int type)
84 for (ExtModeListIter i = EMode.begin(); i < EMode.end(); i++)
86 if ((i->modechar == modechar) && (i->type == type) && (i->list == true))
94 bool ModeDefinedOper(char modechar, int type)
96 for (ExtModeListIter i = EMode.begin(); i < EMode.end(); i++)
98 if ((i->modechar == modechar) && (i->type == type) && (i->needsoper == true))
106 // returns number of parameters for a custom mode when it is switched on
107 int ModeDefinedOn(char modechar, int type)
109 for (ExtModeListIter i = EMode.begin(); i < EMode.end(); i++)
111 if ((i->modechar == modechar) && (i->type == type))
113 return i->params_when_on;
119 // returns number of parameters for a custom mode when it is switched on
120 int ModeDefinedOff(char modechar, int type)
122 for (ExtModeListIter i = EMode.begin(); i < EMode.end(); i++)
124 if ((i->modechar == modechar) && (i->type == type))
126 return i->params_when_off;
132 // returns true if an extended mode character is in use
133 bool DoAddExtendedMode(char modechar, int type, bool requires_oper, int params_on, int params_off)
135 if (ModeDefined(modechar,type)) {
138 EMode.push_back(ExtMode(modechar,type,requires_oper,params_on,params_off));
142 // turns a mode into a listmode
143 void ModeMakeList(char modechar)
145 for (ExtModeListIter i = EMode.begin(); i < EMode.end(); i++)
147 if ((i->modechar == modechar) && (i->type == MT_CHANNEL))
156 // version is a simple class for holding a modules version number
158 Version::Version(int major, int minor, int revision, int build, int flags) : Major(major), Minor(minor), Revision(revision), Build(build), Flags(flags) { };
160 // admin is a simple class for holding a server's administrative info
162 Admin::Admin(std::string name, std::string email, std::string nick) : Name(name), Email(email), Nick(nick) { };
164 Request::Request(char* anydata, Module* src, Module* dst) : data(anydata), source(src), dest(dst) { };
166 char* Request::GetData()
171 Module* Request::GetSource()
176 Module* Request::GetDest()
181 char* Request::Send()
185 return dest->OnRequest(this);
193 Event::Event(char* anydata, Module* src, std::string eventid) : data(anydata), source(src), id(eventid) { };
195 char* Event::GetData()
200 Module* Event::GetSource()
207 FOREACH_MOD OnEvent(this);
211 std::string Event::GetEventID()
217 // These declarations define the behavours of the base class Module (which does nothing at all)
219 Module::Module(Server* Me) { }
220 Module::~Module() { }
221 void Module::OnUserConnect(userrec* user) { }
222 void Module::OnUserQuit(userrec* user, std::string message) { }
223 void Module::OnUserDisconnect(userrec* user) { }
224 void Module::OnUserJoin(userrec* user, chanrec* channel) { }
225 void Module::OnUserPart(userrec* user, chanrec* channel) { }
226 void Module::OnRehash(std::string parameter) { }
227 void Module::OnServerRaw(std::string &raw, bool inbound, userrec* user) { }
228 int Module::OnUserPreJoin(userrec* user, chanrec* chan, const char* cname) { return 0; }
229 int Module::OnExtendedMode(userrec* user, void* target, char modechar, int type, bool mode_on, string_list ¶ms) { return false; }
230 void Module::OnMode(userrec* user, void* dest, int target_type, std::string text) { };
231 Version Module::GetVersion() { return Version(1,0,0,0,VF_VENDOR); }
232 void Module::OnOper(userrec* user, std::string opertype) { };
233 void Module::OnInfo(userrec* user) { };
234 void Module::OnWhois(userrec* source, userrec* dest) { };
235 int Module::OnUserPreInvite(userrec* source,userrec* dest,chanrec* channel) { return 0; };
236 int Module::OnUserPreMessage(userrec* user,void* dest,int target_type, std::string &text) { return 0; };
237 int Module::OnUserPreNotice(userrec* user,void* dest,int target_type, std::string &text) { return 0; };
238 int Module::OnUserPreNick(userrec* user, std::string newnick) { return 0; };
239 void Module::OnUserPostNick(userrec* user, std::string oldnick) { };
240 int Module::OnAccessCheck(userrec* source,userrec* dest,chanrec* channel,int access_type) { return ACR_DEFAULT; };
241 void Module::On005Numeric(std::string &output) { };
242 int Module::OnKill(userrec* source, userrec* dest, std::string reason) { return 0; };
243 void Module::OnLoadModule(Module* mod,std::string name) { };
244 void Module::OnUnloadModule(Module* mod,std::string name) { };
245 void Module::OnBackgroundTimer(time_t curtime) { };
246 void Module::OnSendList(userrec* user, chanrec* channel, char mode) { };
247 int Module::OnPreCommand(std::string command, char **parameters, int pcnt, userrec *user, bool validated) { return 0; };
248 bool Module::OnCheckReady(userrec* user) { return true; };
249 void Module::OnUserRegister(userrec* user) { };
250 int Module::OnUserPreKick(userrec* source, userrec* user, chanrec* chan, std::string reason) { return 0; };
251 void Module::OnUserKick(userrec* source, userrec* user, chanrec* chan, std::string reason) { };
252 int Module::OnRawMode(userrec* user, chanrec* chan, char mode, std::string param, bool adding, int pcnt) { return 0; };
253 int Module::OnCheckInvite(userrec* user, chanrec* chan) { return 0; };
254 int Module::OnCheckKey(userrec* user, chanrec* chan, std::string keygiven) { return 0; };
255 int Module::OnCheckLimit(userrec* user, chanrec* chan) { return 0; };
256 int Module::OnCheckBan(userrec* user, chanrec* chan) { return 0; };
257 void Module::OnStats(char symbol) { };
258 int Module::OnChangeLocalUserHost(userrec* user, std::string newhost) { return 0; };
259 int Module::OnChangeLocalUserGECOS(userrec* user, std::string newhost) { return 0; };
260 int Module::OnLocalTopicChange(userrec* user, chanrec* chan, std::string topic) { return 0; };
261 void Module::OnEvent(Event* event) { return; };
262 char* Module::OnRequest(Request* request) { return NULL; };
263 int Module::OnOperCompare(std::string password, std::string input) { return 0; };
264 void Module::OnGlobalOper(userrec* user) { };
265 void Module::OnGlobalConnect(userrec* user) { };
266 int Module::OnAddBan(userrec* source, chanrec* channel,std::string banmask) { return 0; };
267 int Module::OnDelBan(userrec* source, chanrec* channel,std::string banmask) { return 0; };
268 void Module::OnRawSocketAccept(int fd, std::string ip, int localport) { };
269 int Module::OnRawSocketWrite(int fd, char* buffer, int count) { return 0; };
270 void Module::OnRawSocketClose(int fd) { };
271 int Module::OnRawSocketRead(int fd, char* buffer, unsigned int count, int &readresult) { return 0; };
272 void Module::OnUserMessage(userrec* user, void* dest, int target_type, std::string text) { };
273 void Module::OnUserNotice(userrec* user, void* dest, int target_type, std::string text) { };
274 void Module::OnRemoteKill(userrec* source, userrec* dest, std::string reason) { };
275 void Module::OnUserInvite(userrec* source,userrec* dest,chanrec* channel) { };
276 void Module::OnPostLocalTopicChange(userrec* user, chanrec* chan, std::string topic) { };
277 void Module::OnGetServerDescription(std::string servername,std::string &description) { };
278 void Module::OnSyncUser(userrec* user, Module* proto, void* opaque) { };
279 void Module::OnSyncChannel(chanrec* chan, Module* proto, void* opaque) { };
280 void Module::ProtoSendMode(void* opaque, int target_type, void* target, std::string modeline) { };
281 void Module::OnSyncChannelMetaData(chanrec* chan, Module* proto,void* opaque, std::string extname) { };
282 void Module::OnSyncUserMetaData(userrec* user, Module* proto,void* opaque, std::string extname) { };
283 void Module::OnDecodeMetaData(int target_type, void* target, std::string extname, std::string extdata) { };
284 void Module::ProtoSendMetaData(void* opaque, int target_type, void* target, std::string extname, std::string extdata) { };
285 void Module::OnWallops(userrec* user, std::string text) { };
286 void Module::OnChangeHost(userrec* user, std::string newhost) { };
287 void Module::OnChangeName(userrec* user, std::string gecos) { };
288 void Module::OnAddGLine(long duration, userrec* source, std::string reason, std::string hostmask) { };
289 void Module::OnAddZLine(long duration, userrec* source, std::string reason, std::string ipmask) { };
290 void Module::OnAddKLine(long duration, userrec* source, std::string reason, std::string hostmask) { };
291 void Module::OnAddQLine(long duration, userrec* source, std::string reason, std::string nickmask) { };
292 void Module::OnAddELine(long duration, userrec* source, std::string reason, std::string hostmask) { };
293 void Module::OnDelGLine(userrec* source, std::string hostmask) { };
294 void Module::OnDelZLine(userrec* source, std::string ipmask) { };
295 void Module::OnDelKLine(userrec* source, std::string hostmask) { };
296 void Module::OnDelQLine(userrec* source, std::string nickmask) { };
297 void Module::OnDelELine(userrec* source, std::string hostmask) { };
298 void Module::OnCleanup(int target_type, void* item) { };
300 /* server is a wrapper class that provides methods to all of the C-style
301 * exports in the core
312 void Server::AddSocket(InspSocket* sock)
314 module_sockets.push_back(sock);
317 void Server::RehashServer()
319 WriteOpers("*** Rehashing config file");
320 Config->Read(false,NULL);
323 ServerConfig* Server::GetConfig()
328 std::string Server::GetVersion()
330 return ServerInstance->GetVersionString();
333 void Server::DelSocket(InspSocket* sock)
335 for (std::vector<InspSocket*>::iterator a = module_sockets.begin(); a < module_sockets.end(); a++)
339 module_sockets.erase(a);
345 void Server::SendOpers(std::string s)
347 WriteOpers("%s",s.c_str());
350 bool Server::MatchText(std::string sliteral, std::string spattern)
352 char literal[MAXBUF],pattern[MAXBUF];
353 strlcpy(literal,sliteral.c_str(),MAXBUF);
354 strlcpy(pattern,spattern.c_str(),MAXBUF);
355 return match(literal,pattern);
358 void Server::SendToModeMask(std::string modes, int flags, std::string text)
360 WriteMode(modes.c_str(),flags,"%s",text.c_str());
363 chanrec* Server::JoinUserToChannel(userrec* user, std::string cname, std::string key)
365 return add_channel(user,cname.c_str(),key.c_str(),false);
368 chanrec* Server::PartUserFromChannel(userrec* user, std::string cname, std::string reason)
370 return del_channel(user,cname.c_str(),reason.c_str(),false);
373 chanuserlist Server::GetUsers(chanrec* chan)
377 std::vector<char*> *list = chan->GetUsers();
378 for (std::vector<char*>::iterator i = list->begin(); i != list->end(); i++)
381 userl.push_back((userrec*)o);
385 void Server::ChangeUserNick(userrec* user, std::string nickname)
387 force_nickchange(user,nickname.c_str());
390 void Server::QuitUser(userrec* user, std::string reason)
392 kill_link(user,reason.c_str());
395 bool Server::IsUlined(std::string server)
397 return is_uline(server.c_str());
400 void Server::CallCommandHandler(std::string commandname, char** parameters, int pcnt, userrec* user)
402 ServerInstance->Parser->CallHandler(commandname,parameters,pcnt,user);
405 bool Server::IsValidModuleCommand(std::string commandname, int pcnt, userrec* user)
407 return ServerInstance->Parser->IsValidCommand(commandname, pcnt, user);
410 void Server::Log(int level, std::string s)
412 log(level,"%s",s.c_str());
415 void Server::AddCommand(command_t *f)
417 ServerInstance->Parser->CreateCommand(f);
420 void Server::SendMode(char **parameters, int pcnt, userrec *user)
422 ServerInstance->ModeGrok->ServerMode(parameters,pcnt,user);
425 void Server::Send(int Socket, std::string s)
427 Write(Socket,"%s",s.c_str());
430 void Server::SendServ(int Socket, std::string s)
432 WriteServ(Socket,"%s",s.c_str());
435 void Server::SendFrom(int Socket, userrec* User, std::string s)
437 WriteFrom(Socket,User,"%s",s.c_str());
440 void Server::SendTo(userrec* Source, userrec* Dest, std::string s)
444 // if source is NULL, then the message originates from the local server
445 Write(Dest->fd,":%s %s",this->GetServerName().c_str(),s.c_str());
449 // otherwise it comes from the user specified
450 WriteTo(Source,Dest,"%s",s.c_str());
454 void Server::SendChannelServerNotice(std::string ServName, chanrec* Channel, std::string text)
456 WriteChannelWithServ((char*)ServName.c_str(), Channel, "%s", text.c_str());
459 void Server::SendChannel(userrec* User, chanrec* Channel, std::string s,bool IncludeSender)
463 WriteChannel(Channel,User,"%s",s.c_str());
467 ChanExceptSender(Channel,User,"%s",s.c_str());
471 bool Server::CommonChannels(userrec* u1, userrec* u2)
473 return (common_channels(u1,u2) != 0);
476 void Server::SendCommon(userrec* User, std::string text,bool IncludeSender)
480 WriteCommon(User,"%s",text.c_str());
484 WriteCommonExcept(User,"%s",text.c_str());
488 void Server::SendWallops(userrec* User, std::string text)
490 WriteWallOps(User,false,"%s",text.c_str());
493 void Server::ChangeHost(userrec* user, std::string host)
495 ChangeDisplayedHost(user,host.c_str());
498 void Server::ChangeGECOS(userrec* user, std::string gecos)
500 ChangeName(user,gecos.c_str());
503 bool Server::IsNick(std::string nick)
505 return (isnick(nick.c_str()) != 0);
508 userrec* Server::FindNick(std::string nick)
513 userrec* Server::FindDescriptor(int socket)
515 return (socket < 65536 ? fd_ref_table[socket] : NULL);
518 chanrec* Server::FindChannel(std::string channel)
520 return FindChan(channel.c_str());
523 std::string Server::ChanMode(userrec* User, chanrec* Chan)
525 return cmode(User,Chan);
528 bool Server::IsOnChannel(userrec* User, chanrec* Chan)
530 return has_channel(User,Chan);
533 std::string Server::GetServerName()
535 return Config->ServerName;
538 std::string Server::GetNetworkName()
540 return Config->Network;
543 std::string Server::GetServerDescription()
545 return Config->ServerDesc;
548 Admin Server::GetAdmin()
550 return Admin(Config->AdminName,Config->AdminEmail,Config->AdminNick);
555 bool Server::AddExtendedMode(char modechar, int type, bool requires_oper, int params_when_on, int params_when_off)
557 if (((modechar >= 'A') && (modechar <= 'Z')) || ((modechar >= 'a') && (modechar <= 'z')))
559 if (type == MT_SERVER)
561 log(DEBUG,"*** API ERROR *** Modes of type MT_SERVER are reserved for future expansion");
564 if (((params_when_on>0) || (params_when_off>0)) && (type == MT_CLIENT))
566 log(DEBUG,"*** API ERROR *** Parameters on MT_CLIENT modes are not supported");
569 if ((params_when_on>1) || (params_when_off>1))
571 log(DEBUG,"*** API ERROR *** More than one parameter for an MT_CHANNEL mode is not yet supported");
574 return DoAddExtendedMode(modechar,type,requires_oper,params_when_on,params_when_off);
578 log(DEBUG,"*** API ERROR *** Muppet modechar detected.");
583 bool Server::AddExtendedListMode(char modechar)
585 bool res = DoAddExtendedMode(modechar,MT_CHANNEL,false,1,1);
587 ModeMakeList(modechar);
591 int Server::CountUsers(chanrec* c)
597 bool Server::UserToPseudo(userrec* user,std::string message)
599 unsigned int old_fd = user->fd;
600 user->fd = FD_MAGIC_NUMBER;
602 Write(old_fd,"ERROR :Closing link (%s@%s) [%s]",user->ident,user->host,message.c_str());
603 ServerInstance->SE->DelFd(old_fd);
609 bool Server::PseudoToUser(userrec* alive,userrec* zombie,std::string message)
611 zombie->fd = alive->fd;
612 alive->fd = FD_MAGIC_NUMBER;
613 alive->ClearBuffer();
614 Write(zombie->fd,":%s!%s@%s NICK %s",alive->nick,alive->ident,alive->host,zombie->nick);
615 kill_link(alive,message.c_str());
616 fd_ref_table[zombie->fd] = zombie;
617 for (unsigned int i = 0; i < zombie->chans.size(); i++)
619 if (zombie->chans[i].channel != NULL)
621 if (zombie->chans[i].channel->name)
623 chanrec* Ptr = zombie->chans[i].channel;
624 WriteFrom(zombie->fd,zombie,"JOIN %s",Ptr->name);
627 WriteServ(zombie->fd,"332 %s %s :%s", zombie->nick, Ptr->name, Ptr->topic);
628 WriteServ(zombie->fd,"333 %s %s %s %d", zombie->nick, Ptr->name, Ptr->setby, Ptr->topicset);
630 userlist(zombie,Ptr);
631 WriteServ(zombie->fd,"366 %s %s :End of /NAMES list.", zombie->nick, Ptr->name);
639 void Server::AddGLine(long duration, std::string source, std::string reason, std::string hostmask)
641 add_gline(duration, source.c_str(), reason.c_str(), hostmask.c_str());
644 void Server::AddQLine(long duration, std::string source, std::string reason, std::string nickname)
646 add_qline(duration, source.c_str(), reason.c_str(), nickname.c_str());
649 void Server::AddZLine(long duration, std::string source, std::string reason, std::string ipaddr)
651 add_zline(duration, source.c_str(), reason.c_str(), ipaddr.c_str());
654 void Server::AddKLine(long duration, std::string source, std::string reason, std::string hostmask)
656 add_kline(duration, source.c_str(), reason.c_str(), hostmask.c_str());
659 void Server::AddELine(long duration, std::string source, std::string reason, std::string hostmask)
661 add_eline(duration, source.c_str(), reason.c_str(), hostmask.c_str());
664 bool Server::DelGLine(std::string hostmask)
666 return del_gline(hostmask.c_str());
669 bool Server::DelQLine(std::string nickname)
671 return del_qline(nickname.c_str());
674 bool Server::DelZLine(std::string ipaddr)
676 return del_zline(ipaddr.c_str());
679 bool Server::DelKLine(std::string hostmask)
681 return del_kline(hostmask.c_str());
684 bool Server::DelELine(std::string hostmask)
686 return del_eline(hostmask.c_str());
689 long Server::CalcDuration(std::string delta)
691 return duration(delta.c_str());
694 bool Server::IsValidMask(std::string mask)
696 const char* dest = mask.c_str();
697 if (strchr(dest,'!')==0)
699 if (strchr(dest,'@')==0)
701 for (unsigned int i = 0; i < strlen(dest); i++)
704 for (unsigned int i = 0; i < strlen(dest); i++)
708 for (unsigned int i = 0; i < strlen(dest); i++)
714 for (unsigned int i = 0; i < strlen(dest); i++)
723 Module* Server::FindModule(std::string name)
725 for (int i = 0; i <= MODCOUNT; i++)
727 if (Config->module_names[i] == name)
735 ConfigReader::ConfigReader()
737 Config->ClearStack();
738 this->cache = new std::stringstream(std::stringstream::in | std::stringstream::out);
739 this->errorlog = new std::stringstream(std::stringstream::in | std::stringstream::out);
740 this->readerror = Config->LoadConf(CONFIG_FILE,this->cache,this->errorlog);
741 if (!this->readerror)
742 this->error = CONF_FILE_NOT_FOUND;
746 ConfigReader::~ConfigReader()
751 delete this->errorlog;
755 ConfigReader::ConfigReader(std::string filename)
757 Config->ClearStack();
758 this->cache = new std::stringstream(std::stringstream::in | std::stringstream::out);
759 this->errorlog = new std::stringstream(std::stringstream::in | std::stringstream::out);
760 this->readerror = Config->LoadConf(filename.c_str(),this->cache,this->errorlog);
761 if (!this->readerror)
762 this->error = CONF_FILE_NOT_FOUND;
765 std::string ConfigReader::ReadValue(std::string tag, std::string name, int index)
770 strlcpy(t,tag.c_str(),MAXBUF);
771 strlcpy(n,name.c_str(),MAXBUF);
772 int res = Config->ReadConf(cache,t,n,index,val);
775 this->error = CONF_VALUE_NOT_FOUND;
781 bool ConfigReader::ReadFlag(std::string tag, std::string name, int index)
786 strlcpy(t,tag.c_str(),MAXBUF);
787 strlcpy(n,name.c_str(),MAXBUF);
788 int res = Config->ReadConf(cache,t,n,index,val);
791 this->error = CONF_VALUE_NOT_FOUND;
795 return ((s == "yes") || (s == "YES") || (s == "true") || (s == "TRUE") || (s == "1"));
798 long ConfigReader::ReadInteger(std::string tag, std::string name, int index, bool needs_unsigned)
803 strlcpy(t,tag.c_str(),MAXBUF);
804 strlcpy(n,name.c_str(),MAXBUF);
805 int res = Config->ReadConf(cache,t,n,index,val);
808 this->error = CONF_VALUE_NOT_FOUND;
811 for (unsigned int i = 0; i < strlen(val); i++)
813 if (!isdigit(val[i]))
815 this->error = CONF_NOT_A_NUMBER;
819 if ((needs_unsigned) && (atoi(val)<0))
821 this->error = CONF_NOT_UNSIGNED;
827 long ConfigReader::GetError()
829 long olderr = this->error;
834 void ConfigReader::DumpErrors(bool bail, userrec* user)
838 printf("There were errors in your configuration:\n%s",errorlog->str().c_str());
846 WriteServ(user->fd,"NOTICE %s :There were errors in the configuration file:",user->nick);
847 while (!errorlog->eof())
849 errorlog->getline(dataline,1024);
850 WriteServ(user->fd,"NOTICE %s :%s",user->nick,dataline);
855 WriteOpers("There were errors in the configuration file:",user->nick);
856 while (!errorlog->eof())
858 errorlog->getline(dataline,1024);
859 WriteOpers(dataline);
867 int ConfigReader::Enumerate(std::string tag)
869 return Config->EnumConf(cache,tag.c_str());
872 int ConfigReader::EnumerateValues(std::string tag, int index)
874 return Config->EnumValues(cache, tag.c_str(), index);
877 bool ConfigReader::Verify()
879 return this->readerror;
883 FileReader::FileReader(std::string filename)
886 readfile(c,filename.c_str());
890 FileReader::FileReader()
894 void FileReader::LoadFile(std::string filename)
897 readfile(c,filename.c_str());
902 FileReader::~FileReader()
906 bool FileReader::Exists()
918 std::string FileReader::GetLine(int x)
920 if ((x<0) || ((unsigned)x>fc.size()))
925 int FileReader::FileSize()
931 std::vector<Module*> modules(255);
932 std::vector<ircd_module*> factory(255);