X-Git-Url: https://git.quassel-irc.org/?p=quassel.git;a=blobdiff_plain;f=src%2Fcommon%2Fircchannel.cpp;h=5c2ba7221f303fcb389cbf94c30b57cc72147f6e;hp=906f4f0ae92e335e99bf1f0756597f12a7e3b4cf;hb=79fbcfb49f7cc92a89f0158ebac1a3006a559e8a;hpb=7795adca52f35204f8c354da6fcc5d8e8ee35531 diff --git a/src/common/ircchannel.cpp b/src/common/ircchannel.cpp index 906f4f0a..5c2ba722 100644 --- a/src/common/ircchannel.cpp +++ b/src/common/ircchannel.cpp @@ -1,11 +1,11 @@ /*************************************************************************** - * Copyright (C) 2005-07 by The Quassel Team * + * Copyright (C) 2005-09 by the Quassel Project * * devel@quassel-irc.org * * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; either version 2 of the License, or * - * (at your option) any later version. * + * (at your option) version 3. * * * * This program is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * @@ -20,47 +20,47 @@ #include "ircchannel.h" -#include "networkinfo.h" -#include "signalproxy.h" +#include "network.h" #include "ircuser.h" +#include "util.h" #include #include +#include #include -IrcChannel::IrcChannel(const QString &channelname, NetworkInfo *networkinfo) - : QObject(networkinfo), +IrcChannel::IrcChannel(const QString &channelname, Network *network) + : SyncableObject(network), _initialized(false), _name(channelname), _topic(QString()), - networkInfo(networkinfo) + network(network), + _codecForEncoding(0), + _codecForDecoding(0) { - setObjectName(QString::number(networkInfo->networkId()) + "/" + channelname); + setObjectName(QString::number(network->networkId().toInt()) + "/" + channelname); } IrcChannel::~IrcChannel() { - } // ==================== // PUBLIC: // ==================== bool IrcChannel::isKnownUser(IrcUser *ircuser) const { - bool isknown = true; - if(ircuser == 0) { qWarning() << "Channel" << name() << "received IrcUser Nullpointer!"; - isknown = false; + return false; } - if(!_userModes.contains(ircuser) && ircuser) { + if(!_userModes.contains(ircuser)) { qWarning() << "Channel" << name() << "received data for unknown User" << ircuser->nick(); - isknown = false; + return false; } - - return isknown; + + return true; } bool IrcChannel::isValidChannelUserMode(const QString &mode) const { @@ -72,31 +72,43 @@ bool IrcChannel::isValidChannelUserMode(const QString &mode) const { return isvalid; } -bool IrcChannel::initialized() const { - return _initialized; +QString IrcChannel::userModes(IrcUser *ircuser) const { + if(_userModes.contains(ircuser)) + return _userModes[ircuser]; + else + return QString(); } -QString IrcChannel::name() const { - return _name; +QString IrcChannel::userModes(const QString &nick) const { + return userModes(network->ircUser(nick)); } -QString IrcChannel::topic() const { - return _topic; +void IrcChannel::setCodecForEncoding(const QString &name) { + setCodecForEncoding(QTextCodec::codecForName(name.toAscii())); } -QList IrcChannel::ircUsers() const { - return _userModes.keys(); +void IrcChannel::setCodecForEncoding(QTextCodec *codec) { + _codecForEncoding = codec; } -QString IrcChannel::userMode(IrcUser *ircuser) const { - if(_userModes.contains(ircuser)) - return _userModes[ircuser]; - else - return QString(); +void IrcChannel::setCodecForDecoding(const QString &name) { + setCodecForDecoding(QTextCodec::codecForName(name.toAscii())); } -QString IrcChannel::userMode(const QString &nick) const { - return userMode(networkInfo->ircUser(nick)); +void IrcChannel::setCodecForDecoding(QTextCodec *codec) { + _codecForDecoding = codec; +} + +QString IrcChannel::decodeString(const QByteArray &text) const { + if(!codecForDecoding()) return network->decodeString(text); + return ::decodeString(text, _codecForDecoding); +} + +QByteArray IrcChannel::encodeString(const QString &string) const { + if(codecForEncoding()) { + return _codecForEncoding->fromUnicode(string); + } + return network->encodeString(string); } // ==================== @@ -107,33 +119,96 @@ void IrcChannel::setTopic(const QString &topic) { emit topicSet(topic); } -void IrcChannel::join(IrcUser *ircuser) { - if(!_userModes.contains(ircuser) && ircuser) { - _userModes[ircuser] = QString(); - ircuser->joinChannel(name()); - connect(ircuser, SIGNAL(destroyed()), this, SLOT(ircUserDestroyed())); +void IrcChannel::setPassword(const QString &password) { + _password = password; + emit passwordSet(password); +} + +void IrcChannel::joinIrcUsers(const QList &users, const QStringList &modes) { + if(users.isEmpty()) + return; + + if(users.count() != modes.count()) { + qWarning() << "IrcChannel::addUsers(): number of nicks does not match number of modes!"; + return; + } + + QStringList newNicks; + QStringList newModes; + QList newUsers; + + IrcUser *ircuser; + for(int i = 0; i < users.count(); i++) { + ircuser = users[i]; + if(!ircuser || _userModes.contains(ircuser)) { + addUserMode(ircuser, modes[i]); + continue; + } + + _userModes[ircuser] = modes[i]; + ircuser->joinChannel(this); + connect(ircuser, SIGNAL(nickSet(QString)), this, SLOT(ircUserNickSet(QString))); + // connect(ircuser, SIGNAL(destroyed()), this, SLOT(ircUserDestroyed())); // if you wonder why there is no counterpart to ircUserJoined: // the joines are propagted by the ircuser. the signal ircUserJoined is only for convenience - emit ircUserJoined(ircuser); + + newNicks << ircuser->nick(); + newModes << modes[i]; + newUsers << ircuser; } + + if(newNicks.isEmpty()) + return; + + emit ircUsersJoined(newUsers); + emit ircUsersJoined(newNicks, newModes); } -void IrcChannel::join(const QString &nick) { - join(networkInfo->ircUser(nick)); +void IrcChannel::joinIrcUsers(const QStringList &nicks, const QStringList &modes) { + QList users; + foreach(QString nick, nicks) + users << network->newIrcUser(nick); + joinIrcUsers(users, modes); +} + +void IrcChannel::joinIrcUsers(IrcUser *ircuser) { + QList users; + users << ircuser; + QStringList modes; + modes << QString(); + joinIrcUsers(users, modes); +} + +void IrcChannel::joinIrcUsers(const QString &nick) { + joinIrcUsers(network->newIrcUser(nick)); } void IrcChannel::part(IrcUser *ircuser) { if(isKnownUser(ircuser)) { _userModes.remove(ircuser); - ircuser->partChannel(name()); + ircuser->partChannel(this); // if you wonder why there is no counterpart to ircUserParted: // the joines are propagted by the ircuser. the signal ircUserParted is only for convenience + disconnect(ircuser, 0, this, 0); emit ircUserParted(ircuser); + + if(network->isMe(ircuser) || _userModes.isEmpty()) { + // in either case we're no longer in the channel + // -> clean up the channel and destroy it + QList users = _userModes.keys(); + _userModes.clear(); + foreach(IrcUser *user, users) { + disconnect(user, 0, this, 0); + user->partChannel(this); + } + emit parted(); + network->removeIrcChannel(this); + } } } void IrcChannel::part(const QString &nick) { - part(networkInfo->ircUser(nick)); + part(network->ircUser(nick)); } // SET USER MODE @@ -141,30 +216,31 @@ void IrcChannel::setUserModes(IrcUser *ircuser, const QString &modes) { if(isKnownUser(ircuser)) { _userModes[ircuser] = modes; emit userModesSet(ircuser->nick(), modes); + emit ircUserModesSet(ircuser, modes); } } void IrcChannel::setUserModes(const QString &nick, const QString &modes) { - setUserModes(networkInfo->ircUser(nick), modes); + setUserModes(network->ircUser(nick), modes); } // ADD USER MODE void IrcChannel::addUserMode(IrcUser *ircuser, const QString &mode) { if(!isKnownUser(ircuser) || !isValidChannelUserMode(mode)) return; - + if(!_userModes[ircuser].contains(mode)) { _userModes[ircuser] += mode; emit userModeAdded(ircuser->nick(), mode); + emit ircUserModeAdded(ircuser, mode); } } void IrcChannel::addUserMode(const QString &nick, const QString &mode) { - addUserMode(networkInfo->ircUser(nick), mode); + addUserMode(network->ircUser(nick), mode); } - // REMOVE USER MODE void IrcChannel::removeUserMode(IrcUser *ircuser, const QString &mode) { if(!isKnownUser(ircuser) || !isValidChannelUserMode(mode)) @@ -173,12 +249,12 @@ void IrcChannel::removeUserMode(IrcUser *ircuser, const QString &mode) { if(_userModes[ircuser].contains(mode)) { _userModes[ircuser].remove(mode); emit userModeRemoved(ircuser->nick(), mode); + emit ircUserModeRemoved(ircuser, mode); } - } void IrcChannel::removeUserMode(const QString &nick, const QString &mode) { - removeUserMode(networkInfo->ircUser(nick), mode); + removeUserMode(network->ircUser(nick), mode); } // INIT SET USER MODES @@ -193,21 +269,274 @@ QVariantMap IrcChannel::initUserModes() const { } void IrcChannel::initSetUserModes(const QVariantMap &usermodes) { - QMapIterator iter(usermodes); - while(iter.hasNext()) { - iter.next(); - setUserModes(iter.key(), iter.value().toString()); + QList users; + QStringList modes; + QVariantMap::const_iterator iter = usermodes.constBegin(); + while(iter != usermodes.constEnd()) { + users << network->newIrcUser(iter.key()); + modes << iter.value().toString(); + iter++; + } + joinIrcUsers(users, modes); +} + +QVariantMap IrcChannel::initChanModes() const { + QVariantMap channelModes; + + QVariantMap A_modes; + QHash::const_iterator A_iter = _A_channelModes.constBegin(); + while(A_iter != _A_channelModes.constEnd()) { + A_modes[A_iter.key()] = A_iter.value(); + A_iter++; + } + channelModes["A"] = A_modes; + + QVariantMap B_modes; + QHash::const_iterator B_iter = _B_channelModes.constBegin(); + while(B_iter != _B_channelModes.constEnd()) { + B_modes[B_iter.key()] = B_iter.value(); + B_iter++; + } + channelModes["B"] = B_modes; + + QVariantMap C_modes; + QHash::const_iterator C_iter = _C_channelModes.constBegin(); + while(C_iter != _C_channelModes.constEnd()) { + C_modes[C_iter.key()] = C_iter.value(); + C_iter++; + } + channelModes["C"] = C_modes; + + QString D_modes; + QSet::const_iterator D_iter = _D_channelModes.constBegin(); + while(D_iter != _D_channelModes.constEnd()) { + D_modes += *D_iter; + D_iter++; } + channelModes["D"] = D_modes; + + return channelModes; +} + +void IrcChannel::initSetChanModes(const QVariantMap &channelModes) { + QVariantMap::const_iterator iter = channelModes["A"].toMap().constBegin(); + QVariantMap::const_iterator iterEnd = channelModes["A"].toMap().constEnd(); + while(iter != iterEnd) { + _A_channelModes[iter.key()[0]] = iter.value().toStringList(); + iter++; + } + + iter = channelModes["B"].toMap().constBegin(); + iterEnd = channelModes["B"].toMap().constEnd(); + while(iter != iterEnd) { + _B_channelModes[iter.key()[0]] = iter.value().toString(); + iter++; + } + + iter = channelModes["C"].toMap().constBegin(); + iterEnd = channelModes["C"].toMap().constEnd(); + while(iter != iterEnd) { + _C_channelModes[iter.key()[0]] = iter.value().toString(); + iter++; + } + + QString D_modes = channelModes["D"].toString(); + for(int i = 0; i < D_modes.count(); i++) { + _D_channelModes << D_modes[i]; + } + } void IrcChannel::ircUserDestroyed() { IrcUser *ircUser = static_cast(sender()); Q_ASSERT(ircUser); _userModes.remove(ircUser); + // no further propagation. + // this leads only to fuck ups. } -void IrcChannel::setInitialized() { - _initialized = true; - emit initDone(); +void IrcChannel::ircUserNickSet(QString nick) { + IrcUser *ircUser = qobject_cast(sender()); + Q_ASSERT(ircUser); + emit ircUserNickSet(ircUser, nick); } +/******************************************************************************* + * + * 3.3 CHANMODES + * + * o CHANMODES=A,B,C,D + * + * The CHANMODES token specifies the modes that may be set on a channel. + * These modes are split into four categories, as follows: + * + * o Type A: Modes that add or remove an address to or from a list. + * These modes always take a parameter when sent by the server to a + * client; when sent by a client, they may be specified without a + * parameter, which requests the server to display the current + * contents of the corresponding list on the channel to the client. + * o Type B: Modes that change a setting on the channel. These modes + * always take a parameter. + * o Type C: Modes that change a setting on the channel. These modes + * take a parameter only when set; the parameter is absent when the + * mode is removed both in the client's and server's MODE command. + * o Type D: Modes that change a setting on the channel. These modes + * never take a parameter. + * + * If the server sends any additional types after these 4, the client + * MUST ignore them; this is intended to allow future extension of this + * token. + * + * The IRC server MUST NOT list modes in CHANMODES which are also + * present in the PREFIX parameter; however, for completeness, modes + * described in PREFIX may be treated as type B modes. + * + ******************************************************************************/ + + +/******************************************************************************* + * Short Version: + * A --> add/remove from List + * B --> set value or remove + * C --> set value or remove + * D --> on/off + * + * B and C behave very similar... we store the data in different datastructes + * for future compatibility + ******************************************************************************/ + +// NOTE: the behavior of addChannelMode and removeChannelMode depends on the type of mode +// see list above for chanmode types +void IrcChannel::addChannelMode(const QChar &mode, const QString &value) { + Network::ChannelModeType modeType = network->channelModeType(mode); + + switch(modeType) { + case Network::NOT_A_CHANMODE: + return; + case Network::A_CHANMODE: + if(!_A_channelModes.contains(mode)) + _A_channelModes[mode] = QStringList(value); + else if(!_A_channelModes[mode].contains(value)) + _A_channelModes[mode] << value; + break; + + case Network::B_CHANMODE: + _B_channelModes[mode] = value; + break; + + case Network::C_CHANMODE: + _C_channelModes[mode] = value; + break; + + case Network::D_CHANMODE: + _D_channelModes << mode; + break; + } + emit channelModeAdded(mode, value); +} + +void IrcChannel::removeChannelMode(const QChar &mode, const QString &value) { + Network::ChannelModeType modeType = network->channelModeType(mode); + + switch(modeType) { + case Network::NOT_A_CHANMODE: + return; + case Network::A_CHANMODE: + if(_A_channelModes.contains(mode)) + _A_channelModes[mode].removeAll(value); + break; + + case Network::B_CHANMODE: + _B_channelModes.remove(mode); + break; + + case Network::C_CHANMODE: + _C_channelModes.remove(mode); + break; + + case Network::D_CHANMODE: + _D_channelModes.remove(mode); + break; + } + emit channelModeRemoved(mode, value); +} + +bool IrcChannel::hasMode(const QChar &mode) const { + Network::ChannelModeType modeType = network->channelModeType(mode); + + switch(modeType) { + case Network::NOT_A_CHANMODE: + return false; + case Network::A_CHANMODE: + return _A_channelModes.contains(mode); + case Network::B_CHANMODE: + return _B_channelModes.contains(mode); + case Network::C_CHANMODE: + return _C_channelModes.contains(mode); + case Network::D_CHANMODE: + return _D_channelModes.contains(mode); + default: + return false; + } +} + +QString IrcChannel::modeValue(const QChar &mode) const { + Network::ChannelModeType modeType = network->channelModeType(mode); + + switch(modeType) { + case Network::B_CHANMODE: + if(_B_channelModes.contains(mode)) + return _B_channelModes[mode]; + else + return QString(); + case Network::C_CHANMODE: + if(_C_channelModes.contains(mode)) + return _C_channelModes[mode]; + else + return QString(); + default: + return QString(); + } + +} + +QStringList IrcChannel::modeValueList(const QChar &mode) const { + Network::ChannelModeType modeType = network->channelModeType(mode); + + switch(modeType) { + case Network::A_CHANMODE: + if(_A_channelModes.contains(mode)) + return _A_channelModes[mode]; + default: + return QStringList(); + } +} + +QString IrcChannel::channelModeString() const { + QStringList params; + QString modeString; + + QSet::const_iterator D_iter = _D_channelModes.constBegin(); + while(D_iter != _D_channelModes.constEnd()) { + modeString += *D_iter; + D_iter++; + } + + QHash::const_iterator BC_iter = _C_channelModes.constBegin(); + while(BC_iter != _C_channelModes.constEnd()) { + modeString += BC_iter.key(); + params << BC_iter.value(); + BC_iter++; + } + + BC_iter = _B_channelModes.constBegin(); + while(BC_iter != _B_channelModes.constEnd()) { + modeString += BC_iter.key(); + params << BC_iter.value(); + BC_iter++; + } + if(modeString.isEmpty()) + return modeString; + else + return QString("+%1 %2").arg(modeString).arg(params.join(" ")); +}