From c67f973227e9c5a504bcceebbf41817cd5882efe Mon Sep 17 00:00:00 2001 From: tastytea Date: Thu, 30 Aug 2018 14:03:25 +0200 Subject: [PATCH] initial commit --- .gitignore | 1 + CMakeLists.txt | 34 +++++++++++ README.md | 4 ++ src/version.hpp.in | 9 +++ src/whyblock.cpp | 146 +++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 194 insertions(+) create mode 100644 .gitignore create mode 100644 CMakeLists.txt create mode 100644 README.md create mode 100644 src/version.hpp.in create mode 100644 src/whyblock.cpp diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..84c048a --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/build/ diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..815cf09 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,34 @@ +cmake_minimum_required (VERSION 3.7) +project (whyblocked + VERSION 0.1.0 + LANGUAGES CXX +) + +include(GNUInstallDirs) +find_package(PkgConfig REQUIRED) +pkg_check_modules(LIBXDG_BASEDIR REQUIRED libxdg-basedir) + +set(CMAKE_CXX_STANDARD 14) +set(CMAKE_CXX_STANDARD_REQUIRED ON) +set(CMAKE_CXX_EXTENSIONS OFF) + +set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -Wall -g -Og") + +include_directories(${PROJECT_SOURCE_DIR}/src) +include_directories(${PROJECT_BINARY_DIR}) + +include_directories(${LIBXDG_BASEDIR_INCLUDE_DIRS}) + +link_directories(${LIBXDG_BASEDIR_LIBRARY_DIRS}) + +# Write version in header +configure_file ( + "${PROJECT_SOURCE_DIR}/src/version.hpp.in" + "${PROJECT_BINARY_DIR}/version.hpp" +) + +file(GLOB sources src/*.cpp) +add_executable(whyblocked ${sources}) +target_link_libraries(whyblocked + ${LIBXDG_BASEDIR_LIBRARIES} vsqlitepp stdc++fs) +install(TARGETS whyblocked DESTINATION ${CMAKE_INSTALL_BINDIR}) diff --git a/README.md b/README.md new file mode 100644 index 0000000..5b9d5a9 --- /dev/null +++ b/README.md @@ -0,0 +1,4 @@ +**whyblocked** reminds you why you blocked someone. It is developed with +Mastodon in mind, but can be used for other contexts, of course. + +It has a text interface and uses a SQLite-database. diff --git a/src/version.hpp.in b/src/version.hpp.in new file mode 100644 index 0000000..cec7915 --- /dev/null +++ b/src/version.hpp.in @@ -0,0 +1,9 @@ +#ifndef VERSION_HPP +#define VERSION_HPP + +namespace global +{ + static constexpr char version[] = "@PROJECT_VERSION@"; +} + +#endif // VERSION_HPP diff --git a/src/whyblock.cpp b/src/whyblock.cpp new file mode 100644 index 0000000..e1a0cbf --- /dev/null +++ b/src/whyblock.cpp @@ -0,0 +1,146 @@ +// CC-0, tastytea + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +using std::string; +using std::cout; +using std::cerr; +using std::cin; +namespace fs = std::experimental::filesystem; + +string get_filepath() +{ + string filepath; + xdgHandle xdg; + xdgInitHandle(&xdg); + filepath = xdgDataHome(&xdg); + xdgWipeHandle(&xdg); + + filepath += "/whyblock"; + if (!fs::exists(filepath)) + { + fs::create_directory(filepath); + } + filepath += "/database.sqlite"; + 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; +} + +int main(int argc, char *argv[]) +{ + try + { + sqlite::connection con(get_filepath()); + string answer; + + cout << "Would you like to add, remove or view records?\n"; + cout << "Or do you want to get the details of a record?\n"; + cout << "Type add, remove, view or details. Or just the first letter\n"; + cout << ": "; + cin >> answer; + switch (answer[0]) + { + case 'a': + case 'A': + { + cout << "ADD\n"; + break; + } + case 'r': + case 'R': + { + cout << "REMOVE\n"; + break; + } + case 'v': + case 'V': + { + sqlite::query q(con, "SELECT * FROM blocks;"); + boost::shared_ptr result = q.get_result(); + while(result->next_row()) + { + if (result->get_int(1) == 1) + { + cout << " Blocked: "; + } + else + { + cout << "Silenced: "; + } + cout << result->get_string(0) << " because: "; + cout << result->get_string(2) << '\n'; + } + break; + } + case 'd': + case 'D': + { + cout << "Which user?\n"; + cin >> answer; + { + sqlite::query q(con, "SELECT * FROM blocks WHERE user = \'" + answer + "\';"); + boost::shared_ptr result = q.get_result(); + result->next_row(); + cout << answer << " is "; + if (result->get_row_count() == 0) + { + cout << "not in the database.\n"; + break; + } + if (result->get_int(1) == 1) + { + cout << "blocked, because: "; + } + else if (result->get_int(1) == 0) + { + cout << "silenced, because: "; + } + cout << result->get_string(2) << '\n'; + } + { + sqlite::query q(con, "SELECT * FROM urls WHERE user = \'" + answer + "\';"); + boost::shared_ptr result = q.get_result(); + while(result->next_row()) + { + cout << result->get_string(1) << '\n'; + } + } + break; + } + default: + cout << "Response not understood.\n"; + } + // sqlite::execute ins(con, "INSERT INTO TEST VALUES(?, ?, ?);"); + // ins % sqlite::nil % "Hello"; + // ins(); + + // sqlite::query q(con, "SELECT * FROM blocks;"); + // boost::shared_ptr result = q.get_result(); + // while(result->next_row()) + // { + // std::cout << "ID: " << result->get_int(0) << "\n" + // << "Name: " << result->get_string(1) << std::endl; + // } + } + catch(const std::exception &e) + { + cerr << "An error occurred: " << e.what() << std::endl; + return 1; + } + + return 0; +}