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:
547  static const ModeHandler::Id MODEID_MAX = 64;
548 
551  typedef TR1NS::unordered_map<std::string, ModeHandler*, irc::insensitive, irc::StrHashComp> ModeHandlerMap;
552 
553  private:
557 
560  static const unsigned int MODETYPE_LAST = 2;
561 
567  ModeHandler* modehandlers[MODETYPE_LAST][128];
568 
571  ModeHandler* modehandlersbyid[MODETYPE_LAST][MODEID_MAX];
572 
575  ModeHandlerMap modehandlersbyname[MODETYPE_LAST];
576 
579  struct
580  {
583  std::vector<ListModeBase*> list;
584 
587  std::vector<PrefixMode*> prefix;
588  } mhlist;
589 
592  ModeWatcherMap modewatchermap;
593 
596  Modes::ChangeList LastChangeList;
597 
601  ModeAction TryMode(User* user, User* targu, Channel* targc, Modes::Change& mcitem, bool SkipACL);
602 
607  ModeHandler::Id AllocateModeId(ModeType mt);
608 
609  public:
610  typedef std::vector<ListModeBase*> ListModeList;
611  typedef std::vector<PrefixMode*> PrefixModeList;
612 
613  typedef unsigned int ModeProcessFlag;
615  {
620  MODE_NONE = 0,
621 
627  MODE_MERGE = 1,
628 
633  MODE_LOCALONLY = 2,
634 
641  MODE_CHECKACCESS = 4
642  };
643 
644  ModeParser();
645  ~ModeParser();
646 
647  static bool IsModeChar(char chr);
648 
662  static void CleanMask(std::string &mask);
663 
665  const Modes::ChangeList& GetLastChangeList() const { return LastChangeList; }
666 
670  void AddMode(ModeHandler* mh);
671 
680  bool DelMode(ModeHandler* mh);
681 
688  void AddModeWatcher(ModeWatcher* mw);
689 
697  bool DelModeWatcher(ModeWatcher* mw);
698 
708  void Process(User* user, Channel* targetchannel, User* targetuser, Modes::ChangeList& changelist, ModeProcessFlag flags = MODE_NONE);
709 
723  unsigned int ProcessSingle(User* user, Channel* targetchannel, User* targetuser, Modes::ChangeList& changelist, ModeProcessFlag flags = MODE_NONE, unsigned int beginindex = 0);
724 
740  void ModeParamsToChangeList(User* user, ModeType type, const std::vector<std::string>& parameters, Modes::ChangeList& changelist, unsigned int beginindex = 1, unsigned int endindex = UINT_MAX);
741 
747  ModeHandler* FindMode(const std::string& modename, ModeType mt);
748 
754  ModeHandler* FindMode(unsigned const char modeletter, ModeType mt);
755 
760  PrefixMode* FindPrefixMode(unsigned char modeletter);
761 
767  PrefixMode* FindPrefix(unsigned const char pfxletter);
768 
775  std::string GiveModeList(ModeType mt);
776 
780  std::string BuildPrefixes(bool lettersAndModes = true);
781 
785  const ListModeList& GetListModes() const { return mhlist.list; }
786 
790  const PrefixModeList& GetPrefixModes() const { return mhlist.prefix; }
791 
796  const ModeHandlerMap& GetModes(ModeType mt) const { return modehandlersbyname[mt]; }
797 
803  void ShowListModeList(User* user, Channel* chan, ModeHandler* mh);
804 };
805 
807 {
808  return (this->type_id == MC_PREFIX ? static_cast<PrefixMode*>(this) : NULL);
809 }
810 
812 {
813  return (this->type_id == MC_PREFIX ? static_cast<const PrefixMode*>(this) : NULL);
814 }
815 
817 {
818  return (this->type_id == MC_LIST ? reinterpret_cast<ListModeBase*>(this) : NULL);
819 }
820 
822 {
823  return (this->type_id == MC_LIST ? reinterpret_cast<const ListModeBase*>(this) : NULL);
824 }
825 
827 {
828  return (this->type_id == MC_PARAM ? reinterpret_cast<ParamModeBase*>(this) : NULL);
829 }
830 
832 {
833  return (this->type_id == MC_PARAM ? reinterpret_cast<const ParamModeBase*>(this) : NULL);
834 }
Definition: mode.h:95
virtual CullResult cull()
Definition: base.cpp:36
bool CanSelfRemove() const
Definition: mode.h:427
unsigned int ranktounset
Definition: mode.h:155
bool selfremove
Definition: mode.h:369
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:361
bool list
Definition: mode.h:139
Definition: mode.h:478
Definition: mode.h:451
TR1NS::unordered_map< std::string, ModeHandler *, irc::insensitive, irc::StrHashComp > ModeHandlerMap
Definition: mode.h:551
ModeType m_type
Definition: mode.h:145
const std::string & GetModeName() const
Definition: mode.h:506
Definition: mode.h:464
unsigned int GetLevelRequired(bool adding) const
Definition: mode.h:330
ModeType GetModeType() const
Definition: mode.h:224
virtual ModResult AccessCheck(User *source, Channel *channel, std::string &parameter, bool adding)
Definition: mode.cpp:72
Definition: users.h:224
ModeProcessFlags
Definition: mode.h:614
const ModeHandlerMap & GetModes(ModeType mt) const
Definition: mode.h:796
std::vector< ListModeBase * > list
Definition: mode.h:583
ListModeBase * IsListModeBase()
Definition: mode.h:816
ParamModeBase * IsParameterMode()
Definition: mode.h:826
Definition: base.h:41
const Modes::ChangeList & GetLastChangeList() const
Definition: mode.h:665
ParamSpec parameters_taken
Definition: mode.h:116
Definition: modechange.h:30
Definition: mode.h:544
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:242
Definition: modules.h:65
unsigned int prefixrank
Definition: mode.h:366
char GetPrefix() const
Definition: mode.h:434
PrefixMode * IsPrefixMode()
Definition: mode.h:806
void RemoveMode(User *user)
Definition: mode.cpp:834
Definition: inspircd.h:65
char mode
Definition: mode.h:121
Definition: parammode.h:22
const std::string & GetSyntax() const
Definition: mode.h:336
unsigned int ranktoset
Definition: mode.h:152
const ListModeList & GetListModes() const
Definition: mode.h:785
Definition: base.h:244
Definition: mode.h:355
std::vector< PrefixMode * > prefix
Definition: mode.h:587
unsigned int GetPrefixRank() const
Definition: mode.h:443
Definition: listmode.h:24
ModeType GetModeType() const
Definition: mode.h:512
bool NeedsOper() const
Definition: mode.h:228
Definition: modechange.h:51
Definition: base.h:31
const PrefixModeList & GetPrefixModes() const
Definition: mode.h:790
Id GetId() const
Definition: mode.h:244
std::string syntax
Definition: mode.h:158
bool oper
Definition: mode.h:127
char GetModeChar() const
Definition: mode.h:239
Definition: channels.h:37