1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2010 InspIRCd Development Team
6 * See: http://wiki.inspircd.org/Credits
8 * This program is free but copyrighted software; see
9 * the file COPYING for details.
11 * ---------------------------------------------------
17 #include "socketengine.h"
18 #include "command_parse.h"
20 #include "exitcodes.h"
27 // Version is a simple class for holding a modules version number
29 VersionBase<API_VERSION>::VersionBase(const std::string &modv, int flags)
30 : description(modv), Flags(flags)
35 bool VersionBase<API_VERSION>::CanLink(const std::string& other_data)
37 return link_data == other_data;
40 Request::Request(Module* src, Module* dst, const char* idstr)
41 : id(idstr), source(src), dest(dst)
48 dest->OnRequest(*this);
51 Event::Event(Module* src, const std::string &eventid) : source(src), id(eventid) { }
55 FOREACH_MOD(I_OnEvent,OnEvent(*this));
58 // These declarations define the behavours of the base class Module (which does nothing at all)
61 CullResult Module::cull()
63 return classbase::cull();
69 ModResult Module::OnSendSnotice(char &snomask, std::string &type, const std::string &message) { return MOD_RES_PASSTHRU; }
70 void Module::OnUserConnect(LocalUser*) { }
71 void Module::OnUserQuit(User*, const std::string&, const std::string&) { }
72 void Module::OnUserDisconnect(LocalUser*) { }
73 void Module::OnUserJoin(Membership*, bool, bool, CUList&) { }
74 void Module::OnPostJoin(Membership*) { }
75 void Module::OnUserPart(Membership*, std::string&, CUList&) { }
76 void Module::OnPreRehash(User*, const std::string&) { }
77 void Module::OnModuleRehash(User*, const std::string&) { }
78 void Module::OnRehash(User*) { }
79 ModResult Module::OnUserPreJoin(User*, Channel*, const char*, std::string&, const std::string&) { return MOD_RES_PASSTHRU; }
80 void Module::OnMode(User*, void*, int, const std::vector<std::string>&, const std::vector<TranslateType>&) { }
81 void Module::OnOper(User*, const std::string&) { }
82 void Module::OnPostOper(User*, const std::string&, const std::string &) { }
83 void Module::OnInfo(User*) { }
84 void Module::OnWhois(User*, User*) { }
85 ModResult Module::OnUserPreInvite(User*, User*, Channel*, time_t) { return MOD_RES_PASSTHRU; }
86 ModResult Module::OnUserPreMessage(User*, void*, int, std::string&, char, CUList&) { return MOD_RES_PASSTHRU; }
87 ModResult Module::OnUserPreNotice(User*, void*, int, std::string&, char, CUList&) { return MOD_RES_PASSTHRU; }
88 ModResult Module::OnUserPreNick(User*, const std::string&) { return MOD_RES_PASSTHRU; }
89 void Module::OnUserPostNick(User*, const std::string&) { }
90 ModResult Module::OnPreMode(User*, User*, Channel*, const std::vector<std::string>&) { return MOD_RES_PASSTHRU; }
91 void Module::On005Numeric(std::string&) { }
92 ModResult Module::OnKill(User*, User*, const std::string&) { return MOD_RES_PASSTHRU; }
93 void Module::OnLoadModule(Module*) { }
94 void Module::OnUnloadModule(Module*) { }
95 void Module::OnBackgroundTimer(time_t) { }
96 ModResult Module::OnPreCommand(std::string&, std::vector<std::string>&, User *, bool, const std::string&) { return MOD_RES_PASSTHRU; }
97 void Module::OnPostCommand(const std::string&, const std::vector<std::string>&, User *, CmdResult, const std::string&) { }
98 ModResult Module::OnCheckReady(LocalUser*) { return MOD_RES_PASSTHRU; }
99 ModResult Module::OnUserRegister(LocalUser*) { return MOD_RES_PASSTHRU; }
100 ModResult Module::OnUserPreKick(User*, Membership*, const std::string&) { return MOD_RES_PASSTHRU; }
101 void Module::OnUserKick(User*, Membership*, const std::string&, CUList&) { }
102 ModResult Module::OnRawMode(User*, Channel*, const char, const std::string &, bool, int) { return MOD_RES_PASSTHRU; }
103 ModResult Module::OnCheckInvite(User*, Channel*) { return MOD_RES_PASSTHRU; }
104 ModResult Module::OnCheckKey(User*, Channel*, const std::string&) { return MOD_RES_PASSTHRU; }
105 ModResult Module::OnCheckLimit(User*, Channel*) { return MOD_RES_PASSTHRU; }
106 ModResult Module::OnCheckChannelBan(User*, Channel*) { return MOD_RES_PASSTHRU; }
107 ModResult Module::OnCheckBan(User*, Channel*, const std::string&) { return MOD_RES_PASSTHRU; }
108 ModResult Module::OnExtBanCheck(User*, Channel*, char) { return MOD_RES_PASSTHRU; }
109 ModResult Module::OnStats(char, User*, string_list&) { return MOD_RES_PASSTHRU; }
110 ModResult Module::OnChangeLocalUserHost(LocalUser*, const std::string&) { return MOD_RES_PASSTHRU; }
111 ModResult Module::OnChangeLocalUserGECOS(LocalUser*, const std::string&) { return MOD_RES_PASSTHRU; }
112 ModResult Module::OnPreTopicChange(User*, Channel*, const std::string&) { return MOD_RES_PASSTHRU; }
113 void Module::OnEvent(Event&) { }
114 void Module::OnRequest(Request&) { }
115 ModResult Module::OnPassCompare(Extensible* ex, const std::string &password, const std::string &input, const std::string& hashtype) { return MOD_RES_PASSTHRU; }
116 void Module::OnGlobalOper(User*) { }
117 void Module::OnPostConnect(User*) { }
118 ModResult Module::OnAddBan(User*, Channel*, const std::string &) { return MOD_RES_PASSTHRU; }
119 ModResult Module::OnDelBan(User*, Channel*, const std::string &) { return MOD_RES_PASSTHRU; }
120 void Module::OnStreamSocketAccept(StreamSocket*, irc::sockets::sockaddrs*, irc::sockets::sockaddrs*) { }
121 int Module::OnStreamSocketWrite(StreamSocket*, std::string&) { return -1; }
122 void Module::OnStreamSocketClose(StreamSocket*) { }
123 void Module::OnStreamSocketConnect(StreamSocket*) { }
124 int Module::OnStreamSocketRead(StreamSocket*, std::string&) { return -1; }
125 void Module::OnUserMessage(User*, void*, int, const std::string&, char, const CUList&) { }
126 void Module::OnUserNotice(User*, void*, int, const std::string&, char, const CUList&) { }
127 void Module::OnRemoteKill(User*, User*, const std::string&, const std::string&) { }
128 void Module::OnUserInvite(User*, User*, Channel*, time_t) { }
129 void Module::OnPostTopicChange(User*, Channel*, const std::string&) { }
130 void Module::OnGetServerDescription(const std::string&, std::string&) { }
131 void Module::OnSyncUser(User*, Module*, void*) { }
132 void Module::OnSyncChannel(Channel*, Module*, void*) { }
133 void Module::OnSyncNetwork(Module*, void*) { }
134 void Module::ProtoSendMode(void*, TargetTypeFlags, void*, const std::vector<std::string>&, const std::vector<TranslateType>&) { }
135 void Module::OnDecodeMetaData(Extensible*, const std::string&, const std::string&) { }
136 void Module::ProtoSendMetaData(void*, Extensible*, const std::string&, const std::string&) { }
137 void Module::OnWallops(User*, const std::string&) { }
138 void Module::OnChangeHost(User*, const std::string&) { }
139 void Module::OnChangeName(User*, const std::string&) { }
140 void Module::OnChangeIdent(User*, const std::string&) { }
141 void Module::OnAddLine(User*, XLine*) { }
142 void Module::OnDelLine(User*, XLine*) { }
143 void Module::OnExpireLine(XLine*) { }
144 void Module::OnCleanup(int, void*) { }
145 ModResult Module::OnChannelPreDelete(Channel*) { return MOD_RES_PASSTHRU; }
146 void Module::OnChannelDelete(Channel*) { }
147 ModResult Module::OnSetAway(User*, const std::string &) { return MOD_RES_PASSTHRU; }
148 ModResult Module::OnUserList(User*, Channel*) { return MOD_RES_PASSTHRU; }
149 ModResult Module::OnWhoisLine(User*, User*, int&, std::string&) { return MOD_RES_PASSTHRU; }
150 void Module::OnBuildNeighborList(User*, UserChanList&, std::map<User*,bool>&) { }
151 void Module::OnGarbageCollect() { }
152 void Module::OnText(User*, void*, int, const std::string&, char, CUList&) { }
153 void Module::OnRunTestSuite() { }
154 void Module::OnNamesListItem(User*, Membership*, std::string&, std::string&) { }
155 ModResult Module::OnNumeric(User*, unsigned int, const std::string&) { return MOD_RES_PASSTHRU; }
156 void Module::OnHookIO(StreamSocket*, ListenSocket*) { }
157 ModResult Module::OnAcceptConnection(int, ListenSocket*, irc::sockets::sockaddrs*, irc::sockets::sockaddrs*) { return MOD_RES_PASSTHRU; }
158 void Module::OnSendWhoLine(User*, User*, Channel*, std::string&) { }
159 ModResult Module::OnChannelRestrictionApply(User*, Channel*, const char*) { return MOD_RES_PASSTHRU; }
161 ModuleManager::ModuleManager() : ModCount(0)
165 ModuleManager::~ModuleManager()
169 bool ModuleManager::Attach(Implementation i, Module* mod)
171 if (std::find(EventHandlers[i].begin(), EventHandlers[i].end(), mod) != EventHandlers[i].end())
174 EventHandlers[i].push_back(mod);
178 bool ModuleManager::Detach(Implementation i, Module* mod)
180 EventHandlerIter x = std::find(EventHandlers[i].begin(), EventHandlers[i].end(), mod);
182 if (x == EventHandlers[i].end())
185 EventHandlers[i].erase(x);
189 void ModuleManager::Attach(Implementation* i, Module* mod, size_t sz)
191 for (size_t n = 0; n < sz; ++n)
195 void ModuleManager::DetachAll(Module* mod)
197 for (size_t n = I_BEGIN + 1; n != I_END; ++n)
198 Detach((Implementation)n, mod);
201 bool ModuleManager::SetPriority(Module* mod, Priority s)
203 for (size_t n = I_BEGIN + 1; n != I_END; ++n)
204 SetPriority(mod, (Implementation)n, s);
209 bool ModuleManager::SetPriority(Module* mod, Implementation i, Priority s, Module** modules, size_t sz)
211 /** To change the priority of a module, we first find its position in the vector,
212 * then we find the position of the other modules in the vector that this module
213 * wants to be before/after. We pick off either the first or last of these depending
214 * on which they want, and we make sure our module is *at least* before or after
215 * the first or last of this subset, depending again on the type of priority.
222 /* Locate our module. This is O(n) but it only occurs on module load so we're
223 * not too bothered about it
225 for (size_t x = 0; x != EventHandlers[i].size(); ++x)
227 if (EventHandlers[i][x] == mod)
235 /* Eh? this module doesnt exist, probably trying to set priority on an event
236 * theyre not attached to.
244 case PRIORITY_DONTCARE:
247 /* Module wants to be first, sod everything else */
249 if (prioritizationState != PRIO_STATE_FIRST)
254 /* Module wants to be last. */
256 if (prioritizationState != PRIO_STATE_FIRST)
258 else if (EventHandlers[i].empty())
261 swap_pos = EventHandlers[i].size() - 1;
263 /* Place this module after a set of other modules */
266 /* Find the latest possible position */
269 for (size_t x = 0; x != EventHandlers[i].size(); ++x)
271 for (size_t n = 0; n < sz; ++n)
273 if ((modules[n]) && (EventHandlers[i][x] == modules[n]) && (x >= swap_pos) && (source <= swap_pos))
282 /* Place this module before a set of other modules */
283 case PRIORITY_BEFORE:
285 swap_pos = EventHandlers[i].size() - 1;
287 for (size_t x = 0; x != EventHandlers[i].size(); ++x)
289 for (size_t n = 0; n < sz; ++n)
291 if ((modules[n]) && (EventHandlers[i][x] == modules[n]) && (x <= swap_pos) && (source >= swap_pos))
302 /* Do we need to swap? */
303 if (swap && (swap_pos != source))
305 // We are going to change positions; we'll need to run again to verify all requirements
306 if (prioritizationState == PRIO_STATE_LAST)
307 prioritizationState = PRIO_STATE_AGAIN;
308 /* Suggestion from Phoenix, "shuffle" the modules to better retain call order */
311 if (source > swap_pos)
314 for (unsigned int j = source; j != swap_pos; j += incrmnt)
316 if (( j + incrmnt > EventHandlers[i].size() - 1) || (j + incrmnt < 0))
319 std::swap(EventHandlers[i][j], EventHandlers[i][j+incrmnt]);
326 std::string& ModuleManager::LastError()
328 return LastModuleError;
331 CmdResult InspIRCd::CallCommandHandler(const std::string &commandname, const std::vector<std::string>& parameters, User* user)
333 return this->Parser->CallHandler(commandname, parameters, user);
336 bool InspIRCd::IsValidModuleCommand(const std::string &commandname, int pcnt, User* user)
338 return this->Parser->IsValidCommand(commandname, pcnt, user);
341 void InspIRCd::AddCommand(Command *f)
343 if (!this->Parser->AddCommand(f))
345 throw ModuleException("Command "+std::string(f->name)+" already exists.");
349 void ModuleManager::AddService(ServiceProvider& item)
351 switch (item.service)
353 case SERVICE_COMMAND:
354 if (!ServerInstance->Parser->AddCommand(static_cast<Command*>(&item)))
355 throw ModuleException("Command "+std::string(item.name)+" already exists.");
359 if (!ServerInstance->Modes->AddMode(static_cast<ModeHandler*>(&item)))
360 throw ModuleException("Mode "+std::string(item.name)+" already exists.");
362 case SERVICE_METADATA:
363 ServerInstance->Extensions.Register(static_cast<ExtensionItem*>(&item));
368 DataProviders.insert(std::make_pair(item.name, &item));
369 std::string::size_type slash = item.name.find('/');
370 if (slash != std::string::npos)
372 DataProviders.insert(std::make_pair(item.name.substr(0, slash), &item));
373 DataProviders.insert(std::make_pair(item.name.substr(slash + 1), &item));
378 throw ModuleException("Cannot add unknown service type");
382 ServiceProvider* ModuleManager::FindService(ServiceType type, const std::string& name)
389 std::multimap<std::string, ServiceProvider*>::iterator i = DataProviders.find(name);
390 if (i != DataProviders.end() && i->second->service == type)
394 // TODO implement finding of the other types
396 throw ModuleException("Cannot find unknown service type");
400 dynamic_reference_base::dynamic_reference_base(Module* Creator, const std::string& Name)
401 : name(Name), value(NULL), creator(Creator)
403 ServerInstance->Modules->ActiveDynrefs.push_back(this);
406 dynamic_reference_base::~dynamic_reference_base()
408 for(unsigned int i = 0; i < ServerInstance->Modules->ActiveDynrefs.size(); i++)
410 if (ServerInstance->Modules->ActiveDynrefs[i] == this)
412 unsigned int last = ServerInstance->Modules->ActiveDynrefs.size() - 1;
414 ServerInstance->Modules->ActiveDynrefs[i] = ServerInstance->Modules->ActiveDynrefs[last];
415 ServerInstance->Modules->ActiveDynrefs.erase(ServerInstance->Modules->ActiveDynrefs.begin() + last);
421 void dynamic_reference_base::SetProvider(const std::string& newname)
427 void dynamic_reference_base::lookup()
430 throw ModuleException("Dynamic reference to '" + name + "' failed to resolve");
433 dynamic_reference_base::operator bool()
437 std::multimap<std::string, ServiceProvider*>::iterator i = ServerInstance->Modules->DataProviders.find(name);
438 if (i != ServerInstance->Modules->DataProviders.end())
439 value = static_cast<DataProvider*>(i->second);
444 void InspIRCd::SendMode(const std::vector<std::string>& parameters, User *user)
446 this->Modes->Process(parameters, user);
449 bool InspIRCd::AddResolver(Resolver* r, bool cached)
452 return this->Res->AddResolverClass(r);
455 r->TriggerCachedResult();
461 Module* ModuleManager::Find(const std::string &name)
463 std::map<std::string, Module*>::iterator modfind = Modules.find(name);
465 if (modfind == Modules.end())
468 return modfind->second;
471 const std::vector<std::string> ModuleManager::GetAllModuleNames(int filter)
473 std::vector<std::string> retval;
474 for (std::map<std::string, Module*>::iterator x = Modules.begin(); x != Modules.end(); ++x)
475 if (!filter || (x->second->GetVersion().Flags & filter))
476 retval.push_back(x->first);
480 ConfigReader::ConfigReader()
486 ConfigReader::~ConfigReader()
490 static ConfigTag* SlowGetTag(const std::string &tag, int index)
492 ConfigTagList tags = ServerInstance->Config->ConfTags(tag);
493 while (tags.first != tags.second)
496 return tags.first->second;
503 std::string ConfigReader::ReadValue(const std::string &tag, const std::string &name, const std::string &default_value, int index, bool allow_linefeeds)
505 std::string result = default_value;
506 if (!SlowGetTag(tag, index)->readString(name, result, allow_linefeeds))
508 this->error = CONF_VALUE_NOT_FOUND;
513 std::string ConfigReader::ReadValue(const std::string &tag, const std::string &name, int index, bool allow_linefeeds)
515 return ReadValue(tag, name, "", index, allow_linefeeds);
518 bool ConfigReader::ReadFlag(const std::string &tag, const std::string &name, const std::string &default_value, int index)
520 bool def = (default_value == "yes");
521 return SlowGetTag(tag, index)->getBool(name, def);
524 bool ConfigReader::ReadFlag(const std::string &tag, const std::string &name, int index)
526 return ReadFlag(tag, name, "", index);
530 int ConfigReader::ReadInteger(const std::string &tag, const std::string &name, const std::string &default_value, int index, bool need_positive)
532 int v = atoi(default_value.c_str());
533 int result = SlowGetTag(tag, index)->getInt(name, v);
535 if ((need_positive) && (result < 0))
537 this->error = CONF_INT_NEGATIVE;
544 int ConfigReader::ReadInteger(const std::string &tag, const std::string &name, int index, bool need_positive)
546 return ReadInteger(tag, name, "", index, need_positive);
549 long ConfigReader::GetError()
551 long olderr = this->error;
556 int ConfigReader::Enumerate(const std::string &tag)
558 ServerInstance->Logs->Log("MODULE", DEBUG, "Module is using ConfigReader::Enumerate on %s; this is slow!",
561 while (SlowGetTag(tag, i)) i++;
565 FileReader::FileReader(const std::string &filename)
570 FileReader::FileReader()
574 std::string FileReader::Contents()
577 for (file_cache::iterator a = this->fc.begin(); a != this->fc.end(); a++)
585 unsigned long FileReader::ContentSize()
587 return this->contentsize;
590 void FileReader::CalcSize()
593 for (file_cache::iterator a = this->fc.begin(); a != this->fc.end(); a++)
594 n += (a->length() + 2);
595 this->contentsize = n;
598 void FileReader::LoadFile(const std::string &filename)
602 if (ServerInstance->Config->ReadFile(c,filename.c_str()))
610 FileReader::~FileReader()
614 bool FileReader::Exists()
616 return (!(fc.size() == 0));
619 std::string FileReader::GetLine(int x)
621 if ((x<0) || ((unsigned)x>fc.size()))
626 int FileReader::FileSize()