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>
37 #include "inspstring.h"
38 #include "helperfuncs.h"
41 #include "socketengine.h"
44 #include "command_parse.h"
48 extern InspIRCd* ServerInstance;
50 extern ModuleList modules;
51 extern FactoryList factory;
53 extern command_table cmdlist;
59 // version is a simple class for holding a modules version number
61 Version::Version(int major, int minor, int revision, int build, int flags)
62 : Major(major), Minor(minor), Revision(revision), Build(build), Flags(flags)
66 Request::Request(char* anydata, Module* src, Module* dst)
67 : data(anydata), source(src), dest(dst)
69 /* Ensure that because this module doesnt support ID strings, it doesnt break modules that do
70 * by passing them uninitialized pointers (could happen)
75 Request::Request(Module* src, Module* dst, const char* idstr)
76 : id(idstr), source(src), dest(dst)
80 char* Request::GetData()
85 const char* Request::GetId()
90 Module* Request::GetSource()
95 Module* Request::GetDest()
100 char* Request::Send()
104 return dest->OnRequest(this);
112 Event::Event(char* anydata, Module* src, const std::string &eventid) : data(anydata), source(src), id(eventid) { };
114 char* Event::GetData()
116 return (char*)this->data;
119 Module* Event::GetSource()
126 FOREACH_MOD(I_OnEvent,OnEvent(this));
130 std::string Event::GetEventID()
136 // These declarations define the behavours of the base class Module (which does nothing at all)
138 Module::Module(Server* Me) { }
139 Module::~Module() { }
140 void Module::OnUserConnect(userrec* user) { }
141 void Module::OnUserQuit(userrec* user, const std::string& message) { }
142 void Module::OnUserDisconnect(userrec* user) { }
143 void Module::OnUserJoin(userrec* user, chanrec* channel) { }
144 void Module::OnUserPart(userrec* user, chanrec* channel, const std::string &partmessage) { }
145 void Module::OnRehash(const std::string ¶meter) { }
146 void Module::OnServerRaw(std::string &raw, bool inbound, userrec* user) { }
147 int Module::OnUserPreJoin(userrec* user, chanrec* chan, const char* cname) { return 0; }
148 void Module::OnMode(userrec* user, void* dest, int target_type, const std::string &text) { };
149 Version Module::GetVersion() { return Version(1,0,0,0,VF_VENDOR); }
150 void Module::OnOper(userrec* user, const std::string &opertype) { };
151 void Module::OnPostOper(userrec* user, const std::string &opertype) { };
152 void Module::OnInfo(userrec* user) { };
153 void Module::OnWhois(userrec* source, userrec* dest) { };
154 int Module::OnUserPreInvite(userrec* source,userrec* dest,chanrec* channel) { return 0; };
155 int Module::OnUserPreMessage(userrec* user,void* dest,int target_type, std::string &text,char status) { return 0; };
156 int Module::OnUserPreNotice(userrec* user,void* dest,int target_type, std::string &text,char status) { return 0; };
157 int Module::OnUserPreNick(userrec* user, const std::string &newnick) { return 0; };
158 void Module::OnUserPostNick(userrec* user, const std::string &oldnick) { };
159 int Module::OnAccessCheck(userrec* source,userrec* dest,chanrec* channel,int access_type) { return ACR_DEFAULT; };
160 void Module::On005Numeric(std::string &output) { };
161 int Module::OnKill(userrec* source, userrec* dest, const std::string &reason) { return 0; };
162 void Module::OnLoadModule(Module* mod,const std::string &name) { };
163 void Module::OnUnloadModule(Module* mod,const std::string &name) { };
164 void Module::OnBackgroundTimer(time_t curtime) { };
165 int Module::OnPreCommand(const std::string &command, const char** parameters, int pcnt, userrec *user, bool validated) { return 0; };
166 bool Module::OnCheckReady(userrec* user) { return true; };
167 void Module::OnUserRegister(userrec* user) { };
168 int Module::OnUserPreKick(userrec* source, userrec* user, chanrec* chan, const std::string &reason) { return 0; };
169 void Module::OnUserKick(userrec* source, userrec* user, chanrec* chan, const std::string &reason) { };
170 int Module::OnRawMode(userrec* user, chanrec* chan, char mode, const std::string ¶m, bool adding, int pcnt) { return 0; };
171 int Module::OnCheckInvite(userrec* user, chanrec* chan) { return 0; };
172 int Module::OnCheckKey(userrec* user, chanrec* chan, const std::string &keygiven) { return 0; };
173 int Module::OnCheckLimit(userrec* user, chanrec* chan) { return 0; };
174 int Module::OnCheckBan(userrec* user, chanrec* chan) { return 0; };
175 int Module::OnStats(char symbol, userrec* user, string_list &results) { return 0; };
176 int Module::OnChangeLocalUserHost(userrec* user, const std::string &newhost) { return 0; };
177 int Module::OnChangeLocalUserGECOS(userrec* user, const std::string &newhost) { return 0; };
178 int Module::OnLocalTopicChange(userrec* user, chanrec* chan, const std::string &topic) { return 0; };
179 void Module::OnEvent(Event* event) { return; };
180 char* Module::OnRequest(Request* request) { return NULL; };
181 int Module::OnOperCompare(const std::string &password, const std::string &input) { return 0; };
182 void Module::OnGlobalOper(userrec* user) { };
183 void Module::OnGlobalConnect(userrec* user) { };
184 int Module::OnAddBan(userrec* source, chanrec* channel,const std::string &banmask) { return 0; };
185 int Module::OnDelBan(userrec* source, chanrec* channel,const std::string &banmask) { return 0; };
186 void Module::OnRawSocketAccept(int fd, const std::string &ip, int localport) { };
187 int Module::OnRawSocketWrite(int fd, const char* buffer, int count) { return 0; };
188 void Module::OnRawSocketClose(int fd) { };
189 int Module::OnRawSocketRead(int fd, char* buffer, unsigned int count, int &readresult) { return 0; };
190 void Module::OnUserMessage(userrec* user, void* dest, int target_type, const std::string &text, char status) { };
191 void Module::OnUserNotice(userrec* user, void* dest, int target_type, const std::string &text, char status) { };
192 void Module::OnRemoteKill(userrec* source, userrec* dest, const std::string &reason) { };
193 void Module::OnUserInvite(userrec* source,userrec* dest,chanrec* channel) { };
194 void Module::OnPostLocalTopicChange(userrec* user, chanrec* chan, const std::string &topic) { };
195 void Module::OnGetServerDescription(const std::string &servername,std::string &description) { };
196 void Module::OnSyncUser(userrec* user, Module* proto, void* opaque) { };
197 void Module::OnSyncChannel(chanrec* chan, Module* proto, void* opaque) { };
198 void Module::ProtoSendMode(void* opaque, int target_type, void* target, const std::string &modeline) { };
199 void Module::OnSyncChannelMetaData(chanrec* chan, Module* proto,void* opaque, const std::string &extname) { };
200 void Module::OnSyncUserMetaData(userrec* user, Module* proto,void* opaque, const std::string &extname) { };
201 void Module::OnSyncOtherMetaData(Module* proto, void* opaque) { };
202 void Module::OnDecodeMetaData(int target_type, void* target, const std::string &extname, const std::string &extdata) { };
203 void Module::ProtoSendMetaData(void* opaque, int target_type, void* target, const std::string &extname, const std::string &extdata) { };
204 void Module::OnWallops(userrec* user, const std::string &text) { };
205 void Module::OnChangeHost(userrec* user, const std::string &newhost) { };
206 void Module::OnChangeName(userrec* user, const std::string &gecos) { };
207 void Module::OnAddGLine(long duration, userrec* source, const std::string &reason, const std::string &hostmask) { };
208 void Module::OnAddZLine(long duration, userrec* source, const std::string &reason, const std::string &ipmask) { };
209 void Module::OnAddKLine(long duration, userrec* source, const std::string &reason, const std::string &hostmask) { };
210 void Module::OnAddQLine(long duration, userrec* source, const std::string &reason, const std::string &nickmask) { };
211 void Module::OnAddELine(long duration, userrec* source, const std::string &reason, const std::string &hostmask) { };
212 void Module::OnDelGLine(userrec* source, const std::string &hostmask) { };
213 void Module::OnDelZLine(userrec* source, const std::string &ipmask) { };
214 void Module::OnDelKLine(userrec* source, const std::string &hostmask) { };
215 void Module::OnDelQLine(userrec* source, const std::string &nickmask) { };
216 void Module::OnDelELine(userrec* source, const std::string &hostmask) { };
217 void Module::OnCleanup(int target_type, void* item) { };
218 void Module::Implements(char* Implements) { for (int j = 0; j < 255; j++) Implements[j] = 0; };
219 void Module::OnChannelDelete(chanrec* chan) { };
220 Priority Module::Prioritize() { return PRIORITY_DONTCARE; }
221 void Module::OnSetAway(userrec* user) { };
222 void Module::OnCancelAway(userrec* user) { };
224 /* server is a wrapper class that provides methods to all of the C-style
225 * exports in the core
228 void Server::AddSocket(InspSocket* sock)
230 ServerInstance->module_sockets.push_back(sock);
233 void Server::RemoveSocket(InspSocket* sock)
235 for (std::vector<InspSocket*>::iterator a = ServerInstance->module_sockets.begin(); a < ServerInstance->module_sockets.end(); a++)
237 InspSocket* s = (InspSocket*)*a;
243 long InspIRCd::PriorityAfter(const std::string &modulename)
245 for (unsigned int j = 0; j < this->Config->module_names.size(); j++)
247 if (this->Config->module_names[j] == modulename)
249 return ((j << 8) | PRIORITY_AFTER);
252 return PRIORITY_DONTCARE;
255 long InspIRCd::PriorityBefore(const std::string &modulename)
257 for (unsigned int j = 0; j < this->Config->module_names.size(); j++)
259 if (this->Config->module_names[j] == modulename)
261 return ((j << 8) | PRIORITY_BEFORE);
264 return PRIORITY_DONTCARE;
267 bool InspIRCd::PublishFeature(const std::string &FeatureName, Module* Mod)
269 if (Features.find(FeatureName) == Features.end())
271 Features[FeatureName] = Mod;
277 bool InspIRCd::UnpublishFeature(const std::string &FeatureName)
279 featurelist::iterator iter = Features.find(FeatureName);
281 if (iter == Features.end())
284 Features.erase(iter);
288 Module* InspIRCd::FindFeature(const std::string &FeatureName)
290 featurelist::iterator iter = Features.find(FeatureName);
292 if (iter == Features.end())
298 const std::string& InspIRCd::GetModuleName(Module* m)
300 static std::string nothing = ""; /* Prevent compiler warning */
301 for (int i = 0; i <= MODCOUNT; i++)
305 return this->Config->module_names[i];
308 return nothing; /* As above */
311 void Server::RehashServer()
313 ServerInstance->WriteOpers("*** Rehashing config file");
314 ServerInstance->Config->Read(false,NULL);
317 void Server::DelSocket(InspSocket* sock)
319 for (std::vector<InspSocket*>::iterator a = ServerInstance->module_sockets.begin(); a < ServerInstance->module_sockets.end(); a++)
323 ServerInstance->module_sockets.erase(a);
329 long Server::GetChannelCount()
331 return (long)ServerInstance->chanlist.size();
334 /* This is ugly, yes, but hash_map's arent designed to be
335 * addressed in this manner, and this is a bit of a kludge.
336 * Luckily its a specialist function and rarely used by
337 * many modules (in fact, it was specially created to make
338 * m_safelist possible, initially).
341 chanrec* Server::GetChannelIndex(long index)
344 for (chan_hash::iterator n = ServerInstance->chanlist.begin(); n != ServerInstance->chanlist.end(); n++, target++)
352 bool Server::MatchText(const std::string &sliteral, const std::string &spattern)
354 return match(sliteral.c_str(),spattern.c_str());
357 bool Server::IsUlined(const std::string &server)
359 return is_uline(server.c_str());
362 bool Server::CallCommandHandler(const std::string &commandname, const char** parameters, int pcnt, userrec* user)
364 return ServerInstance->Parser->CallHandler(commandname,parameters,pcnt,user);
367 bool Server::IsValidModuleCommand(const std::string &commandname, int pcnt, userrec* user)
369 return ServerInstance->Parser->IsValidCommand(commandname, pcnt, user);
372 void Server::AddCommand(command_t *f)
374 if (!ServerInstance->Parser->CreateCommand(f))
376 ModuleException err("Command "+std::string(f->command)+" already exists.");
381 void Server::SendMode(const char** parameters, int pcnt, userrec *user)
383 ServerInstance->ModeGrok->Process(parameters,pcnt,user,true);
386 void Server::DumpText(userrec* User, const std::string &LinePrefix, stringstream &TextStream)
388 std::string CompleteLine = LinePrefix;
389 std::string Word = "";
390 while (TextStream >> Word)
392 if (CompleteLine.length() + Word.length() + 3 > 500)
394 User->WriteServ(CompleteLine);
395 CompleteLine = LinePrefix;
397 CompleteLine = CompleteLine + Word + " ";
399 User->WriteServ(CompleteLine);
402 userrec* Server::FindDescriptor(int socket)
404 return (socket < 65536 ? ServerInstance->fd_ref_table[socket] : NULL);
407 bool Server::AddMode(ModeHandler* mh, const unsigned char mode)
409 return ServerInstance->ModeGrok->AddMode(mh,mode);
412 bool Server::AddModeWatcher(ModeWatcher* mw)
414 return ServerInstance->ModeGrok->AddModeWatcher(mw);
417 bool Server::DelModeWatcher(ModeWatcher* mw)
419 return ServerInstance->ModeGrok->DelModeWatcher(mw);
422 bool Server::AddResolver(Resolver* r)
424 return ServerInstance->Res->AddResolverClass(r);
427 bool InspIRCd::UserToPseudo(userrec* user, const std::string &message)
429 unsigned int old_fd = user->fd;
430 user->Write("ERROR :Closing link (%s@%s) [%s]",user->ident,user->host,message.c_str());
431 user->FlushWriteBuf();
433 user->fd = FD_MAGIC_NUMBER;
435 if (find(local_users.begin(),local_users.end(),user) != local_users.end())
437 local_users.erase(find(local_users.begin(),local_users.end(),user));
438 log(DEBUG,"Delete local user");
441 ServerInstance->SE->DelFd(old_fd);
447 bool InspIRCd::PseudoToUser(userrec* alive, userrec* zombie, const std::string &message)
449 log(DEBUG,"PseudoToUser");
450 zombie->fd = alive->fd;
451 FOREACH_MOD(I_OnUserQuit,OnUserQuit(alive,message));
452 alive->fd = FD_MAGIC_NUMBER;
453 alive->FlushWriteBuf();
454 alive->ClearBuffer();
455 // save these for later
456 std::string oldnick = alive->nick;
457 std::string oldhost = alive->host;
458 std::string oldident = alive->ident;
459 userrec::QuitUser(this,alive,message.c_str());
460 if (find(local_users.begin(),local_users.end(),alive) != local_users.end())
462 local_users.erase(find(local_users.begin(),local_users.end(),alive));
463 log(DEBUG,"Delete local user");
465 // Fix by brain - cant write the user until their fd table entry is updated
466 ServerInstance->fd_ref_table[zombie->fd] = zombie;
467 zombie->Write(":%s!%s@%s NICK %s",oldnick.c_str(),oldident.c_str(),oldhost.c_str(),zombie->nick);
468 for (std::vector<ucrec*>::const_iterator i = zombie->chans.begin(); i != zombie->chans.end(); i++)
470 if (((ucrec*)(*i))->channel != NULL)
472 chanrec* Ptr = ((ucrec*)(*i))->channel;
473 zombie->WriteFrom(zombie,"JOIN %s",Ptr->name);
476 zombie->WriteServ("332 %s %s :%s", zombie->nick, Ptr->name, Ptr->topic);
477 zombie->WriteServ("333 %s %s %s %d", zombie->nick, Ptr->name, Ptr->setby, Ptr->topicset);
479 Ptr->UserList(zombie);
480 zombie->WriteServ("366 %s %s :End of /NAMES list.", zombie->nick, Ptr->name);
483 if ((find(local_users.begin(),local_users.end(),zombie) == local_users.end()) && (zombie->fd != FD_MAGIC_NUMBER))
484 local_users.push_back(zombie);
489 void Server::AddGLine(long duration, const std::string &source, const std::string &reason, const std::string &hostmask)
491 add_gline(duration, source.c_str(), reason.c_str(), hostmask.c_str());
492 apply_lines(APPLY_GLINES);
495 void Server::AddQLine(long duration, const std::string &source, const std::string &reason, const std::string &nickname)
497 add_qline(duration, source.c_str(), reason.c_str(), nickname.c_str());
498 apply_lines(APPLY_QLINES);
501 void Server::AddZLine(long duration, const std::string &source, const std::string &reason, const std::string &ipaddr)
503 add_zline(duration, source.c_str(), reason.c_str(), ipaddr.c_str());
504 apply_lines(APPLY_ZLINES);
507 void Server::AddKLine(long duration, const std::string &source, const std::string &reason, const std::string &hostmask)
509 add_kline(duration, source.c_str(), reason.c_str(), hostmask.c_str());
510 apply_lines(APPLY_KLINES);
513 void Server::AddELine(long duration, const std::string &source, const std::string &reason, const std::string &hostmask)
515 add_eline(duration, source.c_str(), reason.c_str(), hostmask.c_str());
518 bool Server::DelGLine(const std::string &hostmask)
520 return del_gline(hostmask.c_str());
523 bool Server::DelQLine(const std::string &nickname)
525 return del_qline(nickname.c_str());
528 bool Server::DelZLine(const std::string &ipaddr)
530 return del_zline(ipaddr.c_str());
533 bool Server::DelKLine(const std::string &hostmask)
535 return del_kline(hostmask.c_str());
538 bool Server::DelELine(const std::string &hostmask)
540 return del_eline(hostmask.c_str());
543 long Server::CalcDuration(const std::string &delta)
545 return duration(delta.c_str());
549 * XXX why on *earth* is this in modules.cpp...? I think
550 * perhaps we need a server.cpp for Server:: stuff where possible. -- w00t
552 bool Server::IsValidMask(const std::string &mask)
554 char* dest = (char*)mask.c_str();
555 if (strchr(dest,'!')==0)
557 if (strchr(dest,'@')==0)
559 for (char* i = dest; *i; i++)
562 for (char* i = dest; *i; i++)
566 for (char* i = dest; *i; i++)
572 for (char* i = dest; *i; i++)
581 Module* Server::FindModule(const std::string &name)
583 for (int i = 0; i <= MODCOUNT; i++)
585 if (ServerInstance->Config->module_names[i] == name)
593 ConfigReader::ConfigReader()
595 // ServerInstance->Config->ClearStack();
597 /* Is there any reason to load the entire config file again here?
598 * it's needed if they specify another config file, but using the
599 * default one we can just use the global config data - pre-parsed!
601 //~ this->cache = new std::stringstream(std::stringstream::in | std::stringstream::out);
602 this->errorlog = new std::ostringstream(std::stringstream::in | std::stringstream::out);
604 //~ this->readerror = ServerInstance->Config->LoadConf(CONFIG_FILE, this->cache,this->errorlog);
605 //~ if (!this->readerror)
606 //~ this->error = CONF_FILE_NOT_FOUND;
608 this->data = &ServerInstance->Config->config_data;
609 this->privatehash = false;
613 ConfigReader::~ConfigReader()
616 //~ delete this->cache;
618 DELETE(this->errorlog);
619 if(this->privatehash)
624 ConfigReader::ConfigReader(const std::string &filename)
626 ServerInstance->Config->ClearStack();
628 this->data = new ConfigDataHash;
629 this->privatehash = true;
630 this->errorlog = new std::ostringstream(std::stringstream::in | std::stringstream::out);
631 this->readerror = ServerInstance->Config->LoadConf(*this->data, filename, *this->errorlog);
632 if (!this->readerror)
633 this->error = CONF_FILE_NOT_FOUND;
636 std::string ConfigReader::ReadValue(const std::string &tag, const std::string &name, int index)
638 /* Don't need to strlcpy() tag and name anymore, ReadConf() takes const char* */
641 if (!ServerInstance->Config->ConfValue(*this->data, tag, name, index, result))
643 this->error = CONF_VALUE_NOT_FOUND;
650 bool ConfigReader::ReadFlag(const std::string &tag, const std::string &name, int index)
652 return ServerInstance->Config->ConfValueBool(*this->data, tag, name, index);
655 long ConfigReader::ReadInteger(const std::string &tag, const std::string &name, int index, bool needs_unsigned)
659 if(!ServerInstance->Config->ConfValueInteger(*this->data, tag, name, index, result))
661 this->error = CONF_VALUE_NOT_FOUND;
665 if ((needs_unsigned) && (result < 0))
667 this->error = CONF_NOT_UNSIGNED;
674 long ConfigReader::GetError()
676 long olderr = this->error;
681 void ConfigReader::DumpErrors(bool bail, userrec* user)
683 /* XXX - Duplicated code */
687 printf("There were errors in your configuration:\n%s", this->errorlog->str().c_str());
688 InspIRCd::Exit(ERROR);
692 std::string errors = this->errorlog->str();
693 std::string::size_type start;
694 unsigned int prefixlen;
697 /* ":ServerInstance->Config->ServerName NOTICE user->nick :" */
698 prefixlen = strlen(ServerInstance->Config->ServerName) + strlen(user->nick) + 11;
702 user->WriteServ("NOTICE %s :There were errors in the configuration file:",user->nick);
704 while(start < errors.length())
706 user->WriteServ("NOTICE %s :%s",user->nick, errors.substr(start, 510 - prefixlen).c_str());
707 start += 510 - prefixlen;
712 ServerInstance->WriteOpers("There were errors in the configuration file:");
714 while(start < errors.length())
716 ServerInstance->WriteOpers(errors.substr(start, 360).c_str());
726 int ConfigReader::Enumerate(const std::string &tag)
728 return ServerInstance->Config->ConfValueEnum(*this->data, tag);
731 int ConfigReader::EnumerateValues(const std::string &tag, int index)
733 return ServerInstance->Config->ConfVarEnum(*this->data, tag, index);
736 bool ConfigReader::Verify()
738 return this->readerror;
742 FileReader::FileReader(const std::string &filename)
745 ServerInstance->Config->ReadFile(c,filename.c_str());
750 FileReader::FileReader()
754 std::string FileReader::Contents()
757 for (file_cache::iterator a = this->fc.begin(); a != this->fc.end(); a++)
765 unsigned long FileReader::ContentSize()
767 return this->contentsize;
770 void FileReader::CalcSize()
773 for (file_cache::iterator a = this->fc.begin(); a != this->fc.end(); a++)
774 n += (a->length() + 2);
775 this->contentsize = n;
778 void FileReader::LoadFile(const std::string &filename)
781 ServerInstance->Config->ReadFile(c,filename.c_str());
787 FileReader::~FileReader()
791 bool FileReader::Exists()
793 return (!(fc.size() == 0));
796 std::string FileReader::GetLine(int x)
798 if ((x<0) || ((unsigned)x>fc.size()))
803 int FileReader::FileSize()
809 std::vector<Module*> modules(255);
810 std::vector<ircd_module*> factory(255);