1 /***************************************************************************
2 * Copyright (C) 2005-08 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 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
28 QTextCodec *Network::_defaultCodecForServer = 0;
29 QTextCodec *Network::_defaultCodecForEncoding = 0;
30 QTextCodec *Network::_defaultCodecForDecoding = 0;
31 // ====================
33 // ====================
34 Network::Network(const NetworkId &networkid, QObject *parent)
35 : SyncableObject(parent),
37 _networkId(networkid),
40 _networkName(QString("<not initialized>")),
41 _currentServer(QString()),
43 _connectionState(Disconnected),
45 _prefixModes(QString()),
46 _useRandomServer(false),
47 _useAutoIdentify(false),
48 _useAutoReconnect(false),
49 _autoReconnectInterval(60),
50 _autoReconnectRetries(10),
51 _unlimitedReconnectRetries(false),
56 setObjectName(QString::number(networkid.toInt()));
60 emit aboutToBeDestroyed();
63 bool Network::isChannelName(const QString &channelname) const {
64 if(channelname.isEmpty())
67 if(supports("CHANTYPES"))
68 return support("CHANTYPES").contains(channelname[0]);
70 return QString("#&!+").contains(channelname[0]);
73 NetworkInfo Network::networkInfo() const {
75 info.networkName = networkName();
76 info.networkId = networkId();
77 info.identity = identity();
78 info.codecForServer = codecForServer();
79 info.codecForEncoding = codecForEncoding();
80 info.codecForDecoding = codecForDecoding();
81 info.serverList = serverList();
82 info.useRandomServer = useRandomServer();
83 info.perform = perform();
84 info.useAutoIdentify = useAutoIdentify();
85 info.autoIdentifyService = autoIdentifyService();
86 info.autoIdentifyPassword = autoIdentifyPassword();
87 info.useAutoReconnect = useAutoReconnect();
88 info.autoReconnectInterval = autoReconnectInterval();
89 info.autoReconnectRetries = autoReconnectRetries();
90 info.unlimitedReconnectRetries = unlimitedReconnectRetries();
91 info.rejoinChannels = rejoinChannels();
95 void Network::setNetworkInfo(const NetworkInfo &info) {
96 // we don't set our ID!
97 if(!info.networkName.isEmpty() && info.networkName != networkName()) setNetworkName(info.networkName);
98 if(info.identity > 0 && info.identity != identity()) setIdentity(info.identity);
99 if(info.codecForServer != codecForServer()) setCodecForServer(QTextCodec::codecForName(info.codecForServer));
100 if(info.codecForEncoding != codecForEncoding()) setCodecForEncoding(QTextCodec::codecForName(info.codecForEncoding));
101 if(info.codecForDecoding != codecForDecoding()) setCodecForDecoding(QTextCodec::codecForName(info.codecForDecoding));
102 if(info.serverList.count()) setServerList(info.serverList); // FIXME compare components
103 if(info.useRandomServer != useRandomServer()) setUseRandomServer(info.useRandomServer);
104 if(info.perform != perform()) setPerform(info.perform);
105 if(info.useAutoIdentify != useAutoIdentify()) setUseAutoIdentify(info.useAutoIdentify);
106 if(info.autoIdentifyService != autoIdentifyService()) setAutoIdentifyService(info.autoIdentifyService);
107 if(info.autoIdentifyPassword != autoIdentifyPassword()) setAutoIdentifyPassword(info.autoIdentifyPassword);
108 if(info.useAutoReconnect != useAutoReconnect()) setUseAutoReconnect(info.useAutoReconnect);
109 if(info.autoReconnectInterval != autoReconnectInterval()) setAutoReconnectInterval(info.autoReconnectInterval);
110 if(info.autoReconnectRetries != autoReconnectRetries()) setAutoReconnectRetries(info.autoReconnectRetries);
111 if(info.unlimitedReconnectRetries != unlimitedReconnectRetries()) setUnlimitedReconnectRetries(info.unlimitedReconnectRetries);
112 if(info.rejoinChannels != rejoinChannels()) setRejoinChannels(info.rejoinChannels);
115 QString Network::prefixToMode(const QString &prefix) {
116 if(prefixes().contains(prefix))
117 return QString(prefixModes()[prefixes().indexOf(prefix)]);
122 QString Network::modeToPrefix(const QString &mode) {
123 if(prefixModes().contains(mode))
124 return QString(prefixes()[prefixModes().indexOf(mode)]);
129 QStringList Network::nicks() const {
130 // we don't use _ircUsers.keys() since the keys may be
131 // not up to date after a nick change
133 foreach(IrcUser *ircuser, _ircUsers.values()) {
134 nicks << ircuser->nick();
139 QString Network::prefixes() {
140 if(_prefixes.isNull())
146 QString Network::prefixModes() {
147 if(_prefixModes.isNull())
153 // example Unreal IRCD: CHANMODES=beI,kfL,lj,psmntirRcOAQKVCuzNSMTG
154 Network::ChannelModeType Network::channelModeType(const QString &mode) {
156 return NOT_A_CHANMODE;
158 QString chanmodes = support("CHANMODES");
159 if(chanmodes.isEmpty())
160 return NOT_A_CHANMODE;
162 ChannelModeType modeType = A_CHANMODE;
163 for(int i = 0; i < chanmodes.count(); i++) {
164 if(chanmodes[i] == mode[0])
166 else if(chanmodes[i] == ',')
167 modeType = (ChannelModeType)(modeType << 1);
169 if(modeType > D_CHANMODE) {
170 qWarning() << "Network" << networkId() << "supplied invalid CHANMODES:" << chanmodes;
171 modeType = NOT_A_CHANMODE;
176 QString Network::support(const QString ¶m) const {
177 QString support_ = param.toUpper();
178 if(_supports.contains(support_))
179 return _supports[support_];
184 IrcUser *Network::newIrcUser(const QString &hostmask) {
185 QString nick(nickFromMask(hostmask).toLower());
186 if(!_ircUsers.contains(nick)) {
187 IrcUser *ircuser = new IrcUser(hostmask, this);
190 proxy()->synchronize(ircuser);
192 qWarning() << "unable to synchronize new IrcUser" << hostmask << "forgot to call Network::setProxy(SignalProxy *)?";
194 connect(ircuser, SIGNAL(nickSet(QString)), this, SLOT(ircUserNickChanged(QString)));
195 connect(ircuser, SIGNAL(initDone()), this, SLOT(ircUserInitDone()));
196 connect(ircuser, SIGNAL(destroyed()), this, SLOT(ircUserDestroyed()));
197 _ircUsers[nick] = ircuser;
198 emit ircUserAdded(hostmask);
199 emit ircUserAdded(ircuser);
201 return _ircUsers[nick];
204 void Network::ircUserDestroyed() {
205 IrcUser *ircUser = static_cast<IrcUser *>(sender());
209 QHash<QString, IrcUser *>::iterator ircUserIter = _ircUsers.begin();
210 while(ircUserIter != _ircUsers.end()) {
211 if(ircUser == *ircUserIter) {
212 ircUserIter = _ircUsers.erase(ircUserIter);
219 void Network::removeIrcUser(IrcUser *ircuser) {
220 QString nick = _ircUsers.key(ircuser);
224 _ircUsers.remove(nick);
225 disconnect(ircuser, 0, this, 0);
226 emit ircUserRemoved(nick);
227 emit ircUserRemoved(ircuser);
228 ircuser->deleteLater();
231 void Network::removeIrcUser(const QString &nick) {
233 if((ircuser = ircUser(nick)) != 0)
234 removeIrcUser(ircuser);
237 void Network::removeChansAndUsers() {
238 QList<IrcUser *> users = ircUsers();
239 foreach(IrcUser *user, users) {
242 QList<IrcChannel *> channels = ircChannels();
243 foreach(IrcChannel *channel, channels) {
244 removeIrcChannel(channel);
248 IrcUser *Network::ircUser(QString nickname) const {
249 nickname = nickname.toLower();
250 if(_ircUsers.contains(nickname))
251 return _ircUsers[nickname];
256 IrcChannel *Network::newIrcChannel(const QString &channelname) {
257 if(!_ircChannels.contains(channelname.toLower())) {
258 IrcChannel *channel = new IrcChannel(channelname, this);
261 proxy()->synchronize(channel);
263 qWarning() << "unable to synchronize new IrcChannel" << channelname << "forgot to call Network::setProxy(SignalProxy *)?";
265 connect(channel, SIGNAL(initDone()), this, SLOT(ircChannelInitDone()));
266 connect(channel, SIGNAL(destroyed()), this, SLOT(channelDestroyed()));
267 _ircChannels[channelname.toLower()] = channel;
268 emit ircChannelAdded(channelname);
269 emit ircChannelAdded(channel);
271 return _ircChannels[channelname.toLower()];
274 IrcChannel *Network::ircChannel(QString channelname) const {
275 channelname = channelname.toLower();
276 if(_ircChannels.contains(channelname))
277 return _ircChannels[channelname];
282 QByteArray Network::defaultCodecForServer() {
283 if(_defaultCodecForServer)
284 return _defaultCodecForServer->name();
288 void Network::setDefaultCodecForServer(const QByteArray &name) {
289 _defaultCodecForServer = QTextCodec::codecForName(name);
292 QByteArray Network::defaultCodecForEncoding() {
293 if(_defaultCodecForEncoding)
294 return _defaultCodecForEncoding->name();
298 void Network::setDefaultCodecForEncoding(const QByteArray &name) {
299 _defaultCodecForEncoding = QTextCodec::codecForName(name);
302 QByteArray Network::defaultCodecForDecoding() {
303 if(_defaultCodecForDecoding)
304 return _defaultCodecForDecoding->name();
308 void Network::setDefaultCodecForDecoding(const QByteArray &name) {
309 _defaultCodecForDecoding = QTextCodec::codecForName(name);
312 QByteArray Network::codecForServer() const {
314 return _codecForServer->name();
318 void Network::setCodecForServer(const QByteArray &name) {
319 setCodecForServer(QTextCodec::codecForName(name));
322 void Network::setCodecForServer(QTextCodec *codec) {
323 _codecForServer = codec;
324 emit codecForServerSet(codecForServer());
327 QByteArray Network::codecForEncoding() const {
328 if(_codecForEncoding)
329 return _codecForEncoding->name();
333 void Network::setCodecForEncoding(const QByteArray &name) {
334 setCodecForEncoding(QTextCodec::codecForName(name));
337 void Network::setCodecForEncoding(QTextCodec *codec) {
338 _codecForEncoding = codec;
339 emit codecForEncodingSet(codecForEncoding());
342 QByteArray Network::codecForDecoding() const {
343 if(_codecForDecoding)
344 return _codecForDecoding->name();
345 else return QByteArray();
348 void Network::setCodecForDecoding(const QByteArray &name) {
349 setCodecForDecoding(QTextCodec::codecForName(name));
352 void Network::setCodecForDecoding(QTextCodec *codec) {
353 _codecForDecoding = codec;
354 emit codecForDecodingSet(codecForDecoding());
357 // FIXME use server encoding if appropriate
358 QString Network::decodeString(const QByteArray &text) const {
359 if(_codecForDecoding)
360 return ::decodeString(text, _codecForDecoding);
361 else return ::decodeString(text, _defaultCodecForDecoding);
364 QByteArray Network::encodeString(const QString &string) const {
365 if(_codecForEncoding) {
366 return _codecForEncoding->fromUnicode(string);
368 if(_defaultCodecForEncoding) {
369 return _defaultCodecForEncoding->fromUnicode(string);
371 return string.toAscii();
374 QString Network::decodeServerString(const QByteArray &text) const {
376 return ::decodeString(text, _codecForServer);
378 return ::decodeString(text, _defaultCodecForServer);
381 QByteArray Network::encodeServerString(const QString &string) const {
382 if(_codecForServer) {
383 return _codecForServer->fromUnicode(string);
385 if(_defaultCodecForServer) {
386 return _defaultCodecForServer->fromUnicode(string);
388 return string.toAscii();
391 // ====================
393 // ====================
394 void Network::setNetworkName(const QString &networkName) {
395 _networkName = networkName;
396 emit networkNameSet(networkName);
399 void Network::setCurrentServer(const QString ¤tServer) {
400 _currentServer = currentServer;
401 emit currentServerSet(currentServer);
404 void Network::setConnected(bool connected) {
405 if(_connected == connected)
408 _connected = connected;
410 setMyNick(QString());
411 setCurrentServer(QString());
412 removeChansAndUsers();
414 emit connectedSet(connected);
417 //void Network::setConnectionState(ConnectionState state) {
418 void Network::setConnectionState(int state) {
419 _connectionState = (ConnectionState)state;
420 //qDebug() << "netstate" << networkId() << networkName() << state;
421 emit connectionStateSet(state);
422 emit connectionStateSet(_connectionState);
425 void Network::setMyNick(const QString &nickname) {
427 if(!_myNick.isEmpty() && !ircUser(myNick())) {
428 newIrcUser(myNick());
430 emit myNickSet(nickname);
433 void Network::setIdentity(IdentityId id) {
435 emit identitySet(id);
438 void Network::setServerList(const QVariantList &serverList) {
439 _serverList = serverList;
440 emit serverListSet(serverList);
443 void Network::setUseRandomServer(bool use) {
444 _useRandomServer = use;
445 emit useRandomServerSet(use);
448 void Network::setPerform(const QStringList &perform) {
450 emit performSet(perform);
453 void Network::setUseAutoIdentify(bool use) {
454 _useAutoIdentify = use;
455 emit useAutoIdentifySet(use);
458 void Network::setAutoIdentifyService(const QString &service) {
459 _autoIdentifyService = service;
460 emit autoIdentifyServiceSet(service);
463 void Network::setAutoIdentifyPassword(const QString &password) {
464 _autoIdentifyPassword = password;
465 emit autoIdentifyPasswordSet(password);
468 void Network::setUseAutoReconnect(bool use) {
469 _useAutoReconnect = use;
470 emit useAutoReconnectSet(use);
473 void Network::setAutoReconnectInterval(quint32 interval) {
474 _autoReconnectInterval = interval;
475 emit autoReconnectIntervalSet(interval);
478 void Network::setAutoReconnectRetries(quint16 retries) {
479 _autoReconnectRetries = retries;
480 emit autoReconnectRetriesSet(retries);
483 void Network::setUnlimitedReconnectRetries(bool unlimited) {
484 _unlimitedReconnectRetries = unlimited;
485 emit unlimitedReconnectRetriesSet(unlimited);
488 void Network::setRejoinChannels(bool rejoin) {
489 _rejoinChannels = rejoin;
490 emit rejoinChannelsSet(rejoin);
493 void Network::addSupport(const QString ¶m, const QString &value) {
494 if(!_supports.contains(param)) {
495 _supports[param] = value;
496 emit supportAdded(param, value);
500 void Network::removeSupport(const QString ¶m) {
501 if(_supports.contains(param)) {
502 _supports.remove(param);
503 emit supportRemoved(param);
507 QVariantMap Network::initSupports() const {
508 QVariantMap supports;
509 QHashIterator<QString, QString> iter(_supports);
510 while(iter.hasNext()) {
512 supports[iter.key()] = iter.value();
517 QStringList Network::initIrcUsers() const {
518 QStringList hostmasks;
519 foreach(IrcUser *ircuser, ircUsers()) {
520 hostmasks << ircuser->hostmask();
525 QStringList Network::initIrcChannels() const {
526 QStringList channels;
527 QHash<QString, IrcChannel *>::const_iterator iter = _ircChannels.constBegin();
528 while(iter != _ircChannels.constEnd()) {
529 channels << iter.value()->name();
535 void Network::initSetSupports(const QVariantMap &supports) {
536 QMapIterator<QString, QVariant> iter(supports);
537 while(iter.hasNext()) {
539 addSupport(iter.key(), iter.value().toString());
543 void Network::initSetIrcUsers(const QStringList &hostmasks) {
544 if(!_ircUsers.empty())
546 foreach(QString hostmask, hostmasks) {
547 newIrcUser(hostmask);
551 void Network::initSetIrcChannels(const QStringList &channels) {
552 if(!_ircChannels.empty())
554 foreach(QString channel, channels)
555 newIrcChannel(channel);
558 IrcUser *Network::updateNickFromMask(const QString &mask) {
559 QString nick(nickFromMask(mask).toLower());
562 if(_ircUsers.contains(nick)) {
563 ircuser = _ircUsers[nick];
564 ircuser->updateHostmask(mask);
566 ircuser = newIrcUser(mask);
571 void Network::ircUserNickChanged(QString newnick) {
572 QString oldnick = _ircUsers.key(qobject_cast<IrcUser*>(sender()));
577 if(newnick.toLower() != oldnick) _ircUsers[newnick.toLower()] = _ircUsers.take(oldnick);
579 if(myNick().toLower() == oldnick)
583 void Network::ircUserInitDone() {
584 IrcUser *ircuser = static_cast<IrcUser *>(sender());
586 emit ircUserInitDone(ircuser);
589 void Network::ircChannelInitDone() {
590 IrcChannel *ircchannel = static_cast<IrcChannel *>(sender());
591 Q_ASSERT(ircchannel);
592 emit ircChannelInitDone(ircchannel);
595 void Network::removeIrcChannel(IrcChannel *channel) {
596 QString chanName = _ircChannels.key(channel);
597 if(chanName.isNull())
600 _ircChannels.remove(chanName);
601 disconnect(channel, 0, this, 0);
602 emit ircChannelRemoved(chanName);
603 emit ircChannelRemoved(channel);
604 channel->deleteLater();
607 void Network::removeIrcChannel(const QString &channel) {
609 if((chan = ircChannel(channel)) != 0)
610 removeIrcChannel(chan);
613 void Network::channelDestroyed() {
614 IrcChannel *channel = static_cast<IrcChannel *>(sender());
616 _ircChannels.remove(_ircChannels.key(channel));
617 emit ircChannelRemoved(channel);
620 void Network::emitConnectionError(const QString &errorMsg) {
621 emit connectionError(errorMsg);
624 // ====================
626 // ====================
627 void Network::determinePrefixes() {
628 // seems like we have to construct them first
629 QString PREFIX = support("PREFIX");
631 if(PREFIX.startsWith("(") && PREFIX.contains(")")) {
632 _prefixes = PREFIX.section(")", 1);
633 _prefixModes = PREFIX.mid(1).section(")", 0, 0);
635 QString defaultPrefixes("~&@%+");
636 QString defaultPrefixModes("qaohv");
638 // we just assume that in PREFIX are only prefix chars stored
639 for(int i = 0; i < defaultPrefixes.size(); i++) {
640 if(PREFIX.contains(defaultPrefixes[i])) {
641 _prefixes += defaultPrefixes[i];
642 _prefixModes += defaultPrefixModes[i];
646 if(!_prefixes.isNull())
649 // well... our assumption was obviously wrong...
650 // check if it's only prefix modes
651 for(int i = 0; i < defaultPrefixes.size(); i++) {
652 if(PREFIX.contains(defaultPrefixModes[i])) {
653 _prefixes += defaultPrefixes[i];
654 _prefixModes += defaultPrefixModes[i];
657 // now we've done all we've could...
661 /************************************************************************
663 ************************************************************************/
665 bool NetworkInfo::operator==(const NetworkInfo &other) const {
666 if(networkId != other.networkId) return false;
667 if(networkName != other.networkName) return false;
668 if(identity != other.identity) return false;
669 if(codecForServer != other.codecForServer) return false;
670 if(codecForEncoding != other.codecForEncoding) return false;
671 if(codecForDecoding != other.codecForDecoding) return false;
672 if(serverList != other.serverList) return false;
673 if(useRandomServer != other.useRandomServer) return false;
674 if(perform != other.perform) return false;
675 if(useAutoIdentify != other.useAutoIdentify) return false;
676 if(autoIdentifyService != other.autoIdentifyService) return false;
677 if(autoIdentifyPassword != other.autoIdentifyPassword) return false;
678 if(useAutoReconnect != other.useAutoReconnect) return false;
679 if(autoReconnectInterval != other.autoReconnectInterval) return false;
680 if(autoReconnectRetries != other.autoReconnectRetries) return false;
681 if(unlimitedReconnectRetries != other.unlimitedReconnectRetries) return false;
682 if(rejoinChannels != other.rejoinChannels) return false;
686 bool NetworkInfo::operator!=(const NetworkInfo &other) const {
687 return !(*this == other);
690 QDataStream &operator<<(QDataStream &out, const NetworkInfo &info) {
692 i["NetworkId"] = QVariant::fromValue<NetworkId>(info.networkId);
693 i["NetworkName"] = info.networkName;
694 i["Identity"] = QVariant::fromValue<IdentityId>(info.identity);
695 i["CodecForServer"] = info.codecForServer;
696 i["CodecForEncoding"] = info.codecForEncoding;
697 i["CodecForDecoding"] = info.codecForDecoding;
698 i["ServerList"] = info.serverList;
699 i["UseRandomServer"] = info.useRandomServer;
700 i["Perform"] = info.perform;
701 i["UseAutoIdentify"] = info.useAutoIdentify;
702 i["AutoIdentifyService"] = info.autoIdentifyService;
703 i["AutoIdentifyPassword"] = info.autoIdentifyPassword;
704 i["UseAutoReconnect"] = info.useAutoReconnect;
705 i["AutoReconnectInterval"] = info.autoReconnectInterval;
706 i["AutoReconnectRetries"] = info.autoReconnectRetries;
707 i["UnlimitedReconnectRetries"] = info.unlimitedReconnectRetries;
708 i["RejoinChannels"] = info.rejoinChannels;
713 QDataStream &operator>>(QDataStream &in, NetworkInfo &info) {
716 info.networkId = i["NetworkId"].value<NetworkId>();
717 info.networkName = i["NetworkName"].toString();
718 info.identity = i["Identity"].value<IdentityId>();
719 info.codecForServer = i["CodecForServer"].toByteArray();
720 info.codecForEncoding = i["CodecForEncoding"].toByteArray();
721 info.codecForDecoding = i["CodecForDecoding"].toByteArray();
722 info.serverList = i["ServerList"].toList();
723 info.useRandomServer = i["UseRandomServer"].toBool();
724 info.perform = i["Perform"].toStringList();
725 info.useAutoIdentify = i["UseAutoIdentify"].toBool();
726 info.autoIdentifyService = i["AutoIdentifyService"].toString();
727 info.autoIdentifyPassword = i["AutoIdentifyPassword"].toString();
728 info.useAutoReconnect = i["UseAutoReconnect"].toBool();
729 info.autoReconnectInterval = i["AutoReconnectInterval"].toUInt();
730 info.autoReconnectRetries = i["AutoReconnectRetries"].toInt();
731 info.unlimitedReconnectRetries = i["UnlimitedReconnectRetries"].toBool();
732 info.rejoinChannels = i["RejoinChannels"].toBool();
736 QDebug operator<<(QDebug dbg, const NetworkInfo &i) {
737 dbg.nospace() << "(id = " << i.networkId << " name = " << i.networkName << " identity = " << i.identity
738 << " codecForServer = " << i.codecForServer << " codecForEncoding = " << i.codecForEncoding << " codecForDecoding = " << i.codecForDecoding
739 << " serverList = " << i.serverList << " useRandomServer = " << i.useRandomServer << " perform = " << i.perform
740 << " useAutoIdentify = " << i.useAutoIdentify << " autoIdentifyService = " << i.autoIdentifyService << " autoIdentifyPassword = " << i.autoIdentifyPassword
741 << " useAutoReconnect = " << i.useAutoReconnect << " autoReconnectInterval = " << i.autoReconnectInterval
742 << " autoReconnectRetries = " << i.autoReconnectRetries << " unlimitedReconnectRetries = " << i.unlimitedReconnectRetries
743 << " rejoinChannels = " << i.rejoinChannels << ")";