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