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