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-2020 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 */
283  virtual void DisplayList(User* user, Channel* channel);
284 
291  virtual void OnParameterMissing(User* user, User* dest, Channel* channel);
292 
298  virtual void DisplayEmptyList(User* user, Channel* channel);
299 
310  virtual bool ResolveModeConflict(std::string &their_param, const std::string &our_param, Channel* channel);
311 
317  void RemoveMode(User* user);
318 
329  virtual void RemoveMode(Channel* channel, Modes::ChangeList& changelist);
330 
334  inline unsigned int GetLevelRequired(bool adding) const
335  {
336  return adding ? ranktoset : ranktounset;
337  }
338 
340  const std::string& GetSyntax() const { return syntax; }
341 
342  friend class ModeParser;
343 };
344 
359 class CoreExport PrefixMode : public ModeHandler
360 {
361  protected:
365  char prefix;
366 
370  unsigned int prefixrank;
371 
374 
375  public:
384  PrefixMode(Module* Creator, const std::string& Name, char ModeLetter, unsigned int Rank = 0, char PrefixChar = 0);
385 
394  ModResult AccessCheck(User* source, Channel* channel, std::string &parameter, bool adding) CXX11_OVERRIDE;
395 
409  ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string& param, bool adding) CXX11_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) CXX11_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 SimpleUserModeHandler : public ModeHandler
456 {
457  public:
458  SimpleUserModeHandler(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, std::string& parameter, bool adding) CXX11_OVERRIDE;
465 };
466 
472 class CoreExport SimpleChannelModeHandler : public ModeHandler
473 {
474  public:
475  SimpleChannelModeHandler(Module* Creator, const std::string& Name, char modeletter, bool operonly = false)
476  : ModeHandler(Creator, Name, modeletter, PARAM_NONE, MODETYPE_CHANNEL)
477  {
478  oper = operonly;
479  }
480 
481  ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string& parameter, bool adding) CXX11_OVERRIDE;
482 };
483 
490 class CoreExport ModeWatcher : public classbase
491 {
492  private:
496  const std::string mode;
497 
501  ModeType m_type;
502 
503  public:
504  ModuleRef creator;
508  ModeWatcher(Module* creator, const std::string& modename, ModeType type);
512  virtual ~ModeWatcher();
513 
518  const std::string& GetModeName() const { return mode; }
519 
524  ModeType GetModeType() const { return m_type; }
525 
538  virtual bool BeforeMode(User* source, User* dest, Channel* channel, std::string& parameter, bool adding);
548  virtual void AfterMode(User* source, User* dest, Channel* channel, const std::string& parameter, bool adding);
549 };
550 
556 class CoreExport ModeParser : public fakederef<ModeParser>
557 {
558  public:
560  static const ModeHandler::Id MODEID_MAX = 64;
561 
563  static const size_t MODE_PARAM_MAX = 250;
564 
567  typedef TR1NS::unordered_map<std::string, ModeHandler*, irc::insensitive, irc::StrHashComp> ModeHandlerMap;
568 
569  private:
573 
576  static const unsigned int MODETYPE_LAST = 2;
577 
583  ModeHandler* modehandlers[MODETYPE_LAST][128];
584 
587  ModeHandler* modehandlersbyid[MODETYPE_LAST][MODEID_MAX];
588 
591  ModeHandlerMap modehandlersbyname[MODETYPE_LAST];
592 
595  struct
596  {
599  std::vector<ListModeBase*> list;
600 
603  std::vector<PrefixMode*> prefix;
604  } mhlist;
605 
608  ModeWatcherMap modewatchermap;
609 
612  Modes::ChangeList LastChangeList;
613 
617  ModeAction TryMode(User* user, User* targu, Channel* targc, Modes::Change& mcitem, bool SkipACL);
618 
623  ModeHandler::Id AllocateModeId(ModeType mt);
624 
625  public:
626  typedef std::vector<ListModeBase*> ListModeList;
627  typedef std::vector<PrefixMode*> PrefixModeList;
628 
629  typedef unsigned int ModeProcessFlag;
631  {
636  MODE_NONE = 0,
637 
643  MODE_MERGE = 1,
644 
649  MODE_LOCALONLY = 2,
650 
657  MODE_CHECKACCESS = 4
658  };
659 
660  ModeParser();
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, unsigned int beginindex = 1, unsigned int 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 
827 inline const PrefixMode* ModeHandler::IsPrefixMode() const
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 
837 inline const ListModeBase* ModeHandler::IsListModeBase() const
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 
847 inline const ParamModeBase* ModeHandler::IsParameterMode() const
848 {
849  return (this->type_id == MC_PARAM ? reinterpret_cast<const ParamModeBase*>(this) : NULL);
850 }
fakederef
Definition: inspircd.h:71
ModeHandler::OnModeChange
virtual ModeAction OnModeChange(User *source, User *dest, Channel *channel, std::string &parameter, bool adding)
Definition: mode.cpp:82
ModeHandler::IsParameterMode
ParamModeBase * IsParameterMode()
Definition: mode.h:842
Modes::ChangeList
Definition: modechange.h:52
ModeHandler::parameters_taken
ParamSpec parameters_taken
Definition: mode.h:120
ModeParser::list
std::vector< ListModeBase * > list
Definition: mode.h:599
ServiceProvider
Definition: base.h:249
ModeHandler::RemoveMode
void RemoveMode(User *user)
Definition: mode.cpp:839
PrefixMode::selfremove
bool selfremove
Definition: mode.h:373
ModeHandler::mode
char mode
Definition: mode.h:125
Modes::Change
Definition: modechange.h:31
PrefixMode::CanSelfRemove
bool CanSelfRemove() const
Definition: mode.h:431
classbase::cull
virtual CullResult cull()
Definition: base.cpp:41
ModeHandler::ranktoset
unsigned int ranktoset
Definition: mode.h:156
ModeWatcher
Definition: mode.h:490
PrefixMode::prefixrank
unsigned int prefixrank
Definition: mode.h:370
ModResult
Definition: modules.h:70
ModeParser::ModeHandlerMap
TR1NS::unordered_map< std::string, ModeHandler *, irc::insensitive, irc::StrHashComp > ModeHandlerMap
Definition: mode.h:567
ModeHandler::NeedsOper
bool NeedsOper() const
Definition: mode.h:232
ModeWatcher::GetModeName
const std::string & GetModeName() const
Definition: mode.h:518
ModeParser
Definition: mode.h:556
ModeHandler::IsPrefixMode
PrefixMode * IsPrefixMode()
Definition: mode.h:822
ModeHandler::GetModeChar
char GetModeChar() const
Definition: mode.h:243
classbase
Definition: base.h:46
ModeHandler::GetLevelRequired
unsigned int GetLevelRequired(bool adding) const
Definition: mode.h:334
ModeHandler::GetModeType
ModeType GetModeType() const
Definition: mode.h:228
ModeHandler::syntax
std::string syntax
Definition: mode.h:162
SimpleChannelModeHandler
Definition: mode.h:472
ModeParser::GetListModes
const ListModeList & GetListModes() const
Definition: mode.h:801
ModeHandler::AccessCheck
virtual ModResult AccessCheck(User *source, Channel *channel, std::string &parameter, bool adding)
Definition: mode.cpp:77
Channel
Definition: channels.h:40
ModeParser::prefix
std::vector< PrefixMode * > prefix
Definition: mode.h:603
ModeParser::GetModes
const ModeHandlerMap & GetModes(ModeType mt) const
Definition: mode.h:812
ModeWatcher::GetModeType
ModeType GetModeType() const
Definition: mode.h:524
PrefixMode::GetPrefixRank
unsigned int GetPrefixRank() const
Definition: mode.h:447
ModeHandler::ranktounset
unsigned int ranktounset
Definition: mode.h:159
ModeHandler::list
bool list
Definition: mode.h:143
ModeHandler::GetId
Id GetId() const
Definition: mode.h:248
ListModeBase
Definition: listmode.h:26
ModeHandler::oper
bool oper
Definition: mode.h:131
PrefixMode::GetPrefix
char GetPrefix() const
Definition: mode.h:438
SimpleUserModeHandler
Definition: mode.h:455
PrefixMode
Definition: mode.h:359
ModeParser::GetLastChangeList
const Modes::ChangeList & GetLastChangeList() const
Definition: mode.h:681
Module
Definition: modules.h:247
User
Definition: users.h:233
CullResult
Definition: base.h:36
ModeHandler::ModeHandler
ModeHandler(Module *me, const std::string &name, char modeletter, ParamSpec params, ModeType type, Class mclass=MC_OTHER)
Definition: mode.cpp:33
ParamModeBase
Definition: parammode.h:23
ModeParser::GetPrefixModes
const PrefixModeList & GetPrefixModes() const
Definition: mode.h:806
PrefixMode::prefix
char prefix
Definition: mode.h:365
ModeHandler::IsListModeBase
ListModeBase * IsListModeBase()
Definition: mode.h:832
ModeHandler::m_type
ModeType m_type
Definition: mode.h:149
insp::flat_multimap< std::string, ModeWatcher * >
ModeHandler
Definition: mode.h:99
ModeHandler::GetSyntax
const std::string & GetSyntax() const
Definition: mode.h:340
ModeParser::ModeProcessFlags
ModeProcessFlags
Definition: mode.h:630
ModeHandler::type_id
const Class type_id
Definition: mode.h:153