Provide CoreNetwork accessor for getting a cipher for a given target
authorManuel Nickschas <sputnick@quassel-irc.org>
Thu, 29 Jul 2010 14:35:48 +0000 (16:35 +0200)
committerManuel Nickschas <sputnick@quassel-irc.org>
Thu, 29 Jul 2010 14:35:48 +0000 (16:35 +0200)
Removes code duplication in encrypt()/decrypt(), which were performing
the same task, and will come in handy later.

src/core/corenetwork.cpp
src/core/corenetwork.h
src/core/coreuserinputhandler.cpp
src/core/ircserverhandler.cpp

index ccb5600..b7ec55d 100644 (file)
@@ -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<CoreIrcChannel *>(ircChannel(target));
+  if(channel) {
+    if(channel->cipher()->setKey(key))
+      return channel->cipher();
+  } else {
+    CoreIrcUser *user = qobject_cast<CoreIrcUser *>(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());
 }
index 7b45850..a579e3b 100644 (file)
@@ -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
index ecab9e1..185baf7 100644 (file)
@@ -604,25 +604,15 @@ int CoreUserInputHandler::lastParamOverrun(const QString &cmd, const QList<QByte
 
 #ifdef HAVE_QCA2
 QByteArray CoreUserInputHandler::encrypt(const QString &target, const QByteArray &message_) const {
-  if(target.isEmpty() || message_.isEmpty())
+  if(message_.isEmpty())
     return message_;
 
-  QByteArray key = network()->cipherKey(target);
-  if(key.isEmpty())
+  Cipher *cipher = network()->cipher(target);
+  if(!cipher)
     return message_;
 
   QByteArray message = message_;
-
-  CoreIrcChannel *channel = qobject_cast<CoreIrcChannel *>(network()->ircChannel(target));
-  if(channel) {
-    if(channel->cipher()->setKey(key))
-      channel->cipher()->encrypt(message);
-  } else {
-    CoreIrcUser *user = qobject_cast<CoreIrcUser *>(network()->ircUser(target));
-    if(user && user->cipher()->setKey(key))
-      user->cipher()->encrypt(message);
-  }
-
+  cipher->encrypt(message);
   return message;
 }
 #endif
index b9a2f6c..534229c 100644 (file)
@@ -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<CoreIrcChannel *>(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<CoreIrcUser *>(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