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),
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),
55 _autoAwayActive(false)
57 setObjectName(QString::number(networkid.toInt()));
61 emit aboutToBeDestroyed();
64 bool Network::isChannelName(const QString &channelname) const {
65 if(channelname.isEmpty())
68 if(supports("CHANTYPES"))
69 return support("CHANTYPES").contains(channelname[0]);
71 return QString("#&!+").contains(channelname[0]);
74 NetworkInfo Network::networkInfo() const {
76 info.networkName = networkName();
77 info.networkId = networkId();
78 info.identity = identity();
79 info.codecForServer = codecForServer();
80 info.codecForEncoding = codecForEncoding();
81 info.codecForDecoding = codecForDecoding();
82 info.serverList = serverList();
83 info.useRandomServer = useRandomServer();
84 info.perform = perform();
85 info.useAutoIdentify = useAutoIdentify();
86 info.autoIdentifyService = autoIdentifyService();
87 info.autoIdentifyPassword = autoIdentifyPassword();
88 info.useAutoReconnect = useAutoReconnect();
89 info.autoReconnectInterval = autoReconnectInterval();
90 info.autoReconnectRetries = autoReconnectRetries();
91 info.unlimitedReconnectRetries = unlimitedReconnectRetries();
92 info.rejoinChannels = rejoinChannels();
96 void Network::setNetworkInfo(const NetworkInfo &info) {
97 // we don't set our ID!
98 if(!info.networkName.isEmpty() && info.networkName != networkName()) setNetworkName(info.networkName);
99 if(info.identity > 0 && info.identity != identity()) setIdentity(info.identity);
100 if(info.codecForServer != codecForServer()) setCodecForServer(QTextCodec::codecForName(info.codecForServer));
101 if(info.codecForEncoding != codecForEncoding()) setCodecForEncoding(QTextCodec::codecForName(info.codecForEncoding));
102 if(info.codecForDecoding != codecForDecoding()) setCodecForDecoding(QTextCodec::codecForName(info.codecForDecoding));
103 if(info.serverList.count()) setServerList(info.serverList); // FIXME compare components
104 if(info.useRandomServer != useRandomServer()) setUseRandomServer(info.useRandomServer);
105 if(info.perform != perform()) setPerform(info.perform);
106 if(info.useAutoIdentify != useAutoIdentify()) setUseAutoIdentify(info.useAutoIdentify);
107 if(info.autoIdentifyService != autoIdentifyService()) setAutoIdentifyService(info.autoIdentifyService);
108 if(info.autoIdentifyPassword != autoIdentifyPassword()) setAutoIdentifyPassword(info.autoIdentifyPassword);
109 if(info.useAutoReconnect != useAutoReconnect()) setUseAutoReconnect(info.useAutoReconnect);
110 if(info.autoReconnectInterval != autoReconnectInterval()) setAutoReconnectInterval(info.autoReconnectInterval);
111 if(info.autoReconnectRetries != autoReconnectRetries()) setAutoReconnectRetries(info.autoReconnectRetries);
112 if(info.unlimitedReconnectRetries != unlimitedReconnectRetries()) setUnlimitedReconnectRetries(info.unlimitedReconnectRetries);
113 if(info.rejoinChannels != rejoinChannels()) setRejoinChannels(info.rejoinChannels);
116 QString Network::prefixToMode(const QString &prefix) {
117 if(prefixes().contains(prefix))
118 return QString(prefixModes()[prefixes().indexOf(prefix)]);
123 QString Network::modeToPrefix(const QString &mode) {
124 if(prefixModes().contains(mode))
125 return QString(prefixes()[prefixModes().indexOf(mode)]);
130 QStringList Network::nicks() const {
131 // we don't use _ircUsers.keys() since the keys may be
132 // not up to date after a nick change
134 foreach(IrcUser *ircuser, _ircUsers.values()) {
135 nicks << ircuser->nick();
140 QString Network::prefixes() {
141 if(_prefixes.isNull())
147 QString Network::prefixModes() {
148 if(_prefixModes.isNull())
154 // example Unreal IRCD: CHANMODES=beI,kfL,lj,psmntirRcOAQKVCuzNSMTG
155 Network::ChannelModeType Network::channelModeType(const QString &mode) {
157 return NOT_A_CHANMODE;
159 QString chanmodes = support("CHANMODES");
160 if(chanmodes.isEmpty())
161 return NOT_A_CHANMODE;
163 ChannelModeType modeType = A_CHANMODE;
164 for(int i = 0; i < chanmodes.count(); i++) {
165 if(chanmodes[i] == mode[0])
167 else if(chanmodes[i] == ',')
168 modeType = (ChannelModeType)(modeType << 1);
170 if(modeType > D_CHANMODE) {
171 qWarning() << "Network" << networkId() << "supplied invalid CHANMODES:" << chanmodes;
172 modeType = NOT_A_CHANMODE;
177 QString Network::support(const QString ¶m) const {
178 QString support_ = param.toUpper();
179 if(_supports.contains(support_))
180 return _supports[support_];
185 IrcUser *Network::newIrcUser(const QString &hostmask, const QVariantMap &initData) {
186 QString nick(nickFromMask(hostmask).toLower());
187 if(!_ircUsers.contains(nick)) {
188 IrcUser *ircuser = new IrcUser(hostmask, this);
189 if(!initData.isEmpty()) {
190 ircuser->fromVariantMap(initData);
191 ircuser->setInitialized();
195 proxy()->synchronize(ircuser);
197 qWarning() << "unable to synchronize new IrcUser" << hostmask << "forgot to call Network::setProxy(SignalProxy *)?";
199 connect(ircuser, SIGNAL(nickSet(QString)), this, SLOT(ircUserNickChanged(QString)));
201 _ircUsers[nick] = ircuser;
203 emit ircUserAdded(hostmask);
204 emit ircUserAdded(ircuser);
207 return _ircUsers[nick];
210 IrcUser *Network::ircUser(QString nickname) const {
211 nickname = nickname.toLower();
212 if(_ircUsers.contains(nickname))
213 return _ircUsers[nickname];
218 void Network::removeIrcUser(IrcUser *ircuser) {
219 QString nick = _ircUsers.key(ircuser);
223 _ircUsers.remove(nick);
224 disconnect(ircuser, 0, this, 0);
225 ircuser->deleteLater();
228 void Network::removeIrcChannel(IrcChannel *channel) {
229 QString chanName = _ircChannels.key(channel);
230 if(chanName.isNull())
233 _ircChannels.remove(chanName);
234 disconnect(channel, 0, this, 0);
235 channel->deleteLater();
238 void Network::removeChansAndUsers() {
239 QList<IrcUser *> users = ircUsers();
241 QList<IrcChannel *> channels = ircChannels();
242 _ircChannels.clear();
244 foreach(IrcChannel *channel, channels) {
245 disconnect(channel, 0, this, 0);
247 foreach(IrcUser *user, users) {
248 disconnect(user, 0, this, 0);
252 qDeleteAll(channels);
255 IrcChannel *Network::newIrcChannel(const QString &channelname, const QVariantMap &initData) {
256 if(!_ircChannels.contains(channelname.toLower())) {
257 IrcChannel *channel = ircChannelFactory(channelname);
258 if(!initData.isEmpty()) {
259 channel->fromVariantMap(initData);
260 channel->setInitialized();
264 proxy()->synchronize(channel);
266 qWarning() << "unable to synchronize new IrcChannel" << channelname << "forgot to call Network::setProxy(SignalProxy *)?";
268 _ircChannels[channelname.toLower()] = channel;
270 emit ircChannelAdded(channelname);
271 emit ircChannelAdded(channel);
273 return _ircChannels[channelname.toLower()];
276 IrcChannel *Network::ircChannel(QString channelname) const {
277 channelname = channelname.toLower();
278 if(_ircChannels.contains(channelname))
279 return _ircChannels[channelname];
284 QByteArray Network::defaultCodecForServer() {
285 if(_defaultCodecForServer)
286 return _defaultCodecForServer->name();
290 void Network::setDefaultCodecForServer(const QByteArray &name) {
291 _defaultCodecForServer = QTextCodec::codecForName(name);
294 QByteArray Network::defaultCodecForEncoding() {
295 if(_defaultCodecForEncoding)
296 return _defaultCodecForEncoding->name();
300 void Network::setDefaultCodecForEncoding(const QByteArray &name) {
301 _defaultCodecForEncoding = QTextCodec::codecForName(name);
304 QByteArray Network::defaultCodecForDecoding() {
305 if(_defaultCodecForDecoding)
306 return _defaultCodecForDecoding->name();
310 void Network::setDefaultCodecForDecoding(const QByteArray &name) {
311 _defaultCodecForDecoding = QTextCodec::codecForName(name);
314 QByteArray Network::codecForServer() const {
316 return _codecForServer->name();
320 void Network::setCodecForServer(const QByteArray &name) {
321 setCodecForServer(QTextCodec::codecForName(name));
324 void Network::setCodecForServer(QTextCodec *codec) {
325 _codecForServer = codec;
326 emit codecForServerSet(codecForServer());
329 QByteArray Network::codecForEncoding() const {
330 if(_codecForEncoding)
331 return _codecForEncoding->name();
335 void Network::setCodecForEncoding(const QByteArray &name) {
336 setCodecForEncoding(QTextCodec::codecForName(name));
339 void Network::setCodecForEncoding(QTextCodec *codec) {
340 _codecForEncoding = codec;
341 emit codecForEncodingSet(codecForEncoding());
344 QByteArray Network::codecForDecoding() const {
345 if(_codecForDecoding)
346 return _codecForDecoding->name();
347 else return QByteArray();
350 void Network::setCodecForDecoding(const QByteArray &name) {
351 setCodecForDecoding(QTextCodec::codecForName(name));
354 void Network::setCodecForDecoding(QTextCodec *codec) {
355 _codecForDecoding = codec;
356 emit codecForDecodingSet(codecForDecoding());
359 // FIXME use server encoding if appropriate
360 QString Network::decodeString(const QByteArray &text) const {
361 if(_codecForDecoding)
362 return ::decodeString(text, _codecForDecoding);
363 else return ::decodeString(text, _defaultCodecForDecoding);
366 QByteArray Network::encodeString(const QString &string) const {
367 if(_codecForEncoding) {
368 return _codecForEncoding->fromUnicode(string);
370 if(_defaultCodecForEncoding) {
371 return _defaultCodecForEncoding->fromUnicode(string);
373 return string.toAscii();
376 QString Network::decodeServerString(const QByteArray &text) const {
378 return ::decodeString(text, _codecForServer);
380 return ::decodeString(text, _defaultCodecForServer);
383 QByteArray Network::encodeServerString(const QString &string) const {
384 if(_codecForServer) {
385 return _codecForServer->fromUnicode(string);
387 if(_defaultCodecForServer) {
388 return _defaultCodecForServer->fromUnicode(string);
390 return string.toAscii();
393 // ====================
395 // ====================
396 void Network::setNetworkName(const QString &networkName) {
397 _networkName = networkName;
398 emit networkNameSet(networkName);
401 void Network::setCurrentServer(const QString ¤tServer) {
402 _currentServer = currentServer;
403 emit currentServerSet(currentServer);
406 void Network::setConnected(bool connected) {
407 if(_connected == connected)
410 _connected = connected;
412 setMyNick(QString());
413 setCurrentServer(QString());
414 removeChansAndUsers();
416 emit connectedSet(connected);
419 //void Network::setConnectionState(ConnectionState state) {
420 void Network::setConnectionState(int state) {
421 _connectionState = (ConnectionState)state;
422 //qDebug() << "netstate" << networkId() << networkName() << state;
423 emit connectionStateSet(state);
424 emit connectionStateSet(_connectionState);
427 void Network::setMyNick(const QString &nickname) {
429 if(!_myNick.isEmpty() && !ircUser(myNick())) {
430 newIrcUser(myNick());
432 emit myNickSet(nickname);
435 void Network::setLatency(int latency) {
436 if(_latency == latency)
439 emit latencySet(latency);
442 void Network::setIdentity(IdentityId id) {
444 emit identitySet(id);
447 void Network::setServerList(const QVariantList &serverList) {
448 _serverList = serverList;
449 emit serverListSet(serverList);
452 void Network::setUseRandomServer(bool use) {
453 _useRandomServer = use;
454 emit useRandomServerSet(use);
457 void Network::setPerform(const QStringList &perform) {
459 emit performSet(perform);
462 void Network::setUseAutoIdentify(bool use) {
463 _useAutoIdentify = use;
464 emit useAutoIdentifySet(use);
467 void Network::setAutoIdentifyService(const QString &service) {
468 _autoIdentifyService = service;
469 emit autoIdentifyServiceSet(service);
472 void Network::setAutoIdentifyPassword(const QString &password) {
473 _autoIdentifyPassword = password;
474 emit autoIdentifyPasswordSet(password);
477 void Network::setUseAutoReconnect(bool use) {
478 _useAutoReconnect = use;
479 emit useAutoReconnectSet(use);
482 void Network::setAutoReconnectInterval(quint32 interval) {
483 _autoReconnectInterval = interval;
484 emit autoReconnectIntervalSet(interval);
487 void Network::setAutoReconnectRetries(quint16 retries) {
488 _autoReconnectRetries = retries;
489 emit autoReconnectRetriesSet(retries);
492 void Network::setUnlimitedReconnectRetries(bool unlimited) {
493 _unlimitedReconnectRetries = unlimited;
494 emit unlimitedReconnectRetriesSet(unlimited);
497 void Network::setRejoinChannels(bool rejoin) {
498 _rejoinChannels = rejoin;
499 emit rejoinChannelsSet(rejoin);
502 void Network::addSupport(const QString ¶m, const QString &value) {
503 if(!_supports.contains(param)) {
504 _supports[param] = value;
505 emit supportAdded(param, value);
509 void Network::removeSupport(const QString ¶m) {
510 if(_supports.contains(param)) {
511 _supports.remove(param);
512 emit supportRemoved(param);
516 QVariantMap Network::initSupports() const {
517 QVariantMap supports;
518 QHashIterator<QString, QString> iter(_supports);
519 while(iter.hasNext()) {
521 supports[iter.key()] = iter.value();
526 QVariantMap Network::initIrcUsersAndChannels() const {
527 QVariantMap usersAndChannels;
529 QVariantMap channels;
531 QHash<QString, IrcUser *>::const_iterator userIter = _ircUsers.constBegin();
532 QHash<QString, IrcUser *>::const_iterator userIterEnd = _ircUsers.constEnd();
533 while(userIter != userIterEnd) {
534 users[userIter.value()->hostmask()] = userIter.value()->toVariantMap();
537 usersAndChannels["users"] = users;
539 QHash<QString, IrcChannel *>::const_iterator channelIter = _ircChannels.constBegin();
540 QHash<QString, IrcChannel *>::const_iterator channelIterEnd = _ircChannels.constEnd();
541 while(channelIter != channelIterEnd) {
542 channels[channelIter.value()->name()] = channelIter.value()->toVariantMap();
545 usersAndChannels["channels"] = channels;
547 return usersAndChannels;
550 void Network::initSetIrcUsersAndChannels(const QVariantMap &usersAndChannels) {
552 if(isInitialized()) {
553 qWarning() << "Network" << networkId() << "received init data for users and channels allthough there allready are known users or channels!";
557 QVariantMap users = usersAndChannels.value("users").toMap();
558 QVariantMap::const_iterator userIter = users.constBegin();
559 QVariantMap::const_iterator userIterEnd = users.constEnd();
560 while(userIter != userIterEnd) {
561 newIrcUser(userIter.key(), userIter.value().toMap());
565 QVariantMap channels = usersAndChannels.value("channels").toMap();
566 QVariantMap::const_iterator channelIter = channels.constBegin();
567 QVariantMap::const_iterator channelIterEnd = channels.constEnd();
568 while(channelIter != channelIterEnd) {
569 newIrcChannel(channelIter.key(), channelIter.value().toMap());
575 void Network::initSetSupports(const QVariantMap &supports) {
576 QMapIterator<QString, QVariant> iter(supports);
577 while(iter.hasNext()) {
579 addSupport(iter.key(), iter.value().toString());
583 IrcUser *Network::updateNickFromMask(const QString &mask) {
584 QString nick(nickFromMask(mask).toLower());
587 if(_ircUsers.contains(nick)) {
588 ircuser = _ircUsers[nick];
589 ircuser->updateHostmask(mask);
591 ircuser = newIrcUser(mask);
596 void Network::ircUserNickChanged(QString newnick) {
597 QString oldnick = _ircUsers.key(qobject_cast<IrcUser*>(sender()));
602 if(newnick.toLower() != oldnick) _ircUsers[newnick.toLower()] = _ircUsers.take(oldnick);
604 if(myNick().toLower() == oldnick)
608 void Network::emitConnectionError(const QString &errorMsg) {
609 emit connectionError(errorMsg);
612 // ====================
614 // ====================
615 void Network::determinePrefixes() {
616 // seems like we have to construct them first
617 QString prefix = support("PREFIX");
619 if(prefix.startsWith("(") && prefix.contains(")")) {
620 _prefixes = prefix.section(")", 1);
621 _prefixModes = prefix.mid(1).section(")", 0, 0);
623 QString defaultPrefixes("~&@%+");
624 QString defaultPrefixModes("qaohv");
626 if(prefix.isEmpty()) {
627 _prefixes = defaultPrefixes;
628 _prefixModes = defaultPrefixModes;
632 // we just assume that in PREFIX are only prefix chars stored
633 for(int i = 0; i < defaultPrefixes.size(); i++) {
634 if(prefix.contains(defaultPrefixes[i])) {
635 _prefixes += defaultPrefixes[i];
636 _prefixModes += defaultPrefixModes[i];
640 if(!_prefixes.isNull())
643 // well... our assumption was obviously wrong...
644 // check if it's only prefix modes
645 for(int i = 0; i < defaultPrefixes.size(); i++) {
646 if(prefix.contains(defaultPrefixModes[i])) {
647 _prefixes += defaultPrefixes[i];
648 _prefixModes += defaultPrefixModes[i];
651 // now we've done all we've could...
655 /************************************************************************
657 ************************************************************************/
659 bool NetworkInfo::operator==(const NetworkInfo &other) const {
660 if(networkId != other.networkId) return false;
661 if(networkName != other.networkName) return false;
662 if(identity != other.identity) return false;
663 if(codecForServer != other.codecForServer) return false;
664 if(codecForEncoding != other.codecForEncoding) return false;
665 if(codecForDecoding != other.codecForDecoding) return false;
666 if(serverList != other.serverList) return false;
667 if(useRandomServer != other.useRandomServer) return false;
668 if(perform != other.perform) return false;
669 if(useAutoIdentify != other.useAutoIdentify) return false;
670 if(autoIdentifyService != other.autoIdentifyService) return false;
671 if(autoIdentifyPassword != other.autoIdentifyPassword) return false;
672 if(useAutoReconnect != other.useAutoReconnect) return false;
673 if(autoReconnectInterval != other.autoReconnectInterval) return false;
674 if(autoReconnectRetries != other.autoReconnectRetries) return false;
675 if(unlimitedReconnectRetries != other.unlimitedReconnectRetries) return false;
676 if(rejoinChannels != other.rejoinChannels) return false;
680 bool NetworkInfo::operator!=(const NetworkInfo &other) const {
681 return !(*this == other);
684 QDataStream &operator<<(QDataStream &out, const NetworkInfo &info) {
686 i["NetworkId"] = QVariant::fromValue<NetworkId>(info.networkId);
687 i["NetworkName"] = info.networkName;
688 i["Identity"] = QVariant::fromValue<IdentityId>(info.identity);
689 i["CodecForServer"] = info.codecForServer;
690 i["CodecForEncoding"] = info.codecForEncoding;
691 i["CodecForDecoding"] = info.codecForDecoding;
692 i["ServerList"] = info.serverList;
693 i["UseRandomServer"] = info.useRandomServer;
694 i["Perform"] = info.perform;
695 i["UseAutoIdentify"] = info.useAutoIdentify;
696 i["AutoIdentifyService"] = info.autoIdentifyService;
697 i["AutoIdentifyPassword"] = info.autoIdentifyPassword;
698 i["UseAutoReconnect"] = info.useAutoReconnect;
699 i["AutoReconnectInterval"] = info.autoReconnectInterval;
700 i["AutoReconnectRetries"] = info.autoReconnectRetries;
701 i["UnlimitedReconnectRetries"] = info.unlimitedReconnectRetries;
702 i["RejoinChannels"] = info.rejoinChannels;
707 QDataStream &operator>>(QDataStream &in, NetworkInfo &info) {
710 info.networkId = i["NetworkId"].value<NetworkId>();
711 info.networkName = i["NetworkName"].toString();
712 info.identity = i["Identity"].value<IdentityId>();
713 info.codecForServer = i["CodecForServer"].toByteArray();
714 info.codecForEncoding = i["CodecForEncoding"].toByteArray();
715 info.codecForDecoding = i["CodecForDecoding"].toByteArray();
716 info.serverList = i["ServerList"].toList();
717 info.useRandomServer = i["UseRandomServer"].toBool();
718 info.perform = i["Perform"].toStringList();
719 info.useAutoIdentify = i["UseAutoIdentify"].toBool();
720 info.autoIdentifyService = i["AutoIdentifyService"].toString();
721 info.autoIdentifyPassword = i["AutoIdentifyPassword"].toString();
722 info.useAutoReconnect = i["UseAutoReconnect"].toBool();
723 info.autoReconnectInterval = i["AutoReconnectInterval"].toUInt();
724 info.autoReconnectRetries = i["AutoReconnectRetries"].toInt();
725 info.unlimitedReconnectRetries = i["UnlimitedReconnectRetries"].toBool();
726 info.rejoinChannels = i["RejoinChannels"].toBool();
730 QDebug operator<<(QDebug dbg, const NetworkInfo &i) {
731 dbg.nospace() << "(id = " << i.networkId << " name = " << i.networkName << " identity = " << i.identity
732 << " codecForServer = " << i.codecForServer << " codecForEncoding = " << i.codecForEncoding << " codecForDecoding = " << i.codecForDecoding
733 << " serverList = " << i.serverList << " useRandomServer = " << i.useRandomServer << " perform = " << i.perform
734 << " useAutoIdentify = " << i.useAutoIdentify << " autoIdentifyService = " << i.autoIdentifyService << " autoIdentifyPassword = " << i.autoIdentifyPassword
735 << " useAutoReconnect = " << i.useAutoReconnect << " autoReconnectInterval = " << i.autoReconnectInterval
736 << " autoReconnectRetries = " << i.autoReconnectRetries << " unlimitedReconnectRetries = " << i.unlimitedReconnectRetries
737 << " rejoinChannels = " << i.rejoinChannels << ")";