2 * InspIRCd -- Internet Relay Chat Daemon
4 * Copyright (C) 2012-2014 Attila Molnar <attilamolnar@hush.com>
5 * Copyright (C) 2009 Daniel De Graaf <danieldg@inspircd.org>
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.
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
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/>.
23 uint64_t ConvToUInt64(const std::string& in);
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).
32 class CoreExport Membership : public Extensible, public insp::intrusive_list_node<Membership>
35 /** Type of the Membership id
39 /** User on the channel
43 /** Channel the user is on
47 /** List of prefix mode letters this member has,
48 * sorted by prefix rank, highest first
52 /** Id of this Membership, set by the protocol module, other components should never read or
57 /** Converts a string to a Membership::Id
58 * @param str The string to convert
59 * @return Raw value of type Membership::Id
61 static Id IdFromString(const std::string& str)
63 return ConvToUInt64(str);
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.
71 Membership(User* u, Channel* c) : user(u), chan(c) {}
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
77 inline bool hasMode(char m) const
79 return modes.find(m) != std::string::npos;
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
87 unsigned int getRank();
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
97 bool SetPrefix(PrefixMode* mh, bool adding);
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().
106 char GetPrefixChar() const;
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.
113 const char* GetAllPrefixChars() const;
116 template <typename T>
120 /** List of pending Invitations
122 insp::intrusive_list<Invitation, T> invites;
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.
130 friend class Invitation;
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.
137 class CoreExport Invitation : public insp::intrusive_list_node<Invitation, Channel>, public insp::intrusive_list_node<Invitation, LocalUser>
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
144 Invitation(Channel* c, LocalUser* u, time_t timeout) : user(u), chan(c), expiry(timeout) {}
147 /** User the invitation is for
149 LocalUser* const user;
151 /** Channel where the user is invited to
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.
161 * Removes references to this Invitation from the associated user and channel.
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
173 static void Create(Channel* c, LocalUser* u, time_t timeout);
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
182 static Invitation* Find(Channel* c, LocalUser* u, bool check_expired = true);
185 typedef insp::intrusive_list<Invitation, LocalUser> InviteList;
188 inline void InviteBase<T>::ClearInvites()
190 for (typename insp::intrusive_list<Invitation, T>::iterator i = invites.begin(); i != invites.end(); )
192 Invitation* inv = *i;
193 // Destructing the Invitation invalidates the iterator, so move it now