]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - include/membership.h
Merge insp20
[user/henk/code/inspircd.git] / include / membership.h
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2012-2014 Attila Molnar <attilamolnar@hush.com>
5  *   Copyright (C) 2009 Daniel De Graaf <danieldg@inspircd.org>
6  *
7  * This file is part of InspIRCd.  InspIRCd is free software: you can
8  * redistribute it and/or modify it under the terms of the GNU General Public
9  * License as published by the Free Software Foundation, version 2.
10  *
11  * This program is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
14  * details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20
21 #pragma once
22
23 uint64_t ConvToUInt64(const std::string& in);
24
25 /**
26  * Represents a member of a channel.
27  * A Membership object is created when a user joins a channel, and destroyed when a user leaves
28  * (via kick, part or quit) a channel.
29  * All prefix modes a member has is tracked by this object. Moreover, Memberships are Extensibles
30  * meaning modules can add arbitrary data to them using extensions (see m_delaymsg for an example).
31  */
32 class CoreExport Membership : public Extensible, public intrusive_list_node<Membership>
33 {
34  public:
35         /** Type of the Membership id
36          */
37         typedef uint64_t Id;
38
39         /** User on the channel
40          */
41         User* const user;
42
43         /** Channel the user is on
44          */
45         Channel* const chan;
46
47         /** List of prefix mode letters this member has,
48          * sorted by prefix rank, highest first
49          */
50         std::string modes;
51
52         /** Id of this Membership, set by the protocol module, other components should never read or
53          * write this field.
54          */
55         Id id;
56
57         /** Converts a string to a Membership::Id
58          * @param str The string to convert
59          * @return Raw value of type Membership::Id
60          */
61         static Id IdFromString(const std::string& str)
62         {
63                 return ConvToUInt64(str);
64         }
65
66         /** Constructor, sets the user and chan fields to the parameters, does NOT update any bookkeeping
67          * information in the User or the Channel.
68          * Call Channel::JoinUser() or ForceJoin() to make a user join a channel instead of constructing
69          * Membership objects directly.
70          */
71         Membership(User* u, Channel* c) : user(u), chan(c) {}
72
73         /** Returns true if this member has a given prefix mode set
74          * @param m The prefix mode letter to check
75          * @return True if the member has the prefix mode set, false otherwise
76          */
77         inline bool hasMode(char m) const
78         {
79                 return modes.find(m) != std::string::npos;
80         }
81
82         /** Returns the rank of this member.
83          * The rank of a member is defined as the rank given by the 'strongest' prefix mode a
84          * member has. See the PrefixMode class description for more info.
85          * @return The rank of the member
86          */
87         unsigned int getRank();
88
89         /** Add a prefix character to a user.
90          * Only the core should call this method, usually from
91          * within the mode parser or when the first user joins
92          * the channel (to grant the default privs to them)
93          * @param mh The mode handler of the prefix mode to associate
94          * @param adding True if adding the prefix, false when removing
95          * @return True if a change was made
96          */
97         bool SetPrefix(PrefixMode* mh, bool adding);
98
99         /** Get the highest prefix this user has on the channel
100          * @return A character containing the highest prefix.
101          * If the user has no prefix, 0 is returned. If the user has multiple prefixes,
102          * the highest is returned. If you do not recognise the prefix character you
103          * can get, you can deal with it in a 'proportional' manner compared to known
104          * prefixes, using GetPrefixValue().
105          */
106         char GetPrefixChar() const;
107
108         /** Return all prefix chars this member has.
109          * @return A list of all prefix characters. The prefixes will always
110          * be in rank order, greatest first, as certain IRC clients require
111          * this when multiple prefixes are used names lists.
112          */
113         const char* GetAllPrefixChars() const;
114 };
115
116 template <typename T>
117 class InviteBase
118 {
119  protected:
120         /** List of pending Invitations
121          */
122         intrusive_list<Invitation, T> invites;
123
124  public:
125         /** Remove and destruct all pending invitations this user or channel has.
126          * Must be called before the object is destroyed, also called when the TS of the channel is lowered.
127          */
128         void ClearInvites();
129
130         friend class Invitation;
131 };
132
133 /**
134  * The Invitation class contains all data about a pending invitation.
135  * Invitation objects are referenced from the user and the channel they belong to.
136  */
137 class CoreExport Invitation : public intrusive_list_node<Invitation, Channel>, public intrusive_list_node<Invitation, LocalUser>
138 {
139         /** Constructs an Invitation, only called by Create()
140          * @param c Channel the user is invited to
141          * @param u User being invited
142          * @param timeout Expiration time for this Invitation
143          */
144         Invitation(Channel* c, LocalUser* u, time_t timeout) : user(u), chan(c), expiry(timeout) {}
145
146  public:
147         /** User the invitation is for
148          */
149         LocalUser* const user;
150
151         /** Channel where the user is invited to
152          */
153         Channel* const chan;
154
155         /** Timestamp when this Invitation expires or 0 if it doesn't expire.
156          * Invitation::Create() can update this field; see that for more info.
157          */
158         time_t expiry;
159
160         /** Destructor
161          * Removes references to this Invitation from the associated user and channel.
162          */
163         ~Invitation();
164
165         /** Create or extend an Invitation.
166          * When a user is invited to join a channel either a new Invitation object is created or
167          * or the expiration timestamp is updated if there is already a pending Invitation for
168          * the given (user, channel) pair and the new expiration time is further than the current.
169          * @param c Target channel
170          * @param u Target user
171          * @param timeout Timestamp when the invite should expire, 0 for no expiration
172          */
173         static void Create(Channel* c, LocalUser* u, time_t timeout);
174
175         /** Finds the Invitation object for the given channel/user pair.
176          * @param c Target channel, can be NULL to remove expired entries
177          * @param u Target user, cannot be NULL
178          * @param check_expired Pass true to remove all expired invites found while searching, false
179          * to return with an Invitation even if it's expired
180          * @return Invitation object for the given (channel, user) pair if it exists, NULL otherwise
181          */
182         static Invitation* Find(Channel* c, LocalUser* u, bool check_expired = true);
183 };
184
185 typedef intrusive_list<Invitation, LocalUser> InviteList;
186
187 template<typename T>
188 inline void InviteBase<T>::ClearInvites()
189 {
190         for (typename intrusive_list<Invitation, T>::iterator i = invites.begin(); i != invites.end(); )
191         {
192                 Invitation* inv = *i;
193                 // Destructing the Invitation invalidates the iterator, so move it now
194                 ++i;
195                 delete inv;
196         }
197 }