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