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