recreating some indexes that were lost in an upgrade
[quassel.git] / src / common / network.h
1 /***************************************************************************
2  *   Copyright (C) 2005-08 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 NETWORK_H
22 #define NETWORK_H
23
24 #include <QString>
25 #include <QStringList>
26 #include <QList>
27 #include <QNetworkProxy>
28 #include <QHash>
29 #include <QVariantMap>
30 #include <QPointer>
31 #include <QMutex>
32
33 #include "types.h"
34 #include "util.h"
35 #include "syncableobject.h"
36
37 #include "signalproxy.h"
38 #include "ircuser.h"
39 #include "ircchannel.h"
40
41 // defined below!
42 struct NetworkInfo;
43
44 // TODO: ConnectionInfo to propagate and sync the current state of NetworkConnection, encodings etcpp
45
46 class Network : public SyncableObject {
47   Q_OBJECT
48   Q_ENUMS(ConnectionState Network::ConnectionState)
49
50   Q_PROPERTY(QString networkName READ networkName WRITE setNetworkName STORED false)
51   Q_PROPERTY(QString currentServer READ currentServer WRITE setCurrentServer STORED false)
52   Q_PROPERTY(QString myNick READ myNick WRITE setMyNick STORED false)
53   Q_PROPERTY(int latency READ latency WRITE setLatency STORED false)
54   Q_PROPERTY(QByteArray codecForServer READ codecForServer WRITE setCodecForServer STORED false)
55   Q_PROPERTY(QByteArray codecForEncoding READ codecForEncoding WRITE setCodecForEncoding STORED false)
56   Q_PROPERTY(QByteArray codecForDecoding READ codecForDecoding WRITE setCodecForDecoding STORED false)
57   Q_PROPERTY(IdentityId identityId READ identity WRITE setIdentity STORED false)
58   Q_PROPERTY(bool isConnected READ isConnected WRITE setConnected STORED false)
59   //Q_PROPERTY(Network::ConnectionState connectionState READ connectionState WRITE setConnectionState STORED false)
60   Q_PROPERTY(int connectionState READ connectionState WRITE setConnectionState STORED false)
61   Q_PROPERTY(bool useRandomServer READ useRandomServer WRITE setUseRandomServer STORED false)
62   Q_PROPERTY(QStringList perform READ perform WRITE setPerform STORED false)
63   Q_PROPERTY(bool useAutoIdentify READ useAutoIdentify WRITE setUseAutoIdentify STORED false)
64   Q_PROPERTY(QString autoIdentifyService READ autoIdentifyService WRITE setAutoIdentifyService STORED false)
65   Q_PROPERTY(QString autoIdentifyPassword READ autoIdentifyPassword WRITE setAutoIdentifyPassword STORED false)
66   Q_PROPERTY(bool useAutoReconnect READ useAutoReconnect WRITE setUseAutoReconnect STORED false)
67   Q_PROPERTY(quint32 autoReconnectInterval READ autoReconnectInterval WRITE setAutoReconnectInterval STORED false)
68   Q_PROPERTY(quint16 autoReconnectRetries READ autoReconnectRetries WRITE setAutoReconnectRetries STORED false)
69   Q_PROPERTY(bool unlimitedReconnectRetries READ unlimitedReconnectRetries WRITE setUnlimitedReconnectRetries STORED false)
70   Q_PROPERTY(bool rejoinChannels READ rejoinChannels WRITE setRejoinChannels STORED false)
71
72 public:
73   enum ConnectionState {
74     Disconnected,
75     Connecting,
76     Initializing,
77     Initialized,
78     Reconnecting,
79     Disconnecting
80   };
81
82   // see:
83   //  http://www.irc.org/tech_docs/005.html
84   //  http://www.irc.org/tech_docs/draft-brocklesby-irc-isupport-03.txt
85   enum ChannelModeType {
86     NOT_A_CHANMODE = 0x00,
87     A_CHANMODE = 0x01,
88     B_CHANMODE = 0x02,
89     C_CHANMODE = 0x04,
90     D_CHANMODE = 0x08
91   };
92
93   struct Server {
94     QString host;
95     uint port;
96     QString password;
97     bool useSsl;
98     int sslVersion;
99
100     bool useProxy;
101     int proxyType;
102     QString proxyHost;
103     uint proxyPort;
104     QString proxyUser;
105     QString proxyPass;
106
107     Server() : port(6667), useSsl(false), sslVersion(0), useProxy(false), proxyType(QNetworkProxy::Socks5Proxy), proxyHost("localhost"), proxyPort(8080) {}
108     Server(const QString &host, uint port, const QString &password, bool useSsl)
109       : host(host), port(port), password(password), useSsl(useSsl), sslVersion(0),
110         useProxy(false), proxyType(QNetworkProxy::Socks5Proxy), proxyHost("localhost"), proxyPort(8080) {}
111     bool operator==(const Server &other) const;
112     bool operator!=(const Server &other) const;
113   };
114   typedef QList<Server> ServerList;
115
116   Network(const NetworkId &networkid, QObject *parent = 0);
117   ~Network();
118
119   inline NetworkId networkId() const { return _networkId; }
120
121   inline SignalProxy *proxy() const { return _proxy; }
122   inline void setProxy(SignalProxy *proxy) { _proxy = proxy; }
123
124   inline bool isMyNick(const QString &nick) const { return (myNick().toLower() == nick.toLower()); }
125   inline bool isMe(IrcUser *ircuser) const { return (ircuser->nick().toLower() == myNick().toLower()); }
126
127   bool isChannelName(const QString &channelname) const;
128
129   inline bool isConnected() const { return _connected; }
130   //Network::ConnectionState connectionState() const;
131   inline int connectionState() const { return _connectionState; }
132
133   QString prefixToMode(const QString &prefix);
134   inline QString prefixToMode(const QCharRef &prefix) { return prefixToMode(QString(prefix)); }
135   QString modeToPrefix(const QString &mode);
136   inline QString modeToPrefix(const QCharRef &mode) { return modeToPrefix(QString(mode)); }
137
138   ChannelModeType channelModeType(const QString &mode);
139   inline ChannelModeType channelModeType(const QCharRef &mode) { return channelModeType(QString(mode)); }
140
141   inline const QString &networkName() const { return _networkName; }
142   inline const QString &currentServer() const { return _currentServer; }
143   inline const QString &myNick() const { return _myNick; }
144   inline int latency() const { return _latency; }
145   inline IrcUser *me() const { return ircUser(myNick()); }
146   inline IdentityId identity() const { return _identity; }
147   QStringList nicks() const;
148   inline QStringList channels() const { return _ircChannels.keys(); }
149   inline const ServerList &serverList() const { return _serverList; }
150   inline bool useRandomServer() const { return _useRandomServer; }
151   inline const QStringList &perform() const { return _perform; }
152   inline bool useAutoIdentify() const { return _useAutoIdentify; }
153   inline const QString &autoIdentifyService() const { return _autoIdentifyService; }
154   inline const QString &autoIdentifyPassword() const { return _autoIdentifyPassword; }
155   inline bool useAutoReconnect() const { return _useAutoReconnect; }
156   inline quint32 autoReconnectInterval() const { return _autoReconnectInterval; }
157   inline quint16 autoReconnectRetries() const { return _autoReconnectRetries; }
158   inline bool unlimitedReconnectRetries() const { return _unlimitedReconnectRetries; }
159   inline bool rejoinChannels() const { return _rejoinChannels; }
160
161   NetworkInfo networkInfo() const;
162   void setNetworkInfo(const NetworkInfo &);
163
164   QString prefixes();
165   QString prefixModes();
166
167   bool supports(const QString &param) const { return _supports.contains(param); }
168   QString support(const QString &param) const;
169
170   IrcUser *newIrcUser(const QString &hostmask, const QVariantMap &initData = QVariantMap());
171   inline IrcUser *newIrcUser(const QByteArray &hostmask) { return newIrcUser(decodeServerString(hostmask)); }
172   IrcUser *ircUser(QString nickname) const;
173   inline IrcUser *ircUser(const QByteArray &nickname) const { return ircUser(decodeServerString(nickname)); }
174   inline QList<IrcUser *> ircUsers() const { return _ircUsers.values(); }
175   inline quint32 ircUserCount() const { return _ircUsers.count(); }
176
177   IrcChannel *newIrcChannel(const QString &channelname, const QVariantMap &initData = QVariantMap());
178   inline IrcChannel *newIrcChannel(const QByteArray &channelname) { return newIrcChannel(decodeServerString(channelname)); }
179   IrcChannel *ircChannel(QString channelname) const;
180   inline IrcChannel *ircChannel(const QByteArray &channelname) const { return ircChannel(decodeServerString(channelname)); }
181   inline QList<IrcChannel *> ircChannels() const { return _ircChannels.values(); }
182   inline quint32 ircChannelCount() const { return _ircChannels.count(); }
183
184   QByteArray codecForServer() const;
185   QByteArray codecForEncoding() const;
186   QByteArray codecForDecoding() const;
187   void setCodecForServer(QTextCodec *codec);
188   void setCodecForEncoding(QTextCodec *codec);
189   void setCodecForDecoding(QTextCodec *codec);
190
191   QString decodeString(const QByteArray &text) const;
192   QByteArray encodeString(const QString &string) const;
193   QString decodeServerString(const QByteArray &text) const;
194   QByteArray encodeServerString(const QString &string) const;
195
196   static QByteArray defaultCodecForServer();
197   static QByteArray defaultCodecForEncoding();
198   static QByteArray defaultCodecForDecoding();
199   static void setDefaultCodecForServer(const QByteArray &name);
200   static void setDefaultCodecForEncoding(const QByteArray &name);
201   static void setDefaultCodecForDecoding(const QByteArray &name);
202
203   inline bool autoAwayActive() const { return _autoAwayActive; }
204   inline void setAutoAwayActive(bool active) { _autoAwayActive = active; }
205
206 public slots:
207   void setNetworkName(const QString &networkName);
208   void setCurrentServer(const QString &currentServer);
209   void setConnected(bool isConnected);
210   void setConnectionState(int state);
211   virtual void setMyNick(const QString &mynick);
212   void setLatency(int latency);
213   void setIdentity(IdentityId);
214
215   void setServerList(const QVariantList &serverList);
216   void setUseRandomServer(bool);
217   void setPerform(const QStringList &);
218   void setUseAutoIdentify(bool);
219   void setAutoIdentifyService(const QString &);
220   void setAutoIdentifyPassword(const QString &);
221   virtual void setUseAutoReconnect(bool);
222   virtual void setAutoReconnectInterval(quint32);
223   virtual void setAutoReconnectRetries(quint16);
224   void setUnlimitedReconnectRetries(bool);
225   void setRejoinChannels(bool);
226
227   void setCodecForServer(const QByteArray &codecName);
228   void setCodecForEncoding(const QByteArray &codecName);
229   void setCodecForDecoding(const QByteArray &codecName);
230
231   void addSupport(const QString &param, const QString &value = QString());
232   void removeSupport(const QString &param);
233
234   inline void addIrcUser(const QString &hostmask) { newIrcUser(hostmask); }
235   inline void addIrcChannel(const QString &channel) { newIrcChannel(channel); }
236
237   //init geters
238   QVariantMap initSupports() const;
239   inline QVariantList initServerList() const { return toVariantList(serverList()); }
240   virtual QVariantMap initIrcUsersAndChannels() const;
241
242   //init seters
243   void initSetSupports(const QVariantMap &supports);
244   inline void initSetServerList(const QVariantList &serverList) { _serverList = fromVariantList<Server>(serverList); }
245   virtual void initSetIrcUsersAndChannels(const QVariantMap &usersAndChannels);
246
247   IrcUser *updateNickFromMask(const QString &mask);
248
249   // these slots are to keep the hashlists of all users and the
250   // channel lists up to date
251   void ircUserNickChanged(QString newnick);
252
253   virtual inline void requestConnect() const { emit connectRequested(); }
254   virtual inline void requestDisconnect() const { emit disconnectRequested(); }
255   virtual inline void requestSetNetworkInfo(const NetworkInfo &info) { emit setNetworkInfoRequested(info); }
256
257   void emitConnectionError(const QString &);
258
259 private slots:
260   void removeIrcUser(IrcUser *ircuser);
261   void removeIrcChannel(IrcChannel *ircChannel);
262   void removeChansAndUsers();
263
264 signals:
265   void aboutToBeDestroyed();
266   void networkNameSet(const QString &networkName);
267   void currentServerSet(const QString &currentServer);
268   void connectedSet(bool isConnected);
269   void connectionStateSet(Network::ConnectionState);
270   void connectionStateSet(int);
271   void connectionError(const QString &errorMsg);
272   void myNickSet(const QString &mynick);
273   void latencySet(int latency);
274   void identitySet(IdentityId);
275
276   void serverListSet(QVariantList serverList);
277   void useRandomServerSet(bool);
278   void performSet(const QStringList &);
279   void useAutoIdentifySet(bool);
280   void autoIdentifyServiceSet(const QString &);
281   void autoIdentifyPasswordSet(const QString &);
282   void useAutoReconnectSet(bool);
283   void autoReconnectIntervalSet(quint32);
284   void autoReconnectRetriesSet(quint16);
285   void unlimitedReconnectRetriesSet(bool);
286   void rejoinChannelsSet(bool);
287
288   void codecForServerSet(const QByteArray &codecName);
289   void codecForEncodingSet(const QByteArray &codecName);
290   void codecForDecodingSet(const QByteArray &codecName);
291
292   void supportAdded(const QString &param, const QString &value);
293   void supportRemoved(const QString &param);
294
295   void ircUserAdded(const QString &hostmask);
296   void ircUserAdded(IrcUser *);
297   void ircChannelAdded(const QString &channelname);
298   void ircChannelAdded(IrcChannel *);
299
300   void connectRequested() const;
301   void disconnectRequested() const;
302   void setNetworkInfoRequested(const NetworkInfo &) const;
303
304 protected:
305   inline virtual IrcChannel *ircChannelFactory(const QString &channelname) { return new IrcChannel(channelname, this); }
306
307 private:
308   QPointer<SignalProxy> _proxy;
309
310   NetworkId _networkId;
311   IdentityId _identity;
312
313   QString _myNick;
314   int _latency;
315   QString _networkName;
316   QString _currentServer;
317   bool _connected;
318   ConnectionState _connectionState;
319
320   QString _prefixes;
321   QString _prefixModes;
322
323   QHash<QString, IrcUser *> _ircUsers;  // stores all known nicks for the server
324   QHash<QString, IrcChannel *> _ircChannels; // stores all known channels
325   QHash<QString, QString> _supports;  // stores results from RPL_ISUPPORT
326
327   ServerList _serverList;
328   bool _useRandomServer;
329   QStringList _perform;
330
331   bool _useAutoIdentify;
332   QString _autoIdentifyService;
333   QString _autoIdentifyPassword;
334
335   bool _useAutoReconnect;
336   quint32 _autoReconnectInterval;
337   quint16 _autoReconnectRetries;
338   bool _unlimitedReconnectRetries;
339   bool _rejoinChannels;
340
341   void determinePrefixes();
342
343   QTextCodec *_codecForServer;
344   QTextCodec *_codecForEncoding;
345   QTextCodec *_codecForDecoding;
346
347   static QTextCodec *_defaultCodecForServer;
348   static QTextCodec *_defaultCodecForEncoding;
349   static QTextCodec *_defaultCodecForDecoding;
350
351   bool _autoAwayActive; // when this is active handle305 and handle306 don't trigger any output
352
353   friend class IrcUser;
354   friend class IrcChannel;
355 };
356
357 //! Stores all editable information about a network (as opposed to runtime state).
358 struct NetworkInfo {
359   NetworkId networkId;
360   QString networkName;
361   IdentityId identity;
362
363   bool useCustomEncodings; // not used!
364   QByteArray codecForServer;
365   QByteArray codecForEncoding;
366   QByteArray codecForDecoding;
367
368   // Server entry: QString "Host", uint "Port", QString "Password", bool "UseSSL"
369   Network::ServerList serverList;
370   bool useRandomServer;
371
372   QStringList perform;
373
374   bool useAutoIdentify;
375   QString autoIdentifyService;
376   QString autoIdentifyPassword;
377
378   bool useAutoReconnect;
379   quint32 autoReconnectInterval;
380   quint16 autoReconnectRetries;
381   bool unlimitedReconnectRetries;
382   bool rejoinChannels;
383
384   bool operator==(const NetworkInfo &other) const;
385   bool operator!=(const NetworkInfo &other) const;
386 };
387
388 QDataStream &operator<<(QDataStream &out, const NetworkInfo &info);
389 QDataStream &operator>>(QDataStream &in, NetworkInfo &info);
390 QDebug operator<<(QDebug dbg, const NetworkInfo &i);
391 Q_DECLARE_METATYPE(NetworkInfo)
392
393 QDataStream &operator<<(QDataStream &out, const Network::Server &server);
394 QDataStream &operator>>(QDataStream &in, Network::Server &server);
395 QDebug operator<<(QDebug dbg, const Network::Server &server);
396 Q_DECLARE_METATYPE(Network::Server)
397
398 #endif