-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdefs.cpp
More file actions
executable file
·121 lines (96 loc) · 2.78 KB
/
defs.cpp
File metadata and controls
executable file
·121 lines (96 loc) · 2.78 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
#include "defs.hpp"
#include <iostream>
#include <vector>
AbsSocket::AbsSocket() {
//Setting nick, server, password, port and channel
std::cout << "Input password: ";
std::getline(std::cin, password);
std::cout << "Input your nick (default: env_name): ";
std::getline(std::cin, nick);
nick = (nick.empty() ? getenv("USER") : nick);
std::cout << "Input your host (default: irc.freenode.net): ";
std::getline(std::cin, host);
host = (host.empty() ? "irc.freenode.net" : host );
std::cout << "Input port (default: 6667): ";
std::getline(std::cin, port);
port = (port.empty() ? "6667" : port);
channel = "";
const int status = getaddrinfo(host.c_str(), port.c_str(), NULL, &res);
if ( status != 0 ) {
std::cerr << "Error getting address information: " << gai_strerror(status) << '\n';
return;
}
file_desc = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
if ( file_desc == -1 ) {
std::cerr << "Error getting socket.\n";
return;
}
}
AbsSocket::~AbsSocket() {
freeaddrinfo(res);
shutdown(file_desc, 2);
}
bool AbsSocket::to_connect() {
if ( connect(file_desc, res->ai_addr, res->ai_addrlen) == -1 ) {
std::cerr << "Error connecting!\n";
return false;
}
//Init (Nick and Password)
to_send(password.empty() ? "" : ("PASS " + password));
to_send("NICK " + nick);
to_send("USER " + nick + " localhost " + host + " :" + nick);
return true;
}
bool AbsSocket::to_send(const std::string &phrase) {
if ( send(file_desc, (phrase + "\r\n").c_str(), phrase.length() + 2, 0) == -1 ) {
std::cerr << "Error sending!\n";
return false;
}
return true;
}
std::string AbsSocket::to_receive() {
std::vector<char> rec_buf (1024);
int bytes_received = recv(file_desc, rec_buf.data(), 1024, 0);
switch ( bytes_received ) {
case 0:
std::cerr << "Server closed connection.\n";
return "CLOSED";
case -1:
std::cerr << "Error receiving.\n";
return "ERROR";
default:
rec_buf.push_back('\0');
}
std::string resp (rec_buf.begin(), rec_buf.end());
if ( resp.substr(0,4) == "PING" ) { to_send("PONG :" + resp.substr(7));} // So we're not automatically removed
return resp;
}
void AbsSocket::parse_and_send(std::string &phrase) {
if ( phrase[0] == '/' ) {
switch( phrase[1] ) {
case 'j':
to_send("PART " + channel); //Exit old channel
channel = phrase.substr(3);
to_send("JOIN " + channel);
break;
case 'q':
to_send("QUIT");
break;
case 'r':
to_send(phrase.substr(3));
break;
case 's':
{
std::string other_person = phrase.substr(3, phrase.find_first_of(' ', 3));
std::string message = phrase.substr(phrase.find_first_of(' ', 3) + 1);
to_send("PRIVMSG " + other_person + " :" + message);
break;
}
default:
std::cout << "Command not found!\n";
break;
}
return;
}
to_send("PRIVMSG "+ channel + " :" + phrase);
}