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