1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2013-2014, 2016 Attila Molnar <attilamolnar@hush.com>
5  *   Copyright (C) 2013, 2017-2018 Sadie Powell <sadie@witchery.services>
6  *   Copyright (C) 2012 Robby <robby@chatbelgie.be>
7  *   Copyright (C) 2010 Craig Edwards <brain@inspircd.org>
8  *   Copyright (C) 2009 Daniel De Graaf <danieldg@inspircd.org>
9  *
10  * This file is part of InspIRCd.  InspIRCd is free software: you can
11  * redistribute it and/or modify it under the terms of the GNU General Public
12  * License as published by the Free Software Foundation, version 2.
13  *
14  * This program is distributed in the hope that it will be useful, but WITHOUT
15  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
17  * details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21  */
22 
23 
24 #pragma once
25 
26 #include "convto.h"
27 
28 /**
29  * Represents a member of a channel.
30  * A Membership object is created when a user joins a channel, and destroyed when a user leaves
31  * (via kick, part or quit) a channel.
32  * All prefix modes a member has is tracked by this object. Moreover, Memberships are Extensibles
33  * meaning modules can add arbitrary data to them using extensions (see m_delaymsg for an example).
34  */
35 class CoreExport Membership : public Extensible, public insp::intrusive_list_node<Membership>
36 {
37  public:
38 	/** Type of the Membership id
39 	 */
40 	typedef uint64_t Id;
41 
42 	/** User on the channel
43 	 */
44 	User* const user;
45 
46 	/** Channel the user is on
47 	 */
48 	Channel* const chan;
49 
50 	/** List of prefix mode letters this member has,
51 	 * sorted by prefix rank, highest first
52 	 */
53 	std::string modes;
54 
55 	/** Id of this Membership, set by the protocol module, other components should never read or
56 	 * write this field.
57 	 */
58 	Id id;
59 
60 	/** Converts a string to a Membership::Id
61 	 * @param str The string to convert
62 	 * @return Raw value of type Membership::Id
63 	 */
IdFromString(const std::string & str)64 	static Id IdFromString(const std::string& str)
65 	{
66 		return ConvToNum<Id>(str);
67 	}
68 
69 	/** Constructor, sets the user and chan fields to the parameters, does NOT update any bookkeeping
70 	 * information in the User or the Channel.
71 	 * Call Channel::JoinUser() or ForceJoin() to make a user join a channel instead of constructing
72 	 * Membership objects directly.
73 	 */
Membership(User * u,Channel * c)74 	Membership(User* u, Channel* c) : user(u), chan(c) {}
75 
76 	/** Check if this member has a given prefix mode set
77 	 * @param pm Prefix mode to check
78 	 * @return True if the member has the prefix mode set, false otherwise
79 	 */
HasMode(const PrefixMode * pm)80 	bool HasMode(const PrefixMode* pm) const
81 	{
82 		return (modes.find(pm->GetModeChar()) != std::string::npos);
83 	}
84 
85 	/** Returns the rank of this member.
86 	 * The rank of a member is defined as the rank given by the 'strongest' prefix mode a
87 	 * member has. See the PrefixMode class description for more info.
88 	 * @return The rank of the member
89 	 */
90 	unsigned int getRank();
91 
92 	/** Add a prefix character to a user.
93 	 * Only the core should call this method, usually from
94 	 * within the mode parser or when the first user joins
95 	 * the channel (to grant the default privs to them)
96 	 * @param mh The mode handler of the prefix mode to associate
97 	 * @param adding True if adding the prefix, false when removing
98 	 * @return True if a change was made
99 	 */
100 	bool SetPrefix(PrefixMode* mh, bool adding);
101 
102 	/** Get the highest prefix this user has on the channel
103 	 * @return A character containing the highest prefix.
104 	 * If the user has no prefix, 0 is returned. If the user has multiple prefixes,
105 	 * the highest is returned. If you do not recognise the prefix character you
106 	 * can get, you can deal with it in a 'proportional' manner compared to known
107 	 * prefixes, using GetPrefixValue().
108 	 */
109 	char GetPrefixChar() const;
110 
111 	/** Return all prefix chars this member has.
112 	 * @return A list of all prefix characters. The prefixes will always
113 	 * be in rank order, greatest first, as certain IRC clients require
114 	 * this when multiple prefixes are used names lists.
115 	 */
116 	std::string GetAllPrefixChars() const;
117 
118 	/** Sends a server notice to this user in the context of this channel.
119 	 * @param text The contents of the message to send.
120 	 */
121 	void WriteNotice(const std::string& text) const;
122 };
123