1 /***************************************************************************
2 * Copyright (C) 2005-2015 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 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
19 ***************************************************************************/
25 #include "coreircchannel.h"
26 #include "coreircuser.h"
31 # include <QSslSocket>
34 # include <QTcpSocket>
41 #include "coresession.h"
46 class CoreUserInputHandler;
47 class CoreIgnoreListManager;
50 class CoreNetwork : public Network
56 CoreNetwork(const NetworkId &networkid, CoreSession *session);
58 inline virtual const QMetaObject *syncMetaObject() const { return &Network::staticMetaObject; }
60 inline CoreIdentity *identityPtr() const { return coreSession()->identity(identity()); }
61 inline CoreSession *coreSession() const { return _coreSession; }
62 inline CoreNetworkConfig *networkConfig() const { return coreSession()->networkConfig(); }
64 inline CoreUserInputHandler *userInputHandler() const { return _userInputHandler; }
65 inline CoreIgnoreListManager *ignoreListManager() { return coreSession()->ignoreListManager(); }
67 //! Decode a string using the server (network) decoding.
68 inline QString serverDecode(const QByteArray &string) const { return decodeServerString(string); }
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;
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;
76 //! Encode a string using the server (network) encoding.
77 inline QByteArray serverEncode(const QString &string) const { return encodeServerString(string); }
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;
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;
85 inline QString channelKey(const QString &channel) const { return _channelKeys.value(channel.toLower(), QString()); }
87 inline bool isAutoWhoInProgress(const QString &channel) const { return _autoWhoPending.value(channel.toLower(), 0); }
89 inline UserId userId() const { return _coreSession->user(); }
91 inline QAbstractSocket::SocketState socketState() const { return socket.state(); }
92 inline bool socketConnected() const { return socket.state() == QAbstractSocket::ConnectedState; }
93 inline QHostAddress localAddress() const { return socket.localAddress(); }
94 inline QHostAddress peerAddress() const { return socket.peerAddress(); }
95 inline quint16 localPort() const { return socket.localPort(); }
96 inline quint16 peerPort() const { return socket.peerPort(); }
98 QList<QList<QByteArray>> splitMessage(const QString &cmd, const QString &message, std::function<QList<QByteArray>(QString &)> cmdGenerator);
101 virtual void setMyNick(const QString &mynick);
103 virtual void requestConnect() const;
104 virtual void requestDisconnect() const;
105 virtual void requestSetNetworkInfo(const NetworkInfo &info);
107 virtual void setUseAutoReconnect(bool);
108 virtual void setAutoReconnectInterval(quint32);
109 virtual void setAutoReconnectRetries(quint16);
111 void setPingInterval(int interval);
113 void connectToIrc(bool reconnecting = false);
114 void disconnectFromIrc(bool requested = true, const QString &reason = QString(), bool withReconnect = false);
116 void userInput(BufferInfo bufferInfo, QString msg);
117 void putRawLine(QByteArray input);
118 void putCmd(const QString &cmd, const QList<QByteArray> ¶ms, const QByteArray &prefix = QByteArray());
119 void putCmd(const QString &cmd, const QList<QList<QByteArray>> ¶ms, const QByteArray &prefix = QByteArray());
121 void setChannelJoined(const QString &channel);
122 void setChannelParted(const QString &channel);
123 void addChannelKey(const QString &channel, const QString &key);
124 void removeChannelKey(const QString &channel);
128 Cipher *cipher(const QString &recipient);
129 QByteArray cipherKey(const QString &recipient) const;
130 void setCipherKey(const QString &recipient, const QByteArray &key);
131 bool cipherUsesCBC(const QString &target);
134 void setAutoWhoEnabled(bool enabled);
135 void setAutoWhoInterval(int interval);
136 void setAutoWhoDelay(int delay);
138 bool setAutoWhoDone(const QString &channel);
140 void updateIssuedModes(const QString &requestedModes);
141 void updatePersistentModes(QString addModes, QString removeModes);
142 void resetPersistentModes();
144 Server usedServer() const;
146 inline void resetPingTimeout() { _pingCount = 0; }
148 inline void displayMsg(Message::Type msgType, BufferInfo::Type bufferType, const QString &target, const QString &text, const QString &sender = "", Message::Flags flags = Message::None)
150 emit displayMsg(networkId(), msgType, bufferType, target, text, sender, flags);
155 void recvRawServerMsg(QString);
156 void displayStatusMsg(QString);
157 void displayMsg(NetworkId, Message::Type, BufferInfo::Type, const QString &target, const QString &text, const QString &sender = "", Message::Flags flags = Message::None);
158 void disconnected(NetworkId networkId);
159 void connectionError(const QString &errorMsg);
161 void quitRequested(NetworkId networkId);
162 void sslErrors(const QVariant &errorData);
164 void newEvent(Event *event);
165 void socketOpen(const CoreIdentity *identity, const QHostAddress &localAddress, quint16 localPort, const QHostAddress &peerAddress, quint16 peerPort);
166 void socketInitialized(const CoreIdentity *identity, const QHostAddress &localAddress, quint16 localPort, const QHostAddress &peerAddress, quint16 peerPort);
167 void socketDisconnected(const CoreIdentity *identity, const QHostAddress &localAddress, quint16 localPort, const QHostAddress &peerAddress, quint16 peerPort);
170 inline virtual IrcChannel *ircChannelFactory(const QString &channelname) { return new CoreIrcChannel(channelname, this); }
171 inline virtual IrcUser *ircUserFactory(const QString &hostmask) { return new CoreIrcUser(hostmask, this); }
174 // TODO: remove cached cipher keys, when appropriate
175 //virtual void removeIrcUser(IrcUser *ircuser);
176 //virtual void removeIrcChannel(IrcChannel *ircChannel);
177 //virtual void removeChansAndUsers();
180 void socketHasData();
181 void socketError(QAbstractSocket::SocketError);
182 void socketInitialized();
183 inline void socketCloseTimeout() { socket.abort(); }
184 void socketDisconnected();
185 void socketStateChanged(QAbstractSocket::SocketState);
186 void networkInitialized();
189 void restoreUserModes();
190 void doAutoReconnect();
192 void enablePingTimeout(bool enable = true);
193 void disablePingTimeout();
195 void startAutoWhoCycle();
198 void sslErrors(const QList<QSslError> &errors);
201 void fillBucketAndProcessQueue();
203 void writeToSocket(const QByteArray &data);
206 CoreSession *_coreSession;
214 CoreUserInputHandler *_userInputHandler;
216 QHash<QString, QString> _channelKeys; // stores persistent channels and their passwords, if any
218 QTimer _autoReconnectTimer;
219 int _autoReconnectCount;
221 QTimer _socketCloseTimer;
223 /* this flag triggers quitRequested() once the socket is closed
224 * it is needed to determine whether or not the connection needs to be
225 * in the automatic session restore. */
229 bool _previousConnectionAttemptFailed;
230 int _lastUsedServerIndex;
237 QStringList _autoWhoQueue;
238 QHash<QString, int> _autoWhoPending;
239 QTimer _autoWhoTimer, _autoWhoCycleTimer;
241 QTimer _tokenBucketTimer;
242 int _messageDelay; // token refill speed in ms
243 int _burstSize; // size of the token bucket
244 int _tokenBucket; // the virtual bucket that holds the tokens
245 QList<QByteArray> _msgQueue;
247 QString _requestedUserModes; // 2 strings separated by a '-' character. first part are requested modes to add, the second to remove
251 #endif //CORENETWORK_H