/***************************************************************************
- * Copyright (C) 2005-09 by the Quassel Project *
+ * Copyright (C) 2005-2012 by the Quassel Project *
* devel@quassel-irc.org *
* *
* This program is free software; you can redistribute it and/or modify *
#include "util.h"
-#include "ctcphandler.h"
-#include "coreidentity.h"
-#include "ircuser.h"
+#include "ctcpparser.h"
-#include <QDebug>
#include <QRegExp>
+#ifdef HAVE_QCA2
+# include "cipher.h"
+#endif
+
CoreUserInputHandler::CoreUserInputHandler(CoreNetwork *parent)
- : BasicHandler(parent)
+ : CoreBasicHandler(parent)
{
}
// ====================
void CoreUserInputHandler::handleAway(const BufferInfo &bufferInfo, const QString &msg) {
Q_UNUSED(bufferInfo)
+ if(msg.startsWith("-all")) {
+ if(msg.length() == 4) {
+ coreSession()->globalAway();
+ return;
+ }
+ Q_ASSERT(msg.length() > 4);
+ if(msg[4] == ' ') {
+ coreSession()->globalAway(msg.mid(5));
+ return;
+ }
+ }
+ issueAway(msg);
+}
+void CoreUserInputHandler::issueAway(const QString &msg, bool autoCheck) {
QString awayMsg = msg;
IrcUser *me = network()->me();
// if there is no message supplied we have to check if we are already away or not
- if(msg.isEmpty()) {
+ if(autoCheck && msg.isEmpty()) {
if(me && !me->isAway()) {
- awayMsg = network()->identityPtr()->awayReason();
+ Identity *identity = network()->identityPtr();
+ if(identity) {
+ awayMsg = identity->awayReason();
+ }
if(awayMsg.isEmpty()) {
- awayMsg = tr("away");
+ awayMsg = tr("away");
}
}
}
if(ctcpTag.isEmpty())
return;
- QString message = "";
- QString verboseMessage = tr("sending CTCP-%1 request").arg(ctcpTag);
+ QString message = msg.section(' ', 2);
+ QString verboseMessage = tr("sending CTCP-%1 request to %2").arg(ctcpTag).arg(nick);
if(ctcpTag == "PING") {
uint now = QDateTime::currentDateTime().toTime_t();
message = QString::number(now);
}
- network()->ctcpHandler()->query(nick, ctcpTag, message);
+ // FIXME make this a proper event
+ coreNetwork()->coreSession()->ctcpParser()->query(coreNetwork(), nick, ctcpTag, message);
emit displayMsg(Message::Action, BufferInfo::StatusBuffer, "", verboseMessage, network()->myNick());
}
+void CoreUserInputHandler::handleDelkey(const BufferInfo &bufferInfo, const QString &msg) {
+#ifdef HAVE_QCA2
+ if(!bufferInfo.isValid())
+ return;
+
+ if(!Cipher::neededFeaturesAvailable())
+ return;
+
+ QStringList parms = msg.split(' ', QString::SkipEmptyParts);
+
+ if(parms.isEmpty() && !bufferInfo.bufferName().isEmpty())
+ parms.prepend(bufferInfo.bufferName());
+
+ if(parms.isEmpty()) {
+ emit displayMsg(Message::Info, bufferInfo.bufferName(), "",
+ tr("[usage] /delkey <nick|channel> deletes the encryption key for nick or channel or just /delkey when in a channel or query."));
+ return;
+ }
+
+ QString target = parms.at(0);
+
+ if(network()->cipherKey(target).isEmpty()) {
+ emit displayMsg(Message::Info, bufferInfo.bufferName(), tr("No key has been set for %1.").arg(target));
+ return;
+ }
+
+ network()->setCipherKey(target, QByteArray());
+
+ if(network()->isChannelName(target) && network()->channels().contains(target)) {
+ qobject_cast<CoreIrcChannel *>(network()->ircChannel(target))->setEncrypted(false);
+ }
+ else if(network()->nicks().contains(target)) {
+ qobject_cast<CoreIrcUser *>(network()->ircUser(target))->setEncrypted(false);
+ }
+
+ emit displayMsg(Message::Info, bufferInfo.bufferName(), tr("The key for %1 has been deleted.").arg(target));
+
+#else
+ Q_UNUSED(msg)
+ emit displayMsg(Message::Error, bufferInfo.bufferName(), "", tr("Error: Setting an encryption key requires Quassel to have been built "
+ "with support for the Qt Cryptographic Architecture (QCA2) library. "
+ "Contact your distributor about a Quassel package with QCA2 "
+ "support, or rebuild Quassel with QCA2 present."));
+#endif
+}
+
void CoreUserInputHandler::handleDeop(const BufferInfo &bufferInfo, const QString &msg) {
QStringList nicks = msg.split(' ', QString::SkipEmptyParts);
QString m = "-"; for(int i = 0; i < nicks.count(); i++) m += 'o';
QString sane_msg = msg;
sane_msg.replace(QRegExp(", +"), ",");
QStringList params = sane_msg.trimmed().split(" ");
+
QStringList chans = params[0].split(",", QString::SkipEmptyParts);
QStringList keys;
+ if(params.count() > 1)
+ keys = params[1].split(",");
+
int i;
for(i = 0; i < chans.count(); i++) {
if(!network()->isChannelName(chans[i]))
chans[i].prepend('#');
+
+ if(i < keys.count()) {
+ network()->addChannelKey(chans[i], keys[i]);
+ } else {
+ network()->removeChannelKey(chans[i]);
+ }
}
- params[0] = chans.join(",");
- if(params.count() > 1) keys = params[1].split(",");
- emit putCmd("JOIN", serverEncode(params)); // FIXME handle messages longer than 512 bytes!
+
+ static const char *cmd = "JOIN";
i = 0;
- for(; i < keys.count(); i++) {
- if(i >= chans.count()) break;
- network()->addChannelKey(chans[i], keys[i]);
- }
- for(; i < chans.count(); i++) {
- network()->removeChannelKey(chans[i]);
+ QStringList joinChans, joinKeys;
+ int slicesize = chans.count();
+ QList<QByteArray> encodedParams;
+
+ // go through all to-be-joined channels and (re)build the join list
+ while(i < chans.count()) {
+ joinChans.append(chans.at(i));
+ if(i < keys.count())
+ joinKeys.append(keys.at(i));
+
+ // if the channel list we built so far either contains all requested channels or exceeds
+ // the desired amount of channels in this slice, try to send what we have so far
+ if(++i == chans.count() || joinChans.count() >= slicesize) {
+ params.clear();
+ params.append(joinChans.join(","));
+ params.append(joinKeys.join(","));
+ encodedParams = serverEncode(params);
+ // check if it fits in one command
+ if(lastParamOverrun(cmd, encodedParams) == 0) {
+ emit putCmd(cmd, encodedParams);
+ } else if(slicesize > 1) {
+ // back to start of slice, try again with half the amount of channels
+ i -= slicesize;
+ slicesize /= 2;
+ }
+ joinChans.clear();
+ joinKeys.clear();
+ }
}
}
void CoreUserInputHandler::handleMe(const BufferInfo &bufferInfo, const QString &msg) {
if(bufferInfo.bufferName().isEmpty()) return; // server buffer
- network()->ctcpHandler()->query(bufferInfo.bufferName(), "ACTION", msg);
+ // FIXME make this a proper event
+ coreNetwork()->coreSession()->ctcpParser()->query(coreNetwork(), bufferInfo.bufferName(), "ACTION", msg);
emit displayMsg(Message::Action, bufferInfo.type(), bufferInfo.bufferName(), msg, network()->myNick(), Message::Self);
}
QStringList params = msg.split(' ', QString::SkipEmptyParts);
// if the first argument is neither a channel nor us (user modes are only to oneself) the current buffer is assumed to be the target
- if(!params.isEmpty() && !network()->isChannelName(params[0]) && !network()->isMyNick(params[0]))
- params.prepend(bufferInfo.bufferName());
+ if(!params.isEmpty()) {
+ if(!network()->isChannelName(params[0]) && !network()->isMyNick(params[0]))
+ params.prepend(bufferInfo.bufferName());
+ if(network()->isMyNick(params[0]) && params.count() == 2)
+ network()->updateIssuedModes(params[1]);
+ if(params[0] == "-reset" && params.count() == 1) {
+ // FIXME: give feedback to the user (I don't want to add new strings right now)
+ network()->resetPersistentModes();
+ return;
+ }
+ }
// TODO handle correct encoding for buffer modes (channelEncode())
emit putCmd("MODE", serverEncode(params));
if(!msg.contains(' '))
return;
- QByteArray target = serverEncode(msg.section(' ', 0, 0));
- putPrivmsg(target, userEncode(target, msg.section(' ', 1)));
+ QString target = msg.section(' ', 0, 0);
+ QByteArray encMsg = userEncode(target, msg.section(' ', 1));
+
+#ifdef HAVE_QCA2
+ putPrivmsg(serverEncode(target), encMsg, network()->cipher(target));
+#else
+ putPrivmsg(serverEncode(target), encMsg);
+#endif
}
void CoreUserInputHandler::handleNick(const BufferInfo &bufferInfo, const QString &msg) {
QString target = msg.section(' ', 0, 0);
QString message = msg.section(' ', 1);
if(message.isEmpty())
- emit displayMsg(Message::Server, BufferInfo::QueryBuffer, target, "Starting query with " + target, network()->myNick(), Message::Self);
+ emit displayMsg(Message::Server, BufferInfo::QueryBuffer, target, tr("Starting query with %1").arg(target), network()->myNick(), Message::Self);
else
emit displayMsg(Message::Plain, BufferInfo::QueryBuffer, target, message, network()->myNick(), Message::Self);
handleMsg(bufferInfo, msg);
void CoreUserInputHandler::handleSay(const BufferInfo &bufferInfo, const QString &msg) {
if(bufferInfo.bufferName().isEmpty())
return; // server buffer
- putPrivmsg(serverEncode(bufferInfo.bufferName()), channelEncode(bufferInfo.bufferName(), msg));
+
+ QByteArray encMsg = channelEncode(bufferInfo.bufferName(), msg);
+#ifdef HAVE_QCA2
+ putPrivmsg(serverEncode(bufferInfo.bufferName()), encMsg, network()->cipher(bufferInfo.bufferName()));
+#else
+ putPrivmsg(serverEncode(bufferInfo.bufferName()), encMsg);
+#endif
emit displayMsg(Message::Plain, bufferInfo.type(), bufferInfo.bufferName(), msg, network()->myNick(), Message::Self);
}
+void CoreUserInputHandler::handleSetkey(const BufferInfo &bufferInfo, const QString &msg) {
+#ifdef HAVE_QCA2
+ if(!bufferInfo.isValid())
+ return;
+
+ if(!Cipher::neededFeaturesAvailable())
+ return;
+
+ QStringList parms = msg.split(' ', QString::SkipEmptyParts);
+
+ if(parms.count() == 1 && !bufferInfo.bufferName().isEmpty())
+ parms.prepend(bufferInfo.bufferName());
+ else if(parms.count() != 2) {
+ emit displayMsg(Message::Info, bufferInfo.bufferName(),
+ tr("[usage] /setkey <nick|channel> <key> sets the encryption key for nick or channel. "
+ "/setkey <key> when in a channel or query buffer sets the key for it."));
+ return;
+ }
+
+ QString target = parms.at(0);
+ QByteArray key = parms.at(1).toLocal8Bit();
+
+ network()->setCipherKey(target, key);
+
+ if(network()->isChannelName(target) && network()->channels().contains(target))
+ qobject_cast<CoreIrcChannel *>(network()->ircChannel(target))->setEncrypted(true);
+ else if(network()->nicks().contains(target))
+ qobject_cast<CoreIrcUser *>(network()->ircUser(target))->setEncrypted(true);
+
+ emit displayMsg(Message::Info, bufferInfo.bufferName(), tr("The key for %1 has been set.").arg(target));
+#else
+ Q_UNUSED(msg)
+ emit displayMsg(Message::Error, bufferInfo.bufferName(), tr("Error: Setting an encryption key requires Quassel to have been built "
+ "with support for the Qt Cryptographic Architecture (QCA) library. "
+ "Contact your distributor about a Quassel package with QCA "
+ "support, or rebuild Quassel with QCA present."));
+#endif
+}
+
+void CoreUserInputHandler::handleShowkey(const BufferInfo &bufferInfo, const QString &msg) {
+#ifdef HAVE_QCA2
+ if(!bufferInfo.isValid())
+ return;
+
+ if(!Cipher::neededFeaturesAvailable())
+ return;
+
+ QStringList parms = msg.split(' ', QString::SkipEmptyParts);
+
+ if(parms.isEmpty() && !bufferInfo.bufferName().isEmpty())
+ parms.prepend(bufferInfo.bufferName());
+
+ if(parms.isEmpty()) {
+ emit displayMsg(Message::Info, bufferInfo.bufferName(), "",
+ tr("[usage] /showkey <nick|channel> shows the encryption key for nick or channel or just /showkey when in a channel or query."));
+ return;
+ }
+
+ QString target = parms.at(0);
+ QByteArray key = network()->cipherKey(target);
+
+ if(key.isEmpty()) {
+ emit displayMsg(Message::Info, bufferInfo.bufferName(), tr("No key has been set for %1.").arg(target));
+ return;
+ }
+
+ emit displayMsg(Message::Info, bufferInfo.bufferName(), tr("The key for %1 is %2").arg(target).arg(QString(key)));
+
+#else
+ Q_UNUSED(msg)
+ emit displayMsg(Message::Error, bufferInfo.bufferName(), "", tr("Error: Setting an encryption key requires Quassel to have been built "
+ "with support for the Qt Cryptographic Architecture (QCA2) library. "
+ "Contact your distributor about a Quassel package with QCA2 "
+ "support, or rebuild Quassel with QCA2 present."));
+#endif
+}
+
void CoreUserInputHandler::handleTopic(const BufferInfo &bufferInfo, const QString &msg) {
- if(bufferInfo.bufferName().isEmpty()) return;
+ if(bufferInfo.bufferName().isEmpty())
+ return;
+
QList<QByteArray> params;
params << serverEncode(bufferInfo.bufferName());
- if(!msg.isEmpty())
- params << channelEncode(bufferInfo.bufferName(), msg);
+
+ if(!msg.isEmpty()) {
+# ifdef HAVE_QCA2
+ params << encrypt(bufferInfo.bufferName(), channelEncode(bufferInfo.bufferName(), msg));
+# else
+ params << channelEncode(bufferInfo.bufferName(), msg);
+# endif
+ }
+
emit putCmd("TOPIC", params);
}
void CoreUserInputHandler::defaultHandler(QString cmd, const BufferInfo &bufferInfo, const QString &msg) {
Q_UNUSED(bufferInfo);
- emit putCmd(serverEncode(cmd.toUpper()), serverEncode(msg));
+ emit putCmd(serverEncode(cmd.toUpper()), serverEncode(msg.split(" ")));
}
-void CoreUserInputHandler::putPrivmsg(const QByteArray &target, const QByteArray &message) {
+void CoreUserInputHandler::putPrivmsg(const QByteArray &target, const QByteArray &message, Cipher *cipher) {
+ // Encrypted messages need special care. There's no clear relation between cleartext and encrypted message length,
+ // so we can't just compute the maxSplitPos. Instead, we need to loop through the splitpoints until the crypted
+ // version is short enough...
+ // TODO: check out how the various possible encryption methods behave length-wise and make
+ // this clean by predicting the length of the crypted msg.
+ // For example, blowfish-ebc seems to create 8-char chunks.
+
static const char *cmd = "PRIVMSG";
- int overrun = lastParamOverrun(cmd, QList<QByteArray>() << target << message);
- if(overrun) {
- static const char *splitter = " .,-";
- int maxSplitPos = message.count() - overrun;
- int splitPos = -1;
- for(const char *splitChar = splitter; *splitChar != 0; splitChar++) {
- splitPos = qMax(splitPos, message.lastIndexOf(*splitChar, maxSplitPos));
+ static const char *splitter = " .,-";
+
+ int maxSplitPos = message.count();
+ int splitPos = maxSplitPos;
+ forever {
+ QByteArray crypted = message.left(splitPos);
+ bool isEncrypted = false;
+#ifdef HAVE_QCA2
+ if(cipher && !message.isEmpty()) {
+ isEncrypted = cipher->encrypt(crypted);
}
- if(splitPos <= 0) {
- splitPos = maxSplitPos;
+#endif
+ int overrun = lastParamOverrun(cmd, QList<QByteArray>() << target << crypted);
+ if(overrun) {
+ // In case this is not an encrypted msg, we can just cut off at the end
+ if(!isEncrypted)
+ maxSplitPos = message.count() - overrun;
+
+ splitPos = -1;
+ for(const char *splitChar = splitter; *splitChar != 0; splitChar++) {
+ splitPos = qMax(splitPos, message.lastIndexOf(*splitChar, maxSplitPos) + 1); // keep split char on old line
+ }
+ if(splitPos <= 0 || splitPos > maxSplitPos)
+ splitPos = maxSplitPos;
+
+ maxSplitPos = splitPos - 1;
+ if(maxSplitPos <= 0) { // this should never happen, but who knows...
+ qWarning() << tr("[Error] Could not encrypt your message: %1").arg(message.data());
+ return;
+ }
+ continue; // we never come back here for !encrypted!
}
- putCmd(cmd, QList<QByteArray>() << target << message.left(splitPos));
- putPrivmsg(target, message.mid(splitPos));
+
+ // now we have found a valid splitpos (or didn't need to split to begin with)
+ putCmd(cmd, QList<QByteArray>() << target << crypted);
+ if(splitPos < message.count())
+ putPrivmsg(target, message.mid(splitPos), cipher);
+
return;
- } else {
- putCmd(cmd, QList<QByteArray>() << target << message);
}
}
// returns 0 if the message will not be chopped by the irc server or number of chopped bytes if message is too long
int CoreUserInputHandler::lastParamOverrun(const QString &cmd, const QList<QByteArray> ¶ms) {
- // the server will pass our message trunkated to 512 bytes including CRLF with the following format:
+ // the server will pass our message truncated to 512 bytes including CRLF with the following format:
// ":prefix COMMAND param0 param1 :lastparam"
// where prefix = "nickname!user@host"
// that means that the last message can be as long as:
}
}
+#ifdef HAVE_QCA2
+QByteArray CoreUserInputHandler::encrypt(const QString &target, const QByteArray &message_, bool *didEncrypt) const {
+ if(didEncrypt)
+ *didEncrypt = false;
+
+ if(message_.isEmpty())
+ return message_;
+
+ if(!Cipher::neededFeaturesAvailable())
+ return message_;
+
+ Cipher *cipher = network()->cipher(target);
+ if(!cipher)
+ return message_;
+
+ QByteArray message = message_;
+ bool result = cipher->encrypt(message);
+ if(didEncrypt)
+ *didEncrypt = result;
+
+ return message;
+}
+#endif
+
void CoreUserInputHandler::timerEvent(QTimerEvent *event) {
if(!_delayedCommands.contains(event->timerId())) {
QObject::timerEvent(event);