summaryrefslogtreecommitdiff
path: root/telnet_connection.cpp
blob: 2cc723ed6b37267b1c5044d3fb3aa6a876f7c1b8 (plain)
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
#include "telnet_connection.h"
#include "commands.h"

#include <boost/bind.hpp>
#include <boost/algorithm/string.hpp>
#include <boost/algorithm/string/regex.hpp>
#include <boost/format.hpp>

#include <iostream>

telnet::Connection::Connection(boost::asio::io_service& io_service) : socket(io_service) {
}

void telnet::Connection::handle_read(const boost::system::error_code& error, size_t bytes_transferred) {
	if(error) {
		return;
	}

	std::string line;
	std::istream is(&buf);
	std::getline(is, line);
	boost::trim(line);

	if(line == "exit") {
		return;
	}

	std::vector<std::string> args = parse_args(line);
	// no arguments, i.e. empty line
	if(!args.size()) {
		start();
		return;
	}

	std::vector<std::string> r;
	try {
		Commands cmd(socket.get_io_service(), args);
		r = cmd();
	} catch(CommandException& ce) {
		std::string s(ce.what());
		s += '\n';
		boost::asio::write(socket, boost::asio::buffer(s));
		start();
		return;
	}

	for(std::vector<std::string>::iterator it = r.begin(); it != r.end(); it++) {
		boost::asio::write(socket, boost::asio::buffer(*it + "\n"));
	}

	start();
}

std::vector<std::string> telnet::Connection::parse_args(std::string& line) {
	std::string::const_iterator begin = line.begin();
	std::string::const_iterator end = line.end();

	boost::regex re("(\")?((?(1)[^\"]|[^ ])+)(?(1)\")");
	boost::match_results<std::string::const_iterator> what;
	boost::match_flag_type flags = boost::match_default;

	std::vector<std::string> args;

	while(boost::regex_search(begin, end, what, re, flags)) {
		std::string s = std::string(what[2].first, what[2].second);
		boost::algorithm::trim(s);
		// avoid empty strings when parsing multi-word arguments (a "b c")
		if(s.size()) {
			args.push_back(s);
		}
		begin = what[0].second;
	}

	return args;
}

void telnet::Connection::start() {
	boost::asio::async_read_until(socket, buf, "\n", boost::bind(&Connection::handle_read, shared_from_this(),
				boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred));
}