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