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;
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 UserMembIter to remove, must be valid
62 void DelUser(const UserMembIter& membiter);
65 /** Creates a channel record and initialises it with default values
66 * @throw Nothing at present.
68 Channel(const std::string &name, time_t ts);
70 /** Checks whether the channel should be destroyed, and if yes, begins
71 * the teardown procedure.
73 * If there are users on the channel or a module vetoes the deletion
74 * (OnPreChannelDelete hook) then nothing else happens.
75 * Otherwise, first the OnChannelDelete event is fired, then the channel is
76 * removed from the channel list. All pending invites are destroyed and
77 * finally the channel is added to the cull list.
81 /** The channel's name.
85 /** Time that the object was instantiated (used for TS calculation etc)
91 UserMembList userlist;
94 * If this is an empty string, no channel topic is set.
98 /** Time topic was set.
99 * If no topic was ever set, this will be equal to Channel::created
103 /** The last user to set the topic.
104 * If this member is an empty string, no topic was ever set.
106 std::string setby; /* 128 */
108 /** Sets or unsets a custom mode in the channels info
109 * @param mode The mode character to set or unset
110 * @param value True if you want to set the mode or false if you want to remove it
112 void SetMode(ModeHandler* mode, bool value);
113 void SetMode(char mode,bool mode_on);
115 /** Sets or unsets a custom mode in the channels info
116 * @param mode The mode character to set or unset
117 * @param parameter The parameter string to associate with this mode character.
118 * If it is empty, the mode is unset; if it is nonempty, the mode is set.
120 void SetModeParam(ModeHandler* mode, const std::string& parameter);
121 void SetModeParam(char mode, const std::string& parameter);
123 /** Returns true if a mode is set on a channel
124 * @param mode The mode character you wish to query
125 * @return True if the custom mode is set, false if otherwise
127 inline bool IsModeSet(char mode) { return modes[mode-'A']; }
128 inline bool IsModeSet(ModeHandler* mode) { return modes[mode->GetModeChar()-'A']; }
131 /** Returns the parameter for a custom mode on a channel.
132 * @param mode The mode character you wish to query
134 * For example if "+L #foo" is set, and you pass this method
135 * 'L', it will return '\#foo'. If the mode is not set on the
136 * channel, or the mode has no parameters associated with it,
137 * it will return an empty string.
139 * @return The parameter for this mode is returned, or an empty string
141 std::string GetModeParameter(char mode);
142 std::string GetModeParameter(ModeHandler* mode);
144 /** Sets the channel topic.
145 * @param u The user setting the topic
146 * @param t The topic to set it to. Non-const, as it may be modified by a hook.
147 * @param forceset If set to true then all access checks will be bypassed.
149 int SetTopic(User *u, std::string &t, bool forceset = false);
151 /** Obtain the channel "user counter"
152 * This returns the number of users on this channel
154 * @return The number of users on this channel
156 long GetUserCounter() const { return userlist.size(); }
158 /** Add a user pointer to the internal reference list
159 * @param user The user to add
161 * The data inserted into the reference list is a table as it is
162 * an arbitary pointer compared to other users by its memory address,
163 * as this is a very fast 32 or 64 bit integer comparison.
165 Membership* AddUser(User* user);
167 /** Delete a user pointer to the internal reference list
168 * @param user The user to delete
169 * @return number of users left on the channel after deletion of the user
171 void DelUser(User* user);
173 /** Obtain the internal reference list
174 * The internal reference list contains a list of User*.
175 * These are used for rapid comparison to determine
176 * channel membership for PRIVMSG, NOTICE, QUIT, PART etc.
177 * The resulting pointer to the vector should be considered
178 * readonly and only modified via AddUser and DelUser.
180 * @return This function returns pointer to a map of User pointers (CUList*).
182 const UserMembList* GetUsers() const { return &userlist; }
184 /** Returns true if the user given is on the given channel.
185 * @param user The user to look for
186 * @return True if the user is on this channel
188 bool HasUser(User* user);
190 Membership* GetUser(User* user);
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 (must be on this channel)
195 * @param reason The reason for the kick
196 * @param srcmemb The membership of the user who does the kick, can be NULL
198 void KickUser(User* src, User* user, const std::string& reason, Membership* srcmemb = NULL);
200 /** Part a user from this channel with the given reason.
201 * If the reason field is NULL, no reason will be sent.
202 * @param user The user who is parting (must be on this channel)
203 * @param reason The part reason
205 void PartUser(User *user, std::string &reason);
207 /** Join a local user to a channel, with or without permission checks. May be a channel that doesn't exist yet.
208 * @param user The user to join to the channel.
209 * @param channame The channel name to join to. Does not have to exist.
210 * @param key The key of the channel, if given
211 * @param override If true, override all join restrictions such as +bkil
212 * @return A pointer to the Channel the user was joined to. A new Channel may have
213 * been created if the channel did not exist before the user was joined to it.
214 * If the user could not be joined to a channel, the return value is NULL.
216 static Channel* JoinUser(LocalUser* user, std::string channame, bool override = false, const std::string& key = "");
218 /** Join a user to an existing channel, without doing any permission checks
219 * @param user The user to join to the channel
220 * @param privs Priviliges (prefix mode letters) to give to this user, may be NULL
221 * @param bursting True if this join is the result of a netburst (passed to modules in the OnUserJoin hook)
222 * @param created True if this channel was just created by a local user (passed to modules in the OnUserJoin hook)
224 void ForceJoin(User* user, const std::string* privs = NULL, bool bursting = false, bool created_by_local = false);
226 /** Write to a channel, from a user, using va_args for text
227 * @param user User whos details to prefix the line with
228 * @param text A printf-style format string which builds the output line without prefix
229 * @param ... Zero or more POD types
231 void WriteChannel(User* user, const char* text, ...) CUSTOM_PRINTF(3, 4);
233 /** Write to a channel, from a user, using std::string for text
234 * @param user User whos details to prefix the line with
235 * @param text A std::string containing the output line without prefix
237 void WriteChannel(User* user, const std::string &text);
239 /** Write to a channel, from a server, using va_args for text
240 * @param ServName Server name to prefix the line with
241 * @param text A printf-style format string which builds the output line without prefix
242 * @param ... Zero or more POD type
244 void WriteChannelWithServ(const std::string& ServName, const char* text, ...) CUSTOM_PRINTF(3, 4);
246 /** Write to a channel, from a server, using std::string for text
247 * @param ServName Server name to prefix the line with
248 * @param text A std::string containing the output line without prefix
250 void WriteChannelWithServ(const std::string& ServName, const std::string &text);
252 /** Write to all users on a channel except a specific user, using va_args for text.
253 * Internally, this calls WriteAllExcept().
254 * @param user User whos details to prefix the line with, and to omit from receipt of the message
255 * @param serversource If this parameter is true, use the local server name as the source of the text, otherwise,
256 * use the nick!user\@host of the user.
257 * @param status The status of the users to write to, e.g. '@' or '%'. Use a value of 0 to write to everyone
258 * @param text A printf-style format string which builds the output line without prefix
259 * @param ... Zero or more POD type
261 void WriteAllExceptSender(User* user, bool serversource, char status, const char* text, ...) CUSTOM_PRINTF(5, 6);
263 /** Write to all users on a channel except a list of users, using va_args for text
264 * @param user User whos details to prefix the line with, and to omit from receipt of the message
265 * @param serversource If this parameter is true, use the local server name as the source of the text, otherwise,
266 * use the nick!user\@host of the user.
267 * @param status The status of the users to write to, e.g. '@' or '%'. Use a value of 0 to write to everyone
268 * @param except_list A list of users NOT to send the text to
269 * @param text A printf-style format string which builds the output line without prefix
270 * @param ... Zero or more POD type
272 void WriteAllExcept(User* user, bool serversource, char status, CUList &except_list, const char* text, ...) CUSTOM_PRINTF(6, 7);
274 /** Write to all users on a channel except a specific user, using std::string for text.
275 * Internally, this calls WriteAllExcept().
276 * @param user User whos details to prefix the line with, and to omit from receipt of the message
277 * @param serversource If this parameter is true, use the local server name as the source of the text, otherwise,
278 * use the nick!user\@host of the user.
279 * @param status The status of the users to write to, e.g. '@' or '%'. Use a value of 0 to write to everyone
280 * @param text A std::string containing the output line without prefix
282 void WriteAllExceptSender(User* user, bool serversource, char status, const std::string& text);
284 /** Write to all users on a channel except a list of users, using std::string for text
285 * @param user User whos details to prefix the line with, and to omit from receipt of the message
286 * @param serversource If this parameter is true, use the local server name as the source of the text, otherwise,
287 * use the nick!user\@host of the user.
288 * @param status The status of the users to write to, e.g. '@' or '%'. Use a value of 0 to write to everyone
289 * @param except_list A list of users NOT to send the text to
290 * @param text A std::string containing the output line without prefix
292 void WriteAllExcept(User* user, bool serversource, char status, CUList &except_list, const std::string& text);
293 /** Write a line of text that already includes the source */
294 void RawWriteAllExcept(User* user, bool serversource, char status, CUList &except_list, const std::string& text);
296 /** Return the channel's modes with parameters.
297 * @param showkey If this is set to true, the actual key is shown,
298 * otherwise it is replaced with '<KEY>'
299 * @return The channel mode string
301 const char* ChanModes(bool showkey);
303 /** Spool the NAMES list for this channel to the given user
304 * @param user The user to spool the NAMES list to
306 void UserList(User *user);
308 /** Get a users prefix on this channel in a string.
309 * @param user The user to look up
310 * @return A character array containing the prefix string.
311 * Unlike GetStatus and GetStatusFlags which will only return the
312 * core specified modes @, % and + (op, halfop and voice), GetPrefixChar
313 * will also return module-defined prefixes. If the user has to prefix,
314 * an empty but non-null string is returned. If the user has multiple
315 * prefixes, the highest is returned. If you do not recognise the prefix
316 * character you can get, you can deal with it in a 'proprtional' manner
317 * compared to known prefixes, using GetPrefixValue().
319 const char* GetPrefixChar(User *user);
321 /** Return all of a users mode prefixes into a char* string.
322 * @param user The user to look up
323 * @return A list of all prefix characters. The prefixes will always
324 * be in rank order, greatest first, as certain IRC clients require
325 * this when multiple prefixes are used names lists.
327 const char* GetAllPrefixChars(User* user);
329 /** Get the value of a users prefix on this channel.
330 * @param user The user to look up
331 * @return The module or core-defined value of the users prefix.
332 * The values for op, halfop and voice status are constants in
333 * mode.h, and are OP_VALUE, HALFOP_VALUE, and VOICE_VALUE respectively.
334 * If the value you are given does not match one of these three, it is
335 * a module-defined mode, and it should be compared in proportion to
336 * these three constants. For example a value greater than OP_VALUE
337 * is a prefix of greater 'worth' than ops, and a value less than
338 * VOICE_VALUE is of lesser 'worth' than a voice.
340 unsigned int GetPrefixValue(User* user);
342 /** Add a prefix character to a user.
343 * Only the core should call this method, usually from
344 * within the mode parser or when the first user joins
345 * the channel (to grant ops to them)
346 * @param user The user to associate the privilage with
347 * @param prefix The prefix character to associate
348 * @param adding True if adding the prefix, false when removing
349 * @return True if a change was made
351 bool SetPrefix(User* user, char prefix, bool adding);
353 /** Check if a user is banned on this channel
354 * @param user A user to check against the banlist
355 * @returns True if the user given is banned
357 bool IsBanned(User* user);
359 /** Check a single ban for match
361 bool CheckBan(User* user, const std::string& banmask);
363 /** Get the status of an "action" type extban
365 ModResult GetExtBanStatus(User *u, char type);
368 inline bool Channel::HasUser(User* user)
370 return (userlist.find(user) != userlist.end());