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 "configreader.h"
24 #include "socketengine.h"
25 #include "command_parse.h"
29 // version is a simple class for holding a modules version number
30 Version::Version(int major, int minor, int revision, int build, int flags, int api_ver)
31 : Major(major), Minor(minor), Revision(revision), Build(build), Flags(flags), API(api_ver)
35 Request::Request(char* anydata, Module* src, Module* dst)
36 : data(anydata), source(src), dest(dst)
38 /* Ensure that because this module doesnt support ID strings, it doesnt break modules that do
39 * by passing them uninitialized pointers (could happen)
44 Request::Request(Module* src, Module* dst, const char* idstr)
45 : id(idstr), source(src), dest(dst)
49 char* Request::GetData()
54 const char* Request::GetId()
59 Module* Request::GetSource()
64 Module* Request::GetDest()
73 return dest->OnRequest(this);
81 Event::Event(char* anydata, Module* src, const std::string &eventid) : data(anydata), source(src), id(eventid) { };
83 char* Event::GetData()
85 return (char*)this->data;
88 Module* Event::GetSource()
93 char* Event::Send(InspIRCd* ServerInstance)
95 FOREACH_MOD(I_OnEvent,OnEvent(this));
99 std::string Event::GetEventID()
105 // These declarations define the behavours of the base class Module (which does nothing at all)
107 Module::Module(InspIRCd* Me) : ServerInstance(Me) { }
108 Module::~Module() { }
109 void Module::OnUserConnect(userrec* user) { }
110 void Module::OnUserQuit(userrec* user, const std::string& message) { }
111 void Module::OnUserDisconnect(userrec* user) { }
112 void Module::OnUserJoin(userrec* user, chanrec* channel) { }
113 void Module::OnPostJoin(userrec* user, chanrec* channel) { }
114 void Module::OnUserPart(userrec* user, chanrec* channel, const std::string &partmessage) { }
115 void Module::OnRehash(const std::string ¶meter) { }
116 void Module::OnServerRaw(std::string &raw, bool inbound, userrec* user) { }
117 int Module::OnUserPreJoin(userrec* user, chanrec* chan, const char* cname, std::string &privs) { return 0; }
118 void Module::OnMode(userrec* user, void* dest, int target_type, const std::string &text) { };
119 Version Module::GetVersion() { return Version(1,0,0,0,VF_VENDOR,-1); }
120 void Module::OnOper(userrec* user, const std::string &opertype) { };
121 void Module::OnPostOper(userrec* user, const std::string &opertype) { };
122 void Module::OnInfo(userrec* user) { };
123 void Module::OnWhois(userrec* source, userrec* dest) { };
124 int Module::OnUserPreInvite(userrec* source,userrec* dest,chanrec* channel) { return 0; };
125 int Module::OnUserPreMessage(userrec* user,void* dest,int target_type, std::string &text,char status) { return 0; };
126 int Module::OnUserPreNotice(userrec* user,void* dest,int target_type, std::string &text,char status) { return 0; };
127 int Module::OnUserPreNick(userrec* user, const std::string &newnick) { return 0; };
128 void Module::OnUserPostNick(userrec* user, const std::string &oldnick) { };
129 int Module::OnAccessCheck(userrec* source,userrec* dest,chanrec* channel,int access_type) { return ACR_DEFAULT; };
130 void Module::On005Numeric(std::string &output) { };
131 int Module::OnKill(userrec* source, userrec* dest, const std::string &reason) { return 0; };
132 void Module::OnLoadModule(Module* mod,const std::string &name) { };
133 void Module::OnUnloadModule(Module* mod,const std::string &name) { };
134 void Module::OnBackgroundTimer(time_t curtime) { };
135 int Module::OnPreCommand(const std::string &command, const char** parameters, int pcnt, userrec *user, bool validated, const std::string &original_line) { return 0; };
136 void Module::OnPostCommand(const std::string &command, const char** parameters, int pcnt, userrec *user, CmdResult result, const std::string &original_line) { };
137 bool Module::OnCheckReady(userrec* user) { return true; };
138 void Module::OnUserRegister(userrec* user) { };
139 int Module::OnUserPreKick(userrec* source, userrec* user, chanrec* chan, const std::string &reason) { return 0; };
140 void Module::OnUserKick(userrec* source, userrec* user, chanrec* chan, const std::string &reason) { };
141 int Module::OnRawMode(userrec* user, chanrec* chan, char mode, const std::string ¶m, bool adding, int pcnt) { return 0; };
142 int Module::OnCheckInvite(userrec* user, chanrec* chan) { return 0; };
143 int Module::OnCheckKey(userrec* user, chanrec* chan, const std::string &keygiven) { return 0; };
144 int Module::OnCheckLimit(userrec* user, chanrec* chan) { return 0; };
145 int Module::OnCheckBan(userrec* user, chanrec* chan) { return 0; };
146 int Module::OnStats(char symbol, userrec* user, string_list &results) { return 0; };
147 int Module::OnChangeLocalUserHost(userrec* user, const std::string &newhost) { return 0; };
148 int Module::OnChangeLocalUserGECOS(userrec* user, const std::string &newhost) { return 0; };
149 int Module::OnLocalTopicChange(userrec* user, chanrec* chan, const std::string &topic) { return 0; };
150 void Module::OnEvent(Event* event) { return; };
151 char* Module::OnRequest(Request* request) { return NULL; };
152 int Module::OnOperCompare(const std::string &password, const std::string &input) { return 0; };
153 void Module::OnGlobalOper(userrec* user) { };
154 void Module::OnPostConnect(userrec* user) { };
155 int Module::OnAddBan(userrec* source, chanrec* channel,const std::string &banmask) { return 0; };
156 int Module::OnDelBan(userrec* source, chanrec* channel,const std::string &banmask) { return 0; };
157 void Module::OnRawSocketAccept(int fd, const std::string &ip, int localport) { };
158 int Module::OnRawSocketWrite(int fd, const char* buffer, int count) { return 0; };
159 void Module::OnRawSocketClose(int fd) { };
160 int Module::OnRawSocketRead(int fd, char* buffer, unsigned int count, int &readresult) { return 0; };
161 void Module::OnUserMessage(userrec* user, void* dest, int target_type, const std::string &text, char status) { };
162 void Module::OnUserNotice(userrec* user, void* dest, int target_type, const std::string &text, char status) { };
163 void Module::OnRemoteKill(userrec* source, userrec* dest, const std::string &reason) { };
164 void Module::OnUserInvite(userrec* source,userrec* dest,chanrec* channel) { };
165 void Module::OnPostLocalTopicChange(userrec* user, chanrec* chan, const std::string &topic) { };
166 void Module::OnGetServerDescription(const std::string &servername,std::string &description) { };
167 void Module::OnSyncUser(userrec* user, Module* proto, void* opaque) { };
168 void Module::OnSyncChannel(chanrec* chan, Module* proto, void* opaque) { };
169 void Module::ProtoSendMode(void* opaque, int target_type, void* target, const std::string &modeline) { };
170 void Module::OnSyncChannelMetaData(chanrec* chan, Module* proto,void* opaque, const std::string &extname) { };
171 void Module::OnSyncUserMetaData(userrec* user, Module* proto,void* opaque, const std::string &extname) { };
172 void Module::OnSyncOtherMetaData(Module* proto, void* opaque) { };
173 void Module::OnDecodeMetaData(int target_type, void* target, const std::string &extname, const std::string &extdata) { };
174 void Module::ProtoSendMetaData(void* opaque, int target_type, void* target, const std::string &extname, const std::string &extdata) { };
175 void Module::OnWallops(userrec* user, const std::string &text) { };
176 void Module::OnChangeHost(userrec* user, const std::string &newhost) { };
177 void Module::OnChangeName(userrec* user, const std::string &gecos) { };
178 void Module::OnAddGLine(long duration, userrec* source, const std::string &reason, const std::string &hostmask) { };
179 void Module::OnAddZLine(long duration, userrec* source, const std::string &reason, const std::string &ipmask) { };
180 void Module::OnAddKLine(long duration, userrec* source, const std::string &reason, const std::string &hostmask) { };
181 void Module::OnAddQLine(long duration, userrec* source, const std::string &reason, const std::string &nickmask) { };
182 void Module::OnAddELine(long duration, userrec* source, const std::string &reason, const std::string &hostmask) { };
183 void Module::OnDelGLine(userrec* source, const std::string &hostmask) { };
184 void Module::OnDelZLine(userrec* source, const std::string &ipmask) { };
185 void Module::OnDelKLine(userrec* source, const std::string &hostmask) { };
186 void Module::OnDelQLine(userrec* source, const std::string &nickmask) { };
187 void Module::OnDelELine(userrec* source, const std::string &hostmask) { };
188 void Module::OnCleanup(int target_type, void* item) { };
189 void Module::Implements(char* Implements) { for (int j = 0; j < 255; j++) Implements[j] = 0; };
190 void Module::OnChannelDelete(chanrec* chan) { };
191 Priority Module::Prioritize() { return PRIORITY_DONTCARE; }
192 void Module::OnSetAway(userrec* user) { };
193 void Module::OnCancelAway(userrec* user) { };
194 int Module::OnUserList(userrec* user, chanrec* Ptr) { return 0; };
197 long InspIRCd::PriorityAfter(const std::string &modulename)
199 for (unsigned int j = 0; j < this->Config->module_names.size(); j++)
201 if (this->Config->module_names[j] == modulename)
203 return ((j << 8) | PRIORITY_AFTER);
206 return PRIORITY_DONTCARE;
209 long InspIRCd::PriorityBefore(const std::string &modulename)
211 for (unsigned int j = 0; j < this->Config->module_names.size(); j++)
213 if (this->Config->module_names[j] == modulename)
215 return ((j << 8) | PRIORITY_BEFORE);
218 return PRIORITY_DONTCARE;
221 bool InspIRCd::PublishFeature(const std::string &FeatureName, Module* Mod)
223 if (Features.find(FeatureName) == Features.end())
225 Features[FeatureName] = Mod;
231 bool InspIRCd::UnpublishFeature(const std::string &FeatureName)
233 featurelist::iterator iter = Features.find(FeatureName);
235 if (iter == Features.end())
238 Features.erase(iter);
242 Module* InspIRCd::FindFeature(const std::string &FeatureName)
244 featurelist::iterator iter = Features.find(FeatureName);
246 if (iter == Features.end())
252 const std::string& InspIRCd::GetModuleName(Module* m)
254 static std::string nothing = ""; /* Prevent compiler warning */
255 for (int i = 0; i <= this->GetModuleCount(); i++)
257 if (this->modules[i] == m)
259 return this->Config->module_names[i];
262 return nothing; /* As above */
265 void InspIRCd::RehashServer()
267 this->WriteOpers("*** Rehashing config file");
268 this->Config->Read(false,NULL);
271 /* This is ugly, yes, but hash_map's arent designed to be
272 * addressed in this manner, and this is a bit of a kludge.
273 * Luckily its a specialist function and rarely used by
274 * many modules (in fact, it was specially created to make
275 * m_safelist possible, initially).
278 chanrec* InspIRCd::GetChannelIndex(long index)
281 for (chan_hash::iterator n = this->chanlist.begin(); n != this->chanlist.end(); n++, target++)
289 bool InspIRCd::MatchText(const std::string &sliteral, const std::string &spattern)
291 return match(sliteral.c_str(),spattern.c_str());
294 bool InspIRCd::CallCommandHandler(const std::string &commandname, const char** parameters, int pcnt, userrec* user)
296 return this->Parser->CallHandler(commandname,parameters,pcnt,user);
299 bool InspIRCd::IsValidModuleCommand(const std::string &commandname, int pcnt, userrec* user)
301 return this->Parser->IsValidCommand(commandname, pcnt, user);
304 void InspIRCd::AddCommand(command_t *f)
306 if (!this->Parser->CreateCommand(f))
308 ModuleException err("Command "+std::string(f->command)+" already exists.");
313 void InspIRCd::SendMode(const char** parameters, int pcnt, userrec *user)
315 this->Modes->Process(parameters,pcnt,user,true);
318 void InspIRCd::DumpText(userrec* User, const std::string &LinePrefix, stringstream &TextStream)
320 std::string CompleteLine = LinePrefix;
321 std::string Word = "";
322 while (TextStream >> Word)
324 if (CompleteLine.length() + Word.length() + 3 > 500)
326 User->WriteServ(CompleteLine);
327 CompleteLine = LinePrefix;
329 CompleteLine = CompleteLine + Word + " ";
331 User->WriteServ(CompleteLine);
334 userrec* InspIRCd::FindDescriptor(int socket)
336 return reinterpret_cast<userrec*>(this->SE->GetRef(socket));
339 bool InspIRCd::AddMode(ModeHandler* mh, const unsigned char mode)
341 return this->Modes->AddMode(mh,mode);
344 bool InspIRCd::AddModeWatcher(ModeWatcher* mw)
346 return this->Modes->AddModeWatcher(mw);
349 bool InspIRCd::DelModeWatcher(ModeWatcher* mw)
351 return this->Modes->DelModeWatcher(mw);
354 bool InspIRCd::AddResolver(Resolver* r)
356 return this->Res->AddResolverClass(r);
359 bool InspIRCd::UserToPseudo(userrec* user, const std::string &message)
361 unsigned int old_fd = user->GetFd();
362 user->Write("ERROR :Closing link (%s@%s) [%s]",user->ident,user->host,message.c_str());
363 user->FlushWriteBuf();
365 user->SetFd(FD_MAGIC_NUMBER);
367 if (find(local_users.begin(),local_users.end(),user) != local_users.end())
369 local_users.erase(find(local_users.begin(),local_users.end(),user));
372 this->SE->DelFd(user);
378 bool InspIRCd::PseudoToUser(userrec* alive, userrec* zombie, const std::string &message)
380 zombie->SetFd(alive->GetFd());
381 FOREACH_MOD_I(this,I_OnUserQuit,OnUserQuit(alive,message));
382 alive->SetFd(FD_MAGIC_NUMBER);
383 alive->FlushWriteBuf();
384 alive->ClearBuffer();
385 // save these for later
386 std::string oldnick = alive->nick;
387 std::string oldhost = alive->host;
388 std::string oldident = alive->ident;
389 userrec::QuitUser(this,alive,message.c_str());
390 if (find(local_users.begin(),local_users.end(),alive) != local_users.end())
392 local_users.erase(find(local_users.begin(),local_users.end(),alive));
394 // Fix by brain - cant write the user until their fd table entry is updated
395 zombie->Write(":%s!%s@%s NICK %s",oldnick.c_str(),oldident.c_str(),oldhost.c_str(),zombie->nick);
396 for (std::vector<ucrec*>::const_iterator i = zombie->chans.begin(); i != zombie->chans.end(); i++)
398 if (((ucrec*)(*i))->channel != NULL)
400 chanrec* Ptr = ((ucrec*)(*i))->channel;
401 zombie->WriteFrom(zombie,"JOIN %s",Ptr->name);
404 zombie->WriteServ("332 %s %s :%s", zombie->nick, Ptr->name, Ptr->topic);
405 zombie->WriteServ("333 %s %s %s %d", zombie->nick, Ptr->name, Ptr->setby, Ptr->topicset);
407 Ptr->UserList(zombie);
408 zombie->WriteServ("366 %s %s :End of /NAMES list.", zombie->nick, Ptr->name);
411 if ((find(local_users.begin(),local_users.end(),zombie) == local_users.end()) && (zombie->GetFd() != FD_MAGIC_NUMBER))
412 local_users.push_back(zombie);
416 void InspIRCd::AddGLine(long duration, const std::string &source, const std::string &reason, const std::string &hostmask)
418 XLines->add_gline(duration, source.c_str(), reason.c_str(), hostmask.c_str());
419 XLines->apply_lines(APPLY_GLINES);
422 void InspIRCd::AddQLine(long duration, const std::string &source, const std::string &reason, const std::string &nickname)
424 XLines->add_qline(duration, source.c_str(), reason.c_str(), nickname.c_str());
425 XLines->apply_lines(APPLY_QLINES);
428 void InspIRCd::AddZLine(long duration, const std::string &source, const std::string &reason, const std::string &ipaddr)
430 XLines->add_zline(duration, source.c_str(), reason.c_str(), ipaddr.c_str());
431 XLines->apply_lines(APPLY_ZLINES);
434 void InspIRCd::AddKLine(long duration, const std::string &source, const std::string &reason, const std::string &hostmask)
436 XLines->add_kline(duration, source.c_str(), reason.c_str(), hostmask.c_str());
437 XLines->apply_lines(APPLY_KLINES);
440 void InspIRCd::AddELine(long duration, const std::string &source, const std::string &reason, const std::string &hostmask)
442 XLines->add_eline(duration, source.c_str(), reason.c_str(), hostmask.c_str());
445 bool InspIRCd::DelGLine(const std::string &hostmask)
447 return XLines->del_gline(hostmask.c_str());
450 bool InspIRCd::DelQLine(const std::string &nickname)
452 return XLines->del_qline(nickname.c_str());
455 bool InspIRCd::DelZLine(const std::string &ipaddr)
457 return XLines->del_zline(ipaddr.c_str());
460 bool InspIRCd::DelKLine(const std::string &hostmask)
462 return XLines->del_kline(hostmask.c_str());
465 bool InspIRCd::DelELine(const std::string &hostmask)
467 return XLines->del_eline(hostmask.c_str());
471 * XXX why on *earth* is this in modules.cpp...? I think
472 * perhaps we need a server.cpp for InspIRCd:: stuff where possible. -- w00t
474 bool InspIRCd::IsValidMask(const std::string &mask)
476 char* dest = (char*)mask.c_str();
477 if (strchr(dest,'!')==0)
479 if (strchr(dest,'@')==0)
481 for (char* i = dest; *i; i++)
484 for (char* i = dest; *i; i++)
488 for (char* i = dest; *i; i++)
494 for (char* i = dest; *i; i++)
503 Module* InspIRCd::FindModule(const std::string &name)
505 for (int i = 0; i <= this->GetModuleCount(); i++)
507 if (this->Config->module_names[i] == name)
509 return this->modules[i];
515 ConfigReader::ConfigReader(InspIRCd* Instance) : ServerInstance(Instance)
517 /* Is there any reason to load the entire config file again here?
518 * it's needed if they specify another config file, but using the
519 * default one we can just use the global config data - pre-parsed!
521 this->errorlog = new std::ostringstream(std::stringstream::in | std::stringstream::out);
523 this->data = &ServerInstance->Config->config_data;
524 this->privatehash = false;
528 ConfigReader::~ConfigReader()
531 DELETE(this->errorlog);
532 if(this->privatehash)
537 ConfigReader::ConfigReader(InspIRCd* Instance, const std::string &filename) : ServerInstance(Instance)
539 ServerInstance->Config->ClearStack();
541 this->data = new ConfigDataHash;
542 this->privatehash = true;
543 this->errorlog = new std::ostringstream(std::stringstream::in | std::stringstream::out);
544 this->readerror = ServerInstance->Config->LoadConf(*this->data, filename, *this->errorlog);
545 if (!this->readerror)
546 this->error = CONF_FILE_NOT_FOUND;
549 std::string ConfigReader::ReadValue(const std::string &tag, const std::string &name, int index)
551 /* Don't need to strlcpy() tag and name anymore, ReadConf() takes const char* */
554 if (!ServerInstance->Config->ConfValue(*this->data, tag, name, index, result))
556 this->error = CONF_VALUE_NOT_FOUND;
563 bool ConfigReader::ReadFlag(const std::string &tag, const std::string &name, int index)
565 return ServerInstance->Config->ConfValueBool(*this->data, tag, name, index);
568 long ConfigReader::ReadInteger(const std::string &tag, const std::string &name, int index, bool needs_unsigned)
572 if(!ServerInstance->Config->ConfValueInteger(*this->data, tag, name, index, result))
574 this->error = CONF_VALUE_NOT_FOUND;
578 if ((needs_unsigned) && (result < 0))
580 this->error = CONF_NOT_UNSIGNED;
587 long ConfigReader::GetError()
589 long olderr = this->error;
594 void ConfigReader::DumpErrors(bool bail, userrec* user)
596 /* XXX - Duplicated code */
600 printf("There were errors in your configuration:\n%s", this->errorlog->str().c_str());
601 InspIRCd::Exit(ERROR);
605 std::string errors = this->errorlog->str();
606 std::string::size_type start;
607 unsigned int prefixlen;
610 /* ":ServerInstance->Config->ServerName NOTICE user->nick :" */
611 prefixlen = strlen(ServerInstance->Config->ServerName) + strlen(user->nick) + 11;
615 user->WriteServ("NOTICE %s :There were errors in the configuration file:",user->nick);
617 while(start < errors.length())
619 user->WriteServ("NOTICE %s :%s",user->nick, errors.substr(start, 510 - prefixlen).c_str());
620 start += 510 - prefixlen;
625 ServerInstance->WriteOpers("There were errors in the configuration file:");
627 while(start < errors.length())
629 ServerInstance->WriteOpers(errors.substr(start, 360).c_str());
639 int ConfigReader::Enumerate(const std::string &tag)
641 return ServerInstance->Config->ConfValueEnum(*this->data, tag);
644 int ConfigReader::EnumerateValues(const std::string &tag, int index)
646 return ServerInstance->Config->ConfVarEnum(*this->data, tag, index);
649 bool ConfigReader::Verify()
651 return this->readerror;
655 FileReader::FileReader(InspIRCd* Instance, const std::string &filename) : ServerInstance(Instance)
658 ServerInstance->Config->ReadFile(c,filename.c_str());
663 FileReader::FileReader(InspIRCd* Instance) : ServerInstance(Instance)
667 std::string FileReader::Contents()
670 for (file_cache::iterator a = this->fc.begin(); a != this->fc.end(); a++)
678 unsigned long FileReader::ContentSize()
680 return this->contentsize;
683 void FileReader::CalcSize()
686 for (file_cache::iterator a = this->fc.begin(); a != this->fc.end(); a++)
687 n += (a->length() + 2);
688 this->contentsize = n;
691 void FileReader::LoadFile(const std::string &filename)
694 ServerInstance->Config->ReadFile(c,filename.c_str());
700 FileReader::~FileReader()
704 bool FileReader::Exists()
706 return (!(fc.size() == 0));
709 std::string FileReader::GetLine(int x)
711 if ((x<0) || ((unsigned)x>fc.size()))
716 int FileReader::FileSize()