1 /***************************************************************************
2 * Copyright (C) 2005-2019 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 ***************************************************************************/
26 #include "ircchannel.h"
28 #include "signalproxy.h"
31 IrcUser::IrcUser(const QString& hostmask, Network* network)
32 : SyncableObject(network)
34 , _nick(nickFromMask(hostmask))
35 , _user(userFromMask(hostmask))
36 , _host(hostFromMask(hostmask))
42 // _idleTime(QDateTime::currentDateTime()),
44 , _lastAwayMessageTime()
45 , _whoisServiceReply()
48 , _codecForEncoding(nullptr)
49 , _codecForDecoding(nullptr)
52 _lastAwayMessageTime.setTimeSpec(Qt::UTC);
53 _lastAwayMessageTime.setMSecsSinceEpoch(0);
56 // ====================
58 // ====================
60 QString IrcUser::hostmask() const
62 return QString("%1!%2@%3").arg(nick()).arg(user()).arg(host());
65 QDateTime IrcUser::idleTime()
67 if ((QDateTime::currentDateTime().toMSecsSinceEpoch() - _idleTimeSet.toMSecsSinceEpoch()) > 1200000) {
68 // 20 * 60 * 1000 = 1200000
69 // 20 minutes have elapsed, clear the known idle time as it's likely inaccurate by now
70 _idleTime = QDateTime();
75 QStringList IrcUser::channels() const
79 foreach (channel, _channels) {
80 chanList << channel->name();
85 void IrcUser::setCodecForEncoding(const QString& name)
87 setCodecForEncoding(QTextCodec::codecForName(name.toLatin1()));
90 void IrcUser::setCodecForEncoding(QTextCodec* codec)
92 _codecForEncoding = codec;
95 void IrcUser::setCodecForDecoding(const QString& name)
97 setCodecForDecoding(QTextCodec::codecForName(name.toLatin1()));
100 void IrcUser::setCodecForDecoding(QTextCodec* codec)
102 _codecForDecoding = codec;
105 QString IrcUser::decodeString(const QByteArray& text) const
107 if (!codecForDecoding())
108 return network()->decodeString(text);
109 return ::decodeString(text, codecForDecoding());
112 QByteArray IrcUser::encodeString(const QString& string) const
114 if (codecForEncoding()) {
115 return codecForEncoding()->fromUnicode(string);
117 return network()->encodeString(string);
120 // ====================
122 // ====================
123 void IrcUser::setUser(const QString& user)
125 if (!user.isEmpty() && _user != user) {
131 void IrcUser::setRealName(const QString& realName)
133 if (!realName.isEmpty() && _realName != realName) {
134 _realName = realName;
139 void IrcUser::setAccount(const QString& account)
141 if (_account != account) {
147 void IrcUser::setAway(bool away)
157 void IrcUser::setAwayMessage(const QString& awayMessage)
159 if (!awayMessage.isEmpty() && _awayMessage != awayMessage) {
160 _awayMessage = awayMessage;
162 SYNC(ARG(awayMessage))
166 void IrcUser::setIdleTime(const QDateTime& idleTime)
168 if (idleTime.isValid() && _idleTime != idleTime) {
169 _idleTime = idleTime;
170 _idleTimeSet = QDateTime::currentDateTime();
175 void IrcUser::setLoginTime(const QDateTime& loginTime)
177 if (loginTime.isValid() && _loginTime != loginTime) {
178 _loginTime = loginTime;
183 void IrcUser::setServer(const QString& server)
185 if (!server.isEmpty() && _server != server) {
191 void IrcUser::setIrcOperator(const QString& ircOperator)
193 if (!ircOperator.isEmpty() && _ircOperator != ircOperator) {
194 _ircOperator = ircOperator;
195 SYNC(ARG(ircOperator))
199 // This function is only ever called by SYNC calls from legacy cores (pre-0.13).
200 // Therefore, no SYNC call is needed here.
201 void IrcUser::setLastAwayMessage(int lastAwayMessage)
203 #if QT_VERSION >= 0x050800
204 QDateTime lastAwayMessageTime = QDateTime::fromSecsSinceEpoch(lastAwayMessage);
206 // toSecsSinceEpoch() was added in Qt 5.8. Manually downconvert to seconds for now.
207 // See https://doc.qt.io/qt-5/qdatetime.html#toMSecsSinceEpoch
208 QDateTime lastAwayMessageTime = QDateTime::fromMSecsSinceEpoch(lastAwayMessage * 1000);
210 lastAwayMessageTime.setTimeSpec(Qt::UTC);
211 setLastAwayMessageTime(lastAwayMessageTime);
214 void IrcUser::setLastAwayMessageTime(const QDateTime& lastAwayMessageTime)
216 if (lastAwayMessageTime > _lastAwayMessageTime) {
217 _lastAwayMessageTime = lastAwayMessageTime;
218 SYNC(ARG(lastAwayMessageTime))
222 void IrcUser::setHost(const QString& host)
224 if (!host.isEmpty() && _host != host) {
230 void IrcUser::setNick(const QString& nick)
232 if (!nick.isEmpty() && nick != _nick) {
240 void IrcUser::setWhoisServiceReply(const QString& whoisServiceReply)
242 if (!whoisServiceReply.isEmpty() && whoisServiceReply != _whoisServiceReply) {
243 _whoisServiceReply = whoisServiceReply;
244 SYNC(ARG(whoisServiceReply))
248 void IrcUser::setSuserHost(const QString& suserHost)
250 if (!suserHost.isEmpty() && suserHost != _suserHost) {
251 _suserHost = suserHost;
256 void IrcUser::setEncrypted(bool encrypted)
258 _encrypted = encrypted;
259 emit encryptedSet(encrypted);
263 void IrcUser::updateObjectName()
265 setObjectName(QString::number(network()->networkId().toInt()) + "/" + _nick);
268 void IrcUser::updateHostmask(const QString& mask)
270 if (mask == hostmask())
273 QString user = userFromMask(mask);
274 QString host = hostFromMask(mask);
279 void IrcUser::joinChannel(IrcChannel* channel, bool skip_channel_join)
282 if (!_channels.contains(channel)) {
283 _channels.insert(channel);
284 if (!skip_channel_join)
285 channel->joinIrcUser(this);
289 void IrcUser::joinChannel(const QString& channelname)
291 joinChannel(network()->newIrcChannel(channelname));
294 void IrcUser::partChannel(IrcChannel* channel)
296 if (_channels.contains(channel)) {
297 _channels.remove(channel);
298 disconnect(channel, nullptr, this, nullptr);
300 QString channelName = channel->name();
301 SYNC_OTHER(partChannel, ARG(channelName))
302 if (_channels.isEmpty() && !network()->isMe(this))
307 void IrcUser::partChannel(const QString& channelname)
309 IrcChannel* channel = network()->ircChannel(channelname);
310 if (channel == nullptr) {
311 qWarning() << "IrcUser::partChannel(): received part for unknown Channel" << channelname;
314 partChannel(channel);
320 QList<IrcChannel*> channels = _channels.toList();
322 foreach (IrcChannel* channel, channels) {
323 disconnect(channel, nullptr, this, nullptr);
326 network()->removeIrcUser(this);
331 void IrcUser::channelDestroyed()
334 auto* channel = static_cast<IrcChannel*>(sender());
335 if (_channels.contains(channel)) {
336 _channels.remove(channel);
337 if (_channels.isEmpty() && !network()->isMe(this))
342 void IrcUser::setUserModes(const QString& modes)
344 if (_userModes != modes) {
347 emit userModesSet(modes);
351 void IrcUser::addUserModes(const QString& modes)
356 // Don't needlessly sync when no changes are made
357 bool changesMade = false;
358 for (int i = 0; i < modes.count(); i++) {
359 if (!_userModes.contains(modes[i])) {
360 _userModes += modes[i];
367 emit userModesAdded(modes);
371 void IrcUser::removeUserModes(const QString& modes)
376 for (int i = 0; i < modes.count(); i++) {
377 _userModes.remove(modes[i]);
380 emit userModesRemoved(modes);
383 void IrcUser::setLastChannelActivity(BufferId buffer, const QDateTime& time)
385 _lastActivity[buffer] = time;
386 emit lastChannelActivityUpdated(buffer, time);
389 void IrcUser::setLastSpokenTo(BufferId buffer, const QDateTime& time)
391 _lastSpokenTo[buffer] = time;
392 emit lastSpokenToUpdated(buffer, time);