1 /***************************************************************************
2 * Copyright (C) 2005-2018 by the Quassel Project *
3 * devel@quassel-irc.org *
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. *
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. *
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 ***************************************************************************/
25 #include "signalproxy.h"
26 #include "ircchannel.h"
31 IrcUser::IrcUser(const QString &hostmask, Network *network) : SyncableObject(network),
33 _nick(nickFromMask(hostmask)),
34 _user(userFromMask(hostmask)),
35 _host(hostFromMask(hostmask)),
40 // _idleTime(QDateTime::currentDateTime()),
42 _lastAwayMessageTime(),
46 _codecForEncoding(nullptr),
47 _codecForDecoding(nullptr)
50 _lastAwayMessageTime.setTimeSpec(Qt::UTC);
51 _lastAwayMessageTime.setMSecsSinceEpoch(0);
55 // ====================
57 // ====================
59 QString IrcUser::hostmask() const
61 return QString("%1!%2@%3").arg(nick()).arg(user()).arg(host());
65 QDateTime IrcUser::idleTime()
67 if ((QDateTime::currentDateTime().toMSecsSinceEpoch() - _idleTimeSet.toMSecsSinceEpoch())
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();
77 QStringList IrcUser::channels() const
81 foreach(channel, _channels) {
82 chanList << channel->name();
88 void IrcUser::setCodecForEncoding(const QString &name)
90 setCodecForEncoding(QTextCodec::codecForName(name.toLatin1()));
94 void IrcUser::setCodecForEncoding(QTextCodec *codec)
96 _codecForEncoding = codec;
100 void IrcUser::setCodecForDecoding(const QString &name)
102 setCodecForDecoding(QTextCodec::codecForName(name.toLatin1()));
106 void IrcUser::setCodecForDecoding(QTextCodec *codec)
108 _codecForDecoding = codec;
112 QString IrcUser::decodeString(const QByteArray &text) const
114 if (!codecForDecoding()) return network()->decodeString(text);
115 return ::decodeString(text, codecForDecoding());
119 QByteArray IrcUser::encodeString(const QString &string) const
121 if (codecForEncoding()) {
122 return codecForEncoding()->fromUnicode(string);
124 return network()->encodeString(string);
128 // ====================
130 // ====================
131 void IrcUser::setUser(const QString &user)
133 if (!user.isEmpty() && _user != user) {
140 void IrcUser::setRealName(const QString &realName)
142 if (!realName.isEmpty() && _realName != realName) {
143 _realName = realName;
149 void IrcUser::setAccount(const QString &account)
151 if (_account != account) {
158 void IrcUser::setAway(bool away)
169 void IrcUser::setAwayMessage(const QString &awayMessage)
171 if (!awayMessage.isEmpty() && _awayMessage != awayMessage) {
172 _awayMessage = awayMessage;
174 SYNC(ARG(awayMessage))
179 void IrcUser::setIdleTime(const QDateTime &idleTime)
181 if (idleTime.isValid() && _idleTime != idleTime) {
182 _idleTime = idleTime;
183 _idleTimeSet = QDateTime::currentDateTime();
189 void IrcUser::setLoginTime(const QDateTime &loginTime)
191 if (loginTime.isValid() && _loginTime != loginTime) {
192 _loginTime = loginTime;
198 void IrcUser::setServer(const QString &server)
200 if (!server.isEmpty() && _server != server) {
207 void IrcUser::setIrcOperator(const QString &ircOperator)
209 if (!ircOperator.isEmpty() && _ircOperator != ircOperator) {
210 _ircOperator = ircOperator;
211 SYNC(ARG(ircOperator))
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)
220 #if QT_VERSION >= 0x050800
221 QDateTime lastAwayMessageTime = QDateTime::fromSecsSinceEpoch(lastAwayMessage);
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);
227 lastAwayMessageTime.setTimeSpec(Qt::UTC);
228 setLastAwayMessageTime(lastAwayMessageTime);
232 void IrcUser::setLastAwayMessageTime(const QDateTime &lastAwayMessageTime)
234 if (lastAwayMessageTime > _lastAwayMessageTime) {
235 _lastAwayMessageTime = lastAwayMessageTime;
236 SYNC(ARG(lastAwayMessageTime))
241 void IrcUser::setHost(const QString &host)
243 if (!host.isEmpty() && _host != host) {
250 void IrcUser::setNick(const QString &nick)
252 if (!nick.isEmpty() && nick != _nick) {
261 void IrcUser::setWhoisServiceReply(const QString &whoisServiceReply)
263 if (!whoisServiceReply.isEmpty() && whoisServiceReply != _whoisServiceReply) {
264 _whoisServiceReply = whoisServiceReply;
265 SYNC(ARG(whoisServiceReply))
270 void IrcUser::setSuserHost(const QString &suserHost)
272 if (!suserHost.isEmpty() && suserHost != _suserHost) {
273 _suserHost = suserHost;
279 void IrcUser::setEncrypted(bool encrypted)
281 _encrypted = encrypted;
282 emit encryptedSet(encrypted);
287 void IrcUser::updateObjectName()
289 renameObject(QString::number(network()->networkId().toInt()) + "/" + _nick);
293 void IrcUser::updateHostmask(const QString &mask)
295 if (mask == hostmask())
298 QString user = userFromMask(mask);
299 QString host = hostFromMask(mask);
305 void IrcUser::joinChannel(IrcChannel *channel, bool skip_channel_join)
308 if (!_channels.contains(channel)) {
309 _channels.insert(channel);
310 if (!skip_channel_join)
311 channel->joinIrcUser(this);
316 void IrcUser::joinChannel(const QString &channelname)
318 joinChannel(network()->newIrcChannel(channelname));
322 void IrcUser::partChannel(IrcChannel *channel)
324 if (_channels.contains(channel)) {
325 _channels.remove(channel);
326 disconnect(channel, nullptr, this, nullptr);
328 QString channelName = channel->name();
329 SYNC_OTHER(partChannel, ARG(channelName))
330 if (_channels.isEmpty() && !network()->isMe(this))
336 void IrcUser::partChannel(const QString &channelname)
338 IrcChannel *channel = network()->ircChannel(channelname);
339 if (channel == nullptr) {
340 qWarning() << "IrcUser::partChannel(): received part for unknown Channel" << channelname;
343 partChannel(channel);
350 QList<IrcChannel *> channels = _channels.toList();
352 foreach(IrcChannel *channel, channels) {
353 disconnect(channel, nullptr, this, nullptr);
356 network()->removeIrcUser(this);
362 void IrcUser::channelDestroyed()
365 auto *channel = static_cast<IrcChannel *>(sender());
366 if (_channels.contains(channel)) {
367 _channels.remove(channel);
368 if (_channels.isEmpty() && !network()->isMe(this))
374 void IrcUser::setUserModes(const QString &modes)
376 if (_userModes != modes) {
379 emit userModesSet(modes);
384 void IrcUser::addUserModes(const QString &modes)
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];
400 emit userModesAdded(modes);
405 void IrcUser::removeUserModes(const QString &modes)
410 for (int i = 0; i < modes.count(); i++) {
411 _userModes.remove(modes[i]);
414 emit userModesRemoved(modes);
418 void IrcUser::setLastChannelActivity(BufferId buffer, const QDateTime &time)
420 _lastActivity[buffer] = time;
421 emit lastChannelActivityUpdated(buffer, time);
425 void IrcUser::setLastSpokenTo(BufferId buffer, const QDateTime &time)
427 _lastSpokenTo[buffer] = time;
428 emit lastSpokenToUpdated(buffer, time);