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