2012-09-02 22:11:28 +00:00
|
|
|
/*
|
|
|
|
* ET: Legacy
|
|
|
|
* Copyright (C) 2012 Jan Simek <mail@etlegacy.com>
|
|
|
|
*
|
|
|
|
* This file is part of ET: Legacy - http://www.etlegacy.com
|
|
|
|
*
|
|
|
|
* ET: Legacy is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* ET: Legacy is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
2012-10-13 22:05:40 +00:00
|
|
|
* This file contains code from the Wetsi project created by acqu.
|
|
|
|
* Wetsi source code repository is located at:
|
|
|
|
* http://sourceforge.net/p/wetsi/
|
|
|
|
*
|
2012-09-02 22:11:28 +00:00
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with ET: Legacy. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
#include "etparser.h"
|
|
|
|
|
2012-09-14 11:18:17 +00:00
|
|
|
ETParser::ETParser(std::vector<std::string> packets)
|
2012-09-02 22:11:28 +00:00
|
|
|
{
|
2012-09-14 11:18:17 +00:00
|
|
|
for (int i = 0; i < packets.size(); i++)
|
|
|
|
{
|
|
|
|
ParseResponse(packets[i]);
|
|
|
|
}
|
2012-09-02 22:11:28 +00:00
|
|
|
|
2012-09-14 11:18:17 +00:00
|
|
|
// Just print it all out for now:
|
|
|
|
std::map <std::string, std::string>::iterator it;
|
|
|
|
for (it = response_variables_.begin(); it != response_variables_.end(); ++it)
|
|
|
|
{
|
2012-10-13 23:47:05 +00:00
|
|
|
std::cout << it->first << ": " << it->second << std::endl;
|
2012-09-14 11:18:17 +00:00
|
|
|
}
|
2012-09-02 22:11:28 +00:00
|
|
|
}
|
|
|
|
|
2012-09-14 11:18:17 +00:00
|
|
|
void ETParser::ParseResponse(std::string rsp_to_parse)
|
2012-09-02 22:11:28 +00:00
|
|
|
{
|
2012-09-14 11:18:17 +00:00
|
|
|
set_response_name(rsp_to_parse);
|
2012-09-02 22:11:28 +00:00
|
|
|
|
2012-09-14 11:18:17 +00:00
|
|
|
if (get_variable("response_name") == "statusResponse"
|
|
|
|
|| get_variable("response_name") == "infoResponse")
|
|
|
|
{
|
|
|
|
std::vector<std::string> parts;
|
|
|
|
boost::split(parts, rsp_to_parse, boost::is_any_of("\n"), boost::token_compress_on);
|
|
|
|
|
|
|
|
SplitVariables(parts.at(1));
|
2012-10-13 23:15:31 +00:00
|
|
|
|
|
|
|
// Only relevant for 'statusResponse'
|
|
|
|
// Player information is separated by a newline.
|
2012-09-14 11:18:17 +00:00
|
|
|
if (parts.size() > 1)
|
|
|
|
{
|
|
|
|
for (int i = 2; i < parts.size(); i++)
|
|
|
|
{
|
|
|
|
if (!parts[i].empty())
|
|
|
|
{
|
2012-10-13 23:15:31 +00:00
|
|
|
add_variable("player", parts[i]);
|
2012-09-14 11:18:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (get_variable("response_name") == "getserversResponse")
|
|
|
|
{
|
2012-10-13 22:05:40 +00:00
|
|
|
/*
|
|
|
|
* The following code was adapted from the Wetsi project
|
|
|
|
*/
|
2012-10-13 23:47:05 +00:00
|
|
|
int i = rsp_to_parse.find('\\') + 1; // skip to the first item
|
2012-10-13 22:05:40 +00:00
|
|
|
|
|
|
|
if (i >= rsp_to_parse.npos)
|
|
|
|
{
|
|
|
|
std::cout << "ERROR: nothing to parse" << std::endl;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (i < rsp_to_parse.npos)
|
|
|
|
{
|
|
|
|
// 'EOT' found, abort successfully
|
|
|
|
if (rsp_to_parse[i] == 'E' && rsp_to_parse[i + 1] == 'O' && rsp_to_parse[i + 2] == 'T')
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// should never happen
|
|
|
|
if (i + 6 >= rsp_to_parse.npos)
|
|
|
|
{
|
|
|
|
std::cout << "ERROR: incomplete packet" << std::endl;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// parse out ip
|
2012-10-13 23:47:05 +00:00
|
|
|
std::stringstream server_addr;
|
|
|
|
server_addr << (int)(unsigned char)rsp_to_parse[i++] << "."
|
|
|
|
<< (int)(unsigned char)rsp_to_parse[i++] << "."
|
|
|
|
<< (int)(unsigned char)rsp_to_parse[i++] << "."
|
|
|
|
<< (int)(unsigned char)rsp_to_parse[i++] << ":"
|
|
|
|
<< (unsigned short)((rsp_to_parse[i++] << 8) + rsp_to_parse[i++]);
|
|
|
|
|
|
|
|
add_variable("server", server_addr.str());
|
2012-10-13 22:05:40 +00:00
|
|
|
|
|
|
|
// should never happen
|
|
|
|
if (rsp_to_parse[i++] != '\\')
|
|
|
|
{
|
|
|
|
std::cout << "ERROR: char '\\' not found. Returning." << std::endl;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2012-09-14 11:18:17 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-10-13 22:05:40 +00:00
|
|
|
std::cout << "ERROR: unknown response type" << std::endl;
|
2012-09-14 11:18:17 +00:00
|
|
|
}
|
2012-09-02 22:11:28 +00:00
|
|
|
}
|
|
|
|
|
2012-09-14 11:18:17 +00:00
|
|
|
void ETParser::SplitVariables(std::string rsp_to_split)
|
2012-09-02 22:11:28 +00:00
|
|
|
{
|
|
|
|
// recv_msg.erase(recv_msg.find('\0'), recv_msg.npos);
|
|
|
|
|
|
|
|
std::string key, value;
|
2012-09-14 11:18:17 +00:00
|
|
|
size_t tokenStart = 0, tokenEnd = 0;
|
2012-09-02 22:11:28 +00:00
|
|
|
|
|
|
|
for (;; )
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Search for a key
|
|
|
|
*/
|
2012-09-14 11:18:17 +00:00
|
|
|
tokenStart = rsp_to_split.find('\\', tokenEnd++);
|
|
|
|
tokenEnd = rsp_to_split.find('\\', ++tokenStart);
|
2012-09-02 22:11:28 +00:00
|
|
|
|
|
|
|
// No more keys
|
|
|
|
if (tokenStart == std::string::npos)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Key without a value
|
|
|
|
if (tokenEnd == std::string::npos)
|
|
|
|
{
|
2012-09-14 11:18:17 +00:00
|
|
|
key = rsp_to_split.substr(tokenStart,
|
|
|
|
rsp_to_split.length() - tokenStart);
|
2012-10-13 23:15:31 +00:00
|
|
|
add_variable(key, std::string(""));
|
2012-09-02 22:11:28 +00:00
|
|
|
std::cout << "Warning: adding a key with empty value." << std::endl;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2012-09-14 11:18:17 +00:00
|
|
|
key = rsp_to_split.substr(tokenStart, tokenEnd - tokenStart);
|
2012-09-02 22:11:28 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Search for a value
|
|
|
|
*/
|
2012-09-14 11:18:17 +00:00
|
|
|
tokenStart = rsp_to_split.find('\\', tokenEnd++);
|
|
|
|
tokenEnd = rsp_to_split.find('\\', ++tokenStart);
|
2012-09-02 22:11:28 +00:00
|
|
|
|
|
|
|
// No more values
|
|
|
|
if (tokenStart == std::string::npos)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Value is not at the end
|
|
|
|
if (tokenEnd != std::string::npos)
|
|
|
|
{
|
2012-09-14 11:18:17 +00:00
|
|
|
value = rsp_to_split.substr(tokenStart,
|
|
|
|
tokenEnd - tokenStart);
|
2012-09-02 22:11:28 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Last value
|
2012-09-14 11:18:17 +00:00
|
|
|
value = rsp_to_split.substr(tokenStart,
|
|
|
|
rsp_to_split.length() - tokenStart);
|
2012-09-02 22:11:28 +00:00
|
|
|
}
|
|
|
|
|
2012-10-13 23:15:31 +00:00
|
|
|
add_variable(key, value);
|
2012-09-02 22:11:28 +00:00
|
|
|
|
|
|
|
// FIXME: This should not happen, but it does. Why?
|
2012-09-14 11:18:17 +00:00
|
|
|
if (tokenStart >= rsp_to_split.length() ||
|
|
|
|
tokenEnd >= rsp_to_split.length())
|
2012-09-02 22:11:28 +00:00
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2012-09-14 11:18:17 +00:00
|
|
|
}
|
2012-09-02 22:11:28 +00:00
|
|
|
|
2012-09-14 11:18:17 +00:00
|
|
|
std::string ETParser::get_variable(std::string key)
|
|
|
|
{
|
|
|
|
return response_variables_.find(key)->second;
|
|
|
|
}
|
2012-09-02 22:11:28 +00:00
|
|
|
|
2012-09-14 11:18:17 +00:00
|
|
|
void ETParser::add_variable(std::string key, std::string value)
|
|
|
|
{
|
|
|
|
response_variables_.insert(
|
|
|
|
std::pair<std::string, std::string>(key, value));
|
|
|
|
}
|
|
|
|
|
|
|
|
void ETParser::set_response_name(std::string rsp_header)
|
|
|
|
{
|
2012-10-13 23:15:31 +00:00
|
|
|
// Check if the packet is valid
|
|
|
|
if (rsp_header.substr(0, 4) != std::string(4, 0xff))
|
2012-09-02 22:11:28 +00:00
|
|
|
{
|
2012-10-13 23:15:31 +00:00
|
|
|
std::cout << "WARNING: response contains invalid header"
|
|
|
|
<< std::endl;
|
2012-09-14 11:18:17 +00:00
|
|
|
}
|
2012-10-13 23:15:31 +00:00
|
|
|
else
|
2012-09-14 11:18:17 +00:00
|
|
|
{
|
2012-10-13 23:15:31 +00:00
|
|
|
// Omit OOB (4 x 0xFF) from the packet start
|
|
|
|
rsp_header = rsp_header.substr(4);
|
2012-09-02 22:11:28 +00:00
|
|
|
}
|
2012-10-13 23:15:31 +00:00
|
|
|
|
|
|
|
// getserversResponse is delimited by a backslash
|
|
|
|
// statusResponse and infoResponse are delimited by a newline
|
|
|
|
std::vector<std::string> parts;
|
|
|
|
boost::split(parts, rsp_header, boost::is_any_of("\\ \n"), boost::token_compress_on);
|
|
|
|
|
|
|
|
response_variables_.erase("response_name");
|
|
|
|
add_variable("response_name", parts[0]);
|
2012-09-14 11:18:17 +00:00
|
|
|
}
|