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);
60 // ====================
62 // ====================
64 QString IrcUser::hostmask() const
66 return QString("%1!%2@%3").arg(nick()).arg(user()).arg(host());
70 QDateTime IrcUser::idleTime()
72 if ((QDateTime::currentDateTime().toMSecsSinceEpoch() - _idleTimeSet.toMSecsSinceEpoch())
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();
82 QStringList IrcUser::channels() const
86 foreach(channel, _channels) {
87 chanList << channel->name();
93 void IrcUser::setCodecForEncoding(const QString &name)
95 setCodecForEncoding(QTextCodec::codecForName(name.toLatin1()));
99 void IrcUser::setCodecForEncoding(QTextCodec *codec)
101 _codecForEncoding = codec;
105 void IrcUser::setCodecForDecoding(const QString &name)
107 setCodecForDecoding(QTextCodec::codecForName(name.toLatin1()));
111 void IrcUser::setCodecForDecoding(QTextCodec *codec)
113 _codecForDecoding = codec;
117 QString IrcUser::decodeString(const QByteArray &text) const
119 if (!codecForDecoding()) return network()->decodeString(text);
120 return ::decodeString(text, codecForDecoding());
124 QByteArray IrcUser::encodeString(const QString &string) const
126 if (codecForEncoding()) {
127 return codecForEncoding()->fromUnicode(string);
129 return network()->encodeString(string);
133 // ====================
135 // ====================
136 void IrcUser::setUser(const QString &user)
138 if (!user.isEmpty() && _user != user) {
145 void IrcUser::setRealName(const QString &realName)
147 if (!realName.isEmpty() && _realName != realName) {
148 _realName = realName;
154 void IrcUser::setAccount(const QString &account)
156 if (_account != account) {
163 void IrcUser::setAway(bool away)
174 void IrcUser::setAwayMessage(const QString &awayMessage)
176 if (!awayMessage.isEmpty() && _awayMessage != awayMessage) {
177 _awayMessage = awayMessage;
179 SYNC(ARG(awayMessage))
184 void IrcUser::setIdleTime(const QDateTime &idleTime)
186 if (idleTime.isValid() && _idleTime != idleTime) {
187 _idleTime = idleTime;
188 _idleTimeSet = QDateTime::currentDateTime();
194 void IrcUser::setLoginTime(const QDateTime &loginTime)
196 if (loginTime.isValid() && _loginTime != loginTime) {
197 _loginTime = loginTime;
203 void IrcUser::setServer(const QString &server)
205 if (!server.isEmpty() && _server != server) {
212 void IrcUser::setIrcOperator(const QString &ircOperator)
214 if (!ircOperator.isEmpty() && _ircOperator != ircOperator) {
215 _ircOperator = ircOperator;
216 SYNC(ARG(ircOperator))
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)
225 #if QT_VERSION >= 0x050800
226 QDateTime lastAwayMessageTime = QDateTime::fromSecsSinceEpoch(lastAwayMessage);
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);
232 lastAwayMessageTime.setTimeSpec(Qt::UTC);
233 setLastAwayMessageTime(lastAwayMessageTime);
237 void IrcUser::setLastAwayMessageTime(const QDateTime &lastAwayMessageTime)
239 if (lastAwayMessageTime > _lastAwayMessageTime) {
240 _lastAwayMessageTime = lastAwayMessageTime;
241 SYNC(ARG(lastAwayMessageTime))
246 void IrcUser::setHost(const QString &host)
248 if (!host.isEmpty() && _host != host) {
255 void IrcUser::setNick(const QString &nick)
257 if (!nick.isEmpty() && nick != _nick) {
266 void IrcUser::setWhoisServiceReply(const QString &whoisServiceReply)
268 if (!whoisServiceReply.isEmpty() && whoisServiceReply != _whoisServiceReply) {
269 _whoisServiceReply = whoisServiceReply;
270 SYNC(ARG(whoisServiceReply))
275 void IrcUser::setSuserHost(const QString &suserHost)
277 if (!suserHost.isEmpty() && suserHost != _suserHost) {
278 _suserHost = suserHost;
284 void IrcUser::setEncrypted(bool encrypted)
286 _encrypted = encrypted;
287 emit encryptedSet(encrypted);
292 void IrcUser::updateObjectName()
294 renameObject(QString::number(network()->networkId().toInt()) + "/" + _nick);
298 void IrcUser::updateHostmask(const QString &mask)
300 if (mask == hostmask())
303 QString user = userFromMask(mask);
304 QString host = hostFromMask(mask);
310 void IrcUser::joinChannel(IrcChannel *channel, bool skip_channel_join)
313 if (!_channels.contains(channel)) {
314 _channels.insert(channel);
315 if (!skip_channel_join)
316 channel->joinIrcUser(this);
321 void IrcUser::joinChannel(const QString &channelname)
323 joinChannel(network()->newIrcChannel(channelname));
327 void IrcUser::partChannel(IrcChannel *channel)
329 if (_channels.contains(channel)) {
330 _channels.remove(channel);
331 disconnect(channel, nullptr, this, nullptr);
333 QString channelName = channel->name();
334 SYNC_OTHER(partChannel, ARG(channelName))
335 if (_channels.isEmpty() && !network()->isMe(this))
341 void IrcUser::partChannel(const QString &channelname)
343 IrcChannel *channel = network()->ircChannel(channelname);
344 if (channel == nullptr) {
345 qWarning() << "IrcUser::partChannel(): received part for unknown Channel" << channelname;
348 partChannel(channel);
355 QList<IrcChannel *> channels = _channels.toList();
357 foreach(IrcChannel *channel, channels) {
358 disconnect(channel, nullptr, this, nullptr);
361 network()->removeIrcUser(this);
367 void IrcUser::channelDestroyed()
370 IrcChannel *channel = static_cast<IrcChannel *>(sender());
371 if (_channels.contains(channel)) {
372 _channels.remove(channel);
373 if (_channels.isEmpty() && !network()->isMe(this))
379 void IrcUser::setUserModes(const QString &modes)
381 if (_userModes != modes) {
384 emit userModesSet(modes);
389 void IrcUser::addUserModes(const QString &modes)
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];
405 emit userModesAdded(modes);
410 void IrcUser::removeUserModes(const QString &modes)
415 for (int i = 0; i < modes.count(); i++) {
416 _userModes.remove(modes[i]);
419 emit userModesRemoved(modes);
423 void IrcUser::setLastChannelActivity(BufferId buffer, const QDateTime &time)
425 _lastActivity[buffer] = time;
426 emit lastChannelActivityUpdated(buffer, time);
430 void IrcUser::setLastSpokenTo(BufferId buffer, const QDateTime &time)
432 _lastSpokenTo[buffer] = time;
433 emit lastSpokenToUpdated(buffer, time);