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 * ---------------------------------------------------
18 #include "inspircd_io.h"
19 #include "inspircd_util.h"
20 #include "inspircd_config.h"
23 #include <sys/errno.h>
24 #include <sys/ioctl.h>
25 #include <sys/utsname.h>
30 #include <ext/hash_map>
42 #include "connection.h"
54 #include "inspstring.h"
57 #define nspace __gnu_cxx
65 extern std::vector<Module*> modules;
66 extern std::vector<ircd_module*> factory;
71 extern char ServerName[MAXBUF];
72 extern char Network[MAXBUF];
73 extern char ServerDesc[MAXBUF];
74 extern char AdminName[MAXBUF];
75 extern char AdminEmail[MAXBUF];
76 extern char AdminNick[MAXBUF];
77 extern char diepass[MAXBUF];
78 extern char restartpass[MAXBUF];
79 extern char motd[MAXBUF];
80 extern char rules[MAXBUF];
81 extern char list[MAXBUF];
82 extern char PrefixQuit[MAXBUF];
83 extern char DieValue[MAXBUF];
86 extern int WHOWAS_STALE;
87 extern int WHOWAS_MAX;
89 extern time_t startup_time;
90 extern int NetBufferSize;
91 extern int MaxWhoResults;
92 extern time_t nb_start;
94 extern std::vector<int> fd_reap;
95 extern std::vector<std::string> module_names;
97 extern int boundPortCount;
99 extern int UDPportCount;
100 extern int ports[MAXSOCKS];
101 extern int defaultRoute;
103 extern std::vector<long> auth_cookies;
104 extern std::stringstream config_f;
106 extern serverrec* me[32];
108 extern FILE *log_file;
114 template<> struct hash<in_addr>
116 template<> struct nspace::hash<in_addr>
119 size_t operator()(const struct in_addr &a) const
122 memcpy(&q,&a,sizeof(size_t));
127 template<> struct hash<string>
129 template<> struct nspace::hash<string>
132 size_t operator()(const string &s) const
135 static struct hash<const char *> strhash;
136 strlcpy(a,s.c_str(),MAXBUF);
146 bool operator()(const string& s1, const string& s2) const
148 char a[MAXBUF],b[MAXBUF];
149 strlcpy(a,s1.c_str(),MAXBUF);
150 strlcpy(b,s2.c_str(),MAXBUF);
153 return (strcasecmp(a,b) == 0);
158 struct InAddr_HashComp
161 bool operator()(const in_addr &s1, const in_addr &s2) const
166 memcpy(&q,&s1,sizeof(size_t));
167 memcpy(&p,&s2,sizeof(size_t));
175 typedef nspace::hash_map<std::string, userrec*, nspace::hash<string>, StrHashComp> user_hash;
176 typedef nspace::hash_map<std::string, chanrec*, nspace::hash<string>, StrHashComp> chan_hash;
177 typedef nspace::hash_map<in_addr,string*, nspace::hash<in_addr>, InAddr_HashComp> address_cache;
178 typedef std::deque<command_t> command_table;
181 extern user_hash clientlist;
182 extern chan_hash chanlist;
183 extern user_hash whowas;
184 extern command_table cmdlist;
185 extern file_cache MOTD;
186 extern file_cache RULES;
187 extern address_cache IP;
190 // class type for holding an extended mode character - internal to core
192 class ExtMode : public classbase
201 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) { };
204 typedef std::vector<ExtMode> ExtModeList;
205 typedef ExtModeList::iterator ExtModeListIter;
210 // returns true if an extended mode character is in use
211 bool ModeDefined(char modechar, int type)
213 for (ExtModeListIter i = EMode.begin(); i < EMode.end(); i++)
215 if ((i->modechar == modechar) && (i->type == type))
223 bool ModeIsListMode(char modechar, int type)
225 for (ExtModeListIter i = EMode.begin(); i < EMode.end(); i++)
227 if ((i->modechar == modechar) && (i->type == type) && (i->list == true))
235 bool ModeDefinedOper(char modechar, int type)
237 for (ExtModeListIter i = EMode.begin(); i < EMode.end(); i++)
239 if ((i->modechar == modechar) && (i->type == type) && (i->needsoper == true))
247 // returns number of parameters for a custom mode when it is switched on
248 int ModeDefinedOn(char modechar, int type)
250 for (ExtModeListIter i = EMode.begin(); i < EMode.end(); i++)
252 if ((i->modechar == modechar) && (i->type == type))
254 return i->params_when_on;
260 // returns number of parameters for a custom mode when it is switched on
261 int ModeDefinedOff(char modechar, int type)
263 for (ExtModeListIter i = EMode.begin(); i < EMode.end(); i++)
265 if ((i->modechar == modechar) && (i->type == type))
267 return i->params_when_off;
273 // returns true if an extended mode character is in use
274 bool DoAddExtendedMode(char modechar, int type, bool requires_oper, int params_on, int params_off)
276 if (ModeDefined(modechar,type)) {
279 EMode.push_back(ExtMode(modechar,type,requires_oper,params_on,params_off));
283 // turns a mode into a listmode
284 void ModeMakeList(char modechar)
286 for (ExtModeListIter i = EMode.begin(); i < EMode.end(); i++)
288 if ((i->modechar == modechar) && (i->type == MT_CHANNEL))
297 // version is a simple class for holding a modules version number
299 Version::Version(int major, int minor, int revision, int build, int flags) : Major(major), Minor(minor), Revision(revision), Build(build), Flags(flags) { };
301 // admin is a simple class for holding a server's administrative info
303 Admin::Admin(std::string name, std::string email, std::string nick) : Name(name), Email(email), Nick(nick) { };
305 Request::Request(char* anydata, Module* src, Module* dst) : data(anydata), source(src), dest(dst) { };
307 char* Request::GetData()
312 Module* Request::GetSource()
317 Module* Request::GetDest()
322 char* Request::Send()
326 return dest->OnRequest(this);
334 Event::Event(char* anydata, Module* src, std::string eventid) : data(anydata), source(src), id(eventid) { };
336 char* Event::GetData()
341 Module* Event::GetSource()
348 FOREACH_MOD OnEvent(this);
352 std::string Event::GetEventID()
359 Module::~Module() { }
360 void Module::OnUserConnect(userrec* user) { }
361 void Module::OnUserQuit(userrec* user) { }
362 void Module::OnUserDisconnect(userrec* user) { }
363 void Module::OnUserJoin(userrec* user, chanrec* channel) { }
364 void Module::OnUserPart(userrec* user, chanrec* channel) { }
365 void Module::OnPacketTransmit(std::string &data, std::string serv) { }
366 void Module::OnPacketReceive(std::string &data, std::string serv) { }
367 void Module::OnRehash() { }
368 void Module::OnServerRaw(std::string &raw, bool inbound, userrec* user) { }
369 int Module::OnUserPreJoin(userrec* user, chanrec* chan, const char* cname) { return 0; }
370 int Module::OnExtendedMode(userrec* user, void* target, char modechar, int type, bool mode_on, string_list ¶ms) { return false; }
371 Version Module::GetVersion() { return Version(1,0,0,0,VF_VENDOR); }
372 void Module::OnOper(userrec* user) { };
373 void Module::OnInfo(userrec* user) { };
374 void Module::OnWhois(userrec* source, userrec* dest) { };
375 int Module::OnUserPreInvite(userrec* source,userrec* dest,chanrec* channel) { return 0; };
376 int Module::OnUserPreMessage(userrec* user,void* dest,int target_type, std::string &text) { return 0; };
377 int Module::OnUserPreNotice(userrec* user,void* dest,int target_type, std::string &text) { return 0; };
378 int Module::OnUserPreNick(userrec* user, std::string newnick) { return 0; };
379 void Module::OnUserPostNick(userrec* user, std::string oldnick) { };
380 int Module::OnAccessCheck(userrec* source,userrec* dest,chanrec* channel,int access_type) { return ACR_DEFAULT; };
381 string_list Module::OnUserSync(userrec* user) { string_list empty; return empty; }
382 string_list Module::OnChannelSync(chanrec* chan) { string_list empty; return empty; }
383 void Module::On005Numeric(std::string &output) { };
384 int Module::OnKill(userrec* source, userrec* dest, std::string reason) { return 0; };
385 void Module::OnLoadModule(Module* mod,std::string name) { };
386 void Module::OnBackgroundTimer(time_t curtime) { };
387 void Module::OnSendList(userrec* user, chanrec* channel, char mode) { };
388 int Module::OnPreCommand(std::string command, char **parameters, int pcnt, userrec *user) { return 0; };
389 bool Module::OnCheckReady(userrec* user) { return true; };
390 void Module::OnUserRegister(userrec* user) { };
391 int Module::OnUserPreKick(userrec* source, userrec* user, chanrec* chan, std::string reason) { return 0; };
392 void Module::OnUserKick(userrec* source, userrec* user, chanrec* chan, std::string reason) { };
393 int Module::OnRawMode(userrec* user, chanrec* chan, char mode, std::string param, bool adding, int pcnt) { return 0; };
394 int Module::OnCheckInvite(userrec* user, chanrec* chan) { return 0; };
395 int Module::OnCheckKey(userrec* user, chanrec* chan, std::string keygiven) { return 0; };
396 int Module::OnCheckLimit(userrec* user, chanrec* chan) { return 0; };
397 int Module::OnCheckBan(userrec* user, chanrec* chan) { return 0; };
398 void Module::OnStats(char symbol) { };
399 int Module::OnChangeLocalUserHost(userrec* user, std::string newhost) { return 0; };
400 int Module::OnChangeLocalUserGECOS(userrec* user, std::string newhost) { return 0; };
401 int Module::OnLocalTopicChange(userrec* user, chanrec* chan, std::string topic) { return 0; };
402 int Module::OnMeshToken(char token,string_list params,serverrec* source,serverrec* reply, std::string tcp_host,std::string ipaddr,int port) { return 0; };
403 void Module::OnEvent(Event* event) { return; };
404 char* Module::OnRequest(Request* request) { return NULL; };
405 int Module::OnOperCompare(std::string password, std::string input) { return 0; };
406 void Module::OnGlobalOper(userrec* user) { };
407 void Module::OnGlobalConnect(userrec* user) { };
409 // server is a wrapper class that provides methods to all of the C-style
410 // exports in the core
421 void Server::SendOpers(std::string s)
423 WriteOpers("%s",s.c_str());
426 bool Server::MatchText(std::string sliteral, std::string spattern)
428 char literal[MAXBUF],pattern[MAXBUF];
429 strlcpy(literal,sliteral.c_str(),MAXBUF);
430 strlcpy(pattern,spattern.c_str(),MAXBUF);
431 return match(literal,pattern);
434 void Server::SendToModeMask(std::string modes, int flags, std::string text)
436 WriteMode(modes.c_str(),flags,"%s",text.c_str());
439 chanrec* Server::JoinUserToChannel(userrec* user, std::string cname, std::string key)
441 return add_channel(user,cname.c_str(),key.c_str(),true);
444 chanrec* Server::PartUserFromChannel(userrec* user, std::string cname, std::string reason)
446 return del_channel(user,cname.c_str(),reason.c_str(),false);
449 chanuserlist Server::GetUsers(chanrec* chan)
453 std::vector<char*> *list = chan->GetUsers();
454 for (std::vector<char*>::iterator i = list->begin(); i != list->end(); i++)
457 userl.push_back((userrec*)o);
461 void Server::ChangeUserNick(userrec* user, std::string nickname)
463 force_nickchange(user,nickname.c_str());
466 void Server::QuitUser(userrec* user, std::string reason)
468 send_network_quit(user->nick,reason.c_str());
469 kill_link(user,reason.c_str());
472 bool Server::IsUlined(std::string server)
474 return is_uline(server.c_str());
477 void Server::CallCommandHandler(std::string commandname, char** parameters, int pcnt, userrec* user)
479 call_handler(commandname.c_str(),parameters,pcnt,user);
482 void Server::Log(int level, std::string s)
484 log(level,"%s",s.c_str());
487 void Server::AddCommand(char* cmd, handlerfunc f, char flags, int minparams, char* source)
489 createcommand(cmd,f,flags,minparams,source);
492 void Server::SendMode(char **parameters, int pcnt, userrec *user)
494 server_mode(parameters,pcnt,user);
497 void Server::Send(int Socket, std::string s)
499 Write(Socket,"%s",s.c_str());
502 void Server::SendServ(int Socket, std::string s)
504 WriteServ(Socket,"%s",s.c_str());
507 void Server::SendFrom(int Socket, userrec* User, std::string s)
509 WriteFrom(Socket,User,"%s",s.c_str());
512 void Server::SendTo(userrec* Source, userrec* Dest, std::string s)
516 // if source is NULL, then the message originates from the local server
517 Write(Dest->fd,":%s %s",this->GetServerName().c_str(),s.c_str());
521 // otherwise it comes from the user specified
522 WriteTo(Source,Dest,"%s",s.c_str());
526 void Server::SendChannelServerNotice(std::string ServName, chanrec* Channel, std::string text)
528 WriteChannelWithServ((char*)ServName.c_str(), Channel, "%s", text.c_str());
531 void Server::SendChannel(userrec* User, chanrec* Channel, std::string s,bool IncludeSender)
535 WriteChannel(Channel,User,"%s",s.c_str());
539 ChanExceptSender(Channel,User,"%s",s.c_str());
543 bool Server::CommonChannels(userrec* u1, userrec* u2)
545 return (common_channels(u1,u2) != 0);
548 void Server::SendCommon(userrec* User, std::string text,bool IncludeSender)
552 WriteCommon(User,"%s",text.c_str());
556 WriteCommonExcept(User,"%s",text.c_str());
560 void Server::SendWallops(userrec* User, std::string text)
562 WriteWallOps(User,false,"%s",text.c_str());
565 void Server::ChangeHost(userrec* user, std::string host)
567 ChangeDisplayedHost(user,host.c_str());
570 void Server::ChangeGECOS(userrec* user, std::string gecos)
572 ChangeName(user,gecos.c_str());
575 bool Server::IsNick(std::string nick)
577 return (isnick(nick.c_str()) != 0);
580 userrec* Server::FindNick(std::string nick)
585 chanrec* Server::FindChannel(std::string channel)
587 return FindChan(channel.c_str());
590 std::string Server::ChanMode(userrec* User, chanrec* Chan)
592 return cmode(User,Chan);
595 bool Server::IsOnChannel(userrec* User, chanrec* Chan)
597 return has_channel(User,Chan);
600 std::string Server::GetServerName()
602 return getservername();
605 std::string Server::GetNetworkName()
607 return getnetworkname();
610 Admin Server::GetAdmin()
612 return Admin(getadminname(),getadminemail(),getadminnick());
617 bool Server::AddExtendedMode(char modechar, int type, bool requires_oper, int params_when_on, int params_when_off)
619 if (((modechar >= 'A') && (modechar <= 'Z')) || ((modechar >= 'a') && (modechar <= 'z')))
621 if (type == MT_SERVER)
623 log(DEBUG,"*** API ERROR *** Modes of type MT_SERVER are reserved for future expansion");
626 if (((params_when_on>0) || (params_when_off>0)) && (type == MT_CLIENT))
628 log(DEBUG,"*** API ERROR *** Parameters on MT_CLIENT modes are not supported");
631 if ((params_when_on>1) || (params_when_off>1))
633 log(DEBUG,"*** API ERROR *** More than one parameter for an MT_CHANNEL mode is not yet supported");
636 return DoAddExtendedMode(modechar,type,requires_oper,params_when_on,params_when_off);
640 log(DEBUG,"*** API ERROR *** Muppet modechar detected.");
645 bool Server::AddExtendedListMode(char modechar)
647 bool res = DoAddExtendedMode(modechar,MT_CHANNEL,false,1,1);
649 ModeMakeList(modechar);
653 int Server::CountUsers(chanrec* c)
659 bool Server::UserToPseudo(userrec* user,std::string message)
661 unsigned int old_fd = user->fd;
662 user->fd = FD_MAGIC_NUMBER;
663 Write(old_fd,"ERROR :Closing link (%s@%s) [%s]",user->ident,user->host,message.c_str());
668 bool Server::PseudoToUser(userrec* alive,userrec* zombie,std::string message)
670 zombie->fd = alive->fd;
671 alive->fd = FD_MAGIC_NUMBER;
672 Write(zombie->fd,":%s!%s@%s NICK %s",alive->nick,alive->ident,alive->host,zombie->nick);
673 kill_link(alive,message.c_str());
674 for (int i = 0; i != MAXCHANS; i++)
676 if (zombie->chans[i].channel != NULL)
678 if (zombie->chans[i].channel->name)
680 chanrec* Ptr = zombie->chans[i].channel;
681 WriteFrom(zombie->fd,zombie,"JOIN %s",Ptr->name);
684 WriteServ(zombie->fd,"332 %s %s :%s", zombie->nick, Ptr->name, Ptr->topic);
685 WriteServ(zombie->fd,"333 %s %s %s %d", zombie->nick, Ptr->name, Ptr->setby, Ptr->topicset);
687 userlist(zombie,Ptr);
688 WriteServ(zombie->fd,"366 %s %s :End of /NAMES list.", zombie->nick, Ptr->name);
689 //WriteServ(zombie->fd,"324 %s %s +%s",zombie->nick, Ptr->name,chanmodes(Ptr));
690 //WriteServ(zombie->fd,"329 %s %s %d", zombie->nick, Ptr->name, Ptr->created);
698 void Server::AddGLine(long duration, std::string source, std::string reason, std::string hostmask)
700 add_gline(duration, source.c_str(), reason.c_str(), hostmask.c_str());
703 void Server::AddQLine(long duration, std::string source, std::string reason, std::string nickname)
705 add_qline(duration, source.c_str(), reason.c_str(), nickname.c_str());
708 void Server::AddZLine(long duration, std::string source, std::string reason, std::string ipaddr)
710 add_zline(duration, source.c_str(), reason.c_str(), ipaddr.c_str());
713 void Server::AddKLine(long duration, std::string source, std::string reason, std::string hostmask)
715 add_kline(duration, source.c_str(), reason.c_str(), hostmask.c_str());
718 void Server::AddELine(long duration, std::string source, std::string reason, std::string hostmask)
720 add_eline(duration, source.c_str(), reason.c_str(), hostmask.c_str());
723 bool Server::DelGLine(std::string hostmask)
725 del_gline(hostmask.c_str());
728 bool Server::DelQLine(std::string nickname)
730 del_qline(nickname.c_str());
733 bool Server::DelZLine(std::string ipaddr)
735 del_zline(ipaddr.c_str());
738 bool Server::DelKLine(std::string hostmask)
740 del_kline(hostmask.c_str());
743 bool Server::DelELine(std::string hostmask)
745 del_eline(hostmask.c_str());
748 long Server::CalcDuration(std::string delta)
750 return duration(delta.c_str());
753 bool Server::IsValidMask(std::string mask)
755 const char* dest = mask.c_str();
756 if (strchr(dest,'!')==0)
758 if (strchr(dest,'@')==0)
760 for (int i = 0; i < strlen(dest); i++)
763 for (int i = 0; i < strlen(dest); i++)
767 for (int i = 0; i < strlen(dest); i++)
773 for (int i = 0; i < strlen(dest); i++)
782 void Server::MeshSendAll(std::string text)
784 NetSendToAll((char*)text.c_str());
787 void Server::MeshSendCommon(userrec* user, std::string text)
790 NetSendToCommon(user,(char*)text.c_str());
793 void Server::MeshSendAllAlive(std::string text)
795 NetSendToAllAlive((char*)text.c_str());
798 void Server::MeshSendUnicast(std::string destination, std::string text)
800 NetSendToOne((char*)destination.c_str(),(char*)text.c_str());
803 void Server::MeshSendAllExcept(std::string target, std::string text)
805 NetSendToAllExcept(target.c_str(),(char*)text.c_str());
808 bool Server::MeshCheckChan(chanrec *c,std::string servername)
812 return ChanAnyOnThisServer(c,(char*)servername.c_str());
817 bool Server::MeshCheckCommon(userrec* u,std::string servername)
821 return CommonOnThisServer(u,(char*)servername.c_str());
826 Module* Server::FindModule(std::string name)
828 for (int i = 0; i <= MODCOUNT; i++)
830 if (module_names[i] == name)
838 ConfigReader::ConfigReader()
840 this->cache = new std::stringstream(std::stringstream::in | std::stringstream::out);
841 this->errorlog = new std::stringstream(std::stringstream::in | std::stringstream::out);
842 this->readerror = LoadConf(CONFIG_FILE,this->cache,this->errorlog);
843 if (!this->readerror)
844 this->error = CONF_FILE_NOT_FOUND;
848 ConfigReader::~ConfigReader()
853 delete this->errorlog;
857 ConfigReader::ConfigReader(std::string filename)
859 this->cache = new std::stringstream(std::stringstream::in | std::stringstream::out);
860 this->errorlog = new std::stringstream(std::stringstream::in | std::stringstream::out);
861 this->readerror = LoadConf(filename.c_str(),this->cache,this->errorlog);
862 if (!this->readerror)
863 this->error = CONF_FILE_NOT_FOUND;
866 std::string ConfigReader::ReadValue(std::string tag, std::string name, int index)
871 strlcpy(t,tag.c_str(),MAXBUF);
872 strlcpy(n,name.c_str(),MAXBUF);
873 int res = ReadConf(cache,t,n,index,val);
876 this->error = CONF_VALUE_NOT_FOUND;
879 return std::string(val);
882 bool ConfigReader::ReadFlag(std::string tag, std::string name, int index)
887 strlcpy(t,tag.c_str(),MAXBUF);
888 strlcpy(n,name.c_str(),MAXBUF);
889 int res = ReadConf(cache,t,n,index,val);
892 this->error = CONF_VALUE_NOT_FOUND;
896 return ((s == "yes") || (s == "YES") || (s == "true") || (s == "TRUE") || (s == "1"));
899 long ConfigReader::ReadInteger(std::string tag, std::string name, int index, bool needs_unsigned)
904 strlcpy(t,tag.c_str(),MAXBUF);
905 strlcpy(n,name.c_str(),MAXBUF);
906 int res = ReadConf(cache,t,n,index,val);
909 this->error = CONF_VALUE_NOT_FOUND;
912 for (int i = 0; i < strlen(val); i++)
914 if (!isdigit(val[i]))
916 this->error = CONF_NOT_A_NUMBER;
920 if ((needs_unsigned) && (atoi(val)<0))
922 this->error = CONF_NOT_UNSIGNED;
928 long ConfigReader::GetError()
930 long olderr = this->error;
935 void ConfigReader::DumpErrors(bool bail, userrec* user)
939 printf("There were errors in your configuration:\n%s",errorlog->str().c_str());
947 WriteServ(user->fd,"NOTICE %s :There were errors in the configuration file:",user->nick);
948 while (!errorlog->eof())
950 errorlog->getline(dataline,1024);
951 WriteServ(user->fd,"NOTICE %s :%s",user->nick,dataline);
956 WriteOpers("There were errors in the configuration file:",user->nick);
957 while (!errorlog->eof())
959 errorlog->getline(dataline,1024);
960 WriteOpers(dataline);
968 int ConfigReader::Enumerate(std::string tag)
970 return EnumConf(cache,tag.c_str());
973 int ConfigReader::EnumerateValues(std::string tag, int index)
975 return EnumValues(cache, tag.c_str(), index);
978 bool ConfigReader::Verify()
980 return this->readerror;
984 FileReader::FileReader(std::string filename)
987 readfile(c,filename.c_str());
991 FileReader::FileReader()
995 void FileReader::LoadFile(std::string filename)
998 readfile(c,filename.c_str());
1003 FileReader::~FileReader()
1007 bool FileReader::Exists()
1019 std::string FileReader::GetLine(int x)
1021 if ((x<0) || (x>fc.size()))
1026 int FileReader::FileSize()
1032 std::vector<Module*> modules(255);
1033 std::vector<ircd_module*> factory(255);