remwharead/browser-plugins/webextension/native-wrapper/remwharead_wrapper.cpp

161 lines
3.6 KiB
C++
Raw Normal View History

/* This file is part of remwharead.
2020-01-28 02:42:49 +01:00
* Copyright © 2019, 2020 tastytea <tastytea@tastytea.de>
*
* This program 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, version 3.
*
* This program 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.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
2020-01-28 02:42:49 +01:00
#include <Poco/Message.h>
2020-10-31 13:30:04 +01:00
#include <sys/wait.h>
#include <cstdint>
#include <cstdlib>
#include <cstring>
2020-10-31 13:30:04 +01:00
#include <iostream>
#include <string>
using std::cin;
using std::cout;
2020-10-31 13:30:04 +01:00
using std::string;
using std::system;
2020-10-31 13:30:04 +01:00
using std::uint32_t;
2020-01-28 02:42:49 +01:00
class Message
{
public:
//! Read message from stdin and store it in _msg.
Message();
//! Decode message and return argument string for launch().
2020-10-31 13:30:04 +01:00
[[nodiscard]] string decode();
2020-01-28 02:42:49 +01:00
private:
string _msg;
//! Replace "\u001f" with the char in _msg.
void unescape();
//! Replace " with \" in field.
static void replace_in_field(string &field);
};
//! Send a message back.
void send_message(const string &message);
2020-01-28 02:42:49 +01:00
//! Launch remwharead with args.
int launch(const string &args);
2020-01-28 02:42:49 +01:00
int main()
{
Message message;
const auto args{message.decode()};
const int ret = launch(args);
if (ret == 0)
{
send_message("Command successful.");
}
else
{
2020-10-31 13:30:04 +01:00
send_message("Command failed with status: " + std::to_string(ret)
+ '.');
2020-01-28 02:42:49 +01:00
}
return ret;
}
Message::Message()
{
// Read message length.
uint32_t length{0};
char buffer[4];
2020-01-28 02:42:49 +01:00
cin.read(buffer, sizeof(length));
std::memcpy(&length, buffer, sizeof(length));
// Ignore quotes.
length -= 2;
cin.ignore(1);
// Read message.
char c{'\0'};
for (; length > 0; --length)
{
cin.read(&c, 1);
2020-01-28 02:42:49 +01:00
_msg += c;
}
}
2020-01-28 02:42:49 +01:00
string Message::decode()
{
2020-01-28 02:42:49 +01:00
unescape();
constexpr char separator{'\u001f'}; // UNIT SEPARATOR.
2020-01-28 02:42:49 +01:00
if (_msg[0] != separator) // Extension uses old method.
{
2020-01-28 02:42:49 +01:00
return _msg;
}
size_t pos{1};
size_t endpos{0};
string newargs;
2020-01-28 02:42:49 +01:00
while ((endpos = _msg.find(separator, pos)) != string::npos)
{
2020-01-28 02:42:49 +01:00
string field{_msg.substr(pos, endpos - pos)};
replace_in_field(field);
newargs += " \"" + field + '"';
pos = endpos + 1;
}
return newargs;
}
2020-01-28 02:42:49 +01:00
void Message::unescape()
{
size_t pos{0};
while ((pos = _msg.find(R"(\u001f)", pos)) != string::npos)
{
_msg.replace(pos, 6, "\u001f");
}
}
void Message::replace_in_field(string &field)
{
size_t pos{0};
while ((pos = field.find('"', pos)) != string::npos)
{
2020-01-28 02:42:49 +01:00
field.replace(pos, 1, R"(\")");
pos += 2;
}
}
2020-01-28 02:42:49 +01:00
void send_message(const string &message)
{
2020-01-28 02:42:49 +01:00
const auto length = static_cast<uint32_t>(message.length() + 2);
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
2020-10-31 13:30:04 +01:00
cout.write(reinterpret_cast<const char *>(&length), sizeof(uint32_t));
2020-01-28 02:42:49 +01:00
cout << '"' << message << '"';
}
2020-01-28 02:42:49 +01:00
int launch(const string &args)
{
const string cmd = "remwharead " + args + " 2>/dev/null";
// NOLINTNEXTLINE(cert-env33-c)
int ret = system(cmd.c_str());
2020-10-31 13:30:04 +01:00
if (WIFEXITED(ret)) // NOLINT(hicpp-signed-bitwise)
{
2020-01-28 02:42:49 +01:00
ret = WEXITSTATUS(ret); // NOLINT(hicpp-signed-bitwise)
}
return ret;
}