Adjusted workflow.
This commit is contained in:
177
src/io/socket/rest/TwitchChat.cpp
Normal file
177
src/io/socket/rest/TwitchChat.cpp
Normal file
@@ -0,0 +1,177 @@
|
||||
#include "ehs/io/socket/rest/TwitchChat.h"
|
||||
#include "ehs/io/socket/DNS.h"
|
||||
#include "ehs/io/Console.h"
|
||||
|
||||
namespace ehs
|
||||
{
|
||||
TwitchChat::~TwitchChat()
|
||||
{
|
||||
UnInitialize();
|
||||
}
|
||||
|
||||
TwitchChat::TwitchChat()
|
||||
: initialized(false)
|
||||
{
|
||||
}
|
||||
|
||||
TwitchChat::TwitchChat(const Str_8& username)
|
||||
: username(username), initialized(false)
|
||||
{
|
||||
}
|
||||
|
||||
TwitchChat::TwitchChat(const Str_8& username, const Str_8& token)
|
||||
: username(username), token(token), initialized(false)
|
||||
{
|
||||
}
|
||||
|
||||
TwitchChat::TwitchChat(const TwitchChat& chat)
|
||||
: username(chat.username), token(chat.token), initialized(false)
|
||||
{
|
||||
}
|
||||
|
||||
TwitchChat& TwitchChat::operator=(const TwitchChat& chat)
|
||||
{
|
||||
if (this == &chat)
|
||||
return *this;
|
||||
|
||||
client = TCP();
|
||||
username = chat.username;
|
||||
token = chat.token;
|
||||
channel = Str_8();
|
||||
initialized = false;
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
void TwitchChat::SetToken(const Str_8& newToken)
|
||||
{
|
||||
token = newToken;
|
||||
}
|
||||
|
||||
void TwitchChat::Initialize()
|
||||
{
|
||||
if (initialized)
|
||||
return;
|
||||
|
||||
client = TCP(ehs::AddrType::IPV4);
|
||||
client.Connect(DNS::Resolve("irc.chat.twitch.tv"), 6667);
|
||||
client.SetBlocking(false);
|
||||
|
||||
Str_8 r("PASS oauth:" + token + "\r\n");
|
||||
|
||||
Console::Write_8("< " + r, false);
|
||||
|
||||
client.Send(r.ToBytes(), (int) r.Size());
|
||||
|
||||
r = "NICK " + username + "\r\n";
|
||||
|
||||
Console::Write_8("< " + r, false);
|
||||
|
||||
client.Send(r.ToBytes(), (int) r.Size());
|
||||
|
||||
initialized = true;
|
||||
}
|
||||
|
||||
void TwitchChat::UnInitialize()
|
||||
{
|
||||
if (!initialized)
|
||||
return;
|
||||
|
||||
client.Release();
|
||||
|
||||
initialized = false;
|
||||
}
|
||||
|
||||
void TwitchChat::JoinChannel(const Str_8& newChannel)
|
||||
{
|
||||
if (!initialized || channel == newChannel)
|
||||
return;
|
||||
|
||||
channel = newChannel;
|
||||
|
||||
Str_8 r("Join #" + newChannel + "\r\n");
|
||||
|
||||
Console::Write_8("< " + r, false);
|
||||
|
||||
client.Send(r.ToBytes(), (int) r.Size());
|
||||
}
|
||||
|
||||
void TwitchChat::LeaveChannel()
|
||||
{
|
||||
if (!initialized)
|
||||
return;
|
||||
|
||||
Str_8 r("PART #" + channel + "\r\n");
|
||||
|
||||
Console::Write_8("< " + r, false);
|
||||
|
||||
client.Send(r.ToBytes(), (int) r.Size());
|
||||
}
|
||||
|
||||
void TwitchChat::SendPong()
|
||||
{
|
||||
if (!initialized)
|
||||
return;
|
||||
|
||||
Str_8 r("PONG :tmi.twitch.tv\r\n");
|
||||
|
||||
Console::Write_8("< " + r, false);
|
||||
|
||||
client.Send(r.ToBytes(), (int) r.Size());
|
||||
}
|
||||
|
||||
void TwitchChat::SendMsg(const Str_8& msg)
|
||||
{
|
||||
if (!initialized)
|
||||
return;
|
||||
|
||||
Str_8 r("PRIVMSG #" + channel + " :" + msg + "\r\n");
|
||||
|
||||
Console::Write_8("< " + r, false);
|
||||
|
||||
client.Send(r.ToBytes(), (int) r.Size());
|
||||
|
||||
//irc.SendStr(":" + username + "!" + username + "@" + username + ".tmi.twitch.tv PRIVMSG #" + username + " :" + msg + "\r\n");
|
||||
}
|
||||
|
||||
void TwitchChat::WhisperMsg(const Str_8& user, const Str_8& msg)
|
||||
{
|
||||
if (!initialized)
|
||||
return;
|
||||
|
||||
Str_8 r("PRIVMSG #jtv :/w " + user + " " + msg + "\r\n");
|
||||
|
||||
Console::Write_8("< " + r, false);
|
||||
|
||||
client.Send(r.ToBytes(), (int) r.Size());
|
||||
}
|
||||
|
||||
Str_8 TwitchChat::RecvMsg()
|
||||
{
|
||||
Str_8 result;
|
||||
|
||||
Byte received[1024];
|
||||
UInt_64 recvSize = 0;
|
||||
|
||||
do
|
||||
{
|
||||
recvSize = client.Receive(received, 1024);
|
||||
if (recvSize)
|
||||
result.Push((Char_8*) received, recvSize);
|
||||
else
|
||||
break;
|
||||
} while (!result.Find("\r\n", nullptr, SearchPattern::RIGHT_LEFT));
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
Str_8 TwitchChat::GetUsername() const
|
||||
{
|
||||
return username;
|
||||
}
|
||||
|
||||
Str_8 TwitchChat::GetChannel() const
|
||||
{
|
||||
return channel;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user