1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2009 InspIRCd Development Team
6 * See: http://wiki.inspircd.org/Credits
8 * This program is free but copyrighted software; see
9 * the file COPYING for details.
11 * ---------------------------------------------------
14 #ifndef __CHANNELS_H__
15 #define __CHANNELS_H__
17 #include "membership.h"
19 /** RFC1459 channel modes
22 CM_TOPICLOCK = 't'-65, /* +t: Only operators can change topic */
23 CM_NOEXTERNAL = 'n'-65, /* +n: Only users in the channel can message it */
24 CM_INVITEONLY = 'i'-65, /* +i: Invite only */
25 CM_MODERATED = 'm'-65, /* +m: Only voices and above can talk */
26 CM_SECRET = 's'-65, /* +s: Secret channel */
27 CM_PRIVATE = 'p'-65, /* +p: Private channel */
28 CM_KEY = 'k'-65, /* +k: Keyed channel */
29 CM_LIMIT = 'l'-65 /* +l: Maximum user limit */
32 /* Forward declarations - needed */
36 /** Holds an entry for a ban list, exemption list, or invite list.
37 * This class contains a single element in a channel list, such as a banlist.
39 class HostItem : public classbase
42 /** Time the item was added
45 /** Who added the item
48 /** The actual item data
52 HostItem() { /* stub */ }
53 virtual ~HostItem() { /* stub */ }
56 /** A subclass of HostItem designed to hold channel bans (+b)
58 class BanItem : public HostItem
62 /** Holds a complete ban list
64 typedef std::list<BanItem> BanList;
66 /** A list of custom modes parameters on a channel
68 typedef std::map<char,std::string> CustomModeList;
70 /** used to hold a channel and a users modes on that channel, e.g. +v, +h, +o
72 enum UserChannelModes {
73 UCMODE_OP = 1, /* Opped user */
74 UCMODE_VOICE = 2, /* Voiced user */
75 UCMODE_HOP = 4 /* Halfopped user */
78 /** Holds all relevent information for a channel.
79 * This class represents a channel, and contains its name, modes, topic, topic set time,
80 * etc, and an instance of the BanList type.
82 class CoreExport Channel : public Extensible
84 /** Connect a Channel to a User
86 static Channel* ForceChan(Channel* Ptr, User* user, const std::string &privs, bool bursting, bool created);
88 /** Set default modes for the channel on creation
90 void SetDefaultModes();
92 /** Maximum number of bans (cached)
97 /** Creates a channel record and initialises it with default values
98 * @throw Nothing at present.
100 Channel(const std::string &name, time_t ts);
102 /** The channel's name.
104 std::string name; /* CHANMAX */
106 /** Time that the object was instantiated (used for TS calculation etc)
110 /** Modes for the channel.
111 * This is not a null terminated string! It is a bitset where
112 * each item in it represents if a mode is set. For example
113 * for mode +A, index 0. Use modechar-65 to calculate which
116 std::bitset<64> modes;
120 UserMembList userlist;
122 /** Parameters for custom modes.
123 * One for each custom mode letter.
125 CustomModeList custom_mode_params;
128 * If this is an empty string, no channel topic is set.
130 std::string topic; /* MAXTOPIC */
132 /** Time topic was set.
133 * If no topic was ever set, this will be equal to Channel::created
137 /** The last user to set the topic.
138 * If this member is an empty string, no topic was ever set.
140 std::string setby; /* 128 */
142 /** The list of all bans set on the channel.
146 /** Sets or unsets a custom mode in the channels info
147 * @param mode The mode character to set or unset
148 * @param mode_on True if you want to set the mode or false if you want to remove it
150 void SetMode(char mode,bool mode_on);
152 /** Sets or unsets a custom mode in the channels info
153 * @param mode The mode character to set or unset
154 * @param parameter The parameter string to associate with this mode character.
155 * If it is empty, the mode is unset; if it is nonempty, the mode is set.
157 void SetModeParam(char mode, std::string parameter);
159 /** Returns true if a mode is set on a channel
160 * @param mode The mode character you wish to query
161 * @return True if the custom mode is set, false if otherwise
163 inline bool IsModeSet(char mode) { return modes[mode-'A']; }
166 /** Returns the parameter for a custom mode on a channel.
167 * @param mode The mode character you wish to query
169 * For example if "+L #foo" is set, and you pass this method
170 * 'L', it will return '#foo'. If the mode is not set on the
171 * channel, or the mode has no parameters associated with it,
172 * it will return an empty string.
174 * @return The parameter for this mode is returned, or an empty string
176 std::string GetModeParameter(char mode);
178 /** Sets the channel topic.
179 * @param u The user setting the topic
180 * @param t The topic to set it to. Non-const, as it may be modified by a hook.
181 * @param forceset If set to true then all access checks will be bypassed.
183 int SetTopic(User *u, std::string &t, bool forceset = false);
185 /** Obtain the channel "user counter"
186 * This returns the channel reference counter, which is initialized
187 * to 0 when the channel is created and incremented/decremented
188 * upon joins, parts quits and kicks.
190 * @return The number of users on this channel
192 long GetUserCounter();
194 /** Add a user pointer to the internal reference list
195 * @param user The user to add
197 * The data inserted into the reference list is a table as it is
198 * an arbitary pointer compared to other users by its memory address,
199 * as this is a very fast 32 or 64 bit integer comparison.
201 Membership* AddUser(User* user);
203 /** Delete a user pointer to the internal reference list
204 * @param user The user to delete
205 * @return number of users left on the channel after deletion of the user
207 unsigned long DelUser(User* user);
209 /** Obtain the internal reference list
210 * The internal reference list contains a list of User*.
211 * These are used for rapid comparison to determine
212 * channel membership for PRIVMSG, NOTICE, QUIT, PART etc.
213 * The resulting pointer to the vector should be considered
214 * readonly and only modified via AddUser and DelUser.
216 * @return This function returns pointer to a map of User pointers (CUList*).
218 const UserMembList* GetUsers();
220 /** Returns true if the user given is on the given channel.
221 * @param The user to look for
222 * @return True if the user is on this channel
224 bool HasUser(User* user);
226 Membership* GetUser(User* user);
228 /** Make src kick user from this channel with the given reason.
229 * @param src The source of the kick
230 * @param user The user being kicked (must be on this channel)
231 * @param reason The reason for the kick
232 * @return The number of users left on the channel. If this is zero
233 * when the method returns, you MUST delete the Channel immediately!
235 long KickUser(User *src, User *user, const char* reason);
237 /** Make the server kick user from this channel with the given reason.
238 * @param user The user being kicked (must be on this channel)
239 * @param reason The reason for the kick
240 * @param triggerevents True if you wish this kick to trigger module events
241 * @return The number of users left on the channel. If this is zero
242 * when the method returns, you MUST delete the Channel immediately!
244 long ServerKickUser(User* user, const char* reason, const std::string& servername = "");
246 /** Part a user from this channel with the given reason.
247 * If the reason field is NULL, no reason will be sent.
248 * @param user The user who is parting (must be on this channel)
249 * @param reason The part reason
250 * @return The number of users left on the channel. If this is zero
251 * when the method returns, you MUST delete the Channel immediately!
253 long PartUser(User *user, std::string &reason);
255 /* Join a user to a channel. May be a channel that doesnt exist yet.
256 * @param user The user to join to the channel.
257 * @param cn The channel name to join to. Does not have to exist.
258 * @param key The key of the channel, if given
259 * @param override If true, override all join restrictions such as +bkil
260 * @return A pointer to the Channel the user was joined to. A new Channel may have
261 * been created if the channel did not exist before the user was joined to it.
262 * If the user could not be joined to a channel, the return value may be NULL.
264 static Channel* JoinUser(User *user, const char* cn, bool override, const char* key, bool bursting, time_t TS = 0);
266 /** Write to a channel, from a user, using va_args for text
267 * @param user User whos details to prefix the line with
268 * @param text A printf-style format string which builds the output line without prefix
269 * @param ... Zero or more POD types
271 void WriteChannel(User* user, const char* text, ...) CUSTOM_PRINTF(3, 4);
273 /** Write to a channel, from a user, using std::string for text
274 * @param user User whos details to prefix the line with
275 * @param text A std::string containing the output line without prefix
277 void WriteChannel(User* user, const std::string &text);
279 /** Write to a channel, from a server, using va_args for text
280 * @param ServName Server name to prefix the line with
281 * @param text A printf-style format string which builds the output line without prefix
282 * @param ... Zero or more POD type
284 void WriteChannelWithServ(const std::string& ServName, const char* text, ...) CUSTOM_PRINTF(3, 4);
286 /** Write to a channel, from a server, using std::string for text
287 * @param ServName Server name to prefix the line with
288 * @param text A std::string containing the output line without prefix
290 void WriteChannelWithServ(const std::string& ServName, const std::string &text);
292 /** Write to all users on a channel except a specific user, using va_args for text.
293 * Internally, this calls WriteAllExcept().
294 * @param user User whos details to prefix the line with, and to omit from receipt of the message
295 * @param serversource If this parameter is true, use the local server name as the source of the text, otherwise,
296 * use the nick!user@host of the user.
297 * @param status The status of the users to write to, e.g. '@' or '%'. Use a value of 0 to write to everyone
298 * @param text A printf-style format string which builds the output line without prefix
299 * @param ... Zero or more POD type
301 void WriteAllExceptSender(User* user, bool serversource, char status, const char* text, ...) CUSTOM_PRINTF(5, 6);
303 /** Write to all users on a channel except a list of users, using va_args for text
304 * @param user User whos details to prefix the line with, and to omit from receipt of the message
305 * @param serversource If this parameter is true, use the local server name as the source of the text, otherwise,
306 * use the nick!user@host of the user.
307 * @param status The status of the users to write to, e.g. '@' or '%'. Use a value of 0 to write to everyone
308 * @param except_list A list of users NOT to send the text to
309 * @param text A printf-style format string which builds the output line without prefix
310 * @param ... Zero or more POD type
312 void WriteAllExcept(User* user, bool serversource, char status, CUList &except_list, const char* text, ...) CUSTOM_PRINTF(6, 7);
314 /** Write to all users on a channel except a specific user, using std::string for text.
315 * Internally, this calls WriteAllExcept().
316 * @param user User whos details to prefix the line with, and to omit from receipt of the message
317 * @param serversource If this parameter is true, use the local server name as the source of the text, otherwise,
318 * use the nick!user@host of the user.
319 * @param status The status of the users to write to, e.g. '@' or '%'. Use a value of 0 to write to everyone
320 * @param text A std::string containing the output line without prefix
322 void WriteAllExceptSender(User* user, bool serversource, char status, const std::string& text);
324 /** Write to all users on a channel except a list of users, using std::string for text
325 * @param user User whos details to prefix the line with, and to omit from receipt of the message
326 * @param serversource If this parameter is true, use the local server name as the source of the text, otherwise,
327 * use the nick!user@host of the user.
328 * @param status The status of the users to write to, e.g. '@' or '%'. Use a value of 0 to write to everyone
329 * @param except_list A list of users NOT to send the text to
330 * @param text A std::string containing the output line without prefix
332 void WriteAllExcept(User* user, bool serversource, char status, CUList &except_list, const std::string& text);
333 /** Write a line of text that already includes the source */
334 void RawWriteAllExcept(User* user, bool serversource, char status, CUList &except_list, const std::string& text);
336 /** Returns the maximum number of bans allowed to be set on this channel
337 * @return The maximum number of bans allowed
341 /** Return the channel's modes with parameters.
342 * @param showkey If this is set to true, the actual key is shown,
343 * otherwise it is replaced with '<KEY>'
344 * @return The channel mode string
346 char* ChanModes(bool showkey);
348 /** Spool the NAMES list for this channel to the given user
349 * @param user The user to spool the NAMES list to
351 void UserList(User *user);
353 /** Get the number of invisible users on this channel
354 * @return Number of invisible users
356 int CountInvisible();
358 /** Get a users prefix on this channel in a string.
359 * @param user The user to look up
360 * @return A character array containing the prefix string.
361 * Unlike GetStatus and GetStatusFlags which will only return the
362 * core specified modes @, % and + (op, halfop and voice), GetPrefixChar
363 * will also return module-defined prefixes. If the user has to prefix,
364 * an empty but non-null string is returned. If the user has multiple
365 * prefixes, the highest is returned. If you do not recognise the prefix
366 * character you can get, you can deal with it in a 'proprtional' manner
367 * compared to known prefixes, using GetPrefixValue().
369 const char* GetPrefixChar(User *user);
371 /** Return all of a users mode prefixes into a char* string.
372 * @param user The user to look up
373 * @return A list of all prefix characters. The prefixes will always
374 * be in rank order, greatest first, as certain IRC clients require
375 * this when multiple prefixes are used names lists.
377 const char* GetAllPrefixChars(User* user);
379 /** Get the value of a users prefix on this channel.
380 * @param user The user to look up
381 * @return The module or core-defined value of the users prefix.
382 * The values for op, halfop and voice status are constants in
383 * mode.h, and are OP_VALUE, HALFOP_VALUE, and VOICE_VALUE respectively.
384 * If the value you are given does not match one of these three, it is
385 * a module-defined mode, and it should be compared in proportion to
386 * these three constants. For example a value greater than OP_VALUE
387 * is a prefix of greater 'worth' than ops, and a value less than
388 * VOICE_VALUE is of lesser 'worth' than a voice.
390 unsigned int GetPrefixValue(User* user);
392 /** This method removes all prefix characters from a user.
393 * It will not inform the user or the channel of the removal of prefixes,
394 * and should be used when the user parts or quits.
395 * @param user The user to remove all prefixes from
397 void RemoveAllPrefixes(User* user);
399 /** Add a prefix character to a user.
400 * Only the core should call this method, usually from
401 * within the mode parser or when the first user joins
402 * the channel (to grant ops to them)
403 * @param user The user to associate the privilage with
404 * @param prefix The prefix character to associate
405 * @param adding True if adding the prefix, false when removing
407 void SetPrefix(User* user, char prefix, bool adding);
409 /** Check if a user is banned on this channel
410 * @param user A user to check against the banlist
411 * @returns True if the user given is banned
413 bool IsBanned(User* user);
415 /** Check a single ban for match
417 bool CheckBan(User* user, const std::string& banmask);
419 /** Get the status of an "action" type extban
421 ModResult GetExtBanStatus(User *u, char type);
423 /** Clears the cached max bans value
427 /** Destructor for Channel
429 virtual ~Channel() { /* stub */ }