Fixing BR #315: Nicks missing from nick lists
[quassel.git] / src / common / ircuser.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-08 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 IrcUser::IrcUser(const QString &hostmask, Network *network) : SyncableObject(network),
32     _initialized(false),
33     _nick(nickFromMask(hostmask)),
34     _user(userFromMask(hostmask)),
35     _host(hostFromMask(hostmask)),
36     _realName(),
37     _awayMessage(),
38     _away(false),
39     _server(),
40     // _idleTime(QDateTime::currentDateTime()),
41     _ircOperator(),
42     _lastAwayMessage(0),
43     _whoisServiceReply(),
44     _network(network),
45     _codecForEncoding(0),
46     _codecForDecoding(0)
47 {
48   updateObjectName();
49 }
50
51 IrcUser::~IrcUser() {
52 }
53
54 // ====================
55 //  PUBLIC:
56 // ====================
57
58 QString IrcUser::hostmask() const {
59   return QString("%1!%2@%3").arg(nick()).arg(user()).arg(host());
60 }
61
62 QDateTime IrcUser::idleTime() {
63   if(QDateTime::currentDateTime().toTime_t() - _idleTimeSet.toTime_t() > 1200)
64     _idleTime = QDateTime();
65   return _idleTime;
66 }
67
68 QStringList IrcUser::channels() const {
69   QStringList chanList;
70   IrcChannel *channel;
71   foreach(channel, _channels) {
72     chanList << channel->name();
73   }
74   return chanList;
75 }
76
77
78 void IrcUser::setCodecForEncoding(const QString &name) {
79   setCodecForEncoding(QTextCodec::codecForName(name.toAscii()));
80 }
81
82 void IrcUser::setCodecForEncoding(QTextCodec *codec) {
83   _codecForEncoding = codec;
84 }
85
86 void IrcUser::setCodecForDecoding(const QString &name) {
87   setCodecForDecoding(QTextCodec::codecForName(name.toAscii()));
88 }
89
90 void IrcUser::setCodecForDecoding(QTextCodec *codec) {
91   _codecForDecoding = codec;
92 }
93
94 QString IrcUser::decodeString(const QByteArray &text) const {
95   if(!codecForDecoding()) return network()->decodeString(text);
96   return ::decodeString(text, codecForDecoding());
97 }
98
99 QByteArray IrcUser::encodeString(const QString &string) const {
100   if(codecForEncoding()) {
101     return codecForEncoding()->fromUnicode(string);
102   }
103   return network()->encodeString(string);
104 }
105
106 // ====================
107 //  PUBLIC SLOTS:
108 // ====================
109 void IrcUser::setUser(const QString &user) {
110   if(!user.isEmpty() && _user != user) {
111     _user = user;
112     emit userSet(user);
113   }
114 }
115
116 void IrcUser::setRealName(const QString &realName) {
117   if (!realName.isEmpty() && _realName != realName) {
118     _realName = realName;
119     emit realNameSet(realName);
120   }
121 }
122
123 void IrcUser::setAway(const bool &away) {
124   if(away != _away) {
125     _away = away;
126     emit awaySet(away);
127   }
128 }
129
130 void IrcUser::setAwayMessage(const QString &awayMessage) {
131   if(!awayMessage.isEmpty() && _awayMessage != awayMessage) {
132     _awayMessage = awayMessage;
133     emit awayMessageSet(awayMessage);
134   }
135 }
136
137 void IrcUser::setIdleTime(const QDateTime &idleTime) {
138   if(idleTime.isValid() && _idleTime != idleTime) {
139     _idleTime = idleTime;
140     _idleTimeSet = QDateTime::currentDateTime();
141     emit idleTimeSet(idleTime);
142   }
143 }
144
145 void IrcUser::setLoginTime(const QDateTime &loginTime) {
146   if(loginTime.isValid() && _loginTime != loginTime) {
147     _loginTime = loginTime;
148     emit loginTimeSet(loginTime);
149   }
150 }
151
152 void IrcUser::setServer(const QString &server) {
153   if(!server.isEmpty() && _server != server) {
154     _server = server;
155     emit serverSet(server);
156   }
157 }
158
159 void IrcUser::setIrcOperator(const QString &ircOperator) {
160   if(!ircOperator.isEmpty() && _ircOperator != ircOperator) {
161     _ircOperator = ircOperator;
162     emit ircOperatorSet(ircOperator);
163   }
164 }
165
166 void IrcUser::setLastAwayMessage(const int &lastAwayMessage) {
167   if(lastAwayMessage > _lastAwayMessage) {
168     _lastAwayMessage = lastAwayMessage;
169     emit lastAwayMessageSet(lastAwayMessage);
170   }
171 }
172
173 void IrcUser::setHost(const QString &host) {
174   if(!host.isEmpty() && _host != host) {
175     _host = host;
176     emit hostSet(host);
177   }
178 }
179
180 void IrcUser::setNick(const QString &nick) {
181   if(!nick.isEmpty() && nick != _nick) {
182     _nick = nick;
183     updateObjectName();
184     emit nickSet(nick);
185   }
186 }
187
188 void IrcUser::setWhoisServiceReply(const QString &whoisServiceReply) {
189   if(!whoisServiceReply.isEmpty() && whoisServiceReply != _whoisServiceReply) {
190     _whoisServiceReply = whoisServiceReply;
191     emit whoisServiceReplySet(whoisServiceReply);
192   }
193 }
194
195 void IrcUser::setSuserHost(const QString &suserHost) {
196   if(!suserHost.isEmpty() && suserHost != _suserHost) {
197     _suserHost = suserHost;
198     emit suserHostSet(suserHost);
199   }
200 }
201
202 void IrcUser::updateObjectName() {
203   renameObject(QString::number(network()->networkId().toInt()) + "/" + _nick);
204 }
205
206 void IrcUser::updateHostmask(const QString &mask) {
207   if(mask == hostmask())
208     return;
209
210   QString user = userFromMask(mask);
211   QString host = hostFromMask(mask);
212   setUser(user);
213   setHost(host);
214 }
215
216 void IrcUser::joinChannel(IrcChannel *channel) {
217   Q_ASSERT(channel);
218   if(!_channels.contains(channel)) {
219     _channels.insert(channel);
220     channel->joinIrcUsers(this);
221     // connect(channel, SIGNAL(destroyed()), this, SLOT(channelDestroyed()));
222   }
223 }
224
225 void IrcUser::joinChannel(const QString &channelname) {
226   joinChannel(network()->newIrcChannel(channelname));
227 }
228
229 void IrcUser::partChannel(IrcChannel *channel) {
230   if(_channels.contains(channel)) {
231     _channels.remove(channel);
232     disconnect(channel, 0, this, 0);
233     channel->part(this);
234     emit channelParted(channel->name());
235     if(_channels.isEmpty() && !network()->isMe(this))
236       quit();
237   }
238 }
239
240 void IrcUser::partChannel(const QString &channelname) {
241   IrcChannel *channel = network()->ircChannel(channelname);
242   if(channel == 0) {
243     qWarning() << "IrcUser::partChannel(): received part for unknown Channel" << channelname;
244   } else {
245     partChannel(channel);
246   }
247 }
248
249 void IrcUser::quit() {
250   QList<IrcChannel *> channels = _channels.toList();
251   _channels.clear();
252   foreach(IrcChannel *channel, channels) {
253     disconnect(channel, 0, this, 0);
254     channel->part(this);
255   }
256   network()->removeIrcUser(this);
257   emit quited();
258 }
259
260 void IrcUser::channelDestroyed() {
261   // private slot!
262   IrcChannel *channel = static_cast<IrcChannel*>(sender());
263   if(_channels.contains(channel)) {
264     _channels.remove(channel);
265     if(_channels.isEmpty() && !network()->isMe(this))
266       quit();
267   }
268 }
269
270 void IrcUser::setUserModes(const QString &modes) {
271   _userModes = modes;
272   emit userModesSet(modes);
273 }
274
275 void IrcUser::addUserModes(const QString &modes) {
276   if(modes.isEmpty())
277     return;
278
279   for(int i = 0; i < modes.count(); i++) {
280     if(!_userModes.contains(modes[i]))
281       _userModes += modes[i];
282   }
283
284   emit userModesAdded(modes);
285 }
286
287 void IrcUser::removeUserModes(const QString &modes) {
288   if(modes.isEmpty())
289     return;
290
291   for(int i = 0; i < modes.count(); i++) {
292     _userModes.remove(modes[i]);
293   }
294   emit userModesRemoved(modes);
295 }