: CoreBasicHandler(parent),
_whois(false)
{
- connect(parent, SIGNAL(disconnected(NetworkId)), this, SLOT(destroyNetsplits()));
+
}
IrcServerHandler::~IrcServerHandler() {
- destroyNetsplits();
+
}
/*! Handle a raw message string sent by the server. We try to find a suitable handler, otherwise we call a default handler. */
// IRC SERVER HANDLER
//******************************/
-void IrcServerHandler::handleJoin(const QString &prefix, const QList<QByteArray> ¶ms) {
- if(!checkParamCount("IrcServerHandler::handleJoin()", params, 1))
- return;
-
- QString channel = serverDecode(params[0]);
- IrcUser *ircuser = network()->updateNickFromMask(prefix);
-
- bool handledByNetsplit = false;
- if(!_netsplits.empty()) {
- foreach(Netsplit* n, _netsplits) {
- handledByNetsplit = n->userJoined(prefix, channel);
- if(handledByNetsplit)
- break;
- }
- }
-
- // normal join
- if(!handledByNetsplit) {
- emit displayMsg(Message::Join, BufferInfo::ChannelBuffer, channel, channel, prefix);
- ircuser->joinChannel(channel);
- }
- //qDebug() << "IrcServerHandler::handleJoin()" << prefix << params;
-
- if(network()->isMe(ircuser)) {
- network()->setChannelJoined(channel);
- putCmd("MODE", params[0]); // we want to know the modes of the channel we just joined, so we ask politely
- }
-}
-
-void IrcServerHandler::handleMode(const QString &prefix, const QList<QByteArray> ¶ms) {
- if(!checkParamCount("IrcServerHandler::handleMode()", params, 2))
- return;
-
- if(network()->isChannelName(serverDecode(params[0]))) {
- // Channel Modes
- emit displayMsg(Message::Mode, BufferInfo::ChannelBuffer, serverDecode(params[0]), serverDecode(params).join(" "), prefix);
-
- IrcChannel *channel = network()->ircChannel(params[0]);
- if(!channel) {
- // we received mode information for a channel we're not in. that means probably we've just been kicked out or something like that
- // anyways: we don't have a place to store the data --> discard the info.
- return;
- }
-
- QString modes = params[1];
- bool add = true;
- int paramOffset = 2;
- for(int c = 0; c < modes.length(); c++) {
- if(modes[c] == '+') {
- add = true;
- continue;
- }
- if(modes[c] == '-') {
- add = false;
- continue;
- }
-
- if(network()->prefixModes().contains(modes[c])) {
- // user channel modes (op, voice, etc...)
- if(paramOffset < params.count()) {
- IrcUser *ircUser = network()->ircUser(params[paramOffset]);
- if(!ircUser) {
- qWarning() << Q_FUNC_INFO << "Unknown IrcUser:" << params[paramOffset];
- } else {
- if(add) {
- bool handledByNetsplit = false;
- if(!_netsplits.empty()) {
- foreach(Netsplit* n, _netsplits) {
- handledByNetsplit = n->userAlreadyJoined(ircUser->hostmask(), channel->name());
- if(handledByNetsplit) {
- n->addMode(ircUser->hostmask(), channel->name(), QString(modes[c]));
- break;
- }
- }
- }
- if(!handledByNetsplit)
- channel->addUserMode(ircUser, QString(modes[c]));
- }
- else
- channel->removeUserMode(ircUser, QString(modes[c]));
- }
- } else {
- qWarning() << "Received MODE with too few parameters:" << serverDecode(params);
- }
- paramOffset++;
- } else {
- // regular channel modes
- QString value;
- Network::ChannelModeType modeType = network()->channelModeType(modes[c]);
- if(modeType == Network::A_CHANMODE || modeType == Network::B_CHANMODE || (modeType == Network::C_CHANMODE && add)) {
- if(paramOffset < params.count()) {
- value = params[paramOffset];
- } else {
- qWarning() << "Received MODE with too few parameters:" << serverDecode(params);
- }
- paramOffset++;
- }
-
- if(add)
- channel->addChannelMode(modes[c], value);
- else
- channel->removeChannelMode(modes[c], value);
- }
- }
-
- } else {
- // pure User Modes
- IrcUser *ircUser = network()->newIrcUser(params[0]);
- QString modeString(serverDecode(params[1]));
- QString addModes;
- QString removeModes;
- bool add = false;
- for(int c = 0; c < modeString.count(); c++) {
- if(modeString[c] == '+') {
- add = true;
- continue;
- }
- if(modeString[c] == '-') {
- add = false;
- continue;
- }
- if(add)
- addModes += modeString[c];
- else
- removeModes += modeString[c];
- }
- if(!addModes.isEmpty())
- ircUser->addUserModes(addModes);
- if(!removeModes.isEmpty())
- ircUser->removeUserModes(removeModes);
-
- if(network()->isMe(ircUser)) {
- network()->updatePersistentModes(addModes, removeModes);
- }
-
- // FIXME: redirect
- emit displayMsg(Message::Mode, BufferInfo::StatusBuffer, "", serverDecode(params).join(" "), prefix);
- }
-}
-
void IrcServerHandler::handleNotice(const QString &prefix, const QList<QByteArray> ¶ms) {
if(!checkParamCount("IrcServerHandler::handleNotice()", params, 2))
return;
}
-void IrcServerHandler::handlePing(const QString &prefix, const QList<QByteArray> ¶ms) {
- Q_UNUSED(prefix);
- putCmd("PONG", params);
-}
-
void IrcServerHandler::handlePrivmsg(const QString &prefix, const QList<QByteArray> ¶ms) {
if(!checkParamCount("IrcServerHandler::handlePrivmsg()", params, 1))
return;
}
}
-void IrcServerHandler::handleQuit(const QString &prefix, const QList<QByteArray> ¶ms) {
- IrcUser *ircuser = network()->updateNickFromMask(prefix);
- if(!ircuser) return;
-
- QString msg;
- if(params.count() > 0)
- msg = userDecode(ircuser->nick(), params[0]);
-
- // check if netsplit
- if(Netsplit::isNetsplit(msg)) {
- Netsplit *n;
- if(!_netsplits.contains(msg)) {
- n = new Netsplit();
- connect(n, SIGNAL(finished()), this, SLOT(handleNetsplitFinished()));
- connect(n, SIGNAL(netsplitJoin(const QString&, const QStringList&, const QStringList&, const QString&)),
- this, SLOT(handleNetsplitJoin(const QString&, const QStringList&, const QStringList&, const QString&)));
- connect(n, SIGNAL(netsplitQuit(const QString&, const QStringList&, const QString&)),
- this, SLOT(handleNetsplitQuit(const QString&, const QStringList&, const QString&)));
- connect(n, SIGNAL(earlyJoin(const QString&, const QStringList&, const QStringList&)),
- this, SLOT(handleEarlyNetsplitJoin(const QString&, const QStringList&, const QStringList&)));
- _netsplits.insert(msg, n);
- }
- else {
- n = _netsplits[msg];
- }
- // add this user to the netsplit
- n->userQuit(prefix, ircuser->channels(),msg);
- }
- // normal quit
- else {
- foreach(QString channel, ircuser->channels())
- emit displayMsg(Message::Quit, BufferInfo::ChannelBuffer, channel, msg, prefix);
- ircuser->quit();
- }
-}
-
-/* RPL_ISUPPORT */
-// TODO Complete 005 handling, also use sensible defaults for non-sent stuff
-void IrcServerHandler::handle005(const QString &prefix, const QList<QByteArray> ¶ms) {
- Q_UNUSED(prefix);
- const int numParams = params.size();
- if(numParams == 0) {
- emit displayMsg(Message::Error, BufferInfo::StatusBuffer, "", tr("Received RPL_ISUPPORT (005) without parameters!"), prefix);
- return;
- }
-
- emit displayMsg(Message::Server, BufferInfo::StatusBuffer, "", serverDecode(params).join(" "), prefix);
-
- QString rpl_isupport_suffix = serverDecode(params.last());
- if(!rpl_isupport_suffix.toLower().contains("are supported by this server")) {
- emit displayMsg(Message::Error, BufferInfo::StatusBuffer, "", tr("Received non RFC compliant RPL_ISUPPORT: this can lead to unexpected behavior!"), prefix);
- }
-
- QString rawSupport;
- QString key, value;
- for(int i = 0; i < numParams - 1; i++) {
- QString rawSupport = serverDecode(params[i]);
- QString key = rawSupport.section("=", 0, 0);
- QString value = rawSupport.section("=", 1);
- network()->addSupport(key, value);
- }
-
- /* determine our prefixes here to get an accurate result */
- network()->determinePrefixes();
-}
-
-/* RPL_LIST - "<channel> <# visible> :<topic>" */
-void IrcServerHandler::handle322(const QString &prefix, const QList<QByteArray> ¶ms) {
- Q_UNUSED(prefix)
- QString channelName;
- quint32 userCount = 0;
- QString topic;
-
- int paramCount = params.count();
- switch(paramCount) {
- case 3:
- topic = serverDecode(params[2]);
- case 2:
- userCount = serverDecode(params[1]).toUInt();
- case 1:
- channelName = serverDecode(params[0]);
- default:
- break;
- }
- if(!coreSession()->ircListHelper()->addChannel(network()->networkId(), channelName, userCount, topic))
- emit displayMsg(Message::Server, BufferInfo::StatusBuffer, "", tr("Channel %1 has %2 users. Topic is: %3").arg(channelName).arg(userCount).arg(topic));
-}
-
-/* RPL_LISTEND ":End of LIST" */
-void IrcServerHandler::handle323(const QString &prefix, const QList<QByteArray> ¶ms) {
- Q_UNUSED(prefix)
- Q_UNUSED(params)
-
- if(!coreSession()->ircListHelper()->endOfChannelList(network()->networkId()))
- emit displayMsg(Message::Server, BufferInfo::StatusBuffer, "", tr("End of channel list"));
-}
-
-/* RPL_CHANNELMODEIS - "<channel> <mode> <mode params>" */
-void IrcServerHandler::handle324(const QString &prefix, const QList<QByteArray> ¶ms) {
- Q_UNUSED(prefix);
- handleMode(prefix, params);
-}
-
-/* RPL_??? - "<channel> <homepage> */
-void IrcServerHandler::handle328(const QString &prefix, const QList<QByteArray> ¶ms) {
- Q_UNUSED(prefix);
- if(!checkParamCount("IrcServerHandler::handle328()", params, 2))
- return;
-
- QString channel = serverDecode(params[0]);
- QString homepage = serverDecode(params[1]);
-
- emit displayMsg(Message::Server, BufferInfo::ChannelBuffer, channel, tr("Homepage for %1 is %2").arg(channel, homepage));
-}
-
-
-/* RPL_??? - "<channel> <creation time (unix)>" */
-void IrcServerHandler::handle329(const QString &prefix, const QList<QByteArray> ¶ms) {
- Q_UNUSED(prefix);
- if(!checkParamCount("IrcServerHandler::handle329()", params, 2))
- return;
-
- QString channel = serverDecode(params[0]);
- uint unixtime = params[1].toUInt();
- if(!unixtime) {
- qWarning() << Q_FUNC_INFO << "received invalid timestamp:" << params[1];
- return;
- }
- QDateTime time = QDateTime::fromTime_t(unixtime);
-
- emit displayMsg(Message::Server, BufferInfo::ChannelBuffer, channel, tr("Channel %1 created on %2").arg(channel, time.toString()));
-}
-
-/* RPL_NOTOPIC */
-void IrcServerHandler::handle331(const QString &prefix, const QList<QByteArray> ¶ms) {
- Q_UNUSED(prefix);
- if(!checkParamCount("IrcServerHandler::handle331()", params, 1))
- return;
-
- QString channel = serverDecode(params[0]);
- IrcChannel *chan = network()->ircChannel(channel);
- if(chan)
- chan->setTopic(QString());
-
- emit displayMsg(Message::Topic, BufferInfo::ChannelBuffer, channel, tr("No topic is set for %1.").arg(channel));
-}
-
-/* RPL_TOPIC */
-void IrcServerHandler::handle332(const QString &prefix, const QList<QByteArray> ¶ms) {
- Q_UNUSED(prefix);
- if(!checkParamCount("IrcServerHandler::handle332()", params, 2))
- return;
-
- QString channel = serverDecode(params[0]);
- QByteArray rawTopic = params[1];
-#ifdef HAVE_QCA2
- rawTopic = decrypt(channel, rawTopic, true);
-#endif
- QString topic = channelDecode(channel, rawTopic);
-
- IrcChannel *chan = network()->ircChannel(channel);
- if(chan)
- chan->setTopic(topic);
-
- emit displayMsg(Message::Topic, BufferInfo::ChannelBuffer, channel, tr("Topic for %1 is \"%2\"").arg(channel, topic));
-}
-
-/* Topic set by... */
-void IrcServerHandler::handle333(const QString &prefix, const QList<QByteArray> ¶ms) {
- Q_UNUSED(prefix);
- if(!checkParamCount("IrcServerHandler::handle333()", params, 3))
- return;
-
- QString channel = serverDecode(params[0]);
- emit displayMsg(Message::Topic, BufferInfo::ChannelBuffer, channel,
- tr("Topic set by %1 on %2") .arg(serverDecode(params[1]), QDateTime::fromTime_t(channelDecode(channel, params[2]).toUInt()).toString()));
-}
-
-/* RPL_INVITING - "<nick> <channel>*/
-void IrcServerHandler::handle341(const QString &prefix, const QList<QByteArray> ¶ms) {
- Q_UNUSED(prefix);
- if(!checkParamCount("IrcServerHandler::handle341()", params, 2))
- return;
-
- QString nick = serverDecode(params[0]);
-
- IrcChannel *channel = network()->ircChannel(serverDecode(params[1]));
- if(!channel) {
- qWarning() << "IrcServerHandler::handle341(): unknown channel:" << params[1];
- return;
- }
-
- emit displayMsg(Message::Server, BufferInfo::ChannelBuffer, channel->name(), tr("%1 has been invited to %2").arg(nick).arg(channel->name()));
-}
-
-/* RPL_WHOREPLY: "<channel> <user> <host> <server> <nick>
- ( "H" / "G" > ["*"] [ ( "@" / "+" ) ] :<hopcount> <real name>" */
-void IrcServerHandler::handle352(const QString &prefix, const QList<QByteArray> ¶ms) {
- Q_UNUSED(prefix)
- if(!checkParamCount("IrcServerHandler::handle352()", params, 6))
- return;
-
- QString channel = serverDecode(params[0]);
- IrcUser *ircuser = network()->ircUser(serverDecode(params[4]));
- if(ircuser) {
- ircuser->setUser(serverDecode(params[1]));
- ircuser->setHost(serverDecode(params[2]));
-
- bool away = serverDecode(params[5]).startsWith("G") ? true : false;
- ircuser->setAway(away);
- ircuser->setServer(serverDecode(params[3]));
- ircuser->setRealName(serverDecode(params.last()).section(" ", 1));
- }
-
- if(!network()->isAutoWhoInProgress(channel)) {
- emit displayMsg(Message::Server, BufferInfo::StatusBuffer, "", tr("[Who] %1").arg(serverDecode(params).join(" ")));
- }
-}
-
-/* RPL_NAMREPLY */
-void IrcServerHandler::handle353(const QString &prefix, const QList<QByteArray> ¶ms) {
- Q_UNUSED(prefix);
- if(!checkParamCount("IrcServerHandler::handle353()", params, 3))
- return;
-
- // param[0] is either "=", "*" or "@" indicating a public, private or secret channel
- // we don't use this information at the time beeing
- QString channelname = serverDecode(params[1]);
-
- IrcChannel *channel = network()->ircChannel(channelname);
- if(!channel) {
- qWarning() << "IrcServerHandler::handle353(): received unknown target channel:" << channelname;
- return;
- }
-
- QStringList nicks;
- QStringList modes;
-
- foreach(QString nick, serverDecode(params[2]).split(' ')) {
- QString mode = QString();
-
- if(network()->prefixes().contains(nick[0])) {
- mode = network()->prefixToMode(nick[0]);
- nick = nick.mid(1);
- }
-
- nicks << nick;
- modes << mode;
- }
-
- channel->joinIrcUsers(nicks, modes);
-}
-
-/* RPL_ENDOFWHOWAS - "<nick> :End of WHOWAS" */
-void IrcServerHandler::handle369(const QString &prefix, const QList<QByteArray> ¶ms) {
- Q_UNUSED(prefix)
- emit displayMsg(Message::Server, BufferInfo::StatusBuffer, "", tr("[Whowas] %1").arg(serverDecode(params).join(" ")));
-}
-
-/* ERR_ERRONEUSNICKNAME */
-void IrcServerHandler::handle432(const QString &prefix, const QList<QByteArray> ¶ms) {
- Q_UNUSED(prefix);
-
- QString errnick;
- if(params.size() < 2) {
- // handle unreal-ircd bug, where unreal ircd doesnt supply a TARGET in ERR_ERRONEUSNICKNAME during registration phase:
- // nick @@@
- // :irc.scortum.moep.net 432 @@@ :Erroneous Nickname: Illegal characters
- // correct server reply:
- // :irc.scortum.moep.net 432 * @@@ :Erroneous Nickname: Illegal characters
- errnick = target();
- } else {
- errnick = params[0];
- }
- emit displayMsg(Message::Error, BufferInfo::StatusBuffer, "", tr("Nick %1 contains illegal characters").arg(errnick));
- tryNextNick(errnick, true /* erroneus */);
-}
-
-/* ERR_NICKNAMEINUSE */
-void IrcServerHandler::handle433(const QString &prefix, const QList<QByteArray> ¶ms) {
- Q_UNUSED(prefix);
- if(!checkParamCount("IrcServerHandler::handle433()", params, 1))
- return;
-
- QString errnick = serverDecode(params[0]);
- emit displayMsg(Message::Error, BufferInfo::StatusBuffer, "", tr("Nick already in use: %1").arg(errnick));
-
- // if there is a problem while connecting to the server -> we handle it
- // but only if our connection has not been finished yet...
- if(!network()->currentServer().isEmpty())
- return;
-
- tryNextNick(errnick);
-}
-
-/* ERR_UNAVAILRESOURCE */
-void IrcServerHandler::handle437(const QString &prefix, const QList<QByteArray> ¶ms) {
- Q_UNUSED(prefix);
- if(!checkParamCount("IrcServerHandler::handle437()", params, 1))
- return;
-
- QString errnick = serverDecode(params[0]);
- emit displayMsg(Message::Error, BufferInfo::StatusBuffer, "", tr("Nick/channel is temporarily unavailable: %1").arg(errnick));
-
- // if there is a problem while connecting to the server -> we handle it
- // but only if our connection has not been finished yet...
- if(!network()->currentServer().isEmpty())
- return;
-
- if(!network()->isChannelName(errnick))
- tryNextNick(errnick);
-}
-
-/* Handle signals from Netsplit objects */
-
-void IrcServerHandler::handleNetsplitJoin(const QString &channel, const QStringList &users, const QStringList &modes, const QString& quitMessage)
-{
- IrcChannel *ircChannel = network()->ircChannel(channel);
- if(!ircChannel) {
- return;
- }
- QList<IrcUser *> ircUsers;
- QStringList newModes = modes;
- QStringList newUsers = users;
-
- foreach(QString user, users) {
- IrcUser *iu = network()->ircUser(nickFromMask(user));
- if(iu)
- ircUsers.append(iu);
- else { // the user already quit
- int idx = users.indexOf(user);
- newUsers.removeAt(idx);
- newModes.removeAt(idx);
- }
- }
-
- QString msg = newUsers.join("#:#").append("#:#").append(quitMessage);
- emit displayMsg(Message::NetsplitJoin, BufferInfo::ChannelBuffer, channel, msg);
- ircChannel->joinIrcUsers(ircUsers, newModes);
-}
-
-void IrcServerHandler::handleNetsplitQuit(const QString &channel, const QStringList &users, const QString& quitMessage)
-{
- QString msg = users.join("#:#").append("#:#").append(quitMessage);
- emit displayMsg(Message::NetsplitQuit, BufferInfo::ChannelBuffer, channel, msg);
- foreach(QString user, users) {
- IrcUser *iu = network()->ircUser(nickFromMask(user));
- if(iu)
- iu->quit();
- }
-}
-
-void IrcServerHandler::handleEarlyNetsplitJoin(const QString &channel, const QStringList &users, const QStringList &modes) {
- IrcChannel *ircChannel = network()->ircChannel(channel);
- if(!ircChannel) {
- qDebug() << "handleEarlyNetsplitJoin(): channel " << channel << " invalid";
- return;
- }
- QList<IrcUser *> ircUsers;
- QStringList newModes = modes;
-
- foreach(QString user, users) {
- IrcUser *iu = network()->updateNickFromMask(user);
- if(iu) {
- ircUsers.append(iu);
- emit displayMsg(Message::Join, BufferInfo::ChannelBuffer, channel, channel, user);
- }
- else {
- newModes.removeAt(users.indexOf(user));
- }
- }
- ircChannel->joinIrcUsers(ircUsers, newModes);
-}
-void IrcServerHandler::handleNetsplitFinished()
-{
- Netsplit* n = qobject_cast<Netsplit*>(sender());
- _netsplits.remove(_netsplits.key(n));
- n->deleteLater();
-}
-
-/* */
-
// FIXME networkConnection()->setChannelKey("") for all ERR replies indicating that a JOIN went wrong
// mostly, these are codes in the 47x range
}
}
-void IrcServerHandler::destroyNetsplits() {
- qDeleteAll(_netsplits);
- _netsplits.clear();
-}
-
#ifdef HAVE_QCA2
QByteArray IrcServerHandler::decrypt(const QString &bufferName, const QByteArray &message_, bool isTopic) {
if(message_.isEmpty())