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"
49 extern command_table cmdlist;
53 // version is a simple class for holding a modules version number
55 Version::Version(int major, int minor, int revision, int build, int flags)
56 : Major(major), Minor(minor), Revision(revision), Build(build), Flags(flags)
60 Request::Request(char* anydata, Module* src, Module* dst)
61 : data(anydata), source(src), dest(dst)
63 /* Ensure that because this module doesnt support ID strings, it doesnt break modules that do
64 * by passing them uninitialized pointers (could happen)
69 Request::Request(Module* src, Module* dst, const char* idstr)
70 : id(idstr), source(src), dest(dst)
74 char* Request::GetData()
79 const char* Request::GetId()
84 Module* Request::GetSource()
89 Module* Request::GetDest()
98 return dest->OnRequest(this);
106 Event::Event(char* anydata, Module* src, const std::string &eventid) : data(anydata), source(src), id(eventid) { };
108 char* Event::GetData()
110 return (char*)this->data;
113 Module* Event::GetSource()
118 char* Event::Send(InspIRCd* ServerInstance)
120 FOREACH_MOD(I_OnEvent,OnEvent(this));
124 std::string Event::GetEventID()
130 // These declarations define the behavours of the base class Module (which does nothing at all)
132 Module::Module(InspIRCd* Me) : ServerInstance(Me) { }
133 Module::~Module() { }
134 void Module::OnUserConnect(userrec* user) { }
135 void Module::OnUserQuit(userrec* user, const std::string& message) { }
136 void Module::OnUserDisconnect(userrec* user) { }
137 void Module::OnUserJoin(userrec* user, chanrec* channel) { }
138 void Module::OnUserPart(userrec* user, chanrec* channel, const std::string &partmessage) { }
139 void Module::OnRehash(const std::string ¶meter) { }
140 void Module::OnServerRaw(std::string &raw, bool inbound, userrec* user) { }
141 int Module::OnUserPreJoin(userrec* user, chanrec* chan, const char* cname) { return 0; }
142 void Module::OnMode(userrec* user, void* dest, int target_type, const std::string &text) { };
143 Version Module::GetVersion() { return Version(1,0,0,0,VF_VENDOR); }
144 void Module::OnOper(userrec* user, const std::string &opertype) { };
145 void Module::OnPostOper(userrec* user, const std::string &opertype) { };
146 void Module::OnInfo(userrec* user) { };
147 void Module::OnWhois(userrec* source, userrec* dest) { };
148 int Module::OnUserPreInvite(userrec* source,userrec* dest,chanrec* channel) { return 0; };
149 int Module::OnUserPreMessage(userrec* user,void* dest,int target_type, std::string &text,char status) { return 0; };
150 int Module::OnUserPreNotice(userrec* user,void* dest,int target_type, std::string &text,char status) { return 0; };
151 int Module::OnUserPreNick(userrec* user, const std::string &newnick) { return 0; };
152 void Module::OnUserPostNick(userrec* user, const std::string &oldnick) { };
153 int Module::OnAccessCheck(userrec* source,userrec* dest,chanrec* channel,int access_type) { return ACR_DEFAULT; };
154 void Module::On005Numeric(std::string &output) { };
155 int Module::OnKill(userrec* source, userrec* dest, const std::string &reason) { return 0; };
156 void Module::OnLoadModule(Module* mod,const std::string &name) { };
157 void Module::OnUnloadModule(Module* mod,const std::string &name) { };
158 void Module::OnBackgroundTimer(time_t curtime) { };
159 int Module::OnPreCommand(const std::string &command, const char** parameters, int pcnt, userrec *user, bool validated) { return 0; };
160 bool Module::OnCheckReady(userrec* user) { return true; };
161 void Module::OnUserRegister(userrec* user) { };
162 int Module::OnUserPreKick(userrec* source, userrec* user, chanrec* chan, const std::string &reason) { return 0; };
163 void Module::OnUserKick(userrec* source, userrec* user, chanrec* chan, const std::string &reason) { };
164 int Module::OnRawMode(userrec* user, chanrec* chan, char mode, const std::string ¶m, bool adding, int pcnt) { return 0; };
165 int Module::OnCheckInvite(userrec* user, chanrec* chan) { return 0; };
166 int Module::OnCheckKey(userrec* user, chanrec* chan, const std::string &keygiven) { return 0; };
167 int Module::OnCheckLimit(userrec* user, chanrec* chan) { return 0; };
168 int Module::OnCheckBan(userrec* user, chanrec* chan) { return 0; };
169 int Module::OnStats(char symbol, userrec* user, string_list &results) { return 0; };
170 int Module::OnChangeLocalUserHost(userrec* user, const std::string &newhost) { return 0; };
171 int Module::OnChangeLocalUserGECOS(userrec* user, const std::string &newhost) { return 0; };
172 int Module::OnLocalTopicChange(userrec* user, chanrec* chan, const std::string &topic) { return 0; };
173 void Module::OnEvent(Event* event) { return; };
174 char* Module::OnRequest(Request* request) { return NULL; };
175 int Module::OnOperCompare(const std::string &password, const std::string &input) { return 0; };
176 void Module::OnGlobalOper(userrec* user) { };
177 void Module::OnGlobalConnect(userrec* user) { };
178 int Module::OnAddBan(userrec* source, chanrec* channel,const std::string &banmask) { return 0; };
179 int Module::OnDelBan(userrec* source, chanrec* channel,const std::string &banmask) { return 0; };
180 void Module::OnRawSocketAccept(int fd, const std::string &ip, int localport) { };
181 int Module::OnRawSocketWrite(int fd, const char* buffer, int count) { return 0; };
182 void Module::OnRawSocketClose(int fd) { };
183 int Module::OnRawSocketRead(int fd, char* buffer, unsigned int count, int &readresult) { return 0; };
184 void Module::OnUserMessage(userrec* user, void* dest, int target_type, const std::string &text, char status) { };
185 void Module::OnUserNotice(userrec* user, void* dest, int target_type, const std::string &text, char status) { };
186 void Module::OnRemoteKill(userrec* source, userrec* dest, const std::string &reason) { };
187 void Module::OnUserInvite(userrec* source,userrec* dest,chanrec* channel) { };
188 void Module::OnPostLocalTopicChange(userrec* user, chanrec* chan, const std::string &topic) { };
189 void Module::OnGetServerDescription(const std::string &servername,std::string &description) { };
190 void Module::OnSyncUser(userrec* user, Module* proto, void* opaque) { };
191 void Module::OnSyncChannel(chanrec* chan, Module* proto, void* opaque) { };
192 void Module::ProtoSendMode(void* opaque, int target_type, void* target, const std::string &modeline) { };
193 void Module::OnSyncChannelMetaData(chanrec* chan, Module* proto,void* opaque, const std::string &extname) { };
194 void Module::OnSyncUserMetaData(userrec* user, Module* proto,void* opaque, const std::string &extname) { };
195 void Module::OnSyncOtherMetaData(Module* proto, void* opaque) { };
196 void Module::OnDecodeMetaData(int target_type, void* target, const std::string &extname, const std::string &extdata) { };
197 void Module::ProtoSendMetaData(void* opaque, int target_type, void* target, const std::string &extname, const std::string &extdata) { };
198 void Module::OnWallops(userrec* user, const std::string &text) { };
199 void Module::OnChangeHost(userrec* user, const std::string &newhost) { };
200 void Module::OnChangeName(userrec* user, const std::string &gecos) { };
201 void Module::OnAddGLine(long duration, userrec* source, const std::string &reason, const std::string &hostmask) { };
202 void Module::OnAddZLine(long duration, userrec* source, const std::string &reason, const std::string &ipmask) { };
203 void Module::OnAddKLine(long duration, userrec* source, const std::string &reason, const std::string &hostmask) { };
204 void Module::OnAddQLine(long duration, userrec* source, const std::string &reason, const std::string &nickmask) { };
205 void Module::OnAddELine(long duration, userrec* source, const std::string &reason, const std::string &hostmask) { };
206 void Module::OnDelGLine(userrec* source, const std::string &hostmask) { };
207 void Module::OnDelZLine(userrec* source, const std::string &ipmask) { };
208 void Module::OnDelKLine(userrec* source, const std::string &hostmask) { };
209 void Module::OnDelQLine(userrec* source, const std::string &nickmask) { };
210 void Module::OnDelELine(userrec* source, const std::string &hostmask) { };
211 void Module::OnCleanup(int target_type, void* item) { };
212 void Module::Implements(char* Implements) { for (int j = 0; j < 255; j++) Implements[j] = 0; };
213 void Module::OnChannelDelete(chanrec* chan) { };
214 Priority Module::Prioritize() { return PRIORITY_DONTCARE; }
215 void Module::OnSetAway(userrec* user) { };
216 void Module::OnCancelAway(userrec* user) { };
218 /* server is a wrapper class that provides methods to all of the C-style
219 * exports in the core
222 void InspIRCd::AddSocket(InspSocket* sock)
224 this->module_sockets.push_back(sock);
227 void InspIRCd::RemoveSocket(InspSocket* sock)
229 for (std::vector<InspSocket*>::iterator a = this->module_sockets.begin(); a < this->module_sockets.end(); a++)
231 InspSocket* s = (InspSocket*)*a;
237 long InspIRCd::PriorityAfter(const std::string &modulename)
239 for (unsigned int j = 0; j < this->Config->module_names.size(); j++)
241 if (this->Config->module_names[j] == modulename)
243 return ((j << 8) | PRIORITY_AFTER);
246 return PRIORITY_DONTCARE;
249 long InspIRCd::PriorityBefore(const std::string &modulename)
251 for (unsigned int j = 0; j < this->Config->module_names.size(); j++)
253 if (this->Config->module_names[j] == modulename)
255 return ((j << 8) | PRIORITY_BEFORE);
258 return PRIORITY_DONTCARE;
261 bool InspIRCd::PublishFeature(const std::string &FeatureName, Module* Mod)
263 if (Features.find(FeatureName) == Features.end())
265 Features[FeatureName] = Mod;
271 bool InspIRCd::UnpublishFeature(const std::string &FeatureName)
273 featurelist::iterator iter = Features.find(FeatureName);
275 if (iter == Features.end())
278 Features.erase(iter);
282 Module* InspIRCd::FindFeature(const std::string &FeatureName)
284 featurelist::iterator iter = Features.find(FeatureName);
286 if (iter == Features.end())
292 const std::string& InspIRCd::GetModuleName(Module* m)
294 static std::string nothing = ""; /* Prevent compiler warning */
295 for (int i = 0; i <= this->GetModuleCount(); i++)
297 if (this->modules[i] == m)
299 return this->Config->module_names[i];
302 return nothing; /* As above */
305 void InspIRCd::RehashServer()
307 this->WriteOpers("*** Rehashing config file");
308 this->Config->Read(false,NULL);
311 void InspIRCd::DelSocket(InspSocket* sock)
313 for (std::vector<InspSocket*>::iterator a = this->module_sockets.begin(); a < this->module_sockets.end(); a++)
317 this->module_sockets.erase(a);
323 /* This is ugly, yes, but hash_map's arent designed to be
324 * addressed in this manner, and this is a bit of a kludge.
325 * Luckily its a specialist function and rarely used by
326 * many modules (in fact, it was specially created to make
327 * m_safelist possible, initially).
330 chanrec* InspIRCd::GetChannelIndex(long index)
333 for (chan_hash::iterator n = this->chanlist.begin(); n != this->chanlist.end(); n++, target++)
341 bool InspIRCd::MatchText(const std::string &sliteral, const std::string &spattern)
343 return match(sliteral.c_str(),spattern.c_str());
346 bool InspIRCd::IsUlined(const std::string &server)
348 return is_uline(server.c_str());
351 bool InspIRCd::CallCommandHandler(const std::string &commandname, const char** parameters, int pcnt, userrec* user)
353 return this->Parser->CallHandler(commandname,parameters,pcnt,user);
356 bool InspIRCd::IsValidModuleCommand(const std::string &commandname, int pcnt, userrec* user)
358 return this->Parser->IsValidCommand(commandname, pcnt, user);
361 void InspIRCd::AddCommand(command_t *f)
363 if (!this->Parser->CreateCommand(f))
365 ModuleException err("Command "+std::string(f->command)+" already exists.");
370 void InspIRCd::SendMode(const char** parameters, int pcnt, userrec *user)
372 this->ModeGrok->Process(parameters,pcnt,user,true);
375 void InspIRCd::DumpText(userrec* User, const std::string &LinePrefix, stringstream &TextStream)
377 std::string CompleteLine = LinePrefix;
378 std::string Word = "";
379 while (TextStream >> Word)
381 if (CompleteLine.length() + Word.length() + 3 > 500)
383 User->WriteServ(CompleteLine);
384 CompleteLine = LinePrefix;
386 CompleteLine = CompleteLine + Word + " ";
388 User->WriteServ(CompleteLine);
391 userrec* InspIRCd::FindDescriptor(int socket)
393 return ((socket < MAX_DESCRIPTORS && socket > -1) ? this->fd_ref_table[socket] : NULL);
396 bool InspIRCd::AddMode(ModeHandler* mh, const unsigned char mode)
398 return this->ModeGrok->AddMode(mh,mode);
401 bool InspIRCd::AddModeWatcher(ModeWatcher* mw)
403 return this->ModeGrok->AddModeWatcher(mw);
406 bool InspIRCd::DelModeWatcher(ModeWatcher* mw)
408 return this->ModeGrok->DelModeWatcher(mw);
411 bool InspIRCd::AddResolver(Resolver* r)
413 return this->Res->AddResolverClass(r);
416 bool InspIRCd::UserToPseudo(userrec* user, const std::string &message)
418 unsigned int old_fd = user->fd;
419 user->Write("ERROR :Closing link (%s@%s) [%s]",user->ident,user->host,message.c_str());
420 user->FlushWriteBuf();
422 user->fd = FD_MAGIC_NUMBER;
424 if (find(local_users.begin(),local_users.end(),user) != local_users.end())
426 local_users.erase(find(local_users.begin(),local_users.end(),user));
427 log(DEBUG,"Delete local user");
430 this->SE->DelFd(old_fd);
436 bool InspIRCd::PseudoToUser(userrec* alive, userrec* zombie, const std::string &message)
438 log(DEBUG,"PseudoToUser");
439 zombie->fd = alive->fd;
440 FOREACH_MOD_I(this,I_OnUserQuit,OnUserQuit(alive,message));
441 alive->fd = FD_MAGIC_NUMBER;
442 alive->FlushWriteBuf();
443 alive->ClearBuffer();
444 // save these for later
445 std::string oldnick = alive->nick;
446 std::string oldhost = alive->host;
447 std::string oldident = alive->ident;
448 userrec::QuitUser(this,alive,message.c_str());
449 if (find(local_users.begin(),local_users.end(),alive) != local_users.end())
451 local_users.erase(find(local_users.begin(),local_users.end(),alive));
452 log(DEBUG,"Delete local user");
454 // Fix by brain - cant write the user until their fd table entry is updated
455 this->fd_ref_table[zombie->fd] = zombie;
456 zombie->Write(":%s!%s@%s NICK %s",oldnick.c_str(),oldident.c_str(),oldhost.c_str(),zombie->nick);
457 for (std::vector<ucrec*>::const_iterator i = zombie->chans.begin(); i != zombie->chans.end(); i++)
459 if (((ucrec*)(*i))->channel != NULL)
461 chanrec* Ptr = ((ucrec*)(*i))->channel;
462 zombie->WriteFrom(zombie,"JOIN %s",Ptr->name);
465 zombie->WriteServ("332 %s %s :%s", zombie->nick, Ptr->name, Ptr->topic);
466 zombie->WriteServ("333 %s %s %s %d", zombie->nick, Ptr->name, Ptr->setby, Ptr->topicset);
468 Ptr->UserList(zombie);
469 zombie->WriteServ("366 %s %s :End of /NAMES list.", zombie->nick, Ptr->name);
472 if ((find(local_users.begin(),local_users.end(),zombie) == local_users.end()) && (zombie->fd != FD_MAGIC_NUMBER))
473 local_users.push_back(zombie);
478 void InspIRCd::AddGLine(long duration, const std::string &source, const std::string &reason, const std::string &hostmask)
480 add_gline(duration, source.c_str(), reason.c_str(), hostmask.c_str());
481 apply_lines(APPLY_GLINES);
484 void InspIRCd::AddQLine(long duration, const std::string &source, const std::string &reason, const std::string &nickname)
486 add_qline(duration, source.c_str(), reason.c_str(), nickname.c_str());
487 apply_lines(APPLY_QLINES);
490 void InspIRCd::AddZLine(long duration, const std::string &source, const std::string &reason, const std::string &ipaddr)
492 add_zline(duration, source.c_str(), reason.c_str(), ipaddr.c_str());
493 apply_lines(APPLY_ZLINES);
496 void InspIRCd::AddKLine(long duration, const std::string &source, const std::string &reason, const std::string &hostmask)
498 add_kline(duration, source.c_str(), reason.c_str(), hostmask.c_str());
499 apply_lines(APPLY_KLINES);
502 void InspIRCd::AddELine(long duration, const std::string &source, const std::string &reason, const std::string &hostmask)
504 add_eline(duration, source.c_str(), reason.c_str(), hostmask.c_str());
507 bool InspIRCd::DelGLine(const std::string &hostmask)
509 return del_gline(hostmask.c_str());
512 bool InspIRCd::DelQLine(const std::string &nickname)
514 return del_qline(nickname.c_str());
517 bool InspIRCd::DelZLine(const std::string &ipaddr)
519 return del_zline(ipaddr.c_str());
522 bool InspIRCd::DelKLine(const std::string &hostmask)
524 return del_kline(hostmask.c_str());
527 bool InspIRCd::DelELine(const std::string &hostmask)
529 return del_eline(hostmask.c_str());
532 long InspIRCd::CalcDuration(const std::string &delta)
534 return duration(delta.c_str());
538 * XXX why on *earth* is this in modules.cpp...? I think
539 * perhaps we need a server.cpp for InspIRCd:: stuff where possible. -- w00t
541 bool InspIRCd::IsValidMask(const std::string &mask)
543 char* dest = (char*)mask.c_str();
544 if (strchr(dest,'!')==0)
546 if (strchr(dest,'@')==0)
548 for (char* i = dest; *i; i++)
551 for (char* i = dest; *i; i++)
555 for (char* i = dest; *i; i++)
561 for (char* i = dest; *i; i++)
570 Module* InspIRCd::FindModule(const std::string &name)
572 for (int i = 0; i <= this->GetModuleCount(); i++)
574 if (this->Config->module_names[i] == name)
576 return this->modules[i];
582 ConfigReader::ConfigReader(InspIRCd* Instance) : ServerInstance(Instance)
584 /* Is there any reason to load the entire config file again here?
585 * it's needed if they specify another config file, but using the
586 * default one we can just use the global config data - pre-parsed!
588 this->errorlog = new std::ostringstream(std::stringstream::in | std::stringstream::out);
590 this->data = &ServerInstance->Config->config_data;
591 this->privatehash = false;
595 ConfigReader::~ConfigReader()
598 DELETE(this->errorlog);
599 if(this->privatehash)
604 ConfigReader::ConfigReader(InspIRCd* Instance, const std::string &filename) : ServerInstance(Instance)
606 ServerInstance->Config->ClearStack();
608 this->data = new ConfigDataHash;
609 this->privatehash = true;
610 this->errorlog = new std::ostringstream(std::stringstream::in | std::stringstream::out);
611 this->readerror = ServerInstance->Config->LoadConf(*this->data, filename, *this->errorlog);
612 if (!this->readerror)
613 this->error = CONF_FILE_NOT_FOUND;
616 std::string ConfigReader::ReadValue(const std::string &tag, const std::string &name, int index)
618 /* Don't need to strlcpy() tag and name anymore, ReadConf() takes const char* */
621 if (!ServerInstance->Config->ConfValue(*this->data, tag, name, index, result))
623 this->error = CONF_VALUE_NOT_FOUND;
630 bool ConfigReader::ReadFlag(const std::string &tag, const std::string &name, int index)
632 return ServerInstance->Config->ConfValueBool(*this->data, tag, name, index);
635 long ConfigReader::ReadInteger(const std::string &tag, const std::string &name, int index, bool needs_unsigned)
639 if(!ServerInstance->Config->ConfValueInteger(*this->data, tag, name, index, result))
641 this->error = CONF_VALUE_NOT_FOUND;
645 if ((needs_unsigned) && (result < 0))
647 this->error = CONF_NOT_UNSIGNED;
654 long ConfigReader::GetError()
656 long olderr = this->error;
661 void ConfigReader::DumpErrors(bool bail, userrec* user)
663 /* XXX - Duplicated code */
667 printf("There were errors in your configuration:\n%s", this->errorlog->str().c_str());
668 InspIRCd::Exit(ERROR);
672 std::string errors = this->errorlog->str();
673 std::string::size_type start;
674 unsigned int prefixlen;
677 /* ":ServerInstance->Config->ServerName NOTICE user->nick :" */
678 prefixlen = strlen(ServerInstance->Config->ServerName) + strlen(user->nick) + 11;
682 user->WriteServ("NOTICE %s :There were errors in the configuration file:",user->nick);
684 while(start < errors.length())
686 user->WriteServ("NOTICE %s :%s",user->nick, errors.substr(start, 510 - prefixlen).c_str());
687 start += 510 - prefixlen;
692 ServerInstance->WriteOpers("There were errors in the configuration file:");
694 while(start < errors.length())
696 ServerInstance->WriteOpers(errors.substr(start, 360).c_str());
706 int ConfigReader::Enumerate(const std::string &tag)
708 return ServerInstance->Config->ConfValueEnum(*this->data, tag);
711 int ConfigReader::EnumerateValues(const std::string &tag, int index)
713 return ServerInstance->Config->ConfVarEnum(*this->data, tag, index);
716 bool ConfigReader::Verify()
718 return this->readerror;
722 FileReader::FileReader(InspIRCd* Instance, const std::string &filename) : ServerInstance(Instance)
725 ServerInstance->Config->ReadFile(c,filename.c_str());
730 FileReader::FileReader(InspIRCd* Instance) : ServerInstance(Instance)
734 std::string FileReader::Contents()
737 for (file_cache::iterator a = this->fc.begin(); a != this->fc.end(); a++)
745 unsigned long FileReader::ContentSize()
747 return this->contentsize;
750 void FileReader::CalcSize()
753 for (file_cache::iterator a = this->fc.begin(); a != this->fc.end(); a++)
754 n += (a->length() + 2);
755 this->contentsize = n;
758 void FileReader::LoadFile(const std::string &filename)
761 ServerInstance->Config->ReadFile(c,filename.c_str());
767 FileReader::~FileReader()
771 bool FileReader::Exists()
773 return (!(fc.size() == 0));
776 std::string FileReader::GetLine(int x)
778 if ((x<0) || ((unsigned)x>fc.size()))
783 int FileReader::FileSize()