InspIRCd  3.0
mode.h
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  * Copyright (C) 2009 Daniel De Graaf <[email protected]>
5  * Copyright (C) 2004-2006, 2008 Craig Edwards <[email protected]>
6  * Copyright (C) 2007 Robin Burchell <[email protected]>
7  * Copyright (C) 2007 Dennis Friis <[email protected]>
8  *
9  * This file is part of InspIRCd. InspIRCd is free software: you can
10  * redistribute it and/or modify it under the terms of the GNU General Public
11  * License as published by the Free Software Foundation, version 2.
12  *
13  * This program is distributed in the hope that it will be useful, but WITHOUT
14  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
16  * details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program. If not, see <http://www.gnu.org/licenses/>.
20  */
21 
22 
23 #pragma once
24 
25 #include "ctables.h"
26 #include "modechange.h"
27 
32 enum ModeType
33 {
35  MODETYPE_USER = 0,
37  MODETYPE_CHANNEL = 1
38 };
39 
43 enum ModeAction
44 {
45  MODEACTION_DENY = 0, /* Drop the mode change, AND a parameter if its a parameterized mode */
46  MODEACTION_ALLOW = 1 /* Allow the mode */
47 };
48 
57 enum PrefixModeValue
58 {
59  /* +v */
60  VOICE_VALUE = 10000,
61  /* +h */
62  HALFOP_VALUE = 20000,
63  /* +o */
64  OP_VALUE = 30000
65 };
66 
67 enum ParamSpec
68 {
70  PARAM_NONE,
72  PARAM_SETONLY,
74  PARAM_ALWAYS
75 };
76 
77 class PrefixMode;
78 class ListModeBase;
79 class ParamModeBase;
80 
95 class CoreExport ModeHandler : public ServiceProvider
96 {
97  public:
98  typedef size_t Id;
99 
100  enum Class
101  {
102  MC_PREFIX,
103  MC_LIST,
104  MC_PARAM,
105  MC_OTHER
106  };
107 
108  private:
111  Id modeid;
112 
113  protected:
116  ParamSpec parameters_taken;
117 
121  char mode;
122 
127  bool oper;
128 
139  bool list;
140 
145  ModeType m_type;
146 
149  const Class type_id;
150 
155 
156  public:
168  ModeHandler(Module* me, const std::string& name, char modeletter, ParamSpec params, ModeType type, Class mclass = MC_OTHER);
169  virtual CullResult cull() CXX11_OVERRIDE;
170  virtual ~ModeHandler();
171 
174  void RegisterService() CXX11_OVERRIDE;
175 
179  bool IsListMode() const { return list; }
180 
185  PrefixMode* IsPrefixMode();
186 
191  const PrefixMode* IsPrefixMode() const;
192 
197  ListModeBase* IsListModeBase();
198 
203  const ListModeBase* IsListModeBase() const;
204 
209  ParamModeBase* IsParameterMode();
210 
215  const ParamModeBase* IsParameterMode() const;
216 
220  inline ModeType GetModeType() const { return m_type; }
224  inline bool NeedsOper() const { return oper; }
230  bool NeedsParam(bool adding) const;
235  char GetModeChar() const { return mode; }
236 
240  Id GetId() const { return modeid; }
241 
244  virtual std::string GetUserParameter(const User* user) const;
245 
254  virtual ModResult AccessCheck(User* source, Channel* channel, std::string &parameter, bool adding);
255 
268  virtual ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string &parameter, bool adding); /* Can change the mode parameter as its a ref */
275  virtual void DisplayList(User* user, Channel* channel);
276 
283  virtual void OnParameterMissing(User* user, User* dest, Channel* channel);
284 
290  virtual void DisplayEmptyList(User* user, Channel* channel);
291 
302  virtual bool ResolveModeConflict(std::string &their_param, const std::string &our_param, Channel* channel);
303 
309  void RemoveMode(User* user);
310 
321  virtual void RemoveMode(Channel* channel, Modes::ChangeList& changelist);
322 
323  inline unsigned int GetLevelRequired() const { return levelrequired; }
324 
325  friend class ModeParser;
326 };
327 
342 class CoreExport PrefixMode : public ModeHandler
343 {
344  protected:
348  char prefix;
349 
353  unsigned int prefixrank;
354 
355  public:
364  PrefixMode(Module* Creator, const std::string& Name, char ModeLetter, unsigned int Rank = 0, char PrefixChar = 0);
365 
379  ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string& param, bool adding);
380 
386  void RemoveMode(Channel* channel, Modes::ChangeList& changelist);
387 
393  char GetPrefix() const { return prefix; }
394 
402  unsigned int GetPrefixRank() const { return prefixrank; }
403 };
404 
410 class CoreExport SimpleUserModeHandler : public ModeHandler
411 {
412  public:
413  SimpleUserModeHandler(Module* Creator, const std::string& Name, char modeletter)
414  : ModeHandler(Creator, Name, modeletter, PARAM_NONE, MODETYPE_USER) {}
415  virtual ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string &parameter, bool adding);
416 };
417 
423 class CoreExport SimpleChannelModeHandler : public ModeHandler
424 {
425  public:
426  SimpleChannelModeHandler(Module* Creator, const std::string& Name, char modeletter)
427  : ModeHandler(Creator, Name, modeletter, PARAM_NONE, MODETYPE_CHANNEL) {}
428  virtual ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string &parameter, bool adding);
429 };
430 
437 class CoreExport ModeWatcher : public classbase
438 {
439  private:
443  const std::string mode;
444 
448  ModeType m_type;
449 
450  public:
451  ModuleRef creator;
455  ModeWatcher(Module* creator, const std::string& modename, ModeType type);
459  virtual ~ModeWatcher();
460 
465  const std::string& GetModeName() const { return mode; }
466 
471  ModeType GetModeType() const { return m_type; }
472 
485  virtual bool BeforeMode(User* source, User* dest, Channel* channel, std::string& parameter, bool adding);
495  virtual void AfterMode(User* source, User* dest, Channel* channel, const std::string& parameter, bool adding);
496 };
497 
503 class CoreExport ModeParser : public fakederef<ModeParser>
504 {
505  public:
506  static const ModeHandler::Id MODEID_MAX = 64;
507 
510  typedef TR1NS::unordered_map<std::string, ModeHandler*, irc::insensitive, irc::StrHashComp> ModeHandlerMap;
511 
512  private:
516 
519  static const unsigned int MODETYPE_LAST = 2;
520 
526  ModeHandler* modehandlers[MODETYPE_LAST][128];
527 
530  ModeHandler* modehandlersbyid[MODETYPE_LAST][MODEID_MAX];
531 
534  ModeHandlerMap modehandlersbyname[MODETYPE_LAST];
535 
538  struct
539  {
542  std::vector<ListModeBase*> list;
543 
546  std::vector<PrefixMode*> prefix;
547  } mhlist;
548 
551  ModeWatcherMap modewatchermap;
552 
555  Modes::ChangeList LastChangeList;
556 
560  ModeAction TryMode(User* user, User* targu, Channel* targc, Modes::Change& mcitem, bool SkipACL);
561 
568  std::string CreateModeList(ModeType mt, bool needparam = false);
569 
574  void RecreateModeListFor004Numeric();
575 
580  ModeHandler::Id AllocateModeId(ModeType mt);
581 
585  std::string LastParse;
586 
589  TR1NS::array<std::string, 3> Cached004ModeList;
590 
591  public:
592  typedef std::vector<ListModeBase*> ListModeList;
593  typedef std::vector<PrefixMode*> PrefixModeList;
594 
595  typedef unsigned int ModeProcessFlag;
597  {
602  MODE_NONE = 0,
603 
609  MODE_MERGE = 1,
610 
615  MODE_LOCALONLY = 2,
616 
623  MODE_CHECKACCESS = 4
624  };
625 
626  ModeParser();
627  ~ModeParser();
628 
631  static void InitBuiltinModes();
632 
633  static bool IsModeChar(char chr);
634 
648  static void CleanMask(std::string &mask);
654  const std::string& GetLastParse() const { return LastParse; }
655 
659  void AddMode(ModeHandler* mh);
660 
669  bool DelMode(ModeHandler* mh);
670 
677  void AddModeWatcher(ModeWatcher* mw);
678 
686  bool DelModeWatcher(ModeWatcher* mw);
687 
697  void Process(User* user, Channel* targetchannel, User* targetuser, Modes::ChangeList& changelist, ModeProcessFlag flags = MODE_NONE);
698 
712  unsigned int ProcessSingle(User* user, Channel* targetchannel, User* targetuser, Modes::ChangeList& changelist, ModeProcessFlag flags = MODE_NONE, unsigned int beginindex = 0);
713 
729  void ModeParamsToChangeList(User* user, ModeType type, const std::vector<std::string>& parameters, Modes::ChangeList& changelist, unsigned int beginindex = 1, unsigned int endindex = UINT_MAX);
730 
736  ModeHandler* FindMode(const std::string& modename, ModeType mt);
737 
743  ModeHandler* FindMode(unsigned const char modeletter, ModeType mt);
744 
749  PrefixMode* FindPrefixMode(unsigned char modeletter);
750 
756  PrefixMode* FindPrefix(unsigned const char pfxletter);
757 
764  const TR1NS::array<std::string, 3>& GetModeListFor004Numeric();
765 
772  std::string GiveModeList(ModeType mt);
773 
777  std::string BuildPrefixes(bool lettersAndModes = true);
778 
782  const ListModeList& GetListModes() const { return mhlist.list; }
783 
787  const PrefixModeList& GetPrefixModes() const { return mhlist.prefix; }
788 
793  const ModeHandlerMap& GetModes(ModeType mt) const { return modehandlersbyname[mt]; }
794 
800  void ShowListModeList(User* user, Channel* chan, ModeHandler* mh);
801 };
802 
803 inline const TR1NS::array<std::string, 3>& ModeParser::GetModeListFor004Numeric()
804 {
805  return Cached004ModeList;
806 }
807 
809 {
810  return (this->type_id == MC_PREFIX ? static_cast<PrefixMode*>(this) : NULL);
811 }
812 
814 {
815  return (this->type_id == MC_PREFIX ? static_cast<const PrefixMode*>(this) : NULL);
816 }
817 
819 {
820  return (this->type_id == MC_LIST ? reinterpret_cast<ListModeBase*>(this) : NULL);
821 }
822 
824 {
825  return (this->type_id == MC_LIST ? reinterpret_cast<const ListModeBase*>(this) : NULL);
826 }
827 
829 {
830  return (this->type_id == MC_PARAM ? reinterpret_cast<ParamModeBase*>(this) : NULL);
831 }
832 
834 {
835  return (this->type_id == MC_PARAM ? reinterpret_cast<const ParamModeBase*>(this) : NULL);
836 }
Definition: mode.h:95
virtual CullResult cull()
Definition: base.cpp:36
ModeHandler(Module *me, const std::string &name, char modeletter, ParamSpec params, ModeType type, Class mclass=MC_OTHER)
Definition: mode.cpp:29
char prefix
Definition: mode.h:348
bool list
Definition: mode.h:139
Definition: mode.h:437
Definition: mode.h:410
TR1NS::unordered_map< std::string, ModeHandler *, irc::insensitive, irc::StrHashComp > ModeHandlerMap
Definition: mode.h:510
ModeType m_type
Definition: mode.h:145
const std::string & GetModeName() const
Definition: mode.h:465
Definition: mode.h:423
ModeType GetModeType() const
Definition: mode.h:220
const TR1NS::array< std::string, 3 > & GetModeListFor004Numeric()
Definition: mode.h:803
Definition: users.h:224
ModeProcessFlags
Definition: mode.h:596
const std::string & GetLastParse() const
Definition: mode.h:654
const ModeHandlerMap & GetModes(ModeType mt) const
Definition: mode.h:793
std::vector< ListModeBase * > list
Definition: mode.h:542
ListModeBase * IsListModeBase()
Definition: mode.h:818
ParamModeBase * IsParameterMode()
Definition: mode.h:828
Definition: base.h:41
ParamSpec parameters_taken
Definition: mode.h:116
Definition: modechange.h:30
Definition: mode.h:503
virtual ModeAction OnModeChange(User *source, User *dest, Channel *channel, std::string &parameter, bool adding)
Definition: mode.cpp:71
const Class type_id
Definition: mode.h:149
Definition: modules.h:245
Definition: modules.h:72
unsigned int prefixrank
Definition: mode.h:353
char GetPrefix() const
Definition: mode.h:393
PrefixMode * IsPrefixMode()
Definition: mode.h:808
void RemoveMode(User *user)
Definition: mode.cpp:840
Definition: inspircd.h:59
char mode
Definition: mode.h:121
Definition: parammode.h:22
int levelrequired
Definition: mode.h:154
const ListModeList & GetListModes() const
Definition: mode.h:782
Definition: base.h:244
Definition: mode.h:342
std::vector< PrefixMode * > prefix
Definition: mode.h:546
unsigned int GetPrefixRank() const
Definition: mode.h:402
Definition: listmode.h:24
ModeType GetModeType() const
Definition: mode.h:471
bool NeedsOper() const
Definition: mode.h:224
Definition: modechange.h:51
Definition: base.h:31
const PrefixModeList & GetPrefixModes() const
Definition: mode.h:787
Id GetId() const
Definition: mode.h:240
bool oper
Definition: mode.h:127
char GetModeChar() const
Definition: mode.h:235
Definition: channels.h:37