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 emit connectedSet(connected);
464 //void Network::setConnectionState(ConnectionState state) {
465 void Network::setConnectionState(int state) {
466 _connectionState = (ConnectionState)state;
467 //qDebug() << "netstate" << networkId() << networkName() << state;
468 emit connectionStateSet(state);
469 emit connectionStateSet(_connectionState);
472 void Network::setMyNick(const QString &nickname) {
474 emit myNickSet(nickname);
477 void Network::setIdentity(IdentityId id) {
479 emit identitySet(id);
482 void Network::setServerList(const QVariantList &serverList) {
483 _serverList = serverList;
484 emit serverListSet(serverList);
487 void Network::setUseRandomServer(bool use) {
488 _useRandomServer = use;
489 emit useRandomServerSet(use);
492 void Network::setPerform(const QStringList &perform) {
494 emit performSet(perform);
497 void Network::setUseAutoIdentify(bool use) {
498 _useAutoIdentify = use;
499 emit useAutoIdentifySet(use);
502 void Network::setAutoIdentifyService(const QString &service) {
503 _autoIdentifyService = service;
504 emit autoIdentifyServiceSet(service);
507 void Network::setAutoIdentifyPassword(const QString &password) {
508 _autoIdentifyPassword = password;
509 emit autoIdentifyPasswordSet(password);
512 void Network::setUseAutoReconnect(bool use) {
513 _useAutoReconnect = use;
514 emit useAutoReconnectSet(use);
517 void Network::setAutoReconnectInterval(quint32 interval) {
518 _autoReconnectInterval = interval;
519 emit autoReconnectIntervalSet(interval);
522 void Network::setAutoReconnectRetries(qint16 retries) {
523 _autoReconnectRetries = retries;
524 emit autoReconnectRetriesSet(retries);
527 void Network::setRejoinChannels(bool rejoin) {
528 _rejoinChannels = rejoin;
529 emit rejoinChannelsSet(rejoin);
532 void Network::addSupport(const QString ¶m, const QString &value) {
533 if(!_supports.contains(param)) {
534 _supports[param] = value;
535 emit supportAdded(param, value);
539 void Network::removeSupport(const QString ¶m) {
540 if(_supports.contains(param)) {
541 _supports.remove(param);
542 emit supportRemoved(param);
546 QVariantMap Network::initSupports() const {
547 QVariantMap supports;
548 QHashIterator<QString, QString> iter(_supports);
549 while(iter.hasNext()) {
551 supports[iter.key()] = iter.value();
556 QVariantList Network::initServerList() const {
560 QStringList Network::initIrcUsers() const {
561 QStringList hostmasks;
562 foreach(IrcUser *ircuser, ircUsers()) {
563 hostmasks << ircuser->hostmask();
568 QStringList Network::initIrcChannels() const {
569 return _ircChannels.keys();
572 void Network::initSetSupports(const QVariantMap &supports) {
573 QMapIterator<QString, QVariant> iter(supports);
574 while(iter.hasNext()) {
576 addSupport(iter.key(), iter.value().toString());
580 void Network::initSetServerList(const QVariantList & serverList) {
581 setServerList(serverList);
584 void Network::initSetIrcUsers(const QStringList &hostmasks) {
585 if(!_ircUsers.empty())
587 foreach(QString hostmask, hostmasks) {
588 newIrcUser(hostmask);
592 void Network::initSetChannels(const QStringList &channels) {
593 if(!_ircChannels.empty())
595 foreach(QString channel, channels)
596 newIrcChannel(channel);
599 IrcUser *Network::updateNickFromMask(const QString &mask) {
600 QString nick(nickFromMask(mask).toLower());
603 if(_ircUsers.contains(nick)) {
604 ircuser = _ircUsers[nick];
605 ircuser->updateHostmask(mask);
607 ircuser = newIrcUser(mask);
612 void Network::ircUserNickChanged(QString newnick) {
613 QString oldnick = _ircUsers.key(qobject_cast<IrcUser*>(sender()));
618 if(newnick.toLower() != oldnick) _ircUsers[newnick.toLower()] = _ircUsers.take(oldnick);
620 if(myNick().toLower() == oldnick)
624 void Network::ircUserInitDone() {
625 IrcUser *ircuser = static_cast<IrcUser *>(sender());
627 emit ircUserInitDone(ircuser);
630 void Network::ircChannelInitDone() {
631 IrcChannel *ircchannel = static_cast<IrcChannel *>(sender());
632 Q_ASSERT(ircchannel);
633 emit ircChannelInitDone(ircchannel);
636 void Network::removeIrcChannel(IrcChannel *channel) {
637 QString chanName = _ircChannels.key(channel);
638 if(chanName.isNull())
641 _ircChannels.remove(chanName);
642 disconnect(channel, 0, this, 0);
643 emit ircChannelRemoved(chanName);
644 emit ircChannelRemoved(channel);
645 channel->deleteLater();
648 void Network::removeIrcChannel(const QString &channel) {
650 if((chan = ircChannel(channel)) != 0)
651 removeIrcChannel(chan);
654 void Network::channelDestroyed() {
655 IrcChannel *channel = static_cast<IrcChannel *>(sender());
657 _ircChannels.remove(_ircChannels.key(channel));
658 emit ircChannelRemoved(channel);
661 void Network::requestConnect() const {
663 if(proxy()->proxyMode() == SignalProxy::Client) emit connectRequested(); // on the client this triggers calling this slot on the core
665 if(connectionState() != Disconnected) {
666 qWarning() << "Requesting connect while not being disconnected!";
669 emit connectRequested(networkId()); // and this is for CoreSession :)
673 void Network::requestDisconnect() const {
675 if(proxy()->proxyMode() == SignalProxy::Client) emit disconnectRequested(); // on the client this triggers calling this slot on the core
677 if(connectionState() == Disconnected) {
678 qWarning() << "Requesting disconnect while not being connected!";
681 emit disconnectRequested(networkId()); // and this is for CoreSession :)
685 void Network::emitConnectionError(const QString &errorMsg) {
686 emit connectionError(errorMsg);
689 // ====================
691 // ====================
692 void Network::determinePrefixes() {
693 // seems like we have to construct them first
694 QString PREFIX = support("PREFIX");
696 if(PREFIX.startsWith("(") && PREFIX.contains(")")) {
697 _prefixes = PREFIX.section(")", 1);
698 _prefixModes = PREFIX.mid(1).section(")", 0, 0);
700 QString defaultPrefixes("~&@%+");
701 QString defaultPrefixModes("qaohv");
703 // we just assume that in PREFIX are only prefix chars stored
704 for(int i = 0; i < defaultPrefixes.size(); i++) {
705 if(PREFIX.contains(defaultPrefixes[i])) {
706 _prefixes += defaultPrefixes[i];
707 _prefixModes += defaultPrefixModes[i];
711 if(!_prefixes.isNull())
714 // well... our assumption was obviously wrong...
715 // check if it's only prefix modes
716 for(int i = 0; i < defaultPrefixes.size(); i++) {
717 if(PREFIX.contains(defaultPrefixModes[i])) {
718 _prefixes += defaultPrefixes[i];
719 _prefixModes += defaultPrefixModes[i];
722 // now we've done all we've could...
726 /************************************************************************
728 ************************************************************************/
730 bool NetworkInfo::operator==(const NetworkInfo &other) const {
731 if(networkId != other.networkId) return false;
732 if(networkName != other.networkName) return false;
733 if(identity != other.identity) return false;
734 if(codecForEncoding != other.codecForEncoding) return false;
735 if(codecForDecoding != other.codecForDecoding) return false;
736 if(serverList != other.serverList) return false;
737 if(useRandomServer != other.useRandomServer) return false;
738 if(perform != other.perform) return false;
739 if(useAutoIdentify != other.useAutoIdentify) return false;
740 if(autoIdentifyService != other.autoIdentifyService) return false;
741 if(autoIdentifyPassword != other.autoIdentifyPassword) return false;
742 if(useAutoReconnect != other.useAutoReconnect) return false;
743 if(autoReconnectInterval != other.autoReconnectInterval) return false;
744 if(autoReconnectRetries != other.autoReconnectRetries) return false;
745 if(rejoinChannels != other.rejoinChannels) return false;
749 bool NetworkInfo::operator!=(const NetworkInfo &other) const {
750 return !(*this == other);
753 QDataStream &operator<<(QDataStream &out, const NetworkInfo &info) {
755 i["NetworkId"] = QVariant::fromValue<NetworkId>(info.networkId);
756 i["NetworkName"] = info.networkName;
757 i["Identity"] = QVariant::fromValue<IdentityId>(info.identity);
758 i["CodecForEncoding"] = info.codecForEncoding;
759 i["CodecForDecoding"] = info.codecForDecoding;
760 i["ServerList"] = info.serverList;
761 i["UseRandomServer"] = info.useRandomServer;
762 i["Perform"] = info.perform;
763 i["UseAutoIdentify"] = info.useAutoIdentify;
764 i["AutoIdentifyService"] = info.autoIdentifyService;
765 i["AutoIdentifyPassword"] = info.autoIdentifyPassword;
766 i["UseAutoReconnect"] = info.useAutoReconnect;
767 i["AutoReconnectInterval"] = info.autoReconnectInterval;
768 i["AutoReconnectRetries"] = info.autoReconnectRetries;
769 i["RejoinChannels"] = info.rejoinChannels;
774 QDataStream &operator>>(QDataStream &in, NetworkInfo &info) {
777 info.networkId = i["NetworkId"].value<NetworkId>();
778 info.networkName = i["NetworkName"].toString();
779 info.identity = i["Identity"].value<IdentityId>();
780 info.codecForEncoding = i["CodecForEncoding"].toByteArray();
781 info.codecForDecoding = i["CodecForDecoding"].toByteArray();
782 info.serverList = i["ServerList"].toList();
783 info.useRandomServer = i["UseRandomServer"].toBool();
784 info.perform = i["Perform"].toStringList();
785 info.useAutoIdentify = i["UseAutoIdentify"].toBool();
786 info.autoIdentifyService = i["AutoIdentifyService"].toString();
787 info.autoIdentifyPassword = i["AutoIdentifyPassword"].toString();
788 info.useAutoReconnect = i["UseAutoReconnect"].toBool();
789 info.autoReconnectInterval = i["AutoReconnectInterval"].toUInt();
790 info.autoReconnectRetries = i["AutoReconnectRetries"].toInt();
791 info.rejoinChannels = i["RejoinChannels"].toBool();