2 * InspIRCd -- Internet Relay Chat Daemon
4 * Copyright (C) 2009 Daniel De Graaf <danieldg@inspircd.org>
5 * Copyright (C) 2007 Robin Burchell <robin+git@viroteck.net>
6 * Copyright (C) 2007 Dennis Friis <peavey@inspircd.org>
7 * Copyright (C) 2003-2007 Craig Edwards <craigedwards@brainbox.cc>
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.
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
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/>.
25 #include "membership.h"
27 #include "parammode.h"
29 /** Holds an entry for a ban list, exemption list, or invite list.
30 * This class contains a single element in a channel list, such as a banlist.
33 /** Holds all relevent information for a channel.
34 * This class represents a channel, and contains its name, modes, topic, topic set time,
35 * etc, and an instance of the BanList type.
37 class CoreExport Channel : public Extensible
40 /** A map of Memberships on a channel keyed by User pointers
42 typedef std::map<User*, insp::aligned_storage<Membership> > MemberMap;
45 /** Set default modes for the channel on creation
47 void SetDefaultModes();
49 /** Modes for the channel.
50 * It is a bitset where each item in it represents if a mode is set.
51 * To see if a mode is set, inspect modes[mh->modeid]
53 std::bitset<ModeParser::MODEID_MAX> modes;
55 /** Remove the given membership from the channel's internal map of
56 * memberships and destroy the Membership object.
57 * This function does not remove the channel from User::chanlist.
58 * Since the parameter is an iterator to the target, the complexity
59 * of this function is constant.
60 * @param membiter The MemberMap iterator to remove, must be valid
62 void DelUser(const MemberMap::iterator& membiter);
65 /** Creates a channel record and initialises it with default values
66 * @param name The name of the channel
67 * @param ts The creation time of the channel
68 * @throw CoreException if this channel name is in use
70 Channel(const std::string &name, time_t ts);
72 /** Checks whether the channel should be destroyed, and if yes, begins
73 * the teardown procedure.
75 * If there are users on the channel or a module vetoes the deletion
76 * (OnPreChannelDelete hook) then nothing else happens.
77 * Otherwise, first the OnChannelDelete event is fired, then the channel is
78 * removed from the channel list. All pending invites are destroyed and
79 * finally the channel is added to the cull list.
83 /** The channel's name.
87 /** Time that the object was instantiated (used for TS calculation etc)
96 * If this is an empty string, no channel topic is set.
100 /** Time topic was set.
101 * If no topic was ever set, this will be equal to Channel::created
105 /** The last user to set the topic.
106 * If this member is an empty string, no topic was ever set.
108 std::string setby; /* 128 */
110 /** Sets or unsets a custom mode in the channels info
111 * @param mode The mode character to set or unset
112 * @param value True if you want to set the mode or false if you want to remove it
114 void SetMode(ModeHandler* mode, bool value);
116 /** Returns true if a mode is set on a channel
117 * @param mode The mode character you wish to query
118 * @return True if the custom mode is set, false if otherwise
120 bool IsModeSet(ModeHandler* mode) { return ((mode->GetId() != ModeParser::MODEID_MAX) && (modes[mode->GetId()])); }
121 bool IsModeSet(ModeHandler& mode) { return IsModeSet(&mode); }
122 bool IsModeSet(ChanModeReference& mode);
124 /** Returns the parameter for a custom mode on a channel.
125 * @param mode The mode character you wish to query
127 * For example if "+L #foo" is set, and you pass this method
128 * 'L', it will return '\#foo'. If the mode is not set on the
129 * channel, or the mode has no parameters associated with it,
130 * it will return an empty string.
132 * @return The parameter for this mode is returned, or an empty string
134 std::string GetModeParameter(ModeHandler* mode);
135 std::string GetModeParameter(ChanModeReference& mode);
136 std::string GetModeParameter(ParamModeBase* pm);
138 /** Sets the channel topic.
139 * @param user The user setting the topic.
140 * @param topic The topic to set it to.
142 void SetTopic(User* user, const std::string& topic);
144 /** Obtain the channel "user counter"
145 * This returns the number of users on this channel
147 * @return The number of users on this channel
149 long GetUserCounter() const { return userlist.size(); }
151 /** Add a user pointer to the internal reference list
152 * @param user The user to add
154 * The data inserted into the reference list is a table as it is
155 * an arbitary pointer compared to other users by its memory address,
156 * as this is a very fast 32 or 64 bit integer comparison.
158 Membership* AddUser(User* user);
160 /** Delete a user pointer to the internal reference list
161 * @param user The user to delete
162 * @return number of users left on the channel after deletion of the user
164 void DelUser(User* user);
166 /** Obtain the internal reference list
167 * The internal reference list contains a list of User*.
168 * These are used for rapid comparison to determine
169 * channel membership for PRIVMSG, NOTICE, QUIT, PART etc.
170 * The resulting pointer to the vector should be considered
171 * readonly and only modified via AddUser and DelUser.
173 * @return This function returns pointer to a map of User pointers (CUList*).
175 const MemberMap& GetUsers() const { return userlist; }
177 /** Returns true if the user given is on the given channel.
178 * @param user The user to look for
179 * @return True if the user is on this channel
181 bool HasUser(User* user);
183 Membership* GetUser(User* user);
185 /** Make src kick user from this channel with the given reason.
186 * @param src The source of the kick
187 * @param victimiter Iterator to the user being kicked, must be valid
188 * @param reason The reason for the kick
190 void KickUser(User* src, const MemberMap::iterator& victimiter, const std::string& reason);
192 /** Make src kick user from this channel with the given reason.
193 * @param src The source of the kick
194 * @param user The user being kicked
195 * @param reason The reason for the kick
197 void KickUser(User* src, User* user, const std::string& reason)
199 MemberMap::iterator it = userlist.find(user);
200 if (it != userlist.end())
201 KickUser(src, it, reason);
204 /** Part a user from this channel with the given reason.
205 * If the reason field is NULL, no reason will be sent.
206 * @param user The user who is parting (must be on this channel)
207 * @param reason The part reason
208 * @return True if the user was on the channel and left, false if they weren't and nothing happened
210 bool PartUser(User* user, std::string& reason);
212 /** Join a local user to a channel, with or without permission checks. May be a channel that doesn't exist yet.
213 * @param user The user to join to the channel.
214 * @param channame The channel name to join to. Does not have to exist.
215 * @param key The key of the channel, if given
216 * @param override If true, override all join restrictions such as +bkil
217 * @return A pointer to the Channel the user was joined to. A new Channel may have
218 * been created if the channel did not exist before the user was joined to it.
219 * If the user could not be joined to a channel, the return value is NULL.
221 static Channel* JoinUser(LocalUser* user, std::string channame, bool override = false, const std::string& key = "");
223 /** Join a user to an existing channel, without doing any permission checks
224 * @param user The user to join to the channel
225 * @param privs Priviliges (prefix mode letters) to give to this user, may be NULL
226 * @param bursting True if this join is the result of a netburst (passed to modules in the OnUserJoin hook)
227 * @param created_by_local True if this channel was just created by a local user (passed to modules in the OnUserJoin hook)
228 * @return A newly created Membership object, or NULL if the user was already inside the channel or if the user is a server user
230 Membership* ForceJoin(User* user, const std::string* privs = NULL, bool bursting = false, bool created_by_local = false);
232 /** Write to a channel, from a user, using va_args for text
233 * @param user User whos details to prefix the line with
234 * @param text A printf-style format string which builds the output line without prefix
235 * @param ... Zero or more POD types
237 void WriteChannel(User* user, const char* text, ...) CUSTOM_PRINTF(3, 4);
239 /** Write to a channel, from a user, using std::string for text
240 * @param user User whos details to prefix the line with
241 * @param text A std::string containing the output line without prefix
243 void WriteChannel(User* user, const std::string &text);
245 /** Write to a channel, from a server, using va_args for text
246 * @param ServName Server name to prefix the line with
247 * @param text A printf-style format string which builds the output line without prefix
248 * @param ... Zero or more POD type
250 void WriteChannelWithServ(const std::string& ServName, const char* text, ...) CUSTOM_PRINTF(3, 4);
252 /** Write to a channel, from a server, using std::string for text
253 * @param ServName Server name to prefix the line with
254 * @param text A std::string containing the output line without prefix
256 void WriteChannelWithServ(const std::string& ServName, const std::string &text);
258 /** Write to all users on a channel except a specific user, using va_args for text.
259 * Internally, this calls WriteAllExcept().
260 * @param user User whos details to prefix the line with, and to omit from receipt of the message
261 * @param serversource If this parameter is true, use the local server name as the source of the text, otherwise,
262 * use the nick!user\@host of the user.
263 * @param status The status of the users to write to, e.g. '@' or '%'. Use a value of 0 to write to everyone
264 * @param text A printf-style format string which builds the output line without prefix
265 * @param ... Zero or more POD type
267 void WriteAllExceptSender(User* user, bool serversource, char status, const char* text, ...) CUSTOM_PRINTF(5, 6);
269 /** Write to all users on a channel except a list of users, using va_args for text
270 * @param user User whos details to prefix the line with, and to omit from receipt of the message
271 * @param serversource If this parameter is true, use the local server name as the source of the text, otherwise,
272 * use the nick!user\@host of the user.
273 * @param status The status of the users to write to, e.g. '@' or '%'. Use a value of 0 to write to everyone
274 * @param except_list A list of users NOT to send the text to
275 * @param text A printf-style format string which builds the output line without prefix
276 * @param ... Zero or more POD type
278 void WriteAllExcept(User* user, bool serversource, char status, CUList &except_list, const char* text, ...) CUSTOM_PRINTF(6, 7);
280 /** Write to all users on a channel except a specific user, using std::string for text.
281 * Internally, this calls WriteAllExcept().
282 * @param user User whos details to prefix the line with, and to omit from receipt of the message
283 * @param serversource If this parameter is true, use the local server name as the source of the text, otherwise,
284 * use the nick!user\@host of the user.
285 * @param status The status of the users to write to, e.g. '@' or '%'. Use a value of 0 to write to everyone
286 * @param text A std::string containing the output line without prefix
288 void WriteAllExceptSender(User* user, bool serversource, char status, const std::string& text);
290 /** Write to all users on a channel except a list of users, using std::string for text
291 * @param user User whos details to prefix the line with, and to omit from receipt of the message
292 * @param serversource If this parameter is true, use the local server name as the source of the text, otherwise,
293 * use the nick!user\@host of the user.
294 * @param status The status of the users to write to, e.g. '@' or '%'. Use a value of 0 to write to everyone
295 * @param except_list A list of users NOT to send the text to
296 * @param text A std::string containing the output line without prefix
298 void WriteAllExcept(User* user, bool serversource, char status, CUList &except_list, const std::string& text);
299 /** Write a line of text that already includes the source */
300 void RawWriteAllExcept(User* user, bool serversource, char status, CUList &except_list, const std::string& text);
302 /** Return the channel's modes with parameters.
303 * @param showkey If this is set to true, the actual key is shown,
304 * otherwise it is replaced with '<KEY>'
305 * @return The channel mode string
307 const char* ChanModes(bool showkey);
309 /** Get the value of a users prefix on this channel.
310 * @param user The user to look up
311 * @return The module or core-defined value of the users prefix.
312 * The values for op, halfop and voice status are constants in
313 * mode.h, and are OP_VALUE, HALFOP_VALUE, and VOICE_VALUE respectively.
314 * If the value you are given does not match one of these three, it is
315 * a module-defined mode, and it should be compared in proportion to
316 * these three constants. For example a value greater than OP_VALUE
317 * is a prefix of greater 'worth' than ops, and a value less than
318 * VOICE_VALUE is of lesser 'worth' than a voice.
320 unsigned int GetPrefixValue(User* user);
322 /** Check if a user is banned on this channel
323 * @param user A user to check against the banlist
324 * @returns True if the user given is banned
326 bool IsBanned(User* user);
328 /** Check a single ban for match
330 bool CheckBan(User* user, const std::string& banmask);
332 /** Get the status of an "action" type extban
334 ModResult GetExtBanStatus(User *u, char type);
337 inline bool Channel::HasUser(User* user)
339 return (userlist.find(user) != userlist.end());
342 inline std::string Channel::GetModeParameter(ChanModeReference& mode)
346 return GetModeParameter(*mode);
349 inline std::string Channel::GetModeParameter(ModeHandler* mh)
352 ParamModeBase* pm = mh->IsParameterMode();
353 if (pm && this->IsModeSet(pm))
354 pm->GetParameter(this, out);
358 inline std::string Channel::GetModeParameter(ParamModeBase* pm)
361 if (this->IsModeSet(pm))
362 pm->GetParameter(this, out);
366 inline bool Channel::IsModeSet(ChanModeReference& mode)
370 return IsModeSet(*mode);