99732d91a26b8f447cf9e46735c51183a1b46302
[quassel.git] / src / common / network.h
1 /***************************************************************************
2  *   Copyright (C) 2005-2016 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  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, 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 #include <QByteArray>
33
34 #include "types.h"
35 #include "util.h"
36 #include "syncableobject.h"
37
38 #include "signalproxy.h"
39 #include "ircuser.h"
40 #include "ircchannel.h"
41
42 // defined below!
43 struct NetworkInfo;
44
45 // TODO: ConnectionInfo to propagate and sync the current state of NetworkConnection, encodings etcpp
46
47 class Network : public SyncableObject
48 {
49     SYNCABLE_OBJECT
50     Q_OBJECT
51     Q_ENUMS(ConnectionState)
52
53     Q_PROPERTY(QString networkName READ networkName WRITE setNetworkName)
54     Q_PROPERTY(QString currentServer READ currentServer WRITE setCurrentServer)
55     Q_PROPERTY(QString myNick READ myNick WRITE setMyNick)
56     Q_PROPERTY(int latency READ latency WRITE setLatency)
57     Q_PROPERTY(QByteArray codecForServer READ codecForServer WRITE setCodecForServer)
58     Q_PROPERTY(QByteArray codecForEncoding READ codecForEncoding WRITE setCodecForEncoding)
59     Q_PROPERTY(QByteArray codecForDecoding READ codecForDecoding WRITE setCodecForDecoding)
60     Q_PROPERTY(IdentityId identityId READ identity WRITE setIdentity)
61     Q_PROPERTY(bool isConnected READ isConnected WRITE setConnected)
62     //Q_PROPERTY(Network::ConnectionState connectionState READ connectionState WRITE setConnectionState)
63     Q_PROPERTY(int connectionState READ connectionState WRITE setConnectionState)
64     Q_PROPERTY(bool useRandomServer READ useRandomServer WRITE setUseRandomServer)
65     Q_PROPERTY(QStringList perform READ perform WRITE setPerform)
66     Q_PROPERTY(bool useAutoIdentify READ useAutoIdentify WRITE setUseAutoIdentify)
67     Q_PROPERTY(QString autoIdentifyService READ autoIdentifyService WRITE setAutoIdentifyService)
68     Q_PROPERTY(QString autoIdentifyPassword READ autoIdentifyPassword WRITE setAutoIdentifyPassword)
69     Q_PROPERTY(bool useSasl READ useSasl WRITE setUseSasl)
70     Q_PROPERTY(QString saslAccount READ saslAccount WRITE setSaslAccount)
71     Q_PROPERTY(QString saslPassword READ saslPassword WRITE setSaslPassword)
72     Q_PROPERTY(bool useAutoReconnect READ useAutoReconnect WRITE setUseAutoReconnect)
73     Q_PROPERTY(quint32 autoReconnectInterval READ autoReconnectInterval WRITE setAutoReconnectInterval)
74     Q_PROPERTY(quint16 autoReconnectRetries READ autoReconnectRetries WRITE setAutoReconnectRetries)
75     Q_PROPERTY(bool unlimitedReconnectRetries READ unlimitedReconnectRetries WRITE setUnlimitedReconnectRetries)
76     Q_PROPERTY(bool rejoinChannels READ rejoinChannels WRITE setRejoinChannels)
77
78 public :
79         enum ConnectionState {
80         Disconnected,
81         Connecting,
82         Initializing,
83         Initialized,
84         Reconnecting,
85         Disconnecting
86     };
87
88     // see:
89     //  http://www.irc.org/tech_docs/005.html
90     //  http://www.irc.org/tech_docs/draft-brocklesby-irc-isupport-03.txt
91     enum ChannelModeType {
92         NOT_A_CHANMODE = 0x00,
93         A_CHANMODE = 0x01,
94         B_CHANMODE = 0x02,
95         C_CHANMODE = 0x04,
96         D_CHANMODE = 0x08
97     };
98
99     struct Server {
100         QString host;
101         uint port;
102         QString password;
103         bool useSsl;
104         bool sslVerify;     /// If true, validate SSL certificates
105         int sslVersion;
106
107         bool useProxy;
108         int proxyType;
109         QString proxyHost;
110         uint proxyPort;
111         QString proxyUser;
112         QString proxyPass;
113
114         // sslVerify only applies when useSsl is true.  sslVerify should be enabled by default,
115         // so enabling useSsl offers a more secure default.
116         Server() : port(6667), useSsl(false), sslVerify(true), sslVersion(0), useProxy(false),
117             proxyType(QNetworkProxy::Socks5Proxy), proxyHost("localhost"), proxyPort(8080) {}
118
119         Server(const QString &host, uint port, const QString &password, bool useSsl,
120                bool sslVerify)
121             : host(host), port(port), password(password), useSsl(useSsl), sslVerify(sslVerify),
122               sslVersion(0), useProxy(false), proxyType(QNetworkProxy::Socks5Proxy),
123               proxyHost("localhost"), proxyPort(8080) {}
124
125         bool operator==(const Server &other) const;
126         bool operator!=(const Server &other) const;
127     };
128     typedef QList<Server> ServerList;
129
130     Network(const NetworkId &networkid, QObject *parent = 0);
131     ~Network();
132
133     inline NetworkId networkId() const { return _networkId; }
134
135     inline SignalProxy *proxy() const { return _proxy; }
136     inline void setProxy(SignalProxy *proxy) { _proxy = proxy; }
137
138     inline bool isMyNick(const QString &nick) const { return (myNick().toLower() == nick.toLower()); }
139     inline bool isMe(IrcUser *ircuser) const { return (ircuser->nick().toLower() == myNick().toLower()); }
140
141     bool isChannelName(const QString &channelname) const;
142
143     /**
144      * Checks if the target counts as a STATUSMSG
145      *
146      * Status messages are prefixed with one or more characters from the server-provided STATUSMSG
147      * if available, otherwise "@" and "+" are assumed.  Generally, status messages sent to a
148      * channel are only visible to those with the same or higher permissions, e.g. voiced.
149      *
150      * @param[in] target Name of destination, e.g. a channel or query
151      * @returns True if a STATUSMSG, otherwise false
152      */
153     bool isStatusMsg(const QString &target) const;
154
155     inline bool isConnected() const { return _connected; }
156     //Network::ConnectionState connectionState() const;
157     inline int connectionState() const { return _connectionState; }
158
159     QString prefixToMode(const QString &prefix) const;
160     inline QString prefixToMode(const QCharRef &prefix) const { return prefixToMode(QString(prefix)); }
161     QString modeToPrefix(const QString &mode) const;
162     inline QString modeToPrefix(const QCharRef &mode) const { return modeToPrefix(QString(mode)); }
163
164     ChannelModeType channelModeType(const QString &mode);
165     inline ChannelModeType channelModeType(const QCharRef &mode) { return channelModeType(QString(mode)); }
166
167     inline const QString &networkName() const { return _networkName; }
168     inline const QString &currentServer() const { return _currentServer; }
169     inline const QString &myNick() const { return _myNick; }
170     inline int latency() const { return _latency; }
171     inline IrcUser *me() const { return ircUser(myNick()); }
172     inline IdentityId identity() const { return _identity; }
173     QStringList nicks() const;
174     inline QStringList channels() const { return _ircChannels.keys(); }
175     /**
176      * Gets the list of available capabilities.
177      *
178      * @returns QStringList of available capabilities
179      */
180     inline const QStringList caps() const { return QStringList(_caps.keys()); }
181     /**
182      * Gets the list of enabled (acknowledged) capabilities.
183      *
184      * @returns QStringList of enabled (acknowledged) capabilities
185      */
186     inline const QStringList capsEnabled() const { return _capsEnabled; }
187     inline const ServerList &serverList() const { return _serverList; }
188     inline bool useRandomServer() const { return _useRandomServer; }
189     inline const QStringList &perform() const { return _perform; }
190     inline bool useAutoIdentify() const { return _useAutoIdentify; }
191     inline const QString &autoIdentifyService() const { return _autoIdentifyService; }
192     inline const QString &autoIdentifyPassword() const { return _autoIdentifyPassword; }
193     inline bool useSasl() const { return _useSasl; }
194     inline const QString &saslAccount() const { return _saslAccount; }
195     inline const QString &saslPassword() const { return _saslPassword; }
196     inline bool useAutoReconnect() const { return _useAutoReconnect; }
197     inline quint32 autoReconnectInterval() const { return _autoReconnectInterval; }
198     inline quint16 autoReconnectRetries() const { return _autoReconnectRetries; }
199     inline bool unlimitedReconnectRetries() const { return _unlimitedReconnectRetries; }
200     inline bool rejoinChannels() const { return _rejoinChannels; }
201
202     NetworkInfo networkInfo() const;
203     void setNetworkInfo(const NetworkInfo &);
204
205     QString prefixes() const;
206     QString prefixModes() const;
207     void determinePrefixes() const;
208
209     bool supports(const QString &param) const { return _supports.contains(param); }
210     QString support(const QString &param) const;
211
212     /**
213      * Checks if a given capability is acknowledged and active.
214      *
215      * @param[in] capability Name of capability
216      * @returns True if acknowledged (active), otherwise false
217      */
218     inline bool capEnabled(const QString &capability) const { return _capsEnabled.contains(capability.toLower()); }
219     // IRCv3 specs all use lowercase capability names
220
221     /**
222      * Gets the value of an available capability, e.g. for SASL, "EXTERNAL,PLAIN".
223      *
224      * @param[in] capability Name of capability
225      * @returns Value of capability if one was specified, otherwise empty string
226      */
227     QString capValue(const QString &capability) const { return _caps.value(capability.toLower()); }
228     // IRCv3 specs all use lowercase capability names
229     // QHash returns the default constructed value if not found, in this case, empty string
230     // See:  https://doc.qt.io/qt-4.8/qhash.html#value
231
232     IrcUser *newIrcUser(const QString &hostmask, const QVariantMap &initData = QVariantMap());
233     inline IrcUser *newIrcUser(const QByteArray &hostmask) { return newIrcUser(decodeServerString(hostmask)); }
234     IrcUser *ircUser(QString nickname) const;
235     inline IrcUser *ircUser(const QByteArray &nickname) const { return ircUser(decodeServerString(nickname)); }
236     inline QList<IrcUser *> ircUsers() const { return _ircUsers.values(); }
237     inline quint32 ircUserCount() const { return _ircUsers.count(); }
238
239     IrcChannel *newIrcChannel(const QString &channelname, const QVariantMap &initData = QVariantMap());
240     inline IrcChannel *newIrcChannel(const QByteArray &channelname) { return newIrcChannel(decodeServerString(channelname)); }
241     IrcChannel *ircChannel(QString channelname) const;
242     inline IrcChannel *ircChannel(const QByteArray &channelname) const { return ircChannel(decodeServerString(channelname)); }
243     inline QList<IrcChannel *> ircChannels() const { return _ircChannels.values(); }
244     inline quint32 ircChannelCount() const { return _ircChannels.count(); }
245
246     QByteArray codecForServer() const;
247     QByteArray codecForEncoding() const;
248     QByteArray codecForDecoding() const;
249     void setCodecForServer(QTextCodec *codec);
250     void setCodecForEncoding(QTextCodec *codec);
251     void setCodecForDecoding(QTextCodec *codec);
252
253     QString decodeString(const QByteArray &text) const;
254     QByteArray encodeString(const QString &string) const;
255     QString decodeServerString(const QByteArray &text) const;
256     QByteArray encodeServerString(const QString &string) const;
257
258     static QByteArray defaultCodecForServer();
259     static QByteArray defaultCodecForEncoding();
260     static QByteArray defaultCodecForDecoding();
261     static void setDefaultCodecForServer(const QByteArray &name);
262     static void setDefaultCodecForEncoding(const QByteArray &name);
263     static void setDefaultCodecForDecoding(const QByteArray &name);
264
265     inline bool autoAwayActive() const { return _autoAwayActive; }
266     inline void setAutoAwayActive(bool active) { _autoAwayActive = active; }
267
268 public slots:
269     void setNetworkName(const QString &networkName);
270     void setCurrentServer(const QString &currentServer);
271     void setConnected(bool isConnected);
272     void setConnectionState(int state);
273     virtual void setMyNick(const QString &mynick);
274     void setLatency(int latency);
275     void setIdentity(IdentityId);
276
277     void setServerList(const QVariantList &serverList);
278     void setUseRandomServer(bool);
279     void setPerform(const QStringList &);
280     void setUseAutoIdentify(bool);
281     void setAutoIdentifyService(const QString &);
282     void setAutoIdentifyPassword(const QString &);
283     void setUseSasl(bool);
284     void setSaslAccount(const QString &);
285     void setSaslPassword(const QString &);
286     virtual void setUseAutoReconnect(bool);
287     virtual void setAutoReconnectInterval(quint32);
288     virtual void setAutoReconnectRetries(quint16);
289     void setUnlimitedReconnectRetries(bool);
290     void setRejoinChannels(bool);
291
292     void setCodecForServer(const QByteArray &codecName);
293     void setCodecForEncoding(const QByteArray &codecName);
294     void setCodecForDecoding(const QByteArray &codecName);
295
296     void addSupport(const QString &param, const QString &value = QString());
297     void removeSupport(const QString &param);
298
299     // IRCv3 capability negotiation (can be connected to signals)
300
301     /**
302      * Add an available capability, optionally providing a value.
303      *
304      * This may happen during first connect, or at any time later if a new capability becomes
305      * available (e.g. SASL service starting).
306      *
307      * @param[in] capability Name of the capability
308      * @param[in] value
309      * @parblock
310      * Optional value of the capability, e.g. sasl=plain.
311      * @endparblock
312      */
313     void addCap(const QString &capability, const QString &value = QString());
314
315     /**
316      * Marks a capability as acknowledged (enabled by the IRC server).
317      *
318      * @param[in] capability Name of the capability
319      */
320     void acknowledgeCap(const QString &capability);
321
322     /**
323      * Removes a capability from the list of available capabilities.
324      *
325      * This may happen during first connect, or at any time later if an existing capability becomes
326      * unavailable (e.g. SASL service stopping).  This also removes the capability from the list
327      * of acknowledged capabilities.
328      *
329      * @param[in] capability Name of the capability
330      */
331     void removeCap(const QString &capability);
332
333     /**
334      * Clears all capabilities from the list of available capabilities.
335      *
336      * This also removes the capability from the list of acknowledged capabilities.
337      */
338     void clearCaps();
339
340     inline void addIrcUser(const QString &hostmask) { newIrcUser(hostmask); }
341     inline void addIrcChannel(const QString &channel) { newIrcChannel(channel); }
342
343     //init geters
344     QVariantMap initSupports() const;
345     /**
346      * Get the initial list of available capabilities.
347      *
348      * @return QVariantMap of <QString, QString> indicating available capabilities and values
349      */
350     QVariantMap initCaps() const;
351     /**
352      * Get the initial list of enabled (acknowledged) capabilities.
353      *
354      * @return QVariantList of QString indicating enabled (acknowledged) capabilities and values
355      */
356     QVariantList initCapsEnabled() const { return toVariantList(capsEnabled()); }
357     inline QVariantList initServerList() const { return toVariantList(serverList()); }
358     virtual QVariantMap initIrcUsersAndChannels() const;
359
360     //init seters
361     void initSetSupports(const QVariantMap &supports);
362     /**
363      * Initialize the list of available capabilities.
364      *
365      * @param[in] caps QVariantMap of <QString, QString> indicating available capabilities and values
366      */
367     void initSetCaps(const QVariantMap &caps);
368     /**
369      * Initialize the list of enabled (acknowledged) capabilities.
370      *
371      * @param[in] caps QVariantList of QString indicating enabled (acknowledged) capabilities and values
372      */
373     inline void initSetCapsEnabled(const QVariantList &capsEnabled) { _capsEnabled = fromVariantList<QString>(capsEnabled); }
374     inline void initSetServerList(const QVariantList &serverList) { _serverList = fromVariantList<Server>(serverList); }
375     virtual void initSetIrcUsersAndChannels(const QVariantMap &usersAndChannels);
376
377     /**
378      * Update IrcUser hostmask and username from mask, creating an IrcUser if one does not exist.
379      *
380      * @param[in] mask   Full nick!user@hostmask string
381      * @return IrcUser of the matching nick if exists, otherwise a new IrcUser
382      */
383     IrcUser *updateNickFromMask(const QString &mask);
384
385     // these slots are to keep the hashlists of all users and the
386     // channel lists up to date
387     void ircUserNickChanged(QString newnick);
388
389     virtual inline void requestConnect() const { REQUEST(NO_ARG) }
390     virtual inline void requestDisconnect() const { REQUEST(NO_ARG) }
391     virtual inline void requestSetNetworkInfo(const NetworkInfo &info) { REQUEST(ARG(info)) }
392
393     void emitConnectionError(const QString &);
394
395 protected slots:
396     virtual void removeIrcUser(IrcUser *ircuser);
397     virtual void removeIrcChannel(IrcChannel *ircChannel);
398     virtual void removeChansAndUsers();
399
400 signals:
401     void aboutToBeDestroyed();
402     void networkNameSet(const QString &networkName);
403     void currentServerSet(const QString &currentServer);
404     void connectedSet(bool isConnected);
405     void connectionStateSet(Network::ConnectionState);
406 //   void connectionStateSet(int);
407     void connectionError(const QString &errorMsg);
408     void myNickSet(const QString &mynick);
409 //   void latencySet(int latency);
410     void identitySet(IdentityId);
411
412     void configChanged();
413
414     //   void serverListSet(QVariantList serverList);
415 //   void useRandomServerSet(bool);
416 //   void performSet(const QStringList &);
417 //   void useAutoIdentifySet(bool);
418 //   void autoIdentifyServiceSet(const QString &);
419 //   void autoIdentifyPasswordSet(const QString &);
420 //   void useAutoReconnectSet(bool);
421 //   void autoReconnectIntervalSet(quint32);
422 //   void autoReconnectRetriesSet(quint16);
423 //   void unlimitedReconnectRetriesSet(bool);
424 //   void rejoinChannelsSet(bool);
425
426 //   void codecForServerSet(const QByteArray &codecName);
427 //   void codecForEncodingSet(const QByteArray &codecName);
428 //   void codecForDecodingSet(const QByteArray &codecName);
429
430 //   void supportAdded(const QString &param, const QString &value);
431 //   void supportRemoved(const QString &param);
432
433     // IRCv3 capability negotiation (can drive other slots)
434     /**
435      * Indicates a capability is now available, with optional value in Network::capValue().
436      *
437      * @see Network::addCap()
438      *
439      * @param[in] capability Name of the capability
440      */
441     void capAdded (const QString &capability);
442
443     /**
444      * Indicates a capability was acknowledged (enabled by the IRC server).
445      *
446      * @see Network::acknowledgeCap()
447      *
448      * @param[in] capability Name of the capability
449      */
450     void capAcknowledged(const QString &capability);
451
452     /**
453      * Indicates a capability was removed from the list of available capabilities.
454      *
455      * @see Network::removeCap()
456      *
457      * @param[in] capability Name of the capability
458      */
459     void capRemoved(const QString &capability);
460
461 //   void ircUserAdded(const QString &hostmask);
462     void ircUserAdded(IrcUser *);
463 //   void ircChannelAdded(const QString &channelname);
464     void ircChannelAdded(IrcChannel *);
465
466 //   void connectRequested() const;
467 //   void disconnectRequested() const;
468 //   void setNetworkInfoRequested(const NetworkInfo &) const;
469
470 protected:
471     inline virtual IrcChannel *ircChannelFactory(const QString &channelname) { return new IrcChannel(channelname, this); }
472     inline virtual IrcUser *ircUserFactory(const QString &hostmask) { return new IrcUser(hostmask, this); }
473
474 private:
475     QPointer<SignalProxy> _proxy;
476
477     NetworkId _networkId;
478     IdentityId _identity;
479
480     QString _myNick;
481     int _latency;
482     QString _networkName;
483     QString _currentServer;
484     bool _connected;
485     ConnectionState _connectionState;
486
487     mutable QString _prefixes;
488     mutable QString _prefixModes;
489
490     QHash<QString, IrcUser *> _ircUsers; // stores all known nicks for the server
491     QHash<QString, IrcChannel *> _ircChannels; // stores all known channels
492     QHash<QString, QString> _supports; // stores results from RPL_ISUPPORT
493
494     QHash<QString, QString> _caps;  /// Capabilities supported by the IRC server
495     // By synchronizing the supported capabilities, the client could suggest certain behaviors, e.g.
496     // in the Network settings dialog, recommending SASL instead of using NickServ, or warning if
497     // SASL EXTERNAL isn't available.
498     QStringList _capsEnabled;       /// Enabled capabilities that received 'CAP ACK'
499     // _capsEnabled uses the same values from the <name>=<value> pairs stored in _caps
500
501     ServerList _serverList;
502     bool _useRandomServer;
503     QStringList _perform;
504
505     bool _useAutoIdentify;
506     QString _autoIdentifyService;
507     QString _autoIdentifyPassword;
508
509     bool _useSasl;
510     QString _saslAccount;
511     QString _saslPassword;
512
513     bool _useAutoReconnect;
514     quint32 _autoReconnectInterval;
515     quint16 _autoReconnectRetries;
516     bool _unlimitedReconnectRetries;
517     bool _rejoinChannels;
518
519     QTextCodec *_codecForServer;
520     QTextCodec *_codecForEncoding;
521     QTextCodec *_codecForDecoding;
522
523     static QTextCodec *_defaultCodecForServer;
524     static QTextCodec *_defaultCodecForEncoding;
525     static QTextCodec *_defaultCodecForDecoding;
526
527     bool _autoAwayActive; // when this is active handle305 and handle306 don't trigger any output
528
529     friend class IrcUser;
530     friend class IrcChannel;
531 };
532
533
534 //! Stores all editable information about a network (as opposed to runtime state).
535 struct NetworkInfo {
536     // set some default values, note that this does not initialize e.g. name and id
537     NetworkInfo();
538
539     NetworkId networkId;
540     QString networkName;
541     IdentityId identity;
542
543     bool useCustomEncodings; // not used!
544     QByteArray codecForServer;
545     QByteArray codecForEncoding;
546     QByteArray codecForDecoding;
547
548     Network::ServerList serverList;
549     bool useRandomServer;
550
551     QStringList perform;
552
553     bool useAutoIdentify;
554     QString autoIdentifyService;
555     QString autoIdentifyPassword;
556
557     bool useSasl;
558     QString saslAccount;
559     QString saslPassword;
560
561     bool useAutoReconnect;
562     quint32 autoReconnectInterval;
563     quint16 autoReconnectRetries;
564     bool unlimitedReconnectRetries;
565     bool rejoinChannels;
566
567     bool operator==(const NetworkInfo &other) const;
568     bool operator!=(const NetworkInfo &other) const;
569 };
570
571 QDataStream &operator<<(QDataStream &out, const NetworkInfo &info);
572 QDataStream &operator>>(QDataStream &in, NetworkInfo &info);
573 QDebug operator<<(QDebug dbg, const NetworkInfo &i);
574 Q_DECLARE_METATYPE(NetworkInfo)
575
576 QDataStream &operator<<(QDataStream &out, const Network::Server &server);
577 QDataStream &operator>>(QDataStream &in, Network::Server &server);
578 QDebug operator<<(QDebug dbg, const Network::Server &server);
579 Q_DECLARE_METATYPE(Network::Server)
580
581 #endif