src: Yearly copyright bump
[quassel.git] / src / common / network.h
1 /***************************************************************************
2  *   Copyright (C) 2005-2020 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 #pragma once
22
23 #include "common-export.h"
24
25 #include <utility>
26
27 #include <QByteArray>
28 #include <QHash>
29 #include <QList>
30 #include <QMutex>
31 #include <QNetworkProxy>
32 #include <QPointer>
33 #include <QString>
34 #include <QStringList>
35 #include <QVariantMap>
36
37 #include "ircchannel.h"
38 #include "ircuser.h"
39 #include "signalproxy.h"
40 #include "syncableobject.h"
41 #include "types.h"
42 #include "util.h"
43
44 // IRCv3 capabilities
45 #include "irccap.h"
46
47 // defined below!
48 struct NetworkInfo;
49
50 // TODO: ConnectionInfo to propagate and sync the current state of NetworkConnection, encodings etcpp
51
52 class COMMON_EXPORT Network : public SyncableObject
53 {
54     Q_OBJECT
55     SYNCABLE_OBJECT
56
57     Q_ENUMS(ConnectionState)
58
59     Q_PROPERTY(QString networkName READ networkName WRITE setNetworkName)
60     Q_PROPERTY(QString currentServer READ currentServer WRITE setCurrentServer)
61     Q_PROPERTY(QString myNick READ myNick WRITE setMyNick)
62     Q_PROPERTY(int latency READ latency WRITE setLatency)
63     Q_PROPERTY(QByteArray codecForServer READ codecForServer WRITE setCodecForServer)
64     Q_PROPERTY(QByteArray codecForEncoding READ codecForEncoding WRITE setCodecForEncoding)
65     Q_PROPERTY(QByteArray codecForDecoding READ codecForDecoding WRITE setCodecForDecoding)
66     Q_PROPERTY(IdentityId identityId READ identity WRITE setIdentity)
67     Q_PROPERTY(bool isConnected READ isConnected WRITE setConnected)
68     // Q_PROPERTY(Network::ConnectionState connectionState READ connectionState WRITE setConnectionState)
69     Q_PROPERTY(int connectionState READ connectionState WRITE setConnectionState)
70     Q_PROPERTY(bool useRandomServer READ useRandomServer WRITE setUseRandomServer)
71     Q_PROPERTY(QStringList perform READ perform WRITE setPerform)
72     Q_PROPERTY(bool useAutoIdentify READ useAutoIdentify WRITE setUseAutoIdentify)
73     Q_PROPERTY(QString autoIdentifyService READ autoIdentifyService WRITE setAutoIdentifyService)
74     Q_PROPERTY(QString autoIdentifyPassword READ autoIdentifyPassword WRITE setAutoIdentifyPassword)
75     Q_PROPERTY(bool useSasl READ useSasl WRITE setUseSasl)
76     Q_PROPERTY(QString saslAccount READ saslAccount WRITE setSaslAccount)
77     Q_PROPERTY(QString saslPassword READ saslPassword WRITE setSaslPassword)
78     Q_PROPERTY(bool useAutoReconnect READ useAutoReconnect WRITE setUseAutoReconnect)
79     Q_PROPERTY(quint32 autoReconnectInterval READ autoReconnectInterval WRITE setAutoReconnectInterval)
80     Q_PROPERTY(quint16 autoReconnectRetries READ autoReconnectRetries WRITE setAutoReconnectRetries)
81     Q_PROPERTY(bool unlimitedReconnectRetries READ unlimitedReconnectRetries WRITE setUnlimitedReconnectRetries)
82     Q_PROPERTY(bool rejoinChannels READ rejoinChannels WRITE setRejoinChannels)
83     // Custom rate limiting
84     Q_PROPERTY(bool useCustomMessageRate READ useCustomMessageRate WRITE setUseCustomMessageRate)
85     Q_PROPERTY(quint32 msgRateBurstSize READ messageRateBurstSize WRITE setMessageRateBurstSize)
86     Q_PROPERTY(quint32 msgRateMessageDelay READ messageRateDelay WRITE setMessageRateDelay)
87     Q_PROPERTY(bool unlimitedMessageRate READ unlimitedMessageRate WRITE setUnlimitedMessageRate)
88
89 public:
90     enum ConnectionState
91     {
92         Disconnected,
93         Connecting,
94         Initializing,
95         Initialized,
96         Reconnecting,
97         Disconnecting
98     };
99
100     // see:
101     //  http://www.irc.org/tech_docs/005.html
102     //  http://www.irc.org/tech_docs/draft-brocklesby-irc-isupport-03.txt
103     enum ChannelModeType
104     {
105         NOT_A_CHANMODE = 0x00,
106         A_CHANMODE = 0x01,
107         B_CHANMODE = 0x02,
108         C_CHANMODE = 0x04,
109         D_CHANMODE = 0x08
110     };
111
112     // Default port assignments according to what many IRC networks have settled on.
113     // Technically not a standard, but it's fairly widespread.
114     // See https://freenode.net/news/port-6697-irc-via-tlsssl
115     enum PortDefaults
116     {
117         PORT_PLAINTEXT = 6667,  /// Default port for unencrypted connections
118         PORT_SSL = 6697         /// Default port for encrypted connections
119     };
120
121     struct Server
122     {
123         QString host;
124         uint port{6667};
125         QString password;
126         bool useSsl{false};
127         bool sslVerify{true};  /// If true, validate SSL certificates
128         int sslVersion{0};
129
130         bool useProxy{false};
131         int proxyType{QNetworkProxy::Socks5Proxy};
132         QString proxyHost;
133         uint proxyPort{8080};
134         QString proxyUser;
135         QString proxyPass;
136
137         // sslVerify only applies when useSsl is true.  sslVerify should be enabled by default,
138         // so enabling useSsl offers a more secure default.
139         Server()
140             : proxyHost("localhost")
141         {}
142
143         Server(QString host, uint port, QString password, bool useSsl, bool sslVerify)
144             : host(std::move(host))
145             , port(port)
146             , password(std::move(password))
147             , useSsl(useSsl)
148             , sslVerify(sslVerify)
149             , proxyType(QNetworkProxy::Socks5Proxy)
150             , proxyHost("localhost")
151             , proxyPort(8080)
152         {}
153
154         bool operator==(const Server& other) const;
155         bool operator!=(const Server& other) const;
156     };
157     using ServerList = QList<Server>;
158
159     Network(const NetworkId& networkid, QObject* parent = nullptr);
160     ~Network() override;
161
162     inline NetworkId networkId() const { return _networkId; }
163
164     inline SignalProxy* proxy() const { return _proxy; }
165     inline void setProxy(SignalProxy* proxy) { _proxy = proxy; }
166
167     inline bool isMyNick(const QString& nick) const { return (myNick().toLower() == nick.toLower()); }
168     inline bool isMe(IrcUser* ircuser) const { return (ircuser->nick().toLower() == myNick().toLower()); }
169
170     bool isChannelName(const QString& channelname) const;
171
172     /**
173      * Checks if the target counts as a STATUSMSG
174      *
175      * Status messages are prefixed with one or more characters from the server-provided STATUSMSG
176      * if available, otherwise "@" and "+" are assumed.  Generally, status messages sent to a
177      * channel are only visible to those with the same or higher permissions, e.g. voiced.
178      *
179      * @param[in] target Name of destination, e.g. a channel or query
180      * @returns True if a STATUSMSG, otherwise false
181      */
182     bool isStatusMsg(const QString& target) const;
183
184     inline bool isConnected() const { return _connected; }
185     // Network::ConnectionState connectionState() const;
186     inline int connectionState() const { return _connectionState; }
187
188     /**@{*/
189     /**
190      * Translates a user’s prefix to the channelmode associated with it.
191      * @param prefix Prefix to be translated.
192      */
193     QString prefixToMode(const QString& prefix) const;
194     inline QString prefixToMode(const QCharRef& prefix) const { return prefixToMode(QString(prefix)); }
195     inline QString prefixesToModes(const QString& prefix) const
196     {
197         QString modes;
198         for (QChar c : prefix) {
199             modes += prefixToMode(c);
200         }
201         return modes;
202     }
203     /**@}*/
204
205     /**@{*/
206     /**
207      * Translates a user’s prefix to the channelmode associated with it.
208      * @param prefix Prefix to be translated.
209      */
210     QString modeToPrefix(const QString& mode) const;
211     inline QString modeToPrefix(const QCharRef& mode) const { return modeToPrefix(QString(mode)); }
212     inline QString modesToPrefixes(const QString& mode) const
213     {
214         QString prefixes;
215         for (QChar c : mode) {
216             prefixes += modeToPrefix(c);
217         }
218         return prefixes;
219     }
220     /**@}*/
221
222     /**
223      * Sorts the user channelmodes according to priority set by PREFIX
224      *
225      * Given a list of channel modes, sorts according to the order of PREFIX, putting the highest
226      * modes first.  Any unknown modes are moved to the end in no given order.
227      *
228      * If prefix modes cannot be determined from the network, no changes will be made.
229      *
230      * @param modes User channelmodes
231      * @return Priority-sorted user channelmodes
232      */
233     QString sortPrefixModes(const QString& modes) const;
234
235     /**@{*/
236     /**
237      * Sorts the list of users' channelmodes according to priority set by PREFIX
238      *
239      * Maintains order of the modes list.
240      *
241      * @seealso Network::sortPrefixModes()
242      *
243      * @param modesList List of users' channel modes
244      * @return Priority-sorted list of users' channel modes
245      */
246     inline QStringList sortPrefixModes(const QStringList& modesList) const
247     {
248         QStringList sortedModesList;
249         // Sort each individual mode string, appending back
250         // Must maintain the order received!
251         for (QString modes : modesList) {
252             sortedModesList << sortPrefixModes(modes);
253         }
254         return sortedModesList;
255     }
256     /**@}*/
257
258     ChannelModeType channelModeType(const QString& mode);
259     inline ChannelModeType channelModeType(const QCharRef& mode) { return channelModeType(QString(mode)); }
260
261     inline const QString& networkName() const { return _networkName; }
262     inline const QString& currentServer() const { return _currentServer; }
263     inline const QString& myNick() const { return _myNick; }
264     inline int latency() const { return _latency; }
265     inline IrcUser* me() const { return ircUser(myNick()); }
266     inline IdentityId identity() const { return _identity; }
267     QStringList nicks() const;
268     inline QStringList channels() const { return _ircChannels.keys(); }
269     /**
270      * Gets the list of available capabilities.
271      *
272      * @returns QStringList of available capabilities
273      */
274     inline const QStringList caps() const { return QStringList(_caps.keys()); }
275     /**
276      * Gets the list of enabled (acknowledged) capabilities.
277      *
278      * @returns QStringList of enabled (acknowledged) capabilities
279      */
280     inline const QStringList capsEnabled() const { return _capsEnabled; }
281     inline const ServerList& serverList() const { return _serverList; }
282     inline bool useRandomServer() const { return _useRandomServer; }
283     inline const QStringList& perform() const { return _perform; }
284     inline bool useAutoIdentify() const { return _useAutoIdentify; }
285     inline const QString& autoIdentifyService() const { return _autoIdentifyService; }
286     inline const QString& autoIdentifyPassword() const { return _autoIdentifyPassword; }
287     inline bool useSasl() const { return _useSasl; }
288     inline const QString& saslAccount() const { return _saslAccount; }
289     inline const QString& saslPassword() const { return _saslPassword; }
290     inline bool useAutoReconnect() const { return _useAutoReconnect; }
291     inline quint32 autoReconnectInterval() const { return _autoReconnectInterval; }
292     inline quint16 autoReconnectRetries() const { return _autoReconnectRetries; }
293     inline bool unlimitedReconnectRetries() const { return _unlimitedReconnectRetries; }
294     inline bool rejoinChannels() const { return _rejoinChannels; }
295
296     // Custom rate limiting
297
298     /**
299      * Gets whether or not custom rate limiting is used
300      *
301      * @return True if custom rate limiting is enabled, otherwise false.
302      */
303     inline bool useCustomMessageRate() const { return _useCustomMessageRate; }
304
305     /**
306      * Gets maximum number of messages to send without any delays
307      *
308      * @return
309      * @parblock
310      * Maximum number of messages to send without any delays.  A value of 1 disables message
311      * bursting.
312      * @endparblock
313      */
314     inline quint32 messageRateBurstSize() const { return _messageRateBurstSize; }
315
316     /**
317      * Gets the delay between messages after the maximum number of undelayed messages have been sent
318      *
319      * @return
320      * @parblock
321      * Delay in milliseconds between messages after the maximum number of undelayed messages have
322      * been sent.
323      * @endparblock
324      */
325     inline quint32 messageRateDelay() const { return _messageRateDelay; }
326
327     /**
328      * Gets whether or not all rate limiting is disabled, e.g. for IRC bridges
329      *
330      * @return If true, disable rate limiting, otherwise apply configured limits.
331      */
332     inline bool unlimitedMessageRate() const { return _unlimitedMessageRate; }
333
334     NetworkInfo networkInfo() const;
335     void setNetworkInfo(const NetworkInfo&);
336
337     QString prefixes() const;
338     QString prefixModes() const;
339     void determinePrefixes() const;
340
341     bool supports(const QString& param) const { return _supports.contains(param); }
342     QString support(const QString& param) const;
343
344     /**
345      * Checks if a given capability is advertised by the server.
346      *
347      * These results aren't valid if the network is disconnected or capability negotiation hasn't
348      * happened, and some servers might not correctly advertise capabilities.  Don't treat this as
349      * a guarantee.
350      *
351      * @param[in] capability Name of capability
352      * @returns True if connected and advertised by the server, otherwise false
353      */
354     inline bool capAvailable(const QString& capability) const { return _caps.contains(capability.toLower()); }
355     // IRCv3 specs all use lowercase capability names
356
357     /**
358      * Checks if a given capability is acknowledged and active.
359      *
360      * @param[in] capability Name of capability
361      * @returns True if acknowledged (active), otherwise false
362      */
363     inline bool capEnabled(const QString& capability) const { return _capsEnabled.contains(capability.toLower()); }
364     // IRCv3 specs all use lowercase capability names
365
366     /**
367      * Gets the value of an available capability, e.g. for SASL, "EXTERNAL,PLAIN".
368      *
369      * @param[in] capability Name of capability
370      * @returns Value of capability if one was specified, otherwise empty string
371      */
372     QString capValue(const QString& capability) const { return _caps.value(capability.toLower()); }
373     // IRCv3 specs all use lowercase capability names
374     // QHash returns the default constructed value if not found, in this case, empty string
375     // See:  https://doc.qt.io/qt-4.8/qhash.html#value
376
377     /**
378      * Check if the given authentication mechanism is likely to be supported.
379      *
380      * This depends on the server advertising SASL support and either declaring available mechanisms
381      * (SASL 3.2), or just indicating something is supported (SASL 3.1).
382      *
383      * @param[in] saslMechanism  Desired SASL mechanism
384      * @return True if mechanism supported or unknown, otherwise false
385      */
386     bool saslMaybeSupports(const QString& saslMechanism) const;
387
388     IrcUser* newIrcUser(const QString& hostmask, const QVariantMap& initData = QVariantMap());
389     inline IrcUser* newIrcUser(const QByteArray& hostmask) { return newIrcUser(decodeServerString(hostmask)); }
390     IrcUser* ircUser(QString nickname) const;
391     inline IrcUser* ircUser(const QByteArray& nickname) const { return ircUser(decodeServerString(nickname)); }
392     inline QList<IrcUser*> ircUsers() const { return _ircUsers.values(); }
393     inline quint32 ircUserCount() const { return _ircUsers.count(); }
394
395     IrcChannel* newIrcChannel(const QString& channelname, const QVariantMap& initData = QVariantMap());
396     inline IrcChannel* newIrcChannel(const QByteArray& channelname) { return newIrcChannel(decodeServerString(channelname)); }
397     IrcChannel* ircChannel(QString channelname) const;
398     inline IrcChannel* ircChannel(const QByteArray& channelname) const { return ircChannel(decodeServerString(channelname)); }
399     inline QList<IrcChannel*> ircChannels() const { return _ircChannels.values(); }
400     inline quint32 ircChannelCount() const { return _ircChannels.count(); }
401
402     QByteArray codecForServer() const;
403     QByteArray codecForEncoding() const;
404     QByteArray codecForDecoding() const;
405     void setCodecForServer(QTextCodec* codec);
406     void setCodecForEncoding(QTextCodec* codec);
407     void setCodecForDecoding(QTextCodec* codec);
408
409     QString decodeString(const QByteArray& text) const;
410     QByteArray encodeString(const QString& string) const;
411     QString decodeServerString(const QByteArray& text) const;
412     QByteArray encodeServerString(const QString& string) const;
413
414     static QByteArray defaultCodecForServer();
415     static QByteArray defaultCodecForEncoding();
416     static QByteArray defaultCodecForDecoding();
417     static void setDefaultCodecForServer(const QByteArray& name);
418     static void setDefaultCodecForEncoding(const QByteArray& name);
419     static void setDefaultCodecForDecoding(const QByteArray& name);
420
421     inline bool autoAwayActive() const { return _autoAwayActive; }
422     inline void setAutoAwayActive(bool active) { _autoAwayActive = active; }
423
424 public slots:
425     void setNetworkName(const QString& networkName);
426     void setCurrentServer(const QString& currentServer);
427     void setConnected(bool isConnected);
428     void setConnectionState(int state);
429     virtual void setMyNick(const QString& mynick);
430     void setLatency(int latency);
431     void setIdentity(IdentityId);
432
433     void setServerList(const QVariantList& serverList);
434     void setUseRandomServer(bool);
435     void setPerform(const QStringList&);
436     void setUseAutoIdentify(bool);
437     void setAutoIdentifyService(const QString&);
438     void setAutoIdentifyPassword(const QString&);
439     void setUseSasl(bool);
440     void setSaslAccount(const QString&);
441     void setSaslPassword(const QString&);
442     virtual void setUseAutoReconnect(bool);
443     virtual void setAutoReconnectInterval(quint32);
444     virtual void setAutoReconnectRetries(quint16);
445     void setUnlimitedReconnectRetries(bool);
446     void setRejoinChannels(bool);
447
448     // Custom rate limiting
449
450     /**
451      * Sets whether or not custom rate limiting is used.
452      *
453      * Setting limits too low may get you disconnected from the server!
454      *
455      * @param[in] useCustomRate If true, use custom rate limits, otherwise use Quassel defaults.
456      */
457     void setUseCustomMessageRate(bool useCustomRate);
458
459     /**
460      * Sets maximum number of messages to send without any delays
461      *
462      * @param[in] burstSize
463      * @parblock
464      * Maximum number of messages to send without any delays.  A value of 1 disables message
465      * bursting.  Cannot be less than 1 as sending 0 messages at a time accomplishes nothing.
466      * @endparblock
467      */
468     void setMessageRateBurstSize(quint32 burstSize);
469
470     /**
471      * Sets the delay between messages after the maximum number of undelayed messages have been sent
472      *
473      * @param[in] messageDelay
474      * @parblock
475      * Delay in milliseconds between messages after the maximum number of undelayed messages have
476      * been sent.
477      * @endparblock
478      */
479     void setMessageRateDelay(quint32 messageDelay);
480
481     /**
482      * Sets whether or not all rate limiting is disabled, e.g. for IRC bridges
483      *
484      * Don't use with most normal networks.
485      *
486      * @param[in] unlimitedRate If true, disable rate limiting, otherwise apply configured limits.
487      */
488     void setUnlimitedMessageRate(bool unlimitedRate);
489
490     void setCodecForServer(const QByteArray& codecName);
491     void setCodecForEncoding(const QByteArray& codecName);
492     void setCodecForDecoding(const QByteArray& codecName);
493
494     void addSupport(const QString& param, const QString& value = QString());
495     void removeSupport(const QString& param);
496
497     // IRCv3 capability negotiation (can be connected to signals)
498
499     /**
500      * Add an available capability, optionally providing a value.
501      *
502      * This may happen during first connect, or at any time later if a new capability becomes
503      * available (e.g. SASL service starting).
504      *
505      * @param[in] capability Name of the capability
506      * @param[in] value
507      * @parblock
508      * Optional value of the capability, e.g. sasl=plain.
509      * @endparblock
510      */
511     void addCap(const QString& capability, const QString& value = QString());
512
513     /**
514      * Marks a capability as acknowledged (enabled by the IRC server).
515      *
516      * @param[in] capability Name of the capability
517      */
518     void acknowledgeCap(const QString& capability);
519
520     /**
521      * Removes a capability from the list of available capabilities.
522      *
523      * This may happen during first connect, or at any time later if an existing capability becomes
524      * unavailable (e.g. SASL service stopping).  This also removes the capability from the list
525      * of acknowledged capabilities.
526      *
527      * @param[in] capability Name of the capability
528      */
529     void removeCap(const QString& capability);
530
531     /**
532      * Clears all capabilities from the list of available capabilities.
533      *
534      * This also removes the capability from the list of acknowledged capabilities.
535      */
536     void clearCaps();
537
538     inline void addIrcUser(const QString& hostmask) { newIrcUser(hostmask); }
539     inline void addIrcChannel(const QString& channel) { newIrcChannel(channel); }
540
541     // init geters
542     QVariantMap initSupports() const;
543     /**
544      * Get the initial list of available capabilities.
545      *
546      * @return QVariantMap of <QString, QString> indicating available capabilities and values
547      */
548     QVariantMap initCaps() const;
549     /**
550      * Get the initial list of enabled (acknowledged) capabilities.
551      *
552      * @return QVariantList of QString indicating enabled (acknowledged) capabilities and values
553      */
554     QVariantList initCapsEnabled() const { return toVariantList(capsEnabled()); }
555     inline QVariantList initServerList() const { return toVariantList(serverList()); }
556     virtual QVariantMap initIrcUsersAndChannels() const;
557
558     // init seters
559     void initSetSupports(const QVariantMap& supports);
560     /**
561      * Initialize the list of available capabilities.
562      *
563      * @param[in] caps QVariantMap of <QString, QString> indicating available capabilities and values
564      */
565     void initSetCaps(const QVariantMap& caps);
566     /**
567      * Initialize the list of enabled (acknowledged) capabilities.
568      *
569      * @param[in] caps QVariantList of QString indicating enabled (acknowledged) capabilities and values
570      */
571     inline void initSetCapsEnabled(const QVariantList& capsEnabled) { _capsEnabled = fromVariantList<QString>(capsEnabled); }
572     inline void initSetServerList(const QVariantList& serverList) { _serverList = fromVariantList<Server>(serverList); }
573     virtual void initSetIrcUsersAndChannels(const QVariantMap& usersAndChannels);
574
575     /**
576      * Update IrcUser hostmask and username from mask, creating an IrcUser if one does not exist.
577      *
578      * @param[in] mask   Full nick!user@hostmask string
579      * @return IrcUser of the matching nick if exists, otherwise a new IrcUser
580      */
581     IrcUser* updateNickFromMask(const QString& mask);
582
583     // these slots are to keep the hashlists of all users and the
584     // channel lists up to date
585     void ircUserNickChanged(QString newnick);
586
587     virtual inline void requestConnect() const { REQUEST(NO_ARG) }
588     virtual inline void requestDisconnect() const { REQUEST(NO_ARG) }
589     virtual inline void requestSetNetworkInfo(const NetworkInfo& info) { REQUEST(ARG(info)) }
590
591     void emitConnectionError(const QString&);
592
593 protected slots:
594     virtual void removeIrcUser(IrcUser* ircuser);
595     virtual void removeIrcChannel(IrcChannel* ircChannel);
596     virtual void removeChansAndUsers();
597
598 signals:
599     void aboutToBeDestroyed();
600     void networkNameSet(const QString& networkName);
601     void currentServerSet(const QString& currentServer);
602     void connectedSet(bool isConnected);
603     void connectionStateSet(Network::ConnectionState);
604     //   void connectionStateSet(int);
605     void connectionError(const QString& errorMsg);
606     void myNickSet(const QString& mynick);
607     //   void latencySet(int latency);
608     void identitySet(IdentityId);
609
610     void configChanged();
611
612     //   void serverListSet(QVariantList serverList);
613     //   void useRandomServerSet(bool);
614     //   void performSet(const QStringList &);
615     //   void useAutoIdentifySet(bool);
616     //   void autoIdentifyServiceSet(const QString &);
617     //   void autoIdentifyPasswordSet(const QString &);
618     //   void useAutoReconnectSet(bool);
619     //   void autoReconnectIntervalSet(quint32);
620     //   void autoReconnectRetriesSet(quint16);
621     //   void unlimitedReconnectRetriesSet(bool);
622     //   void rejoinChannelsSet(bool);
623
624     // Custom rate limiting (can drive other slots)
625
626     /**
627      * Signals enabling or disabling custom rate limiting
628      *
629      * @see Network::useCustomMessageRate()
630      *
631      * @param[out] useCustomRate
632      */
633     void useCustomMessageRateSet(const bool useCustomRate);
634
635     /**
636      * Signals a change in maximum number of messages to send without any delays
637      *
638      * @see Network::messageRateBurstSize()
639      *
640      * @param[out] burstSize
641      */
642     void messageRateBurstSizeSet(const quint32 burstSize);
643
644     /**
645      * Signals a change in delay between messages after the max. undelayed messages have been sent
646      *
647      * @see Network::messageRateDelay()
648      *
649      * @param[out] messageDelay
650      */
651     void messageRateDelaySet(const quint32 messageDelay);
652
653     /**
654      * Signals enabling or disabling all rate limiting
655      *
656      * @see Network::unlimitedMessageRate()
657      *
658      * @param[out] unlimitedRate
659      */
660     void unlimitedMessageRateSet(const bool unlimitedRate);
661
662     //   void codecForServerSet(const QByteArray &codecName);
663     //   void codecForEncodingSet(const QByteArray &codecName);
664     //   void codecForDecodingSet(const QByteArray &codecName);
665
666     //   void supportAdded(const QString &param, const QString &value);
667     //   void supportRemoved(const QString &param);
668
669     // IRCv3 capability negotiation (can drive other slots)
670     /**
671      * Indicates a capability is now available, with optional value in Network::capValue().
672      *
673      * @see Network::addCap()
674      *
675      * @param[in] capability Name of the capability
676      */
677     void capAdded(const QString& capability);
678
679     /**
680      * Indicates a capability was acknowledged (enabled by the IRC server).
681      *
682      * @see Network::acknowledgeCap()
683      *
684      * @param[in] capability Name of the capability
685      */
686     void capAcknowledged(const QString& capability);
687
688     /**
689      * Indicates a capability was removed from the list of available capabilities.
690      *
691      * @see Network::removeCap()
692      *
693      * @param[in] capability Name of the capability
694      */
695     void capRemoved(const QString& capability);
696
697     //   void ircUserAdded(const QString &hostmask);
698     void ircUserAdded(IrcUser*);
699     //   void ircChannelAdded(const QString &channelname);
700     void ircChannelAdded(IrcChannel*);
701
702     //   void connectRequested() const;
703     //   void disconnectRequested() const;
704     //   void setNetworkInfoRequested(const NetworkInfo &) const;
705
706 protected:
707     inline virtual IrcChannel* ircChannelFactory(const QString& channelname) { return new IrcChannel(channelname, this); }
708     inline virtual IrcUser* ircUserFactory(const QString& hostmask) { return new IrcUser(hostmask, this); }
709
710 private:
711     QPointer<SignalProxy> _proxy;
712
713     NetworkId _networkId;
714     IdentityId _identity;
715
716     QString _myNick;
717     int _latency;
718     QString _networkName;
719     QString _currentServer;
720     bool _connected;
721     ConnectionState _connectionState;
722
723     mutable QString _prefixes;
724     mutable QString _prefixModes;
725
726     QHash<QString, IrcUser*> _ircUsers;        // stores all known nicks for the server
727     QHash<QString, IrcChannel*> _ircChannels;  // stores all known channels
728     QHash<QString, QString> _supports;         // stores results from RPL_ISUPPORT
729
730     QHash<QString, QString> _caps;  /// Capabilities supported by the IRC server
731     // By synchronizing the supported capabilities, the client could suggest certain behaviors, e.g.
732     // in the Network settings dialog, recommending SASL instead of using NickServ, or warning if
733     // SASL EXTERNAL isn't available.
734     QStringList _capsEnabled;  /// Enabled capabilities that received 'CAP ACK'
735     // _capsEnabled uses the same values from the <name>=<value> pairs stored in _caps
736
737     ServerList _serverList;
738     bool _useRandomServer;
739     QStringList _perform;
740
741     bool _useAutoIdentify;
742     QString _autoIdentifyService;
743     QString _autoIdentifyPassword;
744
745     bool _useSasl;
746     QString _saslAccount;
747     QString _saslPassword;
748
749     bool _useAutoReconnect;
750     quint32 _autoReconnectInterval;
751     quint16 _autoReconnectRetries;
752     bool _unlimitedReconnectRetries;
753     bool _rejoinChannels;
754
755     // Custom rate limiting
756     bool _useCustomMessageRate;     /// If true, use custom rate limits, otherwise use defaults
757     quint32 _messageRateBurstSize;  /// Maximum number of messages to send without any delays
758     quint32 _messageRateDelay;      /// Delay in ms. for messages when max. burst messages sent
759     bool _unlimitedMessageRate;     /// If true, disable rate limiting, otherwise apply limits
760
761     QTextCodec* _codecForServer;
762     QTextCodec* _codecForEncoding;
763     QTextCodec* _codecForDecoding;
764
765     static QTextCodec* _defaultCodecForServer;
766     static QTextCodec* _defaultCodecForEncoding;
767     static QTextCodec* _defaultCodecForDecoding;
768
769     bool _autoAwayActive;  // when this is active handle305 and handle306 don't trigger any output
770
771     friend class IrcUser;
772     friend class IrcChannel;
773 };
774
775 //! Stores all editable information about a network (as opposed to runtime state).
776 struct COMMON_EXPORT NetworkInfo
777 {
778     QString networkName;
779
780     Network::ServerList serverList;
781     QStringList perform;
782
783     QString autoIdentifyService{"NickServ"};
784     QString autoIdentifyPassword;
785
786     QString saslAccount;
787     QString saslPassword;
788
789     QByteArray codecForServer;
790     QByteArray codecForEncoding;
791     QByteArray codecForDecoding;
792
793     NetworkId networkId{0};
794     IdentityId identity{1};
795
796     quint32 messageRateBurstSize{5};  ///< Maximum number of messages to send without any delays
797     quint32 messageRateDelay{2200};   ///< Delay in ms. for messages when max. burst messages sent
798
799     quint32 autoReconnectInterval{60};
800     quint16 autoReconnectRetries{20};
801
802     bool rejoinChannels{true};
803     bool useRandomServer{false};
804     bool useAutoIdentify{false};
805     bool useSasl{false};
806     bool useAutoReconnect{true};
807     bool unlimitedReconnectRetries{false};
808     bool useCustomMessageRate{false};  ///< If true, use custom rate limits, otherwise use defaults
809     bool unlimitedMessageRate{false};  ///< If true, disable rate limiting, otherwise apply limits
810
811 public:
812     bool operator==(const NetworkInfo& other) const;
813     bool operator!=(const NetworkInfo& other) const;
814 };
815
816 COMMON_EXPORT QDataStream& operator<<(QDataStream& out, const NetworkInfo& info);
817 COMMON_EXPORT QDataStream& operator>>(QDataStream& in, NetworkInfo& info);
818 COMMON_EXPORT QDebug operator<<(QDebug dbg, const NetworkInfo& i);
819 Q_DECLARE_METATYPE(NetworkInfo)
820
821 COMMON_EXPORT QDataStream& operator<<(QDataStream& out, const Network::Server& server);
822 COMMON_EXPORT QDataStream& operator>>(QDataStream& in, Network::Server& server);
823 COMMON_EXPORT QDebug operator<<(QDebug dbg, const Network::Server& server);
824 Q_DECLARE_METATYPE(Network::Server)