common: Make frequently called util methods more efficient
[quassel.git] / src / common / network.h
1 /***************************************************************************
2  *   Copyright (C) 2005-2016 by the Quassel Project                        *
3  *   devel@quassel-irc.org                                                 *
4  *                                                                         *
5  *   This program is free software; you can redistribute it and/or modify  *
6  *   it under the terms of the GNU General Public License as published by  *
7  *   the Free Software Foundation; either version 2 of the License, or     *
8  *   (at your option) version 3.                                           *
9  *                                                                         *
10  *   This program is distributed in the hope that it will be useful,       *
11  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
12  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
13  *   GNU General Public License for more details.                          *
14  *                                                                         *
15  *   You should have received a copy of the GNU General Public License     *
16  *   along with this program; if not, write to the                         *
17  *   Free Software Foundation, Inc.,                                       *
18  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.         *
19  ***************************************************************************/
20
21 #ifndef NETWORK_H
22 #define NETWORK_H
23
24 #include <QString>
25 #include <QStringList>
26 #include <QList>
27 #include <QNetworkProxy>
28 #include <QHash>
29 #include <QVariantMap>
30 #include <QPointer>
31 #include <QMutex>
32 #include <QByteArray>
33
34 #include "types.h"
35 #include "util.h"
36 #include "syncableobject.h"
37
38 #include "signalproxy.h"
39 #include "ircuser.h"
40 #include "ircchannel.h"
41
42 // 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     QString prefixToMode(const QString &prefix) const;
176     inline QString prefixToMode(const QCharRef &prefix) const { return prefixToMode(QString(prefix)); }
177     QString modeToPrefix(const QString &mode) const;
178     inline QString modeToPrefix(const QCharRef &mode) const { return modeToPrefix(QString(mode)); }
179
180     ChannelModeType channelModeType(const QString &mode);
181     inline ChannelModeType channelModeType(const QCharRef &mode) { return channelModeType(QString(mode)); }
182
183     inline const QString &networkName() const { return _networkName; }
184     inline const QString &currentServer() const { return _currentServer; }
185     inline const QString &myNick() const { return _myNick; }
186     inline int latency() const { return _latency; }
187     inline IrcUser *me() const { return ircUser(myNick()); }
188     inline IdentityId identity() const { return _identity; }
189     QStringList nicks() const;
190     inline QStringList channels() const { return _ircChannels.keys(); }
191     /**
192      * Gets the list of available capabilities.
193      *
194      * @returns QStringList of available capabilities
195      */
196     inline const QStringList caps() const { return QStringList(_caps.keys()); }
197     /**
198      * Gets the list of enabled (acknowledged) capabilities.
199      *
200      * @returns QStringList of enabled (acknowledged) capabilities
201      */
202     inline const QStringList capsEnabled() const { return _capsEnabled; }
203     inline const ServerList &serverList() const { return _serverList; }
204     inline bool useRandomServer() const { return _useRandomServer; }
205     inline const QStringList &perform() const { return _perform; }
206     inline bool useAutoIdentify() const { return _useAutoIdentify; }
207     inline const QString &autoIdentifyService() const { return _autoIdentifyService; }
208     inline const QString &autoIdentifyPassword() const { return _autoIdentifyPassword; }
209     inline bool useSasl() const { return _useSasl; }
210     inline const QString &saslAccount() const { return _saslAccount; }
211     inline const QString &saslPassword() const { return _saslPassword; }
212     inline bool useAutoReconnect() const { return _useAutoReconnect; }
213     inline quint32 autoReconnectInterval() const { return _autoReconnectInterval; }
214     inline quint16 autoReconnectRetries() const { return _autoReconnectRetries; }
215     inline bool unlimitedReconnectRetries() const { return _unlimitedReconnectRetries; }
216     inline bool rejoinChannels() const { return _rejoinChannels; }
217
218     // Custom rate limiting
219
220     /**
221      * Gets whether or not custom rate limiting is used
222      *
223      * @return True if custom rate limiting is enabled, otherwise false.
224      */
225     inline bool useCustomMessageRate() const { return _useCustomMessageRate; }
226
227     /**
228      * Gets maximum number of messages to send without any delays
229      *
230      * @return
231      * @parblock
232      * Maximum number of messages to send without any delays.  A value of 1 disables message
233      * bursting.
234      * @endparblock
235      */
236     inline quint32 messageRateBurstSize() const { return _messageRateBurstSize; }
237
238     /**
239      * Gets the delay between messages after the maximum number of undelayed messages have been sent
240      *
241      * @return
242      * @parblock
243      * Delay in milliseconds between messages after the maximum number of undelayed messages have
244      * been sent.
245      * @endparblock
246      */
247     inline quint32 messageRateDelay() const { return _messageRateDelay; }
248
249     /**
250      * Gets whether or not all rate limiting is disabled, e.g. for IRC bridges
251      *
252      * @return If true, disable rate limiting, otherwise apply configured limits.
253      */
254     inline bool unlimitedMessageRate() const { return _unlimitedMessageRate; }
255
256     NetworkInfo networkInfo() const;
257     void setNetworkInfo(const NetworkInfo &);
258
259     QString prefixes() const;
260     QString prefixModes() const;
261     void determinePrefixes() const;
262
263     bool supports(const QString &param) const { return _supports.contains(param); }
264     QString support(const QString &param) const;
265
266     /**
267      * Checks if a given capability is advertised by the server.
268      *
269      * These results aren't valid if the network is disconnected or capability negotiation hasn't
270      * happened, and some servers might not correctly advertise capabilities.  Don't treat this as
271      * a guarentee.
272      *
273      * @param[in] capability Name of capability
274      * @returns True if connected and advertised by the server, otherwise false
275      */
276     inline bool capAvailable(const QString &capability) const { return _caps.contains(capability.toLower()); }
277     // IRCv3 specs all use lowercase capability names
278
279     /**
280      * Checks if a given capability is acknowledged and active.
281      *
282      * @param[in] capability Name of capability
283      * @returns True if acknowledged (active), otherwise false
284      */
285     inline bool capEnabled(const QString &capability) const { return _capsEnabled.contains(capability.toLower()); }
286     // IRCv3 specs all use lowercase capability names
287
288     /**
289      * Gets the value of an available capability, e.g. for SASL, "EXTERNAL,PLAIN".
290      *
291      * @param[in] capability Name of capability
292      * @returns Value of capability if one was specified, otherwise empty string
293      */
294     QString capValue(const QString &capability) const { return _caps.value(capability.toLower()); }
295     // IRCv3 specs all use lowercase capability names
296     // QHash returns the default constructed value if not found, in this case, empty string
297     // See:  https://doc.qt.io/qt-4.8/qhash.html#value
298
299     /**
300      * Check if the given authentication mechanism is likely to be supported.
301      *
302      * This depends on the server advertising SASL support and either declaring available mechanisms
303      * (SASL 3.2), or just indicating something is supported (SASL 3.1).
304      *
305      * @param[in] saslMechanism  Desired SASL mechanism
306      * @return True if mechanism supported or unknown, otherwise false
307      */
308     bool saslMaybeSupports(const QString &saslMechanism) const;
309
310     IrcUser *newIrcUser(const QString &hostmask, const QVariantMap &initData = QVariantMap());
311     inline IrcUser *newIrcUser(const QByteArray &hostmask) { return newIrcUser(decodeServerString(hostmask)); }
312     IrcUser *ircUser(QString nickname) const;
313     inline IrcUser *ircUser(const QByteArray &nickname) const { return ircUser(decodeServerString(nickname)); }
314     inline QList<IrcUser *> ircUsers() const { return _ircUsers.values(); }
315     inline quint32 ircUserCount() const { return _ircUsers.count(); }
316
317     IrcChannel *newIrcChannel(const QString &channelname, const QVariantMap &initData = QVariantMap());
318     inline IrcChannel *newIrcChannel(const QByteArray &channelname) { return newIrcChannel(decodeServerString(channelname)); }
319     IrcChannel *ircChannel(QString channelname) const;
320     inline IrcChannel *ircChannel(const QByteArray &channelname) const { return ircChannel(decodeServerString(channelname)); }
321     inline QList<IrcChannel *> ircChannels() const { return _ircChannels.values(); }
322     inline quint32 ircChannelCount() const { return _ircChannels.count(); }
323
324     QByteArray codecForServer() const;
325     QByteArray codecForEncoding() const;
326     QByteArray codecForDecoding() const;
327     void setCodecForServer(QTextCodec *codec);
328     void setCodecForEncoding(QTextCodec *codec);
329     void setCodecForDecoding(QTextCodec *codec);
330
331     QString decodeString(const QByteArray &text) const;
332     QByteArray encodeString(const QString &string) const;
333     QString decodeServerString(const QByteArray &text) const;
334     QByteArray encodeServerString(const QString &string) const;
335
336     static QByteArray defaultCodecForServer();
337     static QByteArray defaultCodecForEncoding();
338     static QByteArray defaultCodecForDecoding();
339     static void setDefaultCodecForServer(const QByteArray &name);
340     static void setDefaultCodecForEncoding(const QByteArray &name);
341     static void setDefaultCodecForDecoding(const QByteArray &name);
342
343     inline bool autoAwayActive() const { return _autoAwayActive; }
344     inline void setAutoAwayActive(bool active) { _autoAwayActive = active; }
345
346 public slots:
347     void setNetworkName(const QString &networkName);
348     void setCurrentServer(const QString &currentServer);
349     void setConnected(bool isConnected);
350     void setConnectionState(int state);
351     virtual void setMyNick(const QString &mynick);
352     void setLatency(int latency);
353     void setIdentity(IdentityId);
354
355     void setServerList(const QVariantList &serverList);
356     void setUseRandomServer(bool);
357     void setPerform(const QStringList &);
358     void setUseAutoIdentify(bool);
359     void setAutoIdentifyService(const QString &);
360     void setAutoIdentifyPassword(const QString &);
361     void setUseSasl(bool);
362     void setSaslAccount(const QString &);
363     void setSaslPassword(const QString &);
364     virtual void setUseAutoReconnect(bool);
365     virtual void setAutoReconnectInterval(quint32);
366     virtual void setAutoReconnectRetries(quint16);
367     void setUnlimitedReconnectRetries(bool);
368     void setRejoinChannels(bool);
369
370     // Custom rate limiting
371
372     /**
373      * Sets whether or not custom rate limiting is used.
374      *
375      * Setting limits too low may get you disconnected from the server!
376      *
377      * @param[in] useCustomRate If true, use custom rate limits, otherwise use Quassel defaults.
378      */
379     void setUseCustomMessageRate(bool useCustomRate);
380
381     /**
382      * Sets maximum number of messages to send without any delays
383      *
384      * @param[in] burstSize
385      * @parblock
386      * Maximum number of messages to send without any delays.  A value of 1 disables message
387      * bursting.  Cannot be less than 1 as sending 0 messages at a time accomplishes nothing.
388      * @endparblock
389      */
390     void setMessageRateBurstSize(quint32 burstSize);
391
392     /**
393      * Sets the delay between messages after the maximum number of undelayed messages have been sent
394      *
395      * @param[in] messageDelay
396      * @parblock
397      * Delay in milliseconds between messages after the maximum number of undelayed messages have
398      * been sent.
399      * @endparblock
400      */
401     void setMessageRateDelay(quint32 messageDelay);
402
403     /**
404      * Sets whether or not all rate limiting is disabled, e.g. for IRC bridges
405      *
406      * Don't use with most normal networks.
407      *
408      * @param[in] unlimitedRate If true, disable rate limiting, otherwise apply configured limits.
409      */
410     void setUnlimitedMessageRate(bool unlimitedRate);
411
412     void setCodecForServer(const QByteArray &codecName);
413     void setCodecForEncoding(const QByteArray &codecName);
414     void setCodecForDecoding(const QByteArray &codecName);
415
416     void addSupport(const QString &param, const QString &value = QString());
417     void removeSupport(const QString &param);
418
419     // IRCv3 capability negotiation (can be connected to signals)
420
421     /**
422      * Add an available capability, optionally providing a value.
423      *
424      * This may happen during first connect, or at any time later if a new capability becomes
425      * available (e.g. SASL service starting).
426      *
427      * @param[in] capability Name of the capability
428      * @param[in] value
429      * @parblock
430      * Optional value of the capability, e.g. sasl=plain.
431      * @endparblock
432      */
433     void addCap(const QString &capability, const QString &value = QString());
434
435     /**
436      * Marks a capability as acknowledged (enabled by the IRC server).
437      *
438      * @param[in] capability Name of the capability
439      */
440     void acknowledgeCap(const QString &capability);
441
442     /**
443      * Removes a capability from the list of available capabilities.
444      *
445      * This may happen during first connect, or at any time later if an existing capability becomes
446      * unavailable (e.g. SASL service stopping).  This also removes the capability from the list
447      * of acknowledged capabilities.
448      *
449      * @param[in] capability Name of the capability
450      */
451     void removeCap(const QString &capability);
452
453     /**
454      * Clears all capabilities from the list of available capabilities.
455      *
456      * This also removes the capability from the list of acknowledged capabilities.
457      */
458     void clearCaps();
459
460     inline void addIrcUser(const QString &hostmask) { newIrcUser(hostmask); }
461     inline void addIrcChannel(const QString &channel) { newIrcChannel(channel); }
462
463     //init geters
464     QVariantMap initSupports() const;
465     /**
466      * Get the initial list of available capabilities.
467      *
468      * @return QVariantMap of <QString, QString> indicating available capabilities and values
469      */
470     QVariantMap initCaps() const;
471     /**
472      * Get the initial list of enabled (acknowledged) capabilities.
473      *
474      * @return QVariantList of QString indicating enabled (acknowledged) capabilities and values
475      */
476     QVariantList initCapsEnabled() const { return toVariantList(capsEnabled()); }
477     inline QVariantList initServerList() const { return toVariantList(serverList()); }
478     virtual QVariantMap initIrcUsersAndChannels() const;
479
480     //init seters
481     void initSetSupports(const QVariantMap &supports);
482     /**
483      * Initialize the list of available capabilities.
484      *
485      * @param[in] caps QVariantMap of <QString, QString> indicating available capabilities and values
486      */
487     void initSetCaps(const QVariantMap &caps);
488     /**
489      * Initialize the list of enabled (acknowledged) capabilities.
490      *
491      * @param[in] caps QVariantList of QString indicating enabled (acknowledged) capabilities and values
492      */
493     inline void initSetCapsEnabled(const QVariantList &capsEnabled) { _capsEnabled = fromVariantList<QString>(capsEnabled); }
494     inline void initSetServerList(const QVariantList &serverList) { _serverList = fromVariantList<Server>(serverList); }
495     virtual void initSetIrcUsersAndChannels(const QVariantMap &usersAndChannels);
496
497     /**
498      * Update IrcUser hostmask and username from mask, creating an IrcUser if one does not exist.
499      *
500      * @param[in] mask   Full nick!user@hostmask string
501      * @return IrcUser of the matching nick if exists, otherwise a new IrcUser
502      */
503     IrcUser *updateNickFromMask(const QString &mask);
504
505     // these slots are to keep the hashlists of all users and the
506     // channel lists up to date
507     void ircUserNickChanged(QString newnick);
508
509     virtual inline void requestConnect() const { REQUEST(NO_ARG) }
510     virtual inline void requestDisconnect() const { REQUEST(NO_ARG) }
511     virtual inline void requestSetNetworkInfo(const NetworkInfo &info) { REQUEST(ARG(info)) }
512
513     void emitConnectionError(const QString &);
514
515 protected slots:
516     virtual void removeIrcUser(IrcUser *ircuser);
517     virtual void removeIrcChannel(IrcChannel *ircChannel);
518     virtual void removeChansAndUsers();
519
520 signals:
521     void aboutToBeDestroyed();
522     void networkNameSet(const QString &networkName);
523     void currentServerSet(const QString &currentServer);
524     void connectedSet(bool isConnected);
525     void connectionStateSet(Network::ConnectionState);
526 //   void connectionStateSet(int);
527     void connectionError(const QString &errorMsg);
528     void myNickSet(const QString &mynick);
529 //   void latencySet(int latency);
530     void identitySet(IdentityId);
531
532     void configChanged();
533
534     //   void serverListSet(QVariantList serverList);
535 //   void useRandomServerSet(bool);
536 //   void performSet(const QStringList &);
537 //   void useAutoIdentifySet(bool);
538 //   void autoIdentifyServiceSet(const QString &);
539 //   void autoIdentifyPasswordSet(const QString &);
540 //   void useAutoReconnectSet(bool);
541 //   void autoReconnectIntervalSet(quint32);
542 //   void autoReconnectRetriesSet(quint16);
543 //   void unlimitedReconnectRetriesSet(bool);
544 //   void rejoinChannelsSet(bool);
545
546     // Custom rate limiting (can drive other slots)
547
548     /**
549      * Signals enabling or disabling custom rate limiting
550      *
551      * @see Network::useCustomMessageRate()
552      *
553      * @param[out] useCustomRate
554      */
555     void useCustomMessageRateSet(const bool useCustomRate);
556
557     /**
558      * Signals a change in maximum number of messages to send without any delays
559      *
560      * @see Network::messageRateBurstSize()
561      *
562      * @param[out] burstSize
563      */
564     void messageRateBurstSizeSet(const quint32 burstSize);
565
566     /**
567      * Signals a change in delay between messages after the max. undelayed messages have been sent
568      *
569      * @see Network::messageRateDelay()
570      *
571      * @param[out] messageDelay
572      */
573     void messageRateDelaySet(const quint32 messageDelay);
574
575     /**
576      * Signals enabling or disabling all rate limiting
577      *
578      * @see Network::unlimitedMessageRate()
579      *
580      * @param[out] unlimitedRate
581      */
582     void unlimitedMessageRateSet(const bool unlimitedRate);
583
584 //   void codecForServerSet(const QByteArray &codecName);
585 //   void codecForEncodingSet(const QByteArray &codecName);
586 //   void codecForDecodingSet(const QByteArray &codecName);
587
588 //   void supportAdded(const QString &param, const QString &value);
589 //   void supportRemoved(const QString &param);
590
591     // IRCv3 capability negotiation (can drive other slots)
592     /**
593      * Indicates a capability is now available, with optional value in Network::capValue().
594      *
595      * @see Network::addCap()
596      *
597      * @param[in] capability Name of the capability
598      */
599     void capAdded (const QString &capability);
600
601     /**
602      * Indicates a capability was acknowledged (enabled by the IRC server).
603      *
604      * @see Network::acknowledgeCap()
605      *
606      * @param[in] capability Name of the capability
607      */
608     void capAcknowledged(const QString &capability);
609
610     /**
611      * Indicates a capability was removed from the list of available capabilities.
612      *
613      * @see Network::removeCap()
614      *
615      * @param[in] capability Name of the capability
616      */
617     void capRemoved(const QString &capability);
618
619 //   void ircUserAdded(const QString &hostmask);
620     void ircUserAdded(IrcUser *);
621 //   void ircChannelAdded(const QString &channelname);
622     void ircChannelAdded(IrcChannel *);
623
624 //   void connectRequested() const;
625 //   void disconnectRequested() const;
626 //   void setNetworkInfoRequested(const NetworkInfo &) const;
627
628 protected:
629     inline virtual IrcChannel *ircChannelFactory(const QString &channelname) { return new IrcChannel(channelname, this); }
630     inline virtual IrcUser *ircUserFactory(const QString &hostmask) { return new IrcUser(hostmask, this); }
631
632 private:
633     QPointer<SignalProxy> _proxy;
634
635     NetworkId _networkId;
636     IdentityId _identity;
637
638     QString _myNick;
639     int _latency;
640     QString _networkName;
641     QString _currentServer;
642     bool _connected;
643     ConnectionState _connectionState;
644
645     mutable QString _prefixes;
646     mutable QString _prefixModes;
647
648     QHash<QString, IrcUser *> _ircUsers; // stores all known nicks for the server
649     QHash<QString, IrcChannel *> _ircChannels; // stores all known channels
650     QHash<QString, QString> _supports; // stores results from RPL_ISUPPORT
651
652     QHash<QString, QString> _caps;  /// Capabilities supported by the IRC server
653     // By synchronizing the supported capabilities, the client could suggest certain behaviors, e.g.
654     // in the Network settings dialog, recommending SASL instead of using NickServ, or warning if
655     // SASL EXTERNAL isn't available.
656     QStringList _capsEnabled;       /// Enabled capabilities that received 'CAP ACK'
657     // _capsEnabled uses the same values from the <name>=<value> pairs stored in _caps
658
659     ServerList _serverList;
660     bool _useRandomServer;
661     QStringList _perform;
662
663     bool _useAutoIdentify;
664     QString _autoIdentifyService;
665     QString _autoIdentifyPassword;
666
667     bool _useSasl;
668     QString _saslAccount;
669     QString _saslPassword;
670
671     bool _useAutoReconnect;
672     quint32 _autoReconnectInterval;
673     quint16 _autoReconnectRetries;
674     bool _unlimitedReconnectRetries;
675     bool _rejoinChannels;
676
677     // Custom rate limiting
678     bool _useCustomMessageRate;         /// If true, use custom rate limits, otherwise use defaults
679     quint32 _messageRateBurstSize;      /// Maximum number of messages to send without any delays
680     quint32 _messageRateDelay;          /// Delay in ms. for messages when max. burst messages sent
681     bool _unlimitedMessageRate;         /// If true, disable rate limiting, otherwise apply limits
682
683     QTextCodec *_codecForServer;
684     QTextCodec *_codecForEncoding;
685     QTextCodec *_codecForDecoding;
686
687     static QTextCodec *_defaultCodecForServer;
688     static QTextCodec *_defaultCodecForEncoding;
689     static QTextCodec *_defaultCodecForDecoding;
690
691     bool _autoAwayActive; // when this is active handle305 and handle306 don't trigger any output
692
693     friend class IrcUser;
694     friend class IrcChannel;
695 };
696
697
698 //! Stores all editable information about a network (as opposed to runtime state).
699 struct NetworkInfo {
700     // set some default values, note that this does not initialize e.g. name and id
701     NetworkInfo();
702
703     NetworkId networkId;
704     QString networkName;
705     IdentityId identity;
706
707     bool useCustomEncodings; // not used!
708     QByteArray codecForServer;
709     QByteArray codecForEncoding;
710     QByteArray codecForDecoding;
711
712     Network::ServerList serverList;
713     bool useRandomServer;
714
715     QStringList perform;
716
717     bool useAutoIdentify;
718     QString autoIdentifyService;
719     QString autoIdentifyPassword;
720
721     bool useSasl;
722     QString saslAccount;
723     QString saslPassword;
724
725     bool useAutoReconnect;
726     quint32 autoReconnectInterval;
727     quint16 autoReconnectRetries;
728     bool unlimitedReconnectRetries;
729     bool rejoinChannels;
730
731     // Custom rate limiting
732     bool useCustomMessageRate;         /// If true, use custom rate limits, otherwise use defaults
733     quint32 messageRateBurstSize;      /// Maximum number of messages to send without any delays
734     quint32 messageRateDelay;          /// Delay in ms. for messages when max. burst messages sent
735     bool unlimitedMessageRate;         /// If true, disable rate limiting, otherwise apply limits
736
737     bool operator==(const NetworkInfo &other) const;
738     bool operator!=(const NetworkInfo &other) const;
739 };
740
741 QDataStream &operator<<(QDataStream &out, const NetworkInfo &info);
742 QDataStream &operator>>(QDataStream &in, NetworkInfo &info);
743 QDebug operator<<(QDebug dbg, const NetworkInfo &i);
744 Q_DECLARE_METATYPE(NetworkInfo)
745
746 QDataStream &operator<<(QDataStream &out, const Network::Server &server);
747 QDataStream &operator>>(QDataStream &in, Network::Server &server);
748 QDebug operator<<(QDebug dbg, const Network::Server &server);
749 Q_DECLARE_METATYPE(Network::Server)
750
751 #endif