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 
158  std::string syntax;
159 
160  public:
172  ModeHandler(Module* me, const std::string& name, char modeletter, ParamSpec params, ModeType type, Class mclass = MC_OTHER);
173  CullResult cull() CXX11_OVERRIDE;
174  virtual ~ModeHandler();
175 
178  void RegisterService() CXX11_OVERRIDE;
179 
183  bool IsListMode() const { return list; }
184 
189  PrefixMode* IsPrefixMode();
190 
195  const PrefixMode* IsPrefixMode() const;
196 
201  ListModeBase* IsListModeBase();
202 
207  const ListModeBase* IsListModeBase() const;
208 
213  ParamModeBase* IsParameterMode();
214 
219  const ParamModeBase* IsParameterMode() const;
220 
224  inline ModeType GetModeType() const { return m_type; }
228  inline bool NeedsOper() const { return oper; }
234  bool NeedsParam(bool adding) const;
239  char GetModeChar() const { return mode; }
240 
244  Id GetId() const { return modeid; }
245 
248  virtual std::string GetUserParameter(const User* user) const;
249 
258  virtual ModResult AccessCheck(User* source, Channel* channel, std::string &parameter, bool adding);
259 
272  virtual ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string &parameter, bool adding); /* Can change the mode parameter as its a ref */
279  virtual void DisplayList(User* user, Channel* channel);
280 
287  virtual void OnParameterMissing(User* user, User* dest, Channel* channel);
288 
294  virtual void DisplayEmptyList(User* user, Channel* channel);
295 
306  virtual bool ResolveModeConflict(std::string &their_param, const std::string &our_param, Channel* channel);
307 
313  void RemoveMode(User* user);
314 
325  virtual void RemoveMode(Channel* channel, Modes::ChangeList& changelist);
326 
330  inline unsigned int GetLevelRequired(bool adding) const
331  {
332  return adding ? ranktoset : ranktounset;
333  }
334 
336  const std::string& GetSyntax() const { return syntax; }
337 
338  friend class ModeParser;
339 };
340 
355 class CoreExport PrefixMode : public ModeHandler
356 {
357  protected:
361  char prefix;
362 
366  unsigned int prefixrank;
367 
370 
371  public:
380  PrefixMode(Module* Creator, const std::string& Name, char ModeLetter, unsigned int Rank = 0, char PrefixChar = 0);
381 
390  ModResult AccessCheck(User* source, Channel* channel, std::string &parameter, bool adding) CXX11_OVERRIDE;
391 
405  ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string& param, bool adding) CXX11_OVERRIDE;
406 
414  void Update(unsigned int rank, unsigned int setrank, unsigned int unsetrank, bool selfrm);
415 
421  void RemoveMode(Channel* channel, Modes::ChangeList& changelist) CXX11_OVERRIDE;
422 
423 
427  bool CanSelfRemove() const { return selfremove; }
428 
434  char GetPrefix() const { return prefix; }
435 
443  unsigned int GetPrefixRank() const { return prefixrank; }
444 };
445 
451 class CoreExport SimpleUserModeHandler : public ModeHandler
452 {
453  public:
454  SimpleUserModeHandler(Module* Creator, const std::string& Name, char modeletter)
455  : ModeHandler(Creator, Name, modeletter, PARAM_NONE, MODETYPE_USER) {}
456  ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string& parameter, bool adding) CXX11_OVERRIDE;
457 };
458 
464 class CoreExport SimpleChannelModeHandler : public ModeHandler
465 {
466  public:
467  SimpleChannelModeHandler(Module* Creator, const std::string& Name, char modeletter)
468  : ModeHandler(Creator, Name, modeletter, PARAM_NONE, MODETYPE_CHANNEL) {}
469  ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string& parameter, bool adding) CXX11_OVERRIDE;
470 };
471 
478 class CoreExport ModeWatcher : public classbase
479 {
480  private:
484  const std::string mode;
485 
489  ModeType m_type;
490 
491  public:
492  ModuleRef creator;
496  ModeWatcher(Module* creator, const std::string& modename, ModeType type);
500  virtual ~ModeWatcher();
501 
506  const std::string& GetModeName() const { return mode; }
507 
512  ModeType GetModeType() const { return m_type; }
513 
526  virtual bool BeforeMode(User* source, User* dest, Channel* channel, std::string& parameter, bool adding);
536  virtual void AfterMode(User* source, User* dest, Channel* channel, const std::string& parameter, bool adding);
537 };
538 
544 class CoreExport ModeParser : public fakederef<ModeParser>
545 {
546  public:
548  static const ModeHandler::Id MODEID_MAX = 64;
549 
551  static const size_t MODE_PARAM_MAX = 250;
552 
555  typedef TR1NS::unordered_map<std::string, ModeHandler*, irc::insensitive, irc::StrHashComp> ModeHandlerMap;
556 
557  private:
561 
564  static const unsigned int MODETYPE_LAST = 2;
565 
571  ModeHandler* modehandlers[MODETYPE_LAST][128];
572 
575  ModeHandler* modehandlersbyid[MODETYPE_LAST][MODEID_MAX];
576 
579  ModeHandlerMap modehandlersbyname[MODETYPE_LAST];
580 
583  struct
584  {
587  std::vector<ListModeBase*> list;
588 
591  std::vector<PrefixMode*> prefix;
592  } mhlist;
593 
596  ModeWatcherMap modewatchermap;
597 
600  Modes::ChangeList LastChangeList;
601 
605  ModeAction TryMode(User* user, User* targu, Channel* targc, Modes::Change& mcitem, bool SkipACL);
606 
611  ModeHandler::Id AllocateModeId(ModeType mt);
612 
613  public:
614  typedef std::vector<ListModeBase*> ListModeList;
615  typedef std::vector<PrefixMode*> PrefixModeList;
616 
617  typedef unsigned int ModeProcessFlag;
619  {
624  MODE_NONE = 0,
625 
631  MODE_MERGE = 1,
632 
637  MODE_LOCALONLY = 2,
638 
645  MODE_CHECKACCESS = 4
646  };
647 
648  ModeParser();
649  ~ModeParser();
650 
651  static bool IsModeChar(char chr);
652 
666  static void CleanMask(std::string &mask);
667 
669  const Modes::ChangeList& GetLastChangeList() const { return LastChangeList; }
670 
674  void AddMode(ModeHandler* mh);
675 
684  bool DelMode(ModeHandler* mh);
685 
692  void AddModeWatcher(ModeWatcher* mw);
693 
701  bool DelModeWatcher(ModeWatcher* mw);
702 
712  void Process(User* user, Channel* targetchannel, User* targetuser, Modes::ChangeList& changelist, ModeProcessFlag flags = MODE_NONE);
713 
727  unsigned int ProcessSingle(User* user, Channel* targetchannel, User* targetuser, Modes::ChangeList& changelist, ModeProcessFlag flags = MODE_NONE, unsigned int beginindex = 0);
728 
744  void ModeParamsToChangeList(User* user, ModeType type, const std::vector<std::string>& parameters, Modes::ChangeList& changelist, unsigned int beginindex = 1, unsigned int endindex = UINT_MAX);
745 
751  ModeHandler* FindMode(const std::string& modename, ModeType mt);
752 
758  ModeHandler* FindMode(unsigned const char modeletter, ModeType mt);
759 
764  PrefixMode* FindPrefixMode(unsigned char modeletter);
765 
771  PrefixMode* FindPrefix(unsigned const char pfxletter);
772 
779  std::string GiveModeList(ModeType mt);
780 
784  std::string BuildPrefixes(bool lettersAndModes = true);
785 
789  const ListModeList& GetListModes() const { return mhlist.list; }
790 
794  const PrefixModeList& GetPrefixModes() const { return mhlist.prefix; }
795 
800  const ModeHandlerMap& GetModes(ModeType mt) const { return modehandlersbyname[mt]; }
801 
807  void ShowListModeList(User* user, Channel* chan, ModeHandler* mh);
808 };
809 
811 {
812  return (this->type_id == MC_PREFIX ? static_cast<PrefixMode*>(this) : NULL);
813 }
814 
816 {
817  return (this->type_id == MC_PREFIX ? static_cast<const PrefixMode*>(this) : NULL);
818 }
819 
821 {
822  return (this->type_id == MC_LIST ? reinterpret_cast<ListModeBase*>(this) : NULL);
823 }
824 
826 {
827  return (this->type_id == MC_LIST ? reinterpret_cast<const ListModeBase*>(this) : NULL);
828 }
829 
831 {
832  return (this->type_id == MC_PARAM ? reinterpret_cast<ParamModeBase*>(this) : NULL);
833 }
834 
836 {
837  return (this->type_id == MC_PARAM ? reinterpret_cast<const ParamModeBase*>(this) : NULL);
838 }
fakederef
Definition: inspircd.h:65
ModeHandler::OnModeChange
virtual ModeAction OnModeChange(User *source, User *dest, Channel *channel, std::string &parameter, bool adding)
Definition: mode.cpp:77
Modes::ChangeList
Definition: modechange.h:51
ModeHandler::parameters_taken
ParamSpec parameters_taken
Definition: mode.h:116
ModeParser::list
std::vector< ListModeBase * > list
Definition: mode.h:587
ServiceProvider
Definition: base.h:244
ModeHandler::RemoveMode
void RemoveMode(User *user)
Definition: mode.cpp:834
PrefixMode::selfremove
bool selfremove
Definition: mode.h:369
ModeHandler::mode
char mode
Definition: mode.h:121
Modes::Change
Definition: modechange.h:30
PrefixMode::CanSelfRemove
bool CanSelfRemove() const
Definition: mode.h:427
ModeHandler::IsParameterMode
ParamModeBase * IsParameterMode()
Definition: mode.h:830
classbase::cull
virtual CullResult cull()
Definition: base.cpp:36
ModeHandler::IsPrefixMode
PrefixMode * IsPrefixMode()
Definition: mode.h:810
ModeHandler::ranktoset
unsigned int ranktoset
Definition: mode.h:152
ModeWatcher
Definition: mode.h:478
PrefixMode::prefixrank
unsigned int prefixrank
Definition: mode.h:366
ModResult
Definition: modules.h:65
ModeParser::ModeHandlerMap
TR1NS::unordered_map< std::string, ModeHandler *, irc::insensitive, irc::StrHashComp > ModeHandlerMap
Definition: mode.h:555
ModeHandler::NeedsOper
bool NeedsOper() const
Definition: mode.h:228
ModeWatcher::GetModeName
const std::string & GetModeName() const
Definition: mode.h:506
ModeParser
Definition: mode.h:544
ModeHandler::GetModeChar
char GetModeChar() const
Definition: mode.h:239
classbase
Definition: base.h:41
ModeHandler::GetLevelRequired
unsigned int GetLevelRequired(bool adding) const
Definition: mode.h:330
ModeHandler::GetModeType
ModeType GetModeType() const
Definition: mode.h:224
ModeHandler::syntax
std::string syntax
Definition: mode.h:158
SimpleChannelModeHandler
Definition: mode.h:464
ModeParser::GetListModes
const ListModeList & GetListModes() const
Definition: mode.h:789
ModeHandler::AccessCheck
virtual ModResult AccessCheck(User *source, Channel *channel, std::string &parameter, bool adding)
Definition: mode.cpp:72
Channel
Definition: channels.h:37
ModeParser::prefix
std::vector< PrefixMode * > prefix
Definition: mode.h:591
ModeParser::GetModes
const ModeHandlerMap & GetModes(ModeType mt) const
Definition: mode.h:800
ModeHandler::IsListModeBase
ListModeBase * IsListModeBase()
Definition: mode.h:820
ModeWatcher::GetModeType
ModeType GetModeType() const
Definition: mode.h:512
PrefixMode::GetPrefixRank
unsigned int GetPrefixRank() const
Definition: mode.h:443
ModeHandler::ranktounset
unsigned int ranktounset
Definition: mode.h:155
ModeHandler::list
bool list
Definition: mode.h:139
ModeHandler::GetId
Id GetId() const
Definition: mode.h:244
ListModeBase
Definition: listmode.h:24
ModeHandler::oper
bool oper
Definition: mode.h:127
PrefixMode::GetPrefix
char GetPrefix() const
Definition: mode.h:434
SimpleUserModeHandler
Definition: mode.h:451
PrefixMode
Definition: mode.h:355
ModeParser::GetLastChangeList
const Modes::ChangeList & GetLastChangeList() const
Definition: mode.h:669
Module
Definition: modules.h:242
User
Definition: users.h:224
CullResult
Definition: base.h:31
ModeHandler::ModeHandler
ModeHandler(Module *me, const std::string &name, char modeletter, ParamSpec params, ModeType type, Class mclass=MC_OTHER)
Definition: mode.cpp:28
ParamModeBase
Definition: parammode.h:22
ModeParser::GetPrefixModes
const PrefixModeList & GetPrefixModes() const
Definition: mode.h:794
PrefixMode::prefix
char prefix
Definition: mode.h:361
ModeHandler::m_type
ModeType m_type
Definition: mode.h:145
insp::flat_multimap< std::string, ModeWatcher * >
ModeHandler
Definition: mode.h:95
ModeHandler::GetSyntax
const std::string & GetSyntax() const
Definition: mode.h:336
ModeParser::ModeProcessFlags
ModeProcessFlags
Definition: mode.h:618
ModeHandler::type_id
const Class type_id
Definition: mode.h:149