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 2
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,
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_OnNamesListItem, 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 
728  virtual void OnUserInit(LocalUser* user);
729 
740  virtual ModResult OnCheckReady(LocalUser* user);
741 
751  virtual ModResult OnUserRegister(LocalUser* user);
752 
761  virtual ModResult OnCheckInvite(User* user, Channel* chan);
762 
774  virtual ModResult OnRawMode(User* user, Channel* chan, ModeHandler* mh, const std::string& param, bool adding);
775 
786  virtual ModResult OnCheckKey(User* user, Channel* chan, const std::string &keygiven);
787 
796  virtual ModResult OnCheckLimit(User* user, Channel* chan);
797 
805  virtual ModResult OnCheckChannelBan(User* user, Channel* chan);
806 
815  virtual ModResult OnCheckBan(User* user, Channel* chan, const std::string& mask);
816 
821  virtual ModResult OnExtBanCheck(User* user, Channel* chan, char type);
822 
829  virtual ModResult OnPreChangeHost(LocalUser* user, const std::string &newhost);
830 
837  virtual ModResult OnPreChangeRealName(LocalUser* user, const std::string &newhost);
838 
847  virtual ModResult OnPreTopicChange(User* user, Channel* chan, const std::string &topic);
848 
855  virtual void OnPostTopicChange(User* user, Channel* chan, const std::string &topic);
856 
867  virtual ModResult OnPassCompare(Extensible* ex, const std::string &password, const std::string &input, const std::string& hashtype);
868 
875  virtual void OnPostConnect(User* user);
876 
884  virtual ModResult OnAcceptConnection(int fd, ListenSocket* sock, irc::sockets::sockaddrs* client, irc::sockets::sockaddrs* server);
885 
891  virtual void OnGarbageCollect();
892 
897  virtual ModResult OnSetConnectClass(LocalUser* user, ConnectClass* myclass);
898 
909  virtual ModResult OnNamesListItem(User* issuer, Membership* item, std::string& prefixes, std::string& nick);
910 
911  virtual ModResult OnNumeric(User* user, const Numeric::Numeric& numeric);
912 
917  virtual void OnSetUserIP(LocalUser* user);
918 
922  virtual void OnServiceAdd(ServiceProvider& service);
923 
927  virtual void OnServiceDel(ServiceProvider& service);
928 
929  virtual ModResult OnUserWrite(LocalUser* user, ClientProtocol::Message& msg);
930 };
931 
935 class CoreExport ModuleManager : public fakederef<ModuleManager>
936 {
937  public:
938  typedef std::vector<ServiceProvider*> ServiceList;
939 
940  private:
943  std::string LastModuleError;
944 
948  std::map<std::string, Module*> Modules;
949 
950  enum {
951  PRIO_STATE_FIRST,
952  PRIO_STATE_AGAIN,
953  PRIO_STATE_LAST
954  } prioritizationState;
955 
958  void LoadCoreModules(std::map<std::string, ServiceList>& servicemap);
959 
963  bool PrioritizeHooks();
964 
969  void UnregisterModes(Module* mod, ModeType modetype);
970 
971  public:
972  typedef std::map<std::string, Module*> ModuleMap;
973 
977  Module::List EventHandlers[I_END];
978 
980  std::multimap<std::string, ServiceProvider*> DataProviders;
981 
990  ServiceList* NewServices;
991 
997  static std::string ExpandModName(const std::string& modname);
998 
1001  ModuleManager();
1002 
1005  ~ModuleManager();
1006 
1023  bool SetPriority(Module* mod, Implementation i, Priority s, Module* which = NULL);
1024 
1033  void SetPriority(Module* mod, Priority s);
1034 
1042  bool Attach(Implementation i, Module* mod);
1043 
1051  bool Detach(Implementation i, Module* mod);
1052 
1058  void Attach(Implementation* i, Module* mod, size_t sz);
1059 
1063  void DetachAll(Module* mod);
1064 
1068  void AttachAll(Module* mod);
1069 
1073  std::string& LastError();
1074 
1080  bool Load(const std::string& filename, bool defer = false);
1081 
1087  bool Unload(Module* module);
1088 
1091  void LoadAll();
1092  void UnloadAll();
1093  void DoSafeUnload(Module*);
1094 
1100  bool CanUnload(Module* mod);
1101 
1107  Module* Find(const std::string &name);
1108 
1110  void AddService(ServiceProvider&);
1111 
1113  void DelService(ServiceProvider&);
1114 
1118  void AddServices(const ServiceList& list);
1119 
1120  inline void AddServices(ServiceProvider** list, int count)
1121  {
1122  for(int i=0; i < count; i++)
1123  AddService(*list[i]);
1124  }
1125 
1129  ServiceProvider* FindService(ServiceType Type, const std::string& name);
1130 
1131  template<typename T> inline T* FindDataService(const std::string& name)
1132  {
1133  return static_cast<T*>(FindService(SERVICE_DATA, name));
1134  }
1135 
1139  const ModuleMap& GetModules() const { return Modules; }
1140 
1145  void AddReferent(const std::string& name, ServiceProvider* service);
1146 
1150  void DelReferent(ServiceProvider* service);
1151 };
1152 
1156 #define MODULE_INIT_STR MODULE_INIT_STR_FN_2(MODULE_INIT_SYM)
1157 #define MODULE_INIT_STR_FN_2(x) MODULE_INIT_STR_FN_1(x)
1158 #define MODULE_INIT_STR_FN_1(x) #x
1159 #define MODULE_INIT_SYM MODULE_INIT_SYM_FN_2(INSPIRCD_VERSION_MAJ, INSPIRCD_VERSION_API)
1160 #define MODULE_INIT_SYM_FN_2(x,y) MODULE_INIT_SYM_FN_1(x,y)
1161 #define MODULE_INIT_SYM_FN_1(x,y) inspircd_module_ ## x ## _ ## y
1162 
1167 #define MODULE_INIT(y) \
1168  extern "C" DllExport Module * MODULE_INIT_SYM() \
1169  { \
1170  return new y; \
1171  } \
1172  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:1139
std::multimap< std::string, ServiceProvider * > DataProviders
Definition: modules.h:980
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:990
Definition: users.h:66
Definition: ctables.h:221
Definition: users.h:708
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:935
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:61
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:134
Definition: channels.h:37