Adding some debug output to NetworkConnection for the next time we run into the endless
[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 "identity.h"
37 #include "message.h"
38 #include "network.h"
39 #include "signalproxy.h"
40
41 class CoreSession;
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   NetworkId networkId() const;
56   QString networkName() const;
57   Network *network() const;
58   Identity *identity() const;
59   CoreSession *coreSession() const;
60
61   bool isConnected() const;
62   Network::ConnectionState connectionState() const;
63
64   IrcServerHandler *ircServerHandler() const;
65   UserInputHandler *userInputHandler() const;
66   CtcpHandler *ctcpHandler() const;
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 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   void putCmd(const QString &cmd, const QVariantList &params, const QByteArray &prefix = QByteArray());
97
98   void setChannelJoined(const QString &channel);
99   void setChannelParted(const QString &channel);
100   void addChannelKey(const QString &channel, const QString &key);
101   void removeChannelKey(const QString &channel);
102
103 private slots:
104   void sendPerform();
105   void autoReconnectSettingsChanged();
106   void doAutoReconnect();
107   void sendWho();
108   void nickChanged(const QString &newNick, const QString &oldNick); // this signal is inteded to rename query buffers in the storage backend
109
110 signals:
111   // #void networkState(QString net, QVariantMap data);
112   void recvRawServerMsg(QString);
113   void displayStatusMsg(QString);
114   //void displayMsg(Message msg);
115   void displayMsg(Message::Type, BufferInfo::Type, QString target, QString text, QString sender = "", quint8 flags = Message::None);
116   void connected(NetworkId networkId);   ///< Emitted after receipt of 001 to indicate that we can now send data to the IRC server
117   void disconnected(NetworkId networkId);
118   void connectionStateChanged(Network::ConnectionState);
119   void connectionInitialized(); ///< Emitted after receipt of 001 to indicate that we can now send data to the IRC server
120   void connectionError(const QString &errorMsg);
121
122   void quitRequested(NetworkId networkId);
123
124   //void queryRequested(QString network, QString nick);
125   void nickChanged(const NetworkId &networkId, const QString &newNick, const QString &oldNick); // this signal is inteded to rename query buffers in the storage backend
126   void channelJoined(NetworkId, const QString &channel, const QString &key = QString());
127   void channelParted(NetworkId, const QString &channel);
128
129   void sslErrors(const QVariant &errorData);
130
131 private slots:
132   void socketHasData();
133   void socketError(QAbstractSocket::SocketError);
134   void socketConnected();
135   void socketInitialized();
136   void socketDisconnected();
137   void socketStateChanged(QAbstractSocket::SocketState);
138   void setConnectionState(Network::ConnectionState);
139   void networkInitialized(const QString &currentServer);
140
141 #ifndef QT_NO_OPENSSL
142   void socketEncrypted();
143   void sslErrors(const QList<QSslError> &errors);
144 #endif
145
146 private:
147 #ifndef QT_NO_OPENSSL
148   QSslSocket socket;
149 #else
150   QTcpSocket socket;
151 #endif
152
153   Network::ConnectionState _connectionState;
154
155   Network *_network;
156   CoreSession *_coreSession;
157   BufferInfo _statusBufferInfo;
158
159   IrcServerHandler *_ircServerHandler;
160   UserInputHandler *_userInputHandler;
161   CtcpHandler *_ctcpHandler;
162
163   QHash<QString, QString> _channelKeys;  // stores persistent channels and their passwords, if any
164
165   QTimer _autoReconnectTimer;
166   int _autoReconnectCount;
167
168   QTimer _whoTimer;
169
170   bool _previousConnectionAttemptFailed;
171   int _lastUsedServerlistIndex;
172   
173   QByteArray lastMsgReceived;  //  FIXME debug
174   
175   class ParseError : public Exception {
176   public:
177     ParseError(QString cmd, QString prefix, QStringList params);
178   };
179
180   class UnknownCmdError : public Exception {
181   public:
182     UnknownCmdError(QString cmd, QString prefix, QStringList params);
183   };
184 };
185
186 #endif