cmake: avoid de-duplication of user's CXXFLAGS
[quassel.git] / src / common / ircuser.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-2022 by the Quassel Project                        *
3  *   devel@quassel-irc.org                                                 *
4  *                                                                         *
5  *   This program is free software; you can redistribute it and/or modify  *
6  *   it under the terms of the GNU General Public License as published by  *
7  *   the Free Software Foundation; either version 2 of the License, or     *
8  *   (at your option) version 3.                                           *
9  *                                                                         *
10  *   This program is distributed in the hope that it will be useful,       *
11  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
12  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
13  *   GNU General Public License for more details.                          *
14  *                                                                         *
15  *   You should have received a copy of the GNU General Public License     *
16  *   along with this program; if not, write to the                         *
17  *   Free Software Foundation, Inc.,                                       *
18  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.         *
19  ***************************************************************************/
20
21 #include "ircuser.h"
22
23 #include <QDebug>
24 #include <QTextCodec>
25
26 #include "ircchannel.h"
27 #include "network.h"
28 #include "signalproxy.h"
29 #include "util.h"
30
31 IrcUser::IrcUser(const QString& hostmask, Network* network)
32     : SyncableObject(network)
33     , _initialized(false)
34     , _nick(nickFromMask(hostmask))
35     , _user(userFromMask(hostmask))
36     , _host(hostFromMask(hostmask))
37     , _realName()
38     , _awayMessage()
39     , _away(false)
40     , _server()
41     ,
42     // _idleTime(QDateTime::currentDateTime()),
43     _ircOperator()
44     , _lastAwayMessageTime()
45     , _whoisServiceReply()
46     , _encrypted(false)
47     , _network(network)
48     , _codecForEncoding(nullptr)
49     , _codecForDecoding(nullptr)
50 {
51     updateObjectName();
52     _lastAwayMessageTime.setTimeSpec(Qt::UTC);
53     _lastAwayMessageTime.setMSecsSinceEpoch(0);
54 }
55
56 // ====================
57 //  PUBLIC:
58 // ====================
59
60 QString IrcUser::hostmask() const
61 {
62     return QString("%1!%2@%3").arg(nick()).arg(user()).arg(host());
63 }
64
65 QDateTime IrcUser::idleTime()
66 {
67     if ((QDateTime::currentDateTime().toMSecsSinceEpoch() - _idleTimeSet.toMSecsSinceEpoch()) > 1200000) {
68         // 20 * 60 * 1000 = 1200000
69         // 20 minutes have elapsed, clear the known idle time as it's likely inaccurate by now
70         _idleTime = QDateTime();
71     }
72     return _idleTime;
73 }
74
75 QStringList IrcUser::channels() const
76 {
77     QStringList chanList;
78     IrcChannel* channel;
79     foreach (channel, _channels) {
80         chanList << channel->name();
81     }
82     return chanList;
83 }
84
85 void IrcUser::setCodecForEncoding(const QString& name)
86 {
87     setCodecForEncoding(QTextCodec::codecForName(name.toLatin1()));
88 }
89
90 void IrcUser::setCodecForEncoding(QTextCodec* codec)
91 {
92     _codecForEncoding = codec;
93 }
94
95 void IrcUser::setCodecForDecoding(const QString& name)
96 {
97     setCodecForDecoding(QTextCodec::codecForName(name.toLatin1()));
98 }
99
100 void IrcUser::setCodecForDecoding(QTextCodec* codec)
101 {
102     _codecForDecoding = codec;
103 }
104
105 QString IrcUser::decodeString(const QByteArray& text) const
106 {
107     if (!codecForDecoding())
108         return network()->decodeString(text);
109     return ::decodeString(text, codecForDecoding());
110 }
111
112 QByteArray IrcUser::encodeString(const QString& string) const
113 {
114     if (codecForEncoding()) {
115         return codecForEncoding()->fromUnicode(string);
116     }
117     return network()->encodeString(string);
118 }
119
120 // ====================
121 //  PUBLIC SLOTS:
122 // ====================
123 void IrcUser::setUser(const QString& user)
124 {
125     if (!user.isEmpty() && _user != user) {
126         _user = user;
127         SYNC(ARG(user));
128     }
129 }
130
131 void IrcUser::setRealName(const QString& realName)
132 {
133     if (!realName.isEmpty() && _realName != realName) {
134         _realName = realName;
135         SYNC(ARG(realName))
136     }
137 }
138
139 void IrcUser::setAccount(const QString& account)
140 {
141     if (_account != account) {
142         _account = account;
143         SYNC(ARG(account))
144     }
145 }
146
147 void IrcUser::setAway(bool away)
148 {
149     if (away != _away) {
150         _away = away;
151         markAwayChanged();
152         SYNC(ARG(away))
153         emit awaySet(away);
154     }
155 }
156
157 void IrcUser::setAwayMessage(const QString& awayMessage)
158 {
159     if (!awayMessage.isEmpty() && _awayMessage != awayMessage) {
160         _awayMessage = awayMessage;
161         markAwayChanged();
162         SYNC(ARG(awayMessage))
163     }
164 }
165
166 void IrcUser::setIdleTime(const QDateTime& idleTime)
167 {
168     if (idleTime.isValid() && _idleTime != idleTime) {
169         _idleTime = idleTime;
170         _idleTimeSet = QDateTime::currentDateTime();
171         SYNC(ARG(idleTime))
172     }
173 }
174
175 void IrcUser::setLoginTime(const QDateTime& loginTime)
176 {
177     if (loginTime.isValid() && _loginTime != loginTime) {
178         _loginTime = loginTime;
179         SYNC(ARG(loginTime))
180     }
181 }
182
183 void IrcUser::setServer(const QString& server)
184 {
185     if (!server.isEmpty() && _server != server) {
186         _server = server;
187         SYNC(ARG(server))
188     }
189 }
190
191 void IrcUser::setIrcOperator(const QString& ircOperator)
192 {
193     if (!ircOperator.isEmpty() && _ircOperator != ircOperator) {
194         _ircOperator = ircOperator;
195         SYNC(ARG(ircOperator))
196     }
197 }
198
199 // This function is only ever called by SYNC calls from legacy cores (pre-0.13).
200 // Therefore, no SYNC call is needed here.
201 void IrcUser::setLastAwayMessage(int lastAwayMessage)
202 {
203 #if QT_VERSION >= 0x050800
204     QDateTime lastAwayMessageTime = QDateTime::fromSecsSinceEpoch(lastAwayMessage);
205 #else
206     // toSecsSinceEpoch() was added in Qt 5.8.  Manually downconvert to seconds for now.
207     // See https://doc.qt.io/qt-5/qdatetime.html#toMSecsSinceEpoch
208     QDateTime lastAwayMessageTime = QDateTime::fromMSecsSinceEpoch(lastAwayMessage * 1000);
209 #endif
210     lastAwayMessageTime.setTimeSpec(Qt::UTC);
211     setLastAwayMessageTime(lastAwayMessageTime);
212 }
213
214 void IrcUser::setLastAwayMessageTime(const QDateTime& lastAwayMessageTime)
215 {
216     if (lastAwayMessageTime > _lastAwayMessageTime) {
217         _lastAwayMessageTime = lastAwayMessageTime;
218         SYNC(ARG(lastAwayMessageTime))
219     }
220 }
221
222 void IrcUser::setHost(const QString& host)
223 {
224     if (!host.isEmpty() && _host != host) {
225         _host = host;
226         SYNC(ARG(host))
227     }
228 }
229
230 void IrcUser::setNick(const QString& nick)
231 {
232     if (!nick.isEmpty() && nick != _nick) {
233         _nick = nick;
234         updateObjectName();
235         SYNC(ARG(nick))
236         emit nickSet(nick);
237     }
238 }
239
240 void IrcUser::setWhoisServiceReply(const QString& whoisServiceReply)
241 {
242     if (!whoisServiceReply.isEmpty() && whoisServiceReply != _whoisServiceReply) {
243         _whoisServiceReply = whoisServiceReply;
244         SYNC(ARG(whoisServiceReply))
245     }
246 }
247
248 void IrcUser::setSuserHost(const QString& suserHost)
249 {
250     if (!suserHost.isEmpty() && suserHost != _suserHost) {
251         _suserHost = suserHost;
252         SYNC(ARG(suserHost))
253     }
254 }
255
256 void IrcUser::setEncrypted(bool encrypted)
257 {
258     _encrypted = encrypted;
259     emit encryptedSet(encrypted);
260     SYNC(ARG(encrypted))
261 }
262
263 void IrcUser::updateObjectName()
264 {
265     setObjectName(QString::number(network()->networkId().toInt()) + "/" + _nick);
266 }
267
268 void IrcUser::updateHostmask(const QString& mask)
269 {
270     if (mask == hostmask())
271         return;
272
273     QString user = userFromMask(mask);
274     QString host = hostFromMask(mask);
275     setUser(user);
276     setHost(host);
277 }
278
279 void IrcUser::joinChannel(IrcChannel* channel, bool skip_channel_join)
280 {
281     Q_ASSERT(channel);
282     if (!_channels.contains(channel)) {
283         _channels.insert(channel);
284         if (!skip_channel_join)
285             channel->joinIrcUser(this);
286     }
287 }
288
289 void IrcUser::joinChannel(const QString& channelname)
290 {
291     joinChannel(network()->newIrcChannel(channelname));
292 }
293
294 void IrcUser::partChannel(IrcChannel* channel)
295 {
296     partChannelInternal(channel, false);
297 }
298
299 void IrcUser::partChannel(const QString& channelname)
300 {
301     IrcChannel* channel = network()->ircChannel(channelname);
302     if (channel == nullptr) {
303         qWarning() << "IrcUser::partChannel(): received part for unknown Channel" << channelname;
304     }
305     else {
306         partChannel(channel);
307     }
308 }
309
310 void IrcUser::partChannelInternal(IrcChannel* channel, bool skip_sync)
311 {
312     if (_channels.contains(channel)) {
313         _channels.remove(channel);
314         disconnect(channel, nullptr, this, nullptr);
315         channel->part(this);
316         QString channelName = channel->name();
317         if (!skip_sync) SYNC_OTHER(partChannel, ARG(channelName))
318         if (_channels.isEmpty() && !network()->isMe(this))
319             quitInternal(skip_sync);
320     }
321 }
322
323 void IrcUser::quit()
324 {
325     quitInternal(false);
326 }
327
328 void IrcUser::quitInternal(bool skip_sync)
329 {
330     QList<IrcChannel*> channels = _channels.values();
331     _channels.clear();
332     foreach (IrcChannel* channel, channels) {
333         disconnect(channel, nullptr, this, nullptr);
334         channel->part(this);
335     }
336     network()->removeIrcUser(this);
337     if (!skip_sync) SYNC_OTHER(quit, NO_ARG)
338     emit quited();
339 }
340
341 void IrcUser::channelDestroyed()
342 {
343     // private slot!
344     auto* channel = static_cast<IrcChannel*>(sender());
345     if (_channels.contains(channel)) {
346         _channels.remove(channel);
347         if (_channels.isEmpty() && !network()->isMe(this))
348             quit();
349     }
350 }
351
352 void IrcUser::setUserModes(const QString& modes)
353 {
354     if (_userModes != modes) {
355         _userModes = modes;
356         SYNC(ARG(modes))
357         emit userModesSet(modes);
358     }
359 }
360
361 void IrcUser::addUserModes(const QString& modes)
362 {
363     if (modes.isEmpty())
364         return;
365
366     // Don't needlessly sync when no changes are made
367     bool changesMade = false;
368     for (int i = 0; i < modes.count(); i++) {
369         if (!_userModes.contains(modes[i])) {
370             _userModes += modes[i];
371             changesMade = true;
372         }
373     }
374
375     if (changesMade) {
376         SYNC(ARG(modes))
377         emit userModesAdded(modes);
378     }
379 }
380
381 void IrcUser::removeUserModes(const QString& modes)
382 {
383     if (modes.isEmpty())
384         return;
385
386     for (int i = 0; i < modes.count(); i++) {
387         _userModes.remove(modes[i]);
388     }
389     SYNC(ARG(modes))
390     emit userModesRemoved(modes);
391 }
392
393 void IrcUser::setLastChannelActivity(BufferId buffer, const QDateTime& time)
394 {
395     _lastActivity[buffer] = time;
396     emit lastChannelActivityUpdated(buffer, time);
397 }
398
399 void IrcUser::setLastSpokenTo(BufferId buffer, const QDateTime& time)
400 {
401     _lastSpokenTo[buffer] = time;
402     emit lastSpokenToUpdated(buffer, time);
403 }