InspIRCd  3.0
modules.h
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  * Copyright (C) 2009-2010 Daniel De Graaf <[email protected]>
5  * Copyright (C) 2006-2007, 2009 Dennis Friis <[email protected]>
6  * Copyright (C) 2003-2008 Craig Edwards <[email protected]>
7  * Copyright (C) 2008 Thomas Stagner <[email protected]>
8  * Copyright (C) 2007 Robin Burchell <[email protected]>
9  * Copyright (C) 2006-2007 Oliver Lupton <[email protected]>
10  * Copyright (C) 2003 randomdan <[email protected]???>
11  *
12  * This file is part of InspIRCd. InspIRCd is free software: you can
13  * redistribute it and/or modify it under the terms of the GNU General Public
14  * License as published by the Free Software Foundation, version 2.
15  *
16  * This program is distributed in the hope that it will be useful, but WITHOUT
17  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
19  * details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program. If not, see <http://www.gnu.org/licenses/>.
23  */
24 
25 
26 #pragma once
27 
28 #include "dynamic.h"
29 #include "base.h"
30 #include "ctables.h"
31 #include "inspsocket.h"
32 #include <string>
33 #include <deque>
34 #include <sstream>
35 #include "timer.h"
36 #include "mode.h"
37 
39 enum ModuleFlags
40 {
42  VF_NONE = 0,
43 
45  VF_CORE = 1,
46 
47  /* The module is included with InspIRCd. */
48  VF_VENDOR = 2,
49 
51  VF_COMMON = 4,
52 
54  VF_OPTCOMMON = 8
55 };
56 
57 #define MOD_RES_ALLOW (ModResult(1))
58 #define MOD_RES_PASSTHRU (ModResult(0))
59 #define MOD_RES_DENY (ModResult(-1))
60 
65 struct ModResult {
66  int res;
67  ModResult() : res(0) {}
68  explicit ModResult(int r) : res(r) {}
69  inline bool operator==(const ModResult& r) const
70  {
71  return res == r.res;
72  }
73  inline bool operator!=(const ModResult& r) const
74  {
75  return res != r.res;
76  }
77  inline bool operator!() const
78  {
79  return !res;
80  }
81  inline bool check(bool def) const
82  {
83  return (res == 1 || (res == 0 && def));
84  }
88  inline ModResult operator+(const ModResult& r) const
89  {
90  if (res == r.res || r.res == 0)
91  return *this;
92  if (res == 0)
93  return r;
94  // they are different, and neither is passthru
95  return MOD_RES_ALLOW;
96  }
97 };
98 
102 #define INSPIRCD_VERSION_MAJ 300
103 
110 #define INSPIRCD_VERSION_API 6
111 
117 #define FOREACH_MOD(y,x) do { \
118  const Module::List& _handlers = ServerInstance->Modules->EventHandlers[I_ ## y]; \
119  for (Module::List::const_reverse_iterator _i = _handlers.rbegin(), _next; _i != _handlers.rend(); _i = _next) \
120  { \
121  _next = _i+1; \
122  try \
123  { \
124  (*_i)->y x ; \
125  } \
126  catch (CoreException& modexcept) \
127  { \
128  ServerInstance->Logs->Log("MODULE", LOG_DEFAULT, "Exception caught: " + modexcept.GetReason()); \
129  } \
130  } \
131 } while (0);
132 
139 #define DO_EACH_HOOK(n,v,args) \
140 do { \
141  const Module::List& _handlers = ServerInstance->Modules->EventHandlers[I_ ## n]; \
142  for (Module::List::const_reverse_iterator _i = _handlers.rbegin(), _next; _i != _handlers.rend(); _i = _next) \
143  { \
144  _next = _i+1; \
145  try \
146  { \
147  v = (*_i)->n args;
148 
149 #define WHILE_EACH_HOOK(n) \
150  } \
151  catch (CoreException& except_ ## n) \
152  { \
153  ServerInstance->Logs->Log("MODULE", LOG_DEFAULT, "Exception caught: " + (except_ ## n).GetReason()); \
154  } \
155  } \
156 } while(0)
157 
165 #define FIRST_MOD_RESULT(n,v,args) do { \
166  v = MOD_RES_PASSTHRU; \
167  DO_EACH_HOOK(n,v,args) \
168  { \
169  if (v != MOD_RES_PASSTHRU) \
170  break; \
171  } \
172  WHILE_EACH_HOOK(n); \
173 } while (0)
174 
179 class CoreExport Version
180 {
181  public:
184  const std::string description;
185 
188  const int Flags;
189 
191  const std::string link_data;
192 
194  Version(const std::string &desc, int flags = VF_NONE);
195 
197  Version(const std::string &desc, int flags, const std::string& linkdata);
198 };
199 
200 class CoreExport DataProvider : public ServiceProvider
201 {
202  public:
203  DataProvider(Module* Creator, const std::string& Name)
204  : ServiceProvider(Creator, Name, SERVICE_DATA) {}
205 };
206 
209 enum Priority { PRIORITY_FIRST, PRIORITY_LAST, PRIORITY_BEFORE, PRIORITY_AFTER };
210 
213 enum Implementation
214 {
215  I_OnUserConnect, I_OnUserQuit, I_OnUserDisconnect, I_OnUserJoin, I_OnUserPart,
216  I_OnSendSnotice, I_OnUserPreJoin, I_OnUserPreKick, I_OnUserKick, I_OnOper,
217  I_OnUserPreInvite, I_OnUserInvite, I_OnUserPreMessage, I_OnUserPreNick,
218  I_OnUserPostMessage, I_OnUserMessageBlocked, I_OnMode,
219  I_OnDecodeMetaData, I_OnAcceptConnection, I_OnUserInit, I_OnUserPostInit,
220  I_OnChangeHost, I_OnChangeRealName, I_OnAddLine, I_OnDelLine, I_OnExpireLine,
221  I_OnUserPostNick, I_OnPreMode, I_On005Numeric, I_OnKill, I_OnLoadModule,
222  I_OnUnloadModule, I_OnBackgroundTimer, I_OnPreCommand, I_OnCheckReady, I_OnCheckInvite,
223  I_OnRawMode, I_OnCheckKey, I_OnCheckLimit, I_OnCheckBan, I_OnCheckChannelBan, I_OnExtBanCheck,
224  I_OnPreChangeHost, I_OnPreTopicChange,
225  I_OnPostTopicChange, I_OnPostConnect, I_OnPostDeoper,
226  I_OnPreChangeRealName, I_OnUserRegister, I_OnChannelPreDelete, I_OnChannelDelete,
227  I_OnPostOper, I_OnPostCommand, I_OnPostJoin,
228  I_OnBuildNeighborList, I_OnGarbageCollect, I_OnSetConnectClass,
229  I_OnUserMessage, I_OnPassCompare, I_OnNumeric,
230  I_OnPreRehash, I_OnModuleRehash, I_OnChangeIdent, I_OnSetUserIP,
231  I_OnServiceAdd, I_OnServiceDel, I_OnUserWrite,
232  I_END
233 };
234 
240 class CoreExport Module : public classbase, public usecountbase
241 {
245  void DetachEvent(Implementation i);
246 
247  public:
249  typedef std::vector<Module*> List;
250 
253  std::string ModuleSourceFile;
254 
258 
262  bool dying;
263 
268  Module();
269 
273  virtual void init() {}
274 
278  CullResult cull() CXX11_OVERRIDE;
279 
283  virtual ~Module();
284 
285  virtual void Prioritize()
286  {
287  }
288 
294  virtual void ReadConfig(ConfigStatus& status);
295 
300  virtual Version GetVersion() = 0;
301 
306  virtual void OnUserConnect(LocalUser* user);
307 
316  virtual void OnUserQuit(User* user, const std::string &message, const std::string &oper_message);
317 
324  virtual void OnUserDisconnect(LocalUser* user);
325 
330  virtual ModResult OnChannelPreDelete(Channel *chan);
331 
335  virtual void OnChannelDelete(Channel* chan);
336 
346  virtual void OnUserJoin(Membership* memb, bool sync, bool created, CUList& except_list);
347 
353  virtual void OnPostJoin(Membership* memb);
354 
362  virtual void OnUserPart(Membership* memb, std::string &partmessage, CUList& except_list);
363 
374  virtual void OnPreRehash(User* user, const std::string &parameter);
375 
384  virtual void OnModuleRehash(User* user, const std::string &parameter);
385 
393  virtual ModResult OnSendSnotice(char &snomask, std::string &type, const std::string &message);
394 
415  virtual ModResult OnUserPreJoin(LocalUser* user, Channel* chan, const std::string& cname, std::string& privs, const std::string& keygiven);
416 
426  virtual ModResult OnUserPreKick(User* source, Membership* memb, const std::string &reason);
427 
436  virtual void OnUserKick(User* source, Membership* memb, const std::string &reason, CUList& except_list);
437 
444  virtual void OnOper(User* user, const std::string &opertype);
445 
454  virtual void OnPostOper(User* user, const std::string &opername, const std::string &opertype);
455 
459  virtual void OnPostDeoper(User* user);
460 
471  virtual ModResult OnUserPreInvite(User* source,User* dest,Channel* channel, time_t timeout);
472 
483  virtual void OnUserInvite(User* source, User* dest, Channel* channel, time_t timeout, unsigned int notifyrank, CUList& notifyexcepts);
484 
494  virtual ModResult OnUserPreMessage(User* user, const MessageTarget& target, MessageDetails& details);
495 
505  virtual void OnBuildNeighborList(User* source, IncludeChanList& include_c, std::map<User*, bool>& exceptions);
506 
514  virtual ModResult OnUserPreNick(LocalUser* user, const std::string& newnick);
515 
523  virtual void OnUserPostMessage(User* user, const MessageTarget& target, const MessageDetails& details);
524 
532  virtual void OnUserMessage(User* user, const MessageTarget& target, const MessageDetails& details);
533 
541  virtual void OnUserMessageBlocked(User* user, const MessageTarget& target, const MessageDetails& details);
542 
554  virtual void OnMode(User* user, User* usertarget, Channel* chantarget, const Modes::ChangeList& changelist, ModeParser::ModeProcessFlag processflags);
555 
562  virtual void OnDecodeMetaData(Extensible* target, const std::string &extname, const std::string &extdata);
563 
569  virtual void OnChangeHost(User* user, const std::string &newhost);
570 
576  virtual void OnChangeRealName(User* user, const std::string& real);
577 
583  virtual void OnChangeIdent(User* user, const std::string &ident);
584 
590  virtual void OnAddLine(User* source, XLine* line);
591 
597  virtual void OnDelLine(User* source, XLine* line);
598 
603  virtual void OnExpireLine(XLine *line);
604 
613  virtual void OnCleanup(ExtensionItem::ExtensibleType type, Extensible* item);
614 
624  virtual void OnUserPostNick(User* user, const std::string &oldnick);
625 
637  virtual ModResult OnPreMode(User* source, User* dest, Channel* channel, Modes::ChangeList& modes);
638 
643  virtual void On005Numeric(std::map<std::string, std::string>& tokens);
644 
658  virtual ModResult OnKill(User* source, User* dest, const std::string &reason);
659 
671  virtual void OnLoadModule(Module* mod);
672 
684  virtual void OnUnloadModule(Module* mod);
685 
692  virtual void OnBackgroundTimer(time_t curtime);
693 
710  virtual ModResult OnPreCommand(std::string& command, CommandBase::Params& parameters, LocalUser* user, bool validated);
711 
723  virtual void OnPostCommand(Command* command, const CommandBase::Params& parameters, LocalUser* user, CmdResult result, bool loop);
724 
731  virtual void OnUserInit(LocalUser* user);
732 
737  virtual void OnUserPostInit(LocalUser* user);
738 
749  virtual ModResult OnCheckReady(LocalUser* user);
750 
760  virtual ModResult OnUserRegister(LocalUser* user);
761 
770  virtual ModResult OnCheckInvite(User* user, Channel* chan);
771 
783  virtual ModResult OnRawMode(User* user, Channel* chan, ModeHandler* mh, const std::string& param, bool adding);
784 
795  virtual ModResult OnCheckKey(User* user, Channel* chan, const std::string &keygiven);
796 
805  virtual ModResult OnCheckLimit(User* user, Channel* chan);
806 
814  virtual ModResult OnCheckChannelBan(User* user, Channel* chan);
815 
824  virtual ModResult OnCheckBan(User* user, Channel* chan, const std::string& mask);
825 
830  virtual ModResult OnExtBanCheck(User* user, Channel* chan, char type);
831 
838  virtual ModResult OnPreChangeHost(LocalUser* user, const std::string &newhost);
839 
846  virtual ModResult OnPreChangeRealName(LocalUser* user, const std::string &newhost);
847 
856  virtual ModResult OnPreTopicChange(User* user, Channel* chan, const std::string &topic);
857 
864  virtual void OnPostTopicChange(User* user, Channel* chan, const std::string &topic);
865 
876  virtual ModResult OnPassCompare(Extensible* ex, const std::string &password, const std::string &input, const std::string& hashtype);
877 
884  virtual void OnPostConnect(User* user);
885 
893  virtual ModResult OnAcceptConnection(int fd, ListenSocket* sock, irc::sockets::sockaddrs* client, irc::sockets::sockaddrs* server);
894 
900  virtual void OnGarbageCollect();
901 
906  virtual ModResult OnSetConnectClass(LocalUser* user, ConnectClass* myclass);
907 
908  virtual ModResult OnNumeric(User* user, const Numeric::Numeric& numeric);
909 
914  virtual void OnSetUserIP(LocalUser* user);
915 
919  virtual void OnServiceAdd(ServiceProvider& service);
920 
924  virtual void OnServiceDel(ServiceProvider& service);
925 
932  virtual ModResult OnUserWrite(LocalUser* user, ClientProtocol::Message& msg);
933 };
934 
938 class CoreExport ModuleManager : public fakederef<ModuleManager>
939 {
940  public:
941  typedef std::vector<ServiceProvider*> ServiceList;
942 
943  private:
946  std::string LastModuleError;
947 
951  std::map<std::string, Module*> Modules;
952 
953  enum {
954  PRIO_STATE_FIRST,
955  PRIO_STATE_AGAIN,
956  PRIO_STATE_LAST
957  } prioritizationState;
958 
961  void LoadCoreModules(std::map<std::string, ServiceList>& servicemap);
962 
966  bool PrioritizeHooks();
967 
972  void UnregisterModes(Module* mod, ModeType modetype);
973 
974  public:
975  typedef std::map<std::string, Module*> ModuleMap;
976 
980  Module::List EventHandlers[I_END];
981 
983  std::multimap<std::string, ServiceProvider*> DataProviders;
984 
993  ServiceList* NewServices;
994 
1000  static std::string ExpandModName(const std::string& modname);
1001 
1004  ModuleManager();
1005 
1008  ~ModuleManager();
1009 
1026  bool SetPriority(Module* mod, Implementation i, Priority s, Module* which = NULL);
1027 
1036  void SetPriority(Module* mod, Priority s);
1037 
1045  bool Attach(Implementation i, Module* mod);
1046 
1054  bool Detach(Implementation i, Module* mod);
1055 
1061  void Attach(Implementation* i, Module* mod, size_t sz);
1062 
1066  void DetachAll(Module* mod);
1067 
1071  void AttachAll(Module* mod);
1072 
1076  std::string& LastError();
1077 
1083  bool Load(const std::string& filename, bool defer = false);
1084 
1090  bool Unload(Module* module);
1091 
1094  void LoadAll();
1095  void UnloadAll();
1096  void DoSafeUnload(Module*);
1097 
1103  bool CanUnload(Module* mod);
1104 
1110  Module* Find(const std::string &name);
1111 
1113  void AddService(ServiceProvider&);
1114 
1116  void DelService(ServiceProvider&);
1117 
1121  void AddServices(const ServiceList& list);
1122 
1123  inline void AddServices(ServiceProvider** list, int count)
1124  {
1125  for(int i=0; i < count; i++)
1126  AddService(*list[i]);
1127  }
1128 
1132  ServiceProvider* FindService(ServiceType Type, const std::string& name);
1133 
1134  template<typename T> inline T* FindDataService(const std::string& name)
1135  {
1136  return static_cast<T*>(FindService(SERVICE_DATA, name));
1137  }
1138 
1142  const ModuleMap& GetModules() const { return Modules; }
1143 
1148  void AddReferent(const std::string& name, ServiceProvider* service);
1149 
1153  void DelReferent(ServiceProvider* service);
1154 };
1155 
1159 #define MODULE_INIT_STR MODULE_INIT_STR_FN_2(MODULE_INIT_SYM)
1160 #define MODULE_INIT_STR_FN_2(x) MODULE_INIT_STR_FN_1(x)
1161 #define MODULE_INIT_STR_FN_1(x) #x
1162 #define MODULE_INIT_SYM MODULE_INIT_SYM_FN_2(INSPIRCD_VERSION_MAJ, INSPIRCD_VERSION_API)
1163 #define MODULE_INIT_SYM_FN_2(x,y) MODULE_INIT_SYM_FN_1(x,y)
1164 #define MODULE_INIT_SYM_FN_1(x,y) inspircd_module_ ## x ## _ ## y
1165 
1170 #define MODULE_INIT(y) \
1171  extern "C" DllExport Module * MODULE_INIT_SYM() \
1172  { \
1173  return new y; \
1174  } \
1175  extern "C" DllExport const char inspircd_src_version[] = INSPIRCD_VERSION;
Definition: modules.h:179
Definition: mode.h:95
virtual CullResult cull()
Definition: base.cpp:36
Definition: base.h:108
Definition: message.h:89
ExtensibleType
Definition: extensible.h:41
const ModuleMap & GetModules() const
Definition: modules.h:1142
std::multimap< std::string, ServiceProvider * > DataProviders
Definition: modules.h:983
Definition: numeric.h:29
Definition: extensible.h:90
std::vector< Module * > List
Definition: modules.h:249
Definition: users.h:224
bool dying
Definition: modules.h:262
ServiceList * NewServices
Definition: modules.h:993
Definition: users.h:66
Definition: ctables.h:221
Definition: users.h:707
Definition: socket.h:58
Definition: message.h:32
Definition: base.h:41
virtual void init()
Definition: modules.h:273
Definition: membership.h:32
const std::string description
Definition: modules.h:184
Definition: modules.h:938
const int Flags
Definition: modules.h:188
Definition: clientprotocol.h:180
const std::string link_data
Definition: modules.h:191
Definition: modules.h:240
Definition: modules.h:65
DLLManager * ModuleDLLManager
Definition: modules.h:257
std::string ModuleSourceFile
Definition: modules.h:253
Definition: inspircd.h:65
Definition: ctables.h:114
Definition: dynamic.h:28
Definition: modules.h:200
ModResult operator+(const ModResult &r) const
Definition: modules.h:88
Definition: xline.h:32
Definition: base.h:244
Definition: configreader.h:493
Definition: modechange.h:51
Definition: base.h:31
Definition: socket.h:141
Definition: channels.h:37