fixing some merge weirdness
[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   int lastParamOverrun(const QString &cmd, const QList<QByteArray> &params);
99   void putCmd(const QString &cmd, const QList<QByteArray> &params, const QByteArray &prefix = QByteArray());
100
101   void setChannelJoined(const QString &channel);
102   void setChannelParted(const QString &channel);
103   void addChannelKey(const QString &channel, const QString &key);
104   void removeChannelKey(const QString &channel);
105
106   bool setAutoWhoDone(const QString &channel);
107
108 signals:
109   // #void networkState(QString net, QVariantMap data);
110   void recvRawServerMsg(QString);
111   void displayStatusMsg(QString);
112   //void displayMsg(Message msg);
113   void displayMsg(Message::Type, BufferInfo::Type, QString target, QString text, QString sender = "", Message::Flags flags = Message::None);
114   void connected(NetworkId networkId);   ///< Emitted after receipt of 001 to indicate that we can now send data to the IRC server
115   void disconnected(NetworkId networkId);
116   void connectionStateChanged(Network::ConnectionState);
117   void connectionInitialized(); ///< Emitted after receipt of 001 to indicate that we can now send data to the IRC server
118   void connectionError(const QString &errorMsg);
119
120   void quitRequested(NetworkId networkId);
121
122   //void queryRequested(QString network, QString nick);
123   void nickChanged(const NetworkId &networkId, const QString &newNick, const QString &oldNick); // this signal is inteded to rename query buffers in the storage backend
124   void channelJoined(NetworkId, const QString &channel, const QString &key = QString());
125   void channelParted(NetworkId, const QString &channel);
126
127   void sslErrors(const QVariant &errorData);
128
129 private slots:
130   void socketHasData();
131   void socketError(QAbstractSocket::SocketError);
132   void socketConnected();
133   void socketInitialized();
134   void socketCloseTimeout();
135   void socketDisconnected();
136   void socketStateChanged(QAbstractSocket::SocketState);
137   void setConnectionState(Network::ConnectionState);
138   void networkInitialized(const QString &currentServer);
139
140   void sendPerform();
141   void autoReconnectSettingsChanged();
142   void doAutoReconnect();
143   void sendAutoWho();
144   void startAutoWhoCycle();
145   void nickChanged(const QString &newNick, const QString &oldNick); // this signal is inteded to rename query buffers in the storage backend
146
147 #ifndef QT_NO_OPENSSL
148   void socketEncrypted();
149   void sslErrors(const QList<QSslError> &errors);
150 #endif
151
152   void fillBucketAndProcessQueue();
153
154 private:
155 #ifndef QT_NO_OPENSSL
156   QSslSocket socket;
157 #else
158   QTcpSocket socket;
159 #endif
160
161   Network::ConnectionState _connectionState;
162
163   Network *_network;
164   CoreSession *_coreSession;
165   BufferInfo _statusBufferInfo;
166
167   IrcServerHandler *_ircServerHandler;
168   UserInputHandler *_userInputHandler;
169   CtcpHandler *_ctcpHandler;
170
171   QHash<QString, QString> _channelKeys;  // stores persistent channels and their passwords, if any
172
173   QTimer _autoReconnectTimer;
174   
175   int _autoReconnectCount;
176
177   QTimer _socketCloseTimer;
178
179   bool _quitRequested;
180
181   bool _previousConnectionAttemptFailed;
182   int _lastUsedServerlistIndex;
183
184   bool _autoWhoEnabled;
185   QStringList _autoWhoQueue;
186   QHash<QString, int> _autoWhoInProgress;
187   int _autoWhoInterval;
188   int _autoWhoNickLimit;
189   int _autoWhoDelay;
190   QTimer _autoWhoTimer, _autoWhoCycleTimer;
191
192   QTimer _tokenBucketTimer;
193   int _messagesPerSecond;   // token refill speed
194   int _burstSize;   // size of the token bucket
195   int _tokenBucket; // the virtual bucket that holds the tokens
196   QList<QByteArray> _msgQueue;
197
198   void writeToSocket(QByteArray s);
199
200
201
202   class ParseError : public Exception {
203   public:
204     ParseError(QString cmd, QString prefix, QStringList params);
205   };
206
207   class UnknownCmdError : public Exception {
208   public:
209     UnknownCmdError(QString cmd, QString prefix, QStringList params);
210   };
211 };
212
213 #endif