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