From 14281719026a2d9d30667f63f3659c2c4e7e0cca Mon Sep 17 00:00:00 2001 From: Manuel Nickschas Date: Thu, 29 Jul 2010 16:35:48 +0200 Subject: [PATCH] Provide CoreNetwork accessor for getting a cipher for a given target Removes code duplication in encrypt()/decrypt(), which were performing the same task, and will come in handy later. --- src/core/corenetwork.cpp | 20 ++++++++++++++++++++ src/core/corenetwork.h | 1 + src/core/coreuserinputhandler.cpp | 18 ++++-------------- src/core/ircserverhandler.cpp | 18 ++++-------------- 4 files changed, 29 insertions(+), 28 deletions(-) diff --git a/src/core/corenetwork.cpp b/src/core/corenetwork.cpp index ccb5600f..b7ec55d2 100644 --- a/src/core/corenetwork.cpp +++ b/src/core/corenetwork.cpp @@ -277,6 +277,26 @@ void CoreNetwork::removeChannelKey(const QString &channel) { } #ifdef HAVE_QCA2 +Cipher *CoreNetwork::cipher(const QString &target) const { + if(target.isEmpty()) + return 0; + + QByteArray key = cipherKey(target); + if(key.isEmpty()) + return 0; + + CoreIrcChannel *channel = qobject_cast(ircChannel(target)); + if(channel) { + if(channel->cipher()->setKey(key)) + return channel->cipher(); + } else { + CoreIrcUser *user = qobject_cast(ircUser(target)); + if(user && user->cipher()->setKey(key)) + return user->cipher(); + } + return 0; +} + QByteArray CoreNetwork::cipherKey(const QString &recipient) const { return _cipherKeys.value(recipient.toLower(), QByteArray()); } diff --git a/src/core/corenetwork.h b/src/core/corenetwork.h index 7b458509..a579e3bb 100644 --- a/src/core/corenetwork.h +++ b/src/core/corenetwork.h @@ -115,6 +115,7 @@ public slots: // Blowfish stuff #ifdef HAVE_QCA2 + Cipher *cipher(const QString &recipient) const; QByteArray cipherKey(const QString &recipient) const; void setCipherKey(const QString &recipient, const QByteArray &key); #endif diff --git a/src/core/coreuserinputhandler.cpp b/src/core/coreuserinputhandler.cpp index ecab9e10..185baf70 100644 --- a/src/core/coreuserinputhandler.cpp +++ b/src/core/coreuserinputhandler.cpp @@ -604,25 +604,15 @@ int CoreUserInputHandler::lastParamOverrun(const QString &cmd, const QListcipherKey(target); - if(key.isEmpty()) + Cipher *cipher = network()->cipher(target); + if(!cipher) return message_; QByteArray message = message_; - - CoreIrcChannel *channel = qobject_cast(network()->ircChannel(target)); - if(channel) { - if(channel->cipher()->setKey(key)) - channel->cipher()->encrypt(message); - } else { - CoreIrcUser *user = qobject_cast(network()->ircUser(target)); - if(user && user->cipher()->setKey(key)) - user->cipher()->encrypt(message); - } - + cipher->encrypt(message); return message; } #endif diff --git a/src/core/ircserverhandler.cpp b/src/core/ircserverhandler.cpp index b9a2f6c9..534229c0 100644 --- a/src/core/ircserverhandler.cpp +++ b/src/core/ircserverhandler.cpp @@ -1249,25 +1249,15 @@ void IrcServerHandler::destroyNetsplits() { #ifdef HAVE_QCA2 QByteArray IrcServerHandler::decrypt(const QString &bufferName, const QByteArray &message_, bool isTopic) { - if(bufferName.isEmpty() || message_.isEmpty()) + if(message_.isEmpty()) return message_; - const QByteArray key = network()->cipherKey(bufferName); - if(key.isEmpty()) + Cipher *cipher = network()->cipher(bufferName); + if(!cipher) return message_; QByteArray message = message_; - - CoreIrcChannel *channel = qobject_cast(network()->ircChannel(bufferName)); - if(channel) { - if(channel->cipher()->setKey(key)) - message = isTopic? channel->cipher()->decryptTopic(message) : channel->cipher()->decrypt(message); - } else { - CoreIrcUser *user = qobject_cast(network()->ircUser(bufferName)); - if(user && user->cipher()->setKey(key)) - message = user->cipher()->decrypt(message); - } - + message = isTopic? cipher->decryptTopic(message) : cipher->decrypt(message); return message; } #endif -- 2.20.1