anope

- supernets anope source code & configuration
git clone git://git.acid.vegas/anope.git
Log | Files | Refs | Archive | README

access.h (5095B)

      1 /*
      2  *
      3  * (C) 2003-2022 Anope Team
      4  * Contact us at team@anope.org
      5  *
      6  * Please read COPYING and README for further details.
      7  *
      8  * Based on the original code of Epona by Lara.
      9  * Based on the original code of Services by Andy Church.
     10  */
     11 
     12 #ifndef ACCESS_H
     13 #define ACCESS_H
     14 
     15 #include "services.h"
     16 #include "anope.h"
     17 #include "serialize.h"
     18 #include "service.h"
     19 
     20 enum
     21 {
     22 	ACCESS_INVALID = -10000,
     23 	ACCESS_FOUNDER = 10001
     24 };
     25 
     26 /* A privilege, probably configured using a privilege{} block. Most
     27  * commands require specific privileges to be executed. The AccessProvider
     28  * backing each ChanAccess determines whether that ChanAccess has a given
     29  * privilege.
     30  */
     31 struct CoreExport Privilege
     32 {
     33 	Anope::string name;
     34 	Anope::string desc;
     35 	/* Rank relative to other privileges */
     36 	int rank;
     37 
     38 	Privilege(const Anope::string &name, const Anope::string &desc, int rank);
     39 	bool operator==(const Privilege &other) const;
     40 };
     41 
     42 class CoreExport PrivilegeManager
     43 {
     44 	static std::vector<Privilege> Privileges;
     45  public:
     46 	static void AddPrivilege(Privilege p);
     47 	static void RemovePrivilege(Privilege &p);
     48 	static Privilege *FindPrivilege(const Anope::string &name);
     49 	static std::vector<Privilege> &GetPrivileges();
     50 	static void ClearPrivileges();
     51 };
     52 
     53 /* A provider of access. Only used for creating ChanAccesses, as
     54  * they contain pure virtual functions.
     55  */
     56 class CoreExport AccessProvider : public Service
     57 {
     58  public:
     59 	AccessProvider(Module *owner, const Anope::string &name);
     60 	virtual ~AccessProvider();
     61 
     62 	/** Creates a new ChanAccess entry using this provider.
     63 	 * @return The new entry
     64 	 */
     65 	virtual ChanAccess *Create() = 0;
     66 
     67  private:
     68 	static std::list<AccessProvider *> Providers;
     69  public:
     70 	static const std::list<AccessProvider *>& GetProviders();
     71 };
     72 
     73 /* Represents one entry of an access list on a channel. */
     74 class CoreExport ChanAccess : public Serializable
     75 {
     76 	Anope::string mask;
     77 	/* account this access entry is for, if any */
     78 	Serialize::Reference<NickCore> nc;
     79 
     80  public:
     81 	typedef std::vector<ChanAccess *> Path;
     82 
     83 	/* The provider that created this access entry */
     84 	AccessProvider *provider;
     85 	/* Channel this access entry is on */
     86 	Serialize::Reference<ChannelInfo> ci;
     87 	Anope::string creator;
     88 	time_t last_seen;
     89 	time_t created;
     90 
     91 	ChanAccess(AccessProvider *p);
     92 	virtual ~ChanAccess();
     93 
     94 	void SetMask(const Anope::string &mask, ChannelInfo *ci);
     95 	const Anope::string &Mask() const;
     96 	NickCore *GetAccount() const;
     97 
     98 	void Serialize(Serialize::Data &data) const anope_override;
     99 	static Serializable* Unserialize(Serializable *obj, Serialize::Data &);
    100 
    101 	static const unsigned int MAX_DEPTH = 4;
    102 
    103 	/** Check if this access entry matches the given user or account
    104 	 * @param u The user
    105 	 * @param nc The account
    106 	 * @param next Next channel to check if any
    107 	 */
    108 	virtual bool Matches(const User *u, const NickCore *nc, ChannelInfo* &next) const;
    109 
    110 	/** Check if this access entry has the given privilege.
    111 	 * @param name The privilege name
    112 	 */
    113 	virtual bool HasPriv(const Anope::string &name) const = 0;
    114 
    115 	/** Serialize the access given by this access entry into a human
    116 	 * readable form. chanserv/access will return a number, chanserv/xop
    117 	 * will be AOP, SOP, etc.
    118 	 */
    119 	virtual Anope::string AccessSerialize() const = 0;
    120 
    121 	/** Unserialize this access entry from the given data. This data
    122 	 * will be fetched from AccessSerialize.
    123 	 */
    124 	virtual void AccessUnserialize(const Anope::string &data) = 0;
    125 
    126 	/* Comparison operators to other Access entries */
    127 	virtual bool operator>(const ChanAccess &other) const;
    128 	virtual bool operator<(const ChanAccess &other) const;
    129 	bool operator>=(const ChanAccess &other) const;
    130 	bool operator<=(const ChanAccess &other) const;
    131 };
    132 
    133 /* A group of access entries. This is used commonly, for example with ChannelInfo::AccessFor,
    134  * to show what access a user has on a channel because users can match multiple access entries.
    135  */
    136 class CoreExport AccessGroup
    137 {
    138  public:
    139 	/* access entries + paths */
    140 	std::vector<ChanAccess::Path> paths;
    141 	/* Channel these access entries are on */
    142 	const ChannelInfo *ci;
    143 	/* Account these entries affect, if any */
    144 	const NickCore *nc;
    145 	/* super_admin always gets all privs. founder is a special case where ci->founder == nc */
    146 	bool super_admin, founder;
    147 
    148 	AccessGroup();
    149 
    150 	/** Check if this access group has a certain privilege. Eg, it
    151 	 * will check every ChanAccess entry of this group for any that
    152 	 * has the given privilege.
    153 	 * @param priv The privilege
    154 	 * @return true if any entry has the given privilege
    155 	 */
    156 	bool HasPriv(const Anope::string &priv) const;
    157 
    158 	/** Get the "highest" access entry from this group of entries.
    159 	 * The highest entry is determined by the entry that has the privilege
    160 	 * with the highest rank (see Privilege::rank).
    161 	 * @return The "highest" entry
    162 	 */
    163 	const ChanAccess *Highest() const;
    164 
    165 	/* Comparison operators to other AccessGroups */
    166 	bool operator>(const AccessGroup &other) const;
    167 	bool operator<(const AccessGroup &other) const;
    168 	bool operator>=(const AccessGroup &other) const;
    169 	bool operator<=(const AccessGroup &other) const;
    170 
    171 	inline bool empty() const { return paths.empty(); }
    172 };
    173 
    174 #endif