2 * InspIRCd -- Internet Relay Chat Daemon
4 * Copyright (C) 2018-2020 Sadie Powell <sadie@witchery.services>
5 * Copyright (C) 2015, 2018 Attila Molnar <attilamolnar@hush.com>
7 * This file is part of InspIRCd. InspIRCd is free software: you can
8 * redistribute it and/or modify it under the terms of the GNU General Public
9 * License as published by the Free Software Foundation, version 2.
11 * This program is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
25 class ModuleEventListener;
26 class ModuleEventProvider;
29 /** Provider of one or more cross-module events.
30 * Modules who wish to provide events for other modules create instances of this class and use
31 * one of the macros below to fire the event, passing the instance of the event provider class
33 * Event providers are identified using a unique identifier string.
35 class Events::ModuleEventProvider : public ServiceProvider, private dynamic_reference_base::CaptureHook
40 bool operator()(ModuleEventListener* lhs, ModuleEventListener* rhs) const;
45 bool operator()(ModuleEventListener* lhs, ModuleEventListener* rhs) const;
48 typedef insp::flat_multiset<ModuleEventListener*, Comp, ElementComp> SubscriberList;
51 * @param mod Module providing the event(s)
52 * @param eventid Identifier of the event or event group provided, must be unique
54 ModuleEventProvider(Module* mod, const std::string& eventid)
55 : ServiceProvider(mod, eventid, SERVICE_DATA)
58 prov.SetCaptureHook(this);
61 /** Get list of objects subscribed to this event
62 * @return List of subscribed objects
64 const SubscriberList& GetSubscribers() const { return prov->subscribers; }
66 /** Subscribes a listener to this event.
67 * @param subscriber The listener to subscribe.
69 void Subscribe(ModuleEventListener* subscriber)
71 subscribers.insert(subscriber);
72 OnSubscribe(subscriber);
75 /** Unsubscribes a listener from this event.
76 * @param subscriber The listener to unsubscribe.
78 void Unsubscribe(ModuleEventListener* subscriber)
80 subscribers.erase(subscriber);
81 OnUnsubscribe(subscriber);
85 void OnCapture() CXX11_OVERRIDE
87 // If someone else holds the list from now on, clear mine. See below for more info.
92 /** Called when a listener subscribes to this event.
93 * @param subscriber The listener which subscribed.
95 virtual void OnSubscribe(ModuleEventListener* subscriber) { }
97 /** Called when a listener unsubscribes from this event.
98 * @param subscriber The listener which unsubscribed.
100 virtual void OnUnsubscribe(ModuleEventListener* subscriber) { }
102 /** Reference to the active provider for this event. In case multiple event providers
103 * exist for the same event, only one of them contains the list of subscribers.
104 * To handle the case when we are not the ones with the list, we get it from the provider
105 * where the dynref points to.
107 dynamic_reference_nocheck<ModuleEventProvider> prov;
109 /** List of objects subscribed to the event(s) provided by us, or empty if multiple providers
110 * exist with the same name and we are not the ones holding the list.
112 SubscriberList subscribers;
115 /** Base class for abstract classes describing cross-module events.
116 * Subscribers should NOT inherit directly from this class.
118 class Events::ModuleEventListener : private dynamic_reference_base::CaptureHook
120 /** Reference to the provider, can be NULL if none of the provider modules are loaded
122 dynamic_reference_nocheck<ModuleEventProvider> prov;
124 const unsigned int eventpriority;
126 /** Called by the dynref when the event provider becomes available
128 void OnCapture() CXX11_OVERRIDE
130 prov->Subscribe(this);
134 static const unsigned int DefaultPriority = 100;
137 * @param mod Module subscribing
138 * @param eventid Identifier of the event to subscribe to
139 * @param eventprio The priority to give this event listener
141 ModuleEventListener(Module* mod, const std::string& eventid, unsigned int eventprio = DefaultPriority)
143 , eventpriority(eventprio)
145 prov.SetCaptureHook(this);
146 // If the dynamic_reference resolved at construction our capture handler wasn't called
148 ModuleEventListener::OnCapture();
151 ~ModuleEventListener()
154 prov->Unsubscribe(this);
157 /** Retrieves the module which created this listener. */
158 const Module* GetModule() const { return prov.creator; }
160 /** Retrieves the priority of this event. */
161 unsigned int GetPriority() const { return eventpriority; }
164 inline bool Events::ModuleEventProvider::Comp::operator()(Events::ModuleEventListener* lhs, Events::ModuleEventListener* rhs) const
166 return (lhs->GetPriority() < rhs->GetPriority());
169 inline bool Events::ModuleEventProvider::ElementComp::operator()(Events::ModuleEventListener* lhs, Events::ModuleEventListener* rhs) const
171 if (lhs->GetPriority() < rhs->GetPriority())
173 if (lhs->GetPriority() > rhs->GetPriority())
175 return std::less<ModuleEventListener*>()(lhs, rhs);
179 * Run the given hook provided by a module
181 * FOREACH_MOD_CUSTOM(accountevprov, AccountEventListener, OnAccountChange, MOD_RESULT, (user, newaccount))
183 #define FOREACH_MOD_CUSTOM(prov, listenerclass, func, params) do { \
184 const ::Events::ModuleEventProvider::SubscriberList& _handlers = (prov).GetSubscribers(); \
185 for (::Events::ModuleEventProvider::SubscriberList::const_iterator _i = _handlers.begin(); _i != _handlers.end(); ++_i) \
187 listenerclass* _t = static_cast<listenerclass*>(*_i); \
188 const Module* _m = _t->GetModule(); \
189 if (_m && !_m->dying) \
195 * Run the given hook provided by a module until some module returns MOD_RES_ALLOW or MOD_RES_DENY.
196 * If no module does that, result is set to MOD_RES_PASSTHRU.
198 * Example: ModResult MOD_RESULT;
199 * FIRST_MOD_RESULT_CUSTOM(httpevprov, HTTPRequestEventListener, OnHTTPRequest, MOD_RESULT, (request));
201 #define FIRST_MOD_RESULT_CUSTOM(prov, listenerclass, func, result, params) do { \
202 result = MOD_RES_PASSTHRU; \
203 const ::Events::ModuleEventProvider::SubscriberList& _handlers = (prov).GetSubscribers(); \
204 for (::Events::ModuleEventProvider::SubscriberList::const_iterator _i = _handlers.begin(); _i != _handlers.end(); ++_i) \
206 listenerclass* _t = static_cast<listenerclass*>(*_i); \
207 const Module* _m = _t->GetModule(); \
208 if (!_m || _m->dying) \
210 result = _t->func params ; \
211 if (result != MOD_RES_PASSTHRU) \