Refactor Cipher related things.
authorBas Pape <baspape@gmail.com>
Thu, 31 Jan 2013 17:46:17 +0000 (18:46 +0100)
committerBas Pape <baspape@gmail.com>
Thu, 31 Jan 2013 17:46:17 +0000 (18:46 +0100)
Instead of using a hash with set keys, set the key on the relevant
  CoreIrc{User,Channel}'s cipher member.
Let the cipher clear its own key (with appropriate checks elsewhere)
Instantiate a new IrcUser when necessary (a query with someone who does
  not share any channels with us means there is no IrcUser for the target
  until he says something)

src/core/cipher.cpp
src/core/coreircchannel.cpp
src/core/corenetwork.cpp
src/core/corenetwork.h
src/core/coreuserinputhandler.cpp
src/core/ircparser.cpp

index 5ca3129..15a986b 100644 (file)
@@ -35,8 +35,10 @@ Cipher::~Cipher()
 
 bool Cipher::setKey(QByteArray key)
 {
 
 bool Cipher::setKey(QByteArray key)
 {
-    if (key.isEmpty())
+    if (key.isEmpty()) {
+        m_key.clear();
         return false;
         return false;
+    }
 
     if (key.mid(0, 4).toLower() == "ecb:")
     {
 
     if (key.mid(0, 4).toLower() == "ecb:")
     {
index f0e65ff..98995bf 100644 (file)
@@ -59,13 +59,6 @@ void CoreIrcChannel::setEncrypted(bool e)
         if (topic().isEmpty())
             return;
 
         if (topic().isEmpty())
             return;
 
-        QByteArray key = qobject_cast<CoreNetwork *>(network())->cipherKey(name());
-        if (key.isEmpty())
-            return;
-
-        if (!cipher()->setKey(key))
-            return;
-
         QByteArray decrypted = cipher()->decryptTopic(topic().toAscii());
         setTopic(decodeString(decrypted));
     }
         QByteArray decrypted = cipher()->decryptTopic(topic().toAscii());
         setTopic(decodeString(decrypted));
     }
index 3a2c208..512ac10 100644 (file)
@@ -310,7 +310,7 @@ void CoreNetwork::removeChannelKey(const QString &channel)
 
 
 #ifdef HAVE_QCA2
 
 
 #ifdef HAVE_QCA2
-Cipher *CoreNetwork::cipher(const QString &target) const
+Cipher *CoreNetwork::cipher(const QString &target)
 {
     if (target.isEmpty())
         return 0;
 {
     if (target.isEmpty())
         return 0;
@@ -318,39 +318,51 @@ Cipher *CoreNetwork::cipher(const QString &target) const
     if (!Cipher::neededFeaturesAvailable())
         return 0;
 
     if (!Cipher::neededFeaturesAvailable())
         return 0;
 
-    QByteArray key = cipherKey(target);
-    if (key.isEmpty())
-        return 0;
-
     CoreIrcChannel *channel = qobject_cast<CoreIrcChannel *>(ircChannel(target));
     if (channel) {
     CoreIrcChannel *channel = qobject_cast<CoreIrcChannel *>(ircChannel(target));
     if (channel) {
-        if (channel->cipher()->setKey(key))
-            return channel->cipher();
+        return channel->cipher();
     }
     }
-    else {
-        CoreIrcUser *user = qobject_cast<CoreIrcUser *>(ircUser(target));
-        if (user && user->cipher()->setKey(key))
-            return user->cipher();
+    CoreIrcUser *user = qobject_cast<CoreIrcUser *>(ircUser(target));
+    if (user) {
+        return user->cipher();
+    } else if (!isChannelName(target)) {
+        return qobject_cast<CoreIrcUser*>(newIrcUser(target))->cipher();
     }
     return 0;
 }
 
 
     }
     return 0;
 }
 
 
-QByteArray CoreNetwork::cipherKey(const QString &recipient) const
+QByteArray CoreNetwork::cipherKey(const QString &target) const
 {
 {
-    return _cipherKeys.value(recipient.toLower(), QByteArray());
+    CoreIrcChannel *c = qobject_cast<CoreIrcChannel*>(ircChannel(target));
+    if (c)
+        return c->cipher()->key();
+
+    CoreIrcUser *u = qobject_cast<CoreIrcUser*>(ircUser(target));
+    if (u)
+        return u->cipher()->key();
+
+    return QByteArray();
 }
 
 
 }
 
 
-void CoreNetwork::setCipherKey(const QString &recipient, const QByteArray &key)
+void CoreNetwork::setCipherKey(const QString &target, const QByteArray &key)
 {
 {
-    if (!key.isEmpty())
-        _cipherKeys[recipient.toLower()] = key;
-    else
-        _cipherKeys.remove(recipient.toLower());
-}
+    CoreIrcChannel *c = qobject_cast<CoreIrcChannel*>(ircChannel(target));
+    if (c) {
+        c->setEncrypted(c->cipher()->setKey(key));
+        return;
+    }
 
 
+    CoreIrcUser *u = qobject_cast<CoreIrcUser*>(ircUser(target));
+    if (!u && !isChannelName(target))
+        u = qobject_cast<CoreIrcUser*>(newIrcUser(target));
 
 
+    if (u) {
+        u->setEncrypted(u->cipher()->setKey(key));
+        return;
+    }
+}
 #endif /* HAVE_QCA2 */
 
 bool CoreNetwork::setAutoWhoDone(const QString &channel)
 #endif /* HAVE_QCA2 */
 
 bool CoreNetwork::setAutoWhoDone(const QString &channel)
index e58945a..7b6048a 100644 (file)
@@ -120,7 +120,7 @@ public slots:
 
     // Blowfish stuff
 #ifdef HAVE_QCA2
 
     // Blowfish stuff
 #ifdef HAVE_QCA2
-    Cipher *cipher(const QString &recipient) const;
+    Cipher *cipher(const QString &recipient);
     QByteArray cipherKey(const QString &recipient) const;
     void setCipherKey(const QString &recipient, const QByteArray &key);
 #endif
     QByteArray cipherKey(const QString &recipient) const;
     void setCipherKey(const QString &recipient, const QByteArray &key);
 #endif
index 930be96..0326436 100644 (file)
@@ -207,14 +207,6 @@ void CoreUserInputHandler::handleDelkey(const BufferInfo &bufferInfo, const QStr
     }
 
     network()->setCipherKey(target, QByteArray());
     }
 
     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
     emit displayMsg(Message::Info, bufferInfo.bufferName(), tr("The key for %1 has been deleted.").arg(target));
 
 #else
@@ -560,14 +552,8 @@ void CoreUserInputHandler::handleSetkey(const BufferInfo &bufferInfo, const QStr
 
     QString target = parms.at(0);
     QByteArray key = parms.at(1).toLocal8Bit();
 
     QString target = parms.at(0);
     QByteArray key = parms.at(1).toLocal8Bit();
-
     network()->setCipherKey(target, key);
 
     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::Info, bufferInfo.bufferName(), tr("The key for %1 has been set.").arg(target));
 #else
     Q_UNUSED(msg)
@@ -716,7 +702,7 @@ void CoreUserInputHandler::putPrivmsg(const QByteArray &target, const QByteArray
         QByteArray crypted = message.left(splitPos);
         bool isEncrypted = false;
 #ifdef HAVE_QCA2
         QByteArray crypted = message.left(splitPos);
         bool isEncrypted = false;
 #ifdef HAVE_QCA2
-        if (cipher && !message.isEmpty()) {
+        if (cipher && !cipher->key().isEmpty() && !message.isEmpty()) {
             isEncrypted = cipher->encrypt(crypted);
         }
 #endif
             isEncrypted = cipher->encrypt(crypted);
         }
 #endif
@@ -797,7 +783,7 @@ QByteArray CoreUserInputHandler::encrypt(const QString &target, const QByteArray
         return message_;
 
     Cipher *cipher = network()->cipher(target);
         return message_;
 
     Cipher *cipher = network()->cipher(target);
-    if (!cipher)
+    if (!cipher || cipher->key().isEmpty())
         return message_;
 
     QByteArray message = message_;
         return message_;
 
     QByteArray message = message_;
index 69603c1..f71f53c 100644 (file)
@@ -58,7 +58,7 @@ QByteArray IrcParser::decrypt(Network *network, const QString &bufferName, const
         return message;
 
     Cipher *cipher = qobject_cast<CoreNetwork *>(network)->cipher(bufferName);
         return message;
 
     Cipher *cipher = qobject_cast<CoreNetwork *>(network)->cipher(bufferName);
-    if (!cipher)
+    if (!cipher || cipher->key().isEmpty())
         return message;
 
     return isTopic ? cipher->decryptTopic(message) : cipher->decrypt(message);
         return message;
 
     return isTopic ? cipher->decryptTopic(message) : cipher->decrypt(message);