summaryrefslogtreecommitdiff
path: root/bot/src/modules/join.hpp
blob: 16e8b4a498e946f27e1429745f694463d3b5dcb6 (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
#pragma once

#include <pqxx/pqxx>
#include <string>
#include <variant>
#include <vector>

#include "../bundle.hpp"
#include "../commands/command.hpp"
#include "../schemas/channel.hpp"

namespace bot {
  namespace mod {
    class Join : public command::Command {
        std::string get_name() const override { return "join"; }

        std::variant<std::vector<std::string>, std::string> run(
            const InstanceBundle &bundle,
            const command::Request &request) const override {
          if (!bundle.configuration.commands.join_allowed) {
            std::string owner = "";

            if (bundle.configuration.owner.name.has_value()) {
              owner = " " + bundle.localization
                                .get_formatted_line(
                                    request, loc::LineId::MsgOwner,
                                    {*bundle.configuration.owner.name})
                                .value();
            }

            return bundle.localization
                .get_formatted_line(request, loc::LineId::JoinNotAllowed,
                                    {owner})
                .value();
          }

          if (!bundle.configuration.commands.join_allow_from_other_chats &&
              request.channel.get_alias_name() !=
                  bundle.irc_client.get_bot_username()) {
            return bundle.localization
                .get_formatted_line(request, loc::LineId::JoinFromOtherChat,
                                    {bundle.irc_client.get_bot_username()})
                .value();
          }

          pqxx::work work(request.conn);

          pqxx::result channels =
              work.exec("SELECT * FROM channels WHERE alias_id = " +
                        std::to_string(request.user.get_alias_id()));

          if (!channels.empty()) {
            schemas::Channel channel(channels[0]);

            if (channel.get_opted_out_at().has_value()) {
              work.exec("UPDATE channels SET opted_out_at = null WHERE id = " +
                        std::to_string(channel.get_id()));
              work.commit();

              bundle.irc_client.join(channel.get_alias_name());

              return bundle.localization
                  .get_formatted_line(request, loc::LineId::JoinRejoined, {})
                  .value();
            }

            return bundle.localization
                .get_formatted_line(request, loc::LineId::JoinAlreadyIn, {})
                .value();
          }

          work.exec("INSERT INTO channels(alias_id, alias_name) VALUES (" +
                    std::to_string(request.user.get_alias_id()) + ", '" +
                    request.user.get_alias_name() + "')");
          work.commit();

          bundle.irc_client.join(request.user.get_alias_name());
          bundle.irc_client.say(
              request.user.get_alias_name(),
              bundle.localization
                  .get_formatted_line(request, loc::LineId::JoinResponseInChat,
                                      {})
                  .value());

          return bundle.localization
              .get_formatted_line(request, loc::LineId::JoinResponse, {})
              .value();
        }
    };
  }
}