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>
|
|
|
|
#include <tuple>
|
|
|
|
#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::Database() {};
|
|
|
|
|
|
|
|
Database::data::operator bool() const
|
2018-08-30 12:04:56 -04:00
|
|
|
{
|
2019-01-12 12:18:49 -05:00
|
|
|
return !user.empty();
|
|
|
|
}
|
|
|
|
|
|
|
|
const string Database::get_filepath() const
|
|
|
|
{
|
|
|
|
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 12:18:49 -05:00
|
|
|
bool Database::add_user(const string &user, const int blocked,
|
|
|
|
const string &reason)
|
2018-08-30 12:04:56 -04:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
sqlite::connection con(get_filepath());
|
|
|
|
sqlite::execute ins(con, "INSERT INTO blocks VALUES(?, ?, ?);");
|
|
|
|
ins % user % blocked % reason;
|
|
|
|
ins();
|
|
|
|
}
|
|
|
|
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::add_receipt(const string &user, const string &receipt)
|
2018-08-30 12:04:56 -04:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
sqlite::connection con(get_filepath());
|
|
|
|
sqlite::execute ins(con, "INSERT INTO urls VALUES(?, ?);");
|
2018-10-17 11:28:29 -04:00
|
|
|
ins % user % receipt;
|
2018-08-30 12:04:56 -04:00
|
|
|
ins();
|
|
|
|
}
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
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
|
|
|
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);
|
|
|
|
|
|
|
|
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,
|
|
|
|
blocked,
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|