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