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