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 Request::Request(char* anydata, Module* src, Module* dst)
68 : data(anydata), source(src), dest(dst)
70 /* Ensure that because this module doesnt support ID strings, it doesnt break modules that do
71 * by passing them uninitialized pointers (could happen)
76 Request::Request(Module* src, Module* dst, const char* idstr)
77 : id(idstr), source(src), dest(dst)
81 char* Request::GetData()
86 const char* Request::GetId()
91 Module* Request::GetSource()
96 Module* Request::GetDest()
101 char* Request::Send()
105 return dest->OnRequest(this);
113 Event::Event(char* anydata, Module* src, const std::string &eventid) : data(anydata), source(src), id(eventid) { };
115 char* Event::GetData()
117 return (char*)this->data;
120 Module* Event::GetSource()
127 FOREACH_MOD(I_OnEvent,OnEvent(this));
131 std::string Event::GetEventID()
137 // These declarations define the behavours of the base class Module (which does nothing at all)
139 Module::Module(Server* Me) { }
140 Module::~Module() { }
141 void Module::OnUserConnect(userrec* user) { }
142 void Module::OnUserQuit(userrec* user, const std::string& message) { }
143 void Module::OnUserDisconnect(userrec* user) { }
144 void Module::OnUserJoin(userrec* user, chanrec* channel) { }
145 void Module::OnUserPart(userrec* user, chanrec* channel, const std::string &partmessage) { }
146 void Module::OnRehash(const std::string ¶meter) { }
147 void Module::OnServerRaw(std::string &raw, bool inbound, userrec* user) { }
148 int Module::OnUserPreJoin(userrec* user, chanrec* chan, const char* cname) { return 0; }
149 void Module::OnMode(userrec* user, void* dest, int target_type, const std::string &text) { };
150 Version Module::GetVersion() { return Version(1,0,0,0,VF_VENDOR); }
151 void Module::OnOper(userrec* user, const std::string &opertype) { };
152 void Module::OnPostOper(userrec* user, const std::string &opertype) { };
153 void Module::OnInfo(userrec* user) { };
154 void Module::OnWhois(userrec* source, userrec* dest) { };
155 int Module::OnUserPreInvite(userrec* source,userrec* dest,chanrec* channel) { return 0; };
156 int Module::OnUserPreMessage(userrec* user,void* dest,int target_type, std::string &text,char status) { return 0; };
157 int Module::OnUserPreNotice(userrec* user,void* dest,int target_type, std::string &text,char status) { return 0; };
158 int Module::OnUserPreNick(userrec* user, const std::string &newnick) { return 0; };
159 void Module::OnUserPostNick(userrec* user, const std::string &oldnick) { };
160 int Module::OnAccessCheck(userrec* source,userrec* dest,chanrec* channel,int access_type) { return ACR_DEFAULT; };
161 void Module::On005Numeric(std::string &output) { };
162 int Module::OnKill(userrec* source, userrec* dest, const std::string &reason) { return 0; };
163 void Module::OnLoadModule(Module* mod,const std::string &name) { };
164 void Module::OnUnloadModule(Module* mod,const std::string &name) { };
165 void Module::OnBackgroundTimer(time_t curtime) { };
166 int Module::OnPreCommand(const std::string &command, const char** parameters, int pcnt, userrec *user, bool validated) { return 0; };
167 bool Module::OnCheckReady(userrec* user) { return true; };
168 void Module::OnUserRegister(userrec* user) { };
169 int Module::OnUserPreKick(userrec* source, userrec* user, chanrec* chan, const std::string &reason) { return 0; };
170 void Module::OnUserKick(userrec* source, userrec* user, chanrec* chan, const std::string &reason) { };
171 int Module::OnRawMode(userrec* user, chanrec* chan, char mode, const std::string ¶m, bool adding, int pcnt) { return 0; };
172 int Module::OnCheckInvite(userrec* user, chanrec* chan) { return 0; };
173 int Module::OnCheckKey(userrec* user, chanrec* chan, const std::string &keygiven) { return 0; };
174 int Module::OnCheckLimit(userrec* user, chanrec* chan) { return 0; };
175 int Module::OnCheckBan(userrec* user, chanrec* chan) { return 0; };
176 int Module::OnStats(char symbol, userrec* user, string_list &results) { return 0; };
177 int Module::OnChangeLocalUserHost(userrec* user, const std::string &newhost) { return 0; };
178 int Module::OnChangeLocalUserGECOS(userrec* user, const std::string &newhost) { return 0; };
179 int Module::OnLocalTopicChange(userrec* user, chanrec* chan, const std::string &topic) { return 0; };
180 void Module::OnEvent(Event* event) { return; };
181 char* Module::OnRequest(Request* request) { return NULL; };
182 int Module::OnOperCompare(const std::string &password, const std::string &input) { return 0; };
183 void Module::OnGlobalOper(userrec* user) { };
184 void Module::OnGlobalConnect(userrec* user) { };
185 int Module::OnAddBan(userrec* source, chanrec* channel,const std::string &banmask) { return 0; };
186 int Module::OnDelBan(userrec* source, chanrec* channel,const std::string &banmask) { return 0; };
187 void Module::OnRawSocketAccept(int fd, const std::string &ip, int localport) { };
188 int Module::OnRawSocketWrite(int fd, const char* buffer, int count) { return 0; };
189 void Module::OnRawSocketClose(int fd) { };
190 int Module::OnRawSocketRead(int fd, char* buffer, unsigned int count, int &readresult) { return 0; };
191 void Module::OnUserMessage(userrec* user, void* dest, int target_type, const std::string &text, char status) { };
192 void Module::OnUserNotice(userrec* user, void* dest, int target_type, const std::string &text, char status) { };
193 void Module::OnRemoteKill(userrec* source, userrec* dest, const std::string &reason) { };
194 void Module::OnUserInvite(userrec* source,userrec* dest,chanrec* channel) { };
195 void Module::OnPostLocalTopicChange(userrec* user, chanrec* chan, const std::string &topic) { };
196 void Module::OnGetServerDescription(const std::string &servername,std::string &description) { };
197 void Module::OnSyncUser(userrec* user, Module* proto, void* opaque) { };
198 void Module::OnSyncChannel(chanrec* chan, Module* proto, void* opaque) { };
199 void Module::ProtoSendMode(void* opaque, int target_type, void* target, const std::string &modeline) { };
200 void Module::OnSyncChannelMetaData(chanrec* chan, Module* proto,void* opaque, const std::string &extname) { };
201 void Module::OnSyncUserMetaData(userrec* user, Module* proto,void* opaque, const std::string &extname) { };
202 void Module::OnSyncOtherMetaData(Module* proto, void* opaque) { };
203 void Module::OnDecodeMetaData(int target_type, void* target, const std::string &extname, const std::string &extdata) { };
204 void Module::ProtoSendMetaData(void* opaque, int target_type, void* target, const std::string &extname, const std::string &extdata) { };
205 void Module::OnWallops(userrec* user, const std::string &text) { };
206 void Module::OnChangeHost(userrec* user, const std::string &newhost) { };
207 void Module::OnChangeName(userrec* user, const std::string &gecos) { };
208 void Module::OnAddGLine(long duration, userrec* source, const std::string &reason, const std::string &hostmask) { };
209 void Module::OnAddZLine(long duration, userrec* source, const std::string &reason, const std::string &ipmask) { };
210 void Module::OnAddKLine(long duration, userrec* source, const std::string &reason, const std::string &hostmask) { };
211 void Module::OnAddQLine(long duration, userrec* source, const std::string &reason, const std::string &nickmask) { };
212 void Module::OnAddELine(long duration, userrec* source, const std::string &reason, const std::string &hostmask) { };
213 void Module::OnDelGLine(userrec* source, const std::string &hostmask) { };
214 void Module::OnDelZLine(userrec* source, const std::string &ipmask) { };
215 void Module::OnDelKLine(userrec* source, const std::string &hostmask) { };
216 void Module::OnDelQLine(userrec* source, const std::string &nickmask) { };
217 void Module::OnDelELine(userrec* source, const std::string &hostmask) { };
218 void Module::OnCleanup(int target_type, void* item) { };
219 void Module::Implements(char* Implements) { for (int j = 0; j < 255; j++) Implements[j] = 0; };
220 void Module::OnChannelDelete(chanrec* chan) { };
221 Priority Module::Prioritize() { return PRIORITY_DONTCARE; }
222 void Module::OnSetAway(userrec* user) { };
223 void Module::OnCancelAway(userrec* user) { };
225 /* server is a wrapper class that provides methods to all of the C-style
226 * exports in the core
229 void Server::AddSocket(InspSocket* sock)
231 ServerInstance->module_sockets.push_back(sock);
234 void Server::RemoveSocket(InspSocket* sock)
236 for (std::vector<InspSocket*>::iterator a = ServerInstance->module_sockets.begin(); a < ServerInstance->module_sockets.end(); a++)
238 InspSocket* s = (InspSocket*)*a;
244 long Server::PriorityAfter(const std::string &modulename)
246 for (unsigned int j = 0; j < ServerInstance->Config->module_names.size(); j++)
248 if (ServerInstance->Config->module_names[j] == modulename)
250 return ((j << 8) | PRIORITY_AFTER);
253 return PRIORITY_DONTCARE;
256 long Server::PriorityBefore(const std::string &modulename)
258 for (unsigned int j = 0; j < ServerInstance->Config->module_names.size(); j++)
260 if (ServerInstance->Config->module_names[j] == modulename)
262 return ((j << 8) | PRIORITY_BEFORE);
265 return PRIORITY_DONTCARE;
268 bool Server::PublishFeature(const std::string &FeatureName, Module* Mod)
270 if (Features.find(FeatureName) == Features.end())
272 Features[FeatureName] = Mod;
278 bool Server::UnpublishFeature(const std::string &FeatureName)
280 featurelist::iterator iter = Features.find(FeatureName);
282 if (iter == Features.end())
285 Features.erase(iter);
289 Module* Server::FindFeature(const std::string &FeatureName)
291 featurelist::iterator iter = Features.find(FeatureName);
293 if (iter == Features.end())
299 const std::string& Server::GetModuleName(Module* m)
301 static std::string nothing = ""; /* Prevent compiler warning */
302 for (int i = 0; i <= MODCOUNT; i++)
306 return ServerInstance->Config->module_names[i];
309 return nothing; /* As above */
312 void Server::RehashServer()
314 ServerInstance->WriteOpers("*** Rehashing config file");
315 ServerInstance->Config->Read(false,NULL);
318 void Server::DelSocket(InspSocket* sock)
320 for (std::vector<InspSocket*>::iterator a = ServerInstance->module_sockets.begin(); a < ServerInstance->module_sockets.end(); a++)
324 ServerInstance->module_sockets.erase(a);
330 long Server::GetChannelCount()
332 return (long)ServerInstance->chanlist.size();
335 /* This is ugly, yes, but hash_map's arent designed to be
336 * addressed in this manner, and this is a bit of a kludge.
337 * Luckily its a specialist function and rarely used by
338 * many modules (in fact, it was specially created to make
339 * m_safelist possible, initially).
342 chanrec* Server::GetChannelIndex(long index)
345 for (chan_hash::iterator n = ServerInstance->chanlist.begin(); n != ServerInstance->chanlist.end(); n++, target++)
353 bool Server::MatchText(const std::string &sliteral, const std::string &spattern)
355 return match(sliteral.c_str(),spattern.c_str());
358 bool Server::IsUlined(const std::string &server)
360 return is_uline(server.c_str());
363 bool Server::CallCommandHandler(const std::string &commandname, const char** parameters, int pcnt, userrec* user)
365 return ServerInstance->Parser->CallHandler(commandname,parameters,pcnt,user);
368 bool Server::IsValidModuleCommand(const std::string &commandname, int pcnt, userrec* user)
370 return ServerInstance->Parser->IsValidCommand(commandname, pcnt, user);
373 void Server::Log(int level, const std::string &s)
375 log(level,"%s",s.c_str());
378 void Server::AddCommand(command_t *f)
380 if (!ServerInstance->Parser->CreateCommand(f))
382 ModuleException err("Command "+std::string(f->command)+" already exists.");
387 void Server::SendMode(const char** parameters, int pcnt, userrec *user)
389 ServerInstance->ModeGrok->Process(parameters,pcnt,user,true);
392 void Server::DumpText(userrec* User, const std::string &LinePrefix, stringstream &TextStream)
394 std::string CompleteLine = LinePrefix;
395 std::string Word = "";
396 while (TextStream >> Word)
398 if (CompleteLine.length() + Word.length() + 3 > 500)
400 User->WriteServ(CompleteLine);
401 CompleteLine = LinePrefix;
403 CompleteLine = CompleteLine + Word + " ";
405 User->WriteServ(CompleteLine);
408 bool Server::IsNick(const std::string &nick)
410 return (isnick(nick.c_str()) != 0);
413 userrec* Server::FindDescriptor(int socket)
415 return (socket < 65536 ? ServerInstance->fd_ref_table[socket] : NULL);
418 bool Server::AddMode(ModeHandler* mh, const unsigned char mode)
420 return ServerInstance->ModeGrok->AddMode(mh,mode);
423 bool Server::AddModeWatcher(ModeWatcher* mw)
425 return ServerInstance->ModeGrok->AddModeWatcher(mw);
428 bool Server::DelModeWatcher(ModeWatcher* mw)
430 return ServerInstance->ModeGrok->DelModeWatcher(mw);
433 bool Server::AddResolver(Resolver* r)
435 return ServerInstance->Res->AddResolverClass(r);
438 bool InspIRCd::UserToPseudo(userrec* user, const std::string &message)
440 unsigned int old_fd = user->fd;
441 user->Write("ERROR :Closing link (%s@%s) [%s]",user->ident,user->host,message.c_str());
442 user->FlushWriteBuf();
444 user->fd = FD_MAGIC_NUMBER;
446 if (find(local_users.begin(),local_users.end(),user) != local_users.end())
448 local_users.erase(find(local_users.begin(),local_users.end(),user));
449 log(DEBUG,"Delete local user");
452 ServerInstance->SE->DelFd(old_fd);
458 bool InspIRCd::PseudoToUser(userrec* alive, userrec* zombie, const std::string &message)
460 log(DEBUG,"PseudoToUser");
461 zombie->fd = alive->fd;
462 FOREACH_MOD(I_OnUserQuit,OnUserQuit(alive,message));
463 alive->fd = FD_MAGIC_NUMBER;
464 alive->FlushWriteBuf();
465 alive->ClearBuffer();
466 // save these for later
467 std::string oldnick = alive->nick;
468 std::string oldhost = alive->host;
469 std::string oldident = alive->ident;
470 userrec::QuitUser(this,alive,message.c_str());
471 if (find(local_users.begin(),local_users.end(),alive) != local_users.end())
473 local_users.erase(find(local_users.begin(),local_users.end(),alive));
474 log(DEBUG,"Delete local user");
476 // Fix by brain - cant write the user until their fd table entry is updated
477 ServerInstance->fd_ref_table[zombie->fd] = zombie;
478 zombie->Write(":%s!%s@%s NICK %s",oldnick.c_str(),oldident.c_str(),oldhost.c_str(),zombie->nick);
479 for (std::vector<ucrec*>::const_iterator i = zombie->chans.begin(); i != zombie->chans.end(); i++)
481 if (((ucrec*)(*i))->channel != NULL)
483 chanrec* Ptr = ((ucrec*)(*i))->channel;
484 zombie->WriteFrom(zombie,"JOIN %s",Ptr->name);
487 zombie->WriteServ("332 %s %s :%s", zombie->nick, Ptr->name, Ptr->topic);
488 zombie->WriteServ("333 %s %s %s %d", zombie->nick, Ptr->name, Ptr->setby, Ptr->topicset);
490 Ptr->UserList(zombie);
491 zombie->WriteServ("366 %s %s :End of /NAMES list.", zombie->nick, Ptr->name);
494 if ((find(local_users.begin(),local_users.end(),zombie) == local_users.end()) && (zombie->fd != FD_MAGIC_NUMBER))
495 local_users.push_back(zombie);
500 void Server::AddGLine(long duration, const std::string &source, const std::string &reason, const std::string &hostmask)
502 add_gline(duration, source.c_str(), reason.c_str(), hostmask.c_str());
503 apply_lines(APPLY_GLINES);
506 void Server::AddQLine(long duration, const std::string &source, const std::string &reason, const std::string &nickname)
508 add_qline(duration, source.c_str(), reason.c_str(), nickname.c_str());
509 apply_lines(APPLY_QLINES);
512 void Server::AddZLine(long duration, const std::string &source, const std::string &reason, const std::string &ipaddr)
514 add_zline(duration, source.c_str(), reason.c_str(), ipaddr.c_str());
515 apply_lines(APPLY_ZLINES);
518 void Server::AddKLine(long duration, const std::string &source, const std::string &reason, const std::string &hostmask)
520 add_kline(duration, source.c_str(), reason.c_str(), hostmask.c_str());
521 apply_lines(APPLY_KLINES);
524 void Server::AddELine(long duration, const std::string &source, const std::string &reason, const std::string &hostmask)
526 add_eline(duration, source.c_str(), reason.c_str(), hostmask.c_str());
529 bool Server::DelGLine(const std::string &hostmask)
531 return del_gline(hostmask.c_str());
534 bool Server::DelQLine(const std::string &nickname)
536 return del_qline(nickname.c_str());
539 bool Server::DelZLine(const std::string &ipaddr)
541 return del_zline(ipaddr.c_str());
544 bool Server::DelKLine(const std::string &hostmask)
546 return del_kline(hostmask.c_str());
549 bool Server::DelELine(const std::string &hostmask)
551 return del_eline(hostmask.c_str());
554 long Server::CalcDuration(const std::string &delta)
556 return duration(delta.c_str());
560 * XXX why on *earth* is this in modules.cpp...? I think
561 * perhaps we need a server.cpp for Server:: stuff where possible. -- w00t
563 bool Server::IsValidMask(const std::string &mask)
565 char* dest = (char*)mask.c_str();
566 if (strchr(dest,'!')==0)
568 if (strchr(dest,'@')==0)
570 for (char* i = dest; *i; i++)
573 for (char* i = dest; *i; i++)
577 for (char* i = dest; *i; i++)
583 for (char* i = dest; *i; i++)
592 Module* Server::FindModule(const std::string &name)
594 for (int i = 0; i <= MODCOUNT; i++)
596 if (ServerInstance->Config->module_names[i] == name)
604 ConfigReader::ConfigReader()
606 // ServerInstance->Config->ClearStack();
608 /* Is there any reason to load the entire config file again here?
609 * it's needed if they specify another config file, but using the
610 * default one we can just use the global config data - pre-parsed!
612 //~ this->cache = new std::stringstream(std::stringstream::in | std::stringstream::out);
613 this->errorlog = new std::ostringstream(std::stringstream::in | std::stringstream::out);
615 //~ this->readerror = ServerInstance->Config->LoadConf(CONFIG_FILE, this->cache,this->errorlog);
616 //~ if (!this->readerror)
617 //~ this->error = CONF_FILE_NOT_FOUND;
619 this->data = &ServerInstance->Config->config_data;
620 this->privatehash = false;
624 ConfigReader::~ConfigReader()
627 //~ delete this->cache;
629 DELETE(this->errorlog);
630 if(this->privatehash)
635 ConfigReader::ConfigReader(const std::string &filename)
637 ServerInstance->Config->ClearStack();
639 this->data = new ConfigDataHash;
640 this->privatehash = true;
641 this->errorlog = new std::ostringstream(std::stringstream::in | std::stringstream::out);
642 this->readerror = ServerInstance->Config->LoadConf(*this->data, filename, *this->errorlog);
643 if (!this->readerror)
644 this->error = CONF_FILE_NOT_FOUND;
647 std::string ConfigReader::ReadValue(const std::string &tag, const std::string &name, int index)
649 /* Don't need to strlcpy() tag and name anymore, ReadConf() takes const char* */
652 if (!ServerInstance->Config->ConfValue(*this->data, tag, name, index, result))
654 this->error = CONF_VALUE_NOT_FOUND;
661 bool ConfigReader::ReadFlag(const std::string &tag, const std::string &name, int index)
663 return ServerInstance->Config->ConfValueBool(*this->data, tag, name, index);
666 long ConfigReader::ReadInteger(const std::string &tag, const std::string &name, int index, bool needs_unsigned)
670 if(!ServerInstance->Config->ConfValueInteger(*this->data, tag, name, index, result))
672 this->error = CONF_VALUE_NOT_FOUND;
676 if ((needs_unsigned) && (result < 0))
678 this->error = CONF_NOT_UNSIGNED;
685 long ConfigReader::GetError()
687 long olderr = this->error;
692 void ConfigReader::DumpErrors(bool bail, userrec* user)
694 /* XXX - Duplicated code */
698 printf("There were errors in your configuration:\n%s", this->errorlog->str().c_str());
703 std::string errors = this->errorlog->str();
704 std::string::size_type start;
705 unsigned int prefixlen;
708 /* ":ServerInstance->Config->ServerName NOTICE user->nick :" */
709 prefixlen = strlen(ServerInstance->Config->ServerName) + strlen(user->nick) + 11;
713 user->WriteServ("NOTICE %s :There were errors in the configuration file:",user->nick);
715 while(start < errors.length())
717 user->WriteServ("NOTICE %s :%s",user->nick, errors.substr(start, 510 - prefixlen).c_str());
718 start += 510 - prefixlen;
723 ServerInstance->WriteOpers("There were errors in the configuration file:");
725 while(start < errors.length())
727 ServerInstance->WriteOpers(errors.substr(start, 360).c_str());
737 int ConfigReader::Enumerate(const std::string &tag)
739 return ServerInstance->Config->ConfValueEnum(*this->data, tag);
742 int ConfigReader::EnumerateValues(const std::string &tag, int index)
744 return ServerInstance->Config->ConfVarEnum(*this->data, tag, index);
747 bool ConfigReader::Verify()
749 return this->readerror;
753 FileReader::FileReader(const std::string &filename)
756 ServerInstance->Config->ReadFile(c,filename.c_str());
761 FileReader::FileReader()
765 std::string FileReader::Contents()
768 for (file_cache::iterator a = this->fc.begin(); a != this->fc.end(); a++)
776 unsigned long FileReader::ContentSize()
778 return this->contentsize;
781 void FileReader::CalcSize()
784 for (file_cache::iterator a = this->fc.begin(); a != this->fc.end(); a++)
785 n += (a->length() + 2);
786 this->contentsize = n;
789 void FileReader::LoadFile(const std::string &filename)
792 ServerInstance->Config->ReadFile(c,filename.c_str());
798 FileReader::~FileReader()
802 bool FileReader::Exists()
804 return (!(fc.size() == 0));
807 std::string FileReader::GetLine(int x)
809 if ((x<0) || ((unsigned)x>fc.size()))
814 int FileReader::FileSize()
820 std::vector<Module*> modules(255);
821 std::vector<ircd_module*> factory(255);