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