2018-08-30 12:04:56 -04:00
|
|
|
/* This file is part of whyblocked.
|
2019-01-12 11:39:20 -05:00
|
|
|
* Copyright © 2018, 2019 tastytea <tastytea@tastytea.de>
|
2018-08-30 12:04:56 -04:00
|
|
|
*
|
|
|
|
* 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/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
2019-01-12 15:24:09 -05:00
|
|
|
#include <iterator>
|
2018-08-30 12:04:56 -04:00
|
|
|
#include <experimental/filesystem>
|
|
|
|
#include <basedir.h>
|
|
|
|
#include <sqlite/connection.hpp>
|
|
|
|
#include <sqlite/execute.hpp>
|
|
|
|
#include <sqlite/query.hpp>
|
|
|
|
#include "whyblocked.hpp"
|
|
|
|
|
|
|
|
using std::cerr;
|
|
|
|
namespace fs = std::experimental::filesystem;
|
|
|
|
|
2019-01-12 12:18:49 -05:00
|
|
|
Database::data::operator bool() const
|
2018-08-30 12:04:56 -04:00
|
|
|
{
|
2019-01-12 12:18:49 -05:00
|
|
|
return !user.empty();
|
|
|
|
}
|
|
|
|
|
2019-01-12 15:24:09 -05:00
|
|
|
const string Database::get_filepath() const
|
2019-01-12 12:18:49 -05:00
|
|
|
{
|
|
|
|
fs::path filepath;
|
2018-08-30 12:04:56 -04:00
|
|
|
xdgHandle xdg;
|
|
|
|
xdgInitHandle(&xdg);
|
|
|
|
filepath = xdgDataHome(&xdg);
|
|
|
|
xdgWipeHandle(&xdg);
|
|
|
|
|
2019-01-12 12:18:49 -05:00
|
|
|
filepath /= "whyblocked";
|
2018-08-30 12:04:56 -04:00
|
|
|
if (!fs::exists(filepath))
|
|
|
|
{
|
2018-08-30 17:39:54 -04:00
|
|
|
fs::create_directories(filepath);
|
2018-08-30 12:04:56 -04:00
|
|
|
}
|
2019-01-12 12:18:49 -05:00
|
|
|
filepath /= "database.sqlite";
|
2018-08-30 12:04:56 -04:00
|
|
|
if (!fs::exists(filepath))
|
|
|
|
{
|
|
|
|
sqlite::connection con(filepath);
|
|
|
|
sqlite::execute(con, "CREATE TABLE blocks(user TEXT PRIMARY KEY, "
|
|
|
|
"blocked INTEGER, reason TEXT);", true);
|
|
|
|
sqlite::execute(con, "CREATE TABLE urls(user TEXT, url TEXT);", true);
|
|
|
|
}
|
|
|
|
|
|
|
|
return filepath;
|
|
|
|
}
|
|
|
|
|
2019-01-12 15:24:09 -05:00
|
|
|
bool Database::add_user(const Database::data &userdata)
|
2018-08-30 12:04:56 -04:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2019-01-12 13:35:24 -05:00
|
|
|
int blocked_int = 0;
|
2019-01-12 15:24:09 -05:00
|
|
|
if (userdata.blocked)
|
2019-01-12 13:35:24 -05:00
|
|
|
{
|
|
|
|
blocked_int = 1;
|
|
|
|
}
|
2018-08-30 12:04:56 -04:00
|
|
|
sqlite::connection con(get_filepath());
|
2019-01-12 15:24:09 -05:00
|
|
|
{
|
|
|
|
sqlite::execute ins(con, "INSERT INTO blocks VALUES(?, ?, ?);");
|
|
|
|
ins % userdata.user % blocked_int % userdata.reason;
|
|
|
|
ins();
|
|
|
|
}
|
|
|
|
{
|
|
|
|
for (const string &receipt : userdata.receipts)
|
|
|
|
{
|
|
|
|
sqlite::execute ins(con, "INSERT INTO urls VALUES(?, ?);");
|
|
|
|
ins % userdata.user % receipt;
|
|
|
|
ins();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_data.push_back(userdata);
|
2018-08-30 12:04:56 -04:00
|
|
|
}
|
|
|
|
catch (const std::exception &e)
|
|
|
|
{
|
|
|
|
cerr << "An error occurred: " << e.what() << std::endl;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-01-12 12:18:49 -05:00
|
|
|
bool Database::remove(const string &user)
|
2018-08-30 12:04:56 -04:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
sqlite::connection con(get_filepath());
|
|
|
|
sqlite::execute rm_blocks(con, "DELETE FROM blocks WHERE user = ?;");
|
|
|
|
sqlite::execute rm_urls(con, "DELETE FROM urls WHERE user = ?;");
|
|
|
|
rm_blocks % user;
|
|
|
|
rm_urls % user;
|
|
|
|
rm_blocks();
|
|
|
|
rm_urls();
|
2019-01-12 15:24:09 -05:00
|
|
|
reload();
|
2018-08-30 12:04:56 -04:00
|
|
|
}
|
|
|
|
catch (const std::exception &e)
|
|
|
|
{
|
|
|
|
cerr << "An error occurred: " << e.what() << std::endl;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-01-12 15:24:09 -05:00
|
|
|
const vector<Database::data> Database::query(const string &sql_query) const
|
2018-08-30 12:04:56 -04:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
sqlite::connection con(get_filepath());
|
2019-01-12 12:18:49 -05:00
|
|
|
sqlite::query q_blocks(con, sql_query);
|
2018-10-19 23:46:53 -04:00
|
|
|
sqlite::result_type res_blocks = q_blocks.get_result();
|
2019-01-12 12:18:49 -05:00
|
|
|
std::vector<data> result;
|
2018-08-30 12:04:56 -04:00
|
|
|
|
2019-01-12 12:18:49 -05:00
|
|
|
while(res_blocks->next_row())
|
2018-08-30 12:04:56 -04:00
|
|
|
{
|
2019-01-12 12:18:49 -05:00
|
|
|
const string user = res_blocks->get_string(0);
|
|
|
|
const int blocked = res_blocks->get_int(1);
|
|
|
|
const string reason = res_blocks->get_string(2);
|
2019-01-12 13:35:24 -05:00
|
|
|
bool blocked_bool = false;
|
|
|
|
if (blocked == 1)
|
|
|
|
{
|
|
|
|
blocked_bool = true;
|
|
|
|
}
|
2019-01-12 12:18:49 -05:00
|
|
|
|
|
|
|
sqlite::query q_urls(con,
|
|
|
|
"SELECT * FROM urls WHERE user = \'" + user + "\';");
|
|
|
|
sqlite::result_type res_urls = q_urls.get_result();
|
|
|
|
vector<string> receipts;
|
|
|
|
while(res_urls->next_row())
|
|
|
|
{
|
|
|
|
receipts.push_back(res_urls->get_string(1));
|
|
|
|
}
|
2018-08-30 12:04:56 -04:00
|
|
|
|
2019-01-12 12:18:49 -05:00
|
|
|
result.push_back(
|
|
|
|
{
|
|
|
|
user,
|
2019-01-12 13:35:24 -05:00
|
|
|
blocked_bool,
|
2019-01-12 12:18:49 -05:00
|
|
|
reason,
|
|
|
|
receipts
|
|
|
|
});
|
2018-08-30 12:04:56 -04:00
|
|
|
}
|
|
|
|
|
2019-01-12 12:18:49 -05:00
|
|
|
return result;
|
2018-08-30 12:04:56 -04:00
|
|
|
}
|
|
|
|
catch (const std::exception &e)
|
|
|
|
{
|
|
|
|
cerr << "An error occurred: " << e.what() << std::endl;
|
2019-01-12 12:18:49 -05:00
|
|
|
return {};
|
2018-08-30 12:04:56 -04:00
|
|
|
}
|
|
|
|
}
|
2019-01-12 15:24:09 -05:00
|
|
|
|
|
|
|
bool Database::reload()
|
|
|
|
{
|
|
|
|
auto buffer = query();
|
|
|
|
if (buffer.empty())
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
_data = std::move(buffer);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<Database::data> &Database::get_data()
|
|
|
|
{
|
|
|
|
return _data;
|
|
|
|
}
|
|
|
|
|
|
|
|
const Database::data Database::get_user(const string &user) const
|
|
|
|
{
|
|
|
|
for (const Database::data &entry : _data)
|
|
|
|
{
|
|
|
|
if (entry.user == user)
|
|
|
|
{
|
|
|
|
return entry;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return {};
|
|
|
|
}
|