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