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 /** Retrieves the module which created this listener. */
62 const Module* GetModule() const { return prov.creator; }
64 /** Get list of objects subscribed to this event
65 * @return List of subscribed objects
67 const SubscriberList& GetSubscribers() const { return prov->subscribers; }
69 /** Subscribes a listener to this event.
70 * @param subscriber The listener to subscribe.
72 void Subscribe(ModuleEventListener* subscriber)
74 subscribers.insert(subscriber);
75 OnSubscribe(subscriber);
78 /** Unsubscribes a listener from this event.
79 * @param subscriber The listener to unsubscribe.
81 void Unsubscribe(ModuleEventListener* subscriber)
83 subscribers.erase(subscriber);
84 OnUnsubscribe(subscriber);
88 void OnCapture() CXX11_OVERRIDE
90 // If someone else holds the list from now on, clear mine. See below for more info.
95 /** Called when a listener subscribes to this event.
96 * @param subscriber The listener which subscribed.
98 virtual void OnSubscribe(ModuleEventListener* subscriber) { }
100 /** Called when a listener unsubscribes from this event.
101 * @param subscriber The listener which unsubscribed.
103 virtual void OnUnsubscribe(ModuleEventListener* subscriber) { }
105 /** Reference to the active provider for this event. In case multiple event providers
106 * exist for the same event, only one of them contains the list of subscribers.
107 * To handle the case when we are not the ones with the list, we get it from the provider
108 * where the dynref points to.
110 dynamic_reference_nocheck<ModuleEventProvider> prov;
112 /** List of objects subscribed to the event(s) provided by us, or empty if multiple providers
113 * exist with the same name and we are not the ones holding the list.
115 SubscriberList subscribers;
118 /** Base class for abstract classes describing cross-module events.
119 * Subscribers should NOT inherit directly from this class.
121 class Events::ModuleEventListener : private dynamic_reference_base::CaptureHook
123 /** Reference to the provider, can be NULL if none of the provider modules are loaded
125 dynamic_reference_nocheck<ModuleEventProvider> prov;
127 const unsigned int eventpriority;
129 /** Called by the dynref when the event provider becomes available
131 void OnCapture() CXX11_OVERRIDE
133 prov->Subscribe(this);
137 static const unsigned int DefaultPriority = 100;
140 * @param mod Module subscribing
141 * @param eventid Identifier of the event to subscribe to
142 * @param eventprio The priority to give this event listener
144 ModuleEventListener(Module* mod, const std::string& eventid, unsigned int eventprio = DefaultPriority)
146 , eventpriority(eventprio)
148 prov.SetCaptureHook(this);
149 // If the dynamic_reference resolved at construction our capture handler wasn't called
151 ModuleEventListener::OnCapture();
154 ~ModuleEventListener()
157 prov->Unsubscribe(this);
160 /** Retrieves the module which created this listener. */
161 const Module* GetModule() const { return prov.creator; }
163 /** Retrieves the priority of this event. */
164 unsigned int GetPriority() const { return eventpriority; }
167 inline bool Events::ModuleEventProvider::Comp::operator()(Events::ModuleEventListener* lhs, Events::ModuleEventListener* rhs) const
169 return (lhs->GetPriority() < rhs->GetPriority());
172 inline bool Events::ModuleEventProvider::ElementComp::operator()(Events::ModuleEventListener* lhs, Events::ModuleEventListener* rhs) const
174 if (lhs->GetPriority() < rhs->GetPriority())
176 if (lhs->GetPriority() > rhs->GetPriority())
178 return std::less<ModuleEventListener*>()(lhs, rhs);
182 * Run the given hook provided by a module
184 * FOREACH_MOD_CUSTOM(accountevprov, AccountEventListener, OnAccountChange, MOD_RESULT, (user, newaccount))
186 #define FOREACH_MOD_CUSTOM(prov, listenerclass, func, params) do { \
187 if (!(prov).GetModule() || !(prov).GetModule()->dying) \
189 const ::Events::ModuleEventProvider::SubscriberList& _handlers = (prov).GetSubscribers(); \
190 for (::Events::ModuleEventProvider::SubscriberList::const_iterator _i = _handlers.begin(); _i != _handlers.end(); ++_i) \
192 listenerclass* _t = static_cast<listenerclass*>(*_i); \
193 const Module* _m = _t->GetModule(); \
194 if (_m && !_m->dying) \
201 * Run the given hook provided by a module until some module returns MOD_RES_ALLOW or MOD_RES_DENY.
202 * If no module does that, result is set to MOD_RES_PASSTHRU.
204 * Example: ModResult MOD_RESULT;
205 * FIRST_MOD_RESULT_CUSTOM(httpevprov, HTTPRequestEventListener, OnHTTPRequest, MOD_RESULT, (request));
207 #define FIRST_MOD_RESULT_CUSTOM(prov, listenerclass, func, result, params) do { \
208 result = MOD_RES_PASSTHRU; \
209 if (!(prov).GetModule() || !(prov).GetModule()->dying) \
211 const ::Events::ModuleEventProvider::SubscriberList& _handlers = (prov).GetSubscribers(); \
212 for (::Events::ModuleEventProvider::SubscriberList::const_iterator _i = _handlers.begin(); _i != _handlers.end(); ++_i) \
214 listenerclass* _t = static_cast<listenerclass*>(*_i); \
215 const Module* _m = _t->GetModule(); \
216 if (!_m || _m->dying) \
218 result = _t->func params ; \
219 if (result != MOD_RES_PASSTHRU) \