Fixes #746 - Quassel unsets default modes set by server upon first connect
[quassel.git] / src / core / corenetwork.h
1 /***************************************************************************
2  *   Copyright (C) 2005-09 by the Quassel Project                          *
3  *   devel@quassel-irc.org                                                 *
4  *                                                                         *
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.                                           *
9  *                                                                         *
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.                          *
14  *                                                                         *
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  ***************************************************************************/
20
21 #ifndef CORENETWORK_H
22 #define CORENETWORK_H
23
24 #include "network.h"
25 #include "coreircchannel.h"
26
27 #include <QTimer>
28
29 #ifdef HAVE_SSL
30 # include <QSslSocket>
31 # include <QSslError>
32 #else
33 # include <QTcpSocket>
34 #endif
35
36 #include "coresession.h"
37
38 class CoreIdentity;
39 class IrcServerHandler;
40 class CoreUserInputHandler;
41 class CtcpHandler;
42 class CoreIgnoreListManager;
43
44 class CoreNetwork : public Network {
45   SYNCABLE_OBJECT
46   Q_OBJECT
47
48 public:
49   CoreNetwork(const NetworkId &networkid, CoreSession *session);
50   ~CoreNetwork();
51   inline virtual const QMetaObject *syncMetaObject() const { return &Network::staticMetaObject; }
52
53   inline CoreIdentity *identityPtr() const { return coreSession()->identity(identity()); }
54   inline CoreSession *coreSession() const { return _coreSession; }
55   inline CoreNetworkConfig *networkConfig() const { return coreSession()->networkConfig(); }
56
57   inline IrcServerHandler *ircServerHandler() const { return _ircServerHandler; }
58   inline CoreUserInputHandler *userInputHandler() const { return _userInputHandler; }
59   inline CtcpHandler *ctcpHandler() const { return _ctcpHandler; }
60   inline CoreIgnoreListManager *ignoreListManager() { return coreSession()->ignoreListManager(); }
61
62   //! Decode a string using the server (network) decoding.
63   inline QString serverDecode(const QByteArray &string) const { return decodeServerString(string); }
64
65   //! Decode a string using a channel-specific encoding if one is set (and use the standard encoding else).
66   QString channelDecode(const QString &channelName, const QByteArray &string) const;
67
68   //! Decode a string using an IrcUser-specific encoding, if one exists (using the standaed encoding else).
69   QString userDecode(const QString &userNick, const QByteArray &string) const;
70
71   //! Encode a string using the server (network) encoding.
72   inline QByteArray serverEncode(const QString &string) const { return encodeServerString(string); }
73
74   //! Encode a string using the channel-specific encoding, if set, and use the standard encoding else.
75   QByteArray channelEncode(const QString &channelName, const QString &string) const;
76
77   //! Encode a string using the user-specific encoding, if set, and use the standard encoding else.
78   QByteArray userEncode(const QString &userNick, const QString &string) const;
79
80   inline QString channelKey(const QString &channel) const { return _channelKeys.value(channel.toLower(), QString()); }
81
82   inline bool isAutoWhoInProgress(const QString &channel) const { return _autoWhoPending.value(channel.toLower(), 0); }
83
84   inline UserId userId() const { return _coreSession->user(); }
85
86 public slots:
87   virtual void setMyNick(const QString &mynick);
88
89   virtual void requestConnect() const;
90   virtual void requestDisconnect() const;
91   virtual void requestSetNetworkInfo(const NetworkInfo &info);
92
93   virtual void setUseAutoReconnect(bool);
94   virtual void setAutoReconnectInterval(quint32);
95   virtual void setAutoReconnectRetries(quint16);
96
97   void setPingInterval(int interval);
98
99   void connectToIrc(bool reconnecting = false);
100   void disconnectFromIrc(bool requested = true, const QString &reason = QString(), bool withReconnect = false);
101
102   void userInput(BufferInfo bufferInfo, QString msg);
103   void putRawLine(QByteArray input);
104   void putCmd(const QString &cmd, const QList<QByteArray> &params, const QByteArray &prefix = QByteArray());
105
106   void setChannelJoined(const QString &channel);
107   void setChannelParted(const QString &channel);
108   void addChannelKey(const QString &channel, const QString &key);
109   void removeChannelKey(const QString &channel);
110
111   void setAutoWhoEnabled(bool enabled);
112   void setAutoWhoInterval(int interval);
113   void setAutoWhoDelay(int delay);
114
115   bool setAutoWhoDone(const QString &channel);
116
117   void updateIssuedModes(const QString &requestedModes);
118   void updatePersistentModes(QString addModes, QString removeModes);
119   void resetPersistentModes();
120
121   Server usedServer() const;
122
123   inline void resetPingTimeout() { _pingCount = 0; }
124
125   inline void displayMsg(Message::Type msgType, BufferInfo::Type bufferType, const QString &target, const QString &text, const QString &sender = "", Message::Flags flags = Message::None) {
126     emit displayMsg(networkId(), msgType, bufferType, target, text, sender, flags);
127   }
128
129 signals:
130   void recvRawServerMsg(QString);
131   void displayStatusMsg(QString);
132   void displayMsg(NetworkId, Message::Type, BufferInfo::Type, const QString &target, const QString &text, const QString &sender = "", Message::Flags flags = Message::None);
133   void disconnected(NetworkId networkId);
134   void connectionError(const QString &errorMsg);
135
136   void quitRequested(NetworkId networkId);
137   void sslErrors(const QVariant &errorData);
138
139 protected:
140   inline virtual IrcChannel *ircChannelFactory(const QString &channelname) { return new CoreIrcChannel(channelname, this); }
141
142 private slots:
143   void socketHasData();
144   void socketError(QAbstractSocket::SocketError);
145   void socketInitialized();
146   inline void socketCloseTimeout() { socket.disconnectFromHost(); }
147   void socketDisconnected();
148   void socketStateChanged(QAbstractSocket::SocketState);
149   void networkInitialized();
150
151   void sendPerform();
152   void restoreUserModes();
153   void doAutoReconnect();
154   void sendPing();
155   void enablePingTimeout(bool enable = true);
156   void disablePingTimeout();
157   void sendAutoWho();
158   void startAutoWhoCycle();
159
160 #ifdef HAVE_SSL
161   void sslErrors(const QList<QSslError> &errors);
162 #endif
163
164   void fillBucketAndProcessQueue();
165
166   void writeToSocket(const QByteArray &data);
167
168 private:
169   CoreSession *_coreSession;
170
171 #ifdef HAVE_SSL
172   QSslSocket socket;
173 #else
174   QTcpSocket socket;
175 #endif
176
177   IrcServerHandler *_ircServerHandler;
178   CoreUserInputHandler *_userInputHandler;
179   CtcpHandler *_ctcpHandler;
180
181   QHash<QString, QString> _channelKeys;  // stores persistent channels and their passwords, if any
182
183   QTimer _autoReconnectTimer;
184   int _autoReconnectCount;
185
186   QTimer _socketCloseTimer;
187
188   /* this flag triggers quitRequested() once the socket is closed
189    * it is needed to determine whether or not the connection needs to be
190    * in the automatic session restore. */
191   bool _quitRequested;
192   QString _quitReason;
193
194   bool _previousConnectionAttemptFailed;
195   int _lastUsedServerIndex;
196
197   QTimer _pingTimer;
198   uint _lastPingTime;
199   uint _pingCount;
200
201   QStringList _autoWhoQueue;
202   QHash<QString, int> _autoWhoPending;
203   QTimer _autoWhoTimer, _autoWhoCycleTimer;
204
205   QTimer _tokenBucketTimer;
206   int _messageDelay;        // token refill speed in ms
207   int _burstSize;           // size of the token bucket
208   int _tokenBucket;         // the virtual bucket that holds the tokens
209   QList<QByteArray> _msgQueue;
210
211   QString _requestedUserModes; // 2 strings separated by a '-' character. first part are requested modes to add, the second to remove
212 };
213
214 #endif //CORENETWORK_H