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