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 
152  unsigned int ranktoset;
153 
155  unsigned int ranktounset;
156 
157  public:
169  ModeHandler(Module* me, const std::string& name, char modeletter, ParamSpec params, ModeType type, Class mclass = MC_OTHER);
170  CullResult cull() CXX11_OVERRIDE;
171  virtual ~ModeHandler();
172 
175  void RegisterService() CXX11_OVERRIDE;
176 
180  bool IsListMode() const { return list; }
181 
186  PrefixMode* IsPrefixMode();
187 
192  const PrefixMode* IsPrefixMode() const;
193 
198  ListModeBase* IsListModeBase();
199 
204  const ListModeBase* IsListModeBase() const;
205 
210  ParamModeBase* IsParameterMode();
211 
216  const ParamModeBase* IsParameterMode() const;
217 
221  inline ModeType GetModeType() const { return m_type; }
225  inline bool NeedsOper() const { return oper; }
231  bool NeedsParam(bool adding) const;
236  char GetModeChar() const { return mode; }
237 
241  Id GetId() const { return modeid; }
242 
245  virtual std::string GetUserParameter(const User* user) const;
246 
255  virtual ModResult AccessCheck(User* source, Channel* channel, std::string &parameter, bool adding);
256 
269  virtual ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string &parameter, bool adding); /* Can change the mode parameter as its a ref */
276  virtual void DisplayList(User* user, Channel* channel);
277 
284  virtual void OnParameterMissing(User* user, User* dest, Channel* channel);
285 
291  virtual void DisplayEmptyList(User* user, Channel* channel);
292 
303  virtual bool ResolveModeConflict(std::string &their_param, const std::string &our_param, Channel* channel);
304 
310  void RemoveMode(User* user);
311 
322  virtual void RemoveMode(Channel* channel, Modes::ChangeList& changelist);
323 
327  inline unsigned int GetLevelRequired(bool adding) const
328  {
329  return adding ? ranktoset : ranktounset;
330  }
331 
332  friend class ModeParser;
333 };
334 
349 class CoreExport PrefixMode : public ModeHandler
350 {
351  protected:
355  char prefix;
356 
360  unsigned int prefixrank;
361 
364 
365  public:
374  PrefixMode(Module* Creator, const std::string& Name, char ModeLetter, unsigned int Rank = 0, char PrefixChar = 0);
375 
384  ModResult AccessCheck(User* source, Channel* channel, std::string &parameter, bool adding) CXX11_OVERRIDE;
385 
399  ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string& param, bool adding) CXX11_OVERRIDE;
400 
408  void Update(unsigned int rank, unsigned int setrank, unsigned int unsetrank, bool selfrm);
409 
415  void RemoveMode(Channel* channel, Modes::ChangeList& changelist) CXX11_OVERRIDE;
416 
417 
421  bool CanSelfRemove() const { return selfremove; }
422 
428  char GetPrefix() const { return prefix; }
429 
437  unsigned int GetPrefixRank() const { return prefixrank; }
438 };
439 
445 class CoreExport SimpleUserModeHandler : public ModeHandler
446 {
447  public:
448  SimpleUserModeHandler(Module* Creator, const std::string& Name, char modeletter)
449  : ModeHandler(Creator, Name, modeletter, PARAM_NONE, MODETYPE_USER) {}
450  ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string& parameter, bool adding) CXX11_OVERRIDE;
451 };
452 
458 class CoreExport SimpleChannelModeHandler : public ModeHandler
459 {
460  public:
461  SimpleChannelModeHandler(Module* Creator, const std::string& Name, char modeletter)
462  : ModeHandler(Creator, Name, modeletter, PARAM_NONE, MODETYPE_CHANNEL) {}
463  ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string& parameter, bool adding) CXX11_OVERRIDE;
464 };
465 
472 class CoreExport ModeWatcher : public classbase
473 {
474  private:
478  const std::string mode;
479 
483  ModeType m_type;
484 
485  public:
486  ModuleRef creator;
490  ModeWatcher(Module* creator, const std::string& modename, ModeType type);
494  virtual ~ModeWatcher();
495 
500  const std::string& GetModeName() const { return mode; }
501 
506  ModeType GetModeType() const { return m_type; }
507 
520  virtual bool BeforeMode(User* source, User* dest, Channel* channel, std::string& parameter, bool adding);
530  virtual void AfterMode(User* source, User* dest, Channel* channel, const std::string& parameter, bool adding);
531 };
532 
538 class CoreExport ModeParser : public fakederef<ModeParser>
539 {
540  public:
541  static const ModeHandler::Id MODEID_MAX = 64;
542 
545  typedef TR1NS::unordered_map<std::string, ModeHandler*, irc::insensitive, irc::StrHashComp> ModeHandlerMap;
546 
547  private:
551 
554  static const unsigned int MODETYPE_LAST = 2;
555 
561  ModeHandler* modehandlers[MODETYPE_LAST][128];
562 
565  ModeHandler* modehandlersbyid[MODETYPE_LAST][MODEID_MAX];
566 
569  ModeHandlerMap modehandlersbyname[MODETYPE_LAST];
570 
573  struct
574  {
577  std::vector<ListModeBase*> list;
578 
581  std::vector<PrefixMode*> prefix;
582  } mhlist;
583 
586  ModeWatcherMap modewatchermap;
587 
590  Modes::ChangeList LastChangeList;
591 
595  ModeAction TryMode(User* user, User* targu, Channel* targc, Modes::Change& mcitem, bool SkipACL);
596 
603  std::string CreateModeList(ModeType mt, bool needparam = false);
604 
609  void RecreateModeListFor004Numeric();
610 
615  ModeHandler::Id AllocateModeId(ModeType mt);
616 
620  std::string LastParse;
621 
624  TR1NS::array<std::string, 3> Cached004ModeList;
625 
626  public:
627  typedef std::vector<ListModeBase*> ListModeList;
628  typedef std::vector<PrefixMode*> PrefixModeList;
629 
630  typedef unsigned int ModeProcessFlag;
632  {
637  MODE_NONE = 0,
638 
644  MODE_MERGE = 1,
645 
650  MODE_LOCALONLY = 2,
651 
658  MODE_CHECKACCESS = 4
659  };
660 
661  ModeParser();
662  ~ModeParser();
663 
664  static bool IsModeChar(char chr);
665 
679  static void CleanMask(std::string &mask);
685  const std::string& GetLastParse() const { return LastParse; }
686 
690  void AddMode(ModeHandler* mh);
691 
700  bool DelMode(ModeHandler* mh);
701 
708  void AddModeWatcher(ModeWatcher* mw);
709 
717  bool DelModeWatcher(ModeWatcher* mw);
718 
728  void Process(User* user, Channel* targetchannel, User* targetuser, Modes::ChangeList& changelist, ModeProcessFlag flags = MODE_NONE);
729 
743  unsigned int ProcessSingle(User* user, Channel* targetchannel, User* targetuser, Modes::ChangeList& changelist, ModeProcessFlag flags = MODE_NONE, unsigned int beginindex = 0);
744 
760  void ModeParamsToChangeList(User* user, ModeType type, const std::vector<std::string>& parameters, Modes::ChangeList& changelist, unsigned int beginindex = 1, unsigned int endindex = UINT_MAX);
761 
767  ModeHandler* FindMode(const std::string& modename, ModeType mt);
768 
774  ModeHandler* FindMode(unsigned const char modeletter, ModeType mt);
775 
780  PrefixMode* FindPrefixMode(unsigned char modeletter);
781 
787  PrefixMode* FindPrefix(unsigned const char pfxletter);
788 
795  const TR1NS::array<std::string, 3>& GetModeListFor004Numeric();
796 
803  std::string GiveModeList(ModeType mt);
804 
808  std::string BuildPrefixes(bool lettersAndModes = true);
809 
813  const ListModeList& GetListModes() const { return mhlist.list; }
814 
818  const PrefixModeList& GetPrefixModes() const { return mhlist.prefix; }
819 
824  const ModeHandlerMap& GetModes(ModeType mt) const { return modehandlersbyname[mt]; }
825 
831  void ShowListModeList(User* user, Channel* chan, ModeHandler* mh);
832 };
833 
834 inline const TR1NS::array<std::string, 3>& ModeParser::GetModeListFor004Numeric()
835 {
836  return Cached004ModeList;
837 }
838 
840 {
841  return (this->type_id == MC_PREFIX ? static_cast<PrefixMode*>(this) : NULL);
842 }
843 
845 {
846  return (this->type_id == MC_PREFIX ? static_cast<const PrefixMode*>(this) : NULL);
847 }
848 
850 {
851  return (this->type_id == MC_LIST ? reinterpret_cast<ListModeBase*>(this) : NULL);
852 }
853 
855 {
856  return (this->type_id == MC_LIST ? reinterpret_cast<const ListModeBase*>(this) : NULL);
857 }
858 
860 {
861  return (this->type_id == MC_PARAM ? reinterpret_cast<ParamModeBase*>(this) : NULL);
862 }
863 
865 {
866  return (this->type_id == MC_PARAM ? reinterpret_cast<const ParamModeBase*>(this) : NULL);
867 }
Definition: mode.h:95
virtual CullResult cull()
Definition: base.cpp:36
bool CanSelfRemove() const
Definition: mode.h:421
unsigned int ranktounset
Definition: mode.h:155
bool selfremove
Definition: mode.h:363
ModeHandler(Module *me, const std::string &name, char modeletter, ParamSpec params, ModeType type, Class mclass=MC_OTHER)
Definition: mode.cpp:28
char prefix
Definition: mode.h:355
bool list
Definition: mode.h:139
Definition: mode.h:472
Definition: mode.h:445
TR1NS::unordered_map< std::string, ModeHandler *, irc::insensitive, irc::StrHashComp > ModeHandlerMap
Definition: mode.h:545
ModeType m_type
Definition: mode.h:145
const std::string & GetModeName() const
Definition: mode.h:500
Definition: mode.h:458
unsigned int GetLevelRequired(bool adding) const
Definition: mode.h:327
ModeType GetModeType() const
Definition: mode.h:221
const TR1NS::array< std::string, 3 > & GetModeListFor004Numeric()
Definition: mode.h:834
virtual ModResult AccessCheck(User *source, Channel *channel, std::string &parameter, bool adding)
Definition: mode.cpp:72
Definition: users.h:224
ModeProcessFlags
Definition: mode.h:631
const std::string & GetLastParse() const
Definition: mode.h:685
const ModeHandlerMap & GetModes(ModeType mt) const
Definition: mode.h:824
std::vector< ListModeBase * > list
Definition: mode.h:577
ListModeBase * IsListModeBase()
Definition: mode.h:849
ParamModeBase * IsParameterMode()
Definition: mode.h:859
Definition: base.h:41
ParamSpec parameters_taken
Definition: mode.h:116
Definition: modechange.h:30
Definition: mode.h:538
virtual ModeAction OnModeChange(User *source, User *dest, Channel *channel, std::string &parameter, bool adding)
Definition: mode.cpp:77
const Class type_id
Definition: mode.h:149
Definition: modules.h:239
Definition: modules.h:65
unsigned int prefixrank
Definition: mode.h:360
char GetPrefix() const
Definition: mode.h:428
PrefixMode * IsPrefixMode()
Definition: mode.h:839
void RemoveMode(User *user)
Definition: mode.cpp:874
Definition: inspircd.h:60
char mode
Definition: mode.h:121
Definition: parammode.h:22
unsigned int ranktoset
Definition: mode.h:152
const ListModeList & GetListModes() const
Definition: mode.h:813
Definition: base.h:244
Definition: mode.h:349
std::vector< PrefixMode * > prefix
Definition: mode.h:581
unsigned int GetPrefixRank() const
Definition: mode.h:437
Definition: listmode.h:24
ModeType GetModeType() const
Definition: mode.h:506
bool NeedsOper() const
Definition: mode.h:225
Definition: modechange.h:51
Definition: base.h:31
const PrefixModeList & GetPrefixModes() const
Definition: mode.h:818
Id GetId() const
Definition: mode.h:241
bool oper
Definition: mode.h:127
char GetModeChar() const
Definition: mode.h:236
Definition: channels.h:37