2018-02-05 15:11:50 +01:00
|
|
|
#include "twitchserver.hpp"
|
|
|
|
|
2018-04-27 22:11:19 +02:00
|
|
|
#include "application.hpp"
|
2018-05-26 20:25:00 +02:00
|
|
|
#include "controllers/accounts/accountcontroller.hpp"
|
2018-05-23 22:27:29 +02:00
|
|
|
#include "controllers/highlights/highlightcontroller.hpp"
|
2018-02-05 15:11:50 +01:00
|
|
|
#include "providers/twitch/ircmessagehandler.hpp"
|
|
|
|
#include "providers/twitch/twitchaccount.hpp"
|
2018-03-25 11:37:57 +02:00
|
|
|
#include "providers/twitch/twitchhelpers.hpp"
|
2018-02-05 15:11:50 +01:00
|
|
|
#include "providers/twitch/twitchmessagebuilder.hpp"
|
|
|
|
#include "util/posttothread.hpp"
|
|
|
|
|
2018-06-04 21:05:18 +02:00
|
|
|
#include <IrcCommand>
|
2018-02-05 15:11:50 +01:00
|
|
|
#include <cassert>
|
|
|
|
|
2018-06-04 21:05:18 +02:00
|
|
|
// using namespace Communi;
|
2018-02-05 15:11:50 +01:00
|
|
|
using namespace chatterino::singletons;
|
|
|
|
|
|
|
|
namespace chatterino {
|
|
|
|
namespace providers {
|
|
|
|
namespace twitch {
|
2018-03-25 11:37:57 +02:00
|
|
|
|
2018-02-05 15:11:50 +01:00
|
|
|
TwitchServer::TwitchServer()
|
2018-04-18 09:12:29 +02:00
|
|
|
: whispersChannel(new Channel("/whispers", Channel::TwitchWhispers))
|
|
|
|
, mentionsChannel(new Channel("/mentions", Channel::TwitchMentions))
|
2018-04-20 19:54:45 +02:00
|
|
|
, watchingChannel(Channel::getEmpty(), Channel::TwitchWatching)
|
2018-02-05 15:11:50 +01:00
|
|
|
{
|
2018-04-26 18:10:26 +02:00
|
|
|
qDebug() << "init TwitchServer";
|
2018-02-05 15:11:50 +01:00
|
|
|
}
|
|
|
|
|
2018-04-28 15:20:18 +02:00
|
|
|
void TwitchServer::initialize()
|
2018-02-05 15:11:50 +01:00
|
|
|
{
|
2018-05-26 20:26:25 +02:00
|
|
|
getApp()->accounts->twitch.currentUserChanged.connect(
|
2018-04-28 15:20:18 +02:00
|
|
|
[this]() { util::postToThread([this] { this->connect(); }); });
|
2018-02-05 15:11:50 +01:00
|
|
|
}
|
|
|
|
|
2018-06-04 21:05:18 +02:00
|
|
|
void TwitchServer::initializeConnection(providers::irc::IrcConnection *connection, bool isRead,
|
|
|
|
bool isWrite)
|
2018-02-05 15:11:50 +01:00
|
|
|
{
|
2018-05-26 20:26:25 +02:00
|
|
|
std::shared_ptr<TwitchAccount> account = getApp()->accounts->twitch.getCurrent();
|
2018-02-05 15:11:50 +01:00
|
|
|
|
2018-04-26 18:10:26 +02:00
|
|
|
qDebug() << "logging in as" << account->getUserName();
|
|
|
|
|
2018-02-05 15:11:50 +01:00
|
|
|
QString username = account->getUserName();
|
|
|
|
// QString oauthClient = account->getOAuthClient();
|
|
|
|
QString oauthToken = account->getOAuthToken();
|
|
|
|
|
|
|
|
if (!oauthToken.startsWith("oauth:")) {
|
|
|
|
oauthToken.prepend("oauth:");
|
|
|
|
}
|
|
|
|
|
|
|
|
connection->setUserName(username);
|
|
|
|
connection->setNickName(username);
|
|
|
|
connection->setRealName(username);
|
|
|
|
|
|
|
|
if (!account->isAnon()) {
|
|
|
|
connection->setPassword(oauthToken);
|
|
|
|
|
|
|
|
// fourtf: ignored users
|
|
|
|
// this->refreshIgnoredUsers(username, oauthClient, oauthToken);
|
|
|
|
}
|
|
|
|
|
2018-06-04 21:05:18 +02:00
|
|
|
connection->sendCommand(Communi::IrcCommand::createCapability("REQ", "twitch.tv/membership"));
|
|
|
|
connection->sendCommand(Communi::IrcCommand::createCapability("REQ", "twitch.tv/commands"));
|
|
|
|
connection->sendCommand(Communi::IrcCommand::createCapability("REQ", "twitch.tv/tags"));
|
2018-02-05 15:11:50 +01:00
|
|
|
|
|
|
|
connection->setHost("irc.chat.twitch.tv");
|
|
|
|
connection->setPort(6667);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::shared_ptr<Channel> TwitchServer::createChannel(const QString &channelName)
|
|
|
|
{
|
|
|
|
TwitchChannel *channel = new TwitchChannel(channelName, this->getReadConnection());
|
|
|
|
|
|
|
|
channel->sendMessageSignal.connect(
|
|
|
|
[this](auto chan, auto msg) { this->sendMessage(chan, msg); });
|
|
|
|
|
|
|
|
return std::shared_ptr<Channel>(channel);
|
|
|
|
}
|
|
|
|
|
2018-06-04 21:05:18 +02:00
|
|
|
void TwitchServer::privateMessageReceived(Communi::IrcPrivateMessage *message)
|
2018-02-05 15:11:50 +01:00
|
|
|
{
|
2018-06-04 12:23:23 +02:00
|
|
|
IrcMessageHandler::getInstance().handlePrivMessage(message, *this);
|
2018-02-05 15:11:50 +01:00
|
|
|
}
|
|
|
|
|
2018-06-04 21:05:18 +02:00
|
|
|
void TwitchServer::messageReceived(Communi::IrcMessage *message)
|
2018-02-05 15:11:50 +01:00
|
|
|
{
|
|
|
|
// this->readConnection
|
2018-06-04 21:05:18 +02:00
|
|
|
if (message->type() == Communi::IrcMessage::Type::Private) {
|
2018-02-05 15:11:50 +01:00
|
|
|
// We already have a handler for private messages
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const QString &command = message->command();
|
|
|
|
|
2018-05-26 18:06:55 +02:00
|
|
|
auto &handler = IrcMessageHandler::getInstance();
|
|
|
|
|
2018-02-05 15:11:50 +01:00
|
|
|
if (command == "ROOMSTATE") {
|
2018-05-26 18:06:55 +02:00
|
|
|
handler.handleRoomStateMessage(message);
|
2018-02-05 15:11:50 +01:00
|
|
|
} else if (command == "CLEARCHAT") {
|
2018-05-26 18:06:55 +02:00
|
|
|
handler.handleClearChatMessage(message);
|
2018-02-05 15:11:50 +01:00
|
|
|
} else if (command == "USERSTATE") {
|
2018-05-26 18:06:55 +02:00
|
|
|
handler.handleUserStateMessage(message);
|
2018-02-05 15:11:50 +01:00
|
|
|
} else if (command == "WHISPER") {
|
2018-05-26 18:06:55 +02:00
|
|
|
handler.handleWhisperMessage(message);
|
2018-02-05 15:11:50 +01:00
|
|
|
} else if (command == "USERNOTICE") {
|
2018-06-04 12:23:23 +02:00
|
|
|
handler.handleUserNoticeMessage(message, *this);
|
2018-02-05 15:11:50 +01:00
|
|
|
} else if (command == "MODE") {
|
2018-05-26 18:06:55 +02:00
|
|
|
handler.handleModeMessage(message);
|
2018-02-05 15:11:50 +01:00
|
|
|
} else if (command == "NOTICE") {
|
2018-06-04 21:05:18 +02:00
|
|
|
handler.handleNoticeMessage(static_cast<Communi::IrcNoticeMessage *>(message));
|
2018-05-26 18:06:55 +02:00
|
|
|
} else if (command == "JOIN") {
|
|
|
|
handler.handleJoinMessage(message);
|
|
|
|
} else if (command == "PART") {
|
|
|
|
handler.handlePartMessage(message);
|
2018-02-05 15:11:50 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-04 21:05:18 +02:00
|
|
|
void TwitchServer::writeConnectionMessageReceived(Communi::IrcMessage *message)
|
2018-02-05 15:11:50 +01:00
|
|
|
{
|
|
|
|
switch (message->type()) {
|
2018-06-04 21:05:18 +02:00
|
|
|
case Communi::IrcMessage::Type::Notice: {
|
2018-02-05 15:11:50 +01:00
|
|
|
IrcMessageHandler::getInstance().handleWriteConnectionNoticeMessage(
|
2018-06-04 21:05:18 +02:00
|
|
|
static_cast<Communi::IrcNoticeMessage *>(message));
|
2018-02-05 15:11:50 +01:00
|
|
|
} break;
|
2018-06-04 21:05:18 +02:00
|
|
|
|
|
|
|
default:;
|
2018-02-05 15:11:50 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::shared_ptr<Channel> TwitchServer::getCustomChannel(const QString &channelName)
|
|
|
|
{
|
|
|
|
if (channelName == "/whispers") {
|
|
|
|
return whispersChannel;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (channelName == "/mentions") {
|
|
|
|
return mentionsChannel;
|
|
|
|
}
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
2018-02-05 21:20:38 +01:00
|
|
|
|
|
|
|
void TwitchServer::forEachChannelAndSpecialChannels(std::function<void(ChannelPtr)> func)
|
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> lock(this->channelMutex);
|
|
|
|
|
|
|
|
for (std::weak_ptr<Channel> &weak : this->channels) {
|
|
|
|
std::shared_ptr<Channel> chan = weak.lock();
|
|
|
|
if (!chan) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
func(chan);
|
|
|
|
}
|
|
|
|
|
|
|
|
func(this->whispersChannel);
|
|
|
|
func(this->mentionsChannel);
|
|
|
|
}
|
2018-03-25 11:37:57 +02:00
|
|
|
|
2018-04-22 15:36:01 +02:00
|
|
|
std::shared_ptr<Channel> TwitchServer::getChannelOrEmptyByID(const QString &channelID)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> lock(this->channelMutex);
|
|
|
|
|
|
|
|
for (const auto &weakChannel : this->channels) {
|
|
|
|
auto channel = weakChannel.lock();
|
|
|
|
if (!channel) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto twitchChannel = std::dynamic_pointer_cast<TwitchChannel>(channel);
|
|
|
|
if (!twitchChannel) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (twitchChannel->roomID == channelID) {
|
|
|
|
return twitchChannel;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return Channel::getEmpty();
|
|
|
|
}
|
|
|
|
|
2018-04-21 00:40:17 +02:00
|
|
|
QString TwitchServer::cleanChannelName(const QString &dirtyChannelName)
|
2018-04-01 15:10:15 +02:00
|
|
|
{
|
|
|
|
return dirtyChannelName.toLower();
|
|
|
|
}
|
|
|
|
|
2018-02-05 15:11:50 +01:00
|
|
|
} // namespace twitch
|
|
|
|
} // namespace providers
|
|
|
|
} // namespace chatterino
|