]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - include/channels.h
Index Channel::modes and User::modes with the id of the mode instead of its letter
[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 #include "parammode.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<Channel>
38 {
39         /** Set default modes for the channel on creation
40          */
41         void SetDefaultModes();
42
43         /** Modes for the channel.
44          * It is a bitset where each item in it represents if a mode is set.
45          * To see if a mode is set, inspect modes[mh->modeid]
46          */
47         std::bitset<ModeParser::MODEID_MAX> modes;
48
49         /** Remove the given membership from the channel's internal map of
50          * memberships and destroy the Membership object.
51          * This function does not remove the channel from User::chanlist.
52          * Since the parameter is an iterator to the target, the complexity
53          * of this function is constant.
54          * @param membiter The UserMembIter to remove, must be valid
55          */
56         void DelUser(const UserMembIter& membiter);
57
58  public:
59         /** Creates a channel record and initialises it with default values
60          * @param name The name of the channel
61          * @param ts The creation time of the channel
62          * @throw CoreException if this channel name is in use
63          */
64         Channel(const std::string &name, time_t ts);
65
66         /** Checks whether the channel should be destroyed, and if yes, begins
67          * the teardown procedure.
68          *
69          * If there are users on the channel or a module vetoes the deletion
70          * (OnPreChannelDelete hook) then nothing else happens.
71          * Otherwise, first the OnChannelDelete event is fired, then the channel is
72          * removed from the channel list. All pending invites are destroyed and
73          * finally the channel is added to the cull list.
74          */
75         void CheckDestroy();
76
77         /** The channel's name.
78          */
79         std::string name;
80
81         /** Time that the object was instantiated (used for TS calculation etc)
82         */
83         time_t age;
84
85         /** User list.
86          */
87         UserMembList userlist;
88
89         /** Channel topic.
90          * If this is an empty string, no channel topic is set.
91          */
92         std::string topic;
93
94         /** Time topic was set.
95          * If no topic was ever set, this will be equal to Channel::created
96          */
97         time_t topicset;
98
99         /** The last user to set the topic.
100          * If this member is an empty string, no topic was ever set.
101          */
102         std::string setby; /* 128 */
103
104         /** Sets or unsets a custom mode in the channels info
105          * @param mode The mode character to set or unset
106          * @param value True if you want to set the mode or false if you want to remove it
107          */
108         void SetMode(ModeHandler* mode, bool value);
109
110         /** Returns true if a mode is set on a channel
111           * @param mode The mode character you wish to query
112           * @return True if the custom mode is set, false if otherwise
113           */
114         bool IsModeSet(ModeHandler* mode) { return ((mode->GetId() != ModeParser::MODEID_MAX) && (modes[mode->GetId()])); }
115         bool IsModeSet(ModeHandler& mode) { return IsModeSet(&mode); }
116         bool IsModeSet(ChanModeReference& mode);
117
118         /** Returns the parameter for a custom mode on a channel.
119           * @param mode The mode character you wish to query
120           *
121           * For example if "+L #foo" is set, and you pass this method
122           * 'L', it will return '\#foo'. If the mode is not set on the
123           * channel, or the mode has no parameters associated with it,
124           * it will return an empty string.
125           *
126           * @return The parameter for this mode is returned, or an empty string
127           */
128         std::string GetModeParameter(ModeHandler* mode);
129         std::string GetModeParameter(ChanModeReference& mode);
130         std::string GetModeParameter(ParamModeBase* pm);
131
132         /** Sets the channel topic.
133          * @param user The user setting the topic.
134          * @param topic The topic to set it to.
135          */
136         void SetTopic(User* user, const std::string& topic);
137
138         /** Obtain the channel "user counter"
139          * This returns the number of users on this channel
140          *
141          * @return The number of users on this channel
142          */
143         long GetUserCounter() const { return userlist.size(); }
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() const { return &userlist; }
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          * @param srcmemb The membership of the user who does the kick, can be NULL
184          */
185         void KickUser(User* src, User* user, const std::string& reason, Membership* srcmemb = NULL);
186
187         /** Part a user from this channel with the given reason.
188          * If the reason field is NULL, no reason will be sent.
189          * @param user The user who is parting (must be on this channel)
190          * @param reason The part reason
191          */
192         void PartUser(User *user, std::string &reason);
193
194         /** Join a local user to a channel, with or without permission checks. May be a channel that doesn't exist yet.
195          * @param user The user to join to the channel.
196          * @param channame The channel name to join to. Does not have to exist.
197          * @param key The key of the channel, if given
198          * @param override If true, override all join restrictions such as +bkil
199          * @return A pointer to the Channel the user was joined to. A new Channel may have
200          * been created if the channel did not exist before the user was joined to it.
201          * If the user could not be joined to a channel, the return value is NULL.
202          */
203         static Channel* JoinUser(LocalUser* user, std::string channame, bool override = false, const std::string& key = "");
204
205         /** Join a user to an existing channel, without doing any permission checks
206          * @param user The user to join to the channel
207          * @param privs Priviliges (prefix mode letters) to give to this user, may be NULL
208          * @param bursting True if this join is the result of a netburst (passed to modules in the OnUserJoin hook)
209          * @param created_by_local True if this channel was just created by a local user (passed to modules in the OnUserJoin hook)
210          */
211         void ForceJoin(User* user, const std::string* privs = NULL, bool bursting = false, bool created_by_local = false);
212
213         /** Write to a channel, from a user, using va_args for text
214          * @param user User whos details to prefix the line with
215          * @param text A printf-style format string which builds the output line without prefix
216          * @param ... Zero or more POD types
217          */
218         void WriteChannel(User* user, const char* text, ...) CUSTOM_PRINTF(3, 4);
219
220         /** Write to a channel, from a user, using std::string for text
221          * @param user User whos details to prefix the line with
222          * @param text A std::string containing the output line without prefix
223          */
224         void WriteChannel(User* user, const std::string &text);
225
226         /** Write to a channel, from a server, using va_args for text
227          * @param ServName Server name to prefix the line with
228          * @param text A printf-style format string which builds the output line without prefix
229          * @param ... Zero or more POD type
230          */
231         void WriteChannelWithServ(const std::string& ServName, const char* text, ...) CUSTOM_PRINTF(3, 4);
232
233         /** Write to a channel, from a server, using std::string for text
234          * @param ServName Server name to prefix the line with
235          * @param text A std::string containing the output line without prefix
236          */
237         void WriteChannelWithServ(const std::string& ServName, const std::string &text);
238
239         /** Write to all users on a channel except a specific user, using va_args for text.
240          * Internally, this calls WriteAllExcept().
241          * @param user User whos details to prefix the line with, and to omit from receipt of the message
242          * @param serversource If this parameter is true, use the local server name as the source of the text, otherwise,
243          * use the nick!user\@host of the user.
244          * @param status The status of the users to write to, e.g. '@' or '%'. Use a value of 0 to write to everyone
245          * @param text A printf-style format string which builds the output line without prefix
246          * @param ... Zero or more POD type
247          */
248         void WriteAllExceptSender(User* user, bool serversource, char status, const char* text, ...) CUSTOM_PRINTF(5, 6);
249
250         /** Write to all users on a channel except a list of users, using va_args for text
251          * @param user User whos details to prefix the line with, and to omit from receipt of the message
252          * @param serversource If this parameter is true, use the local server name as the source of the text, otherwise,
253          * use the nick!user\@host of the user.
254          * @param status The status of the users to write to, e.g. '@' or '%'. Use a value of 0 to write to everyone
255          * @param except_list A list of users NOT to send the text to
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 WriteAllExcept(User* user, bool serversource, char status, CUList &except_list, const char* text, ...) CUSTOM_PRINTF(6, 7);
260
261         /** Write to all users on a channel except a specific user, using std::string for text.
262          * Internally, this calls WriteAllExcept().
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 text A std::string containing the output line without prefix
268          */
269         void WriteAllExceptSender(User* user, bool serversource, char status, const std::string& text);
270
271         /** Write to all users on a channel except a list of users, using std::string 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 std::string containing the output line without prefix
278          */
279         void WriteAllExcept(User* user, bool serversource, char status, CUList &except_list, const std::string& text);
280         /** Write a line of text that already includes the source */
281         void RawWriteAllExcept(User* user, bool serversource, char status, CUList &except_list, const std::string& text);
282
283         /** Return the channel's modes with parameters.
284          * @param showkey If this is set to true, the actual key is shown,
285          * otherwise it is replaced with '&lt;KEY&gt;'
286          * @return The channel mode string
287          */
288         const char* ChanModes(bool showkey);
289
290         /** Spool the NAMES list for this channel to the given user
291          * @param user The user to spool the NAMES list to
292          */
293         void UserList(User *user);
294
295         /** Get the value of a users prefix on this channel.
296          * @param user The user to look up
297          * @return The module or core-defined value of the users prefix.
298          * The values for op, halfop and voice status are constants in
299          * mode.h, and are OP_VALUE, HALFOP_VALUE, and VOICE_VALUE respectively.
300          * If the value you are given does not match one of these three, it is
301          * a module-defined mode, and it should be compared in proportion to
302          * these three constants. For example a value greater than OP_VALUE
303          * is a prefix of greater 'worth' than ops, and a value less than
304          * VOICE_VALUE is of lesser 'worth' than a voice.
305          */
306         unsigned int GetPrefixValue(User* user);
307
308         /** Check if a user is banned on this channel
309          * @param user A user to check against the banlist
310          * @returns True if the user given is banned
311          */
312         bool IsBanned(User* user);
313
314         /** Check a single ban for match
315          */
316         bool CheckBan(User* user, const std::string& banmask);
317
318         /** Get the status of an "action" type extban
319          */
320         ModResult GetExtBanStatus(User *u, char type);
321 };
322
323 inline bool Channel::HasUser(User* user)
324 {
325         return (userlist.find(user) != userlist.end());
326 }
327
328 inline std::string Channel::GetModeParameter(ChanModeReference& mode)
329 {
330         if (!mode)
331                 return "";
332         return GetModeParameter(*mode);
333 }
334
335 inline std::string Channel::GetModeParameter(ModeHandler* mh)
336 {
337         std::string out;
338         ParamModeBase* pm = mh->IsParameterMode();
339         if (pm && this->IsModeSet(pm))
340                 pm->GetParameter(this, out);
341         return out;
342 }
343
344 inline std::string Channel::GetModeParameter(ParamModeBase* pm)
345 {
346         std::string out;
347         if (this->IsModeSet(pm))
348                 pm->GetParameter(this, out);
349         return out;
350 }
351
352 inline bool Channel::IsModeSet(ChanModeReference& mode)
353 {
354         if (!mode)
355                 return false;
356         return IsModeSet(*mode);
357 }