1 /***************************************************************************
2 * Copyright (C) 2005-09 by the Quassel Project *
3 * devel@quassel-irc.org *
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. *
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. *
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 ***************************************************************************/
25 #include "coreircchannel.h"
26 #include "coreircuser.h"
32 # include <QSslSocket>
35 # include <QTcpSocket>
37 #include <QAbstractSocket>
43 #include "coresession.h"
46 class CoreUserInputHandler;
47 class CoreIgnoreListManager;
50 class CoreNetwork : public Network {
55 CoreNetwork(const NetworkId &networkid, CoreSession *session);
57 inline virtual const QMetaObject *syncMetaObject() const { return &Network::staticMetaObject; }
59 inline CoreIdentity *identityPtr() const { return coreSession()->identity(identity()); }
60 inline CoreSession *coreSession() const { return _coreSession; }
61 inline CoreNetworkConfig *networkConfig() const { return coreSession()->networkConfig(); }
63 inline CoreUserInputHandler *userInputHandler() const { return _userInputHandler; }
64 inline CoreIgnoreListManager *ignoreListManager() { return coreSession()->ignoreListManager(); }
66 //! Decode a string using the server (network) decoding.
67 inline QString serverDecode(const QByteArray &string) const { return decodeServerString(string); }
69 //! Decode a string using a channel-specific encoding if one is set (and use the standard encoding else).
70 QString channelDecode(const QString &channelName, const QByteArray &string) const;
72 //! Decode a string using an IrcUser-specific encoding, if one exists (using the standaed encoding else).
73 QString userDecode(const QString &userNick, const QByteArray &string) const;
75 //! Encode a string using the server (network) encoding.
76 inline QByteArray serverEncode(const QString &string) const { return encodeServerString(string); }
78 //! Encode a string using the channel-specific encoding, if set, and use the standard encoding else.
79 QByteArray channelEncode(const QString &channelName, const QString &string) const;
81 //! Encode a string using the user-specific encoding, if set, and use the standard encoding else.
82 QByteArray userEncode(const QString &userNick, const QString &string) const;
84 inline QString channelKey(const QString &channel) const { return _channelKeys.value(channel.toLower(), QString()); }
86 inline bool isAutoWhoInProgress(const QString &channel) const { return _autoWhoPending.value(channel.toLower(), 0); }
88 inline UserId userId() const { return _coreSession->user(); }
90 inline QAbstractSocket::SocketState socketState() { return socket.state(); }
91 inline bool socketConnected() { return socket.state() == QAbstractSocket::ConnectedState; }
92 inline QHostAddress localAddress() { return socket.localAddress(); }
93 inline QHostAddress peerAddress() { return socket.peerAddress(); }
94 inline quint16 localPort() { return socket.localPort(); }
95 inline quint16 peerPort() { return socket.peerPort(); }
98 virtual void setMyNick(const QString &mynick);
100 virtual void requestConnect() const;
101 virtual void requestDisconnect() const;
102 virtual void requestSetNetworkInfo(const NetworkInfo &info);
104 virtual void setUseAutoReconnect(bool);
105 virtual void setAutoReconnectInterval(quint32);
106 virtual void setAutoReconnectRetries(quint16);
108 void setPingInterval(int interval);
110 void connectToIrc(bool reconnecting = false);
111 void disconnectFromIrc(bool requested = true, const QString &reason = QString(), bool withReconnect = false);
113 void userInput(BufferInfo bufferInfo, QString msg);
114 void putRawLine(QByteArray input);
115 void putCmd(const QString &cmd, const QList<QByteArray> ¶ms, const QByteArray &prefix = QByteArray());
117 void setChannelJoined(const QString &channel);
118 void setChannelParted(const QString &channel);
119 void addChannelKey(const QString &channel, const QString &key);
120 void removeChannelKey(const QString &channel);
124 Cipher *cipher(const QString &recipient) const;
125 QByteArray cipherKey(const QString &recipient) const;
126 void setCipherKey(const QString &recipient, const QByteArray &key);
129 void setAutoWhoEnabled(bool enabled);
130 void setAutoWhoInterval(int interval);
131 void setAutoWhoDelay(int delay);
133 bool setAutoWhoDone(const QString &channel);
135 void updateIssuedModes(const QString &requestedModes);
136 void updatePersistentModes(QString addModes, QString removeModes);
137 void resetPersistentModes();
139 Server usedServer() const;
141 inline void resetPingTimeout() { _pingCount = 0; }
143 inline void displayMsg(Message::Type msgType, BufferInfo::Type bufferType, const QString &target, const QString &text, const QString &sender = "", Message::Flags flags = Message::None) {
144 emit displayMsg(networkId(), msgType, bufferType, target, text, sender, flags);
148 void recvRawServerMsg(QString);
149 void displayStatusMsg(QString);
150 void displayMsg(NetworkId, Message::Type, BufferInfo::Type, const QString &target, const QString &text, const QString &sender = "", Message::Flags flags = Message::None);
151 void disconnected(NetworkId networkId);
152 void connectionError(const QString &errorMsg);
154 void quitRequested(NetworkId networkId);
155 void sslErrors(const QVariant &errorData);
157 void newEvent(Event *event);
158 void newSocket(const CoreIdentity *identity, const QHostAddress &localAddress, quint16 localPort, const QHostAddress &peerAddress, quint16 peerPort);
161 inline virtual IrcChannel *ircChannelFactory(const QString &channelname) { return new CoreIrcChannel(channelname, this); }
162 inline virtual IrcUser *ircUserFactory(const QString &hostmask) { return new CoreIrcUser(hostmask, this); }
165 // TODO: remove cached cipher keys, when appropriate
166 //virtual void removeIrcUser(IrcUser *ircuser);
167 //virtual void removeIrcChannel(IrcChannel *ircChannel);
168 //virtual void removeChansAndUsers();
171 void socketHasData();
172 void socketError(QAbstractSocket::SocketError);
173 void socketInitialized();
174 inline void socketCloseTimeout() { socket.disconnectFromHost(); }
175 void socketDisconnected();
176 void socketStateChanged(QAbstractSocket::SocketState);
177 void networkInitialized();
180 void restoreUserModes();
181 void doAutoReconnect();
183 void enablePingTimeout(bool enable = true);
184 void disablePingTimeout();
186 void startAutoWhoCycle();
189 void sslErrors(const QList<QSslError> &errors);
192 void fillBucketAndProcessQueue();
194 void writeToSocket(const QByteArray &data);
197 CoreSession *_coreSession;
205 CoreUserInputHandler *_userInputHandler;
207 QHash<QString, QString> _channelKeys; // stores persistent channels and their passwords, if any
209 QTimer _autoReconnectTimer;
210 int _autoReconnectCount;
212 QTimer _socketCloseTimer;
214 /* this flag triggers quitRequested() once the socket is closed
215 * it is needed to determine whether or not the connection needs to be
216 * in the automatic session restore. */
220 bool _previousConnectionAttemptFailed;
221 int _lastUsedServerIndex;
227 QStringList _autoWhoQueue;
228 QHash<QString, int> _autoWhoPending;
229 QTimer _autoWhoTimer, _autoWhoCycleTimer;
231 QTimer _tokenBucketTimer;
232 int _messageDelay; // token refill speed in ms
233 int _burstSize; // size of the token bucket
234 int _tokenBucket; // the virtual bucket that holds the tokens
235 QList<QByteArray> _msgQueue;
237 QString _requestedUserModes; // 2 strings separated by a '-' character. first part are requested modes to add, the second to remove
240 QHash<QString, QByteArray> _cipherKeys;
243 #endif //CORENETWORK_H