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 SocketEngine* SE;
55 extern ServerConfig *Config;
56 extern InspIRCd* ServerInstance;
58 extern std::vector<Module*> modules;
59 extern std::vector<ircd_module*> factory;
60 extern std::vector<InspSocket*> module_sockets;
61 extern CommandParser *Parser;
65 extern userrec* fd_ref_table[65536];
67 extern user_hash clientlist;
68 extern chan_hash chanlist;
69 extern command_table cmdlist;
72 Module* IOHookModule = NULL;
74 // returns true if an extended mode character is in use
75 bool ModeDefined(char modechar, int type)
77 for (ExtModeListIter i = EMode.begin(); i < EMode.end(); i++)
79 if ((i->modechar == modechar) && (i->type == type))
87 bool ModeIsListMode(char modechar, int type)
89 for (ExtModeListIter i = EMode.begin(); i < EMode.end(); i++)
91 if ((i->modechar == modechar) && (i->type == type) && (i->list == true))
99 bool ModeDefinedOper(char modechar, int type)
101 for (ExtModeListIter i = EMode.begin(); i < EMode.end(); i++)
103 if ((i->modechar == modechar) && (i->type == type) && (i->needsoper == true))
111 // returns number of parameters for a custom mode when it is switched on
112 int ModeDefinedOn(char modechar, int type)
114 for (ExtModeListIter i = EMode.begin(); i < EMode.end(); i++)
116 if ((i->modechar == modechar) && (i->type == type))
118 return i->params_when_on;
124 // returns number of parameters for a custom mode when it is switched on
125 int ModeDefinedOff(char modechar, int type)
127 for (ExtModeListIter i = EMode.begin(); i < EMode.end(); i++)
129 if ((i->modechar == modechar) && (i->type == type))
131 return i->params_when_off;
137 // returns true if an extended mode character is in use
138 bool DoAddExtendedMode(char modechar, int type, bool requires_oper, int params_on, int params_off)
140 if (ModeDefined(modechar,type)) {
143 EMode.push_back(ExtMode(modechar,type,requires_oper,params_on,params_off));
147 // turns a mode into a listmode
148 void ModeMakeList(char modechar)
150 for (ExtModeListIter i = EMode.begin(); i < EMode.end(); i++)
152 if ((i->modechar == modechar) && (i->type == MT_CHANNEL))
161 // version is a simple class for holding a modules version number
163 Version::Version(int major, int minor, int revision, int build, int flags) : Major(major), Minor(minor), Revision(revision), Build(build), Flags(flags) { };
165 // admin is a simple class for holding a server's administrative info
167 Admin::Admin(std::string name, std::string email, std::string nick) : Name(name), Email(email), Nick(nick) { };
169 Request::Request(char* anydata, Module* src, Module* dst) : data(anydata), source(src), dest(dst) { };
171 char* Request::GetData()
176 Module* Request::GetSource()
181 Module* Request::GetDest()
186 char* Request::Send()
190 return dest->OnRequest(this);
198 Event::Event(char* anydata, Module* src, std::string eventid) : data(anydata), source(src), id(eventid) { };
200 char* Event::GetData()
205 Module* Event::GetSource()
212 FOREACH_MOD OnEvent(this);
216 std::string Event::GetEventID()
222 // These declarations define the behavours of the base class Module (which does nothing at all)
224 Module::Module(Server* Me) { }
225 Module::~Module() { }
226 void Module::OnUserConnect(userrec* user) { }
227 void Module::OnUserQuit(userrec* user, std::string message) { }
228 void Module::OnUserDisconnect(userrec* user) { }
229 void Module::OnUserJoin(userrec* user, chanrec* channel) { }
230 void Module::OnUserPart(userrec* user, chanrec* channel) { }
231 void Module::OnRehash(std::string parameter) { }
232 void Module::OnServerRaw(std::string &raw, bool inbound, userrec* user) { }
233 int Module::OnUserPreJoin(userrec* user, chanrec* chan, const char* cname) { return 0; }
234 int Module::OnExtendedMode(userrec* user, void* target, char modechar, int type, bool mode_on, string_list ¶ms) { return false; }
235 void Module::OnMode(userrec* user, void* dest, int target_type, std::string text) { };
236 Version Module::GetVersion() { return Version(1,0,0,0,VF_VENDOR); }
237 void Module::OnOper(userrec* user, std::string opertype) { };
238 void Module::OnInfo(userrec* user) { };
239 void Module::OnWhois(userrec* source, userrec* dest) { };
240 int Module::OnUserPreInvite(userrec* source,userrec* dest,chanrec* channel) { return 0; };
241 int Module::OnUserPreMessage(userrec* user,void* dest,int target_type, std::string &text) { return 0; };
242 int Module::OnUserPreNotice(userrec* user,void* dest,int target_type, std::string &text) { return 0; };
243 int Module::OnUserPreNick(userrec* user, std::string newnick) { return 0; };
244 void Module::OnUserPostNick(userrec* user, std::string oldnick) { };
245 int Module::OnAccessCheck(userrec* source,userrec* dest,chanrec* channel,int access_type) { return ACR_DEFAULT; };
246 void Module::On005Numeric(std::string &output) { };
247 int Module::OnKill(userrec* source, userrec* dest, std::string reason) { return 0; };
248 void Module::OnLoadModule(Module* mod,std::string name) { };
249 void Module::OnUnloadModule(Module* mod,std::string name) { };
250 void Module::OnBackgroundTimer(time_t curtime) { };
251 void Module::OnSendList(userrec* user, chanrec* channel, char mode) { };
252 int Module::OnPreCommand(std::string command, char **parameters, int pcnt, userrec *user) { return 0; };
253 bool Module::OnCheckReady(userrec* user) { return true; };
254 void Module::OnUserRegister(userrec* user) { };
255 int Module::OnUserPreKick(userrec* source, userrec* user, chanrec* chan, std::string reason) { return 0; };
256 void Module::OnUserKick(userrec* source, userrec* user, chanrec* chan, std::string reason) { };
257 int Module::OnRawMode(userrec* user, chanrec* chan, char mode, std::string param, bool adding, int pcnt) { return 0; };
258 int Module::OnCheckInvite(userrec* user, chanrec* chan) { return 0; };
259 int Module::OnCheckKey(userrec* user, chanrec* chan, std::string keygiven) { return 0; };
260 int Module::OnCheckLimit(userrec* user, chanrec* chan) { return 0; };
261 int Module::OnCheckBan(userrec* user, chanrec* chan) { return 0; };
262 void Module::OnStats(char symbol) { };
263 int Module::OnChangeLocalUserHost(userrec* user, std::string newhost) { return 0; };
264 int Module::OnChangeLocalUserGECOS(userrec* user, std::string newhost) { return 0; };
265 int Module::OnLocalTopicChange(userrec* user, chanrec* chan, std::string topic) { return 0; };
266 void Module::OnEvent(Event* event) { return; };
267 char* Module::OnRequest(Request* request) { return NULL; };
268 int Module::OnOperCompare(std::string password, std::string input) { return 0; };
269 void Module::OnGlobalOper(userrec* user) { };
270 void Module::OnGlobalConnect(userrec* user) { };
271 int Module::OnAddBan(userrec* source, chanrec* channel,std::string banmask) { return 0; };
272 int Module::OnDelBan(userrec* source, chanrec* channel,std::string banmask) { return 0; };
273 void Module::OnRawSocketAccept(int fd, std::string ip, int localport) { };
274 int Module::OnRawSocketWrite(int fd, char* buffer, int count) { return 0; };
275 void Module::OnRawSocketClose(int fd) { };
276 int Module::OnRawSocketRead(int fd, char* buffer, unsigned int count, int &readresult) { return 0; };
277 void Module::OnUserMessage(userrec* user, void* dest, int target_type, std::string text) { };
278 void Module::OnUserNotice(userrec* user, void* dest, int target_type, std::string text) { };
279 void Module::OnRemoteKill(userrec* source, userrec* dest, std::string reason) { };
280 void Module::OnUserInvite(userrec* source,userrec* dest,chanrec* channel) { };
281 void Module::OnPostLocalTopicChange(userrec* user, chanrec* chan, std::string topic) { };
282 void Module::OnGetServerDescription(std::string servername,std::string &description) { };
283 void Module::OnSyncUser(userrec* user, Module* proto, void* opaque) { };
284 void Module::OnSyncChannel(chanrec* chan, Module* proto, void* opaque) { };
285 void Module::ProtoSendMode(void* opaque, int target_type, void* target, std::string modeline) { };
286 void Module::OnSyncChannelMetaData(chanrec* chan, Module* proto,void* opaque, std::string extname) { };
287 void Module::OnSyncUserMetaData(userrec* user, Module* proto,void* opaque, std::string extname) { };
288 void Module::OnDecodeMetaData(int target_type, void* target, std::string extname, std::string extdata) { };
289 void Module::ProtoSendMetaData(void* opaque, int target_type, void* target, std::string extname, std::string extdata) { };
290 void Module::OnWallops(userrec* user, std::string text) { };
291 void Module::OnChangeHost(userrec* user, std::string newhost) { };
292 void Module::OnChangeName(userrec* user, std::string gecos) { };
293 void Module::OnAddGLine(long duration, userrec* source, std::string reason, std::string hostmask) { };
294 void Module::OnAddZLine(long duration, userrec* source, std::string reason, std::string ipmask) { };
295 void Module::OnAddKLine(long duration, userrec* source, std::string reason, std::string hostmask) { };
296 void Module::OnAddQLine(long duration, userrec* source, std::string reason, std::string nickmask) { };
297 void Module::OnAddELine(long duration, userrec* source, std::string reason, std::string hostmask) { };
298 void Module::OnDelGLine(userrec* source, std::string hostmask) { };
299 void Module::OnDelZLine(userrec* source, std::string ipmask) { };
300 void Module::OnDelKLine(userrec* source, std::string hostmask) { };
301 void Module::OnDelQLine(userrec* source, std::string nickmask) { };
302 void Module::OnDelELine(userrec* source, std::string hostmask) { };
303 void Module::OnCleanup(int target_type, void* item) { };
305 /* server is a wrapper class that provides methods to all of the C-style
306 * exports in the core
317 void Server::AddSocket(InspSocket* sock)
319 module_sockets.push_back(sock);
322 void Server::RehashServer()
324 WriteOpers("*** Rehashing config file");
325 Config->Read(false,NULL);
328 ServerConfig* Server::GetConfig()
333 std::string Server::GetVersion()
335 return ServerInstance->GetVersionString();
338 void Server::DelSocket(InspSocket* sock)
340 for (std::vector<InspSocket*>::iterator a = module_sockets.begin(); a < module_sockets.end(); a++)
344 module_sockets.erase(a);
350 void Server::SendOpers(std::string s)
352 WriteOpers("%s",s.c_str());
355 bool Server::MatchText(std::string sliteral, std::string spattern)
357 char literal[MAXBUF],pattern[MAXBUF];
358 strlcpy(literal,sliteral.c_str(),MAXBUF);
359 strlcpy(pattern,spattern.c_str(),MAXBUF);
360 return match(literal,pattern);
363 void Server::SendToModeMask(std::string modes, int flags, std::string text)
365 WriteMode(modes.c_str(),flags,"%s",text.c_str());
368 chanrec* Server::JoinUserToChannel(userrec* user, std::string cname, std::string key)
370 return add_channel(user,cname.c_str(),key.c_str(),false);
373 chanrec* Server::PartUserFromChannel(userrec* user, std::string cname, std::string reason)
375 return del_channel(user,cname.c_str(),reason.c_str(),false);
378 chanuserlist Server::GetUsers(chanrec* chan)
382 std::vector<char*> *list = chan->GetUsers();
383 for (std::vector<char*>::iterator i = list->begin(); i != list->end(); i++)
386 userl.push_back((userrec*)o);
390 void Server::ChangeUserNick(userrec* user, std::string nickname)
392 force_nickchange(user,nickname.c_str());
395 void Server::QuitUser(userrec* user, std::string reason)
397 kill_link(user,reason.c_str());
400 bool Server::IsUlined(std::string server)
402 return is_uline(server.c_str());
405 void Server::CallCommandHandler(std::string commandname, char** parameters, int pcnt, userrec* user)
407 call_handler(commandname,parameters,pcnt,user);
410 bool Server::IsValidModuleCommand(std::string commandname, int pcnt, userrec* user)
412 return is_valid_cmd(commandname, pcnt, user);
415 void Server::Log(int level, std::string s)
417 log(level,"%s",s.c_str());
420 void Server::AddCommand(char* cmd, handlerfunc f, char flags, int minparams, char* source)
422 createcommand(cmd,f,flags,minparams,source);
425 void Server::SendMode(char **parameters, int pcnt, userrec *user)
427 server_mode(parameters,pcnt,user);
430 void Server::Send(int Socket, std::string s)
432 Write(Socket,"%s",s.c_str());
435 void Server::SendServ(int Socket, std::string s)
437 WriteServ(Socket,"%s",s.c_str());
440 void Server::SendFrom(int Socket, userrec* User, std::string s)
442 WriteFrom(Socket,User,"%s",s.c_str());
445 void Server::SendTo(userrec* Source, userrec* Dest, std::string s)
449 // if source is NULL, then the message originates from the local server
450 Write(Dest->fd,":%s %s",this->GetServerName().c_str(),s.c_str());
454 // otherwise it comes from the user specified
455 WriteTo(Source,Dest,"%s",s.c_str());
459 void Server::SendChannelServerNotice(std::string ServName, chanrec* Channel, std::string text)
461 WriteChannelWithServ((char*)ServName.c_str(), Channel, "%s", text.c_str());
464 void Server::SendChannel(userrec* User, chanrec* Channel, std::string s,bool IncludeSender)
468 WriteChannel(Channel,User,"%s",s.c_str());
472 ChanExceptSender(Channel,User,"%s",s.c_str());
476 bool Server::CommonChannels(userrec* u1, userrec* u2)
478 return (common_channels(u1,u2) != 0);
481 void Server::SendCommon(userrec* User, std::string text,bool IncludeSender)
485 WriteCommon(User,"%s",text.c_str());
489 WriteCommonExcept(User,"%s",text.c_str());
493 void Server::SendWallops(userrec* User, std::string text)
495 WriteWallOps(User,false,"%s",text.c_str());
498 void Server::ChangeHost(userrec* user, std::string host)
500 ChangeDisplayedHost(user,host.c_str());
503 void Server::ChangeGECOS(userrec* user, std::string gecos)
505 ChangeName(user,gecos.c_str());
508 bool Server::IsNick(std::string nick)
510 return (isnick(nick.c_str()) != 0);
513 userrec* Server::FindNick(std::string nick)
518 userrec* Server::FindDescriptor(int socket)
520 return (socket < 65536 ? fd_ref_table[socket] : NULL);
523 chanrec* Server::FindChannel(std::string channel)
525 return FindChan(channel.c_str());
528 std::string Server::ChanMode(userrec* User, chanrec* Chan)
530 return cmode(User,Chan);
533 bool Server::IsOnChannel(userrec* User, chanrec* Chan)
535 return has_channel(User,Chan);
538 std::string Server::GetServerName()
540 return Config->ServerName;
543 std::string Server::GetNetworkName()
545 return Config->Network;
548 std::string Server::GetServerDescription()
550 return Config->ServerDesc;
553 Admin Server::GetAdmin()
555 return Admin(Config->AdminName,Config->AdminEmail,Config->AdminNick);
560 bool Server::AddExtendedMode(char modechar, int type, bool requires_oper, int params_when_on, int params_when_off)
562 if (((modechar >= 'A') && (modechar <= 'Z')) || ((modechar >= 'a') && (modechar <= 'z')))
564 if (type == MT_SERVER)
566 log(DEBUG,"*** API ERROR *** Modes of type MT_SERVER are reserved for future expansion");
569 if (((params_when_on>0) || (params_when_off>0)) && (type == MT_CLIENT))
571 log(DEBUG,"*** API ERROR *** Parameters on MT_CLIENT modes are not supported");
574 if ((params_when_on>1) || (params_when_off>1))
576 log(DEBUG,"*** API ERROR *** More than one parameter for an MT_CHANNEL mode is not yet supported");
579 return DoAddExtendedMode(modechar,type,requires_oper,params_when_on,params_when_off);
583 log(DEBUG,"*** API ERROR *** Muppet modechar detected.");
588 bool Server::AddExtendedListMode(char modechar)
590 bool res = DoAddExtendedMode(modechar,MT_CHANNEL,false,1,1);
592 ModeMakeList(modechar);
596 int Server::CountUsers(chanrec* c)
602 bool Server::UserToPseudo(userrec* user,std::string message)
604 unsigned int old_fd = user->fd;
605 user->fd = FD_MAGIC_NUMBER;
607 Write(old_fd,"ERROR :Closing link (%s@%s) [%s]",user->ident,user->host,message.c_str());
614 bool Server::PseudoToUser(userrec* alive,userrec* zombie,std::string message)
616 zombie->fd = alive->fd;
617 alive->fd = FD_MAGIC_NUMBER;
618 alive->ClearBuffer();
619 Write(zombie->fd,":%s!%s@%s NICK %s",alive->nick,alive->ident,alive->host,zombie->nick);
620 kill_link(alive,message.c_str());
621 fd_ref_table[zombie->fd] = zombie;
622 for (unsigned int i = 0; i < zombie->chans.size(); i++)
624 if (zombie->chans[i].channel != NULL)
626 if (zombie->chans[i].channel->name)
628 chanrec* Ptr = zombie->chans[i].channel;
629 WriteFrom(zombie->fd,zombie,"JOIN %s",Ptr->name);
632 WriteServ(zombie->fd,"332 %s %s :%s", zombie->nick, Ptr->name, Ptr->topic);
633 WriteServ(zombie->fd,"333 %s %s %s %d", zombie->nick, Ptr->name, Ptr->setby, Ptr->topicset);
635 userlist(zombie,Ptr);
636 WriteServ(zombie->fd,"366 %s %s :End of /NAMES list.", zombie->nick, Ptr->name);
644 void Server::AddGLine(long duration, std::string source, std::string reason, std::string hostmask)
646 add_gline(duration, source.c_str(), reason.c_str(), hostmask.c_str());
649 void Server::AddQLine(long duration, std::string source, std::string reason, std::string nickname)
651 add_qline(duration, source.c_str(), reason.c_str(), nickname.c_str());
654 void Server::AddZLine(long duration, std::string source, std::string reason, std::string ipaddr)
656 add_zline(duration, source.c_str(), reason.c_str(), ipaddr.c_str());
659 void Server::AddKLine(long duration, std::string source, std::string reason, std::string hostmask)
661 add_kline(duration, source.c_str(), reason.c_str(), hostmask.c_str());
664 void Server::AddELine(long duration, std::string source, std::string reason, std::string hostmask)
666 add_eline(duration, source.c_str(), reason.c_str(), hostmask.c_str());
669 bool Server::DelGLine(std::string hostmask)
671 return del_gline(hostmask.c_str());
674 bool Server::DelQLine(std::string nickname)
676 return del_qline(nickname.c_str());
679 bool Server::DelZLine(std::string ipaddr)
681 return del_zline(ipaddr.c_str());
684 bool Server::DelKLine(std::string hostmask)
686 return del_kline(hostmask.c_str());
689 bool Server::DelELine(std::string hostmask)
691 return del_eline(hostmask.c_str());
694 long Server::CalcDuration(std::string delta)
696 return duration(delta.c_str());
699 bool Server::IsValidMask(std::string mask)
701 const char* dest = mask.c_str();
702 if (strchr(dest,'!')==0)
704 if (strchr(dest,'@')==0)
706 for (unsigned int i = 0; i < strlen(dest); i++)
709 for (unsigned int i = 0; i < strlen(dest); i++)
713 for (unsigned int i = 0; i < strlen(dest); i++)
719 for (unsigned int i = 0; i < strlen(dest); i++)
728 Module* Server::FindModule(std::string name)
730 for (int i = 0; i <= MODCOUNT; i++)
732 if (Config->module_names[i] == name)
740 ConfigReader::ConfigReader()
742 Config->ClearStack();
743 this->cache = new std::stringstream(std::stringstream::in | std::stringstream::out);
744 this->errorlog = new std::stringstream(std::stringstream::in | std::stringstream::out);
745 this->readerror = Config->LoadConf(CONFIG_FILE,this->cache,this->errorlog);
746 if (!this->readerror)
747 this->error = CONF_FILE_NOT_FOUND;
751 ConfigReader::~ConfigReader()
756 delete this->errorlog;
760 ConfigReader::ConfigReader(std::string filename)
762 Config->ClearStack();
763 this->cache = new std::stringstream(std::stringstream::in | std::stringstream::out);
764 this->errorlog = new std::stringstream(std::stringstream::in | std::stringstream::out);
765 this->readerror = Config->LoadConf(filename.c_str(),this->cache,this->errorlog);
766 if (!this->readerror)
767 this->error = CONF_FILE_NOT_FOUND;
770 std::string ConfigReader::ReadValue(std::string tag, std::string name, int index)
775 strlcpy(t,tag.c_str(),MAXBUF);
776 strlcpy(n,name.c_str(),MAXBUF);
777 int res = Config->ReadConf(cache,t,n,index,val);
780 this->error = CONF_VALUE_NOT_FOUND;
786 bool ConfigReader::ReadFlag(std::string tag, std::string name, int index)
791 strlcpy(t,tag.c_str(),MAXBUF);
792 strlcpy(n,name.c_str(),MAXBUF);
793 int res = Config->ReadConf(cache,t,n,index,val);
796 this->error = CONF_VALUE_NOT_FOUND;
800 return ((s == "yes") || (s == "YES") || (s == "true") || (s == "TRUE") || (s == "1"));
803 long ConfigReader::ReadInteger(std::string tag, std::string name, int index, bool needs_unsigned)
808 strlcpy(t,tag.c_str(),MAXBUF);
809 strlcpy(n,name.c_str(),MAXBUF);
810 int res = Config->ReadConf(cache,t,n,index,val);
813 this->error = CONF_VALUE_NOT_FOUND;
816 for (unsigned int i = 0; i < strlen(val); i++)
818 if (!isdigit(val[i]))
820 this->error = CONF_NOT_A_NUMBER;
824 if ((needs_unsigned) && (atoi(val)<0))
826 this->error = CONF_NOT_UNSIGNED;
832 long ConfigReader::GetError()
834 long olderr = this->error;
839 void ConfigReader::DumpErrors(bool bail, userrec* user)
843 printf("There were errors in your configuration:\n%s",errorlog->str().c_str());
851 WriteServ(user->fd,"NOTICE %s :There were errors in the configuration file:",user->nick);
852 while (!errorlog->eof())
854 errorlog->getline(dataline,1024);
855 WriteServ(user->fd,"NOTICE %s :%s",user->nick,dataline);
860 WriteOpers("There were errors in the configuration file:",user->nick);
861 while (!errorlog->eof())
863 errorlog->getline(dataline,1024);
864 WriteOpers(dataline);
872 int ConfigReader::Enumerate(std::string tag)
874 return Config->EnumConf(cache,tag.c_str());
877 int ConfigReader::EnumerateValues(std::string tag, int index)
879 return Config->EnumValues(cache, tag.c_str(), index);
882 bool ConfigReader::Verify()
884 return this->readerror;
888 FileReader::FileReader(std::string filename)
891 readfile(c,filename.c_str());
895 FileReader::FileReader()
899 void FileReader::LoadFile(std::string filename)
902 readfile(c,filename.c_str());
907 FileReader::~FileReader()
911 bool FileReader::Exists()
923 std::string FileReader::GetLine(int x)
925 if ((x<0) || ((unsigned)x>fc.size()))
930 int FileReader::FileSize()
936 std::vector<Module*> modules(255);
937 std::vector<ircd_module*> factory(255);