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 // version is a simple class for holding a modules version number
50 Version::Version(int major, int minor, int revision, int build, int flags)
51 : Major(major), Minor(minor), Revision(revision), Build(build), Flags(flags)
55 Request::Request(char* anydata, Module* src, Module* dst)
56 : data(anydata), source(src), dest(dst)
58 /* Ensure that because this module doesnt support ID strings, it doesnt break modules that do
59 * by passing them uninitialized pointers (could happen)
64 Request::Request(Module* src, Module* dst, const char* idstr)
65 : id(idstr), source(src), dest(dst)
69 char* Request::GetData()
74 const char* Request::GetId()
79 Module* Request::GetSource()
84 Module* Request::GetDest()
93 return dest->OnRequest(this);
101 Event::Event(char* anydata, Module* src, const std::string &eventid) : data(anydata), source(src), id(eventid) { };
103 char* Event::GetData()
105 return (char*)this->data;
108 Module* Event::GetSource()
113 char* Event::Send(InspIRCd* ServerInstance)
115 FOREACH_MOD(I_OnEvent,OnEvent(this));
119 std::string Event::GetEventID()
125 // These declarations define the behavours of the base class Module (which does nothing at all)
127 Module::Module(InspIRCd* Me) : ServerInstance(Me) { }
128 Module::~Module() { }
129 void Module::OnUserConnect(userrec* user) { }
130 void Module::OnUserQuit(userrec* user, const std::string& message) { }
131 void Module::OnUserDisconnect(userrec* user) { }
132 void Module::OnUserJoin(userrec* user, chanrec* channel) { }
133 void Module::OnUserPart(userrec* user, chanrec* channel, const std::string &partmessage) { }
134 void Module::OnRehash(const std::string ¶meter) { }
135 void Module::OnServerRaw(std::string &raw, bool inbound, userrec* user) { }
136 int Module::OnUserPreJoin(userrec* user, chanrec* chan, const char* cname) { return 0; }
137 void Module::OnMode(userrec* user, void* dest, int target_type, const std::string &text) { };
138 Version Module::GetVersion() { return Version(1,0,0,0,VF_VENDOR); }
139 void Module::OnOper(userrec* user, const std::string &opertype) { };
140 void Module::OnPostOper(userrec* user, const std::string &opertype) { };
141 void Module::OnInfo(userrec* user) { };
142 void Module::OnWhois(userrec* source, userrec* dest) { };
143 int Module::OnUserPreInvite(userrec* source,userrec* dest,chanrec* channel) { return 0; };
144 int Module::OnUserPreMessage(userrec* user,void* dest,int target_type, std::string &text,char status) { return 0; };
145 int Module::OnUserPreNotice(userrec* user,void* dest,int target_type, std::string &text,char status) { return 0; };
146 int Module::OnUserPreNick(userrec* user, const std::string &newnick) { return 0; };
147 void Module::OnUserPostNick(userrec* user, const std::string &oldnick) { };
148 int Module::OnAccessCheck(userrec* source,userrec* dest,chanrec* channel,int access_type) { return ACR_DEFAULT; };
149 void Module::On005Numeric(std::string &output) { };
150 int Module::OnKill(userrec* source, userrec* dest, const std::string &reason) { return 0; };
151 void Module::OnLoadModule(Module* mod,const std::string &name) { };
152 void Module::OnUnloadModule(Module* mod,const std::string &name) { };
153 void Module::OnBackgroundTimer(time_t curtime) { };
154 int Module::OnPreCommand(const std::string &command, const char** parameters, int pcnt, userrec *user, bool validated) { return 0; };
155 bool Module::OnCheckReady(userrec* user) { return true; };
156 void Module::OnUserRegister(userrec* user) { };
157 int Module::OnUserPreKick(userrec* source, userrec* user, chanrec* chan, const std::string &reason) { return 0; };
158 void Module::OnUserKick(userrec* source, userrec* user, chanrec* chan, const std::string &reason) { };
159 int Module::OnRawMode(userrec* user, chanrec* chan, char mode, const std::string ¶m, bool adding, int pcnt) { return 0; };
160 int Module::OnCheckInvite(userrec* user, chanrec* chan) { return 0; };
161 int Module::OnCheckKey(userrec* user, chanrec* chan, const std::string &keygiven) { return 0; };
162 int Module::OnCheckLimit(userrec* user, chanrec* chan) { return 0; };
163 int Module::OnCheckBan(userrec* user, chanrec* chan) { return 0; };
164 int Module::OnStats(char symbol, userrec* user, string_list &results) { return 0; };
165 int Module::OnChangeLocalUserHost(userrec* user, const std::string &newhost) { return 0; };
166 int Module::OnChangeLocalUserGECOS(userrec* user, const std::string &newhost) { return 0; };
167 int Module::OnLocalTopicChange(userrec* user, chanrec* chan, const std::string &topic) { return 0; };
168 void Module::OnEvent(Event* event) { return; };
169 char* Module::OnRequest(Request* request) { return NULL; };
170 int Module::OnOperCompare(const std::string &password, const std::string &input) { return 0; };
171 void Module::OnGlobalOper(userrec* user) { };
172 void Module::OnGlobalConnect(userrec* user) { };
173 int Module::OnAddBan(userrec* source, chanrec* channel,const std::string &banmask) { return 0; };
174 int Module::OnDelBan(userrec* source, chanrec* channel,const std::string &banmask) { return 0; };
175 void Module::OnRawSocketAccept(int fd, const std::string &ip, int localport) { };
176 int Module::OnRawSocketWrite(int fd, const char* buffer, int count) { return 0; };
177 void Module::OnRawSocketClose(int fd) { };
178 int Module::OnRawSocketRead(int fd, char* buffer, unsigned int count, int &readresult) { return 0; };
179 void Module::OnUserMessage(userrec* user, void* dest, int target_type, const std::string &text, char status) { };
180 void Module::OnUserNotice(userrec* user, void* dest, int target_type, const std::string &text, char status) { };
181 void Module::OnRemoteKill(userrec* source, userrec* dest, const std::string &reason) { };
182 void Module::OnUserInvite(userrec* source,userrec* dest,chanrec* channel) { };
183 void Module::OnPostLocalTopicChange(userrec* user, chanrec* chan, const std::string &topic) { };
184 void Module::OnGetServerDescription(const std::string &servername,std::string &description) { };
185 void Module::OnSyncUser(userrec* user, Module* proto, void* opaque) { };
186 void Module::OnSyncChannel(chanrec* chan, Module* proto, void* opaque) { };
187 void Module::ProtoSendMode(void* opaque, int target_type, void* target, const std::string &modeline) { };
188 void Module::OnSyncChannelMetaData(chanrec* chan, Module* proto,void* opaque, const std::string &extname) { };
189 void Module::OnSyncUserMetaData(userrec* user, Module* proto,void* opaque, const std::string &extname) { };
190 void Module::OnSyncOtherMetaData(Module* proto, void* opaque) { };
191 void Module::OnDecodeMetaData(int target_type, void* target, const std::string &extname, const std::string &extdata) { };
192 void Module::ProtoSendMetaData(void* opaque, int target_type, void* target, const std::string &extname, const std::string &extdata) { };
193 void Module::OnWallops(userrec* user, const std::string &text) { };
194 void Module::OnChangeHost(userrec* user, const std::string &newhost) { };
195 void Module::OnChangeName(userrec* user, const std::string &gecos) { };
196 void Module::OnAddGLine(long duration, userrec* source, const std::string &reason, const std::string &hostmask) { };
197 void Module::OnAddZLine(long duration, userrec* source, const std::string &reason, const std::string &ipmask) { };
198 void Module::OnAddKLine(long duration, userrec* source, const std::string &reason, const std::string &hostmask) { };
199 void Module::OnAddQLine(long duration, userrec* source, const std::string &reason, const std::string &nickmask) { };
200 void Module::OnAddELine(long duration, userrec* source, const std::string &reason, const std::string &hostmask) { };
201 void Module::OnDelGLine(userrec* source, const std::string &hostmask) { };
202 void Module::OnDelZLine(userrec* source, const std::string &ipmask) { };
203 void Module::OnDelKLine(userrec* source, const std::string &hostmask) { };
204 void Module::OnDelQLine(userrec* source, const std::string &nickmask) { };
205 void Module::OnDelELine(userrec* source, const std::string &hostmask) { };
206 void Module::OnCleanup(int target_type, void* item) { };
207 void Module::Implements(char* Implements) { for (int j = 0; j < 255; j++) Implements[j] = 0; };
208 void Module::OnChannelDelete(chanrec* chan) { };
209 Priority Module::Prioritize() { return PRIORITY_DONTCARE; }
210 void Module::OnSetAway(userrec* user) { };
211 void Module::OnCancelAway(userrec* user) { };
213 /* server is a wrapper class that provides methods to all of the C-style
214 * exports in the core
217 void InspIRCd::AddSocket(InspSocket* sock)
219 this->module_sockets.push_back(sock);
222 void InspIRCd::RemoveSocket(InspSocket* sock)
224 for (std::vector<InspSocket*>::iterator a = this->module_sockets.begin(); a < this->module_sockets.end(); a++)
226 InspSocket* s = (InspSocket*)*a;
232 long InspIRCd::PriorityAfter(const std::string &modulename)
234 for (unsigned int j = 0; j < this->Config->module_names.size(); j++)
236 if (this->Config->module_names[j] == modulename)
238 return ((j << 8) | PRIORITY_AFTER);
241 return PRIORITY_DONTCARE;
244 long InspIRCd::PriorityBefore(const std::string &modulename)
246 for (unsigned int j = 0; j < this->Config->module_names.size(); j++)
248 if (this->Config->module_names[j] == modulename)
250 return ((j << 8) | PRIORITY_BEFORE);
253 return PRIORITY_DONTCARE;
256 bool InspIRCd::PublishFeature(const std::string &FeatureName, Module* Mod)
258 if (Features.find(FeatureName) == Features.end())
260 Features[FeatureName] = Mod;
266 bool InspIRCd::UnpublishFeature(const std::string &FeatureName)
268 featurelist::iterator iter = Features.find(FeatureName);
270 if (iter == Features.end())
273 Features.erase(iter);
277 Module* InspIRCd::FindFeature(const std::string &FeatureName)
279 featurelist::iterator iter = Features.find(FeatureName);
281 if (iter == Features.end())
287 const std::string& InspIRCd::GetModuleName(Module* m)
289 static std::string nothing = ""; /* Prevent compiler warning */
290 for (int i = 0; i <= this->GetModuleCount(); i++)
292 if (this->modules[i] == m)
294 return this->Config->module_names[i];
297 return nothing; /* As above */
300 void InspIRCd::RehashServer()
302 this->WriteOpers("*** Rehashing config file");
303 this->Config->Read(false,NULL);
306 void InspIRCd::DelSocket(InspSocket* sock)
308 for (std::vector<InspSocket*>::iterator a = this->module_sockets.begin(); a < this->module_sockets.end(); a++)
312 this->module_sockets.erase(a);
318 /* This is ugly, yes, but hash_map's arent designed to be
319 * addressed in this manner, and this is a bit of a kludge.
320 * Luckily its a specialist function and rarely used by
321 * many modules (in fact, it was specially created to make
322 * m_safelist possible, initially).
325 chanrec* InspIRCd::GetChannelIndex(long index)
328 for (chan_hash::iterator n = this->chanlist.begin(); n != this->chanlist.end(); n++, target++)
336 bool InspIRCd::MatchText(const std::string &sliteral, const std::string &spattern)
338 return match(sliteral.c_str(),spattern.c_str());
341 bool InspIRCd::CallCommandHandler(const std::string &commandname, const char** parameters, int pcnt, userrec* user)
343 return this->Parser->CallHandler(commandname,parameters,pcnt,user);
346 bool InspIRCd::IsValidModuleCommand(const std::string &commandname, int pcnt, userrec* user)
348 return this->Parser->IsValidCommand(commandname, pcnt, user);
351 void InspIRCd::AddCommand(command_t *f)
353 if (!this->Parser->CreateCommand(f))
355 ModuleException err("Command "+std::string(f->command)+" already exists.");
360 void InspIRCd::SendMode(const char** parameters, int pcnt, userrec *user)
362 this->Modes->Process(parameters,pcnt,user,true);
365 void InspIRCd::DumpText(userrec* User, const std::string &LinePrefix, stringstream &TextStream)
367 std::string CompleteLine = LinePrefix;
368 std::string Word = "";
369 while (TextStream >> Word)
371 if (CompleteLine.length() + Word.length() + 3 > 500)
373 User->WriteServ(CompleteLine);
374 CompleteLine = LinePrefix;
376 CompleteLine = CompleteLine + Word + " ";
378 User->WriteServ(CompleteLine);
381 userrec* InspIRCd::FindDescriptor(int socket)
383 return ((socket < MAX_DESCRIPTORS && socket > -1) ? this->fd_ref_table[socket] : NULL);
386 bool InspIRCd::AddMode(ModeHandler* mh, const unsigned char mode)
388 return this->Modes->AddMode(mh,mode);
391 bool InspIRCd::AddModeWatcher(ModeWatcher* mw)
393 return this->Modes->AddModeWatcher(mw);
396 bool InspIRCd::DelModeWatcher(ModeWatcher* mw)
398 return this->Modes->DelModeWatcher(mw);
401 bool InspIRCd::AddResolver(Resolver* r)
403 return this->Res->AddResolverClass(r);
406 bool InspIRCd::UserToPseudo(userrec* user, const std::string &message)
408 unsigned int old_fd = user->fd;
409 user->Write("ERROR :Closing link (%s@%s) [%s]",user->ident,user->host,message.c_str());
410 user->FlushWriteBuf();
412 user->fd = FD_MAGIC_NUMBER;
414 if (find(local_users.begin(),local_users.end(),user) != local_users.end())
416 local_users.erase(find(local_users.begin(),local_users.end(),user));
419 this->SE->DelFd(old_fd);
425 bool InspIRCd::PseudoToUser(userrec* alive, userrec* zombie, const std::string &message)
427 zombie->fd = alive->fd;
428 FOREACH_MOD_I(this,I_OnUserQuit,OnUserQuit(alive,message));
429 alive->fd = FD_MAGIC_NUMBER;
430 alive->FlushWriteBuf();
431 alive->ClearBuffer();
432 // save these for later
433 std::string oldnick = alive->nick;
434 std::string oldhost = alive->host;
435 std::string oldident = alive->ident;
436 userrec::QuitUser(this,alive,message.c_str());
437 if (find(local_users.begin(),local_users.end(),alive) != local_users.end())
439 local_users.erase(find(local_users.begin(),local_users.end(),alive));
441 // Fix by brain - cant write the user until their fd table entry is updated
442 this->fd_ref_table[zombie->fd] = zombie;
443 zombie->Write(":%s!%s@%s NICK %s",oldnick.c_str(),oldident.c_str(),oldhost.c_str(),zombie->nick);
444 for (std::vector<ucrec*>::const_iterator i = zombie->chans.begin(); i != zombie->chans.end(); i++)
446 if (((ucrec*)(*i))->channel != NULL)
448 chanrec* Ptr = ((ucrec*)(*i))->channel;
449 zombie->WriteFrom(zombie,"JOIN %s",Ptr->name);
452 zombie->WriteServ("332 %s %s :%s", zombie->nick, Ptr->name, Ptr->topic);
453 zombie->WriteServ("333 %s %s %s %d", zombie->nick, Ptr->name, Ptr->setby, Ptr->topicset);
455 Ptr->UserList(zombie);
456 zombie->WriteServ("366 %s %s :End of /NAMES list.", zombie->nick, Ptr->name);
459 if ((find(local_users.begin(),local_users.end(),zombie) == local_users.end()) && (zombie->fd != FD_MAGIC_NUMBER))
460 local_users.push_back(zombie);
465 void InspIRCd::AddGLine(long duration, const std::string &source, const std::string &reason, const std::string &hostmask)
467 XLines->add_gline(duration, source.c_str(), reason.c_str(), hostmask.c_str());
468 XLines->apply_lines(APPLY_GLINES);
471 void InspIRCd::AddQLine(long duration, const std::string &source, const std::string &reason, const std::string &nickname)
473 XLines->add_qline(duration, source.c_str(), reason.c_str(), nickname.c_str());
474 XLines->apply_lines(APPLY_QLINES);
477 void InspIRCd::AddZLine(long duration, const std::string &source, const std::string &reason, const std::string &ipaddr)
479 XLines->add_zline(duration, source.c_str(), reason.c_str(), ipaddr.c_str());
480 XLines->apply_lines(APPLY_ZLINES);
483 void InspIRCd::AddKLine(long duration, const std::string &source, const std::string &reason, const std::string &hostmask)
485 XLines->add_kline(duration, source.c_str(), reason.c_str(), hostmask.c_str());
486 XLines->apply_lines(APPLY_KLINES);
489 void InspIRCd::AddELine(long duration, const std::string &source, const std::string &reason, const std::string &hostmask)
491 XLines->add_eline(duration, source.c_str(), reason.c_str(), hostmask.c_str());
494 bool InspIRCd::DelGLine(const std::string &hostmask)
496 return XLines->del_gline(hostmask.c_str());
499 bool InspIRCd::DelQLine(const std::string &nickname)
501 return XLines->del_qline(nickname.c_str());
504 bool InspIRCd::DelZLine(const std::string &ipaddr)
506 return XLines->del_zline(ipaddr.c_str());
509 bool InspIRCd::DelKLine(const std::string &hostmask)
511 return XLines->del_kline(hostmask.c_str());
514 bool InspIRCd::DelELine(const std::string &hostmask)
516 return XLines->del_eline(hostmask.c_str());
520 * XXX why on *earth* is this in modules.cpp...? I think
521 * perhaps we need a server.cpp for InspIRCd:: stuff where possible. -- w00t
523 bool InspIRCd::IsValidMask(const std::string &mask)
525 char* dest = (char*)mask.c_str();
526 if (strchr(dest,'!')==0)
528 if (strchr(dest,'@')==0)
530 for (char* i = dest; *i; i++)
533 for (char* i = dest; *i; i++)
537 for (char* i = dest; *i; i++)
543 for (char* i = dest; *i; i++)
552 Module* InspIRCd::FindModule(const std::string &name)
554 for (int i = 0; i <= this->GetModuleCount(); i++)
556 if (this->Config->module_names[i] == name)
558 return this->modules[i];
564 ConfigReader::ConfigReader(InspIRCd* Instance) : ServerInstance(Instance)
566 /* Is there any reason to load the entire config file again here?
567 * it's needed if they specify another config file, but using the
568 * default one we can just use the global config data - pre-parsed!
570 this->errorlog = new std::ostringstream(std::stringstream::in | std::stringstream::out);
572 this->data = &ServerInstance->Config->config_data;
573 this->privatehash = false;
577 ConfigReader::~ConfigReader()
580 DELETE(this->errorlog);
581 if(this->privatehash)
586 ConfigReader::ConfigReader(InspIRCd* Instance, const std::string &filename) : ServerInstance(Instance)
588 ServerInstance->Config->ClearStack();
590 this->data = new ConfigDataHash;
591 this->privatehash = true;
592 this->errorlog = new std::ostringstream(std::stringstream::in | std::stringstream::out);
593 this->readerror = ServerInstance->Config->LoadConf(*this->data, filename, *this->errorlog);
594 if (!this->readerror)
595 this->error = CONF_FILE_NOT_FOUND;
598 std::string ConfigReader::ReadValue(const std::string &tag, const std::string &name, int index)
600 /* Don't need to strlcpy() tag and name anymore, ReadConf() takes const char* */
603 if (!ServerInstance->Config->ConfValue(*this->data, tag, name, index, result))
605 this->error = CONF_VALUE_NOT_FOUND;
612 bool ConfigReader::ReadFlag(const std::string &tag, const std::string &name, int index)
614 return ServerInstance->Config->ConfValueBool(*this->data, tag, name, index);
617 long ConfigReader::ReadInteger(const std::string &tag, const std::string &name, int index, bool needs_unsigned)
621 if(!ServerInstance->Config->ConfValueInteger(*this->data, tag, name, index, result))
623 this->error = CONF_VALUE_NOT_FOUND;
627 if ((needs_unsigned) && (result < 0))
629 this->error = CONF_NOT_UNSIGNED;
636 long ConfigReader::GetError()
638 long olderr = this->error;
643 void ConfigReader::DumpErrors(bool bail, userrec* user)
645 /* XXX - Duplicated code */
649 printf("There were errors in your configuration:\n%s", this->errorlog->str().c_str());
650 InspIRCd::Exit(ERROR);
654 std::string errors = this->errorlog->str();
655 std::string::size_type start;
656 unsigned int prefixlen;
659 /* ":ServerInstance->Config->ServerName NOTICE user->nick :" */
660 prefixlen = strlen(ServerInstance->Config->ServerName) + strlen(user->nick) + 11;
664 user->WriteServ("NOTICE %s :There were errors in the configuration file:",user->nick);
666 while(start < errors.length())
668 user->WriteServ("NOTICE %s :%s",user->nick, errors.substr(start, 510 - prefixlen).c_str());
669 start += 510 - prefixlen;
674 ServerInstance->WriteOpers("There were errors in the configuration file:");
676 while(start < errors.length())
678 ServerInstance->WriteOpers(errors.substr(start, 360).c_str());
688 int ConfigReader::Enumerate(const std::string &tag)
690 return ServerInstance->Config->ConfValueEnum(*this->data, tag);
693 int ConfigReader::EnumerateValues(const std::string &tag, int index)
695 return ServerInstance->Config->ConfVarEnum(*this->data, tag, index);
698 bool ConfigReader::Verify()
700 return this->readerror;
704 FileReader::FileReader(InspIRCd* Instance, const std::string &filename) : ServerInstance(Instance)
707 ServerInstance->Config->ReadFile(c,filename.c_str());
712 FileReader::FileReader(InspIRCd* Instance) : ServerInstance(Instance)
716 std::string FileReader::Contents()
719 for (file_cache::iterator a = this->fc.begin(); a != this->fc.end(); a++)
727 unsigned long FileReader::ContentSize()
729 return this->contentsize;
732 void FileReader::CalcSize()
735 for (file_cache::iterator a = this->fc.begin(); a != this->fc.end(); a++)
736 n += (a->length() + 2);
737 this->contentsize = n;
740 void FileReader::LoadFile(const std::string &filename)
743 ServerInstance->Config->ReadFile(c,filename.c_str());
749 FileReader::~FileReader()
753 bool FileReader::Exists()
755 return (!(fc.size() == 0));
758 std::string FileReader::GetLine(int x)
760 if ((x<0) || ((unsigned)x>fc.size()))
765 int FileReader::FileSize()