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"
56 #define nspace __gnu_cxx
64 extern vector<Module*> modules;
65 extern vector<ircd_module*> factory;
68 extern char ServerName[MAXBUF];
69 extern char Network[MAXBUF];
70 extern char ServerDesc[MAXBUF];
71 extern char AdminName[MAXBUF];
72 extern char AdminEmail[MAXBUF];
73 extern char AdminNick[MAXBUF];
74 extern char diepass[MAXBUF];
75 extern char restartpass[MAXBUF];
76 extern char motd[MAXBUF];
77 extern char rules[MAXBUF];
78 extern char list[MAXBUF];
79 extern char PrefixQuit[MAXBUF];
80 extern char DieValue[MAXBUF];
83 extern int WHOWAS_STALE;
84 extern int WHOWAS_MAX;
86 extern time_t startup_time;
87 extern int NetBufferSize;
88 extern int MaxWhoResults;
89 extern time_t nb_start;
91 extern std::vector<int> fd_reap;
92 extern std::vector<std::string> module_names;
94 extern int boundPortCount;
96 extern int UDPportCount;
97 extern int ports[MAXSOCKS];
98 extern int defaultRoute;
100 extern std::vector<long> auth_cookies;
101 extern std::stringstream config_f;
103 extern serverrec* me[32];
105 extern FILE *log_file;
110 template<> struct nspace::hash<in_addr>
112 size_t operator()(const struct in_addr &a) const
115 memcpy(&q,&a,sizeof(size_t));
120 template<> struct nspace::hash<string>
122 size_t operator()(const string &s) const
125 static struct hash<const char *> strhash;
126 strlcpy(a,s.c_str(),MAXBUF);
137 bool operator()(const string& s1, const string& s2) const
139 char a[MAXBUF],b[MAXBUF];
140 strlcpy(a,s1.c_str(),MAXBUF);
141 strlcpy(b,s2.c_str(),MAXBUF);
142 return (strcasecmp(a,b) == 0);
147 struct InAddr_HashComp
150 bool operator()(const in_addr &s1, const in_addr &s2) const
155 memcpy(&q,&s1,sizeof(size_t));
156 memcpy(&p,&s2,sizeof(size_t));
164 typedef nspace::hash_map<std::string, userrec*, nspace::hash<string>, StrHashComp> user_hash;
165 typedef nspace::hash_map<std::string, chanrec*, nspace::hash<string>, StrHashComp> chan_hash;
166 typedef nspace::hash_map<in_addr,string*, nspace::hash<in_addr>, InAddr_HashComp> address_cache;
167 typedef std::deque<command_t> command_table;
170 extern user_hash clientlist;
171 extern chan_hash chanlist;
172 extern user_hash whowas;
173 extern command_table cmdlist;
174 extern file_cache MOTD;
175 extern file_cache RULES;
176 extern address_cache IP;
179 // class type for holding an extended mode character - internal to core
181 class ExtMode : public classbase
190 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) { };
193 typedef std::vector<ExtMode> ExtModeList;
194 typedef ExtModeList::iterator ExtModeListIter;
199 // returns true if an extended mode character is in use
200 bool ModeDefined(char modechar, int type)
202 log(DEBUG,"Size of extmodes vector is %d",EMode.size());
203 for (ExtModeListIter i = EMode.begin(); i < EMode.end(); i++)
205 if ((i->modechar == modechar) && (i->type == type))
213 bool ModeIsListMode(char modechar, int type)
215 log(DEBUG,"Size of extmodes vector is %d",EMode.size());
216 for (ExtModeListIter i = EMode.begin(); i < EMode.end(); i++)
218 if ((i->modechar == modechar) && (i->type == type) && (i->list == true))
226 bool ModeDefinedOper(char modechar, int type)
228 log(DEBUG,"Size of extmodes vector is %d",EMode.size());
229 for (ExtModeListIter i = EMode.begin(); i < EMode.end(); i++)
231 if ((i->modechar == modechar) && (i->type == type) && (i->needsoper == true))
239 // returns number of parameters for a custom mode when it is switched on
240 int ModeDefinedOn(char modechar, int type)
242 for (ExtModeListIter i = EMode.begin(); i < EMode.end(); i++)
244 if ((i->modechar == modechar) && (i->type == type))
246 return i->params_when_on;
252 // returns number of parameters for a custom mode when it is switched on
253 int ModeDefinedOff(char modechar, int type)
255 for (ExtModeListIter i = EMode.begin(); i < EMode.end(); i++)
257 if ((i->modechar == modechar) && (i->type == type))
259 return i->params_when_off;
265 // returns true if an extended mode character is in use
266 bool DoAddExtendedMode(char modechar, int type, bool requires_oper, int params_on, int params_off)
268 if (ModeDefined(modechar,type)) {
271 EMode.push_back(ExtMode(modechar,type,requires_oper,params_on,params_off));
275 // turns a mode into a listmode
276 void ModeMakeList(char modechar)
278 for (ExtModeListIter i = EMode.begin(); i < EMode.end(); i++)
280 if ((i->modechar == modechar) && (i->type == MT_CHANNEL))
289 // version is a simple class for holding a modules version number
291 Version::Version(int major, int minor, int revision, int build) : Major(major), Minor(minor), Revision(revision), Build(build) { };
293 // admin is a simple class for holding a server's administrative info
295 Admin::Admin(std::string name, std::string email, std::string nick) : Name(name), Email(email), Nick(nick) { };
298 Module::~Module() { }
299 void Module::OnUserConnect(userrec* user) { }
300 void Module::OnUserQuit(userrec* user) { }
301 void Module::OnUserJoin(userrec* user, chanrec* channel) { }
302 void Module::OnUserPart(userrec* user, chanrec* channel) { }
303 void Module::OnPacketTransmit(char *p) { }
304 void Module::OnPacketReceive(char *p) { }
305 void Module::OnRehash() { }
306 void Module::OnServerRaw(std::string &raw, bool inbound, userrec* user) { }
307 int Module::OnUserPreJoin(userrec* user, chanrec* chan, const char* cname) { return 0; }
308 int Module::OnExtendedMode(userrec* user, void* target, char modechar, int type, bool mode_on, string_list ¶ms) { return false; }
309 Version Module::GetVersion() { return Version(1,0,0,0); }
310 void Module::OnOper(userrec* user) { };
311 void Module::OnInfo(userrec* user) { };
312 void Module::OnWhois(userrec* source, userrec* dest) { };
313 int Module::OnUserPreInvite(userrec* source,userrec* dest,chanrec* channel) { return 0; };
314 int Module::OnUserPreMessage(userrec* user,void* dest,int target_type, std::string &text) { return 0; };
315 int Module::OnUserPreNotice(userrec* user,void* dest,int target_type, std::string &text) { return 0; };
316 int Module::OnUserPreNick(userrec* user, std::string newnick) { return 0; };
317 int Module::OnAccessCheck(userrec* source,userrec* dest,chanrec* channel,int access_type) { return ACR_DEFAULT; };
318 string_list Module::OnUserSync(userrec* user) { string_list empty; return empty; }
319 string_list Module::OnChannelSync(chanrec* chan) { string_list empty; return empty; }
322 // server is a wrapper class that provides methods to all of the C-style
323 // exports in the core
334 void Server::SendOpers(std::string s)
336 WriteOpers("%s",s.c_str());
339 bool Server::MatchText(std::string sliteral, std::string spattern)
341 char literal[MAXBUF],pattern[MAXBUF];
342 strlcpy(literal,sliteral.c_str(),MAXBUF);
343 strlcpy(pattern,spattern.c_str(),MAXBUF);
344 return match(literal,pattern);
347 void Server::SendToModeMask(std::string modes, int flags, std::string text)
349 WriteMode(modes.c_str(),flags,"%s",text.c_str());
352 chanrec* Server::JoinUserToChannel(userrec* user, std::string cname, std::string key)
354 return add_channel(user,cname.c_str(),key.c_str(),true);
357 chanrec* Server::PartUserFromChannel(userrec* user, std::string cname, std::string reason)
359 return del_channel(user,cname.c_str(),reason.c_str(),false);
362 chanuserlist Server::GetUsers(chanrec* chan)
366 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
370 if (has_channel(i->second,chan))
372 if (isnick(i->second->nick))
374 userl.push_back(i->second);
381 void Server::ChangeUserNick(userrec* user, std::string nickname)
383 force_nickchange(user,nickname.c_str());
386 void Server::QuitUser(userrec* user, std::string reason)
388 send_network_quit(user->nick,reason.c_str());
389 kill_link(user,reason.c_str());
392 bool Server::IsUlined(std::string server)
394 return is_uline(server.c_str());
397 void Server::CallCommandHandler(std::string commandname, char** parameters, int pcnt, userrec* user)
399 call_handler(commandname.c_str(),parameters,pcnt,user);
402 void Server::Log(int level, std::string s)
404 log(level,"%s",s.c_str());
407 void Server::AddCommand(char* cmd, handlerfunc f, char flags, int minparams)
409 createcommand(cmd,f,flags,minparams);
412 void Server::SendMode(char **parameters, int pcnt, userrec *user)
414 server_mode(parameters,pcnt,user);
417 void Server::Send(int Socket, std::string s)
419 Write(Socket,"%s",s.c_str());
422 void Server::SendServ(int Socket, std::string s)
424 WriteServ(Socket,"%s",s.c_str());
427 void Server::SendFrom(int Socket, userrec* User, std::string s)
429 WriteFrom(Socket,User,"%s",s.c_str());
432 void Server::SendTo(userrec* Source, userrec* Dest, std::string s)
436 // if source is NULL, then the message originates from the local server
437 Write(Dest->fd,":%s %s",this->GetServerName().c_str(),s.c_str());
441 // otherwise it comes from the user specified
442 WriteTo(Source,Dest,"%s",s.c_str());
446 void Server::SendChannel(userrec* User, chanrec* Channel, std::string s,bool IncludeSender)
450 WriteChannel(Channel,User,"%s",s.c_str());
454 ChanExceptSender(Channel,User,"%s",s.c_str());
458 bool Server::CommonChannels(userrec* u1, userrec* u2)
460 return (common_channels(u1,u2) != 0);
463 void Server::SendCommon(userrec* User, std::string text,bool IncludeSender)
467 WriteCommon(User,"%s",text.c_str());
471 WriteCommonExcept(User,"%s",text.c_str());
475 void Server::SendWallops(userrec* User, std::string text)
477 WriteWallOps(User,false,"%s",text.c_str());
480 void Server::ChangeHost(userrec* user, std::string host)
482 ChangeDisplayedHost(user,host.c_str());
485 void Server::ChangeGECOS(userrec* user, std::string gecos)
487 ChangeName(user,gecos.c_str());
490 bool Server::IsNick(std::string nick)
492 return (isnick(nick.c_str()) != 0);
495 userrec* Server::FindNick(std::string nick)
500 chanrec* Server::FindChannel(std::string channel)
502 return FindChan(channel.c_str());
505 std::string Server::ChanMode(userrec* User, chanrec* Chan)
507 return cmode(User,Chan);
510 bool Server::IsOnChannel(userrec* User, chanrec* Chan)
512 return has_channel(User,Chan);
515 std::string Server::GetServerName()
517 return getservername();
520 std::string Server::GetNetworkName()
522 return getnetworkname();
525 Admin Server::GetAdmin()
527 return Admin(getadminname(),getadminemail(),getadminnick());
532 bool Server::AddExtendedMode(char modechar, int type, bool requires_oper, int params_when_on, int params_when_off)
534 if (type == MT_SERVER)
536 log(DEBUG,"*** API ERROR *** Modes of type MT_SERVER are reserved for future expansion");
539 if (((params_when_on>0) || (params_when_off>0)) && (type == MT_CLIENT))
541 log(DEBUG,"*** API ERROR *** Parameters on MT_CLIENT modes are not supported");
544 if ((params_when_on>1) || (params_when_off>1))
546 log(DEBUG,"*** API ERROR *** More than one parameter for an MT_CHANNEL mode is not yet supported");
549 return DoAddExtendedMode(modechar,type,requires_oper,params_when_on,params_when_off);
552 bool Server::AddExtendedListMode(char modechar)
554 bool res = DoAddExtendedMode(modechar,MT_CHANNEL,false,1,1);
556 ModeMakeList(modechar);
560 int Server::CountUsers(chanrec* c)
566 ConfigReader::ConfigReader()
568 this->cache = new std::stringstream(std::stringstream::in | std::stringstream::out);
569 this->readerror = LoadConf(CONFIG_FILE,this->cache);
570 if (!this->readerror)
571 this->error = CONF_FILE_NOT_FOUND;
575 ConfigReader::~ConfigReader()
582 ConfigReader::ConfigReader(std::string filename)
584 this->cache = new std::stringstream(std::stringstream::in | std::stringstream::out);
585 this->readerror = LoadConf(filename.c_str(),this->cache);
586 if (!this->readerror)
587 this->error = CONF_FILE_NOT_FOUND;
590 std::string ConfigReader::ReadValue(std::string tag, std::string name, int index)
595 strlcpy(t,tag.c_str(),MAXBUF);
596 strlcpy(n,name.c_str(),MAXBUF);
597 int res = ReadConf(cache,t,n,index,val);
600 this->error = CONF_VALUE_NOT_FOUND;
603 return std::string(val);
606 bool ConfigReader::ReadFlag(std::string tag, std::string name, int index)
611 strlcpy(t,tag.c_str(),MAXBUF);
612 strlcpy(n,name.c_str(),MAXBUF);
613 int res = ReadConf(cache,t,n,index,val);
616 this->error = CONF_VALUE_NOT_FOUND;
620 return ((s == "yes") || (s == "YES") || (s == "true") || (s == "TRUE") || (s == "1"));
623 long ConfigReader::ReadInteger(std::string tag, std::string name, int index, bool needs_unsigned)
628 strlcpy(t,tag.c_str(),MAXBUF);
629 strlcpy(n,name.c_str(),MAXBUF);
630 int res = ReadConf(cache,t,n,index,val);
633 this->error = CONF_VALUE_NOT_FOUND;
636 for (int i = 0; i < strlen(val); i++)
638 if (!isdigit(val[i]))
640 this->error = CONF_NOT_A_NUMBER;
644 if ((needs_unsigned) && (atoi(val)<0))
646 this->error = CONF_NOT_UNSIGNED;
652 long ConfigReader::GetError()
654 long olderr = this->error;
660 int ConfigReader::Enumerate(std::string tag)
662 return EnumConf(cache,tag.c_str());
665 int ConfigReader::EnumerateValues(std::string tag, int index)
667 return EnumValues(cache, tag.c_str(), index);
670 bool ConfigReader::Verify()
672 return this->readerror;
676 FileReader::FileReader(std::string filename)
679 readfile(c,filename.c_str());
683 FileReader::FileReader()
687 void FileReader::LoadFile(std::string filename)
690 readfile(c,filename.c_str());
695 FileReader::~FileReader()
699 bool FileReader::Exists()
711 std::string FileReader::GetLine(int x)
713 if ((x<0) || (x>fc.size()))
718 int FileReader::FileSize()
724 std::vector<Module*> modules(255);
725 std::vector<ircd_module*> factory(255);