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