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.contains(channel); }
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 void putCmd(const QString &cmd, const QVariantList ¶ms, const QByteArray &prefix = QByteArray());
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);
105 bool setAutoWhoDone(const QString &channel);
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 = "", quint8 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);
119 void quitRequested(NetworkId networkId);
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);
126 void sslErrors(const QVariant &errorData);
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 ¤tServer);
139 void autoReconnectSettingsChanged();
140 void doAutoReconnect();
142 void startAutoWhoCycle();
143 void nickChanged(const QString &newNick, const QString &oldNick); // this signal is inteded to rename query buffers in the storage backend
145 #ifndef QT_NO_OPENSSL
146 void socketEncrypted();
147 void sslErrors(const QList<QSslError> &errors);
151 #ifndef QT_NO_OPENSSL
157 Network::ConnectionState _connectionState;
160 CoreSession *_coreSession;
161 BufferInfo _statusBufferInfo;
163 IrcServerHandler *_ircServerHandler;
164 UserInputHandler *_userInputHandler;
165 CtcpHandler *_ctcpHandler;
167 QHash<QString, QString> _channelKeys; // stores persistent channels and their passwords, if any
169 QTimer _autoReconnectTimer;
170 int _autoReconnectCount;
172 bool _previousConnectionAttemptFailed;
173 int _lastUsedServerlistIndex;
175 bool _autoWhoEnabled;
176 QStringList _autoWhoQueue;
177 QSet<QString> _autoWhoInProgress;
178 int _autoWhoInterval;
179 int _autoWhoNickLimit;
181 QTimer _autoWhoTimer, _autoWhoCycleTimer;
184 class ParseError : public Exception {
186 ParseError(QString cmd, QString prefix, QStringList params);
189 class UnknownCmdError : public Exception {
191 UnknownCmdError(QString cmd, QString prefix, QStringList params);