mirror-chatterino2/src/providers/twitch/twitchserver.cpp

227 lines
6.6 KiB
C++
Raw Normal View History

2018-02-05 15:11:50 +01:00
#include "twitchserver.hpp"
#include "application.hpp"
#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"
#include "providers/twitch/twitchhelpers.hpp"
2018-02-05 15:11:50 +01:00
#include "providers/twitch/twitchmessagebuilder.hpp"
#include "util/posttothread.hpp"
#include <cassert>
using namespace Communi;
using namespace chatterino::singletons;
namespace chatterino {
namespace providers {
namespace twitch {
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
{
qDebug() << "init TwitchServer";
2018-02-05 15:11:50 +01: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(
[this]() { util::postToThread([this] { this->connect(); }); });
2018-02-05 15:11:50 +01:00
}
void TwitchServer::initializeConnection(IrcConnection *connection, bool isRead, bool isWrite)
{
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
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);
}
connection->sendCommand(IrcCommand::createCapability("REQ", "twitch.tv/membership"));
connection->sendCommand(IrcCommand::createCapability("REQ", "twitch.tv/commands"));
connection->sendCommand(IrcCommand::createCapability("REQ", "twitch.tv/tags"));
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);
}
void TwitchServer::privateMessageReceived(IrcPrivateMessage *message)
{
QString channelName;
2018-05-24 08:58:34 +02:00
if (!trimChannelName(message->target(), channelName)) {
return;
}
2018-02-05 15:11:50 +01:00
this->onPrivateMessage.invoke(message);
auto chan = this->getChannelOrEmpty(channelName);
2018-02-05 15:11:50 +01:00
if (chan->isEmpty()) {
2018-02-05 15:11:50 +01:00
return;
}
messages::MessageParseArgs args;
TwitchMessageBuilder builder(chan.get(), message, args);
if (!builder.isIgnored()) {
2018-05-23 22:27:29 +02:00
messages::MessagePtr msg = builder.build();
if (msg->flags & messages::Message::Highlighted) {
this->mentionsChannel->addMessage(msg);
getApp()->highlights->addHighlight(msg);
2018-02-05 15:11:50 +01:00
}
2018-05-23 22:27:29 +02:00
chan->addMessage(msg);
2018-02-05 15:11:50 +01:00
}
}
void TwitchServer::messageReceived(IrcMessage *message)
{
// this->readConnection
if (message->type() == IrcMessage::Type::Private) {
// We already have a handler for private messages
return;
}
const QString &command = message->command();
auto &handler = IrcMessageHandler::getInstance();
2018-02-05 15:11:50 +01:00
if (command == "ROOMSTATE") {
handler.handleRoomStateMessage(message);
2018-02-05 15:11:50 +01:00
} else if (command == "CLEARCHAT") {
handler.handleClearChatMessage(message);
2018-02-05 15:11:50 +01:00
} else if (command == "USERSTATE") {
handler.handleUserStateMessage(message);
2018-02-05 15:11:50 +01:00
} else if (command == "WHISPER") {
handler.handleWhisperMessage(message);
2018-02-05 15:11:50 +01:00
} else if (command == "USERNOTICE") {
handler.handleUserNoticeMessage(message);
2018-02-05 15:11:50 +01:00
} else if (command == "MODE") {
handler.handleModeMessage(message);
2018-02-05 15:11:50 +01:00
} else if (command == "NOTICE") {
handler.handleNoticeMessage(static_cast<IrcNoticeMessage *>(message));
} else if (command == "JOIN") {
handler.handleJoinMessage(message);
} else if (command == "PART") {
handler.handlePartMessage(message);
2018-02-05 15:11:50 +01:00
}
}
void TwitchServer::writeConnectionMessageReceived(IrcMessage *message)
{
switch (message->type()) {
case IrcMessage::Type::Notice: {
IrcMessageHandler::getInstance().handleWriteConnectionNoticeMessage(
static_cast<IrcNoticeMessage *>(message));
} break;
}
}
std::shared_ptr<Channel> TwitchServer::getCustomChannel(const QString &channelName)
{
if (channelName == "/whispers") {
return whispersChannel;
}
if (channelName == "/mentions") {
return mentionsChannel;
}
return nullptr;
}
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);
}
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-05-25 13:53:55 +02:00
// QString TwitchServer::getLastWhisperedPerson() const
//{
// std::lock_guard<std::mutex> guard(this->lastWhisperedPersonMutex);
// return this->lastWhisperedPerson;
//}
// void TwitchServer::setLastWhisperedPerson(const QString &person)
//{
// std::lock_guard<std::mutex> guard(this->lastWhisperedPersonMutex);
// this->lastWhisperedPerson = person;
//}
QString TwitchServer::cleanChannelName(const QString &dirtyChannelName)
{
return dirtyChannelName.toLower();
}
2018-02-05 15:11:50 +01:00
} // namespace twitch
} // namespace providers
} // namespace chatterino