anope

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

channels.h (10600B)

      1 /* Channel support
      2  *
      3  * (C) 2008-2022 Anope Team
      4  * Contact us at team@anope.org
      5  *
      6  * Please read COPYING and README for further details.
      7  */
      8 
      9 #ifndef CHANNELS_H
     10 #define CHANNELS_H
     11 
     12 #include "anope.h"
     13 #include "extensible.h"
     14 #include "modes.h"
     15 #include "serialize.h"
     16 
     17 typedef Anope::hash_map<Channel *> channel_map;
     18 
     19 extern CoreExport channel_map ChannelList;
     20 
     21 /* A user container, there is one of these per user per channel. */
     22 struct ChanUserContainer : public Extensible
     23 {
     24 	User *user;
     25 	Channel *chan;
     26 	/* Status the user has in the channel */
     27 	ChannelStatus status;
     28 
     29 	ChanUserContainer(User *u, Channel *c) : user(u), chan(c) { }
     30 };
     31 
     32 class CoreExport Channel : public Base, public Extensible
     33 {
     34 	static std::vector<Channel *> deleting;
     35 
     36  public:
     37 	typedef std::multimap<Anope::string, Anope::string> ModeList;
     38  private:
     39 	/** A map of channel modes with their parameters set on this channel
     40 	 */
     41 	ModeList modes;
     42 
     43  public:
     44 	/* Channel name */
     45 	Anope::string name;
     46 	/* Set if this channel is registered. ci->c == this. Contains information relevant to the registered channel */
     47 	Serialize::Reference<ChannelInfo> ci;
     48 	/* When the channel was created */
     49 	time_t creation_time;
     50 	/* If the channel has just been created in a netjoin */
     51 	bool syncing;
     52 	/* Is configured in the conf as a channel bots should be in */
     53 	bool botchannel;
     54 
     55 	/* Users in the channel */
     56 	typedef std::map<User *, ChanUserContainer *> ChanUserList;
     57 	ChanUserList users;
     58 
     59 	/* Current topic of the channel */
     60 	Anope::string topic;
     61 	/* Who set the topic */
     62 	Anope::string topic_setter;
     63 	/* The timestamp associated with the topic. Not necessarily anywhere close to Anope::CurTime.
     64 	 * This is the time the topic was *originally set*. When we restore the topic we want to change the TS back
     65 	 * to this, but we can only do this on certain IRCds.
     66 	 */
     67 	time_t topic_ts;
     68 	/* The actual time the topic was set, probably close to Anope::CurTime */
     69 	time_t topic_time;
     70 
     71 	time_t server_modetime;		/* Time of last server MODE */
     72 	time_t chanserv_modetime;	/* Time of last check_modes() */
     73 	int16_t server_modecount;	/* Number of server MODEs this second */
     74 	int16_t chanserv_modecount;	/* Number of check_mode()'s this sec */
     75 	int16_t bouncy_modes;		/* Did we fail to set modes here? */
     76 
     77  private:
     78 	/** Constructor
     79 	 * @param name The channel name
     80 	 * @param ts The time the channel was created
     81 	 */
     82 	Channel(const Anope::string &nname, time_t ts = Anope::CurTime);
     83 
     84  public:
     85 	/** Destructor
     86 	 */
     87 	~Channel();
     88 
     89 	/** Call if we need to unset all modes and clear all user status (internally).
     90 	 * Only useful if we get a SJOIN with a TS older than what we have here
     91 	 */
     92 	void Reset();
     93 
     94 	/** Restore the channel topic, set mlock (key), set stickied bans, etc
     95 	 */
     96 	void Sync();
     97 
     98 	/** Check if a channels modes are correct.
     99 	 */
    100 	void CheckModes();
    101 
    102 	/** Check if this channel should be deleted
    103 	 */
    104 	bool CheckDelete();
    105 
    106 	/** Join a user internally to the channel
    107 	 * @param u The user
    108 	 * @param status The status to give the user, if any
    109 	 * @return The UserContainer for the user
    110 	 */
    111 	ChanUserContainer* JoinUser(User *u, const ChannelStatus *status);
    112 
    113 	/** Remove a user internally from the channel
    114 	 * @param u The user
    115 	 */
    116 	void DeleteUser(User *u);
    117 
    118 	/** Check if the user is on the channel
    119 	 * @param u The user
    120 	 * @return A user container if found, else NULL
    121 	 */
    122 	ChanUserContainer *FindUser(User *u) const;
    123 
    124 	/** Check if a user has a status on a channel
    125 	 * @param u The user
    126 	 * @param cms The status mode, or NULL to represent no status
    127 	 * @return true or false
    128 	 */
    129 	bool HasUserStatus(User *u, ChannelModeStatus *cms);
    130 
    131 	/** Check if a user has a status on a channel
    132 	 * Use the overloaded function for ChannelModeStatus* to check for no status
    133 	 * @param u The user
    134 	 * @param name The mode name, eg CMODE_OP, CMODE_VOICE
    135 	 * @return true or false
    136 	 */
    137 	bool HasUserStatus(User *u, const Anope::string &name);
    138 
    139 	/** See if a channel has a mode
    140 	 * @param name The mode name
    141 	 * @return The number of modes set
    142 	 * @param param The optional mode param
    143 	 */
    144 	size_t HasMode(const Anope::string &name, const Anope::string &param = "");
    145 
    146 	/** Set a mode internally on a channel, this is not sent out to the IRCd
    147 	 * @param setter The setter
    148 	 * @param cm The mode
    149 	 * @param param The param
    150 	 * @param enforce_mlock true if mlocks should be enforced, false to override mlock
    151 	 */
    152 	void SetModeInternal(MessageSource &source, ChannelMode *cm, const Anope::string &param = "", bool enforce_mlock = true);
    153 
    154 	/** Remove a mode internally on a channel, this is not sent out to the IRCd
    155 	 * @param setter The Setter
    156 	 * @param cm The mode
    157 	 * @param param The param
    158 	 * @param enforce_mlock true if mlocks should be enforced, false to override mlock
    159 	 */
    160 	void RemoveModeInternal(MessageSource &source, ChannelMode *cm, const Anope::string &param = "", bool enforce_mlock = true);
    161 
    162 	/** Set a mode on a channel
    163 	 * @param bi The client setting the modes
    164 	 * @param cm The mode
    165 	 * @param param Optional param arg for the mode
    166 	 * @param enforce_mlock true if mlocks should be enforced, false to override mlock
    167 	 */
    168 	void SetMode(BotInfo *bi, ChannelMode *cm, const Anope::string &param = "", bool enforce_mlock = true);
    169 
    170 	/**
    171 	 * Set a mode on a channel
    172 	 * @param bi The client setting the modes
    173 	 * @param name The mode name
    174 	 * @param param Optional param arg for the mode
    175 	 * @param enforce_mlock true if mlocks should be enforced, false to override mlock
    176 	 */
    177 	void SetMode(BotInfo *bi, const Anope::string &name, const Anope::string &param = "", bool enforce_mlock = true);
    178 
    179 	/** Remove a mode from a channel
    180 	 * @param bi The client setting the modes
    181 	 * @param cm The mode
    182 	 * @param param Optional param arg for the mode
    183 	 * @param enforce_mlock true if mlocks should be enforced, false to override mlock
    184 	 */
    185 	void RemoveMode(BotInfo *bi, ChannelMode *cm, const Anope::string &param = "", bool enforce_mlock = true);
    186 
    187 	/**
    188 	 * Remove a mode from a channel
    189 	 * @param bi The client setting the modes
    190 	 * @param name The mode name
    191 	 * @param param Optional param arg for the mode
    192 	 * @param enforce_mlock true if mlocks should be enforced, false to override mlock
    193 	 */
    194 	void RemoveMode(BotInfo *bi, const Anope::string &name, const Anope::string &param = "", bool enforce_mlock = true);
    195 
    196 	/** Get a modes parameter for the channel
    197 	 * @param name The mode
    198 	 * @param target a string to put the param into
    199 	 * @return true if the parameter was fetched, false if on error (mode not set) etc.
    200 	 */
    201 	bool GetParam(const Anope::string &name, Anope::string &target) const;
    202 
    203 	/** Set a string of modes on the channel
    204 	 * @param bi The client setting the modes
    205 	 * @param enforce_mlock Should mlock be enforced on this mode change
    206 	 * @param cmodes The modes to set
    207 	 */
    208 	void SetModes(BotInfo *bi, bool enforce_mlock, const char *cmodes, ...);
    209 
    210 	/** Set a string of modes internally on a channel
    211 	 * @param source The setter
    212 	 * @param mode the modes
    213 	 * @param enforce_mlock true to enforce mlock
    214 	 */
    215 	void SetModesInternal(MessageSource &source, const Anope::string &mode, time_t ts = 0, bool enforce_mlock = true);
    216 
    217 	/** Does the given user match the given list? (CMODE_BAN, CMODE_EXCEPT, etc, a list mode)
    218 	 * @param u The user
    219 	 * @param list The mode of the list to check (eg CMODE_BAN)
    220 	 * @return true if the user matches the list
    221 	 */
    222 	bool MatchesList(User *u, const Anope::string &list);
    223 
    224 	/** Kick a user from a channel internally
    225 	 * @param source The sender of the kick
    226 	 * @param nick The nick being kicked
    227 	 * @param reason The reason for the kick
    228 	 */
    229 	void KickInternal(const MessageSource &source, const Anope::string &nick, const Anope::string &reason);
    230 
    231 	/** Kick a user from the channel
    232 	 * @param bi The sender, can be NULL for the service bot for this channel
    233 	 * @param u The user being kicked
    234 	 * @param reason The reason for the kick
    235 	 * @return true if the kick was successful, false if a module blocked the kick
    236 	 */
    237 	bool Kick(BotInfo *bi, User *u, const char *reason = NULL, ...);
    238 
    239 	/** Get all modes set on this channel, excluding status modes.
    240 	 * @return a map of modes and their optional parameters.
    241 	 */
    242 	const ModeList &GetModes() const;
    243 
    244 	/** Get a list of modes on a channel
    245 	 * @param name A mode name to get the list of
    246 	 * @return a vector of the list mode entries
    247 	 */
    248 	std::vector<Anope::string> GetModeList(const Anope::string &name);
    249 
    250 	/** Get a string of the modes set on this channel
    251 	 * @param complete Include mode parameters
    252 	 * @param plus If set to false (with complete), mode parameters will not be given for modes requiring no parameters to be unset
    253 	 * @return A mode string
    254 	 */
    255 	Anope::string GetModes(bool complete, bool plus);
    256 
    257 	/** Update the topic of the channel internally, and reset it if topiclock etc says to
    258 	 * @param user The user setting the new topic
    259 	 * @param newtopic The new topic
    260 	 * @param ts The time the new topic is being set
    261 	 */
    262 	void ChangeTopicInternal(User *u, const Anope::string &user, const Anope::string &newtopic, time_t ts = Anope::CurTime);
    263 
    264 	/** Update the topic of the channel, and reset it if topiclock etc says to
    265 	 * @param user The user setting the topic
    266 	 * @param newtopic The new topic
    267 	 * @param ts The time when the new topic is being set
    268 	 */
    269 	void ChangeTopic(const Anope::string &user, const Anope::string &newtopic, time_t ts = Anope::CurTime);
    270 
    271 	/** Set the correct modes, or remove the ones granted without permission,
    272 	 * for the specified user.
    273 	 * @param user The user to give/remove modes to/from
    274 	 * @param give_modes if true modes may be given to the user
    275 	 */
    276 	void SetCorrectModes(User *u, bool give_modes);
    277 
    278 	/** Unbans a user from this channel.
    279 	 * @param u The user to unban
    280 	 * @param mode The mode to unban
    281 	 * @param full Whether or not to match using the user's real host and IP
    282 	 * @return whether or not a ban was removed
    283 	 */
    284 	bool Unban(User *u, const Anope::string &mode, bool full = false);
    285 
    286 	/** Check whether a user is permitted to be on this channel
    287 	 * @param u The user
    288 	 * @return true if they are allowed, false if they aren't and were kicked
    289 	 */
    290 	bool CheckKick(User *user);
    291 
    292 	/** Finds a channel
    293 	 * @param name The channel to find
    294 	 * @return The channel, if found
    295 	 */
    296 	static Channel* Find(const Anope::string &name);
    297 
    298 	/** Finds or creates a channel
    299 	 * @param name The channel name
    300 	 * @param created Set to true if the channel was just created
    301 	 * @param ts The time the channel was created
    302 	 */
    303 	static Channel *FindOrCreate(const Anope::string &name, bool &created, time_t ts = Anope::CurTime);
    304 
    305 	void QueueForDeletion();
    306 
    307 	static void DeleteChannels();
    308 };
    309 
    310 #endif // CHANNELS_H