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