#BR39 - long messages will now be divided in several messages.
[quassel.git] / src / core / networkconnection.h
1 /***************************************************************************
2  *   Copyright (C) 2005-08 by the Quassel Project                          *
3  *   devel@quassel-irc.org                                                 *
4  *                                                                         *
5  *   This program is free software; you can redistribute it and/or modify  *
6  *   it under the terms of the GNU General Public License as published by  *
7  *   the Free Software Foundation; either version 2 of the License, or     *
8  *   (at your option) version 3.                                           *
9  *                                                                         *
10  *   This program is distributed in the hope that it will be useful,       *
11  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
12  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
13  *   GNU General Public License for more details.                          *
14  *                                                                         *
15  *   You should have received a copy of the GNU General Public License     *
16  *   along with this program; if not, write to the                         *
17  *   Free Software Foundation, Inc.,                                       *
18  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
19  ***************************************************************************/
20
21 #ifndef _NETWORKCONNECTION_H_
22 #define _NETWORKCONNECTION_H_
23
24 #include <QAbstractSocket>
25 #include <QString>
26 #include <QStringList>
27 #include <QTimer>
28
29 #ifndef QT_NO_OPENSSL
30 # include <QSslSocket>
31 # include <QSslError>
32 #else
33 # include <QTcpSocket>
34 #endif
35
36 #include "coresession.h"
37 #include "identity.h"
38 #include "message.h"
39 #include "network.h"
40 #include "signalproxy.h"
41
42 class Network;
43
44 class IrcServerHandler;
45 class UserInputHandler;
46 class CtcpHandler;
47
48 class NetworkConnection : public QObject {
49   Q_OBJECT
50
51 public:
52   NetworkConnection(Network *network, CoreSession *session);
53   ~NetworkConnection();
54
55   inline NetworkId networkId() const { return network()->networkId(); }
56   inline QString networkName() const { return network()->networkName(); }
57   inline Network *network() const { return _network; }
58   inline Identity *identity() const { return coreSession()->identity(network()->identity()); }
59   inline CoreSession *coreSession() const { return _coreSession; }
60
61   inline bool isConnected() const { return connectionState() == Network::Initialized; }
62   inline Network::ConnectionState connectionState() const { return _connectionState; }
63
64   inline IrcServerHandler *ircServerHandler() const { return _ircServerHandler; }
65   inline UserInputHandler *userInputHandler() const { return _userInputHandler; }
66   inline CtcpHandler *ctcpHandler() const { return _ctcpHandler; }
67
68   //! Decode a string using the server (network) decoding.
69   QString serverDecode(const QByteArray &string) const;
70
71   //! Decode a string using a channel-specific encoding if one is set (and use the standard encoding else).
72   QString channelDecode(const QString &channelName, const QByteArray &string) const;
73
74   //! Decode a string using an IrcUser-specific encoding, if one exists (using the standaed encoding else).
75   QString userDecode(const QString &userNick, const QByteArray &string) const;
76
77   //! Encode a string using the server (network) encoding.
78   QByteArray serverEncode(const QString &string) const;
79
80   //! Encode a string using the channel-specific encoding, if set, and use the standard encoding else.
81   QByteArray channelEncode(const QString &channelName, const QString &string) const;
82
83   //! Encode a string using the user-specific encoding, if set, and use the standard encoding else.
84   QByteArray userEncode(const QString &userNick, const QString &string) const;
85
86   inline QString channelKey(const QString &channel) const { return _channelKeys.value(channel.toLower(), QString()); }
87   inline QStringList persistentChannels() const { return _channelKeys.keys(); }
88
89   inline bool isAutoWhoInProgress(const QString &channel) const { return _autoWhoInProgress.value(channel.toLower(), 0); }
90
91 public slots:
92   // void setServerOptions();
93   void connectToIrc(bool reconnecting = false);
94   void disconnectFromIrc(bool requested = true);
95   void userInput(BufferInfo bufferInfo, QString msg);
96
97   void putRawLine(QByteArray input);
98   void putCmd(const QString &cmd, const QVariantList &params, const QByteArray &prefix = QByteArray());
99
100   void setChannelJoined(const QString &channel);
101   void setChannelParted(const QString &channel);
102   void addChannelKey(const QString &channel, const QString &key);
103   void removeChannelKey(const QString &channel);
104
105   bool setAutoWhoDone(const QString &channel);
106
107 signals:
108   // #void networkState(QString net, QVariantMap data);
109   void recvRawServerMsg(QString);
110   void displayStatusMsg(QString);
111   //void displayMsg(Message msg);
112   void displayMsg(Message::Type, BufferInfo::Type, QString target, QString text, QString sender = "", Message::Flags flags = Message::None);
113   void connected(NetworkId networkId);   ///< Emitted after receipt of 001 to indicate that we can now send data to the IRC server
114   void disconnected(NetworkId networkId);
115   void connectionStateChanged(Network::ConnectionState);
116   void connectionInitialized(); ///< Emitted after receipt of 001 to indicate that we can now send data to the IRC server
117   void connectionError(const QString &errorMsg);
118
119   void quitRequested(NetworkId networkId);
120
121   //void queryRequested(QString network, QString nick);
122   void nickChanged(const NetworkId &networkId, const QString &newNick, const QString &oldNick); // this signal is inteded to rename query buffers in the storage backend
123   void channelJoined(NetworkId, const QString &channel, const QString &key = QString());
124   void channelParted(NetworkId, const QString &channel);
125
126   void sslErrors(const QVariant &errorData);
127
128 private slots:
129   void socketHasData();
130   void socketError(QAbstractSocket::SocketError);
131   void socketConnected();
132   void socketInitialized();
133   void socketDisconnected();
134   void socketStateChanged(QAbstractSocket::SocketState);
135   void setConnectionState(Network::ConnectionState);
136   void networkInitialized(const QString &currentServer);
137
138   void sendPerform();
139   void autoReconnectSettingsChanged();
140   void doAutoReconnect();
141   void sendAutoWho();
142   void startAutoWhoCycle();
143   void nickChanged(const QString &newNick, const QString &oldNick); // this signal is inteded to rename query buffers in the storage backend
144
145 #ifndef QT_NO_OPENSSL
146   void socketEncrypted();
147   void sslErrors(const QList<QSslError> &errors);
148 #endif
149
150   void fillBucketAndProcessQueue();
151
152 private:
153 #ifndef QT_NO_OPENSSL
154   QSslSocket socket;
155 #else
156   QTcpSocket socket;
157 #endif
158
159   Network::ConnectionState _connectionState;
160
161   Network *_network;
162   CoreSession *_coreSession;
163   BufferInfo _statusBufferInfo;
164
165   IrcServerHandler *_ircServerHandler;
166   UserInputHandler *_userInputHandler;
167   CtcpHandler *_ctcpHandler;
168
169   QHash<QString, QString> _channelKeys;  // stores persistent channels and their passwords, if any
170
171   QTimer _autoReconnectTimer;
172   int _autoReconnectCount;
173
174   bool _previousConnectionAttemptFailed;
175   int _lastUsedServerlistIndex;
176
177   bool _autoWhoEnabled;
178   QStringList _autoWhoQueue;
179   QHash<QString, int> _autoWhoInProgress;
180   int _autoWhoInterval;
181   int _autoWhoNickLimit;
182   int _autoWhoDelay;
183   QTimer _autoWhoTimer, _autoWhoCycleTimer;
184
185   QTimer _tokenBucketTimer;
186   int _messagesPerSecond;   // token refill speed
187   int _burstSize;   // size of the token bucket
188   int _tokenBucket; // the virtual bucket that holds the tokens
189   QList<QByteArray> _msgQueue;
190
191   int _maxMsgSize;
192
193   void writeToSocket(QByteArray s);
194
195
196
197   class ParseError : public Exception {
198   public:
199     ParseError(QString cmd, QString prefix, QStringList params);
200   };
201
202   class UnknownCmdError : public Exception {
203   public:
204     UnknownCmdError(QString cmd, QString prefix, QStringList params);
205   };
206 };
207
208 #endif