1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd is copyright (C) 2002-2006 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 * ---------------------------------------------------
17 #include "inspircd_config.h"
18 //#include "inspircd.h"
19 #include "configreader.h"
21 #include <sys/errno.h>
38 #include "inspstring.h"
39 #include "helperfuncs.h"
42 #include "socketengine.h"
45 #include "command_parse.h"
49 extern InspIRCd* ServerInstance;
51 extern ModuleList modules;
52 extern FactoryList factory;
54 extern command_table cmdlist;
60 // version is a simple class for holding a modules version number
62 Version::Version(int major, int minor, int revision, int build, int flags)
63 : Major(major), Minor(minor), Revision(revision), Build(build), Flags(flags)
67 // admin is a simple class for holding a server's administrative info
69 Admin::Admin(std::string name, std::string email, std::string nick)
70 : Name(name), Email(email), Nick(nick)
74 Request::Request(char* anydata, Module* src, Module* dst)
75 : data(anydata), source(src), dest(dst)
77 /* Ensure that because this module doesnt support ID strings, it doesnt break modules that do
78 * by passing them uninitialized pointers (could happen)
83 Request::Request(Module* src, Module* dst, const char* idstr)
84 : id(idstr), source(src), dest(dst)
88 char* Request::GetData()
93 const char* Request::GetId()
98 Module* Request::GetSource()
103 Module* Request::GetDest()
108 char* Request::Send()
112 return dest->OnRequest(this);
120 Event::Event(char* anydata, Module* src, const std::string &eventid) : data(anydata), source(src), id(eventid) { };
122 char* Event::GetData()
124 return (char*)this->data;
127 Module* Event::GetSource()
134 FOREACH_MOD(I_OnEvent,OnEvent(this));
138 std::string Event::GetEventID()
144 // These declarations define the behavours of the base class Module (which does nothing at all)
146 Module::Module(Server* Me) { }
147 Module::~Module() { }
148 void Module::OnUserConnect(userrec* user) { }
149 void Module::OnUserQuit(userrec* user, const std::string& message) { }
150 void Module::OnUserDisconnect(userrec* user) { }
151 void Module::OnUserJoin(userrec* user, chanrec* channel) { }
152 void Module::OnUserPart(userrec* user, chanrec* channel, const std::string &partmessage) { }
153 void Module::OnRehash(const std::string ¶meter) { }
154 void Module::OnServerRaw(std::string &raw, bool inbound, userrec* user) { }
155 int Module::OnUserPreJoin(userrec* user, chanrec* chan, const char* cname) { return 0; }
156 void Module::OnMode(userrec* user, void* dest, int target_type, const std::string &text) { };
157 Version Module::GetVersion() { return Version(1,0,0,0,VF_VENDOR); }
158 void Module::OnOper(userrec* user, const std::string &opertype) { };
159 void Module::OnPostOper(userrec* user, const std::string &opertype) { };
160 void Module::OnInfo(userrec* user) { };
161 void Module::OnWhois(userrec* source, userrec* dest) { };
162 int Module::OnUserPreInvite(userrec* source,userrec* dest,chanrec* channel) { return 0; };
163 int Module::OnUserPreMessage(userrec* user,void* dest,int target_type, std::string &text,char status) { return 0; };
164 int Module::OnUserPreNotice(userrec* user,void* dest,int target_type, std::string &text,char status) { return 0; };
165 int Module::OnUserPreNick(userrec* user, const std::string &newnick) { return 0; };
166 void Module::OnUserPostNick(userrec* user, const std::string &oldnick) { };
167 int Module::OnAccessCheck(userrec* source,userrec* dest,chanrec* channel,int access_type) { return ACR_DEFAULT; };
168 void Module::On005Numeric(std::string &output) { };
169 int Module::OnKill(userrec* source, userrec* dest, const std::string &reason) { return 0; };
170 void Module::OnLoadModule(Module* mod,const std::string &name) { };
171 void Module::OnUnloadModule(Module* mod,const std::string &name) { };
172 void Module::OnBackgroundTimer(time_t curtime) { };
173 int Module::OnPreCommand(const std::string &command, const char** parameters, int pcnt, userrec *user, bool validated) { return 0; };
174 bool Module::OnCheckReady(userrec* user) { return true; };
175 void Module::OnUserRegister(userrec* user) { };
176 int Module::OnUserPreKick(userrec* source, userrec* user, chanrec* chan, const std::string &reason) { return 0; };
177 void Module::OnUserKick(userrec* source, userrec* user, chanrec* chan, const std::string &reason) { };
178 int Module::OnRawMode(userrec* user, chanrec* chan, char mode, const std::string ¶m, bool adding, int pcnt) { return 0; };
179 int Module::OnCheckInvite(userrec* user, chanrec* chan) { return 0; };
180 int Module::OnCheckKey(userrec* user, chanrec* chan, const std::string &keygiven) { return 0; };
181 int Module::OnCheckLimit(userrec* user, chanrec* chan) { return 0; };
182 int Module::OnCheckBan(userrec* user, chanrec* chan) { return 0; };
183 int Module::OnStats(char symbol, userrec* user, string_list &results) { return 0; };
184 int Module::OnChangeLocalUserHost(userrec* user, const std::string &newhost) { return 0; };
185 int Module::OnChangeLocalUserGECOS(userrec* user, const std::string &newhost) { return 0; };
186 int Module::OnLocalTopicChange(userrec* user, chanrec* chan, const std::string &topic) { return 0; };
187 void Module::OnEvent(Event* event) { return; };
188 char* Module::OnRequest(Request* request) { return NULL; };
189 int Module::OnOperCompare(const std::string &password, const std::string &input) { return 0; };
190 void Module::OnGlobalOper(userrec* user) { };
191 void Module::OnGlobalConnect(userrec* user) { };
192 int Module::OnAddBan(userrec* source, chanrec* channel,const std::string &banmask) { return 0; };
193 int Module::OnDelBan(userrec* source, chanrec* channel,const std::string &banmask) { return 0; };
194 void Module::OnRawSocketAccept(int fd, const std::string &ip, int localport) { };
195 int Module::OnRawSocketWrite(int fd, const char* buffer, int count) { return 0; };
196 void Module::OnRawSocketClose(int fd) { };
197 int Module::OnRawSocketRead(int fd, char* buffer, unsigned int count, int &readresult) { return 0; };
198 void Module::OnUserMessage(userrec* user, void* dest, int target_type, const std::string &text, char status) { };
199 void Module::OnUserNotice(userrec* user, void* dest, int target_type, const std::string &text, char status) { };
200 void Module::OnRemoteKill(userrec* source, userrec* dest, const std::string &reason) { };
201 void Module::OnUserInvite(userrec* source,userrec* dest,chanrec* channel) { };
202 void Module::OnPostLocalTopicChange(userrec* user, chanrec* chan, const std::string &topic) { };
203 void Module::OnGetServerDescription(const std::string &servername,std::string &description) { };
204 void Module::OnSyncUser(userrec* user, Module* proto, void* opaque) { };
205 void Module::OnSyncChannel(chanrec* chan, Module* proto, void* opaque) { };
206 void Module::ProtoSendMode(void* opaque, int target_type, void* target, const std::string &modeline) { };
207 void Module::OnSyncChannelMetaData(chanrec* chan, Module* proto,void* opaque, const std::string &extname) { };
208 void Module::OnSyncUserMetaData(userrec* user, Module* proto,void* opaque, const std::string &extname) { };
209 void Module::OnSyncOtherMetaData(Module* proto, void* opaque) { };
210 void Module::OnDecodeMetaData(int target_type, void* target, const std::string &extname, const std::string &extdata) { };
211 void Module::ProtoSendMetaData(void* opaque, int target_type, void* target, const std::string &extname, const std::string &extdata) { };
212 void Module::OnWallops(userrec* user, const std::string &text) { };
213 void Module::OnChangeHost(userrec* user, const std::string &newhost) { };
214 void Module::OnChangeName(userrec* user, const std::string &gecos) { };
215 void Module::OnAddGLine(long duration, userrec* source, const std::string &reason, const std::string &hostmask) { };
216 void Module::OnAddZLine(long duration, userrec* source, const std::string &reason, const std::string &ipmask) { };
217 void Module::OnAddKLine(long duration, userrec* source, const std::string &reason, const std::string &hostmask) { };
218 void Module::OnAddQLine(long duration, userrec* source, const std::string &reason, const std::string &nickmask) { };
219 void Module::OnAddELine(long duration, userrec* source, const std::string &reason, const std::string &hostmask) { };
220 void Module::OnDelGLine(userrec* source, const std::string &hostmask) { };
221 void Module::OnDelZLine(userrec* source, const std::string &ipmask) { };
222 void Module::OnDelKLine(userrec* source, const std::string &hostmask) { };
223 void Module::OnDelQLine(userrec* source, const std::string &nickmask) { };
224 void Module::OnDelELine(userrec* source, const std::string &hostmask) { };
225 void Module::OnCleanup(int target_type, void* item) { };
226 void Module::Implements(char* Implements) { for (int j = 0; j < 255; j++) Implements[j] = 0; };
227 void Module::OnChannelDelete(chanrec* chan) { };
228 Priority Module::Prioritize() { return PRIORITY_DONTCARE; }
229 void Module::OnSetAway(userrec* user) { };
230 void Module::OnCancelAway(userrec* user) { };
232 /* server is a wrapper class that provides methods to all of the C-style
233 * exports in the core
244 void Server::AddSocket(InspSocket* sock)
246 ServerInstance->module_sockets.push_back(sock);
249 void Server::RemoveSocket(InspSocket* sock)
251 for (std::vector<InspSocket*>::iterator a = ServerInstance->module_sockets.begin(); a < ServerInstance->module_sockets.end(); a++)
253 InspSocket* s = (InspSocket*)*a;
259 long Server::PriorityAfter(const std::string &modulename)
261 for (unsigned int j = 0; j < ServerInstance->Config->module_names.size(); j++)
263 if (ServerInstance->Config->module_names[j] == modulename)
265 return ((j << 8) | PRIORITY_AFTER);
268 return PRIORITY_DONTCARE;
271 long Server::PriorityBefore(const std::string &modulename)
273 for (unsigned int j = 0; j < ServerInstance->Config->module_names.size(); j++)
275 if (ServerInstance->Config->module_names[j] == modulename)
277 return ((j << 8) | PRIORITY_BEFORE);
280 return PRIORITY_DONTCARE;
283 bool Server::PublishFeature(const std::string &FeatureName, Module* Mod)
285 if (Features.find(FeatureName) == Features.end())
287 Features[FeatureName] = Mod;
293 bool Server::UnpublishFeature(const std::string &FeatureName)
295 featurelist::iterator iter = Features.find(FeatureName);
297 if (iter == Features.end())
300 Features.erase(iter);
304 Module* Server::FindFeature(const std::string &FeatureName)
306 featurelist::iterator iter = Features.find(FeatureName);
308 if (iter == Features.end())
314 const std::string& Server::GetModuleName(Module* m)
316 static std::string nothing = ""; /* Prevent compiler warning */
317 for (int i = 0; i <= MODCOUNT; i++)
321 return ServerInstance->Config->module_names[i];
324 return nothing; /* As above */
327 void Server::RehashServer()
329 ServerInstance->WriteOpers("*** Rehashing config file");
330 ServerInstance->Config->Read(false,NULL);
333 void Server::DelSocket(InspSocket* sock)
335 for (std::vector<InspSocket*>::iterator a = ServerInstance->module_sockets.begin(); a < ServerInstance->module_sockets.end(); a++)
339 ServerInstance->module_sockets.erase(a);
345 long Server::GetChannelCount()
347 return (long)ServerInstance->chanlist.size();
350 /* This is ugly, yes, but hash_map's arent designed to be
351 * addressed in this manner, and this is a bit of a kludge.
352 * Luckily its a specialist function and rarely used by
353 * many modules (in fact, it was specially created to make
354 * m_safelist possible, initially).
357 chanrec* Server::GetChannelIndex(long index)
360 for (chan_hash::iterator n = ServerInstance->chanlist.begin(); n != ServerInstance->chanlist.end(); n++, target++)
368 bool Server::MatchText(const std::string &sliteral, const std::string &spattern)
370 return match(sliteral.c_str(),spattern.c_str());
373 bool Server::IsUlined(const std::string &server)
375 return is_uline(server.c_str());
378 bool Server::CallCommandHandler(const std::string &commandname, const char** parameters, int pcnt, userrec* user)
380 return ServerInstance->Parser->CallHandler(commandname,parameters,pcnt,user);
383 bool Server::IsValidModuleCommand(const std::string &commandname, int pcnt, userrec* user)
385 return ServerInstance->Parser->IsValidCommand(commandname, pcnt, user);
388 void Server::Log(int level, const std::string &s)
390 log(level,"%s",s.c_str());
393 void Server::AddCommand(command_t *f)
395 if (!ServerInstance->Parser->CreateCommand(f))
397 ModuleException err("Command "+std::string(f->command)+" already exists.");
402 void Server::SendMode(const char** parameters, int pcnt, userrec *user)
404 ServerInstance->ModeGrok->Process(parameters,pcnt,user,true);
407 void Server::DumpText(userrec* User, const std::string &LinePrefix, stringstream &TextStream)
409 std::string CompleteLine = LinePrefix;
410 std::string Word = "";
411 while (TextStream >> Word)
413 if (CompleteLine.length() + Word.length() + 3 > 500)
415 User->WriteServ(CompleteLine);
416 CompleteLine = LinePrefix;
418 CompleteLine = CompleteLine + Word + " ";
420 User->WriteServ(CompleteLine);
423 bool Server::IsNick(const std::string &nick)
425 return (isnick(nick.c_str()) != 0);
428 userrec* Server::FindDescriptor(int socket)
430 return (socket < 65536 ? ServerInstance->fd_ref_table[socket] : NULL);
433 std::string Server::GetServerName()
435 return ServerInstance->Config->ServerName;
438 std::string Server::GetNetworkName()
440 return ServerInstance->Config->Network;
443 std::string Server::GetServerDescription()
445 return ServerInstance->Config->ServerDesc;
448 Admin Server::GetAdmin()
450 return Admin(ServerInstance->Config->AdminName,ServerInstance->Config->AdminEmail,ServerInstance->Config->AdminNick);
454 bool Server::AddMode(ModeHandler* mh, const unsigned char mode)
456 return ServerInstance->ModeGrok->AddMode(mh,mode);
459 bool Server::AddModeWatcher(ModeWatcher* mw)
461 return ServerInstance->ModeGrok->AddModeWatcher(mw);
464 bool Server::DelModeWatcher(ModeWatcher* mw)
466 return ServerInstance->ModeGrok->DelModeWatcher(mw);
469 bool Server::AddResolver(Resolver* r)
471 return ServerInstance->Res->AddResolverClass(r);
474 bool InspIRCd::UserToPseudo(userrec* user, const std::string &message)
476 unsigned int old_fd = user->fd;
477 user->Write("ERROR :Closing link (%s@%s) [%s]",user->ident,user->host,message.c_str());
478 user->FlushWriteBuf();
480 user->fd = FD_MAGIC_NUMBER;
482 if (find(local_users.begin(),local_users.end(),user) != local_users.end())
484 local_users.erase(find(local_users.begin(),local_users.end(),user));
485 log(DEBUG,"Delete local user");
488 ServerInstance->SE->DelFd(old_fd);
494 bool InspIRCd::PseudoToUser(userrec* alive, userrec* zombie, const std::string &message)
496 log(DEBUG,"PseudoToUser");
497 zombie->fd = alive->fd;
498 FOREACH_MOD(I_OnUserQuit,OnUserQuit(alive,message));
499 alive->fd = FD_MAGIC_NUMBER;
500 alive->FlushWriteBuf();
501 alive->ClearBuffer();
502 // save these for later
503 std::string oldnick = alive->nick;
504 std::string oldhost = alive->host;
505 std::string oldident = alive->ident;
506 userrec::QuitUser(this,alive,message.c_str());
507 if (find(local_users.begin(),local_users.end(),alive) != local_users.end())
509 local_users.erase(find(local_users.begin(),local_users.end(),alive));
510 log(DEBUG,"Delete local user");
512 // Fix by brain - cant write the user until their fd table entry is updated
513 ServerInstance->fd_ref_table[zombie->fd] = zombie;
514 zombie->Write(":%s!%s@%s NICK %s",oldnick.c_str(),oldident.c_str(),oldhost.c_str(),zombie->nick);
515 for (std::vector<ucrec*>::const_iterator i = zombie->chans.begin(); i != zombie->chans.end(); i++)
517 if (((ucrec*)(*i))->channel != NULL)
519 chanrec* Ptr = ((ucrec*)(*i))->channel;
520 zombie->WriteFrom(zombie,"JOIN %s",Ptr->name);
523 zombie->WriteServ("332 %s %s :%s", zombie->nick, Ptr->name, Ptr->topic);
524 zombie->WriteServ("333 %s %s %s %d", zombie->nick, Ptr->name, Ptr->setby, Ptr->topicset);
526 Ptr->UserList(zombie);
527 zombie->WriteServ("366 %s %s :End of /NAMES list.", zombie->nick, Ptr->name);
530 if ((find(local_users.begin(),local_users.end(),zombie) == local_users.end()) && (zombie->fd != FD_MAGIC_NUMBER))
531 local_users.push_back(zombie);
536 void Server::AddGLine(long duration, const std::string &source, const std::string &reason, const std::string &hostmask)
538 add_gline(duration, source.c_str(), reason.c_str(), hostmask.c_str());
539 apply_lines(APPLY_GLINES);
542 void Server::AddQLine(long duration, const std::string &source, const std::string &reason, const std::string &nickname)
544 add_qline(duration, source.c_str(), reason.c_str(), nickname.c_str());
545 apply_lines(APPLY_QLINES);
548 void Server::AddZLine(long duration, const std::string &source, const std::string &reason, const std::string &ipaddr)
550 add_zline(duration, source.c_str(), reason.c_str(), ipaddr.c_str());
551 apply_lines(APPLY_ZLINES);
554 void Server::AddKLine(long duration, const std::string &source, const std::string &reason, const std::string &hostmask)
556 add_kline(duration, source.c_str(), reason.c_str(), hostmask.c_str());
557 apply_lines(APPLY_KLINES);
560 void Server::AddELine(long duration, const std::string &source, const std::string &reason, const std::string &hostmask)
562 add_eline(duration, source.c_str(), reason.c_str(), hostmask.c_str());
565 bool Server::DelGLine(const std::string &hostmask)
567 return del_gline(hostmask.c_str());
570 bool Server::DelQLine(const std::string &nickname)
572 return del_qline(nickname.c_str());
575 bool Server::DelZLine(const std::string &ipaddr)
577 return del_zline(ipaddr.c_str());
580 bool Server::DelKLine(const std::string &hostmask)
582 return del_kline(hostmask.c_str());
585 bool Server::DelELine(const std::string &hostmask)
587 return del_eline(hostmask.c_str());
590 long Server::CalcDuration(const std::string &delta)
592 return duration(delta.c_str());
596 * XXX why on *earth* is this in modules.cpp...? I think
597 * perhaps we need a server.cpp for Server:: stuff where possible. -- w00t
599 bool Server::IsValidMask(const std::string &mask)
601 char* dest = (char*)mask.c_str();
602 if (strchr(dest,'!')==0)
604 if (strchr(dest,'@')==0)
606 for (char* i = dest; *i; i++)
609 for (char* i = dest; *i; i++)
613 for (char* i = dest; *i; i++)
619 for (char* i = dest; *i; i++)
628 Module* Server::FindModule(const std::string &name)
630 for (int i = 0; i <= MODCOUNT; i++)
632 if (ServerInstance->Config->module_names[i] == name)
640 ConfigReader::ConfigReader()
642 // ServerInstance->Config->ClearStack();
644 /* Is there any reason to load the entire config file again here?
645 * it's needed if they specify another config file, but using the
646 * default one we can just use the global config data - pre-parsed!
648 //~ this->cache = new std::stringstream(std::stringstream::in | std::stringstream::out);
649 this->errorlog = new std::ostringstream(std::stringstream::in | std::stringstream::out);
651 //~ this->readerror = ServerInstance->Config->LoadConf(CONFIG_FILE, this->cache,this->errorlog);
652 //~ if (!this->readerror)
653 //~ this->error = CONF_FILE_NOT_FOUND;
655 this->data = &ServerInstance->Config->config_data;
656 this->privatehash = false;
660 ConfigReader::~ConfigReader()
663 //~ delete this->cache;
665 DELETE(this->errorlog);
666 if(this->privatehash)
671 ConfigReader::ConfigReader(const std::string &filename)
673 ServerInstance->Config->ClearStack();
675 this->data = new ConfigDataHash;
676 this->privatehash = true;
677 this->errorlog = new std::ostringstream(std::stringstream::in | std::stringstream::out);
678 this->readerror = ServerInstance->Config->LoadConf(*this->data, filename, *this->errorlog);
679 if (!this->readerror)
680 this->error = CONF_FILE_NOT_FOUND;
683 std::string ConfigReader::ReadValue(const std::string &tag, const std::string &name, int index)
685 /* Don't need to strlcpy() tag and name anymore, ReadConf() takes const char* */
688 if (!ServerInstance->Config->ConfValue(*this->data, tag, name, index, result))
690 this->error = CONF_VALUE_NOT_FOUND;
697 bool ConfigReader::ReadFlag(const std::string &tag, const std::string &name, int index)
699 return ServerInstance->Config->ConfValueBool(*this->data, tag, name, index);
702 long ConfigReader::ReadInteger(const std::string &tag, const std::string &name, int index, bool needs_unsigned)
706 if(!ServerInstance->Config->ConfValueInteger(*this->data, tag, name, index, result))
708 this->error = CONF_VALUE_NOT_FOUND;
712 if ((needs_unsigned) && (result < 0))
714 this->error = CONF_NOT_UNSIGNED;
721 long ConfigReader::GetError()
723 long olderr = this->error;
728 void ConfigReader::DumpErrors(bool bail, userrec* user)
730 /* XXX - Duplicated code */
734 printf("There were errors in your configuration:\n%s", this->errorlog->str().c_str());
739 std::string errors = this->errorlog->str();
740 std::string::size_type start;
741 unsigned int prefixlen;
744 /* ":ServerInstance->Config->ServerName NOTICE user->nick :" */
745 prefixlen = strlen(ServerInstance->Config->ServerName) + strlen(user->nick) + 11;
749 user->WriteServ("NOTICE %s :There were errors in the configuration file:",user->nick);
751 while(start < errors.length())
753 user->WriteServ("NOTICE %s :%s",user->nick, errors.substr(start, 510 - prefixlen).c_str());
754 start += 510 - prefixlen;
759 ServerInstance->WriteOpers("There were errors in the configuration file:");
761 while(start < errors.length())
763 ServerInstance->WriteOpers(errors.substr(start, 360).c_str());
773 int ConfigReader::Enumerate(const std::string &tag)
775 return ServerInstance->Config->ConfValueEnum(*this->data, tag);
778 int ConfigReader::EnumerateValues(const std::string &tag, int index)
780 return ServerInstance->Config->ConfVarEnum(*this->data, tag, index);
783 bool ConfigReader::Verify()
785 return this->readerror;
789 FileReader::FileReader(const std::string &filename)
792 ServerInstance->Config->ReadFile(c,filename.c_str());
797 FileReader::FileReader()
801 std::string FileReader::Contents()
804 for (file_cache::iterator a = this->fc.begin(); a != this->fc.end(); a++)
812 unsigned long FileReader::ContentSize()
814 return this->contentsize;
817 void FileReader::CalcSize()
820 for (file_cache::iterator a = this->fc.begin(); a != this->fc.end(); a++)
821 n += (a->length() + 2);
822 this->contentsize = n;
825 void FileReader::LoadFile(const std::string &filename)
828 ServerInstance->Config->ReadFile(c,filename.c_str());
834 FileReader::~FileReader()
838 bool FileReader::Exists()
840 return (!(fc.size() == 0));
843 std::string FileReader::GetLine(int x)
845 if ((x<0) || ((unsigned)x>fc.size()))
850 int FileReader::FileSize()
856 std::vector<Module*> modules(255);
857 std::vector<ircd_module*> factory(255);