2 * InspIRCd -- Internet Relay Chat Daemon
4 * Copyright (C) 2015 Attila Molnar <attilamolnar@hush.com>
6 * This file is part of InspIRCd. InspIRCd is free software: you can
7 * redistribute it and/or modify it under the terms of the GNU General Public
8 * License as published by the Free Software Foundation, version 2.
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
24 class ModuleEventListener;
25 class ModuleEventProvider;
28 /** Provider of one or more cross-module events.
29 * Modules who wish to provide events for other modules create instances of this class and use
30 * one of the macros below to fire the event, passing the instance of the event provider class
32 * Event providers are identified using a unique identifier string.
34 class Events::ModuleEventProvider : public ServiceProvider, private dynamic_reference_base::CaptureHook
39 bool operator()(ModuleEventListener* one, ModuleEventListener* two) const;
42 typedef insp::flat_multiset<ModuleEventListener*, Comp, std::less<ModuleEventListener*> > SubscriberList;
45 * @param mod Module providing the event(s)
46 * @param eventid Identifier of the event or event group provided, must be unique
48 ModuleEventProvider(Module* mod, const std::string& eventid)
49 : ServiceProvider(mod, eventid, SERVICE_DATA)
52 prov.SetCaptureHook(this);
55 /** Get list of objects subscribed to this event
56 * @return List of subscribed objects
58 const SubscriberList& GetSubscribers() const { return prov->subscribers; }
60 /** Subscribes a listener to this event.
61 * @param subscriber The listener to subscribe.
63 void Subscribe(ModuleEventListener* subscriber)
65 subscribers.insert(subscriber);
66 OnSubscribe(subscriber);
69 /** Unsubscribes a listener from this event.
70 * @param subscriber The listener to unsubscribe.
72 void Unsubscribe(ModuleEventListener* subscriber)
74 subscribers.erase(subscriber);
75 OnUnsubscribe(subscriber);
79 void OnCapture() CXX11_OVERRIDE
81 // If someone else holds the list from now on, clear mine. See below for more info.
86 /** Called when a listener subscribes to this event.
87 * @param subscriber The listener which subscribed.
89 virtual void OnSubscribe(ModuleEventListener* subscriber) { }
91 /** Called when a listener unsubscribes from this event.
92 * @param subscriber The listener which unsubscribed.
94 virtual void OnUnsubscribe(ModuleEventListener* subscriber) { }
96 /** Reference to the active provider for this event. In case multiple event providers
97 * exist for the same event, only one of them contains the list of subscribers.
98 * To handle the case when we are not the ones with the list, we get it from the provider
99 * where the dynref points to.
101 dynamic_reference_nocheck<ModuleEventProvider> prov;
103 /** List of objects subscribed to the event(s) provided by us, or empty if multiple providers
104 * exist with the same name and we are not the ones holding the list.
106 SubscriberList subscribers;
109 /** Base class for abstract classes describing cross-module events.
110 * Subscribers should NOT inherit directly from this class.
112 class Events::ModuleEventListener : private dynamic_reference_base::CaptureHook
114 /** Reference to the provider, can be NULL if none of the provider modules are loaded
116 dynamic_reference_nocheck<ModuleEventProvider> prov;
118 const unsigned int eventpriority;
120 /** Called by the dynref when the event provider becomes available
122 void OnCapture() CXX11_OVERRIDE
124 prov->Subscribe(this);
128 static const unsigned int DefaultPriority = 100;
131 * @param mod Module subscribing
132 * @param eventid Identifier of the event to subscribe to
133 * @param eventprio The priority to give this event listener
135 ModuleEventListener(Module* mod, const std::string& eventid, unsigned int eventprio = DefaultPriority)
137 , eventpriority(eventprio)
139 prov.SetCaptureHook(this);
140 // If the dynamic_reference resolved at construction our capture handler wasn't called
142 ModuleEventListener::OnCapture();
145 ~ModuleEventListener()
148 prov->Unsubscribe(this);
151 /** Retrieves the module which created this listener. */
152 const Module* GetModule() const { return prov.creator; }
154 friend struct ModuleEventProvider::Comp;
157 inline bool Events::ModuleEventProvider::Comp::operator()(Events::ModuleEventListener* one, Events::ModuleEventListener* two) const
159 return (one->eventpriority < two->eventpriority);
163 * Run the given hook provided by a module
165 * FOREACH_MOD_CUSTOM(accountevprov, AccountEventListener, OnAccountChange, MOD_RESULT, (user, newaccount))
167 #define FOREACH_MOD_CUSTOM(prov, listenerclass, func, params) do { \
168 const ::Events::ModuleEventProvider::SubscriberList& _handlers = (prov).GetSubscribers(); \
169 for (::Events::ModuleEventProvider::SubscriberList::const_iterator _i = _handlers.begin(); _i != _handlers.end(); ++_i) \
171 listenerclass* _t = static_cast<listenerclass*>(*_i); \
172 const Module* _m = _t->GetModule(); \
173 if (_m && !_m->dying) \
179 * Run the given hook provided by a module until some module returns MOD_RES_ALLOW or MOD_RES_DENY.
180 * If no module does that, result is set to MOD_RES_PASSTHRU.
182 * Example: ModResult MOD_RESULT;
183 * FIRST_MOD_RESULT_CUSTOM(httpevprov, HTTPRequestEventListener, OnHTTPRequest, MOD_RESULT, (request));
185 #define FIRST_MOD_RESULT_CUSTOM(prov, listenerclass, func, result, params) do { \
186 result = MOD_RES_PASSTHRU; \
187 const ::Events::ModuleEventProvider::SubscriberList& _handlers = (prov).GetSubscribers(); \
188 for (::Events::ModuleEventProvider::SubscriberList::const_iterator _i = _handlers.begin(); _i != _handlers.end(); ++_i) \
190 listenerclass* _t = static_cast<listenerclass*>(*_i); \
191 const Module* _m = _t->GetModule(); \
192 if (!_m || _m->dying) \
194 result = _t->func params ; \
195 if (result != MOD_RES_PASSTHRU) \