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