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