]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - include/channels.h
Header update: 2007 -> 2008
[user/henk/code/inspircd.git] / include / channels.h
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2008 InspIRCd Development Team
6  * See: http://www.inspircd.org/wiki/index.php/Credits
7  *
8  * This program is free but copyrighted software; see
9  *            the file COPYING for details.
10  *
11  * ---------------------------------------------------
12  */
13
14 #ifndef __CHANNELS_H__
15 #define __CHANNELS_H__
16
17 #include "inspircd_config.h"
18 #include "base.h"
19 #include <time.h>
20 #include <vector>
21 #include <string>
22 #include <map>
23
24 /** RFC1459 channel modes
25  */
26 enum ChannelModes {
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 */
35 };
36
37 /* Forward declarations - needed */
38 class User;
39 class Channel;
40
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.
43  */
44 class HostItem : public classbase
45 {
46  public:
47         /** Time the item was added
48          */
49         time_t set_time;
50         /** Who added the item
51          */
52         char set_by[NICKMAX];
53         /** The actual item data
54          */
55         char data[MAXBUF];
56
57         HostItem() { /* stub */ }
58         virtual ~HostItem() { /* stub */ }
59 };
60
61 /** A subclass of HostItem designed to hold channel bans (+b)
62  */
63 class BanItem : public HostItem
64 {
65 };
66
67 /** Holds a complete ban list
68  */
69 typedef std::vector<BanItem>    BanList;
70
71 /** A list of users on a channel
72  */
73 typedef std::map<User*,std::string> CUList;
74
75 /** Shorthand for CUList::iterator
76  */
77 typedef CUList::iterator CUListIter;
78
79 /** Shorthand for CUList::const_iterator
80  */
81 typedef CUList::const_iterator CUListConstIter;
82
83 /** A list of custom modes parameters on a channel
84  */
85 typedef std::map<char,char*> CustomModeList;
86
87
88 /** used to hold a channel and a users modes on that channel, e.g. +v, +h, +o
89  */
90 enum UserChannelModes {
91         UCMODE_OP       = 1,    /* Opped user */
92         UCMODE_VOICE    = 2,    /* Voiced user */
93         UCMODE_HOP      = 4     /* Halfopped user */
94 };
95
96 /** A stored prefix and its rank
97  */
98 typedef std::pair<char, unsigned int> prefixtype;
99
100 /** A list of prefixes set on a user in a channel
101  */
102 typedef std::vector<prefixtype> pfxcontainer;
103
104 /** A list of users with zero or more prefixes set on them
105  */
106 typedef std::map<User*, std::vector<prefixtype> > prefixlist;
107
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.
111  */
112 class CoreExport Channel : public Extensible
113 {
114  private:
115
116         /** Pointer to creator object
117          */
118         InspIRCd* ServerInstance;
119
120         /** Connect a Channel to a User
121          */
122         static Channel* ForceChan(InspIRCd* Instance, Channel* Ptr, User* user, const std::string &privs, bool bursting);
123
124         /** Set default modes for the channel on creation
125          */
126         void SetDefaultModes();
127
128         /** A list of prefixes associated with each user in the channel
129          * (e.g. &%+ etc)
130          */
131         prefixlist prefixes;
132
133         /** Maximum number of bans (cached)
134          */
135         int maxbans;
136
137  public:
138         /** Creates a channel record and initialises it with default values
139          * @throw Nothing at present.
140          */
141         Channel(InspIRCd* Instance, const std::string &name, time_t ts);
142
143         /** The channel's name.
144          */
145         char name[CHANMAX];
146
147         /** Modes for the channel.
148          * This is not a null terminated string! It is a hash where
149          * each item in it represents if a mode is set. For example
150          * for mode +A, index 0. Use modechar-65 to calculate which
151          * field to check.
152          */
153         char modes[64];
154
155         /** User lists.
156          * There are four user lists, one for 
157          * all the users, one for the ops, one for
158          * the halfops and another for the voices.
159          */
160         CUList internal_userlist;
161
162         /** Opped users.
163          * There are four user lists, one for 
164          * all the users, one for the ops, one for
165          * the halfops and another for the voices.
166          */
167         CUList internal_op_userlist;
168
169         /** Halfopped users.
170          * There are four user lists, one for 
171          * all the users, one for the ops, one for
172          * the halfops and another for the voices.
173          */
174         CUList internal_halfop_userlist;
175
176         /** Voiced users.
177          * There are four user lists, one for
178          * all the users, one for the ops, one for
179          * the halfops and another for the voices.
180          */
181         CUList internal_voice_userlist;
182
183         /** Parameters for custom modes.
184          * One for each custom mode letter.
185          */
186         CustomModeList custom_mode_params;
187
188         /** Channel topic.
189          * If this is an empty string, no channel topic is set.
190          */
191         char topic[MAXTOPIC];
192
193         /** Creation time.
194          * This is a timestamp (TS) value.
195          */
196         time_t created;
197
198         /** Time topic was set.
199          * If no topic was ever set, this will be equal to Channel::created
200          */
201         time_t topicset;
202
203         /** The last user to set the topic.
204          * If this member is an empty string, no topic was ever set.
205          */
206         char setby[128];
207
208         /** Contains the channel user limit.
209          * If this value is zero, there is no limit in place.
210          */
211         short int limit;
212         
213         /** Contains the channel key.
214          * If this value is an empty string, there is no channel key in place.
215          */
216         char key[32];
217
218         /** The list of all bans set on the channel.
219          */
220         BanList bans;
221         
222         /** Sets or unsets a custom mode in the channels info
223          * @param mode The mode character to set or unset
224          * @param mode_on True if you want to set the mode or false if you want to remove it
225          */
226         void SetMode(char mode,bool mode_on);
227
228         /** Sets or unsets the parameters for a custom mode in a channels info
229          * @param mode The mode character to set or unset
230          * @param parameter The parameter string to associate with this mode character
231          * @param mode_on True if you want to set the mode or false if you want to remove it
232          */
233         void SetModeParam(char mode,const char* parameter,bool mode_on);
234  
235         /** Returns true if a mode is set on a channel
236           * @param mode The mode character you wish to query
237           * @return True if the custom mode is set, false if otherwise
238           */
239         bool IsModeSet(char mode);
240
241         /** Returns the parameter for a custom mode on a channel.
242           * @param mode The mode character you wish to query
243           *
244           * For example if "+L #foo" is set, and you pass this method
245           * 'L', it will return '#foo'. If the mode is not set on the
246           * channel, or the mode has no parameters associated with it,
247           * it will return an empty string.
248           *
249           * @return The parameter for this mode is returned, or an empty string
250           */
251         std::string GetModeParameter(char mode);
252
253         /** Obtain the channel "user counter"
254          * This returns the channel reference counter, which is initialized
255          * to 0 when the channel is created and incremented/decremented
256          * upon joins, parts quits and kicks.
257          *
258          * @return The number of users on this channel
259          */
260         long GetUserCounter();
261
262         /** Add a user pointer to the internal reference list
263          * @param user The user to add
264          *
265          * The data inserted into the reference list is a table as it is
266          * an arbitary pointer compared to other users by its memory address,
267          * as this is a very fast 32 or 64 bit integer comparison.
268          */
269         void AddUser(User* user);
270
271         /** Add a user pointer to the internal reference list of opped users
272          * @param user The user to add
273          */
274         void AddOppedUser(User* user);
275
276         /** Add a user pointer to the internal reference list of halfopped users
277          * @param user The user to add
278          */
279         void AddHalfoppedUser(User* user);
280
281         /** Add a user pointer to the internal reference list of voiced users
282          * @param user The user to add
283          */
284         void AddVoicedUser(User* user);
285
286         /** Delete a user pointer to the internal reference list
287          * @param user The user to delete
288          * @return number of users left on the channel after deletion of the user
289          */
290         unsigned long DelUser(User* user);
291
292         /** Delete a user pointer to the internal reference list of opped users
293          * @param user The user to delete
294          */
295         void DelOppedUser(User* user);
296
297         /** Delete a user pointer to the internal reference list of halfopped users
298          * @param user The user to delete
299          */
300         void DelHalfoppedUser(User* user);
301
302         /** Delete a user pointer to the internal reference list of voiced users
303          * @param user The user to delete
304          */
305         void DelVoicedUser(User* user);
306
307         /** Obtain the internal reference list
308          * The internal reference list contains a list of User*.
309          * These are used for rapid comparison to determine
310          * channel membership for PRIVMSG, NOTICE, QUIT, PART etc.
311          * The resulting pointer to the vector should be considered
312          * readonly and only modified via AddUser and DelUser.
313          *
314          * @return This function returns pointer to a map of User pointers (CUList*).
315          */
316         CUList* GetUsers();
317
318         /** Obtain the internal reference list of opped users
319          * @return This function returns pointer to a map of User pointers (CUList*).
320          */
321         CUList* GetOppedUsers();
322
323         /** Obtain the internal reference list of halfopped users
324          * @return This function returns pointer to a map of User pointers (CUList*).
325          */
326         CUList* GetHalfoppedUsers();
327
328         /** Obtain the internal reference list of voiced users
329          * @return This function returns pointer to a map of User pointers (CUList*).
330          */
331         CUList* GetVoicedUsers();
332
333         /** Returns true if the user given is on the given channel.
334          * @param The user to look for
335          * @return True if the user is on this channel
336          */
337         bool HasUser(User* user);
338
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!
345          */
346         long KickUser(User *src, User *user, const char* reason);
347
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!
354          */
355         long ServerKickUser(User* user, const char* reason, bool triggerevents);
356
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!
363          */
364         long PartUser(User *user, const char* reason = NULL);
365
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.
374          */
375         static Channel* JoinUser(InspIRCd* ServerInstance, User *user, const char* cn, bool override, const char* key, bool bursting, time_t TS = 0);
376
377         /** Write to a channel, from a user, using va_args for text
378          * @param user User whos details to prefix the line with
379          * @param text A printf-style format string which builds the output line without prefix
380          * @param ... Zero or more POD types
381          */
382         void WriteChannel(User* user, char* text, ...);
383
384         /** Write to a channel, from a user, using std::string for text
385          * @param user User whos details to prefix the line with
386          * @param text A std::string containing the output line without prefix
387          */
388         void WriteChannel(User* user, const std::string &text);
389
390         /** Write to a channel, from a server, using va_args for text
391          * @param ServName Server name to prefix the line with
392          * @param text A printf-style format string which builds the output line without prefix
393          * @param ... Zero or more POD type
394          */
395         void WriteChannelWithServ(const char* ServName, const char* text, ...);
396
397         /** Write to a channel, from a server, using std::string for text
398          * @param ServName Server name to prefix the line with
399          * @param text A std::string containing the output line without prefix
400          */
401         void WriteChannelWithServ(const char* ServName, const std::string &text);
402
403         /** Write to all users on a channel except a specific user, using va_args for text.
404          * Internally, this calls WriteAllExcept().
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 text A printf-style format string which builds the output line without prefix
410          * @param ... Zero or more POD type
411          */
412         void WriteAllExceptSender(User* user, bool serversource, char status, char* text, ...);
413
414         /** Write to all users on a channel except a list of users, using va_args for text
415          * @param user User whos details to prefix the line with, and to omit from receipt of the message
416          * @param serversource If this parameter is true, use the local server name as the source of the text, otherwise,
417          * use the nick!user@host of the user.
418          * @param status The status of the users to write to, e.g. '@' or '%'. Use a value of 0 to write to everyone
419          * @param except_list A list of users NOT to send the text to
420          * @param text A printf-style format string which builds the output line without prefix
421          * @param ... Zero or more POD type
422          */
423         void WriteAllExcept(User* user, bool serversource, char status, CUList &except_list, char* text, ...);
424
425         /** Write to all users on a channel except a specific user, using std::string for text.
426          * Internally, this calls WriteAllExcept().
427          * @param user User whos details to prefix the line with, and to omit from receipt of the message
428          * @param serversource If this parameter is true, use the local server name as the source of the text, otherwise,
429          * use the nick!user@host of the user.
430          * @param status The status of the users to write to, e.g. '@' or '%'. Use a value of 0 to write to everyone
431          * @param text A std::string containing the output line without prefix
432          */
433         void WriteAllExceptSender(User* user, bool serversource, char status, const std::string& text);
434
435         /** Write to all users on a channel except a list of users, using std::string for text
436          * @param user User whos details to prefix the line with, and to omit from receipt of the message
437          * @param serversource If this parameter is true, use the local server name as the source of the text, otherwise,
438          * use the nick!user@host of the user.
439          * @param status The status of the users to write to, e.g. '@' or '%'. Use a value of 0 to write to everyone
440          * @param except_list A list of users NOT to send the text to
441          * @param text A std::string containing the output line without prefix
442          */
443         void WriteAllExcept(User* user, bool serversource, char status, CUList &except_list, const std::string& text);
444
445         /** Returns the maximum number of bans allowed to be set on this channel
446          * @return The maximum number of bans allowed
447          */
448         long GetMaxBans();
449
450         /** Return the channel's modes with parameters.
451          * @param showkey If this is set to true, the actual key is shown,
452          * otherwise it is replaced with '&lt;KEY&gt;'
453          * @return The channel mode string
454          */
455         char* ChanModes(bool showkey);
456
457         /** Spool the NAMES list for this channel to the given user
458          * @param user The user to spool the NAMES list to
459          * @param ulist The user list to send, NULL to use the
460          * channel's default names list of everyone
461          */
462         void UserList(User *user, CUList* ulist = NULL);
463
464         /** Get the number of invisible users on this channel
465          * @return Number of invisible users
466          */
467         int CountInvisible();
468
469         /** Get a users status on this channel
470          * @param user The user to look up
471          * @return One of STATUS_OP, STATUS_HOP, STATUS_VOICE, or zero.
472          */
473         int GetStatus(User *user);
474
475         /** Get a users status on this channel in a bitmask
476          * @param user The user to look up
477          * @return A bitmask containing zero or more of STATUS_OP, STATUS_HOP, STATUS_VOICE
478          */
479         int GetStatusFlags(User *user);
480
481         /** Get a users prefix on this channel in a string.
482          * @param user The user to look up
483          * @return A character array containing the prefix string.
484          * Unlike GetStatus and GetStatusFlags which will only return the
485          * core specified modes @, % and + (op, halfop and voice), GetPrefixChar
486          * will also return module-defined prefixes. If the user has to prefix,
487          * an empty but non-null string is returned. If the user has multiple
488          * prefixes, the highest is returned. If you do not recognise the prefix
489          * character you can get, you can deal with it in a 'proprtional' manner
490          * compared to known prefixes, using GetPrefixValue().
491          */
492         const char* GetPrefixChar(User *user);
493
494         /** Return all of a users mode prefixes into a char* string.
495          * @param user The user to look up
496          * @return A list of all prefix characters. The prefixes will always
497          * be in rank order, greatest first, as certain IRC clients require
498          * this when multiple prefixes are used names lists.
499          */
500         const char* GetAllPrefixChars(User* user);
501
502         /** Get the value of a users prefix on this channel.
503          * @param user The user to look up
504          * @return The module or core-defined value of the users prefix.
505          * The values for op, halfop and voice status are constants in
506          * mode.h, and are OP_VALUE, HALFOP_VALUE, and VOICE_VALUE respectively.
507          * If the value you are given does not match one of these three, it is
508          * a module-defined mode, and it should be compared in proportion to
509          * these three constants. For example a value greater than OP_VALUE
510          * is a prefix of greater 'worth' than ops, and a value less than
511          * VOICE_VALUE is of lesser 'worth' than a voice.
512          */
513         unsigned int GetPrefixValue(User* user);
514
515         /** This method removes all prefix characters from a user.
516          * It will not inform the user or the channel of the removal of prefixes,
517          * and should be used when the user parts or quits.
518          * @param user The user to remove all prefixes from
519          */
520         void RemoveAllPrefixes(User* user);
521
522         /** Add a prefix character to a user.
523          * Only the core should call this method, usually  from
524          * within the mode parser or when the first user joins
525          * the channel (to grant ops to them)
526          * @param user The user to associate the privilage with
527          * @param prefix The prefix character to associate
528          * @param prefix_rank The rank (value) of this prefix character
529          * @param adding True if adding the prefix, false when removing
530          */
531         void SetPrefix(User* user, char prefix, unsigned int prefix_rank, bool adding);
532
533         /** Check if a user is banned on this channel
534          * @param user A user to check against the banlist
535          * @returns True if the user given is banned
536          */
537         bool IsBanned(User* user);
538
539         /** Clears the cached max bans value
540          */
541         void ResetMaxBans();
542
543         /** Destructor for Channel
544          */
545         virtual ~Channel() { /* stub */ }
546 };
547
548 #endif