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 CoreUserInputHandler;
45 class CoreIgnoreListManager;
47 class CoreNetwork : public Network {
52 CoreNetwork(const NetworkId &networkid, CoreSession *session);
54 inline virtual const QMetaObject *syncMetaObject() const { return &Network::staticMetaObject; }
56 inline CoreIdentity *identityPtr() const { return coreSession()->identity(identity()); }
57 inline CoreSession *coreSession() const { return _coreSession; }
58 inline CoreNetworkConfig *networkConfig() const { return coreSession()->networkConfig(); }
60 inline CoreUserInputHandler *userInputHandler() const { return _userInputHandler; }
61 inline CoreIgnoreListManager *ignoreListManager() { return coreSession()->ignoreListManager(); }
63 //! Decode a string using the server (network) decoding.
64 inline QString serverDecode(const QByteArray &string) const { return decodeServerString(string); }
66 //! Decode a string using a channel-specific encoding if one is set (and use the standard encoding else).
67 QString channelDecode(const QString &channelName, const QByteArray &string) const;
69 //! Decode a string using an IrcUser-specific encoding, if one exists (using the standaed encoding else).
70 QString userDecode(const QString &userNick, const QByteArray &string) const;
72 //! Encode a string using the server (network) encoding.
73 inline QByteArray serverEncode(const QString &string) const { return encodeServerString(string); }
75 //! Encode a string using the channel-specific encoding, if set, and use the standard encoding else.
76 QByteArray channelEncode(const QString &channelName, const QString &string) const;
78 //! Encode a string using the user-specific encoding, if set, and use the standard encoding else.
79 QByteArray userEncode(const QString &userNick, const QString &string) const;
81 inline QString channelKey(const QString &channel) const { return _channelKeys.value(channel.toLower(), QString()); }
83 inline bool isAutoWhoInProgress(const QString &channel) const { return _autoWhoPending.value(channel.toLower(), 0); }
85 inline UserId userId() const { return _coreSession->user(); }
88 virtual void setMyNick(const QString &mynick);
90 virtual void requestConnect() const;
91 virtual void requestDisconnect() const;
92 virtual void requestSetNetworkInfo(const NetworkInfo &info);
94 virtual void setUseAutoReconnect(bool);
95 virtual void setAutoReconnectInterval(quint32);
96 virtual void setAutoReconnectRetries(quint16);
98 void setPingInterval(int interval);
100 void connectToIrc(bool reconnecting = false);
101 void disconnectFromIrc(bool requested = true, const QString &reason = QString(), bool withReconnect = false);
103 void userInput(BufferInfo bufferInfo, QString msg);
104 void putRawLine(QByteArray input);
105 void putCmd(const QString &cmd, const QList<QByteArray> ¶ms, const QByteArray &prefix = QByteArray());
107 void setChannelJoined(const QString &channel);
108 void setChannelParted(const QString &channel);
109 void addChannelKey(const QString &channel, const QString &key);
110 void removeChannelKey(const QString &channel);
114 Cipher *cipher(const QString &recipient) const;
115 QByteArray cipherKey(const QString &recipient) const;
116 void setCipherKey(const QString &recipient, const QByteArray &key);
119 void setAutoWhoEnabled(bool enabled);
120 void setAutoWhoInterval(int interval);
121 void setAutoWhoDelay(int delay);
123 bool setAutoWhoDone(const QString &channel);
125 void updateIssuedModes(const QString &requestedModes);
126 void updatePersistentModes(QString addModes, QString removeModes);
127 void resetPersistentModes();
129 Server usedServer() const;
131 inline void resetPingTimeout() { _pingCount = 0; }
133 inline void displayMsg(Message::Type msgType, BufferInfo::Type bufferType, const QString &target, const QString &text, const QString &sender = "", Message::Flags flags = Message::None) {
134 emit displayMsg(networkId(), msgType, bufferType, target, text, sender, flags);
138 void recvRawServerMsg(QString);
139 void displayStatusMsg(QString);
140 void displayMsg(NetworkId, Message::Type, BufferInfo::Type, const QString &target, const QString &text, const QString &sender = "", Message::Flags flags = Message::None);
141 void disconnected(NetworkId networkId);
142 void connectionError(const QString &errorMsg);
144 void quitRequested(NetworkId networkId);
145 void sslErrors(const QVariant &errorData);
148 inline virtual IrcChannel *ircChannelFactory(const QString &channelname) { return new CoreIrcChannel(channelname, this); }
149 inline virtual IrcUser *ircUserFactory(const QString &hostmask) { return new CoreIrcUser(hostmask, this); }
152 // TODO: remove cached cipher keys, when appropriate
153 //virtual void removeIrcUser(IrcUser *ircuser);
154 //virtual void removeIrcChannel(IrcChannel *ircChannel);
155 //virtual void removeChansAndUsers();
158 void socketHasData();
159 void socketError(QAbstractSocket::SocketError);
160 void socketInitialized();
161 inline void socketCloseTimeout() { socket.disconnectFromHost(); }
162 void socketDisconnected();
163 void socketStateChanged(QAbstractSocket::SocketState);
164 void networkInitialized();
167 void restoreUserModes();
168 void doAutoReconnect();
170 void enablePingTimeout(bool enable = true);
171 void disablePingTimeout();
173 void startAutoWhoCycle();
176 void sslErrors(const QList<QSslError> &errors);
179 void fillBucketAndProcessQueue();
181 void writeToSocket(const QByteArray &data);
184 CoreSession *_coreSession;
192 CoreUserInputHandler *_userInputHandler;
194 QHash<QString, QString> _channelKeys; // stores persistent channels and their passwords, if any
196 QTimer _autoReconnectTimer;
197 int _autoReconnectCount;
199 QTimer _socketCloseTimer;
201 /* this flag triggers quitRequested() once the socket is closed
202 * it is needed to determine whether or not the connection needs to be
203 * in the automatic session restore. */
207 bool _previousConnectionAttemptFailed;
208 int _lastUsedServerIndex;
214 QStringList _autoWhoQueue;
215 QHash<QString, int> _autoWhoPending;
216 QTimer _autoWhoTimer, _autoWhoCycleTimer;
218 QTimer _tokenBucketTimer;
219 int _messageDelay; // token refill speed in ms
220 int _burstSize; // size of the token bucket
221 int _tokenBucket; // the virtual bucket that holds the tokens
222 QList<QByteArray> _msgQueue;
224 QString _requestedUserModes; // 2 strings separated by a '-' character. first part are requested modes to add, the second to remove
227 QHash<QString, QByteArray> _cipherKeys;
230 #endif //CORENETWORK_H