message + topic decryption
authorJohannes Huber <johu@gmx.de>
Thu, 25 Feb 2010 18:40:51 +0000 (19:40 +0100)
committerManuel Nickschas <sputnick@quassel-irc.org>
Thu, 22 Jul 2010 07:37:53 +0000 (09:37 +0200)
src/core/ircserverhandler.cpp
src/core/ircserverhandler.h

index ea59836..655f68a 100644 (file)
 
 #include <QDebug>
 
+#ifdef HAVE_QCA2
+#include "cipher.h"
+#endif
+
 IrcServerHandler::IrcServerHandler(CoreNetwork *parent)
   : CoreBasicHandler(parent),
     _whois(false)
@@ -494,6 +498,9 @@ void IrcServerHandler::handlePrivmsg(const QString &prefix, const QList<QByteArr
       ? *targetIter
       : senderNick;
 
+    #ifdef HAVE_QCA2
+    msg = decrypt(target, msg);
+    #endif
     // it's possible to pack multiple privmsgs into one param using ctcp
     // - > we let the ctcpHandler do the work
     network()->ctcpHandler()->parse(Message::Plain, prefix, target, msg);
@@ -549,9 +556,13 @@ void IrcServerHandler::handleTopic(const QString &prefix, const QList<QByteArray
     return;
 
   QString topic;
-  if(params.count() > 1)
-    topic = channelDecode(channel->name(), params[1]);
-
+  if(params.count() > 1) {
+    topic = params[1];
+    #ifdef HAVE_QCA2
+    topic = decryptTopic(channel->name(), topic);
+    #endif
+  }
+  
   channel->setTopic(topic);
 
   emit displayMsg(Message::Topic, BufferInfo::ChannelBuffer, channel->name(), tr("%1 has changed topic for %2 to: \"%3\"").arg(ircuser->nick()).arg(channel->name()).arg(topic));
@@ -989,6 +1000,11 @@ void IrcServerHandler::handle332(const QString &prefix, const QList<QByteArray>
 
   QString channel = serverDecode(params[0]);
   QString topic = channelDecode(channel, params[1]);
+  
+  #ifdef HAVE_QCA2
+  topic = decryptTopic(channel, topic);
+  #endif
+  
   IrcChannel *chan = network()->ircChannel(channel);
   if(chan)
     chan->setTopic(topic);
@@ -1230,6 +1246,72 @@ void IrcServerHandler::destroyNetsplits() {
   _netsplits.clear();
 }
 
+#ifdef HAVE_QCA2
+QByteArray IrcServerHandler::decrypt(const QString &bufferName, QByteArray &message) {
+  if(bufferName.isEmpty())
+    return message;
+
+  if(message.isEmpty())
+    return message;
+
+  const QByteArray key = network()->bufferKey(bufferName);
+  if(key.isEmpty())
+    return message;
+
+  IrcChannel *channel = network()->ircChannel(bufferName);
+  IrcUser *user = network()->ircUser(bufferName);
+
+  //only send encrypted text to decrypter
+  int index = message.indexOf(":",message.indexOf(":")+1);
+
+  /*  if(this->identifyMsgEnabled()) // Workaround braindead Freenode prefixing messages with +
+    ++index;*/
+  
+  QByteArray backup = message.mid(0,index+1);
+
+  if (channel && channel->cipher()->setKey(key))
+    message = channel->cipher()->decrypt(message.mid(index+1));
+  else if (user && user->cipher()->setKey(key))
+    message = user->cipher()->decrypt(message.mid(index+1));
+
+  message.prepend(backup);
+
+  message = channelDecode(bufferName, message).toAscii();
+  
+  return message;
+}
+
+QString IrcServerHandler::decryptTopic(const QString &bufferName, QString &topic) {
+  if(bufferName.isEmpty())
+    return topic;
+
+  if(topic.isEmpty())
+    return topic;
+
+  const QByteArray key = network()->bufferKey(bufferName);
+  if(key.isEmpty())
+    return topic;
+
+  IrcChannel *channel = network()->ircChannel(bufferName);
+  IrcUser *user = network()->ircUser(bufferName);
+
+  //only send encrypted text to decrypter
+  int index = topic.indexOf(":",topic.indexOf(":")+1);
+
+  QString backup = topic.mid(0,index+1);
+
+  if (channel && channel->cipher()->setKey(key))
+    topic = channel->cipher()->decryptTopic(topic.mid(index+1).toAscii());
+  else if (user && user->cipher()->setKey(key))
+    topic = user->cipher()->decryptTopic(topic.mid(index+1).toAscii());
+
+  topic.prepend(backup);
+  topic = channelDecode(bufferName, topic.toAscii());
+
+  return topic;
+}
+#endif
+
 /***********************************************************************************/
 
 
index 7948062..ceb8ef3 100644 (file)
@@ -129,6 +129,11 @@ private:
   // key: quit message
   // value: the corresponding netsplit object
   QHash<QString, Netsplit*> _netsplits;
+  
+  #ifdef HAVE_QCA2
+  QByteArray decrypt(const QString &bufferName, QByteArray &message);
+  QString decryptTopic(const QString &bufferName, QString &topic);
+  #endif
 };