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