2018-02-05 15:11:50 +01:00
|
|
|
#pragma once
|
2019-09-08 22:27:57 +02:00
|
|
|
|
2018-08-11 14:20:53 +02:00
|
|
|
#include "common/Atomic.hpp"
|
2018-08-11 22:23:06 +02:00
|
|
|
#include "common/Channel.hpp"
|
2024-08-18 14:04:26 +02:00
|
|
|
#include "common/Common.hpp"
|
|
|
|
#include "providers/irc/IrcConnection2.hpp"
|
|
|
|
#include "util/RatelimitBucket.hpp"
|
2019-09-08 22:27:57 +02:00
|
|
|
|
2024-08-18 14:04:26 +02:00
|
|
|
#include <IrcMessage>
|
|
|
|
#include <pajlada/signals/signal.hpp>
|
2022-12-31 15:41:01 +01:00
|
|
|
#include <pajlada/signals/signalholder.hpp>
|
|
|
|
|
2018-06-24 12:59:47 +02:00
|
|
|
#include <chrono>
|
2024-08-18 14:04:26 +02:00
|
|
|
#include <functional>
|
2018-04-15 15:09:31 +02:00
|
|
|
#include <memory>
|
2024-08-18 14:04:26 +02:00
|
|
|
#include <mutex>
|
2018-06-06 18:57:22 +02:00
|
|
|
#include <queue>
|
2019-09-08 22:27:57 +02:00
|
|
|
|
2018-02-05 15:11:50 +01:00
|
|
|
namespace chatterino {
|
2019-09-08 22:27:57 +02:00
|
|
|
|
2018-08-02 14:23:27 +02:00
|
|
|
class Settings;
|
|
|
|
class Paths;
|
2018-08-13 14:38:03 +02:00
|
|
|
class TwitchChannel;
|
2024-01-21 14:20:21 +01:00
|
|
|
class BttvEmotes;
|
|
|
|
class FfzEmotes;
|
|
|
|
class SeventvEmotes;
|
2024-08-18 14:04:26 +02:00
|
|
|
class RatelimitBucket;
|
2019-09-08 22:27:57 +02:00
|
|
|
|
2023-05-21 12:10:49 +02:00
|
|
|
class ITwitchIrcServer
|
|
|
|
{
|
|
|
|
public:
|
2024-08-18 14:04:26 +02:00
|
|
|
ITwitchIrcServer() = default;
|
2023-05-21 12:10:49 +02:00
|
|
|
virtual ~ITwitchIrcServer() = default;
|
2024-08-18 14:04:26 +02:00
|
|
|
ITwitchIrcServer(const ITwitchIrcServer &) = delete;
|
|
|
|
ITwitchIrcServer(ITwitchIrcServer &&) = delete;
|
|
|
|
ITwitchIrcServer &operator=(const ITwitchIrcServer &) = delete;
|
|
|
|
ITwitchIrcServer &operator=(ITwitchIrcServer &&) = delete;
|
|
|
|
|
|
|
|
virtual void connect() = 0;
|
|
|
|
|
|
|
|
virtual void sendRawMessage(const QString &rawMessage) = 0;
|
|
|
|
|
|
|
|
virtual ChannelPtr getOrAddChannel(const QString &dirtyChannelName) = 0;
|
|
|
|
virtual ChannelPtr getChannelOrEmpty(const QString &dirtyChannelName) = 0;
|
|
|
|
|
|
|
|
virtual void addFakeMessage(const QString &data) = 0;
|
|
|
|
|
|
|
|
virtual void addGlobalSystemMessage(const QString &messageText) = 0;
|
|
|
|
|
|
|
|
virtual void forEachChannel(std::function<void(ChannelPtr)> func) = 0;
|
2023-05-21 12:10:49 +02:00
|
|
|
|
2024-06-01 14:56:40 +02:00
|
|
|
virtual void forEachChannelAndSpecialChannels(
|
|
|
|
std::function<void(ChannelPtr)> func) = 0;
|
|
|
|
|
|
|
|
virtual std::shared_ptr<Channel> getChannelOrEmptyByID(
|
|
|
|
const QString &channelID) = 0;
|
|
|
|
|
|
|
|
virtual void dropSeventvChannel(const QString &userID,
|
|
|
|
const QString &emoteSetID) = 0;
|
|
|
|
|
2023-10-13 17:41:23 +02:00
|
|
|
virtual const IndirectChannel &getWatchingChannel() const = 0;
|
2024-06-01 14:56:40 +02:00
|
|
|
virtual void setWatchingChannel(ChannelPtr newWatchingChannel) = 0;
|
|
|
|
virtual ChannelPtr getWhispersChannel() const = 0;
|
|
|
|
virtual ChannelPtr getMentionsChannel() const = 0;
|
|
|
|
virtual ChannelPtr getLiveChannel() const = 0;
|
|
|
|
virtual ChannelPtr getAutomodChannel() const = 0;
|
2023-05-21 12:10:49 +02:00
|
|
|
|
2024-02-17 13:26:54 +01:00
|
|
|
virtual QString getLastUserThatWhisperedMe() const = 0;
|
2024-06-01 14:56:40 +02:00
|
|
|
virtual void setLastUserThatWhisperedMe(const QString &user) = 0;
|
2024-02-17 13:26:54 +01:00
|
|
|
|
2023-05-21 12:10:49 +02:00
|
|
|
// Update this interface with TwitchIrcServer methods as needed
|
|
|
|
};
|
|
|
|
|
2024-08-18 14:04:26 +02:00
|
|
|
class TwitchIrcServer final : public ITwitchIrcServer, public QObject
|
2018-02-05 15:11:50 +01:00
|
|
|
{
|
2018-04-30 20:35:01 +02:00
|
|
|
public:
|
2024-08-18 14:04:26 +02:00
|
|
|
enum class ConnectionType {
|
|
|
|
Read,
|
|
|
|
Write,
|
|
|
|
};
|
|
|
|
|
2019-09-18 13:03:16 +02:00
|
|
|
TwitchIrcServer();
|
2023-10-25 18:13:48 +02:00
|
|
|
~TwitchIrcServer() override = default;
|
2019-09-08 22:27:57 +02:00
|
|
|
|
2024-06-03 10:31:30 +02:00
|
|
|
TwitchIrcServer(const TwitchIrcServer &) = delete;
|
|
|
|
TwitchIrcServer(TwitchIrcServer &&) = delete;
|
|
|
|
TwitchIrcServer &operator=(const TwitchIrcServer &) = delete;
|
|
|
|
TwitchIrcServer &operator=(TwitchIrcServer &&) = delete;
|
|
|
|
|
|
|
|
void initialize();
|
2019-09-08 22:27:57 +02:00
|
|
|
|
2024-06-01 14:56:40 +02:00
|
|
|
void forEachChannelAndSpecialChannels(
|
|
|
|
std::function<void(ChannelPtr)> func) override;
|
2019-09-08 22:27:57 +02:00
|
|
|
|
2024-06-01 14:56:40 +02:00
|
|
|
std::shared_ptr<Channel> getChannelOrEmptyByID(
|
|
|
|
const QString &channelID) override;
|
2019-09-08 22:27:57 +02:00
|
|
|
|
2022-08-28 12:20:47 +02:00
|
|
|
void reloadAllBTTVChannelEmotes();
|
|
|
|
void reloadAllFFZChannelEmotes();
|
2022-10-16 13:22:17 +02:00
|
|
|
void reloadAllSevenTVChannelEmotes();
|
2022-08-28 12:20:47 +02:00
|
|
|
|
2022-11-13 12:07:41 +01:00
|
|
|
/** Calls `func` with all twitch channels that have `emoteSetId` added. */
|
|
|
|
void forEachSeventvEmoteSet(const QString &emoteSetId,
|
|
|
|
std::function<void(TwitchChannel &)> func);
|
|
|
|
/** Calls `func` with all twitch channels where the seventv-user-id is `userId`. */
|
|
|
|
void forEachSeventvUser(const QString &userId,
|
|
|
|
std::function<void(TwitchChannel &)> func);
|
|
|
|
/**
|
|
|
|
* Checks if any channel still needs this `userID` or `emoteSetID`.
|
|
|
|
* If not, it unsubscribes from the respective messages.
|
|
|
|
*
|
|
|
|
* It's currently not possible to share emote sets among users,
|
|
|
|
* but it's a commonly requested feature.
|
|
|
|
*/
|
2024-06-01 14:56:40 +02:00
|
|
|
void dropSeventvChannel(const QString &userID,
|
|
|
|
const QString &emoteSetID) override;
|
2022-11-13 12:07:41 +01:00
|
|
|
|
2024-08-18 14:04:26 +02:00
|
|
|
void addFakeMessage(const QString &data) override;
|
|
|
|
|
|
|
|
void addGlobalSystemMessage(const QString &messageText) override;
|
|
|
|
|
|
|
|
// iteration
|
|
|
|
void forEachChannel(std::function<void(ChannelPtr)> func) override;
|
|
|
|
|
|
|
|
void connect() override;
|
|
|
|
void disconnect();
|
|
|
|
|
|
|
|
void sendMessage(const QString &channelName, const QString &message);
|
|
|
|
void sendRawMessage(const QString &rawMessage) override;
|
|
|
|
|
|
|
|
ChannelPtr getOrAddChannel(const QString &dirtyChannelName) override;
|
|
|
|
|
|
|
|
ChannelPtr getChannelOrEmpty(const QString &dirtyChannelName) override;
|
|
|
|
|
|
|
|
void open(ConnectionType type);
|
|
|
|
|
2024-06-01 14:56:40 +02:00
|
|
|
private:
|
2018-08-11 14:20:53 +02:00
|
|
|
Atomic<QString> lastUserThatWhisperedMe;
|
2019-09-08 22:27:57 +02:00
|
|
|
|
2018-02-05 15:11:50 +01:00
|
|
|
const ChannelPtr whispersChannel;
|
|
|
|
const ChannelPtr mentionsChannel;
|
2021-05-09 16:17:04 +02:00
|
|
|
const ChannelPtr liveChannel;
|
2023-12-03 23:07:30 +01:00
|
|
|
const ChannelPtr automodChannel;
|
2018-04-20 19:54:45 +02:00
|
|
|
IndirectChannel watchingChannel;
|
2019-09-08 22:27:57 +02:00
|
|
|
|
2024-06-01 14:56:40 +02:00
|
|
|
public:
|
2023-10-13 17:41:23 +02:00
|
|
|
const IndirectChannel &getWatchingChannel() const override;
|
2024-06-01 14:56:40 +02:00
|
|
|
void setWatchingChannel(ChannelPtr newWatchingChannel) override;
|
|
|
|
ChannelPtr getWhispersChannel() const override;
|
|
|
|
ChannelPtr getMentionsChannel() const override;
|
|
|
|
ChannelPtr getLiveChannel() const override;
|
|
|
|
ChannelPtr getAutomodChannel() const override;
|
2019-09-08 22:27:57 +02:00
|
|
|
|
2024-02-17 13:26:54 +01:00
|
|
|
QString getLastUserThatWhisperedMe() const override;
|
2024-06-01 14:56:40 +02:00
|
|
|
void setLastUserThatWhisperedMe(const QString &user) override;
|
2024-02-17 13:26:54 +01:00
|
|
|
|
2018-02-05 15:11:50 +01:00
|
|
|
protected:
|
2024-08-18 14:04:26 +02:00
|
|
|
void initializeConnection(IrcConnection *connection, ConnectionType type);
|
|
|
|
std::shared_ptr<Channel> createChannel(const QString &channelName);
|
|
|
|
|
|
|
|
void privateMessageReceived(Communi::IrcPrivateMessage *message);
|
|
|
|
void readConnectionMessageReceived(Communi::IrcMessage *message);
|
|
|
|
void writeConnectionMessageReceived(Communi::IrcMessage *message);
|
2023-10-25 18:13:48 +02:00
|
|
|
|
2024-08-18 14:04:26 +02:00
|
|
|
void onReadConnected(IrcConnection *connection);
|
|
|
|
void onWriteConnected(IrcConnection *connection);
|
|
|
|
void onDisconnected();
|
|
|
|
void markChannelsConnected();
|
2023-10-25 18:13:48 +02:00
|
|
|
|
2024-08-18 14:04:26 +02:00
|
|
|
std::shared_ptr<Channel> getCustomChannel(const QString &channelname);
|
2019-09-08 22:27:57 +02:00
|
|
|
|
2024-08-18 14:04:26 +02:00
|
|
|
QString cleanChannelName(const QString &dirtyChannelName);
|
2019-09-08 22:27:57 +02:00
|
|
|
|
2018-05-25 13:53:55 +02:00
|
|
|
private:
|
2024-02-25 14:45:55 +01:00
|
|
|
void onMessageSendRequested(const std::shared_ptr<TwitchChannel> &channel,
|
|
|
|
const QString &message, bool &sent);
|
|
|
|
void onReplySendRequested(const std::shared_ptr<TwitchChannel> &channel,
|
|
|
|
const QString &message, const QString &replyId,
|
|
|
|
bool &sent);
|
2022-07-31 12:45:25 +02:00
|
|
|
|
2024-02-25 14:45:55 +01:00
|
|
|
bool prepareToSend(const std::shared_ptr<TwitchChannel> &channel);
|
2019-09-08 22:27:57 +02:00
|
|
|
|
2024-08-18 14:04:26 +02:00
|
|
|
QMap<QString, std::weak_ptr<Channel>> channels;
|
|
|
|
std::mutex channelMutex;
|
|
|
|
|
|
|
|
QObjectPtr<IrcConnection> writeConnection_ = nullptr;
|
|
|
|
QObjectPtr<IrcConnection> readConnection_ = nullptr;
|
|
|
|
|
|
|
|
// Our rate limiting bucket for the Twitch join rate limits
|
|
|
|
// https://dev.twitch.tv/docs/irc/guide#rate-limits
|
|
|
|
QObjectPtr<RatelimitBucket> joinBucket_;
|
|
|
|
|
|
|
|
QTimer reconnectTimer_;
|
|
|
|
int falloffCounter_ = 1;
|
|
|
|
|
|
|
|
std::mutex connectionMutex_;
|
|
|
|
|
|
|
|
pajlada::Signals::SignalHolder connections_;
|
|
|
|
|
2018-06-24 13:06:11 +02:00
|
|
|
std::mutex lastMessageMutex_;
|
|
|
|
std::queue<std::chrono::steady_clock::time_point> lastMessagePleb_;
|
|
|
|
std::queue<std::chrono::steady_clock::time_point> lastMessageMod_;
|
|
|
|
std::chrono::steady_clock::time_point lastErrorTimeSpeed_;
|
|
|
|
std::chrono::steady_clock::time_point lastErrorTimeAmount_;
|
2018-02-05 15:11:50 +01:00
|
|
|
};
|
2019-09-08 22:27:57 +02:00
|
|
|
|
2018-02-05 15:11:50 +01:00
|
|
|
} // namespace chatterino
|