InspIRCd  4.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 = false;
132 
143  bool list = false;
144 
149  ModeType m_type;
150 
153  const Class type_id;
154 
156  unsigned int ranktoset = HALFOP_VALUE;
157 
159  unsigned int ranktounset = HALFOP_VALUE;
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  Cullable::Result Cull() override;
178 
181  void RegisterService() override;
182 
186  bool IsListMode() const { return list; }
187 
192  PrefixMode* IsPrefixMode();
193 
198  const PrefixMode* IsPrefixMode() const;
199 
204  ListModeBase* IsListModeBase();
205 
210  const ListModeBase* IsListModeBase() const;
211 
216  ParamModeBase* IsParameterMode();
217 
222  const ParamModeBase* IsParameterMode() const;
223 
227  inline ModeType GetModeType() const { return m_type; }
231  inline bool NeedsOper() const { return oper; }
237  bool NeedsParam(bool adding) const;
242  char GetModeChar() const { return mode; }
243 
247  Id GetId() const { return modeid; }
248 
251  virtual std::string GetUserParameter(const User* user) const;
252 
259  virtual ModResult AccessCheck(User* source, Channel* channel, Modes::Change& change);
260 
269  virtual ModeAction OnModeChange(User* source, User* dest, Channel* channel, Modes::Change& change);
270 
277  virtual void DisplayList(User* user, Channel* channel);
278 
285  virtual void OnParameterMissing(User* user, User* dest, Channel* channel);
286 
293  virtual void OnParameterInvalid(User* user, Channel* targetchannel, User* targetuser, const std::string& parameter);
294 
300  virtual void DisplayEmptyList(User* user, Channel* channel);
301 
312  virtual bool ResolveModeConflict(const std::string& their_param, const std::string& our_param, Channel* channel);
313 
319  void RemoveMode(User* user);
320 
331  virtual void RemoveMode(Channel* channel, Modes::ChangeList& changelist);
332 
336  inline unsigned int GetLevelRequired(bool adding) const
337  {
338  return adding ? ranktoset : ranktounset;
339  }
340 
342  const std::string& GetSyntax() const { return syntax; }
343 
344  friend class ModeParser;
345 };
346 
361 class CoreExport PrefixMode : public ModeHandler
362 {
363  protected:
367  char prefix;
368 
372  unsigned int prefixrank;
373 
375  bool selfremove = true;
376 
377  public:
386  PrefixMode(Module* Creator, const std::string& Name, char ModeLetter, unsigned int Rank = 0, char PrefixChar = 0);
387 
395  ModResult AccessCheck(User* source, Channel* channel, Modes::Change& change) override;
396 
409  ModeAction OnModeChange(User* source, User* dest, Channel* channel, Modes::Change& change) override;
410 
418  void Update(unsigned int rank, unsigned int setrank, unsigned int unsetrank, bool selfrm);
419 
425  void RemoveMode(Channel* channel, Modes::ChangeList& changelist) override;
426 
427 
431  bool CanSelfRemove() const { return selfremove; }
432 
438  char GetPrefix() const { return prefix; }
439 
447  unsigned int GetPrefixRank() const { return prefixrank; }
448 };
449 
455 class CoreExport SimpleUserMode : public ModeHandler
456 {
457  public:
458  SimpleUserMode(Module* Creator, const std::string& Name, char modeletter, bool operonly = false)
459  : ModeHandler(Creator, Name, modeletter, PARAM_NONE, MODETYPE_USER)
460  {
461  oper = operonly;
462  }
463 
464  ModeAction OnModeChange(User* source, User* dest, Channel* channel, Modes::Change& change) override;
465 };
466 
472 class CoreExport SimpleChannelMode : public ModeHandler
473 {
474  public:
475 
476  SimpleChannelMode(Module* Creator, const std::string& Name, char modeletter, bool operonly = false)
477  : ModeHandler(Creator, Name, modeletter, PARAM_NONE, MODETYPE_CHANNEL)
478  {
479  oper = operonly;
480  }
481 
482  ModeAction OnModeChange(User* source, User* dest, Channel* channel, Modes::Change& change) override;
483 };
484 
491 class CoreExport ModeWatcher : public Cullable
492 {
493  private:
497  const std::string mode;
498 
502  ModeType m_type;
503 
504  public:
506 
510  ModeWatcher(Module* creator, const std::string& modename, ModeType type);
511 
515  ~ModeWatcher() override;
516 
521  const std::string& GetModeName() const { return mode; }
522 
527  ModeType GetModeType() const { return m_type; }
528 
540  virtual bool BeforeMode(User* source, User* dest, Channel* channel, Modes::Change& change);
541 
549  virtual void AfterMode(User* source, User* dest, Channel* channel, const Modes::Change& change);
550 };
551 
557 class CoreExport ModeParser
558 {
559  public:
561  static const ModeHandler::Id MODEID_MAX = 64;
562 
564  static const size_t MODE_PARAM_MAX = 250;
565 
568  typedef std::unordered_map<std::string, ModeHandler*, irc::insensitive, irc::StrHashComp> ModeHandlerMap;
569 
570  private:
574 
577  static const unsigned int MODETYPE_LAST = 2;
578 
584  ModeHandler* modehandlers[MODETYPE_LAST][128];
585 
588  ModeHandler* modehandlersbyid[MODETYPE_LAST][MODEID_MAX];
589 
592  ModeHandlerMap modehandlersbyname[MODETYPE_LAST];
593 
596  struct
597  {
600  std::vector<ListModeBase*> list;
601 
604  std::vector<PrefixMode*> prefix;
605  } mhlist;
606 
609  ModeWatcherMap modewatchermap;
610 
613  Modes::ChangeList LastChangeList;
614 
618  ModeAction TryMode(User* user, User* targu, Channel* targc, Modes::Change& mcitem, bool SkipACL);
619 
624  ModeHandler::Id AllocateModeId(ModeType mt);
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 
663  static bool IsModeChar(char chr);
664 
678  static void CleanMask(std::string &mask);
679 
681  const Modes::ChangeList& GetLastChangeList() const { return LastChangeList; }
682 
686  void AddMode(ModeHandler* mh);
687 
696  bool DelMode(ModeHandler* mh);
697 
704  void AddModeWatcher(ModeWatcher* mw);
705 
713  bool DelModeWatcher(ModeWatcher* mw);
714 
724  void Process(User* user, Channel* targetchannel, User* targetuser, Modes::ChangeList& changelist, ModeProcessFlag flags = MODE_NONE);
725 
739  unsigned int ProcessSingle(User* user, Channel* targetchannel, User* targetuser, Modes::ChangeList& changelist, ModeProcessFlag flags = MODE_NONE, unsigned int beginindex = 0);
740 
756  void ModeParamsToChangeList(User* user, ModeType type, const std::vector<std::string>& parameters, Modes::ChangeList& changelist, size_t beginindex = 1, size_t endindex = UINT_MAX);
757 
763  ModeHandler* FindMode(const std::string& modename, ModeType mt);
764 
770  ModeHandler* FindMode(unsigned const char modeletter, ModeType mt);
771 
776  PrefixMode* FindPrefixMode(unsigned char modeletter);
777 
783  PrefixMode* FindPrefix(unsigned const char pfxletter);
784 
791  std::string GiveModeList(ModeType mt);
792 
796  std::string BuildPrefixes(bool lettersAndModes = true);
797 
801  const ListModeList& GetListModes() const { return mhlist.list; }
802 
806  const PrefixModeList& GetPrefixModes() const { return mhlist.prefix; }
807 
812  const ModeHandlerMap& GetModes(ModeType mt) const { return modehandlersbyname[mt]; }
813 
819  void ShowListModeList(User* user, Channel* chan, ModeHandler* mh);
820 };
821 
823 {
824  return (this->type_id == MC_PREFIX ? static_cast<PrefixMode*>(this) : NULL);
825 }
826 
828 {
829  return (this->type_id == MC_PREFIX ? static_cast<const PrefixMode*>(this) : NULL);
830 }
831 
833 {
834  return (this->type_id == MC_LIST ? reinterpret_cast<ListModeBase*>(this) : NULL);
835 }
836 
838 {
839  return (this->type_id == MC_LIST ? reinterpret_cast<const ListModeBase*>(this) : NULL);
840 }
841 
843 {
844  return (this->type_id == MC_PARAM ? reinterpret_cast<ParamModeBase*>(this) : NULL);
845 }
846 
848 {
849  return (this->type_id == MC_PARAM ? reinterpret_cast<const ParamModeBase*>(this) : NULL);
850 }
Definition: channels.h:41
Definition: cull.h:38
Definition: cull.h:30
virtual Result Cull()
Definition: cull.cpp:54
Definition: listmode.h:27
Definition: modules.h:72
Definition: mode.h:100
ListModeBase * IsListModeBase()
Definition: mode.h:832
ParamSpec parameters_taken
Definition: mode.h:120
ParamModeBase * IsParameterMode()
Definition: mode.h:842
char GetModeChar() const
Definition: mode.h:242
std::string syntax
Definition: mode.h:162
const std::string & GetSyntax() const
Definition: mode.h:342
ModeType m_type
Definition: mode.h:149
char mode
Definition: mode.h:125
Id GetId() const
Definition: mode.h:247
bool IsListMode() const
Definition: mode.h:186
const Class type_id
Definition: mode.h:153
bool NeedsOper() const
Definition: mode.h:231
unsigned int GetLevelRequired(bool adding) const
Definition: mode.h:336
ModeType GetModeType() const
Definition: mode.h:227
PrefixMode * IsPrefixMode()
Definition: mode.h:822
Definition: mode.h:558
std::vector< ListModeBase * > list
Definition: mode.h:600
const Modes::ChangeList & GetLastChangeList() const
Definition: mode.h:681
const PrefixModeList & GetPrefixModes() const
Definition: mode.h:806
std::vector< PrefixMode * > prefix
Definition: mode.h:604
const ListModeList & GetListModes() const
Definition: mode.h:801
ModeProcessFlags
Definition: mode.h:632
const ModeHandlerMap & GetModes(ModeType mt) const
Definition: mode.h:812
std::unordered_map< std::string, ModeHandler *, irc::insensitive, irc::StrHashComp > ModeHandlerMap
Definition: mode.h:568
Definition: mode.h:492
const std::string & GetModeName() const
Definition: mode.h:521
ModeType GetModeType() const
Definition: mode.h:527
Definition: modechange.h:71
Definition: modules.h:286
Definition: parammode.h:24
Definition: mode.h:362
bool CanSelfRemove() const
Definition: mode.h:431
char GetPrefix() const
Definition: mode.h:438
unsigned int GetPrefixRank() const
Definition: mode.h:447
unsigned int prefixrank
Definition: mode.h:372
char prefix
Definition: mode.h:367
Definition: base.h:204
ModuleRef creator
Definition: base.h:207
virtual void RegisterService()
Definition: base.cpp:65
Definition: mode.h:473
Definition: mode.h:456
Definition: users.h:238
Definition: modechange.h:32