fixing a bug where quit messages from different networks were shown if the user has...
[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 #ifdef HAVE_SSL
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 IrcServerHandler;
43 class UserInputHandler;
44 class CtcpHandler;
45
46 class NetworkConnection : public QObject {
47   Q_OBJECT
48
49 public:
50   NetworkConnection(Network *network, CoreSession *session);
51   ~NetworkConnection();
52
53   inline NetworkId networkId() const { return network()->networkId(); }
54   inline QString networkName() const { return network()->networkName(); }
55   inline Network *network() const { return _network; }
56   inline Identity *identity() const { return coreSession()->identity(network()->identity()); }
57   inline CoreSession *coreSession() const { return _coreSession; }
58
59   inline bool isConnected() const { return connectionState() == Network::Initialized; }
60   inline Network::ConnectionState connectionState() const { return _connectionState; }
61
62   inline IrcServerHandler *ircServerHandler() const { return _ircServerHandler; }
63   inline UserInputHandler *userInputHandler() const { return _userInputHandler; }
64   inline CtcpHandler *ctcpHandler() const { return _ctcpHandler; }
65
66   //! Decode a string using the server (network) decoding.
67   QString serverDecode(const QByteArray &string) const;
68
69   //! Decode a string using a channel-specific encoding if one is set (and use the standard encoding else).
70   QString channelDecode(const QString &channelName, const QByteArray &string) const;
71
72   //! Decode a string using an IrcUser-specific encoding, if one exists (using the standaed encoding else).
73   QString userDecode(const QString &userNick, const QByteArray &string) const;
74
75   //! Encode a string using the server (network) encoding.
76   QByteArray serverEncode(const QString &string) const;
77
78   //! Encode a string using the channel-specific encoding, if set, and use the standard encoding else.
79   QByteArray channelEncode(const QString &channelName, const QString &string) const;
80
81   //! Encode a string using the user-specific encoding, if set, and use the standard encoding else.
82   QByteArray userEncode(const QString &userNick, const QString &string) const;
83
84   inline QString channelKey(const QString &channel) const { return _channelKeys.value(channel.toLower(), QString()); }
85   inline QStringList persistentChannels() const { return _channelKeys.keys(); }
86
87   inline bool isAutoWhoInProgress(const QString &channel) const { return _autoWhoInProgress.value(channel.toLower(), 0); }
88
89 public slots:
90   // void setServerOptions();
91   void connectToIrc(bool reconnecting = false);
92   void disconnectFromIrc(bool requested = true);
93   void userInput(BufferInfo bufferInfo, QString msg);
94
95   void putRawLine(QByteArray input);
96   int lastParamOverrun(const QString &cmd, const QList<QByteArray> &params);
97   void putCmd(const QString &cmd, const QList<QByteArray> &params, const QByteArray &prefix = QByteArray());
98
99   void setChannelJoined(const QString &channel);
100   void setChannelParted(const QString &channel);
101   void addChannelKey(const QString &channel, const QString &key);
102   void removeChannelKey(const QString &channel);
103
104   bool setAutoWhoDone(const QString &channel);
105
106 signals:
107   // #void networkState(QString net, QVariantMap data);
108   void recvRawServerMsg(QString);
109   void displayStatusMsg(QString);
110   //void displayMsg(Message msg);
111   void displayMsg(Message::Type, BufferInfo::Type, QString target, QString text, QString sender = "", Message::Flags flags = Message::None);
112   void connected(NetworkId networkId);   ///< Emitted after receipt of 001 to indicate that we can now send data to the IRC server
113   void disconnected(NetworkId networkId);
114   void connectionStateChanged(Network::ConnectionState);
115   void connectionInitialized(); ///< Emitted after receipt of 001 to indicate that we can now send data to the IRC server
116   void connectionError(const QString &errorMsg);
117
118   void quitRequested(NetworkId networkId);
119
120   //void queryRequested(QString network, QString nick);
121   void nickChanged(const NetworkId &networkId, const QString &newNick, const QString &oldNick); // this signal is inteded to rename query buffers in the storage backend
122   void channelJoined(NetworkId, const QString &channel, const QString &key = QString());
123   void channelParted(NetworkId, const QString &channel);
124
125   void sslErrors(const QVariant &errorData);
126
127 private slots:
128   void socketHasData();
129   void socketError(QAbstractSocket::SocketError);
130   void socketConnected();
131   void socketInitialized();
132   void socketCloseTimeout();
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 sendPing();
142   void sendAutoWho();
143   void startAutoWhoCycle();
144   void nickChanged(const QString &newNick, const QString &oldNick); // this signal is inteded to rename query buffers in the storage backend
145
146 #ifdef HAVE_SSL
147   void socketEncrypted();
148   void sslErrors(const QList<QSslError> &errors);
149 #endif
150
151   void fillBucketAndProcessQueue();
152
153 private:
154 #ifdef HAVE_SSL
155   QSslSocket socket;
156 #else
157   QTcpSocket socket;
158 #endif
159
160   Network::ConnectionState _connectionState;
161
162   Network *_network;
163   CoreSession *_coreSession;
164   BufferInfo _statusBufferInfo;
165
166   IrcServerHandler *_ircServerHandler;
167   UserInputHandler *_userInputHandler;
168   CtcpHandler *_ctcpHandler;
169
170   QHash<QString, QString> _channelKeys;  // stores persistent channels and their passwords, if any
171
172   QTimer _autoReconnectTimer;
173   
174   int _autoReconnectCount;
175
176   QTimer _socketCloseTimer;
177
178   /* this flag triggers quitRequested() once the socket is closed
179    * it is needed to determine whether or not the connection needs to be
180    *in the automatic session restore. */
181   bool _quitRequested;
182
183   bool _previousConnectionAttemptFailed;
184   int _lastUsedServerlistIndex;
185
186   QTimer _pingTimer;
187   
188   bool _autoWhoEnabled;
189   QStringList _autoWhoQueue;
190   QHash<QString, int> _autoWhoInProgress;
191   int _autoWhoInterval;
192   int _autoWhoNickLimit;
193   int _autoWhoDelay;
194   QTimer _autoWhoTimer, _autoWhoCycleTimer;
195
196   QTimer _tokenBucketTimer;
197   int _messagesPerSecond;   // token refill speed
198   int _burstSize;   // size of the token bucket
199   int _tokenBucket; // the virtual bucket that holds the tokens
200   QList<QByteArray> _msgQueue;
201
202   void writeToSocket(QByteArray s);
203
204   class ParseError : public Exception {
205   public:
206     ParseError(QString cmd, QString prefix, QStringList params);
207   };
208
209   class UnknownCmdError : public Exception {
210   public:
211     UnknownCmdError(QString cmd, QString prefix, QStringList params);
212   };
213 };
214
215 #endif