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"
28 /** Holds an entry for a ban list, exemption list, or invite list.
29 * This class contains a single element in a channel list, such as a banlist.
32 /** Holds all relevent information for a channel.
33 * This class represents a channel, and contains its name, modes, topic, topic set time,
34 * etc, and an instance of the BanList type.
36 class CoreExport Channel : public Extensible, public InviteBase
38 /** Set default modes for the channel on creation
40 void SetDefaultModes();
42 /** Modes for the channel.
43 * This is not a null terminated string! It is a bitset where
44 * each item in it represents if a mode is set. For example
45 * for mode +A, index 0. Use modechar-65 to calculate which
48 std::bitset<64> modes;
50 /** Parameters for custom modes.
51 * One for each custom mode letter.
53 CustomModeList custom_mode_params;
56 /** Creates a channel record and initialises it with default values
57 * @throw Nothing at present.
59 Channel(const std::string &name, time_t ts);
61 /** The channel's name.
65 /** Time that the object was instantiated (used for TS calculation etc)
71 UserMembList userlist;
74 * If this is an empty string, no channel topic is set.
78 /** Time topic was set.
79 * If no topic was ever set, this will be equal to Channel::created
83 /** The last user to set the topic.
84 * If this member is an empty string, no topic was ever set.
86 std::string setby; /* 128 */
88 /** Sets or unsets a custom mode in the channels info
89 * @param mode The mode character to set or unset
90 * @param value True if you want to set the mode or false if you want to remove it
92 void SetMode(ModeHandler* mode, bool value);
93 void SetMode(char mode,bool mode_on);
95 /** Sets or unsets a custom mode in the channels info
96 * @param mode The mode character to set or unset
97 * @param parameter The parameter string to associate with this mode character.
98 * If it is empty, the mode is unset; if it is nonempty, the mode is set.
100 void SetModeParam(ModeHandler* mode, const std::string& parameter);
101 void SetModeParam(char mode, const std::string& parameter);
103 /** Returns true if a mode is set on a channel
104 * @param mode The mode character you wish to query
105 * @return True if the custom mode is set, false if otherwise
107 inline bool IsModeSet(char mode) { return modes[mode-'A']; }
108 inline bool IsModeSet(ModeHandler* mode) { return modes[mode->GetModeChar()-'A']; }
111 /** Returns the parameter for a custom mode on a channel.
112 * @param mode The mode character you wish to query
114 * For example if "+L #foo" is set, and you pass this method
115 * 'L', it will return '\#foo'. If the mode is not set on the
116 * channel, or the mode has no parameters associated with it,
117 * it will return an empty string.
119 * @return The parameter for this mode is returned, or an empty string
121 std::string GetModeParameter(char mode);
122 std::string GetModeParameter(ModeHandler* mode);
124 /** Sets the channel topic.
125 * @param u The user setting the topic
126 * @param t The topic to set it to. Non-const, as it may be modified by a hook.
127 * @param forceset If set to true then all access checks will be bypassed.
129 int SetTopic(User *u, std::string &t, bool forceset = false);
131 /** Obtain the channel "user counter"
132 * This returns the channel reference counter, which is initialized
133 * to 0 when the channel is created and incremented/decremented
134 * upon joins, parts quits and kicks.
136 * @return The number of users on this channel
138 long GetUserCounter();
140 /** Add a user pointer to the internal reference list
141 * @param user The user to add
143 * The data inserted into the reference list is a table as it is
144 * an arbitary pointer compared to other users by its memory address,
145 * as this is a very fast 32 or 64 bit integer comparison.
147 Membership* AddUser(User* user);
149 /** Delete a user pointer to the internal reference list
150 * @param user The user to delete
151 * @return number of users left on the channel after deletion of the user
153 void DelUser(User* user);
155 /** Obtain the internal reference list
156 * The internal reference list contains a list of User*.
157 * These are used for rapid comparison to determine
158 * channel membership for PRIVMSG, NOTICE, QUIT, PART etc.
159 * The resulting pointer to the vector should be considered
160 * readonly and only modified via AddUser and DelUser.
162 * @return This function returns pointer to a map of User pointers (CUList*).
164 const UserMembList* GetUsers();
166 /** Returns true if the user given is on the given channel.
167 * @param user The user to look for
168 * @return True if the user is on this channel
170 bool HasUser(User* user);
172 Membership* GetUser(User* user);
174 /** Make src kick user from this channel with the given reason.
175 * @param src The source of the kick
176 * @param user The user being kicked (must be on this channel)
177 * @param reason The reason for the kick
179 void KickUser(User *src, User *user, const std::string& reason);
181 /** Part a user from this channel with the given reason.
182 * If the reason field is NULL, no reason will be sent.
183 * @param user The user who is parting (must be on this channel)
184 * @param reason The part reason
186 void PartUser(User *user, std::string &reason);
188 /** Join a local user to a channel, with or without permission checks. May be a channel that doesn't exist yet.
189 * @param user The user to join to the channel.
190 * @param channame The channel name to join to. Does not have to exist.
191 * @param key The key of the channel, if given
192 * @param override If true, override all join restrictions such as +bkil
193 * @return A pointer to the Channel the user was joined to. A new Channel may have
194 * been created if the channel did not exist before the user was joined to it.
195 * If the user could not be joined to a channel, the return value is NULL.
197 static Channel* JoinUser(LocalUser* user, std::string channame, bool override = false, const std::string& key = "");
199 /** Join a user to an existing channel, without doing any permission checks
200 * @param user The user to join to the channel
201 * @param privs Priviliges (prefix mode letters) to give to this user, may be NULL
202 * @param bursting True if this join is the result of a netburst (passed to modules in the OnUserJoin hook)
203 * @param created True if this channel was just created by a local user (passed to modules in the OnUserJoin hook)
205 void ForceJoin(User* user, const std::string* privs = NULL, bool bursting = false, bool created_by_local = false);
207 /** Write to a channel, from a user, using va_args for text
208 * @param user User whos details to prefix the line with
209 * @param text A printf-style format string which builds the output line without prefix
210 * @param ... Zero or more POD types
212 void WriteChannel(User* user, const char* text, ...) CUSTOM_PRINTF(3, 4);
214 /** Write to a channel, from a user, using std::string for text
215 * @param user User whos details to prefix the line with
216 * @param text A std::string containing the output line without prefix
218 void WriteChannel(User* user, const std::string &text);
220 /** Write to a channel, from a server, using va_args for text
221 * @param ServName Server name to prefix the line with
222 * @param text A printf-style format string which builds the output line without prefix
223 * @param ... Zero or more POD type
225 void WriteChannelWithServ(const std::string& ServName, const char* text, ...) CUSTOM_PRINTF(3, 4);
227 /** Write to a channel, from a server, using std::string for text
228 * @param ServName Server name to prefix the line with
229 * @param text A std::string containing the output line without prefix
231 void WriteChannelWithServ(const std::string& ServName, const std::string &text);
233 /** Write to all users on a channel except a specific user, using va_args for text.
234 * Internally, this calls WriteAllExcept().
235 * @param user User whos details to prefix the line with, and to omit from receipt of the message
236 * @param serversource If this parameter is true, use the local server name as the source of the text, otherwise,
237 * use the nick!user\@host of the user.
238 * @param status The status of the users to write to, e.g. '@' or '%'. Use a value of 0 to write to everyone
239 * @param text A printf-style format string which builds the output line without prefix
240 * @param ... Zero or more POD type
242 void WriteAllExceptSender(User* user, bool serversource, char status, const char* text, ...) CUSTOM_PRINTF(5, 6);
244 /** Write to all users on a channel except a list of users, using va_args for text
245 * @param user User whos details to prefix the line with, and to omit from receipt of the message
246 * @param serversource If this parameter is true, use the local server name as the source of the text, otherwise,
247 * use the nick!user\@host of the user.
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 A list of users NOT to send the text to
250 * @param text A printf-style format string which builds the output line without prefix
251 * @param ... Zero or more POD type
253 void WriteAllExcept(User* user, bool serversource, char status, CUList &except_list, const char* text, ...) CUSTOM_PRINTF(6, 7);
255 /** Write to all users on a channel except a specific user, using std::string for text.
256 * Internally, this calls WriteAllExcept().
257 * @param user User whos details to prefix the line with, and to omit from receipt of the message
258 * @param serversource If this parameter is true, use the local server name as the source of the text, otherwise,
259 * use the nick!user\@host of the user.
260 * @param status The status of the users to write to, e.g. '@' or '%'. Use a value of 0 to write to everyone
261 * @param text A std::string containing the output line without prefix
263 void WriteAllExceptSender(User* user, bool serversource, char status, const std::string& text);
265 /** Write to all users on a channel except a list of users, using std::string for text
266 * @param user User whos details to prefix the line with, and to omit from receipt of the message
267 * @param serversource If this parameter is true, use the local server name as the source of the text, otherwise,
268 * use the nick!user\@host of the user.
269 * @param status The status of the users to write to, e.g. '@' or '%'. Use a value of 0 to write to everyone
270 * @param except_list A list of users NOT to send the text to
271 * @param text A std::string containing the output line without prefix
273 void WriteAllExcept(User* user, bool serversource, char status, CUList &except_list, const std::string& text);
274 /** Write a line of text that already includes the source */
275 void RawWriteAllExcept(User* user, bool serversource, char status, CUList &except_list, const std::string& text);
277 /** Return the channel's modes with parameters.
278 * @param showkey If this is set to true, the actual key is shown,
279 * otherwise it is replaced with '<KEY>'
280 * @return The channel mode string
282 char* ChanModes(bool showkey);
284 /** Spool the NAMES list for this channel to the given user
285 * @param user The user to spool the NAMES list to
287 void UserList(User *user);
289 /** Get the number of invisible users on this channel
290 * @return Number of invisible users
292 int CountInvisible();
294 /** Get a users prefix on this channel in a string.
295 * @param user The user to look up
296 * @return A character array containing the prefix string.
297 * Unlike GetStatus and GetStatusFlags which will only return the
298 * core specified modes @, % and + (op, halfop and voice), GetPrefixChar
299 * will also return module-defined prefixes. If the user has to prefix,
300 * an empty but non-null string is returned. If the user has multiple
301 * prefixes, the highest is returned. If you do not recognise the prefix
302 * character you can get, you can deal with it in a 'proprtional' manner
303 * compared to known prefixes, using GetPrefixValue().
305 const char* GetPrefixChar(User *user);
307 /** Return all of a users mode prefixes into a char* string.
308 * @param user The user to look up
309 * @return A list of all prefix characters. The prefixes will always
310 * be in rank order, greatest first, as certain IRC clients require
311 * this when multiple prefixes are used names lists.
313 const char* GetAllPrefixChars(User* user);
315 /** Get the value of a users prefix on this channel.
316 * @param user The user to look up
317 * @return The module or core-defined value of the users prefix.
318 * The values for op, halfop and voice status are constants in
319 * mode.h, and are OP_VALUE, HALFOP_VALUE, and VOICE_VALUE respectively.
320 * If the value you are given does not match one of these three, it is
321 * a module-defined mode, and it should be compared in proportion to
322 * these three constants. For example a value greater than OP_VALUE
323 * is a prefix of greater 'worth' than ops, and a value less than
324 * VOICE_VALUE is of lesser 'worth' than a voice.
326 unsigned int GetPrefixValue(User* user);
328 /** This method removes all prefix characters from a user.
329 * It will not inform the user or the channel of the removal of prefixes,
330 * and should be used when the user parts or quits.
331 * @param user The user to remove all prefixes from
333 void RemoveAllPrefixes(User* user);
335 /** Add a prefix character to a user.
336 * Only the core should call this method, usually from
337 * within the mode parser or when the first user joins
338 * the channel (to grant ops to them)
339 * @param user The user to associate the privilage with
340 * @param prefix The prefix character to associate
341 * @param adding True if adding the prefix, false when removing
342 * @return True if a change was made
344 bool SetPrefix(User* user, char prefix, bool adding);
346 /** Check if a user is banned on this channel
347 * @param user A user to check against the banlist
348 * @returns True if the user given is banned
350 bool IsBanned(User* user);
352 /** Check a single ban for match
354 bool CheckBan(User* user, const std::string& banmask);
356 /** Get the status of an "action" type extban
358 ModResult GetExtBanStatus(User *u, char type);