diff options
| author | Adam <Adam@anope.org> | 2016-07-28 21:29:35 -0400 |
|---|---|---|
| committer | Adam <Adam@anope.org> | 2016-07-28 21:29:35 -0400 |
| commit | 0e758a2ac23dc4a001e8e126cec14588da9a9769 (patch) | |
| tree | 45df813323e023c5c89db7279426c4ad0943b4a9 /include | |
| parent | a3c8afae00c54d5b95c620248b51f90679d7d53f (diff) | |
Allow serializable fields to use storage in the respective objects.
Split service management code nito a proper servicemanager. Make service
references managed instead of lazy lookup. Also made events and
serializable use service manager instead of their respective systems for
management
Diffstat (limited to 'include')
63 files changed, 1340 insertions, 912 deletions
diff --git a/include/accessgroup.h b/include/accessgroup.h new file mode 100644 index 000000000..bc3d1a81e --- /dev/null +++ b/include/accessgroup.h @@ -0,0 +1,43 @@ +#include "modules/chanserv.h" + +#pragma once + +namespace ChanServ +{ + +/* A group of access entries. This is used commonly, for example with ChanServ::Channel::AccessFor, + * to show what access a user has on a channel because users can match multiple access entries. + */ +class CoreExport AccessGroup : public std::vector<ChanAccess *> +{ + public: + /* Channel these access entries are on */ + ChanServ::Channel *ci = nullptr; + /* Account these entries affect, if any */ + const NickServ::Account *nc = nullptr; + /* super_admin always gets all privs. founder is a special case where ci->founder == nc */ + bool super_admin = false, founder = false; + + /** Check if this access group has a certain privilege. Eg, it + * will check every ChanAccess entry of this group for any that + * has the given privilege. + * @param priv The privilege + * @return true if any entry has the given privilege + */ + bool HasPriv(const Anope::string &priv); + + /** Get the "highest" access entry from this group of entries. + * The highest entry is determined by the entry that has the privilege + * with the highest rank (see Privilege::rank). + * @return The "highest" entry + */ + ChanAccess *Highest(); + + /* Comparison operators to other AccessGroups */ + bool operator>(AccessGroup &other); + bool operator<(AccessGroup &other); + bool operator>=(AccessGroup &other); + bool operator<=(AccessGroup &other); +}; + +} // namespace ChanServ
\ No newline at end of file diff --git a/include/bots.h b/include/bots.h index 80efd8872..d708d8948 100644 --- a/include/bots.h +++ b/include/bots.h @@ -120,12 +120,19 @@ class CoreExport ServiceBot : public LocalUser class BotInfo : public Serialize::Object { + friend class BotInfoType; + + Anope::string nick, user, host, realname; + time_t created = 0; + bool operonly = false; + public: + static constexpr const char *const NAME = "botinfo"; + ServiceBot *bot; Configuration::Block *conf = nullptr; - BotInfo(Serialize::TypeBase *type) : Serialize::Object(type) { } - BotInfo(Serialize::TypeBase *type, Serialize::ID id) : Serialize::Object(type, id) { } + using Serialize::Object::Object; void Delete() override; @@ -155,16 +162,14 @@ class BotInfoType : public Serialize::Type<BotInfo> Serialize::Field<BotInfo, time_t> created; Serialize::Field<BotInfo, bool> operonly; - BotInfoType() : Serialize::Type<BotInfo>(nullptr, "BotInfo") - , nick(this, "nick") - , user(this, "user") - , host(this, "host") - , realname(this, "realname") - , created(this, "created") - , operonly(this, "operonly") + BotInfoType() : Serialize::Type<BotInfo>(nullptr) + , nick(this, "nick", &BotInfo::nick) + , user(this, "user", &BotInfo::user) + , host(this, "host", &BotInfo::host) + , realname(this, "realname", &BotInfo::realname) + , created(this, "created", &BotInfo::created) + , operonly(this, "operonly", &BotInfo::operonly) { } }; -static Serialize::TypeReference<BotInfo> botinfo("BotInfo"); - diff --git a/include/commands.h b/include/commands.h index e94cffae1..18f36c5d1 100644 --- a/include/commands.h +++ b/include/commands.h @@ -116,6 +116,8 @@ class CoreExport Command : public Service /* Module which owns us */ Module *module; + static constexpr const char *NAME = "Command"; + protected: /** Create a new command. * @param owner The owner of the command diff --git a/include/config.h b/include/config.h index 4fe5a94d5..84f47ecb3 100644 --- a/include/config.h +++ b/include/config.h @@ -129,7 +129,7 @@ namespace Configuration unsigned int LineWrap; std::vector<Usermode> Usermodes; std::vector<Channelmode> Channelmodes; - unsigned char CaseMapUpper[256], CaseMapLower[256]; + unsigned char CaseMapUpper[256] = { 0 }, CaseMapLower[256] = { 0 }; /* module configuration blocks */ std::map<Anope::string, Block *> modules; diff --git a/include/event.h b/include/event.h index f7934b84a..2d5ce55b8 100644 --- a/include/event.h +++ b/include/event.h @@ -1,6 +1,8 @@ /* * - * (C) 2014 Anope Team + * (C) 2014-2016 Anope Team + * (C) 2016 Adam <Adam@anope.org> + * * Contact us at team@anope.org * * Please read COPYING and README for further details. @@ -10,6 +12,7 @@ #pragma once #include "service.h" +#include "base.h" /** Possible return types from events. */ @@ -20,9 +23,10 @@ enum EventReturn EVENT_ALLOW }; -struct Events +class Events : public Service { - virtual ~Events() = default; + public: + Events(Module *o, const Anope::string &ename) : Service(o, ename) { } }; /* uninstantiated */ @@ -59,101 +63,94 @@ struct Caller<EventHandler, Func, EventReturn, Args...> } }; -template<typename T> -struct EventName; - template<typename EventHandler> class EventHook; template<typename EventHandler> -class EventHandlers : public Service +class EventDispatcher { - std::vector<EventHandler *> handlers; - friend class EventHook<EventHandler>; + static_assert(std::is_base_of<Events, EventHandler>::value, ""); + + ServiceReferenceList<EventHandler> handlers; public: - EventHandlers(Module *o) : Service(o, "EventHandlers", EventName<EventHandler>::name) - { - } + EventDispatcher(const Anope::string &name) : handlers(name) { } template<typename Func, typename... Args> - auto operator()(Func func, Args&&... args) -> decltype(((static_cast<EventHandler*>(nullptr))->*func)(args...)) + auto Dispatch(Func func, Args&&... args) -> decltype(((static_cast<EventHandler*>(nullptr))->*func)(args...)) { - return Caller<EventHandler, Func, decltype(((static_cast<EventHandler*>(nullptr))->*func)(args...)), Args...>()(this->handlers, func, std::forward<Args>(args)...); + const std::vector<EventHandler *> h = this->handlers.GetServices(); + return Caller<EventHandler, Func, decltype(((static_cast<EventHandler*>(nullptr))->*func)(args...)), Args...>()(h, func, std::forward<Args>(args)...); } }; template<typename EventHandler> -class EventHandlersReference +class EventHook : public EventHandler { - ServiceReference<EventHandlers<EventHandler>> ref; - public: - EventHandlersReference() : ref("EventHandlers", EventName<EventHandler>::name) { } + static_assert(std::is_base_of<Events, EventHandler>::value, ""); - explicit operator bool() + public: + enum class Priority { - return !!ref; + FIRST, + LAST } + priority; - template<typename Func, typename... Args> - auto operator()(Func func, Args&&... args) -> decltype(((static_cast<EventHandler*>(nullptr))->*func)(args...)) + EventHook(Module *creator) : EventHook(creator, Priority::LAST) { } + + EventHook(Module *creator, Priority p) + : EventHandler(creator, EventHandler::NAME) + , priority(p) { - return (**ref)(func, std::forward<Args>(args)...); +#warning "priority doesnt work" } }; -template<typename EventHandler> -class EventHook : public EventHandler +class EventManager { + Anope::hash_map<EventDispatcher<Events> *> cache; - struct ServiceReferenceListener : ServiceReference<EventHandlers<EventHandler>> + template<typename T> + EventDispatcher<T> *GetDispatcher(const Anope::string &name) { - EventHook<EventHandler> *eh; + auto it = cache.find(name); + if (it != cache.end()) + return reinterpret_cast<EventDispatcher<T> *>(it->second); - ServiceReferenceListener(EventHook<EventHandler> *t, const Anope::string &sname) : ServiceReference<EventHandlers<EventHandler>>("EventHandlers", sname), eh(t) - { - } + auto dispatcher = new EventDispatcher<T>(name); + cache[name] = reinterpret_cast<EventDispatcher<Events> *>(dispatcher); + return dispatcher; + } - void OnAcquire() override - { - if (std::find((*this)->handlers.begin(), (*this)->handlers.end(), eh) == (*this)->handlers.end()) - { - if (eh->priority == Priority::LAST) - (*this)->handlers.push_back(eh); - else - (*this)->handlers.insert((*this)->handlers.begin(), eh); - } - } - } handlers; + static EventManager *eventManager; public: - enum class Priority + template< + typename Type, + typename Function, + typename... Args + > + auto Dispatch(Function Type::*func, Args&&... args) -> decltype(((static_cast<Type*>(nullptr))->*func)(args...)) { - FIRST, - LAST - } - priority; + static_assert(std::is_base_of<Events, Type>::value, ""); - EventHook(Priority p = Priority::LAST) : handlers(this, EventName<EventHandler>::name), priority(p) - { - handlers.Check(); + EventDispatcher<Type> *dispatcher = GetDispatcher<Type>(Type::NAME); + return dispatcher->Dispatch(func, std::forward<Args>(args)...); } - ~EventHook() - { - if (!handlers) - return; - - auto it = std::find(handlers->handlers.begin(), handlers->handlers.end(), this); - if (it != handlers->handlers.end()) - handlers->handlers.erase(it); - } + static void Init(); + static EventManager *Get(); }; namespace Event { struct CoreExport PreUserKicked : Events { + static constexpr const char *NAME = "preuserkicked"; + + using Events::Events; + /** Called before a user has been kicked from a channel. * @param source The kicker * @param cu The user, channel, and status of the user being kicked @@ -162,10 +159,13 @@ namespace Event */ virtual EventReturn OnPreUserKicked(const MessageSource &source, ChanUserContainer *cu, const Anope::string &kickmsg) anope_abstract; }; - extern CoreExport EventHandlers<PreUserKicked> OnPreUserKicked; struct CoreExport UserKicked : Events { + static constexpr const char *NAME = "userkicked"; + + using Events::Events; + /** Called when a user has been kicked from a channel. * @param source The kicker * @param target The user being kicked @@ -175,10 +175,13 @@ namespace Event */ virtual void OnUserKicked(const MessageSource &source, User *target, const Anope::string &channel, ChannelStatus &status, const Anope::string &kickmsg) anope_abstract; }; - extern CoreExport EventHandlers<UserKicked> OnUserKicked; struct CoreExport PreBotAssign : Events { + static constexpr const char *NAME = "prebotassign"; + + using Events::Events; + /** Called before a bot is assigned to a channel. * @param sender The user assigning the bot * @param ci The channel the bot is to be assigned to. @@ -187,18 +190,24 @@ namespace Event */ virtual EventReturn OnPreBotAssign(User *sender, ChanServ::Channel *ci, ServiceBot *bi) anope_abstract; }; - extern CoreExport EventHandlers<PreBotAssign> OnPreBotAssign; struct CoreExport BotAssign : Events { + static constexpr const char *NAME = "botassign"; + + using Events::Events; + /** Called when a bot is assigned ot a channel */ virtual void OnBotAssign(User *sender, ChanServ::Channel *ci, ServiceBot *bi) anope_abstract; }; - extern CoreExport EventHandlers<BotAssign> OnBotAssign; struct CoreExport BotUnAssign : Events { + static constexpr const char *NAME = "botunassign"; + + using Events::Events; + /** Called before a bot is unassigned from a channel. * @param sender The user unassigning the bot * @param ci The channel the bot is being removed from @@ -206,40 +215,51 @@ namespace Event */ virtual EventReturn OnBotUnAssign(User *sender, ChanServ::Channel *ci) anope_abstract; }; - extern CoreExport EventHandlers<BotUnAssign> OnBotUnAssign; struct CoreExport UserConnect : Events { + static constexpr const char *NAME = "userconnect"; + + using Events::Events; + /** Called when a new user connects to the network. * @param u The connecting user. * @param exempt set to true/is true if the user should be excepted from bans etc */ virtual void OnUserConnect(User *u, bool &exempt) anope_abstract; }; - extern CoreExport EventHandlers<UserConnect> OnUserConnect; struct CoreExport NewServer : Events { + static constexpr const char *NAME = "newserver"; + + using Events::Events; + /** Called when a new server connects to the network. * @param s The server that has connected to the network */ virtual void OnNewServer(Server *s) anope_abstract; }; - extern CoreExport EventHandlers<NewServer> OnNewServer; struct CoreExport UserNickChange : Events { + static constexpr const char *NAME = "usernickchange"; + + using Events::Events; + /** Called after a user changed the nick * @param u The user. * @param oldnick The old nick of the user */ virtual void OnUserNickChange(User *u, const Anope::string &oldnick) anope_abstract; }; - extern CoreExport EventHandlers<UserNickChange> OnUserNickChange; - struct CoreExport PreCommand : Events { + static constexpr const char *NAME = "precommand"; + + using Events::Events; + /** Called before a command is due to be executed. * @param source The source of the command * @param command The command the user is executing @@ -248,10 +268,13 @@ namespace Event */ virtual EventReturn OnPreCommand(CommandSource &source, Command *command, std::vector<Anope::string> ¶ms) anope_abstract; }; - extern CoreExport EventHandlers<PreCommand> OnPreCommand; struct CoreExport PostCommand : Events { + static constexpr const char *NAME = "postcommand"; + + using Events::Events; + /** Called after a command has been executed. * @param source The source of the command * @param command The command the user executed @@ -259,68 +282,90 @@ namespace Event */ virtual void OnPostCommand(CommandSource &source, Command *command, const std::vector<Anope::string> ¶ms) anope_abstract; }; - extern CoreExport EventHandlers<PostCommand> OnPostCommand; struct CoreExport SaveDatabase : Events { + static constexpr const char *NAME = "savedatabase"; + + using Events::Events; + /** Called when the databases are saved */ virtual void OnSaveDatabase() anope_abstract; }; - extern CoreExport EventHandlers<SaveDatabase> OnSaveDatabase; struct CoreExport LoadDatabase : Events { + static constexpr const char *NAME = "loaddatabase"; + + using Events::Events; + /** Called when the databases are loaded * @return EVENT_CONTINUE to let other modules continue loading, EVENT_STOP to stop */ virtual EventReturn OnLoadDatabase() anope_abstract; }; - extern CoreExport EventHandlers<LoadDatabase> OnLoadDatabase; struct CoreExport Encrypt : Events { + static constexpr const char *NAME = "encrypt"; + + using Events::Events; + /** Called when anope needs to check passwords against encryption * see src/encrypt.c for detailed informations */ virtual EventReturn OnEncrypt(const Anope::string &src, Anope::string &dest) anope_abstract; }; - extern CoreExport EventHandlers<Encrypt> OnEncrypt; struct CoreExport Decrypt : Events { - virtual EventReturn OnDecrypt(const Anope::string &hashm, const Anope::string &src, Anope::string &dest) anope_abstract; - }; - extern CoreExport EventHandlers<Decrypt> OnDecrypt; + static constexpr const char *NAME = "decrypt"; + using Events::Events; + virtual EventReturn OnDecrypt(const Anope::string &hashm, const Anope::string &src, Anope::string &dest) anope_abstract; + }; struct CoreExport CreateBot : Events { + static constexpr const char *NAME = "createbot"; + + using Events::Events; + /** Called when a bot is created or destroyed */ virtual void OnCreateBot(ServiceBot *bi) anope_abstract; }; - extern CoreExport EventHandlers<CreateBot> OnCreateBot; struct CoreExport DelBot : Events { + static constexpr const char *NAME = "delbot"; + + using Events::Events; + virtual void OnDelBot(ServiceBot *bi) anope_abstract; }; - extern CoreExport EventHandlers<DelBot> OnDelBot; struct CoreExport PrePartChannel : Events { + static constexpr const char *NAME = "prepartchannel"; + + using Events::Events; + /** Called before a user parts a channel * @param u The user * @param c The channel */ virtual void OnPrePartChannel(User *u, Channel *c) anope_abstract; }; - extern CoreExport EventHandlers<PrePartChannel> OnPrePartChannel; struct CoreExport PartChannel : Events { + static constexpr const char *NAME = "partchannel"; + + using Events::Events; + /** Called when a user parts a channel * @param u The user * @param c The channel, may be NULL if the channel no longer exists @@ -329,10 +374,13 @@ namespace Event */ virtual void OnPartChannel(User *u, Channel *c, const Anope::string &channel, const Anope::string &msg) anope_abstract; }; - extern CoreExport EventHandlers<PartChannel> OnPartChannel; struct CoreExport LeaveChannel : Events { + static constexpr const char *NAME = "leavechannel"; + + using Events::Events; + /** Called when a user leaves a channel. * From either parting, being kicked, or quitting/killed! * @param u The user @@ -340,10 +388,13 @@ namespace Event */ virtual void OnLeaveChannel(User *u, Channel *c) anope_abstract; }; - extern CoreExport EventHandlers<LeaveChannel> OnLeaveChannel; struct CoreExport JoinChannel : Events { + static constexpr const char *NAME = "joinchannel"; + + using Events::Events; + /** Called after a user joins a channel * If this event triggers the user is allowed to be in the channel, and will * not be kicked for restricted/akick/forbidden, etc. If you want to kick the user, @@ -353,10 +404,13 @@ namespace Event */ virtual void OnJoinChannel(User *u, Channel *c) anope_abstract; }; - extern CoreExport EventHandlers<JoinChannel> OnJoinChannel; struct CoreExport TopicUpdated : Events { + static constexpr const char *NAME = "topicupdated"; + + using Events::Events; + /** Called when a new topic is set * @param source * @param c The channel @@ -365,58 +419,79 @@ namespace Event */ virtual void OnTopicUpdated(User *source, Channel *c, const Anope::string &user, const Anope::string &topic) anope_abstract; }; - extern CoreExport EventHandlers<TopicUpdated> OnTopicUpdated; |
