2 #include "inspircd_io.h"
3 #include "inspircd_util.h"
4 #include "inspircd_config.h"
9 #include <sys/utsname.h>
14 #include <ext/hash_map>
26 #include "connection.h"
40 #define nspace __gnu_cxx
48 extern vector<Module*> modules;
49 extern vector<ircd_module*> factory;
52 extern char ServerName[MAXBUF];
53 extern char Network[MAXBUF];
54 extern char ServerDesc[MAXBUF];
55 extern char AdminName[MAXBUF];
56 extern char AdminEmail[MAXBUF];
57 extern char AdminNick[MAXBUF];
58 extern char diepass[MAXBUF];
59 extern char restartpass[MAXBUF];
60 extern char motd[MAXBUF];
61 extern char rules[MAXBUF];
62 extern char list[MAXBUF];
63 extern char PrefixQuit[MAXBUF];
64 extern char DieValue[MAXBUF];
67 extern int WHOWAS_STALE;
68 extern int WHOWAS_MAX;
70 extern time_t startup_time;
71 extern int NetBufferSize;
72 extern int MaxWhoResults;
73 extern time_t nb_start;
75 extern std::vector<int> fd_reap;
76 extern std::vector<std::string> module_names;
78 extern int boundPortCount;
80 extern int UDPportCount;
81 extern int ports[MAXSOCKS];
82 extern int defaultRoute;
84 extern std::vector<long> auth_cookies;
85 extern std::stringstream config_f;
87 extern serverrec* me[32];
89 extern FILE *log_file;
94 template<> struct nspace::hash<in_addr>
96 size_t operator()(const struct in_addr &a) const
99 memcpy(&q,&a,sizeof(size_t));
104 template<> struct nspace::hash<string>
106 size_t operator()(const string &s) const
109 static struct hash<const char *> strhash;
121 bool operator()(const string& s1, const string& s2) const
123 char a[MAXBUF],b[MAXBUF];
124 strcpy(a,s1.c_str());
125 strcpy(b,s2.c_str());
126 return (strcasecmp(a,b) == 0);
131 struct InAddr_HashComp
134 bool operator()(const in_addr &s1, const in_addr &s2) const
139 memcpy(&q,&s1,sizeof(size_t));
140 memcpy(&p,&s2,sizeof(size_t));
148 typedef nspace::hash_map<std::string, userrec*, nspace::hash<string>, StrHashComp> user_hash;
149 typedef nspace::hash_map<std::string, chanrec*, nspace::hash<string>, StrHashComp> chan_hash;
150 typedef nspace::hash_map<in_addr,string*, nspace::hash<in_addr>, InAddr_HashComp> address_cache;
151 typedef std::deque<command_t> command_table;
154 extern user_hash clientlist;
155 extern chan_hash chanlist;
156 extern user_hash whowas;
157 extern command_table cmdlist;
158 extern file_cache MOTD;
159 extern file_cache RULES;
160 extern address_cache IP;
163 // class type for holding an extended mode character - internal to core
165 class ExtMode : public classbase
174 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) { };
177 typedef std::vector<ExtMode> ExtModeList;
178 typedef ExtModeList::iterator ExtModeListIter;
183 // returns true if an extended mode character is in use
184 bool ModeDefined(char modechar, int type)
186 log(DEBUG,"Size of extmodes vector is %d",EMode.size());
187 for (ExtModeListIter i = EMode.begin(); i < EMode.end(); i++)
189 if ((i->modechar == modechar) && (i->type == type))
197 bool ModeIsListMode(char modechar, int type)
199 log(DEBUG,"Size of extmodes vector is %d",EMode.size());
200 for (ExtModeListIter i = EMode.begin(); i < EMode.end(); i++)
202 if ((i->modechar == modechar) && (i->type == type) && (i->list == true))
210 bool ModeDefinedOper(char modechar, int type)
212 log(DEBUG,"Size of extmodes vector is %d",EMode.size());
213 for (ExtModeListIter i = EMode.begin(); i < EMode.end(); i++)
215 if ((i->modechar == modechar) && (i->type == type) && (i->needsoper == true))
223 // returns number of parameters for a custom mode when it is switched on
224 int ModeDefinedOn(char modechar, int type)
226 for (ExtModeListIter i = EMode.begin(); i < EMode.end(); i++)
228 if ((i->modechar == modechar) && (i->type == type))
230 return i->params_when_on;
236 // returns number of parameters for a custom mode when it is switched on
237 int ModeDefinedOff(char modechar, int type)
239 for (ExtModeListIter i = EMode.begin(); i < EMode.end(); i++)
241 if ((i->modechar == modechar) && (i->type == type))
243 return i->params_when_off;
249 // returns true if an extended mode character is in use
250 bool DoAddExtendedMode(char modechar, int type, bool requires_oper, int params_on, int params_off)
252 if (ModeDefined(modechar,type)) {
255 EMode.push_back(ExtMode(modechar,type,requires_oper,params_on,params_off));
259 // turns a mode into a listmode
260 void ModeMakeList(char modechar)
262 for (ExtModeListIter i = EMode.begin(); i < EMode.end(); i++)
264 if ((i->modechar == modechar) && (i->type == MT_CHANNEL))
273 // version is a simple class for holding a modules version number
275 Version::Version(int major, int minor, int revision, int build) : Major(major), Minor(minor), Revision(revision), Build(build) { };
277 // admin is a simple class for holding a server's administrative info
279 Admin::Admin(std::string name, std::string email, std::string nick) : Name(name), Email(email), Nick(nick) { };
282 Module::~Module() { }
283 void Module::OnUserConnect(userrec* user) { }
284 void Module::OnUserQuit(userrec* user) { }
285 void Module::OnUserJoin(userrec* user, chanrec* channel) { }
286 void Module::OnUserPart(userrec* user, chanrec* channel) { }
287 void Module::OnPacketTransmit(char *p) { }
288 void Module::OnPacketReceive(char *p) { }
289 void Module::OnRehash() { }
290 void Module::OnServerRaw(std::string &raw, bool inbound, userrec* user) { }
291 int Module::OnUserPreJoin(userrec* user, chanrec* chan, const char* cname) { return 0; }
292 int Module::OnExtendedMode(userrec* user, void* target, char modechar, int type, bool mode_on, string_list ¶ms) { return false; }
293 Version Module::GetVersion() { return Version(1,0,0,0); }
294 void Module::OnOper(userrec* user) { };
295 void Module::OnInfo(userrec* user) { };
296 void Module::OnWhois(userrec* source, userrec* dest) { };
297 int Module::OnUserPreMessage(userrec* user,void* dest,int target_type, std::string text) { return 0; };
298 int Module::OnUserPreNotice(userrec* user,void* dest,int target_type, std::string text) { return 0; };
299 int Module::OnUserPreNick(userrec* user, std::string newnick) { return 0; };
300 int Module::OnAccessCheck(userrec* source,userrec* dest,chanrec* channel,int access_type) { return ACR_DEFAULT; };
301 string_list Module::OnUserSync(userrec* user) { string_list empty; return empty; }
302 string_list Module::OnChannelSync(chanrec* chan) { string_list empty; return empty; }
305 // server is a wrapper class that provides methods to all of the C-style
306 // exports in the core
317 void Server::SendOpers(std::string s)
319 WriteOpers("%s",s.c_str());
322 bool Server::MatchText(std::string sliteral, std::string spattern)
324 char literal[MAXBUF],pattern[MAXBUF];
325 strncpy(literal,sliteral.c_str(),MAXBUF);
326 strncpy(pattern,spattern.c_str(),MAXBUF);
327 return match(literal,pattern);
330 void Server::SendToModeMask(std::string modes, int flags, std::string text)
332 WriteMode(modes.c_str(),flags,"%s",text.c_str());
335 chanrec* Server::JoinUserToChannel(userrec* user, std::string cname, std::string key)
337 return add_channel(user,cname.c_str(),key.c_str(),true);
340 chanrec* Server::PartUserFromChannel(userrec* user, std::string cname, std::string reason)
342 return del_channel(user,cname.c_str(),reason.c_str(),false);
345 chanuserlist Server::GetUsers(chanrec* chan)
349 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
353 if (has_channel(i->second,chan))
355 if (isnick(i->second->nick))
357 userl.push_back(i->second);
364 void Server::ChangeUserNick(userrec* user, std::string nickname)
366 force_nickchange(user,nickname.c_str());
369 void Server::QuitUser(userrec* user, std::string reason)
371 send_network_quit(user->nick,reason.c_str());
372 kill_link(user,reason.c_str());
375 bool Server::IsUlined(std::string server)
377 return is_uline(server.c_str());
380 void Server::CallCommandHandler(std::string commandname, char** parameters, int pcnt, userrec* user)
382 call_handler(commandname.c_str(),parameters,pcnt,user);
385 void Server::Log(int level, std::string s)
387 log(level,"%s",s.c_str());
390 void Server::AddCommand(char* cmd, handlerfunc f, char flags, int minparams)
392 createcommand(cmd,f,flags,minparams);
395 void Server::SendMode(char **parameters, int pcnt, userrec *user)
397 server_mode(parameters,pcnt,user);
400 void Server::Send(int Socket, std::string s)
402 Write(Socket,"%s",s.c_str());
405 void Server::SendServ(int Socket, std::string s)
407 WriteServ(Socket,"%s",s.c_str());
410 void Server::SendFrom(int Socket, userrec* User, std::string s)
412 WriteFrom(Socket,User,"%s",s.c_str());
415 void Server::SendTo(userrec* Source, userrec* Dest, std::string s)
419 // if source is NULL, then the message originates from the local server
420 Write(Dest->fd,":%s %s",this->GetServerName().c_str(),s.c_str());
424 // otherwise it comes from the user specified
425 WriteTo(Source,Dest,"%s",s.c_str());
429 void Server::SendChannel(userrec* User, chanrec* Channel, std::string s,bool IncludeSender)
433 WriteChannel(Channel,User,"%s",s.c_str());
437 ChanExceptSender(Channel,User,"%s",s.c_str());
441 bool Server::CommonChannels(userrec* u1, userrec* u2)
443 return (common_channels(u1,u2) != 0);
446 void Server::SendCommon(userrec* User, std::string text,bool IncludeSender)
450 WriteCommon(User,"%s",text.c_str());
454 WriteCommonExcept(User,"%s",text.c_str());
458 void Server::SendWallops(userrec* User, std::string text)
460 WriteWallOps(User,false,"%s",text.c_str());
463 void Server::ChangeHost(userrec* user, std::string host)
465 ChangeDisplayedHost(user,host.c_str());
468 void Server::ChangeGECOS(userrec* user, std::string gecos)
470 ChangeName(user,gecos.c_str());
473 bool Server::IsNick(std::string nick)
475 return (isnick(nick.c_str()) != 0);
478 userrec* Server::FindNick(std::string nick)
483 chanrec* Server::FindChannel(std::string channel)
485 return FindChan(channel.c_str());
488 std::string Server::ChanMode(userrec* User, chanrec* Chan)
490 return cmode(User,Chan);
493 bool Server::IsOnChannel(userrec* User, chanrec* Chan)
495 return has_channel(User,Chan);
498 std::string Server::GetServerName()
500 return getservername();
503 std::string Server::GetNetworkName()
505 return getnetworkname();
508 Admin Server::GetAdmin()
510 return Admin(getadminname(),getadminemail(),getadminnick());
515 bool Server::AddExtendedMode(char modechar, int type, bool requires_oper, int params_when_on, int params_when_off)
517 if (type == MT_SERVER)
519 log(DEBUG,"*** API ERROR *** Modes of type MT_SERVER are reserved for future expansion");
522 if (((params_when_on>0) || (params_when_off>0)) && (type == MT_CLIENT))
524 log(DEBUG,"*** API ERROR *** Parameters on MT_CLIENT modes are not supported");
527 if ((params_when_on>1) || (params_when_off>1))
529 log(DEBUG,"*** API ERROR *** More than one parameter for an MT_CHANNEL mode is not yet supported");
532 return DoAddExtendedMode(modechar,type,requires_oper,params_when_on,params_when_off);
535 bool Server::AddExtendedListMode(char modechar)
537 bool res = DoAddExtendedMode(modechar,MT_CHANNEL,false,1,1);
539 ModeMakeList(modechar);
543 int Server::CountUsers(chanrec* c)
549 ConfigReader::ConfigReader()
551 this->cache = new std::stringstream(std::stringstream::in | std::stringstream::out);
552 this->readerror = LoadConf(CONFIG_FILE,this->cache);
553 if (!this->readerror)
554 this->error = CONF_FILE_NOT_FOUND;
558 ConfigReader::~ConfigReader()
565 ConfigReader::ConfigReader(std::string filename)
567 this->cache = new std::stringstream(std::stringstream::in | std::stringstream::out);
568 this->readerror = LoadConf(filename.c_str(),this->cache);
569 if (!this->readerror)
570 this->error = CONF_FILE_NOT_FOUND;
573 std::string ConfigReader::ReadValue(std::string tag, std::string name, int index)
578 strncpy(t,tag.c_str(),MAXBUF);
579 strncpy(n,name.c_str(),MAXBUF);
580 int res = ReadConf(cache,t,n,index,val);
583 this->error = CONF_VALUE_NOT_FOUND;
586 return std::string(val);
589 bool ConfigReader::ReadFlag(std::string tag, std::string name, int index)
594 strncpy(t,tag.c_str(),MAXBUF);
595 strncpy(n,name.c_str(),MAXBUF);
596 int res = ReadConf(cache,t,n,index,val);
599 this->error = CONF_VALUE_NOT_FOUND;
603 return ((s == "yes") || (s == "YES") || (s == "true") || (s == "TRUE") || (s == "1"));
606 long ConfigReader::ReadInteger(std::string tag, std::string name, int index, bool needs_unsigned)
611 strncpy(t,tag.c_str(),MAXBUF);
612 strncpy(n,name.c_str(),MAXBUF);
613 int res = ReadConf(cache,t,n,index,val);
616 this->error = CONF_VALUE_NOT_FOUND;
619 for (int i = 0; i < strlen(val); i++)
621 if (!isdigit(val[i]))
623 this->error = CONF_NOT_A_NUMBER;
627 if ((needs_unsigned) && (atoi(val)<0))
629 this->error = CONF_NOT_UNSIGNED;
635 long ConfigReader::GetError()
637 long olderr = this->error;
643 int ConfigReader::Enumerate(std::string tag)
645 return EnumConf(cache,tag.c_str());
648 int ConfigReader::EnumerateValues(std::string tag, int index)
650 return EnumValues(cache, tag.c_str(), index);
653 bool ConfigReader::Verify()
655 return this->readerror;
659 FileReader::FileReader(std::string filename)
662 readfile(c,filename.c_str());
666 FileReader::FileReader()
670 void FileReader::LoadFile(std::string filename)
673 readfile(c,filename.c_str());
678 FileReader::~FileReader()
682 bool FileReader::Exists()
694 std::string FileReader::GetLine(int x)
696 if ((x<0) || (x>fc.size()))
701 int FileReader::FileSize()
707 std::vector<Module*> modules(255);
708 std::vector<ircd_module*> factory(255);