2017-06-13 21:13:58 +02:00
|
|
|
#include "application.hpp"
|
2018-04-27 22:11:19 +02:00
|
|
|
|
2018-02-05 15:11:50 +01:00
|
|
|
#include "providers/twitch/twitchserver.hpp"
|
2017-12-31 00:50:07 +01:00
|
|
|
#include "singletons/accountmanager.hpp"
|
2018-01-04 02:50:36 +01:00
|
|
|
#include "singletons/commandmanager.hpp"
|
2017-12-31 00:50:07 +01:00
|
|
|
#include "singletons/emotemanager.hpp"
|
2018-04-28 15:20:18 +02:00
|
|
|
#include "singletons/fontmanager.hpp"
|
2018-02-05 15:11:50 +01:00
|
|
|
#include "singletons/loggingmanager.hpp"
|
2018-04-09 22:59:19 +02:00
|
|
|
#include "singletons/nativemessagingmanager.hpp"
|
2018-04-20 00:15:57 +02:00
|
|
|
#include "singletons/pathmanager.hpp"
|
2018-04-15 15:09:31 +02:00
|
|
|
#include "singletons/pubsubmanager.hpp"
|
2018-04-28 15:20:18 +02:00
|
|
|
#include "singletons/resourcemanager.hpp"
|
2017-12-31 00:50:07 +01:00
|
|
|
#include "singletons/settingsmanager.hpp"
|
|
|
|
#include "singletons/thememanager.hpp"
|
|
|
|
#include "singletons/windowmanager.hpp"
|
2018-04-25 14:53:54 +02:00
|
|
|
#include "util/posttothread.hpp"
|
2017-06-13 21:13:58 +02:00
|
|
|
|
2018-04-27 22:11:19 +02:00
|
|
|
#include <atomic>
|
|
|
|
|
|
|
|
#ifdef Q_OS_WIN
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <io.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#endif
|
|
|
|
|
2017-12-31 22:58:35 +01:00
|
|
|
using namespace chatterino::singletons;
|
|
|
|
|
2017-06-13 21:13:58 +02:00
|
|
|
namespace chatterino {
|
|
|
|
|
2018-04-27 22:11:19 +02:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
bool isBigEndian()
|
|
|
|
{
|
|
|
|
int test = 1;
|
|
|
|
char *p = (char *)&test;
|
|
|
|
|
|
|
|
return p[0] == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
static std::atomic<bool> isAppConstructed{false};
|
|
|
|
static std::atomic<bool> isAppInitialized{false};
|
|
|
|
|
|
|
|
static Application *staticApp = nullptr;
|
|
|
|
|
2017-06-26 16:41:20 +02:00
|
|
|
// this class is responsible for handling the workflow of Chatterino
|
|
|
|
// It will create the instances of the major classes, and connect their signals to each other
|
|
|
|
|
2018-04-27 22:11:19 +02:00
|
|
|
Application::Application(int _argc, char **_argv)
|
|
|
|
: argc(_argc)
|
|
|
|
, argv(_argv)
|
2017-06-13 21:13:58 +02:00
|
|
|
{
|
2018-04-27 22:11:19 +02:00
|
|
|
}
|
2018-04-09 22:59:19 +02:00
|
|
|
|
2018-04-27 22:11:19 +02:00
|
|
|
void Application::construct()
|
|
|
|
{
|
|
|
|
assert(isAppConstructed == false);
|
|
|
|
isAppConstructed = true;
|
|
|
|
|
|
|
|
// 1. Instantiate all classes
|
2018-04-28 15:20:18 +02:00
|
|
|
this->paths = new singletons::PathManager(this->argc, this->argv);
|
2018-04-27 22:11:19 +02:00
|
|
|
this->themes = new singletons::ThemeManager;
|
|
|
|
this->windows = new singletons::WindowManager;
|
|
|
|
this->logging = new singletons::LoggingManager;
|
|
|
|
this->commands = new singletons::CommandManager;
|
|
|
|
this->accounts = new singletons::AccountManager;
|
|
|
|
this->emotes = new singletons::EmoteManager;
|
|
|
|
this->pubsub = new singletons::PubSubManager;
|
|
|
|
this->settings = new singletons::SettingManager;
|
2018-04-28 15:20:18 +02:00
|
|
|
this->fonts = new singletons::FontManager;
|
|
|
|
this->resources = new singletons::ResourceManager;
|
|
|
|
|
|
|
|
this->twitch.server = new providers::twitch::TwitchServer;
|
2018-04-27 22:11:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void Application::instantiate(int argc, char **argv)
|
|
|
|
{
|
|
|
|
assert(staticApp == nullptr);
|
2017-12-31 22:58:35 +01:00
|
|
|
|
2018-04-27 22:11:19 +02:00
|
|
|
staticApp = new Application(argc, argv);
|
|
|
|
}
|
2017-06-13 21:13:58 +02:00
|
|
|
|
2018-04-27 22:11:19 +02:00
|
|
|
void Application::initialize()
|
|
|
|
{
|
|
|
|
assert(isAppInitialized == false);
|
|
|
|
isAppInitialized = true;
|
2018-01-05 02:23:49 +01:00
|
|
|
|
2018-04-27 22:11:19 +02:00
|
|
|
// 2. Initialize/load classes
|
2018-04-28 15:20:18 +02:00
|
|
|
this->settings->initialize();
|
2018-04-27 22:11:19 +02:00
|
|
|
this->windows->initialize();
|
|
|
|
|
|
|
|
this->nativeMessaging->registerHost();
|
|
|
|
|
2018-04-28 15:20:18 +02:00
|
|
|
this->settings->load();
|
2018-04-27 22:11:19 +02:00
|
|
|
this->commands->loadCommands();
|
2017-12-14 00:25:06 +01:00
|
|
|
|
2018-04-27 22:11:19 +02:00
|
|
|
this->emotes->loadGlobalEmotes();
|
2017-06-13 21:13:58 +02:00
|
|
|
|
2018-04-27 22:11:19 +02:00
|
|
|
this->accounts->load();
|
2017-07-28 19:46:53 +02:00
|
|
|
|
2018-04-28 15:20:18 +02:00
|
|
|
this->twitch.server->initialize();
|
|
|
|
|
2017-06-13 21:13:58 +02:00
|
|
|
// XXX
|
2018-04-27 22:11:19 +02:00
|
|
|
this->settings->updateWordTypeMask();
|
2018-04-12 00:33:55 +02:00
|
|
|
|
2018-04-27 22:11:19 +02:00
|
|
|
this->nativeMessaging->openGuiMessageQueue();
|
2018-04-15 15:09:31 +02:00
|
|
|
|
2018-04-27 22:11:19 +02:00
|
|
|
this->pubsub->sig.whisper.sent.connect([](const auto &msg) {
|
2018-04-15 15:09:31 +02:00
|
|
|
debug::Log("WHISPER SENT LOL"); //
|
|
|
|
});
|
|
|
|
|
2018-04-27 22:11:19 +02:00
|
|
|
this->pubsub->sig.whisper.received.connect([](const auto &msg) {
|
2018-04-15 15:09:31 +02:00
|
|
|
debug::Log("WHISPER RECEIVED LOL"); //
|
|
|
|
});
|
|
|
|
|
2018-04-27 22:11:19 +02:00
|
|
|
this->pubsub->sig.moderation.chatCleared.connect([&](const auto &action) {
|
2018-04-15 15:09:31 +02:00
|
|
|
debug::Log("Chat cleared by {}", action.source.name); //
|
|
|
|
});
|
|
|
|
|
2018-04-27 22:11:19 +02:00
|
|
|
this->pubsub->sig.moderation.modeChanged.connect([&](const auto &action) {
|
2018-04-15 15:09:31 +02:00
|
|
|
debug::Log("Mode {} was turned {} by {} (duration {})", (int &)action.mode,
|
|
|
|
(bool &)action.state, action.source.name, action.args.duration);
|
|
|
|
});
|
|
|
|
|
2018-04-27 22:11:19 +02:00
|
|
|
this->pubsub->sig.moderation.moderationStateChanged.connect([&](const auto &action) {
|
2018-04-15 15:09:31 +02:00
|
|
|
debug::Log("User {} was {} by {}", action.target.id, action.modded ? "modded" : "unmodded",
|
|
|
|
action.source.name);
|
|
|
|
});
|
|
|
|
|
2018-04-27 22:11:19 +02:00
|
|
|
this->pubsub->sig.moderation.userBanned.connect([&](const auto &action) {
|
2018-04-28 15:20:18 +02:00
|
|
|
auto chan = this->twitch.server->getChannelOrEmptyByID(action.roomID);
|
2018-04-22 15:47:39 +02:00
|
|
|
|
|
|
|
if (chan->isEmpty()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-04-27 18:35:31 +02:00
|
|
|
auto msg = messages::Message::createTimeoutMessage(action);
|
2018-04-22 15:47:39 +02:00
|
|
|
|
2018-04-25 14:53:54 +02:00
|
|
|
util::postToThread([chan, msg] { chan->addMessage(msg); });
|
2018-04-15 15:09:31 +02:00
|
|
|
});
|
|
|
|
|
2018-04-27 22:11:19 +02:00
|
|
|
this->pubsub->sig.moderation.userUnbanned.connect([&](const auto &action) {
|
2018-04-28 15:20:18 +02:00
|
|
|
auto chan = this->twitch.server->getChannelOrEmptyByID(action.roomID);
|
2018-04-27 18:35:31 +02:00
|
|
|
|
|
|
|
if (chan->isEmpty()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto msg = messages::Message::createUntimeoutMessage(action);
|
|
|
|
|
|
|
|
util::postToThread([chan, msg] { chan->addMessage(msg); });
|
2018-04-15 15:09:31 +02:00
|
|
|
});
|
|
|
|
|
2018-04-27 22:11:19 +02:00
|
|
|
this->pubsub->Start();
|
2018-04-15 15:09:31 +02:00
|
|
|
|
2018-04-27 22:11:19 +02:00
|
|
|
auto RequestModerationActions = [=]() {
|
|
|
|
this->pubsub->UnlistenAllModerationActions();
|
2018-04-15 15:09:31 +02:00
|
|
|
// TODO(pajlada): Unlisten to all authed topics instead of only moderation topics
|
2018-04-27 22:11:19 +02:00
|
|
|
// this->pubsub->UnlistenAllAuthedTopics();
|
2018-04-15 15:09:31 +02:00
|
|
|
|
2018-04-27 22:11:19 +02:00
|
|
|
this->pubsub->ListenToWhispers(this->accounts->Twitch.getCurrent()); //
|
2018-04-15 15:09:31 +02:00
|
|
|
};
|
|
|
|
|
2018-04-27 22:11:19 +02:00
|
|
|
this->accounts->Twitch.userChanged.connect(RequestModerationActions);
|
2018-04-15 15:09:31 +02:00
|
|
|
|
|
|
|
RequestModerationActions();
|
2017-06-13 21:13:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int Application::run(QApplication &qtApp)
|
|
|
|
{
|
|
|
|
// Start connecting to the IRC Servers (Twitch only for now)
|
2018-04-28 15:20:18 +02:00
|
|
|
this->twitch.server->connect();
|
2017-06-13 21:13:58 +02:00
|
|
|
|
|
|
|
// Show main window
|
2018-04-27 22:11:19 +02:00
|
|
|
this->windows->getMainWindow().show();
|
2017-06-13 21:13:58 +02:00
|
|
|
|
|
|
|
return qtApp.exec();
|
|
|
|
}
|
|
|
|
|
2017-12-22 14:44:31 +01:00
|
|
|
void Application::save()
|
|
|
|
{
|
2018-04-27 22:11:19 +02:00
|
|
|
this->windows->save();
|
|
|
|
|
|
|
|
this->commands->saveCommands();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Application::runNativeMessagingHost()
|
|
|
|
{
|
|
|
|
auto app = getApp();
|
|
|
|
|
|
|
|
app->nativeMessaging = new singletons::NativeMessagingManager;
|
|
|
|
|
|
|
|
#ifdef Q_OS_WIN
|
|
|
|
_setmode(_fileno(stdin), _O_BINARY);
|
|
|
|
_setmode(_fileno(stdout), _O_BINARY);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
bool bigEndian = isBigEndian();
|
|
|
|
#endif
|
|
|
|
|
|
|
|
while (true) {
|
|
|
|
char size_c[4];
|
|
|
|
std::cin.read(size_c, 4);
|
|
|
|
|
|
|
|
if (std::cin.eof()) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t size = *reinterpret_cast<uint32_t *>(size_c);
|
|
|
|
#if 0
|
|
|
|
// To avoid breaking strict-aliasing rules and potentially inducing undefined behaviour, the following code can be run instead
|
|
|
|
uint32_t size = 0;
|
|
|
|
if (bigEndian) {
|
|
|
|
size = size_c[3] | static_cast<uint32_t>(size_c[2]) << 8 |
|
|
|
|
static_cast<uint32_t>(size_c[1]) << 16 | static_cast<uint32_t>(size_c[0]) << 24;
|
|
|
|
} else {
|
|
|
|
size = size_c[0] | static_cast<uint32_t>(size_c[1]) << 8 |
|
|
|
|
static_cast<uint32_t>(size_c[2]) << 16 | static_cast<uint32_t>(size_c[3]) << 24;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
char *b = (char *)malloc(size + 1);
|
|
|
|
std::cin.read(b, size);
|
|
|
|
*(b + size) = '\0';
|
|
|
|
|
|
|
|
app->nativeMessaging->sendToGuiProcess(QByteArray(b, size));
|
|
|
|
|
|
|
|
free(b);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Application *getApp()
|
|
|
|
{
|
|
|
|
assert(staticApp != nullptr);
|
2018-01-05 02:38:00 +01:00
|
|
|
|
2018-04-27 22:11:19 +02:00
|
|
|
return staticApp;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool appInitialized()
|
|
|
|
{
|
|
|
|
return isAppInitialized;
|
2017-12-22 14:44:31 +01:00
|
|
|
}
|
|
|
|
|
2017-06-13 21:13:58 +02:00
|
|
|
} // namespace chatterino
|