Making Quassel compile on Mac OS _AGAIN_ -.-
[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   Q_OBJECT
45
46 public:
47   CoreNetwork(const NetworkId &networkid, CoreSession *session);
48   ~CoreNetwork();
49   inline virtual const QMetaObject *syncMetaObject() const { return &Network::staticMetaObject; }
50
51   inline CoreIdentity *identityPtr() const { return coreSession()->identity(identity()); }
52   inline CoreSession *coreSession() const { return _coreSession; }
53
54   inline IrcServerHandler *ircServerHandler() const { return _ircServerHandler; }
55   inline UserInputHandler *userInputHandler() const { return _userInputHandler; }
56   inline CtcpHandler *ctcpHandler() const { return _ctcpHandler; }
57
58   //! Decode a string using the server (network) decoding.
59   inline QString serverDecode(const QByteArray &string) const { return decodeServerString(string); }
60
61   //! Decode a string using a channel-specific encoding if one is set (and use the standard encoding else).
62   QString channelDecode(const QString &channelName, const QByteArray &string) const;
63
64   //! Decode a string using an IrcUser-specific encoding, if one exists (using the standaed encoding else).
65   QString userDecode(const QString &userNick, const QByteArray &string) const;
66
67   //! Encode a string using the server (network) encoding.
68   inline QByteArray serverEncode(const QString &string) const { return encodeServerString(string); }
69
70   //! Encode a string using the channel-specific encoding, if set, and use the standard encoding else.
71   QByteArray channelEncode(const QString &channelName, const QString &string) const;
72
73   //! Encode a string using the user-specific encoding, if set, and use the standard encoding else.
74   QByteArray userEncode(const QString &userNick, const QString &string) const;
75
76   inline QString channelKey(const QString &channel) const { return _channelKeys.value(channel.toLower(), QString()); }
77
78   inline bool isAutoWhoInProgress(const QString &channel) const { return _autoWhoInProgress.value(channel.toLower(), 0); }
79
80   inline UserId userId() const { return _coreSession->user(); }
81
82 public slots:
83   virtual void setMyNick(const QString &mynick);
84
85   virtual void requestConnect() const;
86   virtual void requestDisconnect() const;
87   virtual void requestSetNetworkInfo(const NetworkInfo &info);
88
89   virtual void setUseAutoReconnect(bool);
90   virtual void setAutoReconnectInterval(quint32);
91   virtual void setAutoReconnectRetries(quint16);
92
93   void connectToIrc(bool reconnecting = false);
94   void disconnectFromIrc(bool requested = true, const QString &reason = QString());
95
96   void userInput(BufferInfo bufferInfo, QString msg);
97   void putRawLine(QByteArray input);
98   void putCmd(const QString &cmd, const QList<QByteArray> &params, const QByteArray &prefix = QByteArray());
99
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);
104
105   bool setAutoWhoDone(const QString &channel);
106
107   Server usedServer() const;
108
109 signals:
110   void recvRawServerMsg(QString);
111   void displayStatusMsg(QString);
112   void displayMsg(Message::Type, BufferInfo::Type, QString target, QString text, QString sender = "", Message::Flags flags = Message::None);
113   void disconnected(NetworkId networkId);
114   void connectionError(const QString &errorMsg);
115
116   void quitRequested(NetworkId networkId);
117   void sslErrors(const QVariant &errorData);
118
119 protected:
120   inline virtual IrcChannel *ircChannelFactory(const QString &channelname) { return new CoreIrcChannel(channelname, this); }
121
122 private slots:
123   void socketHasData();
124   void socketError(QAbstractSocket::SocketError);
125   void socketInitialized();
126   inline void socketCloseTimeout() { socket.disconnectFromHost(); }
127   void socketDisconnected();
128   void socketStateChanged(QAbstractSocket::SocketState);
129   void networkInitialized();
130
131   void sendPerform();
132   void doAutoReconnect();
133   void sendPing();
134   void sendAutoWho();
135   void startAutoWhoCycle();
136
137 #ifdef HAVE_SSL
138   void sslErrors(const QList<QSslError> &errors);
139 #endif
140
141   void fillBucketAndProcessQueue();
142
143   void writeToSocket(const QByteArray &data);
144
145 private:
146   CoreSession *_coreSession;
147
148 #ifdef HAVE_SSL
149   QSslSocket socket;
150 #else
151   QTcpSocket socket;
152 #endif
153
154   IrcServerHandler *_ircServerHandler;
155   UserInputHandler *_userInputHandler;
156   CtcpHandler *_ctcpHandler;
157
158   QHash<QString, QString> _channelKeys;  // stores persistent channels and their passwords, if any
159
160   QTimer _autoReconnectTimer;
161   int _autoReconnectCount;
162
163   QTimer _socketCloseTimer;
164
165   /* this flag triggers quitRequested() once the socket is closed
166    * it is needed to determine whether or not the connection needs to be
167    * in the automatic session restore. */
168   bool _quitRequested;
169
170   bool _previousConnectionAttemptFailed;
171   int _lastUsedServerIndex;
172
173   QTimer _pingTimer;
174
175   bool _autoWhoEnabled;
176   QStringList _autoWhoQueue;
177   QHash<QString, int> _autoWhoInProgress;
178   int _autoWhoInterval;
179   int _autoWhoNickLimit;
180   int _autoWhoDelay;
181   QTimer _autoWhoTimer, _autoWhoCycleTimer;
182
183   QTimer _tokenBucketTimer;
184   int _messagesPerSecond;   // token refill speed
185   int _burstSize;           // size of the token bucket
186   int _tokenBucket;         // the virtual bucket that holds the tokens
187   QList<QByteArray> _msgQueue;
188 };
189
190 #endif //CORENETWORK_H