]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - include/channels.h
1b44a7be6fa7c5e47509c07c649032277142d8ef
[user/henk/code/inspircd.git] / include / channels.h
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2013-2016, 2018 Attila Molnar <attilamolnar@hush.com>
5  *   Copyright (C) 2012-2013, 2017-2020 Sadie Powell <sadie@witchery.services>
6  *   Copyright (C) 2012 Robby <robby@chatbelgie.be>
7  *   Copyright (C) 2009 Daniel De Graaf <danieldg@inspircd.org>
8  *   Copyright (C) 2007-2008 Robin Burchell <robin+git@viroteck.net>
9  *   Copyright (C) 2007, 2009 Dennis Friis <peavey@inspircd.org>
10  *   Copyright (C) 2003-2006, 2008, 2010 Craig Edwards <brain@inspircd.org>
11  *
12  * This file is part of InspIRCd.  InspIRCd is free software: you can
13  * redistribute it and/or modify it under the terms of the GNU General Public
14  * License as published by the Free Software Foundation, version 2.
15  *
16  * This program is distributed in the hope that it will be useful, but WITHOUT
17  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
19  * details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
23  */
24
25
26 #pragma once
27
28 #include "membership.h"
29 #include "mode.h"
30 #include "parammode.h"
31
32 /** Holds an entry for a ban list, exemption list, or invite list.
33  * This class contains a single element in a channel list, such as a banlist.
34  */
35
36 /** Holds all relevant information for a channel.
37  * This class represents a channel, and contains its name, modes, topic, topic set time,
38  * etc, and an instance of the BanList type.
39  */
40 class CoreExport Channel : public Extensible
41 {
42  public:
43         /** A map of Memberships on a channel keyed by User pointers
44          */
45         typedef std::map<User*, insp::aligned_storage<Membership> > MemberMap;
46
47  private:
48         /** Set default modes for the channel on creation
49          */
50         void SetDefaultModes();
51
52         /** Modes for the channel.
53          * It is a bitset where each item in it represents if a mode is set.
54          * To see if a mode is set, inspect modes[mh->modeid]
55          */
56         std::bitset<ModeParser::MODEID_MAX> modes;
57
58         /** Remove the given membership from the channel's internal map of
59          * memberships and destroy the Membership object.
60          * This function does not remove the channel from User::chanlist.
61          * Since the parameter is an iterator to the target, the complexity
62          * of this function is constant.
63          * @param membiter The MemberMap iterator to remove, must be valid
64          */
65         void DelUser(const MemberMap::iterator& membiter);
66
67  public:
68         /** Creates a channel record and initialises it with default values
69          * @param name The name of the channel
70          * @param ts The creation time of the channel
71          * @throw CoreException if this channel name is in use
72          */
73         Channel(const std::string &name, time_t ts);
74
75         /** Checks whether the channel should be destroyed, and if yes, begins
76          * the teardown procedure.
77          *
78          * If there are users on the channel or a module vetoes the deletion
79          * (OnPreChannelDelete hook) then nothing else happens.
80          * Otherwise, first the OnChannelDelete event is fired, then the channel is
81          * removed from the channel list. All pending invites are destroyed and
82          * finally the channel is added to the cull list.
83          */
84         void CheckDestroy();
85
86         /** The channel's name.
87          */
88         std::string name;
89
90         /** Time that the object was instantiated (used for TS calculation etc)
91         */
92         time_t age;
93
94         /** User list.
95          */
96         MemberMap userlist;
97
98         /** Channel topic.
99          * If this is an empty string, no channel topic is set.
100          */
101         std::string topic;
102
103         /** Time topic was set.
104          * If no topic was ever set, this will be equal to Channel::created
105          */
106         time_t topicset;
107
108         /** The last user to set the topic.
109          * If this member is an empty string, no topic was ever set.
110          */
111         std::string setby; /* 128 */
112
113         /** Sets or unsets a custom mode in the channels info
114          * @param mode The mode character to set or unset
115          * @param value True if you want to set the mode or false if you want to remove it
116          */
117         void SetMode(ModeHandler* mode, bool value);
118
119         /** Returns true if a mode is set on a channel
120           * @param mode The mode character you wish to query
121           * @return True if the custom mode is set, false if otherwise
122           */
123         bool IsModeSet(ModeHandler* mode) { return ((mode->GetId() != ModeParser::MODEID_MAX) && (modes[mode->GetId()])); }
124         bool IsModeSet(ModeHandler& mode) { return IsModeSet(&mode); }
125         bool IsModeSet(ChanModeReference& mode);
126
127         /** Returns the parameter for a custom mode on a channel.
128           * @param mode The mode character you wish to query
129           *
130           * For example if "+L #foo" is set, and you pass this method
131           * 'L', it will return '\#foo'. If the mode is not set on the
132           * channel, or the mode has no parameters associated with it,
133           * it will return an empty string.
134           *
135           * @return The parameter for this mode is returned, or an empty string
136           */
137         std::string GetModeParameter(ModeHandler* mode);
138         std::string GetModeParameter(ChanModeReference& mode);
139         std::string GetModeParameter(ParamModeBase* pm);
140
141         /** Sets the channel topic.
142          * @param user The user setting the topic.
143          * @param topic The topic to set it to.
144          * @param topicts Timestamp of the new topic.
145          * @param setter Setter string, may be used when the original setter is no longer online.
146          * If omitted or NULL, the setter string is obtained from the user.
147          */
148         void SetTopic(User* user, const std::string& topic, time_t topicts, const std::string* setter = NULL);
149
150         /** Obtain the channel "user counter"
151          * This returns the number of users on this channel
152          *
153          * @return The number of users on this channel
154          */
155         size_t GetUserCounter() const { return userlist.size(); }
156
157         /** Add a user pointer to the internal reference list
158          * @param user The user to add
159          *
160          * The data inserted into the reference list is a table as it is
161          * an arbitrary pointer compared to other users by its memory address,
162          * as this is a very fast 32 or 64 bit integer comparison.
163          */
164         Membership* AddUser(User* user);
165
166         /** Delete a user pointer to the internal reference list
167          * @param user The user to delete
168          * @return number of users left on the channel after deletion of the user
169          */
170         void DelUser(User* user);
171
172         /** Obtain the internal reference list
173          * The internal reference list contains a list of User*.
174          * These are used for rapid comparison to determine
175          * channel membership for PRIVMSG, NOTICE, QUIT, PART etc.
176          * The resulting pointer to the vector should be considered
177          * readonly and only modified via AddUser and DelUser.
178          *
179          * @return This function returns pointer to a map of User pointers (CUList*).
180          */
181         const MemberMap& GetUsers() const { return userlist; }
182
183         /** Returns true if the user given is on the given channel.
184          * @param user The user to look for
185          * @return True if the user is on this channel
186          */
187         bool HasUser(User* user);
188
189         Membership* GetUser(User* user);
190
191         /** Make src kick user from this channel with the given reason.
192          * @param src The source of the kick
193          * @param victimiter Iterator to the user being kicked, must be valid
194          * @param reason The reason for the kick
195          */
196         void KickUser(User* src, const MemberMap::iterator& victimiter, const std::string& reason);
197
198         /** Make src kick user from this channel with the given reason.
199          * @param src The source of the kick
200          * @param user The user being kicked
201          * @param reason The reason for the kick
202          */
203         void KickUser(User* src, User* user, const std::string& reason)
204         {
205                 MemberMap::iterator it = userlist.find(user);
206                 if (it != userlist.end())
207                         KickUser(src, it, reason);
208         }
209
210         /** Part a user from this channel with the given reason.
211          * If the reason field is NULL, no reason will be sent.
212          * @param user The user who is parting (must be on this channel)
213          * @param reason The part reason
214          * @return True if the user was on the channel and left, false if they weren't and nothing happened
215          */
216         bool PartUser(User* user, std::string& reason);
217
218         /** Join a local user to a channel, with or without permission checks. May be a channel that doesn't exist yet.
219          * @param user The user to join to the channel.
220          * @param channame The channel name to join to. Does not have to exist.
221          * @param key The key of the channel, if given
222          * @param override If true, override all join restrictions such as +bkil
223          * @return A pointer to the Channel the user was joined to. A new Channel may have
224          * been created if the channel did not exist before the user was joined to it.
225          * If the user could not be joined to a channel, the return value is NULL.
226          */
227         static Channel* JoinUser(LocalUser* user, std::string channame, bool override = false, const std::string& key = "");
228
229         /** Join a user to an existing channel, without doing any permission checks
230          * @param user The user to join to the channel
231          * @param privs Privileges (prefix mode letters) to give to this user, may be NULL
232          * @param bursting True if this join is the result of a netburst (passed to modules in the OnUserJoin hook)
233          * @param created_by_local True if this channel was just created by a local user (passed to modules in the OnUserJoin hook)
234          * @return A newly created Membership object, or NULL if the user was already inside the channel or if the user is a server user
235          */
236         Membership* ForceJoin(User* user, const std::string* privs = NULL, bool bursting = false, bool created_by_local = false);
237
238         /** Write to all users on a channel except some users
239          * @param protoev Event to send, may contain any number of messages.
240          * @param status The status of the users to write to, e.g. '@' or '%'. Use a value of 0 to write to everyone
241          * @param except_list List of users not to send to
242          */
243         void Write(ClientProtocol::Event& protoev, char status = 0, const CUList& except_list = CUList());
244
245         /** Write to all users on a channel except some users.
246          * @param protoevprov Protocol event provider for the message.
247          * @param msg Message to send.
248          * @param status The status of the users to write to, e.g. '@' or '%'. Use a value of 0 to write to everyone
249          * @param except_list List of users not to send to
250          */
251         void Write(ClientProtocol::EventProvider& protoevprov, ClientProtocol::Message& msg, char status = 0, const CUList& except_list = CUList());
252
253         /** Return the channel's modes with parameters.
254          * @param showsecret If this is set to true, the value of secret parameters
255          * are shown, otherwise they are replaced with '&lt;name&gt;'.
256          * @return The channel mode string
257          */
258         const char* ChanModes(bool showsecret);
259
260         /** Get the value of a users prefix on this channel.
261          * @param user The user to look up
262          * @return The module or core-defined value of the users prefix.
263          * The values for op, halfop and voice status are constants in
264          * mode.h, and are OP_VALUE, HALFOP_VALUE, and VOICE_VALUE respectively.
265          * If the value you are given does not match one of these three, it is
266          * a module-defined mode, and it should be compared in proportion to
267          * these three constants. For example a value greater than OP_VALUE
268          * is a prefix of greater 'worth' than ops, and a value less than
269          * VOICE_VALUE is of lesser 'worth' than a voice.
270          */
271         unsigned int GetPrefixValue(User* user);
272
273         /** Check if a user is banned on this channel
274          * @param user A user to check against the banlist
275          * @returns True if the user given is banned
276          */
277         bool IsBanned(User* user);
278
279         /** Check a single ban for match
280          */
281         bool CheckBan(User* user, const std::string& banmask);
282
283         /** Get the status of an "action" type extban
284          */
285         ModResult GetExtBanStatus(User *u, char type);
286
287         /** Write a NOTICE to all local users on the channel
288          * @param text Text to send
289          * @param status The minimum status rank to send this message to.
290          */
291         void WriteNotice(const std::string& text, char status = 0);
292         void WriteRemoteNotice(const std::string& text, char status = 0);
293 };
294
295 inline bool Channel::HasUser(User* user)
296 {
297         return (userlist.find(user) != userlist.end());
298 }
299
300 inline std::string Channel::GetModeParameter(ChanModeReference& mode)
301 {
302         if (!mode)
303                 return "";
304         return GetModeParameter(*mode);
305 }
306
307 inline std::string Channel::GetModeParameter(ModeHandler* mh)
308 {
309         std::string out;
310         ParamModeBase* pm = mh->IsParameterMode();
311         if (pm && this->IsModeSet(pm))
312                 pm->GetParameter(this, out);
313         return out;
314 }
315
316 inline std::string Channel::GetModeParameter(ParamModeBase* pm)
317 {
318         std::string out;
319         if (this->IsModeSet(pm))
320                 pm->GetParameter(this, out);
321         return out;
322 }
323
324 inline bool Channel::IsModeSet(ChanModeReference& mode)
325 {
326         if (!mode)
327                 return false;
328         return IsModeSet(*mode);
329 }