-
Notifications
You must be signed in to change notification settings - Fork 0
/
splinter.cpp
181 lines (164 loc) · 6.74 KB
/
splinter.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
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
#include "src/libsplinterIRC/client/client.h"
#define VERSION "0.1a"
#include <iostream>
#include <string>
#include <getopt.h>
void show_usage(const std::string& program_name) {
std::cout << "Usage: " << program_name << " -s|--server SERVER -p|--port PORT -n|--nick NICK -w||--password PASSWORD [ -e|--ssl [ -f|--strict-validation ] ][ -S|--use-sasl -u|--sasl-username SASL_USERNAME -k|--sasl-password SASL_PASSWORD ]" << std::endl
<< std::endl
<< "Required options:" << std::endl
<< " -s, --server=ADDR Server address" << std::endl
<< " -p, --port=PORT Server port" << std::endl
<< " -n, --nick=NICK Nickname" << std::endl
<< " -w, --password=PASS Password" << std::endl
<< std::endl
<< "Optional options:" << std::endl
<< " -u, --sasl-username=USER SASL username" << std::endl
<< " -k, --sasl-password=PASS SASL password" << std::endl
<< " -S, --use-sasl Use SASL authentication" << std::endl
<< " -e, --ssl Use SSL" << std::endl
<< " -f, --strict-validation Strict SSL validation" << std::endl
<< " -v, --verbose Verbose output" << std::endl
<< " -V, --version Show version information" << std::endl
<< " -h, --help Show this help screen" << std::endl << std::endl;
}
int main(int argc, char *argv[])
{
std::string master_server;
std::string master_port;
std::string master_nick;
std::string master_password;
std::string sasl_username;
std::string sasl_password;
bool use_sasl = false;
bool use_ssl = false;
bool strict_validation = false;
bool verbose = false;
const char* const short_opts = "s:p:n:w:u:k:SefvVh";
const option long_opts[] = {
{"server", required_argument, nullptr, 's'},
{"port", required_argument, nullptr, 'p'},
{"nick", required_argument, nullptr, 'n'},
{"password", required_argument, nullptr, 'w'},
{"sasl-username", required_argument, nullptr, 'u'},
{"sasl-password", required_argument, nullptr, 'k'},
{"use-sasl", no_argument, nullptr, 'S'},
{"ssl", no_argument, nullptr, 'e'},
{"strict-validation", no_argument, nullptr, 'f'},
{"verbose", no_argument, nullptr, 'v'},
{"version", no_argument, nullptr, 'V'},
{"help", no_argument, nullptr, 'h'},
{nullptr, no_argument, nullptr, 0}
};
while (true)
{
const auto opt = getopt_long(argc, argv, short_opts, long_opts, nullptr);
if (-1 == opt)
break;
switch (opt)
{
case 's':
master_server = optarg;
break;
case 'p':
master_port = optarg;
break;
case 'n':
master_nick = optarg;
break;
case 'w':
master_password = optarg;
break;
case 'u':
sasl_username = optarg;
break;
case 'k':
sasl_password = optarg;
break;
case 'S':
use_sasl = true;
break;
case 'e':
use_ssl = true;
break;
case 'f':
strict_validation = true;
break;
case 'v':
verbose = true;
break;
case 'V':
std::cout << argv[0] << ": v" << VERSION << std::endl;
return 0;
case 'h': // -h or --help
case '?': // Unrecognized option
default:
show_usage(argv[0]);
return 0;
}
}
if (master_server.empty() || master_port.empty() || master_nick.empty() || master_password.empty()) {
std::cerr << "Error: missing required arguments" << std::endl;
show_usage(argv[0]);
return 1;
}
if (use_sasl && (sasl_username.empty() || sasl_password.empty())) {
std::cerr << "Error: missing required SASL arguments" << std::endl;
show_usage(argv[0]);
return 1;
}
// validate server address
if (master_server.empty()) {
std::cerr << "Error: server address cannot be empty" << std::endl;
return 1;
}
if (verbose) {
std::cout << "Server:\t\t" << master_server << std::endl;
std::cout << "Port:\t\t" << master_port << std::endl;
std::cout << "Nick:\t\t" << master_nick << std::endl;
std::cout << "Password:\t" << master_password << std::endl;
std::cout << "Verbose:\t" << (verbose ? "YES" : "NO") << std::endl;
std::cout << std::endl;
std::cout << "Use SASL:\t" << (use_sasl ? "YES" : "NO") << std::endl;
std::cout << "SASL Username:\t" << sasl_username << std::endl;
std::cout << "SASL Password:\t" << sasl_password << std::endl << std::endl;
std::cout << std::endl;
std::cout << "Use SSL:\t" << (use_ssl ? "YES" : "NO") << std::endl;
std::cout << "Strict Validation:\t" << (strict_validation ? "YES" : "NO") << std::endl << std::endl;
}
// validate port number
try {
int port = std::stoi(master_port);
if (port < 1 || port > 65535) {
throw std::out_of_range("Invalid port number");
}
} catch (const std::exception& e) {
std::cerr << "Error: invalid port number (" << e.what() << ")" << std::endl;
return 1;
}
// validate nickname
if (master_nick.empty()) {
std::cerr << "Error: nickname cannot be empty" << std::endl;
return 1;
}
// connect master splinter to the control server
try {
auto master = std::make_shared<splinterClient>(master_server,
master_port,
master_nick,
master_password,
use_sasl,
sasl_username,
sasl_password,
use_ssl,
strict_validation,
verbose);
master->connect();
master->add_to_clients();
master->run_event_loop();
} catch (const std::exception& e) {
std::cerr << "Error: failed to connect to server (" << e.what() << ")\n";
return 1;
}
return 0;
}