summaryrefslogtreecommitdiff
path: root/src/irc/client.cpp
blob: 2d07070ebca836c03484b03c045e0752306d1425 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include "client.hpp"

#include <ixwebsocket/IXWebSocketMessage.h>
#include <ixwebsocket/IXWebSocketMessageType.h>

#include <algorithm>
#include <iostream>
#include <optional>
#include <string>
#include <vector>

#include "message.hpp"

using namespace RedpilledBot::IRC;

Client::Client(std::string username, std::string password) {
  this->username = username;
  this->password = password;

  this->host = "wss://irc-ws.chat.twitch.tv";
  this->port = "443";

  this->websocket.setUrl(this->host + ":" + this->port);
}

void Client::run() {
  this->websocket.setOnMessageCallback(
      [this](const ix::WebSocketMessagePtr &msg) {
        switch (msg->type) {
          case ix::WebSocketMessageType::Message: {
            std::cout << "Got a message: " << msg->str << std::endl;

            std::vector<std::string> lines = split_text(msg->str, '\n');

            for (std::string &line : lines) {
              line.erase(std::remove_if(line.begin(), line.end(),
                                        [](char c) {
                                          return c == '\n' || c == '\r' ||
                                                 c == '\t';
                                        }),
                         line.end());

              std::optional<MessageType> type = define_message_type(line);

              if (!type.has_value()) {
                break;
              }

              MessageType m_type = type.value();

              if (m_type == MessageType::Privmsg) {
                std::optional<Message<MessageType::Privmsg>> message =
                    parse_message<MessageType::Privmsg>(line);

                if (message.has_value()) {
                  this->onPrivmsg(message.value());
                }
              }
            }

            break;
          }
          case ix::WebSocketMessageType::Open: {
            std::cout << "Connected to Twitch IRC!\n";
            this->authorize();
            break;
          }
          case ix::WebSocketMessageType::Close: {
            std::cout << "Twitch IRC Connection closed!\n";
            break;
          }
          default: {
            break;
          }
        }
      });

  this->websocket.run();
}

void Client::authorize() {
  if (this->username.empty() || this->password.empty()) {
    std::cout << "Bot username and password must be set!\n";
    return;
  }

  std::cout << "Authorizing on Twitch IRC servers...\n";

  this->websocket.send("PASS " + this->password + "\r\n");
  this->websocket.send("NICK " + this->username + "\r\n");
  this->websocket.send("CAP REQ :twitch.tv/membership\r\n");
  this->websocket.send("CAP REQ :twitch.tv/commands\r\n");
  this->websocket.send("CAP REQ :twitch.tv/tags\r\n");
}