1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2008 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 /** RFC1459 channel modes
20 CM_TOPICLOCK = 't'-65, /* +t: Only operators can change topic */
21 CM_NOEXTERNAL = 'n'-65, /* +n: Only users in the channel can message it */
22 CM_INVITEONLY = 'i'-65, /* +i: Invite only */
23 CM_MODERATED = 'm'-65, /* +m: Only voices and above can talk */
24 CM_SECRET = 's'-65, /* +s: Secret channel */
25 CM_PRIVATE = 'p'-65, /* +p: Private channel */
26 CM_KEY = 'k'-65, /* +k: Keyed channel */
27 CM_LIMIT = 'l'-65 /* +l: Maximum user limit */
30 /* Forward declarations - needed */
33 /** Holds an entry for a ban list, exemption list, or invite list.
34 * This class contains a single element in a channel list, such as a banlist.
36 class HostItem : public classbase
39 /** Time the item was added
42 /** Who added the item
45 /** The actual item data
49 HostItem() { /* stub */ }
50 virtual ~HostItem() { /* stub */ }
53 /** A subclass of HostItem designed to hold channel bans (+b)
55 class BanItem : public HostItem
59 /** Holds a complete ban list
61 typedef std::vector<BanItem> BanList;
63 /** A list of users on a channel
65 typedef std::map<User*,std::string> CUList;
67 /** Shorthand for CUList::iterator
69 typedef CUList::iterator CUListIter;
71 /** Shorthand for CUList::const_iterator
73 typedef CUList::const_iterator CUListConstIter;
75 /** A list of custom modes parameters on a channel
77 typedef std::map<char,char*> CustomModeList;
80 /** used to hold a channel and a users modes on that channel, e.g. +v, +h, +o
82 enum UserChannelModes {
83 UCMODE_OP = 1, /* Opped user */
84 UCMODE_VOICE = 2, /* Voiced user */
85 UCMODE_HOP = 4 /* Halfopped user */
88 /** A stored prefix and its rank
90 typedef std::pair<char, unsigned int> prefixtype;
92 /** A list of prefixes set on a user in a channel
94 typedef std::vector<prefixtype> pfxcontainer;
96 /** A list of users with zero or more prefixes set on them
98 typedef std::map<User*, std::vector<prefixtype> > prefixlist;
100 /** Holds all relevent information for a channel.
101 * This class represents a channel, and contains its name, modes, time created, topic, topic set time,
102 * etc, and an instance of the BanList type.
104 class CoreExport Channel : public Extensible
108 /** Pointer to creator object
110 InspIRCd* ServerInstance;
112 /** Connect a Channel to a User
114 static Channel* ForceChan(InspIRCd* Instance, Channel* Ptr, User* user, const std::string &privs, bool bursting);
116 /** Set default modes for the channel on creation
118 void SetDefaultModes();
120 /** A list of prefixes associated with each user in the channel
125 /** Maximum number of bans (cached)
130 /** Creates a channel record and initialises it with default values
131 * @throw Nothing at present.
133 Channel(InspIRCd* Instance, const std::string &name, time_t ts);
135 /** The channel's name.
137 std::string name; /* CHANMAX */
139 /** Modes for the channel.
140 * This is not a null terminated string! It is a bitset where
141 * each item in it represents if a mode is set. For example
142 * for mode +A, index 0. Use modechar-65 to calculate which
145 std::bitset<64> modes;
148 * There are four user lists, one for
149 * all the users, one for the ops, one for
150 * the halfops and another for the voices.
152 CUList internal_userlist;
155 * There are four user lists, one for
156 * all the users, one for the ops, one for
157 * the halfops and another for the voices.
159 CUList internal_op_userlist;
162 * There are four user lists, one for
163 * all the users, one for the ops, one for
164 * the halfops and another for the voices.
166 CUList internal_halfop_userlist;
169 * There are four user lists, one for
170 * all the users, one for the ops, one for
171 * the halfops and another for the voices.
173 CUList internal_voice_userlist;
175 /** Parameters for custom modes.
176 * One for each custom mode letter.
178 CustomModeList custom_mode_params;
181 * If this is an empty string, no channel topic is set.
183 std::string topic; /* MAXTOPIC */
186 * This is a timestamp (TS) value.
190 /** Time topic was set.
191 * If no topic was ever set, this will be equal to Channel::created
195 /** The last user to set the topic.
196 * If this member is an empty string, no topic was ever set.
198 std::string setby; /* 128 */
200 /** The list of all bans set on the channel.
204 /** Sets or unsets a custom mode in the channels info
205 * @param mode The mode character to set or unset
206 * @param mode_on True if you want to set the mode or false if you want to remove it
208 void SetMode(char mode,bool mode_on);
210 /** Sets or unsets the parameters for a custom mode in a channels info
211 * @param mode The mode character to set or unset
212 * @param parameter The parameter string to associate with this mode character
213 * @param mode_on True if you want to set the mode or false if you want to remove it
215 void SetModeParam(char mode,const char* parameter,bool mode_on);
217 /** Returns true if a mode is set on a channel
218 * @param mode The mode character you wish to query
219 * @return True if the custom mode is set, false if otherwise
221 bool IsModeSet(char mode);
223 /** Returns the parameter for a custom mode on a channel.
224 * @param mode The mode character you wish to query
226 * For example if "+L #foo" is set, and you pass this method
227 * 'L', it will return '#foo'. If the mode is not set on the
228 * channel, or the mode has no parameters associated with it,
229 * it will return an empty string.
231 * @return The parameter for this mode is returned, or an empty string
233 std::string GetModeParameter(char mode);
235 /** Sets the channel topic.
236 * @param u The user setting the topic
237 * @param t The topic to set it to. Non-const, as it may be modified by a hook.
238 * @param forceset If set to true then all access checks will be bypassed.
240 int SetTopic(User *u, std::string &t, bool forceset = false);
242 /** Obtain the channel "user counter"
243 * This returns the channel reference counter, which is initialized
244 * to 0 when the channel is created and incremented/decremented
245 * upon joins, parts quits and kicks.
247 * @return The number of users on this channel
249 long GetUserCounter();
251 /** Add a user pointer to the internal reference list
252 * @param user The user to add
254 * The data inserted into the reference list is a table as it is
255 * an arbitary pointer compared to other users by its memory address,
256 * as this is a very fast 32 or 64 bit integer comparison.
258 void AddUser(User* user);
260 /** Add a user pointer to the internal reference list of opped users
261 * @param user The user to add
263 void AddOppedUser(User* user);
265 /** Add a user pointer to the internal reference list of halfopped users
266 * @param user The user to add
268 void AddHalfoppedUser(User* user);
270 /** Add a user pointer to the internal reference list of voiced users
271 * @param user The user to add
273 void AddVoicedUser(User* user);
275 /** Delete a user pointer to the internal reference list
276 * @param user The user to delete
277 * @return number of users left on the channel after deletion of the user
279 unsigned long DelUser(User* user);
281 /** Delete a user pointer to the internal reference list of opped users
282 * @param user The user to delete
284 void DelOppedUser(User* user);
286 /** Delete a user pointer to the internal reference list of halfopped users
287 * @param user The user to delete
289 void DelHalfoppedUser(User* user);
291 /** Delete a user pointer to the internal reference list of voiced users
292 * @param user The user to delete
294 void DelVoicedUser(User* user);
296 /** Obtain the internal reference list
297 * The internal reference list contains a list of User*.
298 * These are used for rapid comparison to determine
299 * channel membership for PRIVMSG, NOTICE, QUIT, PART etc.
300 * The resulting pointer to the vector should be considered
301 * readonly and only modified via AddUser and DelUser.
303 * @return This function returns pointer to a map of User pointers (CUList*).
307 /** Obtain the internal reference list of opped users
308 * @return This function returns pointer to a map of User pointers (CUList*).
310 CUList* GetOppedUsers();
312 /** Obtain the internal reference list of halfopped users
313 * @return This function returns pointer to a map of User pointers (CUList*).
315 CUList* GetHalfoppedUsers();
317 /** Obtain the internal reference list of voiced users
318 * @return This function returns pointer to a map of User pointers (CUList*).
320 CUList* GetVoicedUsers();
322 /** Returns true if the user given is on the given channel.
323 * @param The user to look for
324 * @return True if the user is on this channel
326 bool HasUser(User* user);
328 /** Make src kick user from this channel with the given reason.
329 * @param src The source of the kick
330 * @param user The user being kicked (must be on this channel)
331 * @param reason The reason for the kick
332 * @return The number of users left on the channel. If this is zero
333 * when the method returns, you MUST delete the Channel immediately!
335 long KickUser(User *src, User *user, const char* reason);
337 /** Make the server kick user from this channel with the given reason.
338 * @param user The user being kicked (must be on this channel)
339 * @param reason The reason for the kick
340 * @param triggerevents True if you wish this kick to trigger module events
341 * @return The number of users left on the channel. If this is zero
342 * when the method returns, you MUST delete the Channel immediately!
344 long ServerKickUser(User* user, const char* reason, bool triggerevents, const char* servername = NULL);
346 /** Part a user from this channel with the given reason.
347 * If the reason field is NULL, no reason will be sent.
348 * @param user The user who is parting (must be on this channel)
349 * @param reason The part reason
350 * @return The number of users left on the channel. If this is zero
351 * when the method returns, you MUST delete the Channel immediately!
353 long PartUser(User *user, std::string &reason);
355 /* Join a user to a channel. May be a channel that doesnt exist yet.
356 * @param user The user to join to the channel.
357 * @param cn The channel name to join to. Does not have to exist.
358 * @param key The key of the channel, if given
359 * @param override If true, override all join restrictions such as +bkil
360 * @return A pointer to the Channel the user was joined to. A new Channel may have
361 * been created if the channel did not exist before the user was joined to it.
362 * If the user could not be joined to a channel, the return value may be NULL.
364 static Channel* JoinUser(InspIRCd* ServerInstance, User *user, const char* cn, bool override, const char* key, bool bursting, time_t TS = 0);
366 /** Write to a channel, from a user, using va_args for text
367 * @param user User whos details to prefix the line with
368 * @param text A printf-style format string which builds the output line without prefix
369 * @param ... Zero or more POD types
371 void WriteChannel(User* user, const char* text, ...) CUSTOM_PRINTF(3, 4);
373 /** Write to a channel, from a user, using std::string for text
374 * @param user User whos details to prefix the line with
375 * @param text A std::string containing the output line without prefix
377 void WriteChannel(User* user, const std::string &text);
379 /** Write to a channel, from a server, using va_args for text
380 * @param ServName Server name to prefix the line with
381 * @param text A printf-style format string which builds the output line without prefix
382 * @param ... Zero or more POD type
384 void WriteChannelWithServ(const char* ServName, const char* text, ...) CUSTOM_PRINTF(3, 4);
386 /** Write to a channel, from a server, using std::string for text
387 * @param ServName Server name to prefix the line with
388 * @param text A std::string containing the output line without prefix
390 void WriteChannelWithServ(const char* ServName, const std::string &text);
392 /** Write to all users on a channel except a specific user, using va_args for text.
393 * Internally, this calls WriteAllExcept().
394 * @param user User whos details to prefix the line with, and to omit from receipt of the message
395 * @param serversource If this parameter is true, use the local server name as the source of the text, otherwise,
396 * use the nick!user@host of the user.
397 * @param status The status of the users to write to, e.g. '@' or '%'. Use a value of 0 to write to everyone
398 * @param text A printf-style format string which builds the output line without prefix
399 * @param ... Zero or more POD type
401 void WriteAllExceptSender(User* user, bool serversource, char status, const char* text, ...) CUSTOM_PRINTF(5, 6);
403 /** Write to all users on a channel except a list of users, using va_args for text
404 * @param user User whos details to prefix the line with, and to omit from receipt of the message
405 * @param serversource If this parameter is true, use the local server name as the source of the text, otherwise,
406 * use the nick!user@host of the user.
407 * @param status The status of the users to write to, e.g. '@' or '%'. Use a value of 0 to write to everyone
408 * @param except_list A list of users NOT to send the text to
409 * @param text A printf-style format string which builds the output line without prefix
410 * @param ... Zero or more POD type
412 void WriteAllExcept(User* user, bool serversource, char status, CUList &except_list, const char* text, ...) CUSTOM_PRINTF(6, 7);
414 /** Write to all users on a channel except a specific user, using std::string for text.
415 * Internally, this calls WriteAllExcept().
416 * @param user User whos details to prefix the line with, and to omit from receipt of the message
417 * @param serversource If this parameter is true, use the local server name as the source of the text, otherwise,
418 * use the nick!user@host of the user.
419 * @param status The status of the users to write to, e.g. '@' or '%'. Use a value of 0 to write to everyone
420 * @param text A std::string containing the output line without prefix
422 void WriteAllExceptSender(User* user, bool serversource, char status, const std::string& text);
424 /** Write to all users on a channel except a list of users, using std::string for text
425 * @param user User whos details to prefix the line with, and to omit from receipt of the message
426 * @param serversource If this parameter is true, use the local server name as the source of the text, otherwise,
427 * use the nick!user@host of the user.
428 * @param status The status of the users to write to, e.g. '@' or '%'. Use a value of 0 to write to everyone
429 * @param except_list A list of users NOT to send the text to
430 * @param text A std::string containing the output line without prefix
432 void WriteAllExcept(User* user, bool serversource, char status, CUList &except_list, const std::string& text);
434 /** Returns the maximum number of bans allowed to be set on this channel
435 * @return The maximum number of bans allowed
439 /** Return the channel's modes with parameters.
440 * @param showkey If this is set to true, the actual key is shown,
441 * otherwise it is replaced with '<KEY>'
442 * @return The channel mode string
444 char* ChanModes(bool showkey);
446 /** Spool the NAMES list for this channel to the given user
447 * @param user The user to spool the NAMES list to
448 * @param ulist The user list to send, NULL to use the
449 * channel's default names list of everyone
451 void UserList(User *user, CUList* ulist = NULL);
453 /** Get the number of invisible users on this channel
454 * @return Number of invisible users
456 int CountInvisible();
458 /** Get a users status on this channel
459 * @param user The user to look up
460 * @return One of STATUS_OP, STATUS_HOP, STATUS_VOICE, or zero.
462 int GetStatus(User *user);
464 /** Get a users status on this channel in a bitmask
465 * @param user The user to look up
466 * @return A bitmask containing zero or more of STATUS_OP, STATUS_HOP, STATUS_VOICE
468 int GetStatusFlags(User *user);
470 /** Get a users prefix on this channel in a string.
471 * @param user The user to look up
472 * @return A character array containing the prefix string.
473 * Unlike GetStatus and GetStatusFlags which will only return the
474 * core specified modes @, % and + (op, halfop and voice), GetPrefixChar
475 * will also return module-defined prefixes. If the user has to prefix,
476 * an empty but non-null string is returned. If the user has multiple
477 * prefixes, the highest is returned. If you do not recognise the prefix
478 * character you can get, you can deal with it in a 'proprtional' manner
479 * compared to known prefixes, using GetPrefixValue().
481 const char* GetPrefixChar(User *user);
483 /** Return all of a users mode prefixes into a char* string.
484 * @param user The user to look up
485 * @return A list of all prefix characters. The prefixes will always
486 * be in rank order, greatest first, as certain IRC clients require
487 * this when multiple prefixes are used names lists.
489 const char* GetAllPrefixChars(User* user);
491 /** Get the value of a users prefix on this channel.
492 * @param user The user to look up
493 * @return The module or core-defined value of the users prefix.
494 * The values for op, halfop and voice status are constants in
495 * mode.h, and are OP_VALUE, HALFOP_VALUE, and VOICE_VALUE respectively.
496 * If the value you are given does not match one of these three, it is
497 * a module-defined mode, and it should be compared in proportion to
498 * these three constants. For example a value greater than OP_VALUE
499 * is a prefix of greater 'worth' than ops, and a value less than
500 * VOICE_VALUE is of lesser 'worth' than a voice.
502 unsigned int GetPrefixValue(User* user);
504 /** This method removes all prefix characters from a user.
505 * It will not inform the user or the channel of the removal of prefixes,
506 * and should be used when the user parts or quits.
507 * @param user The user to remove all prefixes from
509 void RemoveAllPrefixes(User* user);
511 /** Add a prefix character to a user.
512 * Only the core should call this method, usually from
513 * within the mode parser or when the first user joins
514 * the channel (to grant ops to them)
515 * @param user The user to associate the privilage with
516 * @param prefix The prefix character to associate
517 * @param prefix_rank The rank (value) of this prefix character
518 * @param adding True if adding the prefix, false when removing
520 void SetPrefix(User* user, char prefix, unsigned int prefix_rank, bool adding);
522 /** Check if a user is banned on this channel
523 * @param user A user to check against the banlist
524 * @returns True if the user given is banned
526 bool IsBanned(User* user);
528 /** Check whether an extban of a given type matches
529 * a given user for this channel.
530 * @param u The user to match bans against
531 * @param type The type of extban to check
533 bool IsExtBanned(User *u, char type);
535 /** Overloaded version to check whether a particular string is extbanned
537 bool IsExtBanned(const std::string &str, char type);
539 /** Clears the cached max bans value
543 /** Destructor for Channel
545 virtual ~Channel() { /* stub */ }