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 ***************************************************************************/
22 #include "signalproxy.h"
24 #include "ircchannel.h"
31 QTextCodec *Network::_defaultCodecForEncoding = 0;
32 QTextCodec *Network::_defaultCodecForDecoding = 0;
34 // ====================
36 // ====================
37 Network::Network(const NetworkId &networkid, QObject *parent) : SyncableObject(parent),
38 _networkId(networkid),
41 _networkName(QString("<not initialized>")),
42 _currentServer(QString()),
44 _connectionState(Disconnected),
46 _prefixModes(QString()),
51 setObjectName(QString::number(networkid.toInt()));
54 // I think this is unnecessary since IrcUsers have us as their daddy :)
57 emit aboutToBeDestroyed();
58 // QHashIterator<QString, IrcUser *> ircuser(_ircUsers);
59 // while (ircuser.hasNext()) {
61 // delete ircuser.value();
63 // qDebug() << "Destroying net" << networkName() << networkId();
67 NetworkId Network::networkId() const {
71 SignalProxy *Network::proxy() const {
75 void Network::setProxy(SignalProxy *proxy) {
77 //proxy->synchronize(this); // we should to this explicitly from the outside!
80 bool Network::isMyNick(const QString &nick) const {
81 return (myNick().toLower() == nick.toLower());
84 bool Network::isMe(IrcUser *ircuser) const {
85 return (ircuser->nick().toLower() == myNick().toLower());
88 bool Network::isChannelName(const QString &channelname) const {
89 if(channelname.isEmpty())
92 if(supports("CHANTYPES"))
93 return support("CHANTYPES").contains(channelname[0]);
95 return QString("#&!+").contains(channelname[0]);
98 bool Network::isConnected() const {
102 //Network::ConnectionState Network::connectionState() const {
103 int Network::connectionState() const {
104 return _connectionState;
107 NetworkInfo Network::networkInfo() const {
109 info.networkName = networkName();
110 info.networkId = networkId();
111 info.identity = identity();
112 info.codecForEncoding = codecForEncoding();
113 info.codecForDecoding = codecForDecoding();
114 info.serverList = serverList();
115 info.useRandomServer = useRandomServer();
116 info.perform = perform();
117 info.useAutoIdentify = useAutoIdentify();
118 info.autoIdentifyService = autoIdentifyService();
119 info.autoIdentifyPassword = autoIdentifyPassword();
120 info.useAutoReconnect = useAutoReconnect();
121 info.autoReconnectInterval = autoReconnectInterval();
122 info.autoReconnectRetries = autoReconnectRetries();
123 info.rejoinChannels = rejoinChannels();
127 void Network::setNetworkInfo(const NetworkInfo &info) {
128 // we don't set our ID!
129 if(!info.networkName.isEmpty() && info.networkName != networkName()) setNetworkName(info.networkName);
130 if(info.identity > 0 && info.identity != identity()) setIdentity(info.identity);
131 if(info.codecForEncoding != codecForEncoding()) setCodecForEncoding(QTextCodec::codecForName(info.codecForEncoding));
132 if(info.codecForDecoding != codecForDecoding()) setCodecForDecoding(QTextCodec::codecForName(info.codecForDecoding));
133 if(info.serverList.count()) setServerList(info.serverList); // FIXME compare components
134 if(info.useRandomServer != useRandomServer()) setUseRandomServer(info.useRandomServer);
135 if(info.perform != perform()) setPerform(info.perform);
136 if(info.useAutoIdentify != useAutoIdentify()) setUseAutoIdentify(info.useAutoIdentify);
137 if(info.autoIdentifyService != autoIdentifyService()) setAutoIdentifyService(info.autoIdentifyService);
138 if(info.autoIdentifyPassword != autoIdentifyPassword()) setAutoIdentifyPassword(info.autoIdentifyPassword);
139 if(info.useAutoReconnect != useAutoReconnect()) setUseAutoReconnect(info.useAutoReconnect);
140 if(info.autoReconnectInterval != autoReconnectInterval()) setAutoReconnectInterval(info.autoReconnectInterval);
141 if(info.autoReconnectRetries != autoReconnectRetries()) setAutoReconnectRetries(info.autoReconnectRetries);
142 if(info.rejoinChannels != rejoinChannels()) setRejoinChannels(info.rejoinChannels);
145 QString Network::prefixToMode(const QString &prefix) {
146 if(prefixes().contains(prefix))
147 return QString(prefixModes()[prefixes().indexOf(prefix)]);
152 QString Network::prefixToMode(const QCharRef &prefix) {
153 return prefixToMode(QString(prefix));
156 QString Network::modeToPrefix(const QString &mode) {
157 if(prefixModes().contains(mode))
158 return QString(prefixes()[prefixModes().indexOf(mode)]);
163 QString Network::modeToPrefix(const QCharRef &mode) {
164 return modeToPrefix(QString(mode));
167 QString Network::networkName() const {
171 QString Network::currentServer() const {
172 return _currentServer;
175 QString Network::myNick() const {
179 IdentityId Network::identity() const {
183 QStringList Network::nicks() const {
184 // we don't use _ircUsers.keys() since the keys may be
185 // not up to date after a nick change
187 foreach(IrcUser *ircuser, _ircUsers.values()) {
188 nicks << ircuser->nick();
193 QStringList Network::channels() const {
194 return _ircChannels.keys();
197 QVariantList Network::serverList() const {
201 bool Network::useRandomServer() const {
202 return _useRandomServer;
205 QStringList Network::perform() const {
209 bool Network::useAutoIdentify() const {
210 return _useAutoIdentify;
213 QString Network::autoIdentifyService() const {
214 return _autoIdentifyService;
217 QString Network::autoIdentifyPassword() const {
218 return _autoIdentifyPassword;
221 bool Network::useAutoReconnect() const {
222 return _useAutoReconnect;
225 quint32 Network::autoReconnectInterval() const {
226 return _autoReconnectInterval;
229 qint16 Network::autoReconnectRetries() const {
230 return _autoReconnectRetries;
233 bool Network::rejoinChannels() const {
234 return _rejoinChannels;
237 QString Network::prefixes() {
238 if(_prefixes.isNull())
244 QString Network::prefixModes() {
245 if(_prefixModes.isNull())
251 bool Network::supports(const QString ¶m) const {
252 return _supports.contains(param);
255 QString Network::support(const QString ¶m) const {
256 QString support_ = param.toUpper();
257 if(_supports.contains(support_))
258 return _supports[support_];
263 IrcUser *Network::newIrcUser(const QString &hostmask) {
264 QString nick(nickFromMask(hostmask).toLower());
265 if(!_ircUsers.contains(nick)) {
266 IrcUser *ircuser = new IrcUser(hostmask, this);
267 // mark IrcUser as already initialized to keep the SignalProxy from requesting initData
268 //if(isInitialized())
269 // ircuser->setInitialized();
271 proxy()->synchronize(ircuser);
273 qWarning() << "unable to synchronize new IrcUser" << hostmask << "forgot to call Network::setProxy(SignalProxy *)?";
275 connect(ircuser, SIGNAL(nickSet(QString)), this, SLOT(ircUserNickChanged(QString)));
276 connect(ircuser, SIGNAL(initDone()), this, SLOT(ircUserInitDone()));
277 _ircUsers[nick] = ircuser;
278 emit ircUserAdded(hostmask);
279 emit ircUserAdded(ircuser);
281 return _ircUsers[nick];
284 IrcUser *Network::newIrcUser(const QByteArray &hostmask) {
285 return newIrcUser(decodeString(hostmask));
288 void Network::removeIrcUser(IrcUser *ircuser) {
289 QString nick = _ircUsers.key(ircuser);
293 _ircUsers.remove(nick);
294 disconnect(ircuser, 0, this, 0);
295 emit ircUserRemoved(nick);
296 emit ircUserRemoved(ircuser);
297 ircuser->deleteLater();
300 void Network::removeChansAndUsers() {
301 QList<IrcUser *> users = ircUsers();
302 foreach(IrcUser *user, users) {
305 QList<IrcChannel *> channels = ircChannels();
306 foreach(IrcChannel *channel, channels) {
307 removeIrcChannel(channel);
311 void Network::removeIrcUser(const QString &nick) {
313 if((ircuser = ircUser(nick)) != 0)
314 removeIrcUser(ircuser);
317 IrcUser *Network::ircUser(QString nickname) const {
318 nickname = nickname.toLower();
319 if(_ircUsers.contains(nickname))
320 return _ircUsers[nickname];
325 IrcUser *Network::ircUser(const QByteArray &nickname) const {
326 return ircUser(decodeString(nickname));
329 QList<IrcUser *> Network::ircUsers() const {
330 return _ircUsers.values();
333 quint32 Network::ircUserCount() const {
334 return _ircUsers.count();
337 IrcChannel *Network::newIrcChannel(const QString &channelname) {
338 if(!_ircChannels.contains(channelname.toLower())) {
339 IrcChannel *channel = new IrcChannel(channelname, this);
340 // mark IrcUser as already initialized to keep the SignalProxy from requesting initData
341 //if(isInitialized())
342 // channel->setInitialized();
345 proxy()->synchronize(channel);
347 qWarning() << "unable to synchronize new IrcChannel" << channelname << "forgot to call Network::setProxy(SignalProxy *)?";
349 connect(channel, SIGNAL(initDone()), this, SLOT(ircChannelInitDone()));
350 connect(channel, SIGNAL(destroyed()), this, SLOT(channelDestroyed()));
351 _ircChannels[channelname.toLower()] = channel;
352 emit ircChannelAdded(channelname);
353 emit ircChannelAdded(channel);
355 return _ircChannels[channelname.toLower()];
358 IrcChannel *Network::newIrcChannel(const QByteArray &channelname) {
359 return newIrcChannel(decodeString(channelname));
362 IrcChannel *Network::ircChannel(QString channelname) const {
363 channelname = channelname.toLower();
364 if(_ircChannels.contains(channelname))
365 return _ircChannels[channelname];
370 IrcChannel *Network::ircChannel(const QByteArray &channelname) const {
371 return ircChannel(decodeString(channelname));
375 QList<IrcChannel *> Network::ircChannels() const {
376 return _ircChannels.values();
379 quint32 Network::ircChannelCount() const {
380 return _ircChannels.count();
383 QByteArray Network::defaultCodecForEncoding() {
384 if(_defaultCodecForEncoding) return _defaultCodecForEncoding->name();
388 void Network::setDefaultCodecForEncoding(const QByteArray &name) {
389 _defaultCodecForEncoding = QTextCodec::codecForName(name);
392 QByteArray Network::defaultCodecForDecoding() {
393 if(_defaultCodecForDecoding) return _defaultCodecForDecoding->name();
397 void Network::setDefaultCodecForDecoding(const QByteArray &name) {
398 _defaultCodecForDecoding = QTextCodec::codecForName(name);
401 QByteArray Network::codecForEncoding() const {
402 if(_codecForEncoding) return _codecForEncoding->name();
406 void Network::setCodecForEncoding(const QByteArray &name) {
407 setCodecForEncoding(QTextCodec::codecForName(name));
410 void Network::setCodecForEncoding(QTextCodec *codec) {
411 _codecForEncoding = codec;
412 emit codecForEncodingSet(codecForEncoding());
415 QByteArray Network::codecForDecoding() const {
416 if(_codecForDecoding) return _codecForDecoding->name();
417 else return QByteArray();
420 void Network::setCodecForDecoding(const QByteArray &name) {
421 setCodecForDecoding(QTextCodec::codecForName(name));
424 void Network::setCodecForDecoding(QTextCodec *codec) {
425 _codecForDecoding = codec;
426 emit codecForDecodingSet(codecForDecoding());
429 QString Network::decodeString(const QByteArray &text) const {
430 if(_codecForDecoding) return ::decodeString(text, _codecForDecoding);
431 else return ::decodeString(text, _defaultCodecForDecoding);
434 QByteArray Network::encodeString(const QString string) const {
435 if(_codecForEncoding) {
436 return _codecForEncoding->fromUnicode(string);
438 if(_defaultCodecForEncoding) {
439 return _defaultCodecForEncoding->fromUnicode(string);
441 return string.toAscii();
444 // ====================
446 // ====================
447 void Network::setNetworkName(const QString &networkName) {
448 _networkName = networkName;
449 emit networkNameSet(networkName);
452 void Network::setCurrentServer(const QString ¤tServer) {
453 _currentServer = currentServer;
454 emit currentServerSet(currentServer);
457 void Network::setConnected(bool connected) {
458 _connected = connected;
460 removeChansAndUsers();
461 setCurrentServer(QString());
463 emit connectedSet(connected);
466 //void Network::setConnectionState(ConnectionState state) {
467 void Network::setConnectionState(int state) {
468 _connectionState = (ConnectionState)state;
469 //qDebug() << "netstate" << networkId() << networkName() << state;
470 emit connectionStateSet(state);
471 emit connectionStateSet(_connectionState);
474 void Network::setMyNick(const QString &nickname) {
476 emit myNickSet(nickname);
479 void Network::setIdentity(IdentityId id) {
481 emit identitySet(id);
484 void Network::setServerList(const QVariantList &serverList) {
485 _serverList = serverList;
486 emit serverListSet(serverList);
489 void Network::setUseRandomServer(bool use) {
490 _useRandomServer = use;
491 emit useRandomServerSet(use);
494 void Network::setPerform(const QStringList &perform) {
496 emit performSet(perform);
499 void Network::setUseAutoIdentify(bool use) {
500 _useAutoIdentify = use;
501 emit useAutoIdentifySet(use);
504 void Network::setAutoIdentifyService(const QString &service) {
505 _autoIdentifyService = service;
506 emit autoIdentifyServiceSet(service);
509 void Network::setAutoIdentifyPassword(const QString &password) {
510 _autoIdentifyPassword = password;
511 emit autoIdentifyPasswordSet(password);
514 void Network::setUseAutoReconnect(bool use) {
515 _useAutoReconnect = use;
516 emit useAutoReconnectSet(use);
519 void Network::setAutoReconnectInterval(quint32 interval) {
520 _autoReconnectInterval = interval;
521 emit autoReconnectIntervalSet(interval);
524 void Network::setAutoReconnectRetries(qint16 retries) {
525 _autoReconnectRetries = retries;
526 emit autoReconnectRetriesSet(retries);
529 void Network::setRejoinChannels(bool rejoin) {
530 _rejoinChannels = rejoin;
531 emit rejoinChannelsSet(rejoin);
534 void Network::addSupport(const QString ¶m, const QString &value) {
535 if(!_supports.contains(param)) {
536 _supports[param] = value;
537 emit supportAdded(param, value);
541 void Network::removeSupport(const QString ¶m) {
542 if(_supports.contains(param)) {
543 _supports.remove(param);
544 emit supportRemoved(param);
548 QVariantMap Network::initSupports() const {
549 QVariantMap supports;
550 QHashIterator<QString, QString> iter(_supports);
551 while(iter.hasNext()) {
553 supports[iter.key()] = iter.value();
558 QVariantList Network::initServerList() const {
562 QStringList Network::initIrcUsers() const {
563 QStringList hostmasks;
564 foreach(IrcUser *ircuser, ircUsers()) {
565 hostmasks << ircuser->hostmask();
570 QStringList Network::initIrcChannels() const {
571 return _ircChannels.keys();
574 void Network::initSetSupports(const QVariantMap &supports) {
575 QMapIterator<QString, QVariant> iter(supports);
576 while(iter.hasNext()) {
578 addSupport(iter.key(), iter.value().toString());
582 void Network::initSetServerList(const QVariantList & serverList) {
583 setServerList(serverList);
586 void Network::initSetIrcUsers(const QStringList &hostmasks) {
587 if(!_ircUsers.empty())
589 foreach(QString hostmask, hostmasks) {
590 newIrcUser(hostmask);
594 void Network::initSetChannels(const QStringList &channels) {
595 if(!_ircChannels.empty())
597 foreach(QString channel, channels)
598 newIrcChannel(channel);
601 IrcUser *Network::updateNickFromMask(const QString &mask) {
602 QString nick(nickFromMask(mask).toLower());
605 if(_ircUsers.contains(nick)) {
606 ircuser = _ircUsers[nick];
607 ircuser->updateHostmask(mask);
609 ircuser = newIrcUser(mask);
614 void Network::ircUserNickChanged(QString newnick) {
615 QString oldnick = _ircUsers.key(qobject_cast<IrcUser*>(sender()));
620 if(newnick.toLower() != oldnick) _ircUsers[newnick.toLower()] = _ircUsers.take(oldnick);
622 if(myNick().toLower() == oldnick)
626 void Network::ircUserInitDone() {
627 IrcUser *ircuser = static_cast<IrcUser *>(sender());
629 emit ircUserInitDone(ircuser);
632 void Network::ircChannelInitDone() {
633 IrcChannel *ircchannel = static_cast<IrcChannel *>(sender());
634 Q_ASSERT(ircchannel);
635 emit ircChannelInitDone(ircchannel);
638 void Network::removeIrcChannel(IrcChannel *channel) {
639 QString chanName = _ircChannels.key(channel);
640 if(chanName.isNull())
643 _ircChannels.remove(chanName);
644 disconnect(channel, 0, this, 0);
645 emit ircChannelRemoved(chanName);
646 emit ircChannelRemoved(channel);
647 channel->deleteLater();
650 void Network::removeIrcChannel(const QString &channel) {
652 if((chan = ircChannel(channel)) != 0)
653 removeIrcChannel(chan);
656 void Network::channelDestroyed() {
657 IrcChannel *channel = static_cast<IrcChannel *>(sender());
659 _ircChannels.remove(_ircChannels.key(channel));
660 emit ircChannelRemoved(channel);
663 void Network::requestConnect() const {
665 if(proxy()->proxyMode() == SignalProxy::Client) emit connectRequested(); // on the client this triggers calling this slot on the core
667 if(connectionState() != Disconnected) {
668 qWarning() << "Requesting connect while not being disconnected!";
671 emit connectRequested(networkId()); // and this is for CoreSession :)
675 void Network::requestDisconnect() const {
677 if(proxy()->proxyMode() == SignalProxy::Client) emit disconnectRequested(); // on the client this triggers calling this slot on the core
679 if(connectionState() == Disconnected) {
680 qWarning() << "Requesting disconnect while not being connected!";
683 emit disconnectRequested(networkId()); // and this is for CoreSession :)
687 void Network::emitConnectionError(const QString &errorMsg) {
688 emit connectionError(errorMsg);
691 // ====================
693 // ====================
694 void Network::determinePrefixes() {
695 // seems like we have to construct them first
696 QString PREFIX = support("PREFIX");
698 if(PREFIX.startsWith("(") && PREFIX.contains(")")) {
699 _prefixes = PREFIX.section(")", 1);
700 _prefixModes = PREFIX.mid(1).section(")", 0, 0);
702 QString defaultPrefixes("~&@%+");
703 QString defaultPrefixModes("qaohv");
705 // we just assume that in PREFIX are only prefix chars stored
706 for(int i = 0; i < defaultPrefixes.size(); i++) {
707 if(PREFIX.contains(defaultPrefixes[i])) {
708 _prefixes += defaultPrefixes[i];
709 _prefixModes += defaultPrefixModes[i];
713 if(!_prefixes.isNull())
716 // well... our assumption was obviously wrong...
717 // check if it's only prefix modes
718 for(int i = 0; i < defaultPrefixes.size(); i++) {
719 if(PREFIX.contains(defaultPrefixModes[i])) {
720 _prefixes += defaultPrefixes[i];
721 _prefixModes += defaultPrefixModes[i];
724 // now we've done all we've could...
728 /************************************************************************
730 ************************************************************************/
732 bool NetworkInfo::operator==(const NetworkInfo &other) const {
733 if(networkId != other.networkId) return false;
734 if(networkName != other.networkName) return false;
735 if(identity != other.identity) return false;
736 if(codecForEncoding != other.codecForEncoding) return false;
737 if(codecForDecoding != other.codecForDecoding) return false;
738 if(serverList != other.serverList) return false;
739 if(useRandomServer != other.useRandomServer) return false;
740 if(perform != other.perform) return false;
741 if(useAutoIdentify != other.useAutoIdentify) return false;
742 if(autoIdentifyService != other.autoIdentifyService) return false;
743 if(autoIdentifyPassword != other.autoIdentifyPassword) return false;
744 if(useAutoReconnect != other.useAutoReconnect) return false;
745 if(autoReconnectInterval != other.autoReconnectInterval) return false;
746 if(autoReconnectRetries != other.autoReconnectRetries) return false;
747 if(rejoinChannels != other.rejoinChannels) return false;
751 bool NetworkInfo::operator!=(const NetworkInfo &other) const {
752 return !(*this == other);
755 QDataStream &operator<<(QDataStream &out, const NetworkInfo &info) {
757 i["NetworkId"] = QVariant::fromValue<NetworkId>(info.networkId);
758 i["NetworkName"] = info.networkName;
759 i["Identity"] = QVariant::fromValue<IdentityId>(info.identity);
760 i["CodecForEncoding"] = info.codecForEncoding;
761 i["CodecForDecoding"] = info.codecForDecoding;
762 i["ServerList"] = info.serverList;
763 i["UseRandomServer"] = info.useRandomServer;
764 i["Perform"] = info.perform;
765 i["UseAutoIdentify"] = info.useAutoIdentify;
766 i["AutoIdentifyService"] = info.autoIdentifyService;
767 i["AutoIdentifyPassword"] = info.autoIdentifyPassword;
768 i["UseAutoReconnect"] = info.useAutoReconnect;
769 i["AutoReconnectInterval"] = info.autoReconnectInterval;
770 i["AutoReconnectRetries"] = info.autoReconnectRetries;
771 i["RejoinChannels"] = info.rejoinChannels;
776 QDataStream &operator>>(QDataStream &in, NetworkInfo &info) {
779 info.networkId = i["NetworkId"].value<NetworkId>();
780 info.networkName = i["NetworkName"].toString();
781 info.identity = i["Identity"].value<IdentityId>();
782 info.codecForEncoding = i["CodecForEncoding"].toByteArray();
783 info.codecForDecoding = i["CodecForDecoding"].toByteArray();
784 info.serverList = i["ServerList"].toList();
785 info.useRandomServer = i["UseRandomServer"].toBool();
786 info.perform = i["Perform"].toStringList();
787 info.useAutoIdentify = i["UseAutoIdentify"].toBool();
788 info.autoIdentifyService = i["AutoIdentifyService"].toString();
789 info.autoIdentifyPassword = i["AutoIdentifyPassword"].toString();
790 info.useAutoReconnect = i["UseAutoReconnect"].toBool();
791 info.autoReconnectInterval = i["AutoReconnectInterval"].toUInt();
792 info.autoReconnectRetries = i["AutoReconnectRetries"].toInt();
793 info.rejoinChannels = i["RejoinChannels"].toBool();