1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2007 InspIRCd Development Team
6 * See: http://www.inspircd.org/wiki/index.php/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 "inspircd_config.h"
24 /** RFC1459 channel modes
27 CM_TOPICLOCK = 't'-65, /* +t: Only operators can change topic */
28 CM_NOEXTERNAL = 'n'-65, /* +n: Only users in the channel can message it */
29 CM_INVITEONLY = 'i'-65, /* +i: Invite only */
30 CM_MODERATED = 'm'-65, /* +m: Only voices and above can talk */
31 CM_SECRET = 's'-65, /* +s: Secret channel */
32 CM_PRIVATE = 'p'-65, /* +p: Private channel */
33 CM_KEY = 'k'-65, /* +k: Keyed channel */
34 CM_LIMIT = 'l'-65 /* +l: Maximum user limit */
37 /* Forward declarations - needed */
41 /** Holds an entry for a ban list, exemption list, or invite list.
42 * This class contains a single element in a channel list, such as a banlist.
44 class HostItem : public classbase
47 /** Time the item was added
50 /** Who added the item
53 /** The actual item data
57 HostItem() { /* stub */ }
58 virtual ~HostItem() { /* stub */ }
61 /** A subclass of HostItem designed to hold channel bans (+b)
63 class BanItem : public HostItem
67 /** Holds a complete ban list
69 typedef std::vector<BanItem> BanList;
71 /** A list of users on a channel
73 typedef std::map<User*,std::string> CUList;
75 /** Shorthand for CUList::iterator
77 typedef CUList::iterator CUListIter;
79 /** Shorthand for CUList::const_iterator
81 typedef CUList::const_iterator CUListConstIter;
83 /** A list of custom modes parameters on a channel
85 typedef std::map<char,char*> CustomModeList;
88 /** used to hold a channel and a users modes on that channel, e.g. +v, +h, +o
90 enum UserChannelModes {
91 UCMODE_OP = 1, /* Opped user */
92 UCMODE_VOICE = 2, /* Voiced user */
93 UCMODE_HOP = 4 /* Halfopped user */
96 /** A stored prefix and its rank
98 typedef std::pair<char, unsigned int> prefixtype;
100 /** A list of prefixes set on a user in a channel
102 typedef std::vector<prefixtype> pfxcontainer;
104 /** A list of users with zero or more prefixes set on them
106 typedef std::map<User*, std::vector<prefixtype> > prefixlist;
108 /** Holds all relevent information for a channel.
109 * This class represents a channel, and contains its name, modes, time created, topic, topic set time,
110 * etc, and an instance of the BanList type.
112 class CoreExport Channel : public Extensible
116 /** Pointer to creator object
118 InspIRCd* ServerInstance;
120 /** Connect a Channel to a User
122 static Channel* ForceChan(InspIRCd* Instance, Channel* Ptr, User* user, const std::string &privs, bool bursting);
124 /** Set default modes for the channel on creation
126 void SetDefaultModes();
128 /** A list of prefixes associated with each user in the channel
133 /** Maximum number of bans (cached)
138 /** The channel's name.
142 /** Modes for the channel.
143 * This is not a null terminated string! It is a hash where
144 * each item in it represents if a mode is set. For example
145 * for mode +A, index 0. Use modechar-65 to calculate which
151 * There are four user lists, one for
152 * all the users, one for the ops, one for
153 * the halfops and another for the voices.
155 CUList internal_userlist;
158 * There are four user lists, one for
159 * all the users, one for the ops, one for
160 * the halfops and another for the voices.
162 CUList internal_op_userlist;
165 * There are four user lists, one for
166 * all the users, one for the ops, one for
167 * the halfops and another for the voices.
169 CUList internal_halfop_userlist;
172 * There are four user lists, one for
173 * all the users, one for the ops, one for
174 * the halfops and another for the voices.
176 CUList internal_voice_userlist;
178 /** Parameters for custom modes.
179 * One for each custom mode letter.
181 CustomModeList custom_mode_params;
184 * If this is an empty string, no channel topic is set.
186 char topic[MAXTOPIC];
189 * This is a timestamp (TS) value.
193 /** Time topic was set.
194 * If no topic was ever set, this will be equal to Channel::created
198 /** The last user to set the topic.
199 * If this member is an empty string, no topic was ever set.
203 /** Contains the channel user limit.
204 * If this value is zero, there is no limit in place.
208 /** Contains the channel key.
209 * If this value is an empty string, there is no channel key in place.
213 /** The list of all bans set on the channel.
217 /** Sets or unsets a custom mode in the channels info
218 * @param mode The mode character to set or unset
219 * @param mode_on True if you want to set the mode or false if you want to remove it
221 void SetMode(char mode,bool mode_on);
223 /** Sets or unsets the parameters for a custom mode in a channels info
224 * @param mode The mode character to set or unset
225 * @param parameter The parameter string to associate with this mode character
226 * @param mode_on True if you want to set the mode or false if you want to remove it
228 void SetModeParam(char mode,const char* parameter,bool mode_on);
230 /** Returns true if a mode is set on a channel
231 * @param mode The mode character you wish to query
232 * @return True if the custom mode is set, false if otherwise
234 bool IsModeSet(char mode);
236 /** Returns the parameter for a custom mode on a channel.
237 * @param mode The mode character you wish to query
239 * For example if "+L #foo" is set, and you pass this method
240 * 'L', it will return '#foo'. If the mode is not set on the
241 * channel, or the mode has no parameters associated with it,
242 * it will return an empty string.
244 * @return The parameter for this mode is returned, or an empty string
246 std::string GetModeParameter(char mode);
248 /** Obtain the channel "user counter"
249 * This returns the channel reference counter, which is initialized
250 * to 0 when the channel is created and incremented/decremented
251 * upon joins, parts quits and kicks.
253 * @return The number of users on this channel
255 long GetUserCounter();
257 /** Add a user pointer to the internal reference list
258 * @param user The user to add
260 * The data inserted into the reference list is a table as it is
261 * an arbitary pointer compared to other users by its memory address,
262 * as this is a very fast 32 or 64 bit integer comparison.
264 void AddUser(User* user);
266 /** Add a user pointer to the internal reference list of opped users
267 * @param user The user to add
269 void AddOppedUser(User* user);
271 /** Add a user pointer to the internal reference list of halfopped users
272 * @param user The user to add
274 void AddHalfoppedUser(User* user);
276 /** Add a user pointer to the internal reference list of voiced users
277 * @param user The user to add
279 void AddVoicedUser(User* user);
281 /** Delete a user pointer to the internal reference list
282 * @param user The user to delete
283 * @return number of users left on the channel after deletion of the user
285 unsigned long DelUser(User* user);
287 /** Delete a user pointer to the internal reference list of opped users
288 * @param user The user to delete
290 void DelOppedUser(User* user);
292 /** Delete a user pointer to the internal reference list of halfopped users
293 * @param user The user to delete
295 void DelHalfoppedUser(User* user);
297 /** Delete a user pointer to the internal reference list of voiced users
298 * @param user The user to delete
300 void DelVoicedUser(User* user);
302 /** Obtain the internal reference list
303 * The internal reference list contains a list of User*.
304 * These are used for rapid comparison to determine
305 * channel membership for PRIVMSG, NOTICE, QUIT, PART etc.
306 * The resulting pointer to the vector should be considered
307 * readonly and only modified via AddUser and DelUser.
309 * @return This function returns pointer to a map of User pointers (CUList*).
313 /** Obtain the internal reference list of opped users
314 * @return This function returns pointer to a map of User pointers (CUList*).
316 CUList* GetOppedUsers();
318 /** Obtain the internal reference list of halfopped users
319 * @return This function returns pointer to a map of User pointers (CUList*).
321 CUList* GetHalfoppedUsers();
323 /** Obtain the internal reference list of voiced users
324 * @return This function returns pointer to a map of User pointers (CUList*).
326 CUList* GetVoicedUsers();
328 /** Returns true if the user given is on the given channel.
329 * @param The user to look for
330 * @return True if the user is on this channel
332 bool HasUser(User* user);
334 /** Creates a channel record and initialises it with default values
335 * @throw Nothing at present.
337 Channel(InspIRCd* Instance);
339 /** Make src kick user from this channel with the given reason.
340 * @param src The source of the kick
341 * @param user The user being kicked (must be on this channel)
342 * @param reason The reason for the kick
343 * @return The number of users left on the channel. If this is zero
344 * when the method returns, you MUST delete the Channel immediately!
346 long KickUser(User *src, User *user, const char* reason);
348 /** Make the server kick user from this channel with the given reason.
349 * @param user The user being kicked (must be on this channel)
350 * @param reason The reason for the kick
351 * @param triggerevents True if you wish this kick to trigger module events
352 * @return The number of users left on the channel. If this is zero
353 * when the method returns, you MUST delete the Channel immediately!
355 long ServerKickUser(User* user, const char* reason, bool triggerevents);
357 /** Part a user from this channel with the given reason.
358 * If the reason field is NULL, no reason will be sent.
359 * @param user The user who is parting (must be on this channel)
360 * @param reason The (optional) part reason
361 * @return The number of users left on the channel. If this is zero
362 * when the method returns, you MUST delete the Channel immediately!
364 long PartUser(User *user, const char* reason = NULL);
366 /* Join a user to a channel. May be a channel that doesnt exist yet.
367 * @param user The user to join to the channel.
368 * @param cn The channel name to join to. Does not have to exist.
369 * @param key The key of the channel, if given
370 * @param override If true, override all join restrictions such as +bkil
371 * @return A pointer to the Channel the user was joined to. A new Channel may have
372 * been created if the channel did not exist before the user was joined to it.
373 * If the user could not be joined to a channel, the return value may be NULL.
375 static Channel* JoinUser(InspIRCd* ServerInstance, User *user, const char* cn, bool override, const char* key, bool bursting, time_t TS = 0);
378 * Create a channel record, and insert it into the hash.
379 * @param name The channel name
380 * @param ts The channel timestamp
381 * @return A pointer to the newly created Channel object.
383 static Channel *CreateChannel(InspIRCd *ServerInstance, const std::string &name, time_t ts = 0);
385 /** Write to a channel, from a user, using va_args for text
386 * @param user User whos details to prefix the line with
387 * @param text A printf-style format string which builds the output line without prefix
388 * @param ... Zero or more POD types
390 void WriteChannel(User* user, char* text, ...);
392 /** Write to a channel, from a user, using std::string for text
393 * @param user User whos details to prefix the line with
394 * @param text A std::string containing the output line without prefix
396 void WriteChannel(User* user, const std::string &text);
398 /** Write to a channel, from a server, using va_args for text
399 * @param ServName Server name to prefix the line with
400 * @param text A printf-style format string which builds the output line without prefix
401 * @param ... Zero or more POD type
403 void WriteChannelWithServ(const char* ServName, const char* text, ...);
405 /** Write to a channel, from a server, using std::string for text
406 * @param ServName Server name to prefix the line with
407 * @param text A std::string containing the output line without prefix
409 void WriteChannelWithServ(const char* ServName, const std::string &text);
411 /** Write to all users on a channel except a specific user, using va_args for text.
412 * Internally, this calls WriteAllExcept().
413 * @param user User whos details to prefix the line with, and to omit from receipt of the message
414 * @param serversource If this parameter is true, use the local server name as the source of the text, otherwise,
415 * use the nick!user@host of the user.
416 * @param status The status of the users to write to, e.g. '@' or '%'. Use a value of 0 to write to everyone
417 * @param text A printf-style format string which builds the output line without prefix
418 * @param ... Zero or more POD type
420 void WriteAllExceptSender(User* user, bool serversource, char status, char* text, ...);
422 /** Write to all users on a channel except a list of users, using va_args for text
423 * @param user User whos details to prefix the line with, and to omit from receipt of the message
424 * @param serversource If this parameter is true, use the local server name as the source of the text, otherwise,
425 * use the nick!user@host of the user.
426 * @param status The status of the users to write to, e.g. '@' or '%'. Use a value of 0 to write to everyone
427 * @param except_list A list of users NOT to send the text to
428 * @param text A printf-style format string which builds the output line without prefix
429 * @param ... Zero or more POD type
431 void WriteAllExcept(User* user, bool serversource, char status, CUList &except_list, char* text, ...);
433 /** Write to all users on a channel except a specific user, using std::string for text.
434 * Internally, this calls WriteAllExcept().
435 * @param user User whos details to prefix the line with, and to omit from receipt of the message
436 * @param serversource If this parameter is true, use the local server name as the source of the text, otherwise,
437 * use the nick!user@host of the user.
438 * @param status The status of the users to write to, e.g. '@' or '%'. Use a value of 0 to write to everyone
439 * @param text A std::string containing the output line without prefix
441 void WriteAllExceptSender(User* user, bool serversource, char status, const std::string& text);
443 /** Write to all users on a channel except a list of users, using std::string for text
444 * @param user User whos details to prefix the line with, and to omit from receipt of the message
445 * @param serversource If this parameter is true, use the local server name as the source of the text, otherwise,
446 * use the nick!user@host of the user.
447 * @param status The status of the users to write to, e.g. '@' or '%'. Use a value of 0 to write to everyone
448 * @param except_list A list of users NOT to send the text to
449 * @param text A std::string containing the output line without prefix
451 void WriteAllExcept(User* user, bool serversource, char status, CUList &except_list, const std::string& text);
453 /** Returns the maximum number of bans allowed to be set on this channel
454 * @return The maximum number of bans allowed
458 /** Return the channel's modes with parameters.
459 * @param showkey If this is set to true, the actual key is shown,
460 * otherwise it is replaced with '<KEY>'
461 * @return The channel mode string
463 char* ChanModes(bool showkey);
465 /** Spool the NAMES list for this channel to the given user
466 * @param user The user to spool the NAMES list to
467 * @param ulist The user list to send, NULL to use the
468 * channel's default names list of everyone
470 void UserList(User *user, CUList* ulist = NULL);
472 /** Get the number of invisible users on this channel
473 * @return Number of invisible users
475 int CountInvisible();
477 /** Get a users status on this channel
478 * @param user The user to look up
479 * @return One of STATUS_OP, STATUS_HOP, STATUS_VOICE, or zero.
481 int GetStatus(User *user);
483 /** Get a users status on this channel in a bitmask
484 * @param user The user to look up
485 * @return A bitmask containing zero or more of STATUS_OP, STATUS_HOP, STATUS_VOICE
487 int GetStatusFlags(User *user);
489 /** Get a users prefix on this channel in a string.
490 * @param user The user to look up
491 * @return A character array containing the prefix string.
492 * Unlike GetStatus and GetStatusFlags which will only return the
493 * core specified modes @, % and + (op, halfop and voice), GetPrefixChar
494 * will also return module-defined prefixes. If the user has to prefix,
495 * an empty but non-null string is returned. If the user has multiple
496 * prefixes, the highest is returned. If you do not recognise the prefix
497 * character you can get, you can deal with it in a 'proprtional' manner
498 * compared to known prefixes, using GetPrefixValue().
500 const char* GetPrefixChar(User *user);
502 /** Return all of a users mode prefixes into a char* string.
503 * @param user The user to look up
504 * @return A list of all prefix characters. The prefixes will always
505 * be in rank order, greatest first, as certain IRC clients require
506 * this when multiple prefixes are used names lists.
508 const char* GetAllPrefixChars(User* user);
510 /** Get the value of a users prefix on this channel.
511 * @param user The user to look up
512 * @return The module or core-defined value of the users prefix.
513 * The values for op, halfop and voice status are constants in
514 * mode.h, and are OP_VALUE, HALFOP_VALUE, and VOICE_VALUE respectively.
515 * If the value you are given does not match one of these three, it is
516 * a module-defined mode, and it should be compared in proportion to
517 * these three constants. For example a value greater than OP_VALUE
518 * is a prefix of greater 'worth' than ops, and a value less than
519 * VOICE_VALUE is of lesser 'worth' than a voice.
521 unsigned int GetPrefixValue(User* user);
523 /** This method removes all prefix characters from a user.
524 * It will not inform the user or the channel of the removal of prefixes,
525 * and should be used when the user parts or quits.
526 * @param user The user to remove all prefixes from
528 void RemoveAllPrefixes(User* user);
530 /** Add a prefix character to a user.
531 * Only the core should call this method, usually from
532 * within the mode parser or when the first user joins
533 * the channel (to grant ops to them)
534 * @param user The user to associate the privilage with
535 * @param prefix The prefix character to associate
536 * @param prefix_rank The rank (value) of this prefix character
537 * @param adding True if adding the prefix, false when removing
539 void SetPrefix(User* user, char prefix, unsigned int prefix_rank, bool adding);
541 /** Check if a user is banned on this channel
542 * @param user A user to check against the banlist
543 * @returns True if the user given is banned
545 bool IsBanned(User* user);
547 /** Clears the cached max bans value
551 /** Destructor for Channel
553 virtual ~Channel() { /* stub */ }