]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - include/channels.h
Module API changes to use Membership* where sensible
[user/henk/code/inspircd.git] / include / channels.h
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2009 InspIRCd Development Team
6  * See: http://wiki.inspircd.org/Credits
7  *
8  * This program is free but copyrighted software; see
9  *            the file COPYING for details.
10  *
11  * ---------------------------------------------------
12  */
13
14 #ifndef __CHANNELS_H__
15 #define __CHANNELS_H__
16
17 #include "membership.h"
18
19 /** RFC1459 channel modes
20  */
21 enum ChannelModes {
22         CM_TOPICLOCK = 't'-65,  /* +t: Only operators can change topic */
23         CM_NOEXTERNAL = 'n'-65, /* +n: Only users in the channel can message it */
24         CM_INVITEONLY = 'i'-65, /* +i: Invite only */
25         CM_MODERATED = 'm'-65,  /* +m: Only voices and above can talk */
26         CM_SECRET = 's'-65,     /* +s: Secret channel */
27         CM_PRIVATE = 'p'-65,    /* +p: Private channel */
28         CM_KEY = 'k'-65,        /* +k: Keyed channel */
29         CM_LIMIT = 'l'-65       /* +l: Maximum user limit */
30 };
31
32 /* Forward declarations - needed */
33 class User;
34 struct ModResult;
35
36 /** Holds an entry for a ban list, exemption list, or invite list.
37  * This class contains a single element in a channel list, such as a banlist.
38  */
39 class HostItem : public classbase
40 {
41  public:
42         /** Time the item was added
43          */
44         time_t set_time;
45         /** Who added the item
46          */
47         std::string set_by;
48         /** The actual item data
49          */
50         std::string data;
51
52         HostItem() { /* stub */ }
53         virtual ~HostItem() { /* stub */ }
54 };
55
56 /** A subclass of HostItem designed to hold channel bans (+b)
57  */
58 class BanItem : public HostItem
59 {
60 };
61
62 /** Holds a complete ban list
63  */
64 typedef std::list<BanItem>      BanList;
65
66 /** A list of custom modes parameters on a channel
67  */
68 typedef std::map<char,std::string> CustomModeList;
69
70 /** used to hold a channel and a users modes on that channel, e.g. +v, +h, +o
71  */
72 enum UserChannelModes {
73         UCMODE_OP       = 1,    /* Opped user */
74         UCMODE_VOICE    = 2,    /* Voiced user */
75         UCMODE_HOP      = 4     /* Halfopped user */
76 };
77
78 /** Holds all relevent information for a channel.
79  * This class represents a channel, and contains its name, modes, topic, topic set time,
80  * etc, and an instance of the BanList type.
81  */
82 class CoreExport Channel : public Extensible
83 {
84         /** Connect a Channel to a User
85          */
86         static Channel* ForceChan(InspIRCd* Instance, Channel* Ptr, User* user, const std::string &privs, bool bursting, bool created);
87
88         /** Set default modes for the channel on creation
89          */
90         void SetDefaultModes();
91
92         /** Maximum number of bans (cached)
93          */
94         int maxbans;
95
96  public:
97         /** Pointer to creator object
98          */
99         InspIRCd* ServerInstance;
100
101         /** Creates a channel record and initialises it with default values
102          * @throw Nothing at present.
103          */
104         Channel(InspIRCd* Instance, const std::string &name, time_t ts);
105
106         /** The channel's name.
107          */
108         std::string name; /* CHANMAX */
109
110         /** Time that the object was instantiated (used for TS calculation etc)
111         */
112         time_t age;
113
114         /** Modes for the channel.
115          * This is not a null terminated string! It is a bitset where
116          * each item in it represents if a mode is set. For example
117          * for mode +A, index 0. Use modechar-65 to calculate which
118          * field to check.
119          */
120         std::bitset<64> modes;
121
122         /** User list.
123          */
124         UserMembList userlist;
125
126         /** Parameters for custom modes.
127          * One for each custom mode letter.
128          */
129         CustomModeList custom_mode_params;
130
131         /** Channel topic.
132          * If this is an empty string, no channel topic is set.
133          */
134         std::string topic; /* MAXTOPIC */
135
136         /** Time topic was set.
137          * If no topic was ever set, this will be equal to Channel::created
138          */
139         time_t topicset;
140
141         /** The last user to set the topic.
142          * If this member is an empty string, no topic was ever set.
143          */
144         std::string setby; /* 128 */
145
146         /** The list of all bans set on the channel.
147          */
148         BanList bans;
149
150         /** Sets or unsets a custom mode in the channels info
151          * @param mode The mode character to set or unset
152          * @param mode_on True if you want to set the mode or false if you want to remove it
153          */
154         void SetMode(char mode,bool mode_on);
155
156         /** Sets or unsets a custom mode in the channels info
157          * @param mode The mode character to set or unset
158          * @param parameter The parameter string to associate with this mode character.
159          * If it is empty, the mode is unset; if it is nonempty, the mode is set.
160          */
161         void SetModeParam(char mode, std::string parameter);
162
163         /** Returns true if a mode is set on a channel
164           * @param mode The mode character you wish to query
165           * @return True if the custom mode is set, false if otherwise
166           */
167         inline bool IsModeSet(char mode) { return modes[mode-'A']; }
168
169
170         /** Returns the parameter for a custom mode on a channel.
171           * @param mode The mode character you wish to query
172           *
173           * For example if "+L #foo" is set, and you pass this method
174           * 'L', it will return '#foo'. If the mode is not set on the
175           * channel, or the mode has no parameters associated with it,
176           * it will return an empty string.
177           *
178           * @return The parameter for this mode is returned, or an empty string
179           */
180         std::string GetModeParameter(char mode);
181
182         /** Sets the channel topic.
183          * @param u The user setting the topic
184          * @param t The topic to set it to. Non-const, as it may be modified by a hook.
185          * @param forceset If set to true then all access checks will be bypassed.
186          */
187         int SetTopic(User *u, std::string &t, bool forceset = false);
188
189         /** Obtain the channel "user counter"
190          * This returns the channel reference counter, which is initialized
191          * to 0 when the channel is created and incremented/decremented
192          * upon joins, parts quits and kicks.
193          *
194          * @return The number of users on this channel
195          */
196         long GetUserCounter();
197
198         /** Add a user pointer to the internal reference list
199          * @param user The user to add
200          *
201          * The data inserted into the reference list is a table as it is
202          * an arbitary pointer compared to other users by its memory address,
203          * as this is a very fast 32 or 64 bit integer comparison.
204          */
205         Membership* AddUser(User* user);
206
207         /** Delete a user pointer to the internal reference list
208          * @param user The user to delete
209          * @return number of users left on the channel after deletion of the user
210          */
211         unsigned long DelUser(User* user);
212
213         /** Obtain the internal reference list
214          * The internal reference list contains a list of User*.
215          * These are used for rapid comparison to determine
216          * channel membership for PRIVMSG, NOTICE, QUIT, PART etc.
217          * The resulting pointer to the vector should be considered
218          * readonly and only modified via AddUser and DelUser.
219          *
220          * @return This function returns pointer to a map of User pointers (CUList*).
221          */
222         const UserMembList* GetUsers();
223
224         /** Returns true if the user given is on the given channel.
225          * @param The user to look for
226          * @return True if the user is on this channel
227          */
228         bool HasUser(User* user);
229
230         Membership* GetUser(User* user);
231
232         /** Make src kick user from this channel with the given reason.
233          * @param src The source of the kick
234          * @param user The user being kicked (must be on this channel)
235          * @param reason The reason for the kick
236          * @return The number of users left on the channel. If this is zero
237          * when the method returns, you MUST delete the Channel immediately!
238          */
239         long KickUser(User *src, User *user, const char* reason);
240
241         /** Make the server kick user from this channel with the given reason.
242          * @param user The user being kicked (must be on this channel)
243          * @param reason The reason for the kick
244          * @param triggerevents True if you wish this kick to trigger module events
245          * @return The number of users left on the channel. If this is zero
246          * when the method returns, you MUST delete the Channel immediately!
247          */
248         long ServerKickUser(User* user, const char* reason, const char* servername = NULL);
249
250         /** Part a user from this channel with the given reason.
251          * If the reason field is NULL, no reason will be sent.
252          * @param user The user who is parting (must be on this channel)
253          * @param reason The part reason
254          * @return The number of users left on the channel. If this is zero
255          * when the method returns, you MUST delete the Channel immediately!
256          */
257         long PartUser(User *user, std::string &reason);
258
259         /* Join a user to a channel. May be a channel that doesnt exist yet.
260          * @param user The user to join to the channel.
261          * @param cn The channel name to join to. Does not have to exist.
262          * @param key The key of the channel, if given
263          * @param override If true, override all join restrictions such as +bkil
264          * @return A pointer to the Channel the user was joined to. A new Channel may have
265          * been created if the channel did not exist before the user was joined to it.
266          * If the user could not be joined to a channel, the return value may be NULL.
267          */
268         static Channel* JoinUser(InspIRCd* ServerInstance, User *user, const char* cn, bool override, const char* key, bool bursting, time_t TS = 0);
269
270         /** Write to a channel, from a user, using va_args for text
271          * @param user User whos details to prefix the line with
272          * @param text A printf-style format string which builds the output line without prefix
273          * @param ... Zero or more POD types
274          */
275         void WriteChannel(User* user, const char* text, ...) CUSTOM_PRINTF(3, 4);
276
277         /** Write to a channel, from a user, using std::string for text
278          * @param user User whos details to prefix the line with
279          * @param text A std::string containing the output line without prefix
280          */
281         void WriteChannel(User* user, const std::string &text);
282
283         /** Write to a channel, from a server, using va_args for text
284          * @param ServName Server name to prefix the line with
285          * @param text A printf-style format string which builds the output line without prefix
286          * @param ... Zero or more POD type
287          */
288         void WriteChannelWithServ(const char* ServName, const char* text, ...) CUSTOM_PRINTF(3, 4);
289
290         /** Write to a channel, from a server, using std::string for text
291          * @param ServName Server name to prefix the line with
292          * @param text A std::string containing the output line without prefix
293          */
294         void WriteChannelWithServ(const char* ServName, const std::string &text);
295
296         /** Write to all users on a channel except a specific user, using va_args for text.
297          * Internally, this calls WriteAllExcept().
298          * @param user User whos details to prefix the line with, and to omit from receipt of the message
299          * @param serversource If this parameter is true, use the local server name as the source of the text, otherwise,
300          * use the nick!user@host of the user.
301          * @param status The status of the users to write to, e.g. '@' or '%'. Use a value of 0 to write to everyone
302          * @param text A printf-style format string which builds the output line without prefix
303          * @param ... Zero or more POD type
304          */
305         void WriteAllExceptSender(User* user, bool serversource, char status, const char* text, ...) CUSTOM_PRINTF(5, 6);
306
307         /** Write to all users on a channel except a list of users, using va_args for text
308          * @param user User whos details to prefix the line with, and to omit from receipt of the message
309          * @param serversource If this parameter is true, use the local server name as the source of the text, otherwise,
310          * use the nick!user@host of the user.
311          * @param status The status of the users to write to, e.g. '@' or '%'. Use a value of 0 to write to everyone
312          * @param except_list A list of users NOT to send the text to
313          * @param text A printf-style format string which builds the output line without prefix
314          * @param ... Zero or more POD type
315          */
316         void WriteAllExcept(User* user, bool serversource, char status, CUList &except_list, const char* text, ...) CUSTOM_PRINTF(6, 7);
317
318         /** Write to all users on a channel except a specific user, using std::string for text.
319          * Internally, this calls WriteAllExcept().
320          * @param user User whos details to prefix the line with, and to omit from receipt of the message
321          * @param serversource If this parameter is true, use the local server name as the source of the text, otherwise,
322          * use the nick!user@host of the user.
323          * @param status The status of the users to write to, e.g. '@' or '%'. Use a value of 0 to write to everyone
324          * @param text A std::string containing the output line without prefix
325          */
326         void WriteAllExceptSender(User* user, bool serversource, char status, const std::string& text);
327
328         /** Write to all users on a channel except a list of users, using std::string for text
329          * @param user User whos details to prefix the line with, and to omit from receipt of the message
330          * @param serversource If this parameter is true, use the local server name as the source of the text, otherwise,
331          * use the nick!user@host of the user.
332          * @param status The status of the users to write to, e.g. '@' or '%'. Use a value of 0 to write to everyone
333          * @param except_list A list of users NOT to send the text to
334          * @param text A std::string containing the output line without prefix
335          */
336         void WriteAllExcept(User* user, bool serversource, char status, CUList &except_list, const std::string& text);
337         /** Write a line of text that already includes the source */
338         void RawWriteAllExcept(User* user, bool serversource, char status, CUList &except_list, const std::string& text);
339
340         /** Returns the maximum number of bans allowed to be set on this channel
341          * @return The maximum number of bans allowed
342          */
343         long GetMaxBans();
344
345         /** Return the channel's modes with parameters.
346          * @param showkey If this is set to true, the actual key is shown,
347          * otherwise it is replaced with '&lt;KEY&gt;'
348          * @return The channel mode string
349          */
350         char* ChanModes(bool showkey);
351
352         /** Spool the NAMES list for this channel to the given user
353          * @param user The user to spool the NAMES list to
354          */
355         void UserList(User *user);
356
357         /** Get the number of invisible users on this channel
358          * @return Number of invisible users
359          */
360         int CountInvisible();
361
362         /** Get a users prefix on this channel in a string.
363          * @param user The user to look up
364          * @return A character array containing the prefix string.
365          * Unlike GetStatus and GetStatusFlags which will only return the
366          * core specified modes @, % and + (op, halfop and voice), GetPrefixChar
367          * will also return module-defined prefixes. If the user has to prefix,
368          * an empty but non-null string is returned. If the user has multiple
369          * prefixes, the highest is returned. If you do not recognise the prefix
370          * character you can get, you can deal with it in a 'proprtional' manner
371          * compared to known prefixes, using GetPrefixValue().
372          */
373         const char* GetPrefixChar(User *user);
374
375         /** Return all of a users mode prefixes into a char* string.
376          * @param user The user to look up
377          * @return A list of all prefix characters. The prefixes will always
378          * be in rank order, greatest first, as certain IRC clients require
379          * this when multiple prefixes are used names lists.
380          */
381         const char* GetAllPrefixChars(User* user);
382
383         /** Get the value of a users prefix on this channel.
384          * @param user The user to look up
385          * @return The module or core-defined value of the users prefix.
386          * The values for op, halfop and voice status are constants in
387          * mode.h, and are OP_VALUE, HALFOP_VALUE, and VOICE_VALUE respectively.
388          * If the value you are given does not match one of these three, it is
389          * a module-defined mode, and it should be compared in proportion to
390          * these three constants. For example a value greater than OP_VALUE
391          * is a prefix of greater 'worth' than ops, and a value less than
392          * VOICE_VALUE is of lesser 'worth' than a voice.
393          */
394         unsigned int GetPrefixValue(User* user);
395
396         /** This method removes all prefix characters from a user.
397          * It will not inform the user or the channel of the removal of prefixes,
398          * and should be used when the user parts or quits.
399          * @param user The user to remove all prefixes from
400          */
401         void RemoveAllPrefixes(User* user);
402
403         /** Add a prefix character to a user.
404          * Only the core should call this method, usually  from
405          * within the mode parser or when the first user joins
406          * the channel (to grant ops to them)
407          * @param user The user to associate the privilage with
408          * @param prefix The prefix character to associate
409          * @param prefix_rank The rank (value) of this prefix character
410          * @param adding True if adding the prefix, false when removing
411          */
412         void SetPrefix(User* user, char prefix, unsigned int prefix_rank, bool adding);
413
414         /** Check if a user is banned on this channel
415          * @param user A user to check against the banlist
416          * @returns True if the user given is banned
417          */
418         bool IsBanned(User* user);
419
420         /** Check whether an extban of a given type matches
421          * a given user for this channel.
422          * @param u The user to match bans against
423          * @param type The type of extban to check
424          */
425         ModResult GetExtBanStatus(User *u, char type);
426
427         /** Overloaded version to check whether a particular string is extbanned
428          */
429         ModResult GetExtBanStatus(const std::string &str, char type);
430
431         /** Clears the cached max bans value
432          */
433         void ResetMaxBans();
434
435         /** Destructor for Channel
436          */
437         virtual ~Channel() { /* stub */ }
438 };
439
440 #endif