1 /*
2     SPDX-License-Identifier: GPL-2.0-or-later
3 
4     SPDX-FileCopyrightText: 2002, 2003, 2004 Dario Abatianni <eisfuchs@tigress.com>
5 */
6 
7 #include "channelnick.h"
8 
9 #include "channel.h"
10 #include "server.h"
11 #include "konversation_log.h"
12 
13 
ChannelNick(const NickInfoPtr & nickInfo,const QString & channel)14 ChannelNick::ChannelNick(const NickInfoPtr& nickInfo, const QString& channel)
15 : QSharedData()
16 {
17     m_nickInfo = nickInfo;
18     m_isop = false;
19     m_isadmin = false;
20     m_isowner = false;
21     m_ishalfop = false;
22     m_hasvoice = false;
23     m_timeStamp = 0;
24     m_recentActivity = 0;
25     m_channel = channel;
26     m_isChanged = false;
27 }
28 
~ChannelNick()29 ChannelNick::~ChannelNick()
30 {
31 }
32 
isOp() const33 bool ChannelNick::isOp() const
34 {
35   return m_isop;
36 }
37 
isAdmin() const38 bool ChannelNick::isAdmin() const
39 {
40   return m_isadmin;
41 }
42 
isOwner() const43 bool ChannelNick::isOwner() const
44 {
45   return m_isowner;
46 }
47 
isHalfOp() const48 bool ChannelNick::isHalfOp() const
49 {
50   return m_ishalfop;
51 }
52 
hasVoice() const53 bool ChannelNick::hasVoice() const
54 {
55   return m_hasvoice;
56 }
57 
isAnyTypeOfOp() const58 bool ChannelNick::isAnyTypeOfOp() const
59 {
60   return m_isop || m_isadmin || m_isowner || m_ishalfop;
61 }
62 
getNickInfo() const63 NickInfoPtr ChannelNick::getNickInfo() const
64 {
65   return m_nickInfo;
66 }
67 
68 /** @param mode 'v' to set voice, 'a' to set admin, 'h' to set halfop, 'o' to set op.
69  *  @param state what to set the mode to.
70  */
setMode(char mode,bool state)71 bool ChannelNick::setMode(char mode, bool state)
72 {
73     switch (mode)
74     {
75         case 'q':
76             return setOwner(state);
77         case 'a':
78             return setAdmin(state);
79         case 'o':
80             return setOp(state);
81         case 'h':
82             return setHalfOp(state);
83         case 'v':
84             return setVoice(state);
85         default:
86             qCDebug(KONVERSATION_LOG) << "Mode '" << mode << "' not recognised in setModeForChannelNick";
87             return false;
88     }
89 }
90 
91 /** Used still for passing modes from inputfilter to Server.  Should be removed.
92  */
setMode(uint mode)93 bool ChannelNick::setMode(uint mode)
94 {
95     bool voice = mode%2;
96     mode >>= 1;
97     bool halfop = mode %2;
98     mode >>= 1;
99     bool op = mode %2;
100     mode >>= 1;
101     bool owner = mode %2;
102     mode >>= 1;
103     bool admin = mode %2;
104     return setMode(admin, owner, op, halfop, voice);
105 }
106 
setMode(bool admin,bool owner,bool op,bool halfop,bool voice)107 bool ChannelNick::setMode(bool admin,bool owner,bool op,bool halfop,bool voice)
108 {
109     if(m_isadmin==admin && m_isowner==owner && m_isop==op && m_ishalfop==halfop && m_hasvoice==voice)
110         return false;
111     m_isadmin=admin;
112     m_isowner=owner;
113     m_isop=op;
114     m_ishalfop=halfop;
115     m_hasvoice=voice;
116     markAsChanged();
117     return true;
118 }
119 
120 /** set the voice for the nick, and update
121  * @returns Whether it needed to be changed.  False for no change.
122  */
setVoice(bool state)123 bool ChannelNick::setVoice(bool state)
124 {
125     if(m_hasvoice==state) return false;
126     m_hasvoice=state;
127     markAsChanged();
128     return true;
129 }
130 
setOwner(bool state)131 bool ChannelNick::setOwner(bool state)
132 {
133     if(m_isowner==state) return false;
134     m_isowner=state;
135     markAsChanged();
136     return true;
137 }
138 
setAdmin(bool state)139 bool ChannelNick::setAdmin(bool state)
140 {
141     if(m_isadmin==state) return false;
142     m_isadmin=state;
143     markAsChanged();
144     return true;
145 }
146 
setHalfOp(bool state)147 bool ChannelNick::setHalfOp(bool state)
148 {
149     if(m_ishalfop==state) return false;
150     m_ishalfop=state;
151     markAsChanged();
152     return true;
153 }
154 
setOp(bool state)155 bool ChannelNick::setOp(bool state)
156 {
157     if(m_isop==state) return false;
158     m_isop=state;
159     markAsChanged();
160     return true;
161 }
162 
163 //Purely provided for convience because they are used so often.
164 //Just calls nickInfo->getNickname() etc
getNickname() const165 QString ChannelNick::getNickname() const
166 {
167     return m_nickInfo->getNickname();
168 }
169 
getHostmask() const170 QString ChannelNick::getHostmask() const
171 {
172     return m_nickInfo->getHostmask();
173 }
174 
tooltip() const175 QString ChannelNick::tooltip() const
176 {
177     QString strTooltip;
178     QTextStream tooltip( &strTooltip, QIODevice::WriteOnly );
179 
180     tooltip << "<qt>";
181 
182     tooltip << R"(<table cellspacing="5" cellpadding="0">)";
183 
184     m_nickInfo->tooltipTableData(tooltip);
185 
186     QStringList modes;
187     if(isOp()) modes << i18n("Operator");
188     if(isAdmin()) modes << i18n("Admin");
189     if(isOwner()) modes << i18n("Owner");
190     if(isHalfOp()) modes << i18n("Half-operator");
191     if(hasVoice()) modes << i18n("Has voice");
192     //Don't show anything if the user is just a normal user
193     //if(modes.empty()) modes << i18n("A normal user");
194     if(!modes.empty())
195     {
196         tooltip << "<tr><td><b>" << i18n("Mode") << ":</b></td><td>" << modes.join(QLatin1String(", ")) << "</td></tr>";
197     }
198     tooltip << "</table></qt>";
199     //qCDebug(KONVERSATION_LOG) << strTooltip ;
200     //if(!dirty) return QString();
201     return strTooltip;
202 }
203 
loweredNickname() const204 QString ChannelNick::loweredNickname() const
205 {
206     return m_nickInfo->loweredNickname();
207 }
208 
timeStamp() const209 uint ChannelNick::timeStamp() const
210 {
211   return m_timeStamp;
212 }
213 
recentActivity() const214 uint ChannelNick::recentActivity() const
215 {
216     return m_recentActivity;
217 }
218 
moreActive()219 void ChannelNick::moreActive()
220 {
221     m_recentActivity++;
222 }
223 
lessActive()224 void ChannelNick::lessActive()
225 {
226     m_recentActivity--;
227 }
228 
setTimeStamp(uint stamp)229 void ChannelNick::setTimeStamp(uint stamp)
230 {
231   m_timeStamp = stamp;
232 }
233 
markAsChanged()234 void ChannelNick::markAsChanged()
235 {
236     setChanged(true);
237     m_nickInfo->getServer()->startChannelNickChangedTimer(m_channel);
238 }
239