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,
28 CM_NOEXTERNAL = 'n'-65,
29 CM_INVITEONLY = 'i'-65,
30 CM_MODERATED = 'm'-65,
40 /** Holds an entry for a ban list, exemption list, or invite list.
41 * This class contains a single element in a channel list, such as a banlist.
43 class HostItem : public classbase
46 /** Time the item was added
49 /** Who added the item
52 /** The actual item data
56 HostItem() { /* stub */ }
57 virtual ~HostItem() { /* stub */ }
60 /** A subclass of HostItem designed to hold channel bans (+b)
62 class BanItem : public HostItem
66 /** Holds a complete ban list
68 typedef std::vector<BanItem> BanList;
70 /** A list of users on a channel
72 typedef std::map<userrec*,userrec*> CUList;
74 /** Shorthand for CUList::iterator
76 typedef CUList::iterator CUListIter;
78 /** Shorthand for CUList::const_iterator
80 typedef CUList::const_iterator CUListConstIter;
82 /** A list of custom modes parameters on a channel
84 typedef std::map<char,char*> CustomModeList;
87 /** used to hold a channel and a users modes on that channel, e.g. +v, +h, +o
89 enum UserChannelModes {
97 /** A stored prefix and its rank
99 typedef std::pair<char, unsigned int> prefixtype;
101 /** A list of prefixes set on a user in a channel
103 typedef std::vector<prefixtype> pfxcontainer;
105 /** A list of users with zero or more prefixes set on them
107 typedef std::map<userrec*, std::vector<prefixtype> > prefixlist;
109 /** Holds all relevent information for a channel.
110 * This class represents a channel, and contains its name, modes, time created, topic, topic set time,
111 * etc, and an instance of the BanList type.
113 class chanrec : public Extensible
117 /** Pointer to creator object
119 InspIRCd* ServerInstance;
121 /** Connect a chanrec to a userrec
123 static chanrec* ForceChan(InspIRCd* Instance, chanrec* Ptr, userrec* user, const std::string &privs);
127 /** Maximum number of bans (cached)
132 /** The channels name.
136 /** Modes for the channel.
137 * This is not a null terminated string! It is a hash where
138 * each item in it represents if a mode is set. For example
139 * for mode +A, index 0. Use modechar-65 to calculate which
145 * There are four user lists, one for
146 * all the users, one for the ops, one for
147 * the halfops and another for the voices.
149 CUList internal_userlist;
153 CUList internal_op_userlist;
157 CUList internal_halfop_userlist;
161 CUList internal_voice_userlist;
163 /** Parameters for custom modes
165 CustomModeList custom_mode_params;
168 * If this is an empty string, no channel topic is set.
170 char topic[MAXTOPIC];
174 /** Time topic was set.
175 * If no topic was ever set, this will be equal to chanrec::created
178 /** The last user to set the topic.
179 * If this member is an empty string, no topic was ever set.
183 /** Contains the channel user limit.
184 * If this value is zero, there is no limit in place.
188 /** Contains the channel key.
189 * If this value is an empty string, there is no channel key in place.
193 /** The list of all bans set on the channel.
197 /** Sets or unsets a custom mode in the channels info
198 * @param mode The mode character to set or unset
199 * @param mode_on True if you want to set the mode or false if you want to remove it
201 void SetMode(char mode,bool mode_on);
203 /** Sets or unsets the parameters for a custom mode in a channels info
204 * @param mode The mode character to set or unset
205 * @param parameter The parameter string to associate with this mode character
206 * @param mode_on True if you want to set the mode or false if you want to remove it
208 void SetModeParam(char mode,const char* parameter,bool mode_on);
210 /** Returns true if a mode is set on a channel
211 * @param mode The mode character you wish to query
212 * @return True if the custom mode is set, false if otherwise
214 bool IsModeSet(char mode);
216 /** Returns the parameter for a custom mode on a channel.
217 * @param mode The mode character you wish to query
219 * For example if "+L #foo" is set, and you pass this method
220 * 'L', it will return '#foo'. If the mode is not set on the
221 * channel, or the mode has no parameters associated with it,
222 * it will return an empty string.
224 * @return The parameter for this mode is returned, or an empty string
226 std::string GetModeParameter(char mode);
228 /** Obtain the channel "user counter"
229 * This returns the channel reference counter, which is initialized
230 * to 0 when the channel is created and incremented/decremented
231 * upon joins, parts quits and kicks.
233 * @return The number of users on this channel
235 long GetUserCounter();
237 /** Add a user pointer to the internal reference list
238 * @param user The user to add
240 * The data inserted into the reference list is a table as it is
241 * an arbitary pointer compared to other users by its memory address,
242 * as this is a very fast 32 or 64 bit integer comparison.
244 void AddUser(userrec* user);
246 /** Add a user pointer to the internal reference list of opped users
247 * @param user The user to add
249 void AddOppedUser(userrec* user);
251 /** Add a user pointer to the internal reference list of halfopped users
252 * @param user The user to add
254 void AddHalfoppedUser(userrec* user);
256 /** Add a user pointer to the internal reference list of voiced users
257 * @param user The user to add
259 void AddVoicedUser(userrec* user);
261 /** Delete a user pointer to the internal reference list
262 * @param user The user to delete
263 * @return number of users left on the channel after deletion of the user
265 unsigned long DelUser(userrec* user);
267 /** Delete a user pointer to the internal reference list of opped users
268 * @param user The user to delete
270 void DelOppedUser(userrec* user);
272 /** Delete a user pointer to the internal reference list of halfopped users
273 * @param user The user to delete
275 void DelHalfoppedUser(userrec* user);
277 /** Delete a user pointer to the internal reference list of voiced users
278 * @param user The user to delete
280 void DelVoicedUser(userrec* user);
282 /** Obtain the internal reference list
283 * The internal reference list contains a list of userrec*.
284 * These are used for rapid comparison to determine
285 * channel membership for PRIVMSG, NOTICE, QUIT, PART etc.
286 * The resulting pointer to the vector should be considered
287 * readonly and only modified via AddUser and DelUser.
289 * @return This function returns pointer to a map of userrec pointers (CUList*).
293 /** Obtain the internal reference list of opped users
294 * @return This function returns pointer to a map of userrec pointers (CUList*).
296 CUList* GetOppedUsers();
298 /** Obtain the internal reference list of halfopped users
299 * @return This function returns pointer to a map of userrec pointers (CUList*).
301 CUList* GetHalfoppedUsers();
303 /** Obtain the internal reference list of voiced users
304 * @return This function returns pointer to a map of userrec pointers (CUList*).
306 CUList* GetVoicedUsers();
308 /** Returns true if the user given is on the given channel.
309 * @param The user to look for
310 * @return True if the user is on this channel
312 bool HasUser(userrec* user);
314 /** Creates a channel record and initialises it with default values
315 * @throw Nothing at present.
317 chanrec(InspIRCd* Instance);
319 /** Make src kick user from this channel with the given reason.
320 * @param src The source of the kick
321 * @param user The user being kicked (must be on this channel)
322 * @param reason The reason for the kick
323 * @return The number of users left on the channel. If this is zero
324 * when the method returns, you MUST delete the chanrec immediately!
326 long KickUser(userrec *src, userrec *user, const char* reason);
328 /** Make the server kick user from this channel with the given reason.
329 * @param user The user being kicked (must be on this channel)
330 * @param reason The reason for the kick
331 * @param triggerevents True if you wish this kick to trigger module events
332 * @return The number of users left on the channel. If this is zero
333 * when the method returns, you MUST delete the chanrec immediately!
335 long ServerKickUser(userrec* user, const char* reason, bool triggerevents);
337 /** Part a user from this channel with the given reason.
338 * If the reason field is NULL, no reason will be sent.
339 * @param user The user who is parting (must be on this channel)
340 * @param reason The (optional) part reason
341 * @return The number of users left on the channel. If this is zero
342 * when the method returns, you MUST delete the chanrec immediately!
344 long PartUser(userrec *user, const char* reason = NULL);
346 /* Join a user to a channel. May be a channel that doesnt exist yet.
347 * @param user The user to join to the channel.
348 * @param cn The channel name to join to. Does not have to exist.
349 * @param key The key of the channel, if given
350 * @param override If true, override all join restrictions such as +bkil
351 * @return A pointer to the chanrec the user was joined to. A new chanrec may have
352 * been created if the channel did not exist before the user was joined to it.
353 * If the user could not be joined to a channel, the return value may be NULL.
355 static chanrec* JoinUser(InspIRCd* ServerInstance, userrec *user, const char* cn, bool override, const char* key, time_t TS = 0);
357 /** Write to a channel, from a user, using va_args for text
358 * @param user User whos details to prefix the line with
359 * @param text A printf-style format string which builds the output line without prefix
360 * @param ... Zero or more POD types
362 void WriteChannel(userrec* user, char* text, ...);
364 /** Write to a channel, from a user, using std::string for text
365 * @param user User whos details to prefix the line with
366 * @param text A std::string containing the output line without prefix
368 void WriteChannel(userrec* user, const std::string &text);
370 /** Write to a channel, from a server, using va_args for text
371 * @param ServName Server name to prefix the line with
372 * @param text A printf-style format string which builds the output line without prefix
373 * @param ... Zero or more POD type
375 void WriteChannelWithServ(const char* ServName, const char* text, ...);
377 /** Write to a channel, from a server, using std::string for text
378 * @param ServName Server name to prefix the line with
379 * @param text A std::string containing the output line without prefix
381 void WriteChannelWithServ(const char* ServName, const std::string &text);
383 /** Write to all users on a channel except a specific user, using va_args for text.
384 * Internally, this calls WriteAllExcept().
385 * @param user User whos details to prefix the line with, and to omit from receipt of the message
386 * @param serversource If this parameter is true, use the local server name as the source of the text, otherwise,
387 * use the nick!user@host of the user.
388 * @param status The status of the users to write to, e.g. '@' or '%'. Use a value of 0 to write to everyone
389 * @param text A printf-style format string which builds the output line without prefix
390 * @param ... Zero or more POD type
392 void WriteAllExceptSender(userrec* user, bool serversource, char status, char* text, ...);
394 /** Write to all users on a channel except a list of users, using va_args for text
395 * @param user User whos details to prefix the line with, and to omit from receipt of the message
396 * @param serversource If this parameter is true, use the local server name as the source of the text, otherwise,
397 * use the nick!user@host of the user.
398 * @param status The status of the users to write to, e.g. '@' or '%'. Use a value of 0 to write to everyone
399 * @param except_list A list of users NOT to send the text to
400 * @param text A printf-style format string which builds the output line without prefix
401 * @param ... Zero or more POD type
403 void WriteAllExcept(userrec* user, bool serversource, char status, CUList &except_list, char* text, ...);
405 /** Write to all users on a channel except a specific user, using std::string for text.
406 * Internally, this calls WriteAllExcept().
407 * @param user User whos details to prefix the line with, and to omit from receipt of the message
408 * @param serversource If this parameter is true, use the local server name as the source of the text, otherwise,
409 * use the nick!user@host of the user.
410 * @param status The status of the users to write to, e.g. '@' or '%'. Use a value of 0 to write to everyone
411 * @param text A std::string containing the output line without prefix
413 void WriteAllExceptSender(userrec* user, bool serversource, char status, const std::string& text);
415 /** Write to all users on a channel except a list of users, using std::string for text
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 except_list A list of users NOT to send the text to
421 * @param text A std::string containing the output line without prefix
423 void WriteAllExcept(userrec* user, bool serversource, char status, CUList &except_list, const std::string& text);
425 /** Returns the maximum number of bans allowed to be set on this channel
426 * @return The maximum number of bans allowed
430 /** Return the channel's modes with parameters.
431 * @param showkey If this is set to true, the actual key is shown,
432 * otherwise it is replaced with '<KEY>'
433 * @return The channel mode string
435 char* ChanModes(bool showkey);
437 /** Spool the NAMES list for this channel to the given user
438 * @param user The user to spool the NAMES list to
439 * @param ulist The user list to send, NULL to use the
440 * channel's default names list of everyone
442 void UserList(userrec *user, CUList* ulist = NULL);
444 /** Get the number of invisible users on this channel
445 * @return Number of invisible users
447 int CountInvisible();
449 /** Get a users status on this channel
450 * @param user The user to look up
451 * @return One of STATUS_OP, STATUS_HOP, STATUS_VOICE, or zero.
453 int GetStatus(userrec *user);
455 /** Get a users status on this channel in a bitmask
456 * @param user The user to look up
457 * @return A bitmask containing zero or more of STATUS_OP, STATUS_HOP, STATUS_VOICE
459 int GetStatusFlags(userrec *user);
461 /** Get a users prefix on this channel in a string.
462 * @param user The user to look up
463 * @return A character array containing the prefix string.
464 * Unlike GetStatus and GetStatusFlags which will only return the
465 * core specified modes @, % and + (op, halfop and voice), GetPrefixChar
466 * will also return module-defined prefixes. If the user has to prefix,
467 * an empty but non-null string is returned. If the user has multiple
468 * prefixes, the highest is returned. If you do not recognise the prefix
469 * character you can get, you can deal with it in a 'proprtional' manner
470 * compared to known prefixes, using GetPrefixValue().
472 const char* GetPrefixChar(userrec *user);
474 /** Return all of a users mode prefixes into a char* string.
475 * @param user The user to look up
476 * @return A list of all prefix characters. The prefixes will always
477 * be in rank order, greatest first, as certain IRC clients require
478 * this when multiple prefixes are used names lists.
480 const char* GetAllPrefixChars(userrec* user);
482 /** Get the value of a users prefix on this channel.
483 * @param user The user to look up
484 * @return The module or core-defined value of the users prefix.
485 * The values for op, halfop and voice status are constants in
486 * mode.h, and are OP_VALUE, HALFOP_VALUE, and VOICE_VALUE respectively.
487 * If the value you are given does not match one of these three, it is
488 * a module-defined mode, and it should be compared in proportion to
489 * these three constants. For example a value greater than OP_VALUE
490 * is a prefix of greater 'worth' than ops, and a value less than
491 * VOICE_VALUE is of lesser 'worth' than a voice.
493 unsigned int GetPrefixValue(userrec* user);
495 /** This method removes all prefix characters from a user.
496 * It will not inform the user or the channel of the removal of prefixes,
497 * and should be used when the user parts or quits.
498 * @param user The user to remove all prefixes from
500 void RemoveAllPrefixes(userrec* user);
502 /** Add a prefix character to a user.
503 * Only the core should call this method, usually from
504 * within the mode parser or when the first user joins
505 * the channel (to grant ops to them)
506 * @param user The user to associate the privilage with
507 * @param prefix The prefix character to associate
508 * @param prefix_rank The rank (value) of this prefix character
509 * @param adding True if adding the prefix, false when removing
511 void SetPrefix(userrec* user, char prefix, unsigned int prefix_rank, bool adding);
513 /** Check if a user is banned on this channel
514 * @param user A user to check against the banlist
515 * @returns True if the user given is banned
517 bool IsBanned(userrec* user);
519 /** Clears the cached max bans value
523 /** Destructor for chanrec
525 virtual ~chanrec() { /* stub */ }