fixing typo
[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 #include "coreircuser.h"
27
28 #include <QTimer>
29
30 #ifdef HAVE_SSL
31 # include <QSslSocket>
32 # include <QSslError>
33 #else
34 # include <QTcpSocket>
35 #endif
36
37 #ifdef HAVE_QCA2
38 #  include "cipher.h"
39 #endif
40
41 #include "coresession.h"
42
43 class CoreIdentity;
44 class CoreUserInputHandler;
45 class CoreIgnoreListManager;
46
47 class CoreNetwork : public Network {
48   SYNCABLE_OBJECT
49   Q_OBJECT
50
51 public:
52   CoreNetwork(const NetworkId &networkid, CoreSession *session);
53   ~CoreNetwork();
54   inline virtual const QMetaObject *syncMetaObject() const { return &Network::staticMetaObject; }
55
56   inline CoreIdentity *identityPtr() const { return coreSession()->identity(identity()); }
57   inline CoreSession *coreSession() const { return _coreSession; }
58   inline CoreNetworkConfig *networkConfig() const { return coreSession()->networkConfig(); }
59
60   inline CoreUserInputHandler *userInputHandler() const { return _userInputHandler; }
61   inline CoreIgnoreListManager *ignoreListManager() { return coreSession()->ignoreListManager(); }
62
63   //! Decode a string using the server (network) decoding.
64   inline QString serverDecode(const QByteArray &string) const { return decodeServerString(string); }
65
66   //! Decode a string using a channel-specific encoding if one is set (and use the standard encoding else).
67   QString channelDecode(const QString &channelName, const QByteArray &string) const;
68
69   //! Decode a string using an IrcUser-specific encoding, if one exists (using the standaed encoding else).
70   QString userDecode(const QString &userNick, const QByteArray &string) const;
71
72   //! Encode a string using the server (network) encoding.
73   inline QByteArray serverEncode(const QString &string) const { return encodeServerString(string); }
74
75   //! Encode a string using the channel-specific encoding, if set, and use the standard encoding else.
76   QByteArray channelEncode(const QString &channelName, const QString &string) const;
77
78   //! Encode a string using the user-specific encoding, if set, and use the standard encoding else.
79   QByteArray userEncode(const QString &userNick, const QString &string) const;
80
81   inline QString channelKey(const QString &channel) const { return _channelKeys.value(channel.toLower(), QString()); }
82
83   inline bool isAutoWhoInProgress(const QString &channel) const { return _autoWhoPending.value(channel.toLower(), 0); }
84
85   inline UserId userId() const { return _coreSession->user(); }
86
87 public slots:
88   virtual void setMyNick(const QString &mynick);
89
90   virtual void requestConnect() const;
91   virtual void requestDisconnect() const;
92   virtual void requestSetNetworkInfo(const NetworkInfo &info);
93
94   virtual void setUseAutoReconnect(bool);
95   virtual void setAutoReconnectInterval(quint32);
96   virtual void setAutoReconnectRetries(quint16);
97
98   void setPingInterval(int interval);
99
100   void connectToIrc(bool reconnecting = false);
101   void disconnectFromIrc(bool requested = true, const QString &reason = QString(), bool withReconnect = false);
102
103   void userInput(BufferInfo bufferInfo, QString msg);
104   void putRawLine(QByteArray input);
105   void putCmd(const QString &cmd, const QList<QByteArray> &params, const QByteArray &prefix = QByteArray());
106
107   void setChannelJoined(const QString &channel);
108   void setChannelParted(const QString &channel);
109   void addChannelKey(const QString &channel, const QString &key);
110   void removeChannelKey(const QString &channel);
111
112   // Blowfish stuff
113 #ifdef HAVE_QCA2
114   Cipher *cipher(const QString &recipient) const;
115   QByteArray cipherKey(const QString &recipient) const;
116   void setCipherKey(const QString &recipient, const QByteArray &key);
117 #endif
118
119   void setAutoWhoEnabled(bool enabled);
120   void setAutoWhoInterval(int interval);
121   void setAutoWhoDelay(int delay);
122
123   bool setAutoWhoDone(const QString &channel);
124
125   void updateIssuedModes(const QString &requestedModes);
126   void updatePersistentModes(QString addModes, QString removeModes);
127   void resetPersistentModes();
128
129   Server usedServer() const;
130
131   inline void resetPingTimeout() { _pingCount = 0; }
132
133   inline void displayMsg(Message::Type msgType, BufferInfo::Type bufferType, const QString &target, const QString &text, const QString &sender = "", Message::Flags flags = Message::None) {
134     emit displayMsg(networkId(), msgType, bufferType, target, text, sender, flags);
135   }
136
137 signals:
138   void recvRawServerMsg(QString);
139   void displayStatusMsg(QString);
140   void displayMsg(NetworkId, Message::Type, BufferInfo::Type, const QString &target, const QString &text, const QString &sender = "", Message::Flags flags = Message::None);
141   void disconnected(NetworkId networkId);
142   void connectionError(const QString &errorMsg);
143
144   void quitRequested(NetworkId networkId);
145   void sslErrors(const QVariant &errorData);
146
147 protected:
148   inline virtual IrcChannel *ircChannelFactory(const QString &channelname) { return new CoreIrcChannel(channelname, this); }
149   inline virtual IrcUser *ircUserFactory(const QString &hostmask) { return new CoreIrcUser(hostmask, this); }
150
151 protected slots:
152   // TODO: remove cached cipher keys, when appropriate
153   //virtual void removeIrcUser(IrcUser *ircuser);
154   //virtual void removeIrcChannel(IrcChannel *ircChannel);
155   //virtual void removeChansAndUsers();
156
157 private slots:
158   void socketHasData();
159   void socketError(QAbstractSocket::SocketError);
160   void socketInitialized();
161   inline void socketCloseTimeout() { socket.disconnectFromHost(); }
162   void socketDisconnected();
163   void socketStateChanged(QAbstractSocket::SocketState);
164   void networkInitialized();
165
166   void sendPerform();
167   void restoreUserModes();
168   void doAutoReconnect();
169   void sendPing();
170   void enablePingTimeout(bool enable = true);
171   void disablePingTimeout();
172   void sendAutoWho();
173   void startAutoWhoCycle();
174
175 #ifdef HAVE_SSL
176   void sslErrors(const QList<QSslError> &errors);
177 #endif
178
179   void fillBucketAndProcessQueue();
180
181   void writeToSocket(const QByteArray &data);
182
183 private:
184   CoreSession *_coreSession;
185
186 #ifdef HAVE_SSL
187   QSslSocket socket;
188 #else
189   QTcpSocket socket;
190 #endif
191
192   CoreUserInputHandler *_userInputHandler;
193
194   QHash<QString, QString> _channelKeys;  // stores persistent channels and their passwords, if any
195
196   QTimer _autoReconnectTimer;
197   int _autoReconnectCount;
198
199   QTimer _socketCloseTimer;
200
201   /* this flag triggers quitRequested() once the socket is closed
202    * it is needed to determine whether or not the connection needs to be
203    * in the automatic session restore. */
204   bool _quitRequested;
205   QString _quitReason;
206
207   bool _previousConnectionAttemptFailed;
208   int _lastUsedServerIndex;
209
210   QTimer _pingTimer;
211   uint _lastPingTime;
212   uint _pingCount;
213
214   QStringList _autoWhoQueue;
215   QHash<QString, int> _autoWhoPending;
216   QTimer _autoWhoTimer, _autoWhoCycleTimer;
217
218   QTimer _tokenBucketTimer;
219   int _messageDelay;        // token refill speed in ms
220   int _burstSize;           // size of the token bucket
221   int _tokenBucket;         // the virtual bucket that holds the tokens
222   QList<QByteArray> _msgQueue;
223
224   QString _requestedUserModes; // 2 strings separated by a '-' character. first part are requested modes to add, the second to remove
225
226   // Blowfish key map
227   QHash<QString, QByteArray> _cipherKeys;
228 };
229
230 #endif //CORENETWORK_H