+ enum ConnectionState
+ {
+ Disconnected,
+ Connecting,
+ Initializing,
+ Initialized,
+ Reconnecting,
+ Disconnecting
+ };
+
+ // see:
+ // http://www.irc.org/tech_docs/005.html
+ // http://www.irc.org/tech_docs/draft-brocklesby-irc-isupport-03.txt
+ enum ChannelModeType
+ {
+ NOT_A_CHANMODE = 0x00,
+ A_CHANMODE = 0x01,
+ B_CHANMODE = 0x02,
+ C_CHANMODE = 0x04,
+ D_CHANMODE = 0x08
+ };
+
+ // Default port assignments according to what many IRC networks have settled on.
+ // Technically not a standard, but it's fairly widespread.
+ // See https://freenode.net/news/port-6697-irc-via-tlsssl
+ enum PortDefaults
+ {
+ PORT_PLAINTEXT = 6667, /// Default port for unencrypted connections
+ PORT_SSL = 6697 /// Default port for encrypted connections
+ };
+
+ struct Server
+ {
+ QString host;
+ uint port{6667};
+ QString password;
+ bool useSsl{false};
+ bool sslVerify{true}; /// If true, validate SSL certificates
+ int sslVersion{0};
+
+ bool useProxy{false};
+ int proxyType{QNetworkProxy::Socks5Proxy};
+ QString proxyHost;
+ uint proxyPort{8080};
+ QString proxyUser;
+ QString proxyPass;
+
+ // sslVerify only applies when useSsl is true. sslVerify should be enabled by default,
+ // so enabling useSsl offers a more secure default.
+ Server()
+ : proxyHost("localhost")
+ {}
+
+ Server(QString host, uint port, QString password, bool useSsl, bool sslVerify)
+ : host(std::move(host))
+ , port(port)
+ , password(std::move(password))
+ , useSsl(useSsl)
+ , sslVerify(sslVerify)
+ , proxyType(QNetworkProxy::Socks5Proxy)
+ , proxyHost("localhost")
+ , proxyPort(8080)
+ {}
+
+ bool operator==(const Server& other) const;
+ bool operator!=(const Server& other) const;
+ };
+ using ServerList = QList<Server>;
+
+ Network(const NetworkId& networkid, QObject* parent = nullptr);
+ ~Network() override;
+
+ inline NetworkId networkId() const { return _networkId; }
+
+ inline SignalProxy* proxy() const { return _proxy; }
+ inline void setProxy(SignalProxy* proxy) { _proxy = proxy; }
+
+ inline bool isMyNick(const QString& nick) const { return (myNick().toLower() == nick.toLower()); }
+ inline bool isMe(IrcUser* ircuser) const { return (ircuser->nick().toLower() == myNick().toLower()); }
+
+ bool isChannelName(const QString& channelname) const;
+
+ /**
+ * Checks if the target counts as a STATUSMSG
+ *
+ * Status messages are prefixed with one or more characters from the server-provided STATUSMSG
+ * if available, otherwise "@" and "+" are assumed. Generally, status messages sent to a
+ * channel are only visible to those with the same or higher permissions, e.g. voiced.
+ *
+ * @param[in] target Name of destination, e.g. a channel or query
+ * @returns True if a STATUSMSG, otherwise false
+ */
+ bool isStatusMsg(const QString& target) const;
+
+ inline bool isConnected() const { return _connected; }
+ // Network::ConnectionState connectionState() const;
+ inline int connectionState() const { return _connectionState; }
+
+ /**@{*/
+ /**
+ * Translates a user’s prefix to the channelmode associated with it.
+ * @param prefix Prefix to be translated.
+ */
+ QString prefixToMode(const QString& prefix) const;
+ inline QString prefixToMode(const QCharRef& prefix) const { return prefixToMode(QString(prefix)); }
+ inline QString prefixesToModes(const QString& prefix) const
+ {
+ QString modes;
+ for (QChar c : prefix) {
+ modes += prefixToMode(c);
+ }
+ return modes;
+ }
+ /**@}*/
+
+ /**@{*/
+ /**
+ * Translates a user’s prefix to the channelmode associated with it.
+ * @param prefix Prefix to be translated.
+ */
+ QString modeToPrefix(const QString& mode) const;
+ inline QString modeToPrefix(const QCharRef& mode) const { return modeToPrefix(QString(mode)); }
+ inline QString modesToPrefixes(const QString& mode) const
+ {
+ QString prefixes;
+ for (QChar c : mode) {
+ prefixes += modeToPrefix(c);
+ }
+ return prefixes;
+ }
+ /**@}*/
+
+ /**
+ * Sorts the user channelmodes according to priority set by PREFIX
+ *
+ * Given a list of channel modes, sorts according to the order of PREFIX, putting the highest
+ * modes first. Any unknown modes are moved to the end in no given order.
+ *
+ * If prefix modes cannot be determined from the network, no changes will be made.
+ *
+ * @param modes User channelmodes
+ * @return Priority-sorted user channelmodes
+ */
+ QString sortPrefixModes(const QString& modes) const;
+
+ /**@{*/
+ /**
+ * Sorts the list of users' channelmodes according to priority set by PREFIX
+ *
+ * Maintains order of the modes list.
+ *
+ * @seealso Network::sortPrefixModes()
+ *
+ * @param modesList List of users' channel modes
+ * @return Priority-sorted list of users' channel modes
+ */
+ inline QStringList sortPrefixModes(const QStringList& modesList) const
+ {
+ QStringList sortedModesList;
+ // Sort each individual mode string, appending back
+ // Must maintain the order received!
+ for (QString modes : modesList) {
+ sortedModesList << sortPrefixModes(modes);
+ }
+ return sortedModesList;
+ }
+ /**@}*/
+
+ ChannelModeType channelModeType(const QString& mode);
+ inline ChannelModeType channelModeType(const QCharRef& mode) { return channelModeType(QString(mode)); }
+
+ inline const QString& networkName() const { return _networkName; }
+ inline const QString& currentServer() const { return _currentServer; }
+ inline const QString& myNick() const { return _myNick; }
+ inline int latency() const { return _latency; }
+ inline IrcUser* me() const { return ircUser(myNick()); }
+ inline IdentityId identity() const { return _identity; }
+ QStringList nicks() const;
+ inline QStringList channels() const { return _ircChannels.keys(); }
+ /**
+ * Gets the list of available capabilities.
+ *
+ * @returns QStringList of available capabilities
+ */
+ inline const QStringList caps() const { return QStringList(_caps.keys()); }
+ /**
+ * Gets the list of enabled (acknowledged) capabilities.
+ *
+ * @returns QStringList of enabled (acknowledged) capabilities
+ */
+ inline const QStringList capsEnabled() const { return _capsEnabled; }
+ inline const ServerList& serverList() const { return _serverList; }
+ inline bool useRandomServer() const { return _useRandomServer; }
+ inline const QStringList& perform() const { return _perform; }
+ inline bool useAutoIdentify() const { return _useAutoIdentify; }
+ inline const QString& autoIdentifyService() const { return _autoIdentifyService; }
+ inline const QString& autoIdentifyPassword() const { return _autoIdentifyPassword; }
+ inline bool useSasl() const { return _useSasl; }
+ inline const QString& saslAccount() const { return _saslAccount; }
+ inline const QString& saslPassword() const { return _saslPassword; }
+ inline bool useAutoReconnect() const { return _useAutoReconnect; }
+ inline quint32 autoReconnectInterval() const { return _autoReconnectInterval; }
+ inline quint16 autoReconnectRetries() const { return _autoReconnectRetries; }
+ inline bool unlimitedReconnectRetries() const { return _unlimitedReconnectRetries; }
+ inline bool rejoinChannels() const { return _rejoinChannels; }
+
+ // Custom rate limiting
+
+ /**
+ * Gets whether or not custom rate limiting is used
+ *
+ * @return True if custom rate limiting is enabled, otherwise false.
+ */
+ inline bool useCustomMessageRate() const { return _useCustomMessageRate; }
+
+ /**
+ * Gets maximum number of messages to send without any delays
+ *
+ * @return
+ * @parblock
+ * Maximum number of messages to send without any delays. A value of 1 disables message
+ * bursting.
+ * @endparblock
+ */
+ inline quint32 messageRateBurstSize() const { return _messageRateBurstSize; }
+
+ /**
+ * Gets the delay between messages after the maximum number of undelayed messages have been sent
+ *
+ * @return
+ * @parblock
+ * Delay in milliseconds between messages after the maximum number of undelayed messages have
+ * been sent.
+ * @endparblock
+ */
+ inline quint32 messageRateDelay() const { return _messageRateDelay; }
+
+ /**
+ * Gets whether or not all rate limiting is disabled, e.g. for IRC bridges
+ *
+ * @return If true, disable rate limiting, otherwise apply configured limits.
+ */
+ inline bool unlimitedMessageRate() const { return _unlimitedMessageRate; }
+
+ NetworkInfo networkInfo() const;
+ void setNetworkInfo(const NetworkInfo&);
+
+ QString prefixes() const;
+ QString prefixModes() const;
+ void determinePrefixes() const;
+
+ bool supports(const QString& param) const { return _supports.contains(param); }
+ QString support(const QString& param) const;
+
+ /**
+ * Checks if a given capability is advertised by the server.
+ *
+ * These results aren't valid if the network is disconnected or capability negotiation hasn't
+ * happened, and some servers might not correctly advertise capabilities. Don't treat this as
+ * a guarantee.
+ *
+ * @param[in] capability Name of capability
+ * @returns True if connected and advertised by the server, otherwise false
+ */
+ inline bool capAvailable(const QString& capability) const { return _caps.contains(capability.toLower()); }
+ // IRCv3 specs all use lowercase capability names
+
+ /**
+ * Checks if a given capability is acknowledged and active.
+ *
+ * @param[in] capability Name of capability
+ * @returns True if acknowledged (active), otherwise false
+ */
+ inline bool capEnabled(const QString& capability) const { return _capsEnabled.contains(capability.toLower()); }
+ // IRCv3 specs all use lowercase capability names
+
+ /**
+ * Gets the value of an available capability, e.g. for SASL, "EXTERNAL,PLAIN".
+ *
+ * @param[in] capability Name of capability
+ * @returns Value of capability if one was specified, otherwise empty string
+ */
+ QString capValue(const QString& capability) const { return _caps.value(capability.toLower()); }
+ // IRCv3 specs all use lowercase capability names
+ // QHash returns the default constructed value if not found, in this case, empty string
+ // See: https://doc.qt.io/qt-4.8/qhash.html#value
+
+ /**
+ * Check if the given authentication mechanism is likely to be supported.
+ *
+ * This depends on the server advertising SASL support and either declaring available mechanisms
+ * (SASL 3.2), or just indicating something is supported (SASL 3.1).
+ *
+ * @param[in] saslMechanism Desired SASL mechanism
+ * @return True if mechanism supported or unknown, otherwise false
+ */
+ bool saslMaybeSupports(const QString& saslMechanism) const;
+
+ IrcUser* newIrcUser(const QString& hostmask, const QVariantMap& initData = QVariantMap());
+ inline IrcUser* newIrcUser(const QByteArray& hostmask) { return newIrcUser(decodeServerString(hostmask)); }
+ IrcUser* ircUser(QString nickname) const;
+ inline IrcUser* ircUser(const QByteArray& nickname) const { return ircUser(decodeServerString(nickname)); }
+ inline QList<IrcUser*> ircUsers() const { return _ircUsers.values(); }
+ inline quint32 ircUserCount() const { return _ircUsers.count(); }
+
+ IrcChannel* newIrcChannel(const QString& channelname, const QVariantMap& initData = QVariantMap());
+ inline IrcChannel* newIrcChannel(const QByteArray& channelname) { return newIrcChannel(decodeServerString(channelname)); }
+ IrcChannel* ircChannel(QString channelname) const;
+ inline IrcChannel* ircChannel(const QByteArray& channelname) const { return ircChannel(decodeServerString(channelname)); }
+ inline QList<IrcChannel*> ircChannels() const { return _ircChannels.values(); }
+ inline quint32 ircChannelCount() const { return _ircChannels.count(); }
+
+ QByteArray codecForServer() const;
+ QByteArray codecForEncoding() const;
+ QByteArray codecForDecoding() const;
+ void setCodecForServer(QTextCodec* codec);
+ void setCodecForEncoding(QTextCodec* codec);
+ void setCodecForDecoding(QTextCodec* codec);
+
+ QString decodeString(const QByteArray& text) const;
+ QByteArray encodeString(const QString& string) const;
+ QString decodeServerString(const QByteArray& text) const;
+ QByteArray encodeServerString(const QString& string) const;
+
+ static QByteArray defaultCodecForServer();
+ static QByteArray defaultCodecForEncoding();
+ static QByteArray defaultCodecForDecoding();
+ static void setDefaultCodecForServer(const QByteArray& name);
+ static void setDefaultCodecForEncoding(const QByteArray& name);
+ static void setDefaultCodecForDecoding(const QByteArray& name);
+
+ inline bool autoAwayActive() const { return _autoAwayActive; }
+ inline void setAutoAwayActive(bool active) { _autoAwayActive = active; }