]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - include/channels.h
Spotted problem: must clear out all prefixes attached to a user when they quit or...
[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 typedef std::pair<char, unsigned int> prefixtype;
148 typedef std::vector<prefixtype> pfxcontainer;
149 typedef std::map<userrec*, std::vector<prefixtype> > prefixlist;
150
151 /** Holds all relevent information for a channel.
152  * This class represents a channel, and contains its name, modes, time created, topic, topic set time,
153  * etc, and an instance of the BanList type.
154  */
155 class chanrec : public Extensible
156 {
157  private:
158
159         /** Pointer to creator object
160          */
161         InspIRCd* ServerInstance;
162
163         /** Connect a chanrec to a userrec
164          */
165         static chanrec* ForceChan(InspIRCd* Instance, chanrec* Ptr,ucrec *a,userrec* user, int created);
166
167         prefixlist prefixes;
168
169  public:
170         /** The channels name.
171          */
172         char name[CHANMAX];
173
174         /** Modes for the channel.
175          * This is not a null terminated string! It is a hash where
176          * each item in it represents if a mode is set. For example
177          * for mode +A, index 0. Use modechar-65 to calculate which
178          * field to check.
179          */
180         char modes[64];
181
182         /** User lists
183          * There are four user lists, one for 
184          * all the users, one for the ops, one for
185          * the halfops and another for the voices.
186          */
187         CUList internal_userlist;
188
189         /** Opped users
190          */
191         CUList internal_op_userlist;
192
193         /** Halfopped users
194          */
195         CUList internal_halfop_userlist;
196
197         /** Voiced users
198          */
199         CUList internal_voice_userlist;
200
201         /** Parameters for custom modes
202          */
203         CustomModeList custom_mode_params;
204
205         /** Channel topic.
206          * If this is an empty string, no channel topic is set.
207          */
208         char topic[MAXTOPIC];
209         /** Creation time.
210          */
211         time_t created;
212         /** Time topic was set.
213          * If no topic was ever set, this will be equal to chanrec::created
214          */
215         time_t topicset;
216         /** The last user to set the topic.
217          * If this member is an empty string, no topic was ever set.
218          */
219         char setby[NICKMAX];
220
221         /** Contains the channel user limit.
222          * If this value is zero, there is no limit in place.
223          */
224         short int limit;
225         
226         /** Contains the channel key.
227          * If this value is an empty string, there is no channel key in place.
228          */
229         char key[32];
230
231         /** The list of all bans set on the channel.
232          */
233         BanList bans;
234         
235         /** Sets or unsets a custom mode in the channels info
236          * @param mode The mode character to set or unset
237          * @param mode_on True if you want to set the mode or false if you want to remove it
238          */
239         void SetMode(char mode,bool mode_on);
240
241         /** Sets or unsets the parameters for a custom mode in a channels info
242          * @param mode The mode character to set or unset
243          * @param parameter The parameter string to associate with this mode character
244          * @param mode_on True if you want to set the mode or false if you want to remove it
245          */
246         void SetModeParam(char mode,const char* parameter,bool mode_on);
247  
248         /** Returns true if a mode is set on a channel
249           * @param mode The mode character you wish to query
250           * @return True if the custom mode is set, false if otherwise
251           */
252         bool IsModeSet(char mode);
253
254         /** Returns the parameter for a custom mode on a channel.
255           * @param mode The mode character you wish to query
256           *
257           * For example if "+L #foo" is set, and you pass this method
258           * 'L', it will return '#foo'. If the mode is not set on the
259           * channel, or the mode has no parameters associated with it,
260           * it will return an empty string.
261           *
262           * @return The parameter for this mode is returned, or an empty string
263           */
264         std::string GetModeParameter(char mode);
265
266         /** Obtain the channel "user counter"
267          * This returns the channel reference counter, which is initialized
268          * to 0 when the channel is created and incremented/decremented
269          * upon joins, parts quits and kicks.
270          *
271          * @return The number of users on this channel
272          */
273         long GetUserCounter();
274
275         /** Add a user pointer to the internal reference list
276          * @param user The user to add
277          *
278          * The data inserted into the reference list is a table as it is
279          * an arbitary pointer compared to other users by its memory address,
280          * as this is a very fast 32 or 64 bit integer comparison.
281          */
282         void AddUser(userrec* user);
283
284         /** Add a user pointer to the internal reference list of opped users
285          * @param user The user to add
286          */
287         void AddOppedUser(userrec* user);
288
289         /** Add a user pointer to the internal reference list of halfopped users
290          * @param user The user to add
291          */
292         void AddHalfoppedUser(userrec* user);
293
294         /** Add a user pointer to the internal reference list of voiced users
295          * @param user The user to add
296          */
297         void AddVoicedUser(userrec* user);
298
299         /** Delete a user pointer to the internal reference list
300          * @param user The user to delete
301          * @return number of users left on the channel after deletion of the user
302          */
303         unsigned long DelUser(userrec* user);
304
305         /** Delete a user pointer to the internal reference list of opped users
306          * @param user The user to delete
307          */
308         void DelOppedUser(userrec* user);
309
310         /** Delete a user pointer to the internal reference list of halfopped users
311          * @param user The user to delete
312          */
313         void DelHalfoppedUser(userrec* user);
314
315         /** Delete a user pointer to the internal reference list of voiced users
316          * @param user The user to delete
317          */
318         void DelVoicedUser(userrec* user);
319
320         /** Obtain the internal reference list
321          * The internal reference list contains a list of userrec*.
322          * These are used for rapid comparison to determine
323          * channel membership for PRIVMSG, NOTICE, QUIT, PART etc.
324          * The resulting pointer to the vector should be considered
325          * readonly and only modified via AddUser and DelUser.
326          *
327          * @return This function returns pointer to a map of userrec pointers (CUList*).
328          */
329         CUList* GetUsers();
330
331         /** Obtain the internal reference list of opped users
332          * @return This function returns pointer to a map of userrec pointers (CUList*).
333          */
334         CUList* GetOppedUsers();
335
336         /** Obtain the internal reference list of halfopped users
337          * @return This function returns pointer to a map of userrec pointers (CUList*).
338          */
339         CUList* GetHalfoppedUsers();
340
341         /** Obtain the internal reference list of voiced users
342          * @return This function returns pointer to a map of userrec pointers (CUList*).
343          */
344         CUList* GetVoicedUsers();
345
346         /** Returns true if the user given is on the given channel.
347          * @param The user to look for
348          * @return True if the user is on this channel
349          */
350         bool HasUser(userrec* user);
351
352         /** Creates a channel record and initialises it with default values
353          * @throw Nothing at present.
354          */
355         chanrec(InspIRCd* Instance);
356
357         /** Make src kick user from this channel with the given reason.
358          * @param src The source of the kick
359          * @param user The user being kicked (must be on this channel)
360          * @param reason The reason for the kick
361          * @return The number of users left on the channel. If this is zero
362          * when the method returns, you MUST delete the chanrec immediately!
363          */
364         long KickUser(userrec *src, userrec *user, const char* reason);
365
366         /** Make the server kick user from this channel with the given reason.
367          * @param user The user being kicked (must be on this channel)
368          * @param reason The reason for the kick
369          * @param triggerevents True if you wish this kick to trigger module events
370          * @return The number of users left on the channel. If this is zero
371          * when the method returns, you MUST delete the chanrec immediately!
372          */
373         long ServerKickUser(userrec* user, const char* reason, bool triggerevents);
374
375         /** Part a user from this channel with the given reason.
376          * If the reason field is NULL, no reason will be sent.
377          * @param user The user who is parting (must be on this channel)
378          * @param reason The (optional) part reason
379          * @return The number of users left on the channel. If this is zero
380          * when the method returns, you MUST delete the chanrec immediately!
381          */
382         long PartUser(userrec *user, const char* reason = NULL);
383
384         /* Join a user to a channel. May be a channel that doesnt exist yet.
385          * @param user The user to join to the channel.
386          * @param cn The channel name to join to. Does not have to exist.
387          * @param key The key of the channel, if given
388          * @param override If true, override all join restrictions such as +bkil
389          * @return A pointer to the chanrec the user was joined to. A new chanrec may have
390          * been created if the channel did not exist before the user was joined to it.
391          * If the user could not be joined to a channel, the return value may be NULL.
392          */
393         static chanrec* JoinUser(InspIRCd* ServerInstance, userrec *user, const char* cn, bool override, const char* key = "");
394
395         /** Write to a channel, from a user, using va_args for text
396          * @param user User whos details to prefix the line with
397          * @param text A printf-style format string which builds the output line without prefix
398          * @param ... Zero or more POD types
399          */
400         void WriteChannel(userrec* user, char* text, ...);
401
402         /** Write to a channel, from a user, using std::string for text
403          * @param user User whos details to prefix the line with
404          * @param text A std::string containing the output line without prefix
405          */
406         void WriteChannel(userrec* user, const std::string &text);
407
408         /** Write to a channel, from a server, using va_args for text
409          * @param ServName Server name to prefix the line with
410          * @param text A printf-style format string which builds the output line without prefi
411          * @param ... Zero or more POD type
412          */
413         void WriteChannelWithServ(const char* ServName, const char* text, ...);
414
415         /** Write to a channel, from a server, using std::string for text
416          * @param ServName Server name to prefix the line with
417          * @param text A std::string containing the output line without prefix
418          */
419         void WriteChannelWithServ(const char* ServName, const std::string &text);
420
421         /** Write to all users on a channel except a specific user, using va_args for text
422          * @param user User whos details to prefix the line with, and to omit from receipt of the message
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 printf-style format string which builds the output line without prefi
425          * @param ... Zero or more POD type
426          */
427         void WriteAllExceptSender(userrec* user, char status, char* text, ...);
428
429         /** Write to all users on a channel except a specific user, using std::string 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 std::string containing the output line without prefix
433          */
434         void WriteAllExceptSender(userrec* user, char status, const std::string& text);
435
436         /** Returns the maximum number of bans allowed to be set on this channel
437          * @return The maximum number of bans allowed
438          */
439         long GetMaxBans();
440
441         /** Return the channel's modes with parameters.
442          * @param showkey If this is set to true, the actual key is shown,
443          * otherwise it is replaced with '&lt;KEY&gt;'
444          * @return The channel mode string
445          */
446         char* ChanModes(bool showkey);
447
448         /** Spool the NAMES list for this channel to the given user
449          * @param The user to spool the NAMES list to
450          */
451         void UserList(userrec *user);
452
453         /** Get the number of invisible users on this channel
454          * @return Number of invisible users
455          */
456         int CountInvisible();
457
458         /** Get a users status on this channel
459          * @param The user to look up
460          * @return One of STATUS_OP, STATUS_HOP, STATUS_VOICE, or zero.
461          */
462         int GetStatus(userrec *user);
463
464         /** Get a users status on this channel in a bitmask
465          * @param The user to look up
466          * @return A bitmask containing zero or more of STATUS_OP, STATUS_HOP, STATUS_VOICE
467          */
468         int GetStatusFlags(userrec *user);
469
470         /** Get a users status on this channel in a string
471          * @param The user to look up
472          * @return A character array containing the string "@", "%", "+" or ""
473          */
474         const char* GetStatusChar(userrec *user);
475
476         void RemoveAllPrefixes(userrec* user);
477
478         void SetPrefix(userrec* user, char prefix, unsigned int prefix_rank, bool adding);
479
480         /** Destructor for chanrec
481          */
482         virtual ~chanrec() { /* stub */ }
483 };
484
485 #endif