Greatly simplify the wrap point computation
[quassel.git] / src / core / corenetwork.h
1 /***************************************************************************
2  *   Copyright (C) 2005-09 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 CORENETWORK_H
22 #define CORENETWORK_H
23
24 #include "network.h"
25 #include "coreircchannel.h"
26
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
38 class CoreIdentity;
39 class IrcServerHandler;
40 class UserInputHandler;
41 class CtcpHandler;
42
43 class CoreNetwork : public Network {
44   SYNCABLE_OBJECT
45   Q_OBJECT
46
47 public:
48   CoreNetwork(const NetworkId &networkid, CoreSession *session);
49   ~CoreNetwork();
50   inline virtual const QMetaObject *syncMetaObject() const { return &Network::staticMetaObject; }
51
52   inline CoreIdentity *identityPtr() const { return coreSession()->identity(identity()); }
53   inline CoreSession *coreSession() const { return _coreSession; }
54   inline CoreNetworkConfig *networkConfig() const { return coreSession()->networkConfig(); }
55
56   inline IrcServerHandler *ircServerHandler() const { return _ircServerHandler; }
57   inline UserInputHandler *userInputHandler() const { return _userInputHandler; }
58   inline CtcpHandler *ctcpHandler() const { return _ctcpHandler; }
59
60   //! Decode a string using the server (network) decoding.
61   inline QString serverDecode(const QByteArray &string) const { return decodeServerString(string); }
62
63   //! Decode a string using a channel-specific encoding if one is set (and use the standard encoding else).
64   QString channelDecode(const QString &channelName, const QByteArray &string) const;
65
66   //! Decode a string using an IrcUser-specific encoding, if one exists (using the standaed encoding else).
67   QString userDecode(const QString &userNick, const QByteArray &string) const;
68
69   //! Encode a string using the server (network) encoding.
70   inline QByteArray serverEncode(const QString &string) const { return encodeServerString(string); }
71
72   //! Encode a string using the channel-specific encoding, if set, and use the standard encoding else.
73   QByteArray channelEncode(const QString &channelName, const QString &string) const;
74
75   //! Encode a string using the user-specific encoding, if set, and use the standard encoding else.
76   QByteArray userEncode(const QString &userNick, const QString &string) const;
77
78   inline QString channelKey(const QString &channel) const { return _channelKeys.value(channel.toLower(), QString()); }
79
80   inline bool isAutoWhoInProgress(const QString &channel) const { return _autoWhoPending.value(channel.toLower(), 0); }
81
82   inline UserId userId() const { return _coreSession->user(); }
83
84 public slots:
85   virtual void setMyNick(const QString &mynick);
86
87   virtual void requestConnect() const;
88   virtual void requestDisconnect() const;
89   virtual void requestSetNetworkInfo(const NetworkInfo &info);
90
91   virtual void setUseAutoReconnect(bool);
92   virtual void setAutoReconnectInterval(quint32);
93   virtual void setAutoReconnectRetries(quint16);
94
95   void setPingInterval(int interval);
96
97   void connectToIrc(bool reconnecting = false);
98   void disconnectFromIrc(bool requested = true, const QString &reason = QString(), bool withReconnect = false);
99
100   void userInput(BufferInfo bufferInfo, QString msg);
101   void putRawLine(QByteArray input);
102   void putCmd(const QString &cmd, const QList<QByteArray> &params, const QByteArray &prefix = QByteArray());
103
104   void setChannelJoined(const QString &channel);
105   void setChannelParted(const QString &channel);
106   void addChannelKey(const QString &channel, const QString &key);
107   void removeChannelKey(const QString &channel);
108
109   void setAutoWhoEnabled(bool enabled);
110   void setAutoWhoInterval(int interval);
111   void setAutoWhoDelay(int delay);
112
113   bool setAutoWhoDone(const QString &channel);
114
115   Server usedServer() const;
116
117   inline void resetPingTimeout() { _pingCount = 0; }
118
119   inline void displayMsg(Message::Type msgType, BufferInfo::Type bufferType, const QString &target, const QString &text, const QString &sender = "", Message::Flags flags = Message::None) {
120     emit displayMsg(networkId(), msgType, bufferType, target, text, sender, flags);
121   }
122
123 signals:
124   void recvRawServerMsg(QString);
125   void displayStatusMsg(QString);
126   void displayMsg(NetworkId, Message::Type, BufferInfo::Type, const QString &target, const QString &text, const QString &sender = "", Message::Flags flags = Message::None);
127   void disconnected(NetworkId networkId);
128   void connectionError(const QString &errorMsg);
129
130   void quitRequested(NetworkId networkId);
131   void sslErrors(const QVariant &errorData);
132
133 protected:
134   inline virtual IrcChannel *ircChannelFactory(const QString &channelname) { return new CoreIrcChannel(channelname, this); }
135
136 private slots:
137   void socketHasData();
138   void socketError(QAbstractSocket::SocketError);
139   void socketInitialized();
140   inline void socketCloseTimeout() { socket.disconnectFromHost(); }
141   void socketDisconnected();
142   void socketStateChanged(QAbstractSocket::SocketState);
143   void networkInitialized();
144
145   void sendPerform();
146   void restoreUserModes();
147   void doAutoReconnect();
148   void sendPing();
149   void enablePingTimeout(bool enable = true);
150   void disablePingTimeout();
151   void sendAutoWho();
152   void startAutoWhoCycle();
153
154 #ifdef HAVE_SSL
155   void sslErrors(const QList<QSslError> &errors);
156 #endif
157
158   void fillBucketAndProcessQueue();
159
160   void writeToSocket(const QByteArray &data);
161
162 private:
163   CoreSession *_coreSession;
164
165 #ifdef HAVE_SSL
166   QSslSocket socket;
167 #else
168   QTcpSocket socket;
169 #endif
170
171   IrcServerHandler *_ircServerHandler;
172   UserInputHandler *_userInputHandler;
173   CtcpHandler *_ctcpHandler;
174
175   QHash<QString, QString> _channelKeys;  // stores persistent channels and their passwords, if any
176
177   QTimer _autoReconnectTimer;
178   int _autoReconnectCount;
179
180   QTimer _socketCloseTimer;
181
182   /* this flag triggers quitRequested() once the socket is closed
183    * it is needed to determine whether or not the connection needs to be
184    * in the automatic session restore. */
185   bool _quitRequested;
186   QString _quitReason;
187
188   bool _previousConnectionAttemptFailed;
189   int _lastUsedServerIndex;
190
191   QTimer _pingTimer;
192   uint _lastPingTime;
193   uint _pingCount;
194
195   QStringList _autoWhoQueue;
196   QHash<QString, int> _autoWhoPending;
197   QTimer _autoWhoTimer, _autoWhoCycleTimer;
198
199   QTimer _tokenBucketTimer;
200   int _messageDelay;        // token refill speed in ms
201   int _burstSize;           // size of the token bucket
202   int _tokenBucket;         // the virtual bucket that holds the tokens
203   QList<QByteArray> _msgQueue;
204 };
205
206 #endif //CORENETWORK_H