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