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"
31 # include <QSslSocket>
34 # include <QTcpSocket>
41 #include "coresession.h"
44 class IrcServerHandler;
45 class CoreUserInputHandler;
47 class CoreIgnoreListManager;
49 class CoreNetwork : public Network {
54 CoreNetwork(const NetworkId &networkid, CoreSession *session);
56 inline virtual const QMetaObject *syncMetaObject() const { return &Network::staticMetaObject; }
58 inline CoreIdentity *identityPtr() const { return coreSession()->identity(identity()); }
59 inline CoreSession *coreSession() const { return _coreSession; }
60 inline CoreNetworkConfig *networkConfig() const { return coreSession()->networkConfig(); }
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(); }
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(); }
92 virtual void setMyNick(const QString &mynick);
94 virtual void requestConnect() const;
95 virtual void requestDisconnect() const;
96 virtual void requestSetNetworkInfo(const NetworkInfo &info);
98 virtual void setUseAutoReconnect(bool);
99 virtual void setAutoReconnectInterval(quint32);
100 virtual void setAutoReconnectRetries(quint16);
102 void setPingInterval(int interval);
104 void connectToIrc(bool reconnecting = false);
105 void disconnectFromIrc(bool requested = true, const QString &reason = QString(), bool withReconnect = false);
107 void userInput(BufferInfo bufferInfo, QString msg);
108 void putRawLine(QByteArray input);
109 void putCmd(const QString &cmd, const QList<QByteArray> ¶ms, const QByteArray &prefix = QByteArray());
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);
118 Cipher *cipher(const QString &recipient) const;
119 QByteArray cipherKey(const QString &recipient) const;
120 void setCipherKey(const QString &recipient, const QByteArray &key);
123 void setAutoWhoEnabled(bool enabled);
124 void setAutoWhoInterval(int interval);
125 void setAutoWhoDelay(int delay);
127 bool setAutoWhoDone(const QString &channel);
129 void updateIssuedModes(const QString &requestedModes);
130 void updatePersistentModes(QString addModes, QString removeModes);
131 void resetPersistentModes();
133 Server usedServer() const;
135 inline void resetPingTimeout() { _pingCount = 0; }
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);
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);
148 void quitRequested(NetworkId networkId);
149 void sslErrors(const QVariant &errorData);
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); }
156 // TODO: remove cached cipher keys, when appropriate
157 //virtual void removeIrcUser(IrcUser *ircuser);
158 //virtual void removeIrcChannel(IrcChannel *ircChannel);
159 //virtual void removeChansAndUsers();
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();
171 void restoreUserModes();
172 void doAutoReconnect();
174 void enablePingTimeout(bool enable = true);
175 void disablePingTimeout();
177 void startAutoWhoCycle();
180 void sslErrors(const QList<QSslError> &errors);
183 void fillBucketAndProcessQueue();
185 void writeToSocket(const QByteArray &data);
188 CoreSession *_coreSession;
196 IrcServerHandler *_ircServerHandler;
197 CoreUserInputHandler *_userInputHandler;
198 CtcpHandler *_ctcpHandler;
200 QHash<QString, QString> _channelKeys; // stores persistent channels and their passwords, if any
202 QTimer _autoReconnectTimer;
203 int _autoReconnectCount;
205 QTimer _socketCloseTimer;
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. */
213 bool _previousConnectionAttemptFailed;
214 int _lastUsedServerIndex;
220 QStringList _autoWhoQueue;
221 QHash<QString, int> _autoWhoPending;
222 QTimer _autoWhoTimer, _autoWhoCycleTimer;
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;
230 QString _requestedUserModes; // 2 strings separated by a '-' character. first part are requested modes to add, the second to remove
233 QHash<QString, QByteArray> _cipherKeys;
236 #endif //CORENETWORK_H