1 /***************************************************************************
2 * Copyright (C) 2005-08 by the Quassel Project *
3 * devel@quassel-irc.org *
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. *
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. *
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 ***************************************************************************/
21 #ifndef _NETWORKCONNECTION_H_
22 #define _NETWORKCONNECTION_H_
24 #include <QAbstractSocket>
26 #include <QStringList>
30 # include <QSslSocket>
33 # include <QTcpSocket>
36 #include "coresession.h"
40 #include "signalproxy.h"
42 class IrcServerHandler;
43 class UserInputHandler;
46 class NetworkConnection : public QObject {
50 NetworkConnection(Network *network, CoreSession *session);
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; }
59 inline bool isConnected() const { return connectionState() == Network::Initialized; }
60 inline Network::ConnectionState connectionState() const { return _connectionState; }
62 inline IrcServerHandler *ircServerHandler() const { return _ircServerHandler; }
63 inline UserInputHandler *userInputHandler() const { return _userInputHandler; }
64 inline CtcpHandler *ctcpHandler() const { return _ctcpHandler; }
66 //! Decode a string using the server (network) decoding.
67 QString serverDecode(const QByteArray &string) const;
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;
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;
75 //! Encode a string using the server (network) encoding.
76 QByteArray serverEncode(const QString &string) const;
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;
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;
84 inline QString channelKey(const QString &channel) const { return _channelKeys.value(channel.toLower(), QString()); }
85 inline QStringList persistentChannels() const { return _channelKeys.keys(); }
87 inline bool isAutoWhoInProgress(const QString &channel) const { return _autoWhoInProgress.value(channel.toLower(), 0); }
90 // void setServerOptions();
91 void connectToIrc(bool reconnecting = false);
92 void disconnectFromIrc(bool requested = true);
93 void userInput(BufferInfo bufferInfo, QString msg);
95 void putRawLine(QByteArray input);
96 int lastParamOverrun(const QString &cmd, const QList<QByteArray> ¶ms);
97 void putCmd(const QString &cmd, const QList<QByteArray> ¶ms, const QByteArray &prefix = QByteArray());
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);
104 bool setAutoWhoDone(const QString &channel);
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);
118 void quitRequested(NetworkId networkId);
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);
125 void sslErrors(const QVariant &errorData);
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 ¤tServer);
139 void autoReconnectSettingsChanged();
140 void doAutoReconnect();
143 void startAutoWhoCycle();
144 void nickChanged(const QString &newNick, const QString &oldNick); // this signal is inteded to rename query buffers in the storage backend
146 #ifndef QT_NO_OPENSSL
147 void socketEncrypted();
148 void sslErrors(const QList<QSslError> &errors);
151 void fillBucketAndProcessQueue();
154 #ifndef QT_NO_OPENSSL
160 Network::ConnectionState _connectionState;
163 CoreSession *_coreSession;
164 BufferInfo _statusBufferInfo;
166 IrcServerHandler *_ircServerHandler;
167 UserInputHandler *_userInputHandler;
168 CtcpHandler *_ctcpHandler;
170 QHash<QString, QString> _channelKeys; // stores persistent channels and their passwords, if any
172 QTimer _autoReconnectTimer;
174 int _autoReconnectCount;
176 QTimer _socketCloseTimer;
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. */
183 bool _previousConnectionAttemptFailed;
184 int _lastUsedServerlistIndex;
188 bool _autoWhoEnabled;
189 QStringList _autoWhoQueue;
190 QHash<QString, int> _autoWhoInProgress;
191 int _autoWhoInterval;
192 int _autoWhoNickLimit;
194 QTimer _autoWhoTimer, _autoWhoCycleTimer;
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;
202 void writeToSocket(QByteArray s);
204 class ParseError : public Exception {
206 ParseError(QString cmd, QString prefix, QStringList params);
209 class UnknownCmdError : public Exception {
211 UnknownCmdError(QString cmd, QString prefix, QStringList params);