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