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"
44 class IrcServerHandler;
45 class UserInputHandler;
48 class NetworkConnection : public QObject {
52 NetworkConnection(Network *network, CoreSession *session);
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; }
61 inline bool isConnected() const { return connectionState() == Network::Initialized; }
62 inline Network::ConnectionState connectionState() const { return _connectionState; }
64 inline IrcServerHandler *ircServerHandler() const { return _ircServerHandler; }
65 inline UserInputHandler *userInputHandler() const { return _userInputHandler; }
66 inline CtcpHandler *ctcpHandler() const { return _ctcpHandler; }
68 //! Decode a string using the server (network) decoding.
69 QString serverDecode(const QByteArray &string) const;
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;
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;
77 //! Encode a string using the server (network) encoding.
78 QByteArray serverEncode(const QString &string) const;
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;
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;
86 inline QString channelKey(const QString &channel) const { return _channelKeys.value(channel.toLower(), QString()); }
87 inline QStringList persistentChannels() const { return _channelKeys.keys(); }
89 inline bool isAutoWhoInProgress(const QString &channel) const { return _autoWhoInProgress.value(channel.toLower(), 0); }
92 // void setServerOptions();
93 void connectToIrc(bool reconnecting = false);
94 void disconnectFromIrc(bool requested = true);
95 void userInput(BufferInfo bufferInfo, QString msg);
97 void putRawLine(QByteArray input);
98 int lastParamOverrun(const QString &cmd, const QList<QByteArray> ¶ms);
99 void putCmd(const QString &cmd, const QList<QByteArray> ¶ms, const QByteArray &prefix = QByteArray());
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);
106 bool setAutoWhoDone(const QString &channel);
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);
120 void quitRequested(NetworkId networkId);
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);
127 void sslErrors(const QVariant &errorData);
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 ¤tServer);
141 void autoReconnectSettingsChanged();
142 void doAutoReconnect();
144 void startAutoWhoCycle();
145 void nickChanged(const QString &newNick, const QString &oldNick); // this signal is inteded to rename query buffers in the storage backend
147 #ifndef QT_NO_OPENSSL
148 void socketEncrypted();
149 void sslErrors(const QList<QSslError> &errors);
152 void fillBucketAndProcessQueue();
155 #ifndef QT_NO_OPENSSL
161 Network::ConnectionState _connectionState;
164 CoreSession *_coreSession;
165 BufferInfo _statusBufferInfo;
167 IrcServerHandler *_ircServerHandler;
168 UserInputHandler *_userInputHandler;
169 CtcpHandler *_ctcpHandler;
171 QHash<QString, QString> _channelKeys; // stores persistent channels and their passwords, if any
173 QTimer _autoReconnectTimer;
175 int _autoReconnectCount;
177 QTimer _socketCloseTimer;
179 /* this flag triggers quitRequested() once the socket is closed
180 * it is needed to determine whether or not the connection needs to be
181 *in the automatic session restore. */
184 bool _previousConnectionAttemptFailed;
185 int _lastUsedServerlistIndex;
187 bool _autoWhoEnabled;
188 QStringList _autoWhoQueue;
189 QHash<QString, int> _autoWhoInProgress;
190 int _autoWhoInterval;
191 int _autoWhoNickLimit;
193 QTimer _autoWhoTimer, _autoWhoCycleTimer;
195 QTimer _tokenBucketTimer;
196 int _messagesPerSecond; // token refill speed
197 int _burstSize; // size of the token bucket
198 int _tokenBucket; // the virtual bucket that holds the tokens
199 QList<QByteArray> _msgQueue;
201 void writeToSocket(QByteArray s);
203 class ParseError : public Exception {
205 ParseError(QString cmd, QString prefix, QStringList params);
208 class UnknownCmdError : public Exception {
210 UnknownCmdError(QString cmd, QString prefix, QStringList params);