InspIRCd  3.0
modules.h
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  * Copyright (C) 2020 Matt Schatz <[email protected]>
5  * Copyright (C) 2019 iwalkalone <[email protected]>
6  * Copyright (C) 2013 Adam <[email protected]>
7  * Copyright (C) 2012-2016, 2018 Attila Molnar <[email protected]>
8  * Copyright (C) 2012-2013, 2017-2020 Sadie Powell <[email protected]>
9  * Copyright (C) 2012 Robby <[email protected]>
10  * Copyright (C) 2009-2010 Daniel De Graaf <[email protected]>
11  * Copyright (C) 2009 Uli Schlachter <[email protected]>
12  * Copyright (C) 2008 Thomas Stagner <[email protected]>
13  * Copyright (C) 2007-2009 Robin Burchell <[email protected]>
14  * Copyright (C) 2007-2008, 2010 Craig Edwards <[email protected]>
15  * Copyright (C) 2007 Oliver Lupton <[email protected]>
16  * Copyright (C) 2007 Dennis Friis <[email protected]>
17  *
18  * This file is part of InspIRCd. InspIRCd is free software: you can
19  * redistribute it and/or modify it under the terms of the GNU General Public
20  * License as published by the Free Software Foundation, version 2.
21  *
22  * This program is distributed in the hope that it will be useful, but WITHOUT
23  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
24  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
25  * details.
26  *
27  * You should have received a copy of the GNU General Public License
28  * along with this program. If not, see <http://www.gnu.org/licenses/>.
29  */
30 
31 
32 #pragma once
33 
34 #include "moduledefs.h"
35 #include "dynamic.h"
36 #include "base.h"
37 #include "ctables.h"
38 #include "inspsocket.h"
39 #include "timer.h"
40 #include "mode.h"
41 
43 enum ModuleFlags
44 {
46  VF_NONE = 0,
47 
49  VF_CORE = 1,
50 
51  /* The module is included with InspIRCd. */
52  VF_VENDOR = 2,
53 
55  VF_COMMON = 4,
56 
58  VF_OPTCOMMON = 8
59 };
60 
62 #define MOD_RES_ALLOW (ModResult(1))
63 
65 #define MOD_RES_PASSTHRU (ModResult(0))
66 
68 #define MOD_RES_DENY (ModResult(-1))
69 
71 class ModResult
72 {
73  private:
75  char result;
76 
77  public:
80  : result(0)
81  {
82  }
83 
85  explicit ModResult(char res)
86  : result(res)
87  {
88  }
89 
91  inline bool operator==(const ModResult& res) const
92  {
93  return result == res.result;
94  }
95 
97  inline bool operator!=(const ModResult& res) const
98  {
99  return result != res.result;
100  }
101 
103  inline bool operator!() const
104  {
105  return !result;
106  }
107 
109  inline bool check(bool def) const
110  {
111  return (result == 1 || (result == 0 && def));
112  }
113 
114  /* Merges two results preferring MOD_RES_ALLOW to MOD_RES_DENY. */
115  inline ModResult operator+(const ModResult& res) const
116  {
117  // If the results are identical or the other result is MOD_RES_PASSTHRU
118  // then return this result.
119  if (result == res.result || res.result == 0)
120  return *this;
121 
122  // If this result is MOD_RES_PASSTHRU then return the other result.
123  if (result == 0)
124  return res;
125 
126  // Otherwise, they are different, and neither is MOD_RES_PASSTHRU.
127  return MOD_RES_ALLOW;
128  }
129 };
130 
136 #define FOREACH_MOD(y,x) do { \
137  const Module::List& _handlers = ServerInstance->Modules->EventHandlers[I_ ## y]; \
138  for (Module::List::const_reverse_iterator _i = _handlers.rbegin(), _next; _i != _handlers.rend(); _i = _next) \
139  { \
140  _next = _i+1; \
141  try \
142  { \
143  if (!(*_i)->dying) \
144  (*_i)->y x ; \
145  } \
146  catch (CoreException& modexcept) \
147  { \
148  ServerInstance->Logs->Log("MODULE", LOG_DEFAULT, "Exception caught: " + modexcept.GetReason()); \
149  } \
150  } \
151 } while (0);
152 
159 #define DO_EACH_HOOK(n,v,args) \
160 do { \
161  const Module::List& _handlers = ServerInstance->Modules->EventHandlers[I_ ## n]; \
162  for (Module::List::const_reverse_iterator _i = _handlers.rbegin(), _next; _i != _handlers.rend(); _i = _next) \
163  { \
164  _next = _i+1; \
165  try \
166  { \
167  if (!(*_i)->dying) \
168  v = (*_i)->n args;
169 
170 #define WHILE_EACH_HOOK(n) \
171  } \
172  catch (CoreException& except_ ## n) \
173  { \
174  ServerInstance->Logs->Log("MODULE", LOG_DEFAULT, "Exception caught: " + (except_ ## n).GetReason()); \
175  } \
176  } \
177 } while(0)
178 
186 #define FIRST_MOD_RESULT(n,v,args) do { \
187  v = MOD_RES_PASSTHRU; \
188  DO_EACH_HOOK(n,v,args) \
189  { \
190  if (v != MOD_RES_PASSTHRU) \
191  break; \
192  } \
193  WHILE_EACH_HOOK(n); \
194 } while (0)
195 
200 class CoreExport Version
201 {
202  public:
205  const std::string description;
206 
209  const int Flags;
210 
212  const std::string link_data;
213 
215  Version(const std::string &desc, int flags = VF_NONE);
216 
218  Version(const std::string &desc, int flags, const std::string& linkdata);
219 };
220 
221 class CoreExport DataProvider : public ServiceProvider
222 {
223  public:
224  DataProvider(Module* Creator, const std::string& Name)
225  : ServiceProvider(Creator, Name, SERVICE_DATA) {}
226 };
227 
230 enum Priority { PRIORITY_FIRST, PRIORITY_LAST, PRIORITY_BEFORE, PRIORITY_AFTER };
231 
234 enum Implementation
235 {
236  I_On005Numeric,
237  I_OnAcceptConnection,
238  I_OnAddLine,
239  I_OnBackgroundTimer,
240  I_OnBuildNeighborList,
241  I_OnChangeHost,
242  I_OnChangeRealHost,
243  I_OnChangeIdent,
244  I_OnChangeRealName,
245  I_OnChannelDelete,
246  I_OnChannelPreDelete,
247  I_OnCheckBan,
248  I_OnCheckChannelBan,
249  I_OnCheckInvite,
250  I_OnCheckKey,
251  I_OnCheckLimit,
252  I_OnCheckReady,
253  I_OnCommandBlocked,
254  I_OnConnectionFail,
255  I_OnDecodeMetaData,
256  I_OnDelLine,
257  I_OnExpireLine,
258  I_OnExtBanCheck,
259  I_OnGarbageCollect,
260  I_OnKill,
261  I_OnLoadModule,
262  I_OnMode,
263  I_OnModuleRehash,
264  I_OnNumeric,
265  I_OnOper,
266  I_OnPassCompare,
267  I_OnPostCommand,
268  I_OnPostConnect,
269  I_OnPostDeoper,
270  I_OnPostJoin,
271  I_OnPostOper,
272  I_OnPostTopicChange,
273  I_OnPreChangeHost,
274  I_OnPreChangeRealName,
275  I_OnPreCommand,
276  I_OnPreMode,
277  I_OnPreRehash,
278  I_OnPreTopicChange,
279  I_OnRawMode,
280  I_OnSendSnotice,
281  I_OnServiceAdd,
282  I_OnServiceDel,
283  I_OnSetConnectClass,
284  I_OnSetUserIP,
285  I_OnShutdown,
286  I_OnUnloadModule,
287  I_OnUserConnect,
288  I_OnUserDisconnect,
289  I_OnUserInit,
290  I_OnUserInvite,
291  I_OnUserJoin,
292  I_OnUserKick,
293  I_OnUserMessage,
294  I_OnUserMessageBlocked,
295  I_OnUserPart,
296  I_OnUserPostInit,
297  I_OnUserPostMessage,
298  I_OnUserPostNick,
299  I_OnUserPreInvite,
300  I_OnUserPreJoin,
301  I_OnUserPreKick,
302  I_OnUserPreMessage,
303  I_OnUserPreNick,
304  I_OnUserPreQuit,
305  I_OnUserQuit,
306  I_OnUserRegister,
307  I_OnUserWrite,
308  I_END
309 };
310 
316 class CoreExport Module : public classbase, public usecountbase
317 {
321  void DetachEvent(Implementation i);
322 
323  public:
325  typedef std::vector<Module*> List;
326 
329  std::string ModuleSourceFile;
330 
334 
338  bool dying;
339 
344  Module();
345 
349  virtual void init() { }
350 
354  CullResult cull() CXX11_OVERRIDE;
355 
359  virtual ~Module();
360 
362  virtual void Prioritize() { }
363 
369  virtual void ReadConfig(ConfigStatus& status);
370 
375  virtual Version GetVersion() = 0;
376 
381  virtual void OnUserConnect(LocalUser* user);
382 
391  virtual ModResult OnUserPreQuit(LocalUser* user, std::string& message, std::string& oper_message);
392 
401  virtual void OnUserQuit(User* user, const std::string &message, const std::string &oper_message);
402 
409  virtual void OnUserDisconnect(LocalUser* user);
410 
415  virtual ModResult OnChannelPreDelete(Channel *chan);
416 
420  virtual void OnChannelDelete(Channel* chan);
421 
431  virtual void OnUserJoin(Membership* memb, bool sync, bool created, CUList& except_list);
432 
438  virtual void OnPostJoin(Membership* memb);
439 
447  virtual void OnUserPart(Membership* memb, std::string &partmessage, CUList& except_list);
448 
459  virtual void OnPreRehash(User* user, const std::string &parameter);
460 
469  virtual void OnModuleRehash(User* user, const std::string &parameter);
470 
478  virtual ModResult OnSendSnotice(char &snomask, std::string &type, const std::string &message);
479 
500  virtual ModResult OnUserPreJoin(LocalUser* user, Channel* chan, const std::string& cname, std::string& privs, const std::string& keygiven);
501 
511  virtual ModResult OnUserPreKick(User* source, Membership* memb, const std::string &reason);
512 
521  virtual void OnUserKick(User* source, Membership* memb, const std::string &reason, CUList& except_list);
522 
529  virtual void OnOper(User* user, const std::string &opertype);
530 
539  virtual void OnPostOper(User* user, const std::string &opername, const std::string &opertype);
540 
544  virtual void OnPostDeoper(User* user);
545 
556  virtual ModResult OnUserPreInvite(User* source,User* dest,Channel* channel, time_t timeout);
557 
568  virtual void OnUserInvite(User* source, User* dest, Channel* channel, time_t timeout, unsigned int notifyrank, CUList& notifyexcepts);
569 
579  virtual ModResult OnUserPreMessage(User* user, const MessageTarget& target, MessageDetails& details);
580 
590  virtual void OnBuildNeighborList(User* source, IncludeChanList& include_c, std::map<User*, bool>& exceptions);
591 
599  virtual ModResult OnUserPreNick(LocalUser* user, const std::string& newnick);
600 
608  virtual void OnUserPostMessage(User* user, const MessageTarget& target, const MessageDetails& details);
609 
617  virtual void OnUserMessage(User* user, const MessageTarget& target, const MessageDetails& details);
618 
626  virtual void OnUserMessageBlocked(User* user, const MessageTarget& target, const MessageDetails& details);
627 
639  virtual void OnMode(User* user, User* usertarget, Channel* chantarget, const Modes::ChangeList& changelist, ModeParser::ModeProcessFlag processflags);
640 
647  virtual void OnDecodeMetaData(Extensible* target, const std::string &extname, const std::string &extdata);
648 
654  virtual void OnChangeHost(User* user, const std::string &newhost);
655 
661  virtual void OnChangeRealHost(User* user, const std::string& newhost);
662 
668  virtual void OnChangeRealName(User* user, const std::string& real);
669 
675  virtual void OnChangeIdent(User* user, const std::string &ident);
676 
682  virtual void OnAddLine(User* source, XLine* line);
683 
689  virtual void OnDelLine(User* source, XLine* line);
690 
695  virtual void OnExpireLine(XLine *line);
696 
705  virtual void OnCleanup(ExtensionItem::ExtensibleType type, Extensible* item);
706 
716  virtual void OnUserPostNick(User* user, const std::string &oldnick);
717 
729  virtual ModResult OnPreMode(User* source, User* dest, Channel* channel, Modes::ChangeList& modes);
730 
735  virtual void On005Numeric(std::map<std::string, std::string>& tokens);
736 
750  virtual ModResult OnKill(User* source, User* dest, const std::string &reason);
751 
763  virtual void OnLoadModule(Module* mod);
764 
776  virtual void OnUnloadModule(Module* mod);
777 
784  virtual void OnBackgroundTimer(time_t curtime);
785 
802  virtual ModResult OnPreCommand(std::string& command, CommandBase::Params& parameters, LocalUser* user, bool validated);
803 
815  virtual void OnPostCommand(Command* command, const CommandBase::Params& parameters, LocalUser* user, CmdResult result, bool loop);
816 
822  virtual void OnCommandBlocked(const std::string& command, const CommandBase::Params& parameters, LocalUser* user);
823 
830  virtual void OnUserInit(LocalUser* user);
831 
836  virtual void OnUserPostInit(LocalUser* user);
837 
848  virtual ModResult OnCheckReady(LocalUser* user);
849 
859  virtual ModResult OnUserRegister(LocalUser* user);
860 
869  virtual ModResult OnCheckInvite(User* user, Channel* chan);
870 
882  virtual ModResult OnRawMode(User* user, Channel* chan, ModeHandler* mh, const std::string& param, bool adding);
883 
894  virtual ModResult OnCheckKey(User* user, Channel* chan, const std::string &keygiven);
895 
904  virtual ModResult OnCheckLimit(User* user, Channel* chan);
905 
913  virtual ModResult OnCheckChannelBan(User* user, Channel* chan);
914 
923  virtual ModResult OnCheckBan(User* user, Channel* chan, const std::string& mask);
924 
929  virtual ModResult OnExtBanCheck(User* user, Channel* chan, char type);
930 
937  virtual ModResult OnPreChangeHost(LocalUser* user, const std::string &newhost);
938 
945  virtual ModResult OnPreChangeRealName(LocalUser* user, const std::string &newhost);
946 
955  virtual ModResult OnPreTopicChange(User* user, Channel* chan, const std::string &topic);
956 
963  virtual void OnPostTopicChange(User* user, Channel* chan, const std::string &topic);
964 
975  virtual ModResult OnPassCompare(Extensible* ex, const std::string &password, const std::string &input, const std::string& hashtype);
976 
983  virtual void OnPostConnect(User* user);
984 
992  virtual ModResult OnAcceptConnection(int fd, ListenSocket* sock, irc::sockets::sockaddrs* client, irc::sockets::sockaddrs* server);
993 
999  virtual void OnGarbageCollect();
1000 
1005  virtual ModResult OnSetConnectClass(LocalUser* user, ConnectClass* myclass);
1006 
1007  virtual ModResult OnNumeric(User* user, const Numeric::Numeric& numeric);
1008 
1013  virtual void OnSetUserIP(LocalUser* user);
1014 
1018  virtual void OnServiceAdd(ServiceProvider& service);
1019 
1023  virtual void OnServiceDel(ServiceProvider& service);
1024 
1031  virtual ModResult OnUserWrite(LocalUser* user, ClientProtocol::Message& msg);
1032 
1039  virtual ModResult OnConnectionFail(LocalUser* user, BufferedSocketError error);
1040 
1044  virtual void OnShutdown(const std::string& reason);
1045 };
1046 
1050 class CoreExport ModuleManager : public fakederef<ModuleManager>
1051 {
1052  public:
1053  typedef std::vector<ServiceProvider*> ServiceList;
1054 
1055  private:
1058  std::string LastModuleError;
1059 
1063  std::map<std::string, Module*> Modules;
1064 
1065  enum {
1066  PRIO_STATE_FIRST,
1067  PRIO_STATE_AGAIN,
1068  PRIO_STATE_LAST
1069  } prioritizationState;
1070 
1073  void LoadCoreModules(std::map<std::string, ServiceList>& servicemap);
1074 
1078  bool PrioritizeHooks();
1079 
1084  void UnregisterModes(Module* mod, ModeType modetype);
1085 
1086  public:
1087  typedef std::map<std::string, Module*> ModuleMap;
1088 
1092  Module::List EventHandlers[I_END];
1093 
1095  std::multimap<std::string, ServiceProvider*, irc::insensitive_swo> DataProviders;
1096 
1105  ServiceList* NewServices;
1106 
1112  static std::string ExpandModName(const std::string& modname);
1113 
1116  ModuleManager();
1117 
1120  ~ModuleManager();
1121 
1138  bool SetPriority(Module* mod, Implementation i, Priority s, Module* which = NULL);
1139 
1148  void SetPriority(Module* mod, Priority s);
1149 
1157  bool Attach(Implementation i, Module* mod);
1158 
1166  bool Detach(Implementation i, Module* mod);
1167 
1173  void Attach(Implementation* i, Module* mod, size_t sz);
1174 
1178  void DetachAll(Module* mod);
1179 
1183  void AttachAll(Module* mod);
1184 
1188  std::string& LastError();
1189 
1195  bool Load(const std::string& filename, bool defer = false);
1196 
1202  bool Unload(Module* module);
1203 
1206  void LoadAll();
1207  void UnloadAll();
1208  void DoSafeUnload(Module*);
1209 
1215  bool CanUnload(Module* mod);
1216 
1222  Module* Find(const std::string &name);
1223 
1225  void AddService(ServiceProvider&);
1226 
1228  void DelService(ServiceProvider&);
1229 
1233  void AddServices(const ServiceList& list);
1234 
1235  inline void AddServices(ServiceProvider** list, int count)
1236  {
1237  for(int i=0; i < count; i++)
1238  AddService(*list[i]);
1239  }
1240 
1244  ServiceProvider* FindService(ServiceType Type, const std::string& name);
1245 
1246  template<typename T> inline T* FindDataService(const std::string& name)
1247  {
1248  return static_cast<T*>(FindService(SERVICE_DATA, name));
1249  }
1250 
1254  const ModuleMap& GetModules() const { return Modules; }
1255 
1260  void AddReferent(const std::string& name, ServiceProvider* service);
1261 
1265  void DelReferent(ServiceProvider* service);
1266 };
fakederef
Definition: inspircd.h:71
MessageTarget
Definition: message.h:94
Extensible
Definition: extensible.h:139
Module::ModuleDLLManager
DLLManager * ModuleDLLManager
Definition: modules.h:333
ModuleManager::GetModules
const ModuleMap & GetModules() const
Definition: modules.h:1254
Modes::ChangeList
Definition: modechange.h:52
Version::Flags
const int Flags
Definition: modules.h:209
Module::List
std::vector< Module * > List
Definition: modules.h:325
ServiceProvider
Definition: base.h:250
MessageDetails
Definition: message.h:33
ConnectClass
Definition: users.h:75
classbase::cull
virtual CullResult cull()
Definition: base.cpp:41
DataProvider
Definition: modules.h:221
Module::dying
bool dying
Definition: modules.h:338
irc::sockets::sockaddrs
Definition: socket.h:62
Version::link_data
const std::string link_data
Definition: modules.h:212
ModuleManager
Definition: modules.h:1050
ModuleManager::NewServices
ServiceList * NewServices
Definition: modules.h:1105
classbase
Definition: base.h:47
Version
Definition: modules.h:200
XLine
Definition: xline.h:36
Version::description
const std::string description
Definition: modules.h:205
ModResult::ModResult
ModResult(char res)
Definition: modules.h:85
Channel
Definition: channels.h:40
ModuleManager::DataProviders
std::multimap< std::string, ServiceProvider *, irc::insensitive_swo > DataProviders
Definition: modules.h:1095
ModResult::operator==
bool operator==(const ModResult &res) const
Definition: modules.h:91
ModResult::check
bool check(bool def) const
Definition: modules.h:109
ModResult
Definition: modules.h:71
ModResult::operator!
bool operator!() const
Definition: modules.h:103
ModResult::ModResult
ModResult()
Definition: modules.h:79
Module::init
virtual void init()
Definition: modules.h:349
Module
Definition: modules.h:316
User
Definition: users.h:239
Command
Definition: ctables.h:197
CullResult
Definition: base.h:37
ClientProtocol::Message
Definition: clientprotocol.h:181
DLLManager
Definition: dynamic.h:31
ExtensionItem::ExtensibleType
ExtensibleType
Definition: extensible.h:39
ListenSocket
Definition: socket.h:172
Numeric::Numeric
Definition: numeric.h:30
usecountbase
Definition: base.h:114
CommandBase::Params
Definition: ctables.h:117
Membership
Definition: membership.h:35
ConfigStatus
Definition: configreader.h:504
Module::ModuleSourceFile
std::string ModuleSourceFile
Definition: modules.h:329
ModeHandler
Definition: mode.h:99
ModResult::operator!=
bool operator!=(const ModResult &res) const
Definition: modules.h:97
LocalUser
Definition: users.h:739