Provide CoreNetwork accessor for getting a cipher for a given target
[quassel.git] / src / core / corenetwork.h
index f227de4..a579e3b 100644 (file)
@@ -1,5 +1,5 @@
 /***************************************************************************
- *   Copyright (C) 2005-08 by the Quassel Project                          *
+ *   Copyright (C) 2005-09 by the Quassel Project                          *
  *   devel@quassel-irc.org                                                 *
  *                                                                         *
  *   This program is free software; you can redistribute it and/or modify  *
@@ -23,6 +23,7 @@
 
 #include "network.h"
 #include "coreircchannel.h"
+#include "coreircuser.h"
 
 #include <QTimer>
 
 # include <QTcpSocket>
 #endif
 
+#ifdef HAVE_QCA2
+#  include "cipher.h"
+#endif
+
 #include "coresession.h"
 
-class Identity;
+class CoreIdentity;
 class IrcServerHandler;
-class UserInputHandler;
+class CoreUserInputHandler;
 class CtcpHandler;
+class CoreIgnoreListManager;
 
 class CoreNetwork : public Network {
+  SYNCABLE_OBJECT
   Q_OBJECT
 
 public:
@@ -48,12 +55,14 @@ public:
   ~CoreNetwork();
   inline virtual const QMetaObject *syncMetaObject() const { return &Network::staticMetaObject; }
 
-  inline Identity *identityPtr() const { return coreSession()->identity(identity()); }
+  inline CoreIdentity *identityPtr() const { return coreSession()->identity(identity()); }
   inline CoreSession *coreSession() const { return _coreSession; }
+  inline CoreNetworkConfig *networkConfig() const { return coreSession()->networkConfig(); }
 
   inline IrcServerHandler *ircServerHandler() const { return _ircServerHandler; }
-  inline UserInputHandler *userInputHandler() const { return _userInputHandler; }
+  inline CoreUserInputHandler *userInputHandler() const { return _userInputHandler; }
   inline CtcpHandler *ctcpHandler() const { return _ctcpHandler; }
+  inline CoreIgnoreListManager *ignoreListManager() { return coreSession()->ignoreListManager(); }
 
   //! Decode a string using the server (network) decoding.
   inline QString serverDecode(const QByteArray &string) const { return decodeServerString(string); }
@@ -74,9 +83,8 @@ public:
   QByteArray userEncode(const QString &userNick, const QString &string) const;
 
   inline QString channelKey(const QString &channel) const { return _channelKeys.value(channel.toLower(), QString()); }
-  inline QStringList persistentChannels() const { return _channelKeys.keys(); }
 
-  inline bool isAutoWhoInProgress(const QString &channel) const { return _autoWhoInProgress.value(channel.toLower(), 0); }
+  inline bool isAutoWhoInProgress(const QString &channel) const { return _autoWhoPending.value(channel.toLower(), 0); }
 
   inline UserId userId() const { return _coreSession->user(); }
 
@@ -91,8 +99,10 @@ public slots:
   virtual void setAutoReconnectInterval(quint32);
   virtual void setAutoReconnectRetries(quint16);
 
+  void setPingInterval(int interval);
+
   void connectToIrc(bool reconnecting = false);
-  void disconnectFromIrc(bool requested = true, const QString &reason = QString());
+  void disconnectFromIrc(bool requested = true, const QString &reason = QString(), bool withReconnect = false);
 
   void userInput(BufferInfo bufferInfo, QString msg);
   void putRawLine(QByteArray input);
@@ -103,14 +113,35 @@ public slots:
   void addChannelKey(const QString &channel, const QString &key);
   void removeChannelKey(const QString &channel);
 
+  // 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
+
+  void setAutoWhoEnabled(bool enabled);
+  void setAutoWhoInterval(int interval);
+  void setAutoWhoDelay(int delay);
+
   bool setAutoWhoDone(const QString &channel);
 
-  inline const Server &usedServer() const { return serverList()[_lastUsedServerIndex]; }
+  void updateIssuedModes(const QString &requestedModes);
+  void updatePersistentModes(QString addModes, QString removeModes);
+  void resetPersistentModes();
+
+  Server usedServer() const;
+
+  inline void resetPingTimeout() { _pingCount = 0; }
+
+  inline void displayMsg(Message::Type msgType, BufferInfo::Type bufferType, const QString &target, const QString &text, const QString &sender = "", Message::Flags flags = Message::None) {
+    emit displayMsg(networkId(), msgType, bufferType, target, text, sender, flags);
+  }
 
 signals:
   void recvRawServerMsg(QString);
   void displayStatusMsg(QString);
-  void displayMsg(Message::Type, BufferInfo::Type, QString target, QString text, QString sender = "", Message::Flags flags = Message::None);
+  void displayMsg(NetworkId, Message::Type, BufferInfo::Type, const QString &target, const QString &text, const QString &sender = "", Message::Flags flags = Message::None);
   void disconnected(NetworkId networkId);
   void connectionError(const QString &errorMsg);
 
@@ -119,6 +150,13 @@ signals:
 
 protected:
   inline virtual IrcChannel *ircChannelFactory(const QString &channelname) { return new CoreIrcChannel(channelname, this); }
+  inline virtual IrcUser *ircUserFactory(const QString &hostmask) { return new CoreIrcUser(hostmask, this); }
+
+protected slots:
+  // TODO: remove cached cipher keys, when appropriate
+  //virtual void removeIrcUser(IrcUser *ircuser);
+  //virtual void removeIrcChannel(IrcChannel *ircChannel);
+  //virtual void removeChansAndUsers();
 
 private slots:
   void socketHasData();
@@ -130,13 +168,15 @@ private slots:
   void networkInitialized();
 
   void sendPerform();
+  void restoreUserModes();
   void doAutoReconnect();
   void sendPing();
+  void enablePingTimeout(bool enable = true);
+  void disablePingTimeout();
   void sendAutoWho();
   void startAutoWhoCycle();
 
 #ifdef HAVE_SSL
-  void sslSocketConnected();
   void sslErrors(const QList<QSslError> &errors);
 #endif
 
@@ -154,7 +194,7 @@ private:
 #endif
 
   IrcServerHandler *_ircServerHandler;
-  UserInputHandler *_userInputHandler;
+  CoreUserInputHandler *_userInputHandler;
   CtcpHandler *_ctcpHandler;
 
   QHash<QString, QString> _channelKeys;  // stores persistent channels and their passwords, if any
@@ -168,25 +208,29 @@ private:
    * it is needed to determine whether or not the connection needs to be
    * in the automatic session restore. */
   bool _quitRequested;
+  QString _quitReason;
 
   bool _previousConnectionAttemptFailed;
   int _lastUsedServerIndex;
 
   QTimer _pingTimer;
+  uint _lastPingTime;
+  uint _pingCount;
 
-  bool _autoWhoEnabled;
   QStringList _autoWhoQueue;
-  QHash<QString, int> _autoWhoInProgress;
-  int _autoWhoInterval;
-  int _autoWhoNickLimit;
-  int _autoWhoDelay;
+  QHash<QString, int> _autoWhoPending;
   QTimer _autoWhoTimer, _autoWhoCycleTimer;
 
   QTimer _tokenBucketTimer;
-  int _messagesPerSecond;   // token refill speed
+  int _messageDelay;        // token refill speed in ms
   int _burstSize;           // size of the token bucket
   int _tokenBucket;         // the virtual bucket that holds the tokens
   QList<QByteArray> _msgQueue;
+
+  QString _requestedUserModes; // 2 strings separated by a '-' character. first part are requested modes to add, the second to remove
+
+  // Blowfish key map
+  QHash<QString, QByteArray> _cipherKeys;
 };
 
 #endif //CORENETWORK_H