1dbb0f08a177e958df9f5538af7d6c82b425533a
[quassel.git] / src / common / ircuser.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-09 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  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, 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     _network(network),
46     _codecForEncoding(0),
47     _codecForDecoding(0)
48 {
49   updateObjectName();
50   
51   #ifdef HAVE_QCA2
52   _cipher = 0;
53   #endif
54 }
55
56 IrcUser::~IrcUser() {
57 }
58
59 // ====================
60 //  PUBLIC:
61 // ====================
62
63 QString IrcUser::hostmask() const {
64   return QString("%1!%2@%3").arg(nick()).arg(user()).arg(host());
65 }
66
67 QDateTime IrcUser::idleTime() {
68   if(QDateTime::currentDateTime().toTime_t() - _idleTimeSet.toTime_t() > 1200)
69     _idleTime = QDateTime();
70   return _idleTime;
71 }
72
73 QStringList IrcUser::channels() const {
74   QStringList chanList;
75   IrcChannel *channel;
76   foreach(channel, _channels) {
77     chanList << channel->name();
78   }
79   return chanList;
80 }
81
82
83 void IrcUser::setCodecForEncoding(const QString &name) {
84   setCodecForEncoding(QTextCodec::codecForName(name.toAscii()));
85 }
86
87 void IrcUser::setCodecForEncoding(QTextCodec *codec) {
88   _codecForEncoding = codec;
89 }
90
91 void IrcUser::setCodecForDecoding(const QString &name) {
92   setCodecForDecoding(QTextCodec::codecForName(name.toAscii()));
93 }
94
95 void IrcUser::setCodecForDecoding(QTextCodec *codec) {
96   _codecForDecoding = codec;
97 }
98
99 QString IrcUser::decodeString(const QByteArray &text) const {
100   if(!codecForDecoding()) return network()->decodeString(text);
101   return ::decodeString(text, codecForDecoding());
102 }
103
104 QByteArray IrcUser::encodeString(const QString &string) const {
105   if(codecForEncoding()) {
106     return codecForEncoding()->fromUnicode(string);
107   }
108   return network()->encodeString(string);
109 }
110
111 // ====================
112 //  PUBLIC SLOTS:
113 // ====================
114 void IrcUser::setUser(const QString &user) {
115   if(!user.isEmpty() && _user != user) {
116     _user = user;
117     SYNC(ARG(user));
118   }
119 }
120
121 void IrcUser::setRealName(const QString &realName) {
122   if (!realName.isEmpty() && _realName != realName) {
123     _realName = realName;
124     SYNC(ARG(realName))
125   }
126 }
127
128 void IrcUser::setAway(const bool &away) {
129   if(away != _away) {
130     _away = away;
131     SYNC(ARG(away))
132     emit awaySet(away);
133   }
134 }
135
136 void IrcUser::setAwayMessage(const QString &awayMessage) {
137   if(!awayMessage.isEmpty() && _awayMessage != awayMessage) {
138     _awayMessage = awayMessage;
139     SYNC(ARG(awayMessage))
140   }
141 }
142
143 void IrcUser::setIdleTime(const QDateTime &idleTime) {
144   if(idleTime.isValid() && _idleTime != idleTime) {
145     _idleTime = idleTime;
146     _idleTimeSet = QDateTime::currentDateTime();
147     SYNC(ARG(idleTime))
148   }
149 }
150
151 void IrcUser::setLoginTime(const QDateTime &loginTime) {
152   if(loginTime.isValid() && _loginTime != loginTime) {
153     _loginTime = loginTime;
154     SYNC(ARG(loginTime))
155   }
156 }
157
158 void IrcUser::setServer(const QString &server) {
159   if(!server.isEmpty() && _server != server) {
160     _server = server;
161     SYNC(ARG(server))
162   }
163 }
164
165 void IrcUser::setIrcOperator(const QString &ircOperator) {
166   if(!ircOperator.isEmpty() && _ircOperator != ircOperator) {
167     _ircOperator = ircOperator;
168     SYNC(ARG(ircOperator))
169   }
170 }
171
172 void IrcUser::setLastAwayMessage(const int &lastAwayMessage) {
173   if(lastAwayMessage > _lastAwayMessage) {
174     _lastAwayMessage = lastAwayMessage;
175     SYNC(ARG(lastAwayMessage))
176   }
177 }
178
179 void IrcUser::setHost(const QString &host) {
180   if(!host.isEmpty() && _host != host) {
181     _host = host;
182     SYNC(ARG(host))
183   }
184 }
185
186 void IrcUser::setNick(const QString &nick) {
187   if(!nick.isEmpty() && nick != _nick) {
188     _nick = nick;
189     updateObjectName();
190     SYNC(ARG(nick))
191     emit nickSet(nick);
192   }
193 }
194
195 void IrcUser::setWhoisServiceReply(const QString &whoisServiceReply) {
196   if(!whoisServiceReply.isEmpty() && whoisServiceReply != _whoisServiceReply) {
197     _whoisServiceReply = whoisServiceReply;
198     SYNC(ARG(whoisServiceReply))
199   }
200 }
201
202 void IrcUser::setSuserHost(const QString &suserHost) {
203   if(!suserHost.isEmpty() && suserHost != _suserHost) {
204     _suserHost = suserHost;
205     SYNC(ARG(suserHost))
206   }
207 }
208
209 void IrcUser::updateObjectName() {
210   renameObject(QString::number(network()->networkId().toInt()) + "/" + _nick);
211 }
212
213 void IrcUser::updateHostmask(const QString &mask) {
214   if(mask == hostmask())
215     return;
216
217   QString user = userFromMask(mask);
218   QString host = hostFromMask(mask);
219   setUser(user);
220   setHost(host);
221 }
222
223 void IrcUser::joinChannel(IrcChannel *channel) {
224   Q_ASSERT(channel);
225   if(!_channels.contains(channel)) {
226     _channels.insert(channel);
227     channel->joinIrcUser(this);
228   }
229 }
230
231 void IrcUser::joinChannel(const QString &channelname) {
232   joinChannel(network()->newIrcChannel(channelname));
233 }
234
235 void IrcUser::partChannel(IrcChannel *channel) {
236   if(_channels.contains(channel)) {
237     _channels.remove(channel);
238     disconnect(channel, 0, this, 0);
239     channel->part(this);
240     QString channelName = channel->name();
241     SYNC_OTHER(partChannel, ARG(channelName))
242     if(_channels.isEmpty() && !network()->isMe(this))
243       quit();
244   }
245 }
246
247 void IrcUser::partChannel(const QString &channelname) {
248   IrcChannel *channel = network()->ircChannel(channelname);
249   if(channel == 0) {
250     qWarning() << "IrcUser::partChannel(): received part for unknown Channel" << channelname;
251   } else {
252     partChannel(channel);
253   }
254 }
255
256 void IrcUser::quit() {
257   QList<IrcChannel *> channels = _channels.toList();
258   _channels.clear();
259   foreach(IrcChannel *channel, channels) {
260     disconnect(channel, 0, this, 0);
261     channel->part(this);
262   }
263   network()->removeIrcUser(this);
264   SYNC(NO_ARG)
265   emit quited();
266 }
267
268 void IrcUser::channelDestroyed() {
269   // private slot!
270   IrcChannel *channel = static_cast<IrcChannel*>(sender());
271   if(_channels.contains(channel)) {
272     _channels.remove(channel);
273     if(_channels.isEmpty() && !network()->isMe(this))
274       quit();
275   }
276 }
277
278 void IrcUser::setUserModes(const QString &modes) {
279   _userModes = modes;
280   SYNC(ARG(modes))
281   emit userModesSet(modes);
282 }
283
284 void IrcUser::addUserModes(const QString &modes) {
285   if(modes.isEmpty())
286     return;
287
288   for(int i = 0; i < modes.count(); i++) {
289     if(!_userModes.contains(modes[i]))
290       _userModes += modes[i];
291   }
292
293   SYNC(ARG(modes))
294   emit userModesAdded(modes);
295 }
296
297 void IrcUser::removeUserModes(const QString &modes) {
298   if(modes.isEmpty())
299     return;
300
301   for(int i = 0; i < modes.count(); i++) {
302     _userModes.remove(modes[i]);
303   }
304   SYNC(ARG(modes))
305   emit userModesRemoved(modes);
306 }
307
308 void IrcUser::setLastChannelActivity(BufferId buffer, const QDateTime &time) {
309   _lastActivity[buffer] = time;
310   emit lastChannelActivityUpdated(buffer, time);
311 }
312
313 void IrcUser::setLastSpokenTo(BufferId buffer, const QDateTime &time) {
314   _lastSpokenTo[buffer] = time;
315   emit lastSpokenToUpdated(buffer, time);
316 }
317
318 #ifdef HAVE_QCA2
319 Cipher* IrcUser::cipher() {
320   if(!_cipher)
321     _cipher = new Cipher();
322   return _cipher;
323 }
324 #endif
325
326 void IrcUser::setEncrypted(bool e) {
327   Q_UNUSED(e);
328   // TODO
329 }