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"
30 # include <QSslSocket>
33 # include <QTcpSocket>
36 #include "coresession.h"
39 class IrcServerHandler;
40 class UserInputHandler;
43 class CoreNetwork : public Network {
47 CoreNetwork(const NetworkId &networkid, CoreSession *session);
49 inline virtual const QMetaObject *syncMetaObject() const { return &Network::staticMetaObject; }
51 inline CoreIdentity *identityPtr() const { return coreSession()->identity(identity()); }
52 inline CoreSession *coreSession() const { return _coreSession; }
54 inline IrcServerHandler *ircServerHandler() const { return _ircServerHandler; }
55 inline UserInputHandler *userInputHandler() const { return _userInputHandler; }
56 inline CtcpHandler *ctcpHandler() const { return _ctcpHandler; }
58 //! Decode a string using the server (network) decoding.
59 inline QString serverDecode(const QByteArray &string) const { return decodeServerString(string); }
61 //! Decode a string using a channel-specific encoding if one is set (and use the standard encoding else).
62 QString channelDecode(const QString &channelName, const QByteArray &string) const;
64 //! Decode a string using an IrcUser-specific encoding, if one exists (using the standaed encoding else).
65 QString userDecode(const QString &userNick, const QByteArray &string) const;
67 //! Encode a string using the server (network) encoding.
68 inline QByteArray serverEncode(const QString &string) const { return encodeServerString(string); }
70 //! Encode a string using the channel-specific encoding, if set, and use the standard encoding else.
71 QByteArray channelEncode(const QString &channelName, const QString &string) const;
73 //! Encode a string using the user-specific encoding, if set, and use the standard encoding else.
74 QByteArray userEncode(const QString &userNick, const QString &string) const;
76 inline QString channelKey(const QString &channel) const { return _channelKeys.value(channel.toLower(), QString()); }
78 inline bool isAutoWhoInProgress(const QString &channel) const { return _autoWhoPending.value(channel.toLower(), 0); }
80 inline UserId userId() const { return _coreSession->user(); }
83 virtual void setMyNick(const QString &mynick);
85 virtual void requestConnect() const;
86 virtual void requestDisconnect() const;
87 virtual void requestSetNetworkInfo(const NetworkInfo &info);
89 virtual void setUseAutoReconnect(bool);
90 virtual void setAutoReconnectInterval(quint32);
91 virtual void setAutoReconnectRetries(quint16);
93 void connectToIrc(bool reconnecting = false);
94 void disconnectFromIrc(bool requested = true, const QString &reason = QString(), bool withReconnect = false);
96 void userInput(BufferInfo bufferInfo, QString msg);
97 void putRawLine(QByteArray input);
98 void putCmd(const QString &cmd, const QList<QByteArray> ¶ms, const QByteArray &prefix = QByteArray());
100 void setChannelJoined(const QString &channel);
101 void setChannelParted(const QString &channel);
102 void addChannelKey(const QString &channel, const QString &key);
103 void removeChannelKey(const QString &channel);
105 bool setAutoWhoDone(const QString &channel);
107 Server usedServer() const;
109 inline void resetPingTimeout() { _lastPingTime = 0; }
112 void recvRawServerMsg(QString);
113 void displayStatusMsg(QString);
114 void displayMsg(Message::Type, BufferInfo::Type, QString target, QString text, QString sender = "", Message::Flags flags = Message::None);
115 void disconnected(NetworkId networkId);
116 void connectionError(const QString &errorMsg);
118 void quitRequested(NetworkId networkId);
119 void sslErrors(const QVariant &errorData);
122 inline virtual IrcChannel *ircChannelFactory(const QString &channelname) { return new CoreIrcChannel(channelname, this); }
125 void socketHasData();
126 void socketError(QAbstractSocket::SocketError);
127 void socketInitialized();
128 inline void socketCloseTimeout() { socket.disconnectFromHost(); }
129 void socketDisconnected();
130 void socketStateChanged(QAbstractSocket::SocketState);
131 void networkInitialized();
134 void restoreUserModes();
135 void doAutoReconnect();
137 void enablePingTimeout();
138 void disablePingTimeout();
140 void startAutoWhoCycle();
143 void sslErrors(const QList<QSslError> &errors);
146 void fillBucketAndProcessQueue();
148 void writeToSocket(const QByteArray &data);
151 CoreSession *_coreSession;
159 IrcServerHandler *_ircServerHandler;
160 UserInputHandler *_userInputHandler;
161 CtcpHandler *_ctcpHandler;
163 QHash<QString, QString> _channelKeys; // stores persistent channels and their passwords, if any
165 QTimer _autoReconnectTimer;
166 int _autoReconnectCount;
168 QTimer _socketCloseTimer;
170 /* this flag triggers quitRequested() once the socket is closed
171 * it is needed to determine whether or not the connection needs to be
172 * in the automatic session restore. */
176 bool _previousConnectionAttemptFailed;
177 int _lastUsedServerIndex;
182 bool _autoWhoEnabled;
183 QStringList _autoWhoQueue;
184 QHash<QString, int> _autoWhoPending;
185 int _autoWhoInterval;
186 int _autoWhoNickLimit;
188 QTimer _autoWhoTimer, _autoWhoCycleTimer;
190 QTimer _tokenBucketTimer;
191 int _messagesPerSecond; // token refill speed
192 int _burstSize; // size of the token bucket
193 int _tokenBucket; // the virtual bucket that holds the tokens
194 QList<QByteArray> _msgQueue;
197 #endif //CORENETWORK_H