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(InspIRCd* Instance, 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 /** Pointer to creator object
99 InspIRCd* ServerInstance;
101 /** Creates a channel record and initialises it with default values
102 * @throw Nothing at present.
104 Channel(InspIRCd* Instance, const std::string &name, time_t ts);
106 /** The channel's name.
108 std::string name; /* CHANMAX */
110 /** Time that the object was instantiated (used for TS calculation etc)
114 /** Modes for the channel.
115 * This is not a null terminated string! It is a bitset where
116 * each item in it represents if a mode is set. For example
117 * for mode +A, index 0. Use modechar-65 to calculate which
120 std::bitset<64> modes;
124 UserMembList userlist;
126 /** Parameters for custom modes.
127 * One for each custom mode letter.
129 CustomModeList custom_mode_params;
132 * If this is an empty string, no channel topic is set.
134 std::string topic; /* MAXTOPIC */
136 /** Time topic was set.
137 * If no topic was ever set, this will be equal to Channel::created
141 /** The last user to set the topic.
142 * If this member is an empty string, no topic was ever set.
144 std::string setby; /* 128 */
146 /** The list of all bans set on the channel.
150 /** Sets or unsets a custom mode in the channels info
151 * @param mode The mode character to set or unset
152 * @param mode_on True if you want to set the mode or false if you want to remove it
154 void SetMode(char mode,bool mode_on);
156 /** Sets or unsets a custom mode in the channels info
157 * @param mode The mode character to set or unset
158 * @param parameter The parameter string to associate with this mode character.
159 * If it is empty, the mode is unset; if it is nonempty, the mode is set.
161 void SetModeParam(char mode, std::string parameter);
163 /** Returns true if a mode is set on a channel
164 * @param mode The mode character you wish to query
165 * @return True if the custom mode is set, false if otherwise
167 inline bool IsModeSet(char mode) { return modes[mode-'A']; }
170 /** Returns the parameter for a custom mode on a channel.
171 * @param mode The mode character you wish to query
173 * For example if "+L #foo" is set, and you pass this method
174 * 'L', it will return '#foo'. If the mode is not set on the
175 * channel, or the mode has no parameters associated with it,
176 * it will return an empty string.
178 * @return The parameter for this mode is returned, or an empty string
180 std::string GetModeParameter(char mode);
182 /** Sets the channel topic.
183 * @param u The user setting the topic
184 * @param t The topic to set it to. Non-const, as it may be modified by a hook.
185 * @param forceset If set to true then all access checks will be bypassed.
187 int SetTopic(User *u, std::string &t, bool forceset = false);
189 /** Obtain the channel "user counter"
190 * This returns the channel reference counter, which is initialized
191 * to 0 when the channel is created and incremented/decremented
192 * upon joins, parts quits and kicks.
194 * @return The number of users on this channel
196 long GetUserCounter();
198 /** Add a user pointer to the internal reference list
199 * @param user The user to add
201 * The data inserted into the reference list is a table as it is
202 * an arbitary pointer compared to other users by its memory address,
203 * as this is a very fast 32 or 64 bit integer comparison.
205 Membership* AddUser(User* user);
207 /** Delete a user pointer to the internal reference list
208 * @param user The user to delete
209 * @return number of users left on the channel after deletion of the user
211 unsigned long DelUser(User* user);
213 /** Obtain the internal reference list
214 * The internal reference list contains a list of User*.
215 * These are used for rapid comparison to determine
216 * channel membership for PRIVMSG, NOTICE, QUIT, PART etc.
217 * The resulting pointer to the vector should be considered
218 * readonly and only modified via AddUser and DelUser.
220 * @return This function returns pointer to a map of User pointers (CUList*).
222 const UserMembList* GetUsers();
224 /** Returns true if the user given is on the given channel.
225 * @param The user to look for
226 * @return True if the user is on this channel
228 bool HasUser(User* user);
230 Membership* GetUser(User* user);
232 /** Make src kick user from this channel with the given reason.
233 * @param src The source of the kick
234 * @param user The user being kicked (must be on this channel)
235 * @param reason The reason for the kick
236 * @return The number of users left on the channel. If this is zero
237 * when the method returns, you MUST delete the Channel immediately!
239 long KickUser(User *src, User *user, const char* reason);
241 /** Make the server kick user from this channel with the given reason.
242 * @param user The user being kicked (must be on this channel)
243 * @param reason The reason for the kick
244 * @param triggerevents True if you wish this kick to trigger module events
245 * @return The number of users left on the channel. If this is zero
246 * when the method returns, you MUST delete the Channel immediately!
248 long ServerKickUser(User* user, const char* reason, const char* servername = NULL);
250 /** Part a user from this channel with the given reason.
251 * If the reason field is NULL, no reason will be sent.
252 * @param user The user who is parting (must be on this channel)
253 * @param reason The part reason
254 * @return The number of users left on the channel. If this is zero
255 * when the method returns, you MUST delete the Channel immediately!
257 long PartUser(User *user, std::string &reason);
259 /* Join a user to a channel. May be a channel that doesnt exist yet.
260 * @param user The user to join to the channel.
261 * @param cn The channel name to join to. Does not have to exist.
262 * @param key The key of the channel, if given
263 * @param override If true, override all join restrictions such as +bkil
264 * @return A pointer to the Channel the user was joined to. A new Channel may have
265 * been created if the channel did not exist before the user was joined to it.
266 * If the user could not be joined to a channel, the return value may be NULL.
268 static Channel* JoinUser(InspIRCd* ServerInstance, User *user, const char* cn, bool override, const char* key, bool bursting, time_t TS = 0);
270 /** Write to a channel, from a user, using va_args for text
271 * @param user User whos details to prefix the line with
272 * @param text A printf-style format string which builds the output line without prefix
273 * @param ... Zero or more POD types
275 void WriteChannel(User* user, const char* text, ...) CUSTOM_PRINTF(3, 4);
277 /** Write to a channel, from a user, using std::string for text
278 * @param user User whos details to prefix the line with
279 * @param text A std::string containing the output line without prefix
281 void WriteChannel(User* user, const std::string &text);
283 /** Write to a channel, from a server, using va_args for text
284 * @param ServName Server name to prefix the line with
285 * @param text A printf-style format string which builds the output line without prefix
286 * @param ... Zero or more POD type
288 void WriteChannelWithServ(const char* ServName, const char* text, ...) CUSTOM_PRINTF(3, 4);
290 /** Write to a channel, from a server, using std::string for text
291 * @param ServName Server name to prefix the line with
292 * @param text A std::string containing the output line without prefix
294 void WriteChannelWithServ(const char* ServName, const std::string &text);
296 /** Write to all users on a channel except a specific user, using va_args for text.
297 * Internally, this calls WriteAllExcept().
298 * @param user User whos details to prefix the line with, and to omit from receipt of the message
299 * @param serversource If this parameter is true, use the local server name as the source of the text, otherwise,
300 * use the nick!user@host of the user.
301 * @param status The status of the users to write to, e.g. '@' or '%'. Use a value of 0 to write to everyone
302 * @param text A printf-style format string which builds the output line without prefix
303 * @param ... Zero or more POD type
305 void WriteAllExceptSender(User* user, bool serversource, char status, const char* text, ...) CUSTOM_PRINTF(5, 6);
307 /** Write to all users on a channel except a list of users, using va_args for text
308 * @param user User whos details to prefix the line with, and to omit from receipt of the message
309 * @param serversource If this parameter is true, use the local server name as the source of the text, otherwise,
310 * use the nick!user@host of the user.
311 * @param status The status of the users to write to, e.g. '@' or '%'. Use a value of 0 to write to everyone
312 * @param except_list A list of users NOT to send the text to
313 * @param text A printf-style format string which builds the output line without prefix
314 * @param ... Zero or more POD type
316 void WriteAllExcept(User* user, bool serversource, char status, CUList &except_list, const char* text, ...) CUSTOM_PRINTF(6, 7);
318 /** Write to all users on a channel except a specific user, using std::string for text.
319 * Internally, this calls WriteAllExcept().
320 * @param user User whos details to prefix the line with, and to omit from receipt of the message
321 * @param serversource If this parameter is true, use the local server name as the source of the text, otherwise,
322 * use the nick!user@host of the user.
323 * @param status The status of the users to write to, e.g. '@' or '%'. Use a value of 0 to write to everyone
324 * @param text A std::string containing the output line without prefix
326 void WriteAllExceptSender(User* user, bool serversource, char status, const std::string& text);
328 /** Write to all users on a channel except a list of users, using std::string for text
329 * @param user User whos details to prefix the line with, and to omit from receipt of the message
330 * @param serversource If this parameter is true, use the local server name as the source of the text, otherwise,
331 * use the nick!user@host of the user.
332 * @param status The status of the users to write to, e.g. '@' or '%'. Use a value of 0 to write to everyone
333 * @param except_list A list of users NOT to send the text to
334 * @param text A std::string containing the output line without prefix
336 void WriteAllExcept(User* user, bool serversource, char status, CUList &except_list, const std::string& text);
337 /** Write a line of text that already includes the source */
338 void RawWriteAllExcept(User* user, bool serversource, char status, CUList &except_list, const std::string& text);
340 /** Returns the maximum number of bans allowed to be set on this channel
341 * @return The maximum number of bans allowed
345 /** Return the channel's modes with parameters.
346 * @param showkey If this is set to true, the actual key is shown,
347 * otherwise it is replaced with '<KEY>'
348 * @return The channel mode string
350 char* ChanModes(bool showkey);
352 /** Spool the NAMES list for this channel to the given user
353 * @param user The user to spool the NAMES list to
355 void UserList(User *user);
357 /** Get the number of invisible users on this channel
358 * @return Number of invisible users
360 int CountInvisible();
362 /** Get a users prefix on this channel in a string.
363 * @param user The user to look up
364 * @return A character array containing the prefix string.
365 * Unlike GetStatus and GetStatusFlags which will only return the
366 * core specified modes @, % and + (op, halfop and voice), GetPrefixChar
367 * will also return module-defined prefixes. If the user has to prefix,
368 * an empty but non-null string is returned. If the user has multiple
369 * prefixes, the highest is returned. If you do not recognise the prefix
370 * character you can get, you can deal with it in a 'proprtional' manner
371 * compared to known prefixes, using GetPrefixValue().
373 const char* GetPrefixChar(User *user);
375 /** Return all of a users mode prefixes into a char* string.
376 * @param user The user to look up
377 * @return A list of all prefix characters. The prefixes will always
378 * be in rank order, greatest first, as certain IRC clients require
379 * this when multiple prefixes are used names lists.
381 const char* GetAllPrefixChars(User* user);
383 /** Get the value of a users prefix on this channel.
384 * @param user The user to look up
385 * @return The module or core-defined value of the users prefix.
386 * The values for op, halfop and voice status are constants in
387 * mode.h, and are OP_VALUE, HALFOP_VALUE, and VOICE_VALUE respectively.
388 * If the value you are given does not match one of these three, it is
389 * a module-defined mode, and it should be compared in proportion to
390 * these three constants. For example a value greater than OP_VALUE
391 * is a prefix of greater 'worth' than ops, and a value less than
392 * VOICE_VALUE is of lesser 'worth' than a voice.
394 unsigned int GetPrefixValue(User* user);
396 /** This method removes all prefix characters from a user.
397 * It will not inform the user or the channel of the removal of prefixes,
398 * and should be used when the user parts or quits.
399 * @param user The user to remove all prefixes from
401 void RemoveAllPrefixes(User* user);
403 /** Add a prefix character to a user.
404 * Only the core should call this method, usually from
405 * within the mode parser or when the first user joins
406 * the channel (to grant ops to them)
407 * @param user The user to associate the privilage with
408 * @param prefix The prefix character to associate
409 * @param prefix_rank The rank (value) of this prefix character
410 * @param adding True if adding the prefix, false when removing
412 void SetPrefix(User* user, char prefix, unsigned int prefix_rank, bool adding);
414 /** Check if a user is banned on this channel
415 * @param user A user to check against the banlist
416 * @returns True if the user given is banned
418 bool IsBanned(User* user);
420 /** Check whether an extban of a given type matches
421 * a given user for this channel.
422 * @param u The user to match bans against
423 * @param type The type of extban to check
425 ModResult GetExtBanStatus(User *u, char type);
427 /** Overloaded version to check whether a particular string is extbanned
429 ModResult GetExtBanStatus(const std::string &str, char type);
431 /** Clears the cached max bans value
435 /** Destructor for Channel
437 virtual ~Channel() { /* stub */ }