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