]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - include/channels.h
82a955069fe15c5ecc77591c73538c51517afedb
[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_t set_time;
50         char set_by[NICKMAX];
51         char data[MAXBUF];
52
53         HostItem() { /* stub */ }
54         virtual ~HostItem() { /* stub */ }
55 };
56
57 /** A subclass of HostItem designed to hold channel bans (+b)
58  */
59 class BanItem : public HostItem
60 {
61 };
62
63 /** A subclass of HostItem designed to hold channel exempts (+e)
64  */
65 class ExemptItem : public HostItem
66 {
67 };
68
69 /** A subclass of HostItem designed to hold channel invites (+I)
70  */
71 class InviteItem : public HostItem
72 {
73 };
74
75 /** Holds a complete ban list
76  */
77 typedef std::vector<BanItem>    BanList;
78
79 /** Holds a complete exempt list
80  */
81 typedef std::vector<ExemptItem> ExemptList;
82
83 /** Holds a complete invite list
84  */
85 typedef std::vector<InviteItem> InviteList;
86
87 /** A list of users on a channel
88  */
89 typedef std::map<userrec*,userrec*> CUList;
90
91 /** Shorthand for CUList::iterator
92  */
93 typedef CUList::iterator CUListIter;
94 typedef CUList::const_iterator CUListConstIter;
95
96 /** A list of custom modes parameters on a channel
97  */
98 typedef std::map<char,char*> CustomModeList;
99
100
101 /** used to hold a channel and a users modes on that channel, e.g. +v, +h, +o
102  * needs to come AFTER struct chanrec */
103 enum UserChannelModes {
104         UCMODE_OP      = 1,
105         UCMODE_VOICE   = 2,
106         UCMODE_HOP     = 4
107 };
108
109 /** Holds a user's modes on a channel
110  * This class associates a users privilages with a channel by creating a pointer link between
111  * a userrec and chanrec class. The uc_modes member holds a bitmask of which privilages the user
112  * has on the channel, such as op, voice, etc.
113  */
114 class ucrec : public classbase
115 {
116  public:
117         /** Contains a bitmask of the UCMODE_OP ... UCMODE_FOUNDER values.
118          * If this value is zero, the user has no privilages upon the channel.
119          */
120         char uc_modes;
121
122         /** Points to the channel record where the given modes apply.
123          * If the record is not in use, this value will be NULL.
124          */
125         chanrec *channel;
126
127         /** Constructor for ucrec
128          */
129         ucrec() : uc_modes(0), channel(NULL) { /* stub */ }
130
131         /** Destructor for ucrec
132          */
133         virtual ~ucrec() { /* stub */ }
134 };
135
136
137 /** Holds all relevent information for a channel.
138  * This class represents a channel, and contains its name, modes, time created, topic, topic set time,
139  * etc, and an instance of the BanList type.
140  */
141 class chanrec : public Extensible
142 {
143  private:
144
145         /** Connect a chanrec to a userrec
146          */
147         static chanrec* ForceChan(chanrec* Ptr,ucrec *a,userrec* user, int created);
148
149  public:
150         /** The channels name.
151          */
152         char name[CHANMAX]; /* channel name */
153         /** Modes for the channel.
154          * This is not a null terminated string! It is a hash where
155          * each item in it represents if a mode is set. For example
156          * for mode +A, index 0. Use modechar-65 to calculate which
157          * field to check.
158          */
159         char modes[64];
160
161         /** User lists
162          * There are four user lists, one for 
163          * all the users, one for the ops, one for
164          * the halfops and another for the voices.
165          */
166         CUList internal_userlist;
167         CUList internal_op_userlist;
168         CUList internal_halfop_userlist;
169         CUList internal_voice_userlist;
170
171         /** Parameters for custom modes
172          */
173         CustomModeList custom_mode_params;
174
175         /** Channel topic.
176          * If this is an empty string, no channel topic is set.
177          */
178         char topic[MAXTOPIC];
179         /** Creation time.
180          */
181         time_t created;
182         /** Time topic was set.
183          * If no topic was ever set, this will be equal to chanrec::created
184          */
185         time_t topicset;
186         /** The last user to set the topic.
187          * If this member is an empty string, no topic was ever set.
188          */
189         char setby[NICKMAX];
190
191         /** Contains the channel user limit.
192          * If this value is zero, there is no limit in place.
193          */
194         short int limit;
195         
196         /** Contains the channel key.
197          * If this value is an empty string, there is no channel key in place.
198          */
199         char key[32];
200         
201         /** Contains a bitmask of the CM_* builtin (RFC) binary mode symbols
202          */
203         //char binarymodes;
204         
205         /** The list of all bans set on the channel.
206          */
207         BanList bans;
208         
209         /** Sets or unsets a custom mode in the channels info
210          * @param mode The mode character to set or unset
211          * @param mode_on True if you want to set the mode or false if you want to remove it
212          */
213         void SetMode(char mode,bool mode_on);
214
215         /** Sets or unsets the parameters for a custom mode in a channels info
216          * @param mode The mode character to set or unset
217          * @param parameter The parameter string to associate with this mode character
218          * @param mode_on True if you want to set the mode or false if you want to remove it
219          */
220         void SetModeParam(char mode,const char* parameter,bool mode_on);
221  
222         /** Returns true if a mode is set on a channel
223           * @param mode The mode character you wish to query
224           * @return True if the custom mode is set, false if otherwise
225           */
226         bool IsModeSet(char mode);
227
228         /** Returns the parameter for a custom mode on a channel.
229           * @param mode The mode character you wish to query
230           *
231           * For example if "+L #foo" is set, and you pass this method
232           * 'L', it will return '#foo'. If the mode is not set on the
233           * channel, or the mode has no parameters associated with it,
234           * it will return an empty string.
235           *
236           * @return The parameter for this mode is returned, or an empty string
237           */
238         std::string GetModeParameter(char mode);
239
240         /** Obtain the channel "user counter"
241          * This returns the channel reference counter, which is initialized
242          * to 0 when the channel is created and incremented/decremented
243          * upon joins, parts quits and kicks.
244          *
245          * @return The number of users on this channel
246          */
247         long GetUserCounter();
248
249         /** Add a user pointer to the internal reference list
250          * @param user The user to add
251          *
252          * The data inserted into the reference list is a table as it is
253          * an arbitary pointer compared to other users by its memory address,
254          * as this is a very fast 32 or 64 bit integer comparison.
255          */
256         void AddUser(userrec* user);
257         void AddOppedUser(userrec* user);
258         void AddHalfoppedUser(userrec* user);
259         void AddVoicedUser(userrec* user);
260
261         /** Delete a user pointer to the internal reference list
262          * @param user The user to delete
263          * @return number of users left on the channel
264          */
265         unsigned long DelUser(userrec* user);
266         void DelOppedUser(userrec* user);
267         void DelHalfoppedUser(userrec* user);
268         void DelVoicedUser(userrec* user);
269
270         /** Obtain the internal reference list
271          * The internal reference list contains a list of userrec*.
272          * These are used for rapid comparison to determine
273          * channel membership for PRIVMSG, NOTICE, QUIT, PART etc.
274          * The resulting pointer to the vector should be considered
275          * readonly and only modified via AddUser and DelUser.
276          *
277          * @return This function returns pointer to a map of userrec pointers (CUList*).
278          */
279         CUList* GetUsers();
280         CUList* GetOppedUsers();
281         CUList* GetHalfoppedUsers();
282         CUList* GetVoicedUsers();
283
284         /** Returns true if the user given is on the given channel.
285          */
286         bool HasUser(userrec* user);
287
288         /** Creates a channel record and initialises it with default values
289          */
290         chanrec();
291
292         /** Make src kick user from this channel with the given reason.
293          * @param src The source of the kick
294          * @param user The user being kicked (must be on this channel)
295          * @param reason The reason for the kick
296          * @return The number of users left on the channel. If this is zero
297          * when the method returns, you MUST delete the chanrec immediately!
298          */
299         long KickUser(userrec *src, userrec *user, const char* reason);
300
301         /** Make the server kick user from this channel with the given reason.
302          * @param user The user being kicked (must be on this channel)
303          * @param reason The reason for the kick
304          * @param triggerevents True if you wish this kick to trigger module events
305          * @return The number of users left on the channel. If this is zero
306          * when the method returns, you MUST delete the chanrec immediately!
307          */
308         long ServerKickUser(userrec* user, const char* reason, bool triggerevents);
309
310         /** Part a user from this channel with the given reason.
311          * If the reason field is NULL, no reason will be sent.
312          * @param user The user who is parting (must be on this channel)
313          * @param reason The (optional) part reason
314          * @return The number of users left on the channel. If this is zero
315          * when the method returns, you MUST delete the chanrec immediately!
316          */
317         long PartUser(userrec *user, const char* reason = NULL);
318
319         /* Join a user to a channel. May be a channel that doesnt exist yet.
320          * @param user The user to join to the channel.
321          * @param cn The channel name to join to. Does not have to exist.
322          * @param key The key of the channel, if given
323          * @param override If true, override all join restrictions such as +bkil
324          * @return A pointer to the chanrec the user was joined to. A new chanrec may have
325          * been created if the channel did not exist before the user was joined to it.
326          * If the user could not be joined to a channel, the return value may be NULL.
327          */
328         static chanrec* JoinUser(userrec *user, const char* cn, bool override, const char* key = "");
329
330         void WriteChannel(userrec* user, char* text, ...);
331         void WriteChannel(userrec* user, const std::string &text);
332         void WriteChannelWithServ(const char* ServName, const char* text, ...);
333         void WriteChannelWithServ(const char* ServName, const std::string &text);
334         void WriteAllExceptSender(userrec* user, char status, char* text, ...);
335         void WriteAllExceptSender(userrec* user, char status, const std::string& text);
336
337         /** Destructor for chanrec
338          */
339         virtual ~chanrec() { /* stub */ }
340 };
341
342 #endif