InspIRCd  3.0
mode.h
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  * Copyright (C) 2013-2016 Attila Molnar <[email protected]>
5  * Copyright (C) 2012-2013, 2017-2021 Sadie Powell <[email protected]>
6  * Copyright (C) 2012 Robby <[email protected]>
7  * Copyright (C) 2009 Daniel De Graaf <[email protected]>
8  * Copyright (C) 2008 Thomas Stagner <[email protected]>
9  * Copyright (C) 2007-2008 Robin Burchell <[email protected]>
10  * Copyright (C) 2007 Dennis Friis <[email protected]>
11  * Copyright (C) 2006-2008, 2010 Craig Edwards <[email protected]>
12  *
13  * This file is part of InspIRCd. InspIRCd is free software: you can
14  * redistribute it and/or modify it under the terms of the GNU General Public
15  * License as published by the Free Software Foundation, version 2.
16  *
17  * This program is distributed in the hope that it will be useful, but WITHOUT
18  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
19  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
20  * details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program. If not, see <http://www.gnu.org/licenses/>.
24  */
25 
26 
27 #pragma once
28 
29 #include "ctables.h"
30 #include "modechange.h"
31 
36 enum ModeType
37 {
39  MODETYPE_USER = 0,
41  MODETYPE_CHANNEL = 1
42 };
43 
47 enum ModeAction
48 {
49  MODEACTION_DENY = 0, /* Drop the mode change, AND a parameter if its a parameterized mode */
50  MODEACTION_ALLOW = 1 /* Allow the mode */
51 };
52 
61 enum PrefixModeValue
62 {
63  /* +v */
64  VOICE_VALUE = 10000,
65  /* +h */
66  HALFOP_VALUE = 20000,
67  /* +o */
68  OP_VALUE = 30000
69 };
70 
71 enum ParamSpec
72 {
74  PARAM_NONE,
76  PARAM_SETONLY,
78  PARAM_ALWAYS
79 };
80 
81 class PrefixMode;
82 class ListModeBase;
83 class ParamModeBase;
84 
99 class CoreExport ModeHandler : public ServiceProvider
100 {
101  public:
102  typedef size_t Id;
103 
104  enum Class
105  {
106  MC_PREFIX,
107  MC_LIST,
108  MC_PARAM,
109  MC_OTHER
110  };
111 
112  private:
115  Id modeid;
116 
117  protected:
120  ParamSpec parameters_taken;
121 
125  char mode;
126 
131  bool oper;
132 
143  bool list;
144 
149  ModeType m_type;
150 
153  const Class type_id;
154 
156  unsigned int ranktoset;
157 
159  unsigned int ranktounset;
160 
162  std::string syntax;
163 
164  public:
176  ModeHandler(Module* me, const std::string& name, char modeletter, ParamSpec params, ModeType type, Class mclass = MC_OTHER);
177  CullResult cull() CXX11_OVERRIDE;
178  virtual ~ModeHandler();
179 
182  void RegisterService() CXX11_OVERRIDE;
183 
187  bool IsListMode() const { return list; }
188 
193  PrefixMode* IsPrefixMode();
194 
199  const PrefixMode* IsPrefixMode() const;
200 
205  ListModeBase* IsListModeBase();
206 
211  const ListModeBase* IsListModeBase() const;
212 
217  ParamModeBase* IsParameterMode();
218 
223  const ParamModeBase* IsParameterMode() const;
224 
228  inline ModeType GetModeType() const { return m_type; }
232  inline bool NeedsOper() const { return oper; }
238  bool NeedsParam(bool adding) const;
243  char GetModeChar() const { return mode; }
244 
248  Id GetId() const { return modeid; }
249 
252  virtual std::string GetUserParameter(const User* user) const;
253 
262  virtual ModResult AccessCheck(User* source, Channel* channel, std::string &parameter, bool adding);
263 
276  virtual ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string &parameter, bool adding); /* Can change the mode parameter as its a ref */
277 
284  virtual void DisplayList(User* user, Channel* channel);
285 
292  virtual void OnParameterMissing(User* user, User* dest, Channel* channel);
293 
300  virtual void OnParameterInvalid(User* user, Channel* targetchannel, User* targetuser, const std::string& parameter);
301 
302 
308  virtual void DisplayEmptyList(User* user, Channel* channel);
309 
320  virtual bool ResolveModeConflict(std::string &their_param, const std::string &our_param, Channel* channel);
321 
327  void RemoveMode(User* user);
328 
339  virtual void RemoveMode(Channel* channel, Modes::ChangeList& changelist);
340 
344  inline unsigned int GetLevelRequired(bool adding) const
345  {
346  return adding ? ranktoset : ranktounset;
347  }
348 
350  const std::string& GetSyntax() const { return syntax; }
351 
352  friend class ModeParser;
353 };
354 
369 class CoreExport PrefixMode : public ModeHandler
370 {
371  protected:
375  char prefix;
376 
380  unsigned int prefixrank;
381 
384 
385  public:
394  PrefixMode(Module* Creator, const std::string& Name, char ModeLetter, unsigned int Rank = 0, char PrefixChar = 0);
395 
404  ModResult AccessCheck(User* source, Channel* channel, std::string &parameter, bool adding) CXX11_OVERRIDE;
405 
419  ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string& param, bool adding) CXX11_OVERRIDE;
420 
428  void Update(unsigned int rank, unsigned int setrank, unsigned int unsetrank, bool selfrm);
429 
435  void RemoveMode(Channel* channel, Modes::ChangeList& changelist) CXX11_OVERRIDE;
436 
437 
441  bool CanSelfRemove() const { return selfremove; }
442 
448  char GetPrefix() const { return prefix; }
449 
457  unsigned int GetPrefixRank() const { return prefixrank; }
458 };
459 
465 class CoreExport SimpleUserModeHandler : public ModeHandler
466 {
467  public:
468  SimpleUserModeHandler(Module* Creator, const std::string& Name, char modeletter, bool operonly = false)
469  : ModeHandler(Creator, Name, modeletter, PARAM_NONE, MODETYPE_USER)
470  {
471  oper = operonly;
472  }
473 
474  ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string& parameter, bool adding) CXX11_OVERRIDE;
475 };
476 
482 class CoreExport SimpleChannelModeHandler : public ModeHandler
483 {
484  public:
485  SimpleChannelModeHandler(Module* Creator, const std::string& Name, char modeletter, bool operonly = false)
486  : ModeHandler(Creator, Name, modeletter, PARAM_NONE, MODETYPE_CHANNEL)
487  {
488  oper = operonly;
489  }
490 
491  ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string& parameter, bool adding) CXX11_OVERRIDE;
492 };
493 
500 class CoreExport ModeWatcher : public classbase
501 {
502  private:
506  const std::string mode;
507 
511  ModeType m_type;
512 
513  public:
518  ModeWatcher(Module* creator, const std::string& modename, ModeType type);
522  virtual ~ModeWatcher();
523 
528  const std::string& GetModeName() const { return mode; }
529 
534  ModeType GetModeType() const { return m_type; }
535 
548  virtual bool BeforeMode(User* source, User* dest, Channel* channel, std::string& parameter, bool adding);
558  virtual void AfterMode(User* source, User* dest, Channel* channel, const std::string& parameter, bool adding);
559 };
560 
566 class CoreExport ModeParser : public fakederef<ModeParser>
567 {
568  public:
570  static const ModeHandler::Id MODEID_MAX = 64;
571 
573  static const size_t MODE_PARAM_MAX = 250;
574 
577  typedef TR1NS::unordered_map<std::string, ModeHandler*, irc::insensitive, irc::StrHashComp> ModeHandlerMap;
578 
579  private:
583 
586  static const unsigned int MODETYPE_LAST = 2;
587 
593  ModeHandler* modehandlers[MODETYPE_LAST][128];
594 
597  ModeHandler* modehandlersbyid[MODETYPE_LAST][MODEID_MAX];
598 
601  ModeHandlerMap modehandlersbyname[MODETYPE_LAST];
602 
605  struct
606  {
609  std::vector<ListModeBase*> list;
610 
613  std::vector<PrefixMode*> prefix;
614  } mhlist;
615 
618  ModeWatcherMap modewatchermap;
619 
622  Modes::ChangeList LastChangeList;
623 
631  ModeAction TryMode(User* user, User* usertarget, Channel* chantarget, Modes::Change& mcitem, bool skipacl);
632 
637  ModeHandler::Id AllocateModeId(ModeType mt);
638 
639  public:
640  typedef std::vector<ListModeBase*> ListModeList;
641  typedef std::vector<PrefixMode*> PrefixModeList;
642 
643  typedef unsigned int ModeProcessFlag;
645  {
650  MODE_NONE = 0,
651 
657  MODE_MERGE = 1,
658 
663  MODE_LOCALONLY = 2,
664 
671  MODE_CHECKACCESS = 4
672  };
673 
674  ModeParser();
675  ~ModeParser();
676 
677  static bool IsModeChar(char chr);
678 
692  static void CleanMask(std::string &mask);
693 
695  const Modes::ChangeList& GetLastChangeList() const { return LastChangeList; }
696 
700  void AddMode(ModeHandler* mh);
701 
710  bool DelMode(ModeHandler* mh);
711 
718  void AddModeWatcher(ModeWatcher* mw);
719 
727  bool DelModeWatcher(ModeWatcher* mw);
728 
738  void Process(User* user, Channel* targetchannel, User* targetuser, Modes::ChangeList& changelist, ModeProcessFlag flags = MODE_NONE);
739 
753  unsigned int ProcessSingle(User* user, Channel* targetchannel, User* targetuser, Modes::ChangeList& changelist, ModeProcessFlag flags = MODE_NONE, unsigned int beginindex = 0);
754 
770  void ModeParamsToChangeList(User* user, ModeType type, const std::vector<std::string>& parameters, Modes::ChangeList& changelist, unsigned int beginindex = 1, unsigned int endindex = UINT_MAX);
771 
777  ModeHandler* FindMode(const std::string& modename, ModeType mt);
778 
784  ModeHandler* FindMode(unsigned const char modeletter, ModeType mt);
785 
790  PrefixMode* FindPrefixMode(unsigned char modeletter);
791 
797  PrefixMode* FindPrefix(unsigned const char pfxletter);
798 
805  std::string GiveModeList(ModeType mt);
806 
810  std::string BuildPrefixes(bool lettersAndModes = true);
811 
815  const ListModeList& GetListModes() const { return mhlist.list; }
816 
820  const PrefixModeList& GetPrefixModes() const { return mhlist.prefix; }
821 
826  const ModeHandlerMap& GetModes(ModeType mt) const { return modehandlersbyname[mt]; }
827 
833  void ShowListModeList(User* user, Channel* chan, ModeHandler* mh);
834 };
835 
837 {
838  return (this->type_id == MC_PREFIX ? static_cast<PrefixMode*>(this) : NULL);
839 }
840 
842 {
843  return (this->type_id == MC_PREFIX ? static_cast<const PrefixMode*>(this) : NULL);
844 }
845 
847 {
848  return (this->type_id == MC_LIST ? reinterpret_cast<ListModeBase*>(this) : NULL);
849 }
850 
852 {
853  return (this->type_id == MC_LIST ? reinterpret_cast<const ListModeBase*>(this) : NULL);
854 }
855 
857 {
858  return (this->type_id == MC_PARAM ? reinterpret_cast<ParamModeBase*>(this) : NULL);
859 }
860 
862 {
863  return (this->type_id == MC_PARAM ? reinterpret_cast<const ParamModeBase*>(this) : NULL);
864 }
Definition: channels.h:41
Definition: base.h:36
Definition: listmode.h:27
Definition: modules.h:72
Definition: mode.h:100
ListModeBase * IsListModeBase()
Definition: mode.h:846
ParamSpec parameters_taken
Definition: mode.h:120
ParamModeBase * IsParameterMode()
Definition: mode.h:856
char GetModeChar() const
Definition: mode.h:243
std::string syntax
Definition: mode.h:162
const std::string & GetSyntax() const
Definition: mode.h:350
bool list
Definition: mode.h:143
ModeType m_type
Definition: mode.h:149
unsigned int ranktounset
Definition: mode.h:159
char mode
Definition: mode.h:125
Id GetId() const
Definition: mode.h:248
const Class type_id
Definition: mode.h:153
bool NeedsOper() const
Definition: mode.h:232
unsigned int GetLevelRequired(bool adding) const
Definition: mode.h:344
ModeType GetModeType() const
Definition: mode.h:228
bool oper
Definition: mode.h:131
unsigned int ranktoset
Definition: mode.h:156
PrefixMode * IsPrefixMode()
Definition: mode.h:836
Definition: mode.h:567
std::vector< ListModeBase * > list
Definition: mode.h:609
const Modes::ChangeList & GetLastChangeList() const
Definition: mode.h:695
const PrefixModeList & GetPrefixModes() const
Definition: mode.h:820
std::vector< PrefixMode * > prefix
Definition: mode.h:613
const ListModeList & GetListModes() const
Definition: mode.h:815
TR1NS::unordered_map< std::string, ModeHandler *, irc::insensitive, irc::StrHashComp > ModeHandlerMap
Definition: mode.h:577
ModeProcessFlags
Definition: mode.h:645
const ModeHandlerMap & GetModes(ModeType mt) const
Definition: mode.h:826
Definition: mode.h:501
const std::string & GetModeName() const
Definition: mode.h:528
ModeType GetModeType() const
Definition: mode.h:534
Definition: modechange.h:53
Definition: modules.h:318
Definition: parammode.h:24
Definition: mode.h:370
bool CanSelfRemove() const
Definition: mode.h:441
char GetPrefix() const
Definition: mode.h:448
unsigned int GetPrefixRank() const
Definition: mode.h:457
unsigned int prefixrank
Definition: mode.h:380
char prefix
Definition: mode.h:375
bool selfremove
Definition: mode.h:383
Definition: base.h:249
ModuleRef creator
Definition: base.h:252
Definition: mode.h:483
Definition: mode.h:466
Definition: users.h:247
Definition: base.h:46
virtual CullResult cull()
Definition: base.cpp:41
Definition: modechange.h:32
Definition: inspircd.h:72