-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathbot.cpp
127 lines (106 loc) · 2.4 KB
/
bot.cpp
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
#include "logger.h"
#include "connection.h"
#include "bot.h"
namespace clever_bot {
bot::bot(const std::string& config_file)
{
std::ifstream file(config_file);
std::string key, value;
while (file >> key >> value) {
m_config[key] = value;
}
m_conn.set_write_handler([this]() { this->write_handler(); });
m_conn.set_read_handler([this](const std::string& m) {
this->read_handler(m);
});
m_conn.connect(m_config["SERVER_ADDR"], m_config["SERVER_PORT"]);
if (m_config.find("BOT_NICK") != m_config.end()) {
nick(m_config["BOT_NICK"]);
}
if (m_config.find("CHAN_LIST") != m_config.end()) {
join(m_config["CHAN_LIST"]);
}
}
void bot::write_handler()
{
std::string line, comm, msg, chann;
while (m_conn.alive()) {
std::getline(std::cin, line);
std::istringstream iss(line);
iss >> comm;
if (comm == "/n") {
iss >> comm;
nick(comm);
}
else if (comm == "/j") {
iss >> comm;
join(comm);
}
else if (comm == "/m") {
iss >> comm;
msg = iss.str();
msg.erase(0,4+comm.size());
message(comm, msg);
}
else if (comm == "/q") {
iss >> comm;
quit(comm);
}
else if (comm == "/op") {
iss >> comm >> chann;
op(comm, chann);
}
}
}
void bot::read_handler(const std::string& message)
{
for (auto func: m_read_handlers) {
func(message);
}
}
void bot::add_read_handler(std::function<void (const std::string&)> func)
{
m_read_handlers.push_back(func);
}
void bot::loop()
{
m_conn.run();
}
void bot::nick(const std::string& nck)
{
m_conn.write(std::string("NICK ") + nck);
m_conn.write(std::string("USER ") + nck + " * * :" + nck);
}
void bot::join(const std::string& chann, std::string key)
{
m_conn.write(std::string("JOIN ") + chann + " " + key);
}
void bot::op(const std::string& nck, const std::string& chann)
{
m_conn.write(std::string("MODE ") + chann + " +o " + nck);
}
void bot::pong(const std::string& to)
{
m_conn.write(std::string("PONG ") + to);
}
void bot::message(const std::string& receiver, const std::string& message)
{
m_conn.write(std::string("PRIVMSG ") + receiver + " :" + message);
}
void bot::quit(const std::string& message)
{
m_conn.write(std::string("QUIT :") + message);
m_conn.close();
}
bool bot::rightPass(const std::string& pass)
{
if(pass == m_config["PASSWD"])
return true;
else
return false;
}
} // ns clever_bot