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