]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - include/membership.h
Document the Membership and the Invitation class
[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 /**
24  * Represents a member of a channel.
25  * A Membership object is created when a user joins a channel, and destroyed when a user leaves
26  * (via kick, part or quit) a channel.
27  * All prefix modes a member has is tracked by this object. Moreover, Memberships are Extensibles
28  * meaning modules can add arbitrary data to them using extensions (see m_delaymsg for an example).
29  */
30 class CoreExport Membership : public Extensible, public intrusive_list_node<Membership>
31 {
32  public:
33         /** User on the channel
34          */
35         User* const user;
36
37         /** Channel the user is on
38          */
39         Channel* const chan;
40
41         /** List of prefix mode letters this member has,
42          * sorted by prefix rank, highest first
43          */
44         std::string modes;
45
46         /** Constructor, sets the user and chan fields to the parameters, does NOT update any bookkeeping
47          * information in the User or the Channel.
48          * Call Channel::JoinUser() or ForceJoin() to make a user join a channel instead of constructing
49          * Membership objects directly.
50          */
51         Membership(User* u, Channel* c) : user(u), chan(c) {}
52
53         /** Returns true if this member has a given prefix mode set
54          * @param m The prefix mode letter to check
55          * @return True if the member has the prefix mode set, false otherwise
56          */
57         inline bool hasMode(char m) const
58         {
59                 return modes.find(m) != std::string::npos;
60         }
61
62         /** Returns the rank of this member.
63          * The rank of a member is defined as the rank given by the 'strongest' prefix mode a
64          * member has. See the PrefixMode class description for more info.
65          * @return The rank of the member
66          */
67         unsigned int getRank();
68
69         /** Add a prefix character to a user.
70          * Only the core should call this method, usually from
71          * within the mode parser or when the first user joins
72          * the channel (to grant the default privs to them)
73          * @param mh The mode handler of the prefix mode to associate
74          * @param adding True if adding the prefix, false when removing
75          * @return True if a change was made
76          */
77         bool SetPrefix(PrefixMode* mh, bool adding);
78
79         /** Get the highest prefix this user has on the channel
80          * @return A character containing the highest prefix.
81          * If the user has no prefix, 0 is returned. If the user has multiple prefixes,
82          * the highest is returned. If you do not recognise the prefix character you
83          * can get, you can deal with it in a 'proportional' manner compared to known
84          * prefixes, using GetPrefixValue().
85          */
86         char GetPrefixChar() const;
87
88         /** Return all prefix chars this member has.
89          * @return A list of all prefix characters. The prefixes will always
90          * be in rank order, greatest first, as certain IRC clients require
91          * this when multiple prefixes are used names lists.
92          */
93         const char* GetAllPrefixChars() const;
94 };
95
96 template <typename T>
97 class InviteBase
98 {
99  protected:
100         /** List of pending Invitations
101          */
102         intrusive_list<Invitation, T> invites;
103
104  public:
105         /** Remove and destruct all pending invitations this user or channel has.
106          * Must be called before the object is destroyed, also called when the TS of the channel is lowered.
107          */
108         void ClearInvites();
109
110         friend class Invitation;
111 };
112
113 /**
114  * The Invitation class contains all data about a pending invitation.
115  * Invitation objects are referenced from the user and the channel they belong to.
116  */
117 class CoreExport Invitation : public intrusive_list_node<Invitation, Channel>, public intrusive_list_node<Invitation, LocalUser>
118 {
119         /** Constructs an Invitation, only called by Create()
120          * @param c Channel the user is invited to
121          * @param u User being invited
122          * @param timeout Expiration time for this Invitation
123          */
124         Invitation(Channel* c, LocalUser* u, time_t timeout) : user(u), chan(c), expiry(timeout) {}
125
126  public:
127         /** User the invitation is for
128          */
129         LocalUser* const user;
130
131         /** Channel where the user is invited to
132          */
133         Channel* const chan;
134
135         /** Timestamp when this Invitation expires or 0 if it doesn't expire.
136          * Invitation::Create() can update this field; see that for more info.
137          */
138         time_t expiry;
139
140         /** Destructor
141          * Removes references to this Invitation from the associated user and channel.
142          */
143         ~Invitation();
144
145         /** Create or extend an Invitation.
146          * When a user is invited to join a channel either a new Invitation object is created or
147          * or the expiration timestamp is updated if there is already a pending Invitation for
148          * the given (user, channel) pair and the new expiration time is further than the current.
149          * @param c Target channel
150          * @param u Target user
151          * @param timeout Timestamp when the invite should expire, 0 for no expiration
152          */
153         static void Create(Channel* c, LocalUser* u, time_t timeout);
154
155         /** Finds the Invitation object for the given channel/user pair.
156          * @param c Target channel, can be NULL to remove expired entries
157          * @param u Target user, cannot be NULL
158          * @param check_expired Pass true to remove all expired invites found while searching, false
159          * to return with an Invitation even if it's expired
160          * @return Invitation object for the given (channel, user) pair if it exists, NULL otherwise
161          */
162         static Invitation* Find(Channel* c, LocalUser* u, bool check_expired = true);
163 };
164
165 typedef intrusive_list<Invitation, LocalUser> InviteList;
166
167 template<typename T>
168 inline void InviteBase<T>::ClearInvites()
169 {
170         for (typename intrusive_list<Invitation, T>::iterator i = invites.begin(); i != invites.end(); )
171         {
172                 Invitation* inv = *i;
173                 // Destructing the Invitation invalidates the iterator, so move it now
174                 ++i;
175                 delete inv;
176         }
177 }