2 * InspIRCd -- Internet Relay Chat Daemon
4 * Copyright (C) 2013-2018 Attila Molnar <attilamolnar@hush.com>
5 * Copyright (C) 2012-2013, 2017-2020 Sadie Powell <sadie@witchery.services>
6 * Copyright (C) 2012 Robby <robby@chatbelgie.be>
7 * Copyright (C) 2009 Daniel De Graaf <danieldg@inspircd.org>
8 * Copyright (C) 2007-2008 Robin Burchell <robin+git@viroteck.net>
9 * Copyright (C) 2007, 2009 Dennis Friis <peavey@inspircd.org>
10 * Copyright (C) 2006, 2008, 2010 Craig Edwards <brain@inspircd.org>
12 * This file is part of InspIRCd. InspIRCd is free software: you can
13 * redistribute it and/or modify it under the terms of the GNU General Public
14 * License as published by the Free Software Foundation, version 2.
16 * This program is distributed in the hope that it will be useful, but WITHOUT
17 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
21 * You should have received a copy of the GNU General Public License
22 * along with this program. If not, see <http://www.gnu.org/licenses/>.
28 #include "membership.h"
30 #include "parammode.h"
32 /** Holds an entry for a ban list, exemption list, or invite list.
33 * This class contains a single element in a channel list, such as a banlist.
36 /** Holds all relevant information for a channel.
37 * This class represents a channel, and contains its name, modes, topic, topic set time,
38 * etc, and an instance of the BanList type.
40 class CoreExport Channel : public Extensible
43 /** A map of Memberships on a channel keyed by User pointers
45 typedef std::map<User*, insp::aligned_storage<Membership> > MemberMap;
48 /** Set default modes for the channel on creation
50 void SetDefaultModes();
52 /** Modes for the channel.
53 * It is a bitset where each item in it represents if a mode is set.
54 * To see if a mode is set, inspect modes[mh->modeid]
56 std::bitset<ModeParser::MODEID_MAX> modes;
58 /** Remove the given membership from the channel's internal map of
59 * memberships and destroy the Membership object.
60 * This function does not remove the channel from User::chanlist.
61 * Since the parameter is an iterator to the target, the complexity
62 * of this function is constant.
63 * @param membiter The MemberMap iterator to remove, must be valid
65 void DelUser(const MemberMap::iterator& membiter);
68 /** Creates a channel record and initialises it with default values
69 * @param name The name of the channel
70 * @param ts The creation time of the channel
71 * @throw CoreException if this channel name is in use
73 Channel(const std::string &name, time_t ts);
75 /** Checks whether the channel should be destroyed, and if yes, begins
76 * the teardown procedure.
78 * If there are users on the channel or a module vetoes the deletion
79 * (OnPreChannelDelete hook) then nothing else happens.
80 * Otherwise, first the OnChannelDelete event is fired, then the channel is
81 * removed from the channel list. All pending invites are destroyed and
82 * finally the channel is added to the cull list.
86 /** The channel's name.
90 /** Time that the object was instantiated (used for TS calculation etc)
99 * If this is an empty string, no channel topic is set.
103 /** Time topic was set.
104 * If no topic was ever set, this will be equal to Channel::created
108 /** The last user to set the topic.
109 * If this member is an empty string, no topic was ever set.
111 std::string setby; /* 128 */
113 /** Sets or unsets a custom mode in the channels info
114 * @param mode The mode character to set or unset
115 * @param value True if you want to set the mode or false if you want to remove it
117 void SetMode(ModeHandler* mode, bool value);
119 /** Returns true if a mode is set on a channel
120 * @param mode The mode character you wish to query
121 * @return True if the custom mode is set, false if otherwise
123 bool IsModeSet(ModeHandler* mode) { return ((mode->GetId() != ModeParser::MODEID_MAX) && (modes[mode->GetId()])); }
124 bool IsModeSet(ModeHandler& mode) { return IsModeSet(&mode); }
125 bool IsModeSet(ChanModeReference& mode);
127 /** Returns the parameter for a custom mode on a channel.
128 * @param mode The mode character you wish to query
130 * For example if "+L #foo" is set, and you pass this method
131 * 'L', it will return '\#foo'. If the mode is not set on the
132 * channel, or the mode has no parameters associated with it,
133 * it will return an empty string.
135 * @return The parameter for this mode is returned, or an empty string
137 std::string GetModeParameter(ModeHandler* mode);
138 std::string GetModeParameter(ChanModeReference& mode);
139 std::string GetModeParameter(ParamModeBase* pm);
141 /** Sets the channel topic.
142 * @param user The user setting the topic.
143 * @param topic The topic to set it to.
144 * @param topicts Timestamp of the new topic.
145 * @param setter Setter string, may be used when the original setter is no longer online.
146 * If omitted or NULL, the setter string is obtained from the user.
148 void SetTopic(User* user, const std::string& topic, time_t topicts, const std::string* setter = NULL);
150 /** Obtain the channel "user counter"
151 * This returns the number of users on this channel
153 * @return The number of users on this channel
155 size_t GetUserCounter() const { return userlist.size(); }
157 /** Add a user pointer to the internal reference list
158 * @param user The user to add
160 * The data inserted into the reference list is a table as it is
161 * an arbitrary pointer compared to other users by its memory address,
162 * as this is a very fast 32 or 64 bit integer comparison.
164 Membership* AddUser(User* user);
166 /** Delete a user pointer to the internal reference list
167 * @param user The user to delete
168 * @return number of users left on the channel after deletion of the user
170 void DelUser(User* user);
172 /** Obtain the internal reference list
173 * The internal reference list contains a list of User*.
174 * These are used for rapid comparison to determine
175 * channel membership for PRIVMSG, NOTICE, QUIT, PART etc.
176 * The resulting pointer to the vector should be considered
177 * readonly and only modified via AddUser and DelUser.
179 * @return This function returns pointer to a map of User pointers (CUList*).
181 const MemberMap& GetUsers() const { return userlist; }
183 /** Returns true if the user given is on the given channel.
184 * @param user The user to look for
185 * @return True if the user is on this channel
187 bool HasUser(User* user);
189 Membership* GetUser(User* user);
191 /** Make src kick user from this channel with the given reason.
192 * @param src The source of the kick
193 * @param victimiter Iterator to the user being kicked, must be valid
194 * @param reason The reason for the kick
196 void KickUser(User* src, const MemberMap::iterator& victimiter, const std::string& reason);
198 /** Make src kick user from this channel with the given reason.
199 * @param src The source of the kick
200 * @param user The user being kicked
201 * @param reason The reason for the kick
203 void KickUser(User* src, User* user, const std::string& reason)
205 MemberMap::iterator it = userlist.find(user);
206 if (it != userlist.end())
207 KickUser(src, it, reason);
210 /** Part a user from this channel with the given reason.
211 * If the reason field is NULL, no reason will be sent.
212 * @param user The user who is parting (must be on this channel)
213 * @param reason The part reason
214 * @return True if the user was on the channel and left, false if they weren't and nothing happened
216 bool PartUser(User* user, std::string& reason);
218 /** Join a local user to a channel, with or without permission checks. May be a channel that doesn't exist yet.
219 * @param user The user to join to the channel.
220 * @param channame The channel name to join to. Does not have to exist.
221 * @param key The key of the channel, if given
222 * @param override If true, override all join restrictions such as +bkil
223 * @return A pointer to the Channel the user was joined to. A new Channel may have
224 * been created if the channel did not exist before the user was joined to it.
225 * If the user could not be joined to a channel, the return value is NULL.
227 static Channel* JoinUser(LocalUser* user, std::string channame, bool override = false, const std::string& key = "");
229 /** Join a user to an existing channel, without doing any permission checks
230 * @param user The user to join to the channel
231 * @param privs Privileges (prefix mode letters) to give to this user, may be NULL
232 * @param bursting True if this join is the result of a netburst (passed to modules in the OnUserJoin hook)
233 * @param created_by_local True if this channel was just created by a local user (passed to modules in the OnUserJoin hook)
234 * @return A newly created Membership object, or NULL if the user was already inside the channel or if the user is a server user
236 Membership* ForceJoin(User* user, const std::string* privs = NULL, bool bursting = false, bool created_by_local = false);
238 /** Write to all users on a channel except some users
239 * @param protoev Event to send, may contain any number of messages.
240 * @param status The status of the users to write to, e.g. '@' or '%'. Use a value of 0 to write to everyone
241 * @param except_list List of users not to send to
243 void Write(ClientProtocol::Event& protoev, char status = 0, const CUList& except_list = CUList());
245 /** Write to all users on a channel except some users.
246 * @param protoevprov Protocol event provider for the message.
247 * @param msg Message to send.
248 * @param status The status of the users to write to, e.g. '@' or '%'. Use a value of 0 to write to everyone
249 * @param except_list List of users not to send to
251 void Write(ClientProtocol::EventProvider& protoevprov, ClientProtocol::Message& msg, char status = 0, const CUList& except_list = CUList());
253 /** Return the channel's modes with parameters.
254 * @param showsecret If this is set to true, the value of secret parameters
255 * are shown, otherwise they are replaced with '<name>'.
256 * @return The channel mode string
258 const char* ChanModes(bool showsecret);
260 /** Get the value of a users prefix on this channel.
261 * @param user The user to look up
262 * @return The module or core-defined value of the users prefix.
263 * The values for op, halfop and voice status are constants in
264 * mode.h, and are OP_VALUE, HALFOP_VALUE, and VOICE_VALUE respectively.
265 * If the value you are given does not match one of these three, it is
266 * a module-defined mode, and it should be compared in proportion to
267 * these three constants. For example a value greater than OP_VALUE
268 * is a prefix of greater 'worth' than ops, and a value less than
269 * VOICE_VALUE is of lesser 'worth' than a voice.
271 unsigned int GetPrefixValue(User* user);
273 /** Check if a user is banned on this channel
274 * @param user A user to check against the banlist
275 * @returns True if the user given is banned
277 bool IsBanned(User* user);
279 /** Check a single ban for match
281 bool CheckBan(User* user, const std::string& banmask);
283 /** Get the status of an "action" type extban
285 ModResult GetExtBanStatus(User *u, char type);
287 /** Write a NOTICE to all local users on the channel
288 * @param text Text to send
289 * @param status The minimum status rank to send this message to.
291 void WriteNotice(const std::string& text, char status = 0);
292 void WriteRemoteNotice(const std::string& text, char status = 0);
295 inline bool Channel::HasUser(User* user)
297 return (userlist.find(user) != userlist.end());
300 inline std::string Channel::GetModeParameter(ChanModeReference& mode)
304 return GetModeParameter(*mode);
307 inline std::string Channel::GetModeParameter(ModeHandler* mh)
310 ParamModeBase* pm = mh->IsParameterMode();
311 if (pm && this->IsModeSet(pm))
312 pm->GetParameter(this, out);
316 inline std::string Channel::GetModeParameter(ParamModeBase* pm)
319 if (this->IsModeSet(pm))
320 pm->GetParameter(this, out);
324 inline bool Channel::IsModeSet(ChanModeReference& mode)
328 return IsModeSet(*mode);