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