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;
48 class CoreNetwork : public Network {
53 CoreNetwork(const NetworkId &networkid, CoreSession *session);
55 inline virtual const QMetaObject *syncMetaObject() const { return &Network::staticMetaObject; }
57 inline CoreIdentity *identityPtr() const { return coreSession()->identity(identity()); }
58 inline CoreSession *coreSession() const { return _coreSession; }
59 inline CoreNetworkConfig *networkConfig() const { return coreSession()->networkConfig(); }
61 inline CoreUserInputHandler *userInputHandler() const { return _userInputHandler; }
62 inline CoreIgnoreListManager *ignoreListManager() { return coreSession()->ignoreListManager(); }
64 //! Decode a string using the server (network) decoding.
65 inline QString serverDecode(const QByteArray &string) const { return decodeServerString(string); }
67 //! Decode a string using a channel-specific encoding if one is set (and use the standard encoding else).
68 QString channelDecode(const QString &channelName, const QByteArray &string) const;
70 //! Decode a string using an IrcUser-specific encoding, if one exists (using the standaed encoding else).
71 QString userDecode(const QString &userNick, const QByteArray &string) const;
73 //! Encode a string using the server (network) encoding.
74 inline QByteArray serverEncode(const QString &string) const { return encodeServerString(string); }
76 //! Encode a string using the channel-specific encoding, if set, and use the standard encoding else.
77 QByteArray channelEncode(const QString &channelName, const QString &string) const;
79 //! Encode a string using the user-specific encoding, if set, and use the standard encoding else.
80 QByteArray userEncode(const QString &userNick, const QString &string) const;
82 inline QString channelKey(const QString &channel) const { return _channelKeys.value(channel.toLower(), QString()); }
84 inline bool isAutoWhoInProgress(const QString &channel) const { return _autoWhoPending.value(channel.toLower(), 0); }
86 inline UserId userId() const { return _coreSession->user(); }
89 virtual void setMyNick(const QString &mynick);
91 virtual void requestConnect() const;
92 virtual void requestDisconnect() const;
93 virtual void requestSetNetworkInfo(const NetworkInfo &info);
95 virtual void setUseAutoReconnect(bool);
96 virtual void setAutoReconnectInterval(quint32);
97 virtual void setAutoReconnectRetries(quint16);
99 void setPingInterval(int interval);
101 void connectToIrc(bool reconnecting = false);
102 void disconnectFromIrc(bool requested = true, const QString &reason = QString(), bool withReconnect = false);
104 void userInput(BufferInfo bufferInfo, QString msg);
105 void putRawLine(QByteArray input);
106 void putCmd(const QString &cmd, const QList<QByteArray> ¶ms, const QByteArray &prefix = QByteArray());
108 void setChannelJoined(const QString &channel);
109 void setChannelParted(const QString &channel);
110 void addChannelKey(const QString &channel, const QString &key);
111 void removeChannelKey(const QString &channel);
115 Cipher *cipher(const QString &recipient) const;
116 QByteArray cipherKey(const QString &recipient) const;
117 void setCipherKey(const QString &recipient, const QByteArray &key);
120 void setAutoWhoEnabled(bool enabled);
121 void setAutoWhoInterval(int interval);
122 void setAutoWhoDelay(int delay);
124 bool setAutoWhoDone(const QString &channel);
126 void updateIssuedModes(const QString &requestedModes);
127 void updatePersistentModes(QString addModes, QString removeModes);
128 void resetPersistentModes();
130 Server usedServer() const;
132 inline void resetPingTimeout() { _pingCount = 0; }
134 inline void displayMsg(Message::Type msgType, BufferInfo::Type bufferType, const QString &target, const QString &text, const QString &sender = "", Message::Flags flags = Message::None) {
135 emit displayMsg(networkId(), msgType, bufferType, target, text, sender, flags);
139 void recvRawServerMsg(QString);
140 void displayStatusMsg(QString);
141 void displayMsg(NetworkId, Message::Type, BufferInfo::Type, const QString &target, const QString &text, const QString &sender = "", Message::Flags flags = Message::None);
142 void disconnected(NetworkId networkId);
143 void connectionError(const QString &errorMsg);
145 void quitRequested(NetworkId networkId);
146 void sslErrors(const QVariant &errorData);
148 void newEvent(Event *event);
151 inline virtual IrcChannel *ircChannelFactory(const QString &channelname) { return new CoreIrcChannel(channelname, this); }
152 inline virtual IrcUser *ircUserFactory(const QString &hostmask) { return new CoreIrcUser(hostmask, this); }
155 // TODO: remove cached cipher keys, when appropriate
156 //virtual void removeIrcUser(IrcUser *ircuser);
157 //virtual void removeIrcChannel(IrcChannel *ircChannel);
158 //virtual void removeChansAndUsers();
161 void socketHasData();
162 void socketError(QAbstractSocket::SocketError);
163 void socketInitialized();
164 inline void socketCloseTimeout() { socket.disconnectFromHost(); }
165 void socketDisconnected();
166 void socketStateChanged(QAbstractSocket::SocketState);
167 void networkInitialized();
170 void restoreUserModes();
171 void doAutoReconnect();
173 void enablePingTimeout(bool enable = true);
174 void disablePingTimeout();
176 void startAutoWhoCycle();
179 void sslErrors(const QList<QSslError> &errors);
182 void fillBucketAndProcessQueue();
184 void writeToSocket(const QByteArray &data);
187 CoreSession *_coreSession;
195 CoreUserInputHandler *_userInputHandler;
197 QHash<QString, QString> _channelKeys; // stores persistent channels and their passwords, if any
199 QTimer _autoReconnectTimer;
200 int _autoReconnectCount;
202 QTimer _socketCloseTimer;
204 /* this flag triggers quitRequested() once the socket is closed
205 * it is needed to determine whether or not the connection needs to be
206 * in the automatic session restore. */
210 bool _previousConnectionAttemptFailed;
211 int _lastUsedServerIndex;
217 QStringList _autoWhoQueue;
218 QHash<QString, int> _autoWhoPending;
219 QTimer _autoWhoTimer, _autoWhoCycleTimer;
221 QTimer _tokenBucketTimer;
222 int _messageDelay; // token refill speed in ms
223 int _burstSize; // size of the token bucket
224 int _tokenBucket; // the virtual bucket that holds the tokens
225 QList<QByteArray> _msgQueue;
227 QString _requestedUserModes; // 2 strings separated by a '-' character. first part are requested modes to add, the second to remove
230 QHash<QString, QByteArray> _cipherKeys;
233 #endif //CORENETWORK_H