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 ***************************************************************************/
27 QTextCodec *Network::_defaultCodecForServer = 0;
28 QTextCodec *Network::_defaultCodecForEncoding = 0;
29 QTextCodec *Network::_defaultCodecForDecoding = 0;
30 // ====================
32 // ====================
33 Network::Network(const NetworkId &networkid, QObject *parent)
34 : SyncableObject(parent),
36 _networkId(networkid),
39 _networkName(QString("<not initialized>")),
40 _currentServer(QString()),
42 _connectionState(Disconnected),
44 _prefixModes(QString()),
45 _useRandomServer(false),
46 _useAutoIdentify(false),
47 _useAutoReconnect(false),
48 _autoReconnectInterval(60),
49 _autoReconnectRetries(10),
50 _unlimitedReconnectRetries(false),
55 setObjectName(QString::number(networkid.toInt()));
59 emit aboutToBeDestroyed();
62 bool Network::isChannelName(const QString &channelname) const {
63 if(channelname.isEmpty())
66 if(supports("CHANTYPES"))
67 return support("CHANTYPES").contains(channelname[0]);
69 return QString("#&!+").contains(channelname[0]);
72 NetworkInfo Network::networkInfo() const {
74 info.networkName = networkName();
75 info.networkId = networkId();
76 info.identity = identity();
77 info.codecForServer = codecForServer();
78 info.codecForEncoding = codecForEncoding();
79 info.codecForDecoding = codecForDecoding();
80 info.serverList = serverList();
81 info.useRandomServer = useRandomServer();
82 info.perform = perform();
83 info.useAutoIdentify = useAutoIdentify();
84 info.autoIdentifyService = autoIdentifyService();
85 info.autoIdentifyPassword = autoIdentifyPassword();
86 info.useAutoReconnect = useAutoReconnect();
87 info.autoReconnectInterval = autoReconnectInterval();
88 info.autoReconnectRetries = autoReconnectRetries();
89 info.unlimitedReconnectRetries = unlimitedReconnectRetries();
90 info.rejoinChannels = rejoinChannels();
94 void Network::setNetworkInfo(const NetworkInfo &info) {
95 // we don't set our ID!
96 if(!info.networkName.isEmpty() && info.networkName != networkName()) setNetworkName(info.networkName);
97 if(info.identity > 0 && info.identity != identity()) setIdentity(info.identity);
98 if(info.codecForServer != codecForServer()) setCodecForServer(QTextCodec::codecForName(info.codecForServer));
99 if(info.codecForEncoding != codecForEncoding()) setCodecForEncoding(QTextCodec::codecForName(info.codecForEncoding));
100 if(info.codecForDecoding != codecForDecoding()) setCodecForDecoding(QTextCodec::codecForName(info.codecForDecoding));
101 if(info.serverList.count()) setServerList(info.serverList); // FIXME compare components
102 if(info.useRandomServer != useRandomServer()) setUseRandomServer(info.useRandomServer);
103 if(info.perform != perform()) setPerform(info.perform);
104 if(info.useAutoIdentify != useAutoIdentify()) setUseAutoIdentify(info.useAutoIdentify);
105 if(info.autoIdentifyService != autoIdentifyService()) setAutoIdentifyService(info.autoIdentifyService);
106 if(info.autoIdentifyPassword != autoIdentifyPassword()) setAutoIdentifyPassword(info.autoIdentifyPassword);
107 if(info.useAutoReconnect != useAutoReconnect()) setUseAutoReconnect(info.useAutoReconnect);
108 if(info.autoReconnectInterval != autoReconnectInterval()) setAutoReconnectInterval(info.autoReconnectInterval);
109 if(info.autoReconnectRetries != autoReconnectRetries()) setAutoReconnectRetries(info.autoReconnectRetries);
110 if(info.unlimitedReconnectRetries != unlimitedReconnectRetries()) setUnlimitedReconnectRetries(info.unlimitedReconnectRetries);
111 if(info.rejoinChannels != rejoinChannels()) setRejoinChannels(info.rejoinChannels);
114 QString Network::prefixToMode(const QString &prefix) {
115 if(prefixes().contains(prefix))
116 return QString(prefixModes()[prefixes().indexOf(prefix)]);
121 QString Network::modeToPrefix(const QString &mode) {
122 if(prefixModes().contains(mode))
123 return QString(prefixes()[prefixModes().indexOf(mode)]);
128 QStringList Network::nicks() const {
129 // we don't use _ircUsers.keys() since the keys may be
130 // not up to date after a nick change
132 foreach(IrcUser *ircuser, _ircUsers.values()) {
133 nicks << ircuser->nick();
138 QString Network::prefixes() {
139 if(_prefixes.isNull())
145 QString Network::prefixModes() {
146 if(_prefixModes.isNull())
152 // example Unreal IRCD: CHANMODES=beI,kfL,lj,psmntirRcOAQKVCuzNSMTG
153 Network::ChannelModeType Network::channelModeType(const QString &mode) {
155 return NOT_A_CHANMODE;
157 QString chanmodes = support("CHANMODES");
158 if(chanmodes.isEmpty())
159 return NOT_A_CHANMODE;
161 ChannelModeType modeType = A_CHANMODE;
162 for(int i = 0; i < chanmodes.count(); i++) {
163 if(chanmodes[i] == mode[0])
165 else if(chanmodes[i] == ',')
166 modeType = (ChannelModeType)(modeType << 1);
168 if(modeType > D_CHANMODE) {
169 qWarning() << "Network" << networkId() << "supplied invalid CHANMODES:" << chanmodes;
170 modeType = NOT_A_CHANMODE;
175 QString Network::support(const QString ¶m) const {
176 QString support_ = param.toUpper();
177 if(_supports.contains(support_))
178 return _supports[support_];
183 IrcUser *Network::newIrcUser(const QString &hostmask) {
184 QString nick(nickFromMask(hostmask).toLower());
185 if(!_ircUsers.contains(nick)) {
186 IrcUser *ircuser = new IrcUser(hostmask, this);
189 proxy()->synchronize(ircuser);
191 qWarning() << "unable to synchronize new IrcUser" << hostmask << "forgot to call Network::setProxy(SignalProxy *)?";
193 connect(ircuser, SIGNAL(nickSet(QString)), this, SLOT(ircUserNickChanged(QString)));
194 connect(ircuser, SIGNAL(initDone()), this, SLOT(ircUserInitDone()));
195 connect(ircuser, SIGNAL(destroyed()), this, SLOT(ircUserDestroyed()));
196 _ircUsers[nick] = ircuser;
197 emit ircUserAdded(hostmask);
198 emit ircUserAdded(ircuser);
200 return _ircUsers[nick];
203 void Network::ircUserDestroyed() {
204 IrcUser *ircUser = static_cast<IrcUser *>(sender());
208 QHash<QString, IrcUser *>::iterator ircUserIter = _ircUsers.begin();
209 while(ircUserIter != _ircUsers.end()) {
210 if(ircUser == *ircUserIter) {
211 ircUserIter = _ircUsers.erase(ircUserIter);
218 void Network::removeIrcUser(IrcUser *ircuser) {
219 QString nick = _ircUsers.key(ircuser);
223 _ircUsers.remove(nick);
224 disconnect(ircuser, 0, this, 0);
225 emit ircUserRemoved(nick);
226 emit ircUserRemoved(ircuser);
227 ircuser->deleteLater();
230 void Network::removeIrcUser(const QString &nick) {
232 if((ircuser = ircUser(nick)) != 0)
233 removeIrcUser(ircuser);
236 void Network::removeChansAndUsers() {
237 QList<IrcUser *> users = ircUsers();
238 foreach(IrcUser *user, users) {
241 QList<IrcChannel *> channels = ircChannels();
242 foreach(IrcChannel *channel, channels) {
243 removeIrcChannel(channel);
247 IrcUser *Network::ircUser(QString nickname) const {
248 nickname = nickname.toLower();
249 if(_ircUsers.contains(nickname))
250 return _ircUsers[nickname];
255 IrcChannel *Network::newIrcChannel(const QString &channelname) {
256 if(!_ircChannels.contains(channelname.toLower())) {
257 IrcChannel *channel = new IrcChannel(channelname, this);
260 proxy()->synchronize(channel);
262 qWarning() << "unable to synchronize new IrcChannel" << channelname << "forgot to call Network::setProxy(SignalProxy *)?";
264 connect(channel, SIGNAL(initDone()), this, SLOT(ircChannelInitDone()));
265 connect(channel, SIGNAL(destroyed()), this, SLOT(channelDestroyed()));
266 _ircChannels[channelname.toLower()] = channel;
267 emit ircChannelAdded(channelname);
268 emit ircChannelAdded(channel);
270 return _ircChannels[channelname.toLower()];
273 IrcChannel *Network::ircChannel(QString channelname) const {
274 channelname = channelname.toLower();
275 if(_ircChannels.contains(channelname))
276 return _ircChannels[channelname];
281 QByteArray Network::defaultCodecForServer() {
282 if(_defaultCodecForServer)
283 return _defaultCodecForServer->name();
287 void Network::setDefaultCodecForServer(const QByteArray &name) {
288 _defaultCodecForServer = QTextCodec::codecForName(name);
291 QByteArray Network::defaultCodecForEncoding() {
292 if(_defaultCodecForEncoding)
293 return _defaultCodecForEncoding->name();
297 void Network::setDefaultCodecForEncoding(const QByteArray &name) {
298 _defaultCodecForEncoding = QTextCodec::codecForName(name);
301 QByteArray Network::defaultCodecForDecoding() {
302 if(_defaultCodecForDecoding)
303 return _defaultCodecForDecoding->name();
307 void Network::setDefaultCodecForDecoding(const QByteArray &name) {
308 _defaultCodecForDecoding = QTextCodec::codecForName(name);
311 QByteArray Network::codecForServer() const {
313 return _codecForServer->name();
317 void Network::setCodecForServer(const QByteArray &name) {
318 setCodecForServer(QTextCodec::codecForName(name));
321 void Network::setCodecForServer(QTextCodec *codec) {
322 _codecForServer = codec;
323 emit codecForServerSet(codecForServer());
326 QByteArray Network::codecForEncoding() const {
327 if(_codecForEncoding)
328 return _codecForEncoding->name();
332 void Network::setCodecForEncoding(const QByteArray &name) {
333 setCodecForEncoding(QTextCodec::codecForName(name));
336 void Network::setCodecForEncoding(QTextCodec *codec) {
337 _codecForEncoding = codec;
338 emit codecForEncodingSet(codecForEncoding());
341 QByteArray Network::codecForDecoding() const {
342 if(_codecForDecoding)
343 return _codecForDecoding->name();
344 else return QByteArray();
347 void Network::setCodecForDecoding(const QByteArray &name) {
348 setCodecForDecoding(QTextCodec::codecForName(name));
351 void Network::setCodecForDecoding(QTextCodec *codec) {
352 _codecForDecoding = codec;
353 emit codecForDecodingSet(codecForDecoding());
356 // FIXME use server encoding if appropriate
357 QString Network::decodeString(const QByteArray &text) const {
358 if(_codecForDecoding)
359 return ::decodeString(text, _codecForDecoding);
360 else return ::decodeString(text, _defaultCodecForDecoding);
363 QByteArray Network::encodeString(const QString &string) const {
364 if(_codecForEncoding) {
365 return _codecForEncoding->fromUnicode(string);
367 if(_defaultCodecForEncoding) {
368 return _defaultCodecForEncoding->fromUnicode(string);
370 return string.toAscii();
373 QString Network::decodeServerString(const QByteArray &text) const {
375 return ::decodeString(text, _codecForServer);
377 return ::decodeString(text, _defaultCodecForServer);
380 QByteArray Network::encodeServerString(const QString &string) const {
381 if(_codecForServer) {
382 return _codecForServer->fromUnicode(string);
384 if(_defaultCodecForServer) {
385 return _defaultCodecForServer->fromUnicode(string);
387 return string.toAscii();
390 // ====================
392 // ====================
393 void Network::setNetworkName(const QString &networkName) {
394 _networkName = networkName;
395 emit networkNameSet(networkName);
398 void Network::setCurrentServer(const QString ¤tServer) {
399 _currentServer = currentServer;
400 emit currentServerSet(currentServer);
403 void Network::setConnected(bool connected) {
404 if(_connected == connected)
407 _connected = connected;
409 setMyNick(QString());
410 setCurrentServer(QString());
411 removeChansAndUsers();
413 emit connectedSet(connected);
416 //void Network::setConnectionState(ConnectionState state) {
417 void Network::setConnectionState(int state) {
418 _connectionState = (ConnectionState)state;
419 //qDebug() << "netstate" << networkId() << networkName() << state;
420 emit connectionStateSet(state);
421 emit connectionStateSet(_connectionState);
424 void Network::setMyNick(const QString &nickname) {
426 if(!_myNick.isEmpty() && !ircUser(myNick())) {
427 newIrcUser(myNick());
429 emit myNickSet(nickname);
432 void Network::setIdentity(IdentityId id) {
434 emit identitySet(id);
437 void Network::setServerList(const QVariantList &serverList) {
438 _serverList = serverList;
439 emit serverListSet(serverList);
442 void Network::setUseRandomServer(bool use) {
443 _useRandomServer = use;
444 emit useRandomServerSet(use);
447 void Network::setPerform(const QStringList &perform) {
449 emit performSet(perform);
452 void Network::setUseAutoIdentify(bool use) {
453 _useAutoIdentify = use;
454 emit useAutoIdentifySet(use);
457 void Network::setAutoIdentifyService(const QString &service) {
458 _autoIdentifyService = service;
459 emit autoIdentifyServiceSet(service);
462 void Network::setAutoIdentifyPassword(const QString &password) {
463 _autoIdentifyPassword = password;
464 emit autoIdentifyPasswordSet(password);
467 void Network::setUseAutoReconnect(bool use) {
468 _useAutoReconnect = use;
469 emit useAutoReconnectSet(use);
472 void Network::setAutoReconnectInterval(quint32 interval) {
473 _autoReconnectInterval = interval;
474 emit autoReconnectIntervalSet(interval);
477 void Network::setAutoReconnectRetries(quint16 retries) {
478 _autoReconnectRetries = retries;
479 emit autoReconnectRetriesSet(retries);
482 void Network::setUnlimitedReconnectRetries(bool unlimited) {
483 _unlimitedReconnectRetries = unlimited;
484 emit unlimitedReconnectRetriesSet(unlimited);
487 void Network::setRejoinChannels(bool rejoin) {
488 _rejoinChannels = rejoin;
489 emit rejoinChannelsSet(rejoin);
492 void Network::addSupport(const QString ¶m, const QString &value) {
493 if(!_supports.contains(param)) {
494 _supports[param] = value;
495 emit supportAdded(param, value);
499 void Network::removeSupport(const QString ¶m) {
500 if(_supports.contains(param)) {
501 _supports.remove(param);
502 emit supportRemoved(param);
506 QVariantMap Network::initSupports() const {
507 QVariantMap supports;
508 QHashIterator<QString, QString> iter(_supports);
509 while(iter.hasNext()) {
511 supports[iter.key()] = iter.value();
516 QStringList Network::initIrcUsers() const {
517 QStringList hostmasks;
518 foreach(IrcUser *ircuser, ircUsers()) {
519 hostmasks << ircuser->hostmask();
524 QStringList Network::initIrcChannels() const {
525 QStringList channels;
526 QHash<QString, IrcChannel *>::const_iterator iter = _ircChannels.constBegin();
527 while(iter != _ircChannels.constEnd()) {
528 channels << iter.value()->name();
534 void Network::initSetSupports(const QVariantMap &supports) {
535 QMapIterator<QString, QVariant> iter(supports);
536 while(iter.hasNext()) {
538 addSupport(iter.key(), iter.value().toString());
542 void Network::initSetIrcUsers(const QStringList &hostmasks) {
543 if(!_ircUsers.empty())
545 foreach(QString hostmask, hostmasks) {
546 newIrcUser(hostmask);
550 void Network::initSetIrcChannels(const QStringList &channels) {
551 if(!_ircChannels.empty())
553 foreach(QString channel, channels)
554 newIrcChannel(channel);
557 IrcUser *Network::updateNickFromMask(const QString &mask) {
558 QString nick(nickFromMask(mask).toLower());
561 if(_ircUsers.contains(nick)) {
562 ircuser = _ircUsers[nick];
563 ircuser->updateHostmask(mask);
565 ircuser = newIrcUser(mask);
570 void Network::ircUserNickChanged(QString newnick) {
571 QString oldnick = _ircUsers.key(qobject_cast<IrcUser*>(sender()));
576 if(newnick.toLower() != oldnick) _ircUsers[newnick.toLower()] = _ircUsers.take(oldnick);
578 if(myNick().toLower() == oldnick)
582 void Network::ircUserInitDone() {
583 IrcUser *ircuser = static_cast<IrcUser *>(sender());
585 emit ircUserInitDone(ircuser);
588 void Network::ircChannelInitDone() {
589 IrcChannel *ircchannel = static_cast<IrcChannel *>(sender());
590 Q_ASSERT(ircchannel);
591 emit ircChannelInitDone(ircchannel);
594 void Network::removeIrcChannel(IrcChannel *channel) {
595 QString chanName = _ircChannels.key(channel);
596 if(chanName.isNull())
599 _ircChannels.remove(chanName);
600 disconnect(channel, 0, this, 0);
601 emit ircChannelRemoved(chanName);
602 emit ircChannelRemoved(channel);
603 channel->deleteLater();
606 void Network::removeIrcChannel(const QString &channel) {
608 if((chan = ircChannel(channel)) != 0)
609 removeIrcChannel(chan);
612 void Network::channelDestroyed() {
613 IrcChannel *channel = static_cast<IrcChannel *>(sender());
615 _ircChannels.remove(_ircChannels.key(channel));
616 emit ircChannelRemoved(channel);
619 void Network::emitConnectionError(const QString &errorMsg) {
620 emit connectionError(errorMsg);
623 // ====================
625 // ====================
626 void Network::determinePrefixes() {
627 // seems like we have to construct them first
628 QString PREFIX = support("PREFIX");
630 if(PREFIX.startsWith("(") && PREFIX.contains(")")) {
631 _prefixes = PREFIX.section(")", 1);
632 _prefixModes = PREFIX.mid(1).section(")", 0, 0);
634 QString defaultPrefixes("~&@%+");
635 QString defaultPrefixModes("qaohv");
637 // we just assume that in PREFIX are only prefix chars stored
638 for(int i = 0; i < defaultPrefixes.size(); i++) {
639 if(PREFIX.contains(defaultPrefixes[i])) {
640 _prefixes += defaultPrefixes[i];
641 _prefixModes += defaultPrefixModes[i];
645 if(!_prefixes.isNull())
648 // well... our assumption was obviously wrong...
649 // check if it's only prefix modes
650 for(int i = 0; i < defaultPrefixes.size(); i++) {
651 if(PREFIX.contains(defaultPrefixModes[i])) {
652 _prefixes += defaultPrefixes[i];
653 _prefixModes += defaultPrefixModes[i];
656 // now we've done all we've could...
660 /************************************************************************
662 ************************************************************************/
664 bool NetworkInfo::operator==(const NetworkInfo &other) const {
665 if(networkId != other.networkId) return false;
666 if(networkName != other.networkName) return false;
667 if(identity != other.identity) return false;
668 if(codecForServer != other.codecForServer) return false;
669 if(codecForEncoding != other.codecForEncoding) return false;
670 if(codecForDecoding != other.codecForDecoding) return false;
671 if(serverList != other.serverList) return false;
672 if(useRandomServer != other.useRandomServer) return false;
673 if(perform != other.perform) return false;
674 if(useAutoIdentify != other.useAutoIdentify) return false;
675 if(autoIdentifyService != other.autoIdentifyService) return false;
676 if(autoIdentifyPassword != other.autoIdentifyPassword) return false;
677 if(useAutoReconnect != other.useAutoReconnect) return false;
678 if(autoReconnectInterval != other.autoReconnectInterval) return false;
679 if(autoReconnectRetries != other.autoReconnectRetries) return false;
680 if(unlimitedReconnectRetries != other.unlimitedReconnectRetries) return false;
681 if(rejoinChannels != other.rejoinChannels) return false;
685 bool NetworkInfo::operator!=(const NetworkInfo &other) const {
686 return !(*this == other);
689 QDataStream &operator<<(QDataStream &out, const NetworkInfo &info) {
691 i["NetworkId"] = QVariant::fromValue<NetworkId>(info.networkId);
692 i["NetworkName"] = info.networkName;
693 i["Identity"] = QVariant::fromValue<IdentityId>(info.identity);
694 i["CodecForServer"] = info.codecForServer;
695 i["CodecForEncoding"] = info.codecForEncoding;
696 i["CodecForDecoding"] = info.codecForDecoding;
697 i["ServerList"] = info.serverList;
698 i["UseRandomServer"] = info.useRandomServer;
699 i["Perform"] = info.perform;
700 i["UseAutoIdentify"] = info.useAutoIdentify;
701 i["AutoIdentifyService"] = info.autoIdentifyService;
702 i["AutoIdentifyPassword"] = info.autoIdentifyPassword;
703 i["UseAutoReconnect"] = info.useAutoReconnect;
704 i["AutoReconnectInterval"] = info.autoReconnectInterval;
705 i["AutoReconnectRetries"] = info.autoReconnectRetries;
706 i["UnlimitedReconnectRetries"] = info.unlimitedReconnectRetries;
707 i["RejoinChannels"] = info.rejoinChannels;
712 QDataStream &operator>>(QDataStream &in, NetworkInfo &info) {
715 info.networkId = i["NetworkId"].value<NetworkId>();
716 info.networkName = i["NetworkName"].toString();
717 info.identity = i["Identity"].value<IdentityId>();
718 info.codecForServer = i["CodecForServer"].toByteArray();
719 info.codecForEncoding = i["CodecForEncoding"].toByteArray();
720 info.codecForDecoding = i["CodecForDecoding"].toByteArray();
721 info.serverList = i["ServerList"].toList();
722 info.useRandomServer = i["UseRandomServer"].toBool();
723 info.perform = i["Perform"].toStringList();
724 info.useAutoIdentify = i["UseAutoIdentify"].toBool();
725 info.autoIdentifyService = i["AutoIdentifyService"].toString();
726 info.autoIdentifyPassword = i["AutoIdentifyPassword"].toString();
727 info.useAutoReconnect = i["UseAutoReconnect"].toBool();
728 info.autoReconnectInterval = i["AutoReconnectInterval"].toUInt();
729 info.autoReconnectRetries = i["AutoReconnectRetries"].toInt();
730 info.unlimitedReconnectRetries = i["UnlimitedReconnectRetries"].toBool();
731 info.rejoinChannels = i["RejoinChannels"].toBool();
735 QDebug operator<<(QDebug dbg, const NetworkInfo &i) {
736 dbg.nospace() << "(id = " << i.networkId << " name = " << i.networkName << " identity = " << i.identity
737 << " codecForServer = " << i.codecForServer << " codecForEncoding = " << i.codecForEncoding << " codecForDecoding = " << i.codecForDecoding
738 << " serverList = " << i.serverList << " useRandomServer = " << i.useRandomServer << " perform = " << i.perform
739 << " useAutoIdentify = " << i.useAutoIdentify << " autoIdentifyService = " << i.autoIdentifyService << " autoIdentifyPassword = " << i.autoIdentifyPassword
740 << " useAutoReconnect = " << i.useAutoReconnect << " autoReconnectInterval = " << i.autoReconnectInterval
741 << " autoReconnectRetries = " << i.autoReconnectRetries << " unlimitedReconnectRetries = " << i.unlimitedReconnectRetries
742 << " rejoinChannels = " << i.rejoinChannels << ")";