irc_client.cpp 1.54 KB
Newer Older
1
2
#include <irc/irc_client.hpp>
#include <irc/irc_message.hpp>
3
4
5
6
7
8
9
10
11
12
13
14
15
16

#include <iostream>
#include <stdexcept>

IrcClient::IrcClient()
{
  std::cout << "IrcClient()" << std::endl;
}

IrcClient::~IrcClient()
{
  std::cout << "~IrcClient()" << std::endl;
}

louiz’'s avatar
louiz’ committed
17
18
19
20
void IrcClient::on_connected()
{
}

21
22
23
24
25
26
27
void IrcClient::on_connection_close()
{
  std::cout << "Connection closed by remote server." << std::endl;
}

void IrcClient::parse_in_buffer()
{
louiz’'s avatar
louiz’ committed
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
  while (true)
    {
      auto pos = this->in_buf.find("\r\n");
      if (pos == std::string::npos)
        break ;
      IrcMessage message(this->in_buf.substr(0, pos));
      this->in_buf = this->in_buf.substr(pos + 2, std::string::npos);
      std::cout << message << std::endl;
    }
}

void IrcClient::send_message(IrcMessage&& message)
{
  std::string res;
  if (!message.prefix.empty())
    res += ":" + std::move(message.prefix) + " ";
  res += std::move(message.command);
  for (const std::string& arg: message.arguments)
    {
      if (arg.find(" ") != std::string::npos)
        {
          res += " :" + arg;
          break;
        }
      res += " " + arg;
    }
  res += "\r\n";
55
  this->send_data(std::move(res));
louiz’'s avatar
louiz’ committed
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
}

void IrcClient::send_user_command(const std::string& username, const std::string& realname)
{
  this->send_message(IrcMessage("USER", {username, "NONE", "NONE", realname}));
}

void IrcClient::send_nick_command(const std::string& nick)
{
  this->send_message(IrcMessage("NICK", {nick}));
}

void IrcClient::send_join_command(const std::string& chan_name)
{
  this->send_message(IrcMessage("JOIN", {chan_name}));
71
}