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 Module* IOHookModule = NULL;
71 // returns true if an extended mode character is in use
72 bool ModeDefined(char modechar, int type)
74 for (ExtModeListIter i = EMode.begin(); i < EMode.end(); i++)
76 if ((i->modechar == modechar) && (i->type == type))
84 bool ModeIsListMode(char modechar, int type)
86 for (ExtModeListIter i = EMode.begin(); i < EMode.end(); i++)
88 if ((i->modechar == modechar) && (i->type == type) && (i->list == true))
96 bool ModeDefinedOper(char modechar, int type)
98 for (ExtModeListIter i = EMode.begin(); i < EMode.end(); i++)
100 if ((i->modechar == modechar) && (i->type == type) && (i->needsoper == true))
108 // returns number of parameters for a custom mode when it is switched on
109 int ModeDefinedOn(char modechar, int type)
111 for (ExtModeListIter i = EMode.begin(); i < EMode.end(); i++)
113 if ((i->modechar == modechar) && (i->type == type))
115 return i->params_when_on;
121 // returns number of parameters for a custom mode when it is switched on
122 int ModeDefinedOff(char modechar, int type)
124 for (ExtModeListIter i = EMode.begin(); i < EMode.end(); i++)
126 if ((i->modechar == modechar) && (i->type == type))
128 return i->params_when_off;
134 // returns true if an extended mode character is in use
135 bool DoAddExtendedMode(char modechar, int type, bool requires_oper, int params_on, int params_off)
137 if (ModeDefined(modechar,type)) {
140 EMode.push_back(ExtMode(modechar,type,requires_oper,params_on,params_off));
144 // turns a mode into a listmode
145 void ModeMakeList(char modechar)
147 for (ExtModeListIter i = EMode.begin(); i < EMode.end(); i++)
149 if ((i->modechar == modechar) && (i->type == MT_CHANNEL))
158 // version is a simple class for holding a modules version number
160 Version::Version(int major, int minor, int revision, int build, int flags) : Major(major), Minor(minor), Revision(revision), Build(build), Flags(flags) { };
162 // admin is a simple class for holding a server's administrative info
164 Admin::Admin(std::string name, std::string email, std::string nick) : Name(name), Email(email), Nick(nick) { };
166 Request::Request(char* anydata, Module* src, Module* dst) : data(anydata), source(src), dest(dst) { };
168 char* Request::GetData()
173 Module* Request::GetSource()
178 Module* Request::GetDest()
183 char* Request::Send()
187 return dest->OnRequest(this);
195 Event::Event(char* anydata, Module* src, std::string eventid) : data(anydata), source(src), id(eventid) { };
197 char* Event::GetData()
202 Module* Event::GetSource()
209 FOREACH_MOD OnEvent(this);
213 std::string Event::GetEventID()
219 // These declarations define the behavours of the base class Module (which does nothing at all)
221 Module::Module(Server* Me) { }
222 Module::~Module() { }
223 void Module::OnUserConnect(userrec* user) { }
224 void Module::OnUserQuit(userrec* user, std::string message) { }
225 void Module::OnUserDisconnect(userrec* user) { }
226 void Module::OnUserJoin(userrec* user, chanrec* channel) { }
227 void Module::OnUserPart(userrec* user, chanrec* channel) { }
228 void Module::OnRehash(std::string parameter) { }
229 void Module::OnServerRaw(std::string &raw, bool inbound, userrec* user) { }
230 int Module::OnUserPreJoin(userrec* user, chanrec* chan, const char* cname) { return 0; }
231 int Module::OnExtendedMode(userrec* user, void* target, char modechar, int type, bool mode_on, string_list ¶ms) { return false; }
232 void Module::OnMode(userrec* user, void* dest, int target_type, std::string text) { };
233 Version Module::GetVersion() { return Version(1,0,0,0,VF_VENDOR); }
234 void Module::OnOper(userrec* user, std::string opertype) { };
235 void Module::OnInfo(userrec* user) { };
236 void Module::OnWhois(userrec* source, userrec* dest) { };
237 int Module::OnUserPreInvite(userrec* source,userrec* dest,chanrec* channel) { return 0; };
238 int Module::OnUserPreMessage(userrec* user,void* dest,int target_type, std::string &text) { return 0; };
239 int Module::OnUserPreNotice(userrec* user,void* dest,int target_type, std::string &text) { return 0; };
240 int Module::OnUserPreNick(userrec* user, std::string newnick) { return 0; };
241 void Module::OnUserPostNick(userrec* user, std::string oldnick) { };
242 int Module::OnAccessCheck(userrec* source,userrec* dest,chanrec* channel,int access_type) { return ACR_DEFAULT; };
243 void Module::On005Numeric(std::string &output) { };
244 int Module::OnKill(userrec* source, userrec* dest, std::string reason) { return 0; };
245 void Module::OnLoadModule(Module* mod,std::string name) { };
246 void Module::OnUnloadModule(Module* mod,std::string name) { };
247 void Module::OnBackgroundTimer(time_t curtime) { };
248 void Module::OnSendList(userrec* user, chanrec* channel, char mode) { };
249 int Module::OnPreCommand(std::string command, char **parameters, int pcnt, userrec *user) { return 0; };
250 bool Module::OnCheckReady(userrec* user) { return true; };
251 void Module::OnUserRegister(userrec* user) { };
252 int Module::OnUserPreKick(userrec* source, userrec* user, chanrec* chan, std::string reason) { return 0; };
253 void Module::OnUserKick(userrec* source, userrec* user, chanrec* chan, std::string reason) { };
254 int Module::OnRawMode(userrec* user, chanrec* chan, char mode, std::string param, bool adding, int pcnt) { return 0; };
255 int Module::OnCheckInvite(userrec* user, chanrec* chan) { return 0; };
256 int Module::OnCheckKey(userrec* user, chanrec* chan, std::string keygiven) { return 0; };
257 int Module::OnCheckLimit(userrec* user, chanrec* chan) { return 0; };
258 int Module::OnCheckBan(userrec* user, chanrec* chan) { return 0; };
259 void Module::OnStats(char symbol) { };
260 int Module::OnChangeLocalUserHost(userrec* user, std::string newhost) { return 0; };
261 int Module::OnChangeLocalUserGECOS(userrec* user, std::string newhost) { return 0; };
262 int Module::OnLocalTopicChange(userrec* user, chanrec* chan, std::string topic) { return 0; };
263 void Module::OnEvent(Event* event) { return; };
264 char* Module::OnRequest(Request* request) { return NULL; };
265 int Module::OnOperCompare(std::string password, std::string input) { return 0; };
266 void Module::OnGlobalOper(userrec* user) { };
267 void Module::OnGlobalConnect(userrec* user) { };
268 int Module::OnAddBan(userrec* source, chanrec* channel,std::string banmask) { return 0; };
269 int Module::OnDelBan(userrec* source, chanrec* channel,std::string banmask) { return 0; };
270 void Module::OnRawSocketAccept(int fd, std::string ip, int localport) { };
271 int Module::OnRawSocketWrite(int fd, char* buffer, int count) { return 0; };
272 void Module::OnRawSocketClose(int fd) { };
273 int Module::OnRawSocketRead(int fd, char* buffer, unsigned int count, int &readresult) { return 0; };
274 void Module::OnUserMessage(userrec* user, void* dest, int target_type, std::string text) { };
275 void Module::OnUserNotice(userrec* user, void* dest, int target_type, std::string text) { };
276 void Module::OnRemoteKill(userrec* source, userrec* dest, std::string reason) { };
277 void Module::OnUserInvite(userrec* source,userrec* dest,chanrec* channel) { };
278 void Module::OnPostLocalTopicChange(userrec* user, chanrec* chan, std::string topic) { };
279 void Module::OnGetServerDescription(std::string servername,std::string &description) { };
280 void Module::OnSyncUser(userrec* user, Module* proto, void* opaque) { };
281 void Module::OnSyncChannel(chanrec* chan, Module* proto, void* opaque) { };
282 void Module::ProtoSendMode(void* opaque, int target_type, void* target, std::string modeline) { };
283 void Module::OnSyncChannelMetaData(chanrec* chan, Module* proto,void* opaque, std::string extname) { };
284 void Module::OnSyncUserMetaData(userrec* user, Module* proto,void* opaque, std::string extname) { };
285 void Module::OnDecodeMetaData(int target_type, void* target, std::string extname, std::string extdata) { };
286 void Module::ProtoSendMetaData(void* opaque, int target_type, void* target, std::string extname, std::string extdata) { };
287 void Module::OnWallops(userrec* user, std::string text) { };
288 void Module::OnChangeHost(userrec* user, std::string newhost) { };
289 void Module::OnChangeName(userrec* user, std::string gecos) { };
290 void Module::OnAddGLine(long duration, userrec* source, std::string reason, std::string hostmask) { };
291 void Module::OnAddZLine(long duration, userrec* source, std::string reason, std::string ipmask) { };
292 void Module::OnAddKLine(long duration, userrec* source, std::string reason, std::string hostmask) { };
293 void Module::OnAddQLine(long duration, userrec* source, std::string reason, std::string nickmask) { };
294 void Module::OnAddELine(long duration, userrec* source, std::string reason, std::string hostmask) { };
295 void Module::OnDelGLine(userrec* source, std::string hostmask) { };
296 void Module::OnDelZLine(userrec* source, std::string ipmask) { };
297 void Module::OnDelKLine(userrec* source, std::string hostmask) { };
298 void Module::OnDelQLine(userrec* source, std::string nickmask) { };
299 void Module::OnDelELine(userrec* source, std::string hostmask) { };
300 void Module::OnCleanup(int target_type, void* item) { };
302 /* server is a wrapper class that provides methods to all of the C-style
303 * exports in the core
314 void Server::AddSocket(InspSocket* sock)
316 module_sockets.push_back(sock);
319 void Server::RehashServer()
321 WriteOpers("*** Rehashing config file");
322 Config->Read(false,NULL);
325 ServerConfig* Server::GetConfig()
330 std::string Server::GetVersion()
332 return ServerInstance->GetVersionString();
335 void Server::DelSocket(InspSocket* sock)
337 for (std::vector<InspSocket*>::iterator a = module_sockets.begin(); a < module_sockets.end(); a++)
341 module_sockets.erase(a);
347 void Server::SendOpers(std::string s)
349 WriteOpers("%s",s.c_str());
352 bool Server::MatchText(std::string sliteral, std::string spattern)
354 char literal[MAXBUF],pattern[MAXBUF];
355 strlcpy(literal,sliteral.c_str(),MAXBUF);
356 strlcpy(pattern,spattern.c_str(),MAXBUF);
357 return match(literal,pattern);
360 void Server::SendToModeMask(std::string modes, int flags, std::string text)
362 WriteMode(modes.c_str(),flags,"%s",text.c_str());
365 chanrec* Server::JoinUserToChannel(userrec* user, std::string cname, std::string key)
367 return add_channel(user,cname.c_str(),key.c_str(),false);
370 chanrec* Server::PartUserFromChannel(userrec* user, std::string cname, std::string reason)
372 return del_channel(user,cname.c_str(),reason.c_str(),false);
375 chanuserlist Server::GetUsers(chanrec* chan)
379 std::vector<char*> *list = chan->GetUsers();
380 for (std::vector<char*>::iterator i = list->begin(); i != list->end(); i++)
383 userl.push_back((userrec*)o);
387 void Server::ChangeUserNick(userrec* user, std::string nickname)
389 force_nickchange(user,nickname.c_str());
392 void Server::QuitUser(userrec* user, std::string reason)
394 kill_link(user,reason.c_str());
397 bool Server::IsUlined(std::string server)
399 return is_uline(server.c_str());
402 void Server::CallCommandHandler(std::string commandname, char** parameters, int pcnt, userrec* user)
404 ServerInstance->Parser->CallHandler(commandname,parameters,pcnt,user);
407 bool Server::IsValidModuleCommand(std::string commandname, int pcnt, userrec* user)
409 return ServerInstance->Parser->IsValidCommand(commandname, pcnt, user);
412 void Server::Log(int level, std::string s)
414 log(level,"%s",s.c_str());
417 void Server::AddCommand(command_t *f)
419 ServerInstance->Parser->CreateCommand(f);
422 void Server::SendMode(char **parameters, int pcnt, userrec *user)
424 ServerInstance->ModeGrok->ServerMode(parameters,pcnt,user);
427 void Server::Send(int Socket, std::string s)
429 Write(Socket,"%s",s.c_str());
432 void Server::SendServ(int Socket, std::string s)
434 WriteServ(Socket,"%s",s.c_str());
437 void Server::SendFrom(int Socket, userrec* User, std::string s)
439 WriteFrom(Socket,User,"%s",s.c_str());
442 void Server::SendTo(userrec* Source, userrec* Dest, std::string s)
446 // if source is NULL, then the message originates from the local server
447 Write(Dest->fd,":%s %s",this->GetServerName().c_str(),s.c_str());
451 // otherwise it comes from the user specified
452 WriteTo(Source,Dest,"%s",s.c_str());
456 void Server::SendChannelServerNotice(std::string ServName, chanrec* Channel, std::string text)
458 WriteChannelWithServ((char*)ServName.c_str(), Channel, "%s", text.c_str());
461 void Server::SendChannel(userrec* User, chanrec* Channel, std::string s,bool IncludeSender)
465 WriteChannel(Channel,User,"%s",s.c_str());
469 ChanExceptSender(Channel,User,"%s",s.c_str());
473 bool Server::CommonChannels(userrec* u1, userrec* u2)
475 return (common_channels(u1,u2) != 0);
478 void Server::SendCommon(userrec* User, std::string text,bool IncludeSender)
482 WriteCommon(User,"%s",text.c_str());
486 WriteCommonExcept(User,"%s",text.c_str());
490 void Server::SendWallops(userrec* User, std::string text)
492 WriteWallOps(User,false,"%s",text.c_str());
495 void Server::ChangeHost(userrec* user, std::string host)
497 ChangeDisplayedHost(user,host.c_str());
500 void Server::ChangeGECOS(userrec* user, std::string gecos)
502 ChangeName(user,gecos.c_str());
505 bool Server::IsNick(std::string nick)
507 return (isnick(nick.c_str()) != 0);
510 userrec* Server::FindNick(std::string nick)
515 userrec* Server::FindDescriptor(int socket)
517 return (socket < 65536 ? fd_ref_table[socket] : NULL);
520 chanrec* Server::FindChannel(std::string channel)
522 return FindChan(channel.c_str());
525 std::string Server::ChanMode(userrec* User, chanrec* Chan)
527 return cmode(User,Chan);
530 bool Server::IsOnChannel(userrec* User, chanrec* Chan)
532 return has_channel(User,Chan);
535 std::string Server::GetServerName()
537 return Config->ServerName;
540 std::string Server::GetNetworkName()
542 return Config->Network;
545 std::string Server::GetServerDescription()
547 return Config->ServerDesc;
550 Admin Server::GetAdmin()
552 return Admin(Config->AdminName,Config->AdminEmail,Config->AdminNick);
557 bool Server::AddExtendedMode(char modechar, int type, bool requires_oper, int params_when_on, int params_when_off)
559 if (((modechar >= 'A') && (modechar <= 'Z')) || ((modechar >= 'a') && (modechar <= 'z')))
561 if (type == MT_SERVER)
563 log(DEBUG,"*** API ERROR *** Modes of type MT_SERVER are reserved for future expansion");
566 if (((params_when_on>0) || (params_when_off>0)) && (type == MT_CLIENT))
568 log(DEBUG,"*** API ERROR *** Parameters on MT_CLIENT modes are not supported");
571 if ((params_when_on>1) || (params_when_off>1))
573 log(DEBUG,"*** API ERROR *** More than one parameter for an MT_CHANNEL mode is not yet supported");
576 return DoAddExtendedMode(modechar,type,requires_oper,params_when_on,params_when_off);
580 log(DEBUG,"*** API ERROR *** Muppet modechar detected.");
585 bool Server::AddExtendedListMode(char modechar)
587 bool res = DoAddExtendedMode(modechar,MT_CHANNEL,false,1,1);
589 ModeMakeList(modechar);
593 int Server::CountUsers(chanrec* c)
599 bool Server::UserToPseudo(userrec* user,std::string message)
601 unsigned int old_fd = user->fd;
602 user->fd = FD_MAGIC_NUMBER;
604 Write(old_fd,"ERROR :Closing link (%s@%s) [%s]",user->ident,user->host,message.c_str());
605 ServerInstance->SE->DelFd(old_fd);
611 bool Server::PseudoToUser(userrec* alive,userrec* zombie,std::string message)
613 zombie->fd = alive->fd;
614 alive->fd = FD_MAGIC_NUMBER;
615 alive->ClearBuffer();
616 Write(zombie->fd,":%s!%s@%s NICK %s",alive->nick,alive->ident,alive->host,zombie->nick);
617 kill_link(alive,message.c_str());
618 fd_ref_table[zombie->fd] = zombie;
619 for (unsigned int i = 0; i < zombie->chans.size(); i++)
621 if (zombie->chans[i].channel != NULL)
623 if (zombie->chans[i].channel->name)
625 chanrec* Ptr = zombie->chans[i].channel;
626 WriteFrom(zombie->fd,zombie,"JOIN %s",Ptr->name);
629 WriteServ(zombie->fd,"332 %s %s :%s", zombie->nick, Ptr->name, Ptr->topic);
630 WriteServ(zombie->fd,"333 %s %s %s %d", zombie->nick, Ptr->name, Ptr->setby, Ptr->topicset);
632 userlist(zombie,Ptr);
633 WriteServ(zombie->fd,"366 %s %s :End of /NAMES list.", zombie->nick, Ptr->name);
641 void Server::AddGLine(long duration, std::string source, std::string reason, std::string hostmask)
643 add_gline(duration, source.c_str(), reason.c_str(), hostmask.c_str());
646 void Server::AddQLine(long duration, std::string source, std::string reason, std::string nickname)
648 add_qline(duration, source.c_str(), reason.c_str(), nickname.c_str());
651 void Server::AddZLine(long duration, std::string source, std::string reason, std::string ipaddr)
653 add_zline(duration, source.c_str(), reason.c_str(), ipaddr.c_str());
656 void Server::AddKLine(long duration, std::string source, std::string reason, std::string hostmask)
658 add_kline(duration, source.c_str(), reason.c_str(), hostmask.c_str());
661 void Server::AddELine(long duration, std::string source, std::string reason, std::string hostmask)
663 add_eline(duration, source.c_str(), reason.c_str(), hostmask.c_str());
666 bool Server::DelGLine(std::string hostmask)
668 return del_gline(hostmask.c_str());
671 bool Server::DelQLine(std::string nickname)
673 return del_qline(nickname.c_str());
676 bool Server::DelZLine(std::string ipaddr)
678 return del_zline(ipaddr.c_str());
681 bool Server::DelKLine(std::string hostmask)
683 return del_kline(hostmask.c_str());
686 bool Server::DelELine(std::string hostmask)
688 return del_eline(hostmask.c_str());
691 long Server::CalcDuration(std::string delta)
693 return duration(delta.c_str());
696 bool Server::IsValidMask(std::string mask)
698 const char* dest = mask.c_str();
699 if (strchr(dest,'!')==0)
701 if (strchr(dest,'@')==0)
703 for (unsigned int i = 0; i < strlen(dest); i++)
706 for (unsigned int i = 0; i < strlen(dest); i++)
710 for (unsigned int i = 0; i < strlen(dest); i++)
716 for (unsigned int i = 0; i < strlen(dest); i++)
725 Module* Server::FindModule(std::string name)
727 for (int i = 0; i <= MODCOUNT; i++)
729 if (Config->module_names[i] == name)
737 ConfigReader::ConfigReader()
739 Config->ClearStack();
740 this->cache = new std::stringstream(std::stringstream::in | std::stringstream::out);
741 this->errorlog = new std::stringstream(std::stringstream::in | std::stringstream::out);
742 this->readerror = Config->LoadConf(CONFIG_FILE,this->cache,this->errorlog);
743 if (!this->readerror)
744 this->error = CONF_FILE_NOT_FOUND;
748 ConfigReader::~ConfigReader()
753 delete this->errorlog;
757 ConfigReader::ConfigReader(std::string filename)
759 Config->ClearStack();
760 this->cache = new std::stringstream(std::stringstream::in | std::stringstream::out);
761 this->errorlog = new std::stringstream(std::stringstream::in | std::stringstream::out);
762 this->readerror = Config->LoadConf(filename.c_str(),this->cache,this->errorlog);
763 if (!this->readerror)
764 this->error = CONF_FILE_NOT_FOUND;
767 std::string ConfigReader::ReadValue(std::string tag, std::string name, int index)
772 strlcpy(t,tag.c_str(),MAXBUF);
773 strlcpy(n,name.c_str(),MAXBUF);
774 int res = Config->ReadConf(cache,t,n,index,val);
777 this->error = CONF_VALUE_NOT_FOUND;
783 bool ConfigReader::ReadFlag(std::string tag, std::string name, int index)
788 strlcpy(t,tag.c_str(),MAXBUF);
789 strlcpy(n,name.c_str(),MAXBUF);
790 int res = Config->ReadConf(cache,t,n,index,val);
793 this->error = CONF_VALUE_NOT_FOUND;
797 return ((s == "yes") || (s == "YES") || (s == "true") || (s == "TRUE") || (s == "1"));
800 long ConfigReader::ReadInteger(std::string tag, std::string name, int index, bool needs_unsigned)
805 strlcpy(t,tag.c_str(),MAXBUF);
806 strlcpy(n,name.c_str(),MAXBUF);
807 int res = Config->ReadConf(cache,t,n,index,val);
810 this->error = CONF_VALUE_NOT_FOUND;
813 for (unsigned int i = 0; i < strlen(val); i++)
815 if (!isdigit(val[i]))
817 this->error = CONF_NOT_A_NUMBER;
821 if ((needs_unsigned) && (atoi(val)<0))
823 this->error = CONF_NOT_UNSIGNED;
829 long ConfigReader::GetError()
831 long olderr = this->error;
836 void ConfigReader::DumpErrors(bool bail, userrec* user)
840 printf("There were errors in your configuration:\n%s",errorlog->str().c_str());
848 WriteServ(user->fd,"NOTICE %s :There were errors in the configuration file:",user->nick);
849 while (!errorlog->eof())
851 errorlog->getline(dataline,1024);
852 WriteServ(user->fd,"NOTICE %s :%s",user->nick,dataline);
857 WriteOpers("There were errors in the configuration file:",user->nick);
858 while (!errorlog->eof())
860 errorlog->getline(dataline,1024);
861 WriteOpers(dataline);
869 int ConfigReader::Enumerate(std::string tag)
871 return Config->EnumConf(cache,tag.c_str());
874 int ConfigReader::EnumerateValues(std::string tag, int index)
876 return Config->EnumValues(cache, tag.c_str(), index);
879 bool ConfigReader::Verify()
881 return this->readerror;
885 FileReader::FileReader(std::string filename)
888 readfile(c,filename.c_str());
892 FileReader::FileReader()
896 void FileReader::LoadFile(std::string filename)
899 readfile(c,filename.c_str());
904 FileReader::~FileReader()
908 bool FileReader::Exists()
920 std::string FileReader::GetLine(int x)
922 if ((x<0) || ((unsigned)x>fc.size()))
927 int FileReader::FileSize()
933 std::vector<Module*> modules(255);
934 std::vector<ircd_module*> factory(255);