Repository navigation
Expand file tree
/
Copy pathirc.cc
More file actions
195 lines (173 loc) · 3.6 KB
/
Copy pathirc.cc
File metadata and controls
195 lines (173 loc) · 3.6 KB
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#include "irc.hh"
/*
* irc.cc
* Author: Mark Swoope
* Date: July 2017
*/
void irc::message::clear()
{
origin.nickname.clear();
origin.user.clear();
origin.host.clear();
command.clear();
params.clear();
}
void irc::message::str(const std::string& s)
{
std::stringstream sstream;
std::string prefix, param;
clear();
if (s.empty()) return;
sstream.str(s);
if (sstream.peek() == ':') {
sstream.ignore(1);
sstream >> prefix;
if (prefix.find_first_of("!@") != std::string::npos) {
std::stringstream prefixstream;
prefixstream.str(prefix);
std::getline(prefixstream, origin.nickname, '!');
std::getline(prefixstream, origin.user, '@');
std::getline(prefixstream, origin.host);
} else {
origin.host = prefix;
}
}
sstream >> command;
while (true) {
sstream >> std::ws;
if (sstream.peek() == ':') {
sstream.ignore(1);
std::getline(sstream, param, '\r');
params.push_back(param);
break;
} else {
sstream >> param;
if (!sstream) break;
params.push_back(param);
}
}
}
std::string irc::message::str() const
{
std::string result;
std::size_t i;
if (!origin.nickname.empty()) {
result += ':';
result += origin.nickname;
result += '!';
result += origin.user;
result += '@';
result += origin.host;
result += ' ';
} else if (!origin.host.empty()) {
result += ':';
result += origin.host;
result += ' ';
}
result += command;
result += ' ';
if (!params.empty()) {
for (i = 0; i < params.size() - 1; ++i) {
result += params[i];
result += ' ';
}
result += ':';
result += params[i];
}
return result;
}
irc::client::client() : stream(), param()
{
}
irc::client::client(const param_type& p) : stream(), param()
{
open(p);
}
irc::client::~client()
{
close();
}
const irc::client::param_type* irc::client::rdparam() const
{
return ¶m;
}
swoope::socketstream* irc::client::rdstream()
{
return &stream;
}
bool irc::client::is_open() const
{
return stream.is_open();
}
void irc::client::open(const param_type& p)
{
std::string line;
irc::message msg;
const int reg_complete = 10;
int reg_progress = 0;
if (is_open() == true) return;
stream.clear();
stream.open(p.host, p.port);
if (!stream) return;
stream.rdbuf()->pubsetbuf(0, 1024);
stream.unsetf(std::ios_base::unitbuf);
if (!p.password.empty())
stream << "PASS " << p.password << "\r\n";
stream <<
"NICK " << p.nickname << "\r\n" <<
"USER " << p.nickname << " 0 * :" << p.nickname << "\r\n";
stream.flush();
if (!stream) {
stream.close();
return;
}
while (std::getline(stream, line)) {
msg.str(line);
line.erase(--line.end());
std::cout << line << std::endl;
if (msg.command == "PING") {
stream << "PONG " << msg.params.back() << "\r\n";
stream.flush();
continue;
}
std::size_t number = static_cast<std::size_t>(std::atoi(
msg.command.c_str()));
if (number <= 4) {
reg_progress += number;
if (reg_progress == reg_complete) break;
} else if (number >= 400) {
stream.setstate(std::ios_base::failbit);
break;
}
}
if (!stream) stream.close();
else param = p;
}
bool irc::client::read(irc::message& msg)
{
std::string line;
if (is_open() == false) return false;
std::getline(stream, line);
if (!stream) return false;
msg.str(line);
return true;
}
void irc::client::write(const irc::message& msg)
{
if (is_open() == false) return;
stream << msg.str() << "\r\n" << std::flush;
}
void irc::client::shutdown(std::ios_base::openmode how)
{
if (is_open() == false) return;
stream.shutdown(how);
}
void irc::client::close()
{
if (is_open() == false) return;
stream.close();
}
bool irc::client::operator!() const
{
return !stream;
}