3cde7e79b9e0dda4475422b5773a7e88eb0fb065
[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(),
44     _whoisServiceReply(),
45     _encrypted(false),
46     _network(network),
47     _codecForEncoding(0),
48     _codecForDecoding(0)
49 {
50     updateObjectName();
51     _lastAwayMessage.setTimeSpec(Qt::UTC);
52     _lastAwayMessage.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         SYNC(ARG(away))
169         emit awaySet(away);
170     }
171 }
172
173
174 void IrcUser::setAwayMessage(const QString &awayMessage)
175 {
176     if (!awayMessage.isEmpty() && _awayMessage != awayMessage) {
177         _awayMessage = awayMessage;
178         SYNC(ARG(awayMessage))
179     }
180 }
181
182
183 void IrcUser::setIdleTime(const QDateTime &idleTime)
184 {
185     if (idleTime.isValid() && _idleTime != idleTime) {
186         _idleTime = idleTime;
187         _idleTimeSet = QDateTime::currentDateTime();
188         SYNC(ARG(idleTime))
189     }
190 }
191
192
193 void IrcUser::setLoginTime(const QDateTime &loginTime)
194 {
195     if (loginTime.isValid() && _loginTime != loginTime) {
196         _loginTime = loginTime;
197         SYNC(ARG(loginTime))
198     }
199 }
200
201
202 void IrcUser::setServer(const QString &server)
203 {
204     if (!server.isEmpty() && _server != server) {
205         _server = server;
206         SYNC(ARG(server))
207     }
208 }
209
210
211 void IrcUser::setIrcOperator(const QString &ircOperator)
212 {
213     if (!ircOperator.isEmpty() && _ircOperator != ircOperator) {
214         _ircOperator = ircOperator;
215         SYNC(ARG(ircOperator))
216     }
217 }
218
219
220 void IrcUser::setLastAwayMessage(const QDateTime &lastAwayMessage)
221 {
222     if (lastAwayMessage > _lastAwayMessage) {
223         _lastAwayMessage = lastAwayMessage;
224         SYNC(ARG(lastAwayMessage))
225     }
226 }
227
228
229 void IrcUser::setHost(const QString &host)
230 {
231     if (!host.isEmpty() && _host != host) {
232         _host = host;
233         SYNC(ARG(host))
234     }
235 }
236
237
238 void IrcUser::setNick(const QString &nick)
239 {
240     if (!nick.isEmpty() && nick != _nick) {
241         _nick = nick;
242         updateObjectName();
243         SYNC(ARG(nick))
244         emit nickSet(nick);
245     }
246 }
247
248
249 void IrcUser::setWhoisServiceReply(const QString &whoisServiceReply)
250 {
251     if (!whoisServiceReply.isEmpty() && whoisServiceReply != _whoisServiceReply) {
252         _whoisServiceReply = whoisServiceReply;
253         SYNC(ARG(whoisServiceReply))
254     }
255 }
256
257
258 void IrcUser::setSuserHost(const QString &suserHost)
259 {
260     if (!suserHost.isEmpty() && suserHost != _suserHost) {
261         _suserHost = suserHost;
262         SYNC(ARG(suserHost))
263     }
264 }
265
266
267 void IrcUser::setEncrypted(bool encrypted)
268 {
269     _encrypted = encrypted;
270     emit encryptedSet(encrypted);
271     SYNC(ARG(encrypted))
272 }
273
274
275 void IrcUser::updateObjectName()
276 {
277     renameObject(QString::number(network()->networkId().toInt()) + "/" + _nick);
278 }
279
280
281 void IrcUser::updateHostmask(const QString &mask)
282 {
283     if (mask == hostmask())
284         return;
285
286     QString user = userFromMask(mask);
287     QString host = hostFromMask(mask);
288     setUser(user);
289     setHost(host);
290 }
291
292
293 void IrcUser::joinChannel(IrcChannel *channel, bool skip_channel_join)
294 {
295     Q_ASSERT(channel);
296     if (!_channels.contains(channel)) {
297         _channels.insert(channel);
298         if (!skip_channel_join)
299             channel->joinIrcUser(this);
300     }
301 }
302
303
304 void IrcUser::joinChannel(const QString &channelname)
305 {
306     joinChannel(network()->newIrcChannel(channelname));
307 }
308
309
310 void IrcUser::partChannel(IrcChannel *channel)
311 {
312     if (_channels.contains(channel)) {
313         _channels.remove(channel);
314         disconnect(channel, 0, this, 0);
315         channel->part(this);
316         QString channelName = channel->name();
317         SYNC_OTHER(partChannel, ARG(channelName))
318         if (_channels.isEmpty() && !network()->isMe(this))
319             quit();
320     }
321 }
322
323
324 void IrcUser::partChannel(const QString &channelname)
325 {
326     IrcChannel *channel = network()->ircChannel(channelname);
327     if (channel == 0) {
328         qWarning() << "IrcUser::partChannel(): received part for unknown Channel" << channelname;
329     }
330     else {
331         partChannel(channel);
332     }
333 }
334
335
336 void IrcUser::quit()
337 {
338     QList<IrcChannel *> channels = _channels.toList();
339     _channels.clear();
340     foreach(IrcChannel *channel, channels) {
341         disconnect(channel, 0, this, 0);
342         channel->part(this);
343     }
344     network()->removeIrcUser(this);
345     SYNC(NO_ARG)
346     emit quited();
347 }
348
349
350 void IrcUser::channelDestroyed()
351 {
352     // private slot!
353     IrcChannel *channel = static_cast<IrcChannel *>(sender());
354     if (_channels.contains(channel)) {
355         _channels.remove(channel);
356         if (_channels.isEmpty() && !network()->isMe(this))
357             quit();
358     }
359 }
360
361
362 void IrcUser::setUserModes(const QString &modes)
363 {
364     if (_userModes != modes) {
365         _userModes = modes;
366         SYNC(ARG(modes))
367         emit userModesSet(modes);
368     }
369 }
370
371
372 void IrcUser::addUserModes(const QString &modes)
373 {
374     if (modes.isEmpty())
375         return;
376
377     // Don't needlessly sync when no changes are made
378     bool changesMade = false;
379     for (int i = 0; i < modes.count(); i++) {
380         if (!_userModes.contains(modes[i])) {
381             _userModes += modes[i];
382             changesMade = true;
383         }
384     }
385
386     if (changesMade) {
387         SYNC(ARG(modes))
388         emit userModesAdded(modes);
389     }
390 }
391
392
393 void IrcUser::removeUserModes(const QString &modes)
394 {
395     if (modes.isEmpty())
396         return;
397
398     for (int i = 0; i < modes.count(); i++) {
399         _userModes.remove(modes[i]);
400     }
401     SYNC(ARG(modes))
402     emit userModesRemoved(modes);
403 }
404
405
406 void IrcUser::setLastChannelActivity(BufferId buffer, const QDateTime &time)
407 {
408     _lastActivity[buffer] = time;
409     emit lastChannelActivityUpdated(buffer, time);
410 }
411
412
413 void IrcUser::setLastSpokenTo(BufferId buffer, const QDateTime &time)
414 {
415     _lastSpokenTo[buffer] = time;
416     emit lastSpokenToUpdated(buffer, time);
417 }