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 
601  ModeHandler::Id AllocateModeId(ModeType mt);
602 
603  public:
604  typedef std::vector<ListModeBase*> ListModeList;
605  typedef std::vector<PrefixMode*> PrefixModeList;
606 
607  typedef unsigned int ModeProcessFlag;
609  {
614  MODE_NONE = 0,
615 
621  MODE_MERGE = 1,
622 
627  MODE_LOCALONLY = 2,
628 
635  MODE_CHECKACCESS = 4
636  };
637 
638  ModeParser();
639  ~ModeParser();
640 
641  static bool IsModeChar(char chr);
642 
656  static void CleanMask(std::string &mask);
657 
659  const Modes::ChangeList& GetLastChangeList() const { return LastChangeList; }
660 
664  void AddMode(ModeHandler* mh);
665 
674  bool DelMode(ModeHandler* mh);
675 
682  void AddModeWatcher(ModeWatcher* mw);
683 
691  bool DelModeWatcher(ModeWatcher* mw);
692 
702  void Process(User* user, Channel* targetchannel, User* targetuser, Modes::ChangeList& changelist, ModeProcessFlag flags = MODE_NONE);
703 
717  unsigned int ProcessSingle(User* user, Channel* targetchannel, User* targetuser, Modes::ChangeList& changelist, ModeProcessFlag flags = MODE_NONE, unsigned int beginindex = 0);
718 
734  void ModeParamsToChangeList(User* user, ModeType type, const std::vector<std::string>& parameters, Modes::ChangeList& changelist, unsigned int beginindex = 1, unsigned int endindex = UINT_MAX);
735 
741  ModeHandler* FindMode(const std::string& modename, ModeType mt);
742 
748  ModeHandler* FindMode(unsigned const char modeletter, ModeType mt);
749 
754  PrefixMode* FindPrefixMode(unsigned char modeletter);
755 
761  PrefixMode* FindPrefix(unsigned const char pfxletter);
762 
769  std::string GiveModeList(ModeType mt);
770 
774  std::string BuildPrefixes(bool lettersAndModes = true);
775 
779  const ListModeList& GetListModes() const { return mhlist.list; }
780 
784  const PrefixModeList& GetPrefixModes() const { return mhlist.prefix; }
785 
790  const ModeHandlerMap& GetModes(ModeType mt) const { return modehandlersbyname[mt]; }
791 
797  void ShowListModeList(User* user, Channel* chan, ModeHandler* mh);
798 };
799 
801 {
802  return (this->type_id == MC_PREFIX ? static_cast<PrefixMode*>(this) : NULL);
803 }
804 
806 {
807  return (this->type_id == MC_PREFIX ? static_cast<const PrefixMode*>(this) : NULL);
808 }
809 
811 {
812  return (this->type_id == MC_LIST ? reinterpret_cast<ListModeBase*>(this) : NULL);
813 }
814 
816 {
817  return (this->type_id == MC_LIST ? reinterpret_cast<const ListModeBase*>(this) : NULL);
818 }
819 
821 {
822  return (this->type_id == MC_PARAM ? reinterpret_cast<ParamModeBase*>(this) : NULL);
823 }
824 
826 {
827  return (this->type_id == MC_PARAM ? reinterpret_cast<const ParamModeBase*>(this) : NULL);
828 }
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
virtual ModResult AccessCheck(User *source, Channel *channel, std::string &parameter, bool adding)
Definition: mode.cpp:72
Definition: users.h:224
ModeProcessFlags
Definition: mode.h:608
const ModeHandlerMap & GetModes(ModeType mt) const
Definition: mode.h:790
std::vector< ListModeBase * > list
Definition: mode.h:577
ListModeBase * IsListModeBase()
Definition: mode.h:810
ParamModeBase * IsParameterMode()
Definition: mode.h:820
Definition: base.h:41
const Modes::ChangeList & GetLastChangeList() const
Definition: mode.h:659
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:240
Definition: modules.h:65
unsigned int prefixrank
Definition: mode.h:360
char GetPrefix() const
Definition: mode.h:428
PrefixMode * IsPrefixMode()
Definition: mode.h:800
void RemoveMode(User *user)
Definition: mode.cpp:830
Definition: inspircd.h:61
char mode
Definition: mode.h:121
Definition: parammode.h:22
unsigned int ranktoset
Definition: mode.h:152
const ListModeList & GetListModes() const
Definition: mode.h:779
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:784
Id GetId() const
Definition: mode.h:241
bool oper
Definition: mode.h:127
char GetModeChar() const
Definition: mode.h:236
Definition: channels.h:37