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 /** 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 */
34 /** Holds an entry for a ban list, exemption list, or invite list.
35 * This class contains a single element in a channel list, such as a banlist.
37 class HostItem : public classbase
40 /** Time the item was added
43 /** Who added the item
46 /** The actual item data
50 HostItem() { /* stub */ }
51 virtual ~HostItem() { /* stub */ }
54 /** A subclass of HostItem designed to hold channel bans (+b)
56 class BanItem : public HostItem
60 /** Holds a complete ban list
62 typedef std::list<BanItem> BanList;
64 /** A list of users on a channel
66 typedef std::map<User*,std::string> CUList;
68 /** Shorthand for CUList::iterator
70 typedef CUList::iterator CUListIter;
72 /** Shorthand for CUList::const_iterator
74 typedef CUList::const_iterator CUListConstIter;
76 /** A list of custom modes parameters on a channel
78 typedef std::map<char,std::string> CustomModeList;
81 /** used to hold a channel and a users modes on that channel, e.g. +v, +h, +o
83 enum UserChannelModes {
84 UCMODE_OP = 1, /* Opped user */
85 UCMODE_VOICE = 2, /* Voiced user */
86 UCMODE_HOP = 4 /* Halfopped user */
89 /** A stored prefix and its rank
91 typedef std::pair<char, unsigned int> prefixtype;
93 /** A list of prefixes set on a user in a channel
95 typedef std::vector<prefixtype> pfxcontainer;
97 /** A list of users with zero or more prefixes set on them
99 typedef std::map<User*, std::vector<prefixtype> > prefixlist;
101 /** Holds all relevent information for a channel.
102 * This class represents a channel, and contains its name, modes, topic, topic set time,
103 * etc, and an instance of the BanList type.
105 class CoreExport Channel : public Extensible
109 /** Pointer to creator object
111 InspIRCd* ServerInstance;
113 /** Connect a Channel to a User
115 static Channel* ForceChan(InspIRCd* Instance, Channel* Ptr, User* user, const std::string &privs, bool bursting, bool created);
117 /** Set default modes for the channel on creation
119 void SetDefaultModes();
121 /** A list of prefixes associated with each user in the channel
126 /** Maximum number of bans (cached)
131 /** Creates a channel record and initialises it with default values
132 * @throw Nothing at present.
134 Channel(InspIRCd* Instance, const std::string &name, time_t ts);
136 /** The channel's name.
138 std::string name; /* CHANMAX */
140 /** Time that the object was instantiated (used for TS calculation etc)
144 /** Modes for the channel.
145 * This is not a null terminated string! It is a bitset where
146 * each item in it represents if a mode is set. For example
147 * for mode +A, index 0. Use modechar-65 to calculate which
150 std::bitset<64> modes;
153 * There are four user lists, one for
154 * all the users, one for the ops, one for
155 * the halfops and another for the voices.
157 CUList internal_userlist;
160 * There are four user lists, one for
161 * all the users, one for the ops, one for
162 * the halfops and another for the voices.
164 CUList internal_op_userlist;
167 * There are four user lists, one for
168 * all the users, one for the ops, one for
169 * the halfops and another for the voices.
171 CUList internal_halfop_userlist;
174 * There are four user lists, one for
175 * all the users, one for the ops, one for
176 * the halfops and another for the voices.
178 CUList internal_voice_userlist;
180 /** Parameters for custom modes.
181 * One for each custom mode letter.
183 CustomModeList custom_mode_params;
186 * If this is an empty string, no channel topic is set.
188 std::string topic; /* MAXTOPIC */
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 a custom mode in the 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 * If it is empty, the mode is unset; if it is nonempty, the mode is set.
215 void SetModeParam(char mode, std::string parameter);
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 inline bool IsModeSet(char mode) { return modes[mode-'A']; }
224 /** Returns the parameter for a custom mode on a channel.
225 * @param mode The mode character you wish to query
227 * For example if "+L #foo" is set, and you pass this method
228 * 'L', it will return '#foo'. If the mode is not set on the
229 * channel, or the mode has no parameters associated with it,
230 * it will return an empty string.
232 * @return The parameter for this mode is returned, or an empty string
234 std::string GetModeParameter(char mode);
236 /** Sets the channel topic.
237 * @param u The user setting the topic
238 * @param t The topic to set it to. Non-const, as it may be modified by a hook.
239 * @param forceset If set to true then all access checks will be bypassed.
241 int SetTopic(User *u, std::string &t, bool forceset = false);
243 /** Obtain the channel "user counter"
244 * This returns the channel reference counter, which is initialized
245 * to 0 when the channel is created and incremented/decremented
246 * upon joins, parts quits and kicks.
248 * @return The number of users on this channel
250 long GetUserCounter();
252 /** Add a user pointer to the internal reference list
253 * @param user The user to add
255 * The data inserted into the reference list is a table as it is
256 * an arbitary pointer compared to other users by its memory address,
257 * as this is a very fast 32 or 64 bit integer comparison.
259 void AddUser(User* user);
261 /** Add a user pointer to the internal reference list of opped users
262 * @param user The user to add
264 void AddOppedUser(User* user);
266 /** Add a user pointer to the internal reference list of halfopped users
267 * @param user The user to add
269 void AddHalfoppedUser(User* user);
271 /** Add a user pointer to the internal reference list of voiced users
272 * @param user The user to add
274 void AddVoicedUser(User* user);
276 /** Delete a user pointer to the internal reference list
277 * @param user The user to delete
278 * @return number of users left on the channel after deletion of the user
280 unsigned long DelUser(User* user);
282 /** Delete a user pointer to the internal reference list of opped users
283 * @param user The user to delete
285 void DelOppedUser(User* user);
287 /** Delete a user pointer to the internal reference list of halfopped users
288 * @param user The user to delete
290 void DelHalfoppedUser(User* user);
292 /** Delete a user pointer to the internal reference list of voiced users
293 * @param user The user to delete
295 void DelVoicedUser(User* user);
297 /** Obtain the internal reference list
298 * The internal reference list contains a list of User*.
299 * These are used for rapid comparison to determine
300 * channel membership for PRIVMSG, NOTICE, QUIT, PART etc.
301 * The resulting pointer to the vector should be considered
302 * readonly and only modified via AddUser and DelUser.
304 * @return This function returns pointer to a map of User pointers (CUList*).
308 /** Obtain the internal reference list of opped users
309 * @return This function returns pointer to a map of User pointers (CUList*).
311 CUList* GetOppedUsers();
313 /** Obtain the internal reference list of halfopped users
314 * @return This function returns pointer to a map of User pointers (CUList*).
316 CUList* GetHalfoppedUsers();
318 /** Obtain the internal reference list of voiced users
319 * @return This function returns pointer to a map of User pointers (CUList*).
321 CUList* GetVoicedUsers();
323 /** Returns true if the user given is on the given channel.
324 * @param The user to look for
325 * @return True if the user is on this channel
327 bool HasUser(User* user);
329 /** Make src kick user from this channel with the given reason.
330 * @param src The source of the kick
331 * @param user The user being kicked (must be on this channel)
332 * @param reason The reason for the kick
333 * @return The number of users left on the channel. If this is zero
334 * when the method returns, you MUST delete the Channel immediately!
336 long KickUser(User *src, User *user, const char* reason);
338 /** Make the server kick user from this channel with the given reason.
339 * @param user The user being kicked (must be on this channel)
340 * @param reason The reason for the kick
341 * @param triggerevents True if you wish this kick to trigger module events
342 * @return The number of users left on the channel. If this is zero
343 * when the method returns, you MUST delete the Channel immediately!
345 long ServerKickUser(User* user, const char* reason, const char* servername = NULL);
347 /** Part a user from this channel with the given reason.
348 * If the reason field is NULL, no reason will be sent.
349 * @param user The user who is parting (must be on this channel)
350 * @param reason The part reason
351 * @return The number of users left on the channel. If this is zero
352 * when the method returns, you MUST delete the Channel immediately!
354 long PartUser(User *user, std::string &reason);
356 /* Join a user to a channel. May be a channel that doesnt exist yet.
357 * @param user The user to join to the channel.
358 * @param cn The channel name to join to. Does not have to exist.
359 * @param key The key of the channel, if given
360 * @param override If true, override all join restrictions such as +bkil
361 * @return A pointer to the Channel the user was joined to. A new Channel may have
362 * been created if the channel did not exist before the user was joined to it.
363 * If the user could not be joined to a channel, the return value may be NULL.
365 static Channel* JoinUser(InspIRCd* ServerInstance, User *user, const char* cn, bool override, const char* key, bool bursting, time_t TS = 0);
367 /** Write to a channel, from a user, using va_args for text
368 * @param user User whos details to prefix the line with
369 * @param text A printf-style format string which builds the output line without prefix
370 * @param ... Zero or more POD types
372 void WriteChannel(User* user, const char* text, ...) CUSTOM_PRINTF(3, 4);
374 /** Write to a channel, from a user, using std::string for text
375 * @param user User whos details to prefix the line with
376 * @param text A std::string containing the output line without prefix
378 void WriteChannel(User* user, const std::string &text);
380 /** Write to a channel, from a server, using va_args for text
381 * @param ServName Server name to prefix the line with
382 * @param text A printf-style format string which builds the output line without prefix
383 * @param ... Zero or more POD type
385 void WriteChannelWithServ(const char* ServName, const char* text, ...) CUSTOM_PRINTF(3, 4);
387 /** Write to a channel, from a server, using std::string for text
388 * @param ServName Server name to prefix the line with
389 * @param text A std::string containing the output line without prefix
391 void WriteChannelWithServ(const char* ServName, const std::string &text);
393 /** Write to all users on a channel except a specific user, using va_args for text.
394 * Internally, this calls WriteAllExcept().
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 text A printf-style format string which builds the output line without prefix
400 * @param ... Zero or more POD type
402 void WriteAllExceptSender(User* user, bool serversource, char status, const char* text, ...) CUSTOM_PRINTF(5, 6);
404 /** Write to all users on a channel except a list of users, using va_args for text
405 * @param user User whos details to prefix the line with, and to omit from receipt of the message
406 * @param serversource If this parameter is true, use the local server name as the source of the text, otherwise,
407 * use the nick!user@host of the user.
408 * @param status The status of the users to write to, e.g. '@' or '%'. Use a value of 0 to write to everyone
409 * @param except_list A list of users NOT to send the text to
410 * @param text A printf-style format string which builds the output line without prefix
411 * @param ... Zero or more POD type
413 void WriteAllExcept(User* user, bool serversource, char status, CUList &except_list, const char* text, ...) CUSTOM_PRINTF(6, 7);
415 /** Write to all users on a channel except a specific user, using std::string for text.
416 * Internally, this calls WriteAllExcept().
417 * @param user User whos details to prefix the line with, and to omit from receipt of the message
418 * @param serversource If this parameter is true, use the local server name as the source of the text, otherwise,
419 * use the nick!user@host of the user.
420 * @param status The status of the users to write to, e.g. '@' or '%'. Use a value of 0 to write to everyone
421 * @param text A std::string containing the output line without prefix
423 void WriteAllExceptSender(User* user, bool serversource, char status, const std::string& text);
425 /** Write to all users on a channel except a list of users, using std::string for text
426 * @param user User whos details to prefix the line with, and to omit from receipt of the message
427 * @param serversource If this parameter is true, use the local server name as the source of the text, otherwise,
428 * use the nick!user@host of the user.
429 * @param status The status of the users to write to, e.g. '@' or '%'. Use a value of 0 to write to everyone
430 * @param except_list A list of users NOT to send the text to
431 * @param text A std::string containing the output line without prefix
433 void WriteAllExcept(User* user, bool serversource, char status, CUList &except_list, const std::string& text);
434 /** Write a line of text that already includes the source */
435 void RawWriteAllExcept(User* user, bool serversource, char status, CUList &except_list, const std::string& text);
437 /** Returns the maximum number of bans allowed to be set on this channel
438 * @return The maximum number of bans allowed
442 /** Return the channel's modes with parameters.
443 * @param showkey If this is set to true, the actual key is shown,
444 * otherwise it is replaced with '<KEY>'
445 * @return The channel mode string
447 char* ChanModes(bool showkey);
449 /** Spool the NAMES list for this channel to the given user
450 * @param user The user to spool the NAMES list to
451 * @param ulist The user list to send, NULL to use the
452 * channel's default names list of everyone
454 void UserList(User *user, CUList* ulist = NULL);
456 /** Get the number of invisible users on this channel
457 * @return Number of invisible users
459 int CountInvisible();
461 /** Get a users status on this channel
462 * @param user The user to look up
463 * @return One of STATUS_OP, STATUS_HOP, STATUS_VOICE, or zero.
465 int GetStatus(User *user);
467 /** Get a users status on this channel in a bitmask
468 * @param user The user to look up
469 * @return A bitmask containing zero or more of STATUS_OP, STATUS_HOP, STATUS_VOICE
471 int GetStatusFlags(User *user);
473 /** Get a users prefix on this channel in a string.
474 * @param user The user to look up
475 * @return A character array containing the prefix string.
476 * Unlike GetStatus and GetStatusFlags which will only return the
477 * core specified modes @, % and + (op, halfop and voice), GetPrefixChar
478 * will also return module-defined prefixes. If the user has to prefix,
479 * an empty but non-null string is returned. If the user has multiple
480 * prefixes, the highest is returned. If you do not recognise the prefix
481 * character you can get, you can deal with it in a 'proprtional' manner
482 * compared to known prefixes, using GetPrefixValue().
484 const char* GetPrefixChar(User *user);
486 /** Return all of a users mode prefixes into a char* string.
487 * @param user The user to look up
488 * @return A list of all prefix characters. The prefixes will always
489 * be in rank order, greatest first, as certain IRC clients require
490 * this when multiple prefixes are used names lists.
492 const char* GetAllPrefixChars(User* user);
494 /** Get the value of a users prefix on this channel.
495 * @param user The user to look up
496 * @return The module or core-defined value of the users prefix.
497 * The values for op, halfop and voice status are constants in
498 * mode.h, and are OP_VALUE, HALFOP_VALUE, and VOICE_VALUE respectively.
499 * If the value you are given does not match one of these three, it is
500 * a module-defined mode, and it should be compared in proportion to
501 * these three constants. For example a value greater than OP_VALUE
502 * is a prefix of greater 'worth' than ops, and a value less than
503 * VOICE_VALUE is of lesser 'worth' than a voice.
505 unsigned int GetPrefixValue(User* user);
507 /** This method removes all prefix characters from a user.
508 * It will not inform the user or the channel of the removal of prefixes,
509 * and should be used when the user parts or quits.
510 * @param user The user to remove all prefixes from
512 void RemoveAllPrefixes(User* user);
514 /** Add a prefix character to a user.
515 * Only the core should call this method, usually from
516 * within the mode parser or when the first user joins
517 * the channel (to grant ops to them)
518 * @param user The user to associate the privilage with
519 * @param prefix The prefix character to associate
520 * @param prefix_rank The rank (value) of this prefix character
521 * @param adding True if adding the prefix, false when removing
523 void SetPrefix(User* user, char prefix, unsigned int prefix_rank, bool adding);
525 /** Check if a user is banned on this channel
526 * @param user A user to check against the banlist
527 * @returns True if the user given is banned
529 bool IsBanned(User* user);
531 /** Check whether an extban of a given type matches
532 * a given user for this channel.
533 * @param u The user to match bans against
534 * @param type The type of extban to check
536 ModResult GetExtBanStatus(User *u, char type);
538 /** Overloaded version to check whether a particular string is extbanned
540 ModResult GetExtBanStatus(const std::string &str, char type);
542 /** Clears the cached max bans value
546 /** Destructor for Channel
548 virtual ~Channel() { /* stub */ }