Browse Source

initial commit

tastytea 3 months ago
commit
c67f973227
Signed by: tastytea <tastytea@tastytea.de> GPG Key ID: CFC39497F1B26E07
5 changed files with 194 additions and 0 deletions
  1. 1
    0
      .gitignore
  2. 34
    0
      CMakeLists.txt
  3. 4
    0
      README.md
  4. 9
    0
      src/version.hpp.in
  5. 146
    0
      src/whyblock.cpp

+ 1
- 0
.gitignore View File

@@ -0,0 +1 @@
1
+/build/

+ 34
- 0
CMakeLists.txt View File

@@ -0,0 +1,34 @@
1
+cmake_minimum_required (VERSION 3.7)
2
+project (whyblocked
3
+         VERSION 0.1.0
4
+         LANGUAGES CXX
5
+)
6
+
7
+include(GNUInstallDirs)
8
+find_package(PkgConfig REQUIRED)
9
+pkg_check_modules(LIBXDG_BASEDIR REQUIRED libxdg-basedir)
10
+
11
+set(CMAKE_CXX_STANDARD 14)
12
+set(CMAKE_CXX_STANDARD_REQUIRED ON)
13
+set(CMAKE_CXX_EXTENSIONS OFF)
14
+
15
+set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -Wall -g -Og")
16
+
17
+include_directories(${PROJECT_SOURCE_DIR}/src)
18
+include_directories(${PROJECT_BINARY_DIR})
19
+
20
+include_directories(${LIBXDG_BASEDIR_INCLUDE_DIRS})
21
+
22
+link_directories(${LIBXDG_BASEDIR_LIBRARY_DIRS})
23
+
24
+# Write version in header
25
+configure_file (
26
+    "${PROJECT_SOURCE_DIR}/src/version.hpp.in"
27
+    "${PROJECT_BINARY_DIR}/version.hpp"
28
+)
29
+
30
+file(GLOB sources src/*.cpp)
31
+add_executable(whyblocked ${sources})
32
+target_link_libraries(whyblocked
33
+                      ${LIBXDG_BASEDIR_LIBRARIES} vsqlitepp stdc++fs)
34
+install(TARGETS whyblocked DESTINATION ${CMAKE_INSTALL_BINDIR})

+ 4
- 0
README.md View File

@@ -0,0 +1,4 @@
1
+**whyblocked** reminds you why you blocked someone. It is developed with
2
+Mastodon in mind, but can be used for other contexts, of course.
3
+
4
+It has a text interface and uses a SQLite-database.

+ 9
- 0
src/version.hpp.in View File

@@ -0,0 +1,9 @@
1
+#ifndef VERSION_HPP
2
+#define VERSION_HPP
3
+
4
+namespace global
5
+{
6
+    static constexpr char version[] = "@PROJECT_VERSION@";
7
+}
8
+
9
+#endif // VERSION_HPP

+ 146
- 0
src/whyblock.cpp View File

@@ -0,0 +1,146 @@
1
+// CC-0, tastytea
2
+
3
+#include <iostream>
4
+#include <string>
5
+#include <experimental/filesystem>
6
+#include <basedir.h>
7
+#include <sqlite/connection.hpp>
8
+#include <sqlite/execute.hpp>
9
+#include <sqlite/query.hpp>
10
+#include <boost/shared_ptr.hpp>
11
+#include <version.hpp>
12
+
13
+using std::string;
14
+using std::cout;
15
+using std::cerr;
16
+using std::cin;
17
+namespace fs = std::experimental::filesystem;
18
+
19
+string get_filepath()
20
+{
21
+    string filepath;
22
+    xdgHandle xdg;
23
+    xdgInitHandle(&xdg);
24
+    filepath = xdgDataHome(&xdg);
25
+    xdgWipeHandle(&xdg);
26
+
27
+    filepath += "/whyblock";
28
+    if (!fs::exists(filepath))
29
+    {
30
+        fs::create_directory(filepath);
31
+    }
32
+    filepath += "/database.sqlite";
33
+    if (!fs::exists(filepath))
34
+    {
35
+        sqlite::connection con(filepath);
36
+        sqlite::execute(con, "CREATE TABLE blocks(user TEXT PRIMARY KEY, blocked INTEGER, reason TEXT);", true);
37
+        sqlite::execute(con, "CREATE TABLE urls(user TEXT, url TEXT);", true);
38
+    }
39
+
40
+    return filepath;
41
+}
42
+
43
+int main(int argc, char *argv[])
44
+{
45
+    try
46
+    {
47
+        sqlite::connection con(get_filepath());
48
+        string answer;
49
+        
50
+        cout << "Would you like to add, remove or view records?\n";
51
+        cout << "Or do you want to get the details of a record?\n";
52
+        cout << "Type add, remove, view or details. Or just the first letter\n";
53
+        cout << ": ";
54
+        cin >> answer;
55
+        switch (answer[0])
56
+        {
57
+            case 'a':
58
+            case 'A':
59
+            {
60
+                cout << "ADD\n";
61
+                break;
62
+            }
63
+            case 'r':
64
+            case 'R':
65
+            {
66
+                cout << "REMOVE\n";
67
+                break;
68
+            }
69
+            case 'v':
70
+            case 'V':
71
+            {
72
+                sqlite::query q(con, "SELECT * FROM blocks;");
73
+                boost::shared_ptr<sqlite::result> result = q.get_result();
74
+                while(result->next_row())
75
+                {
76
+                    if (result->get_int(1) == 1)
77
+                    {
78
+                        cout << " Blocked: ";
79
+                    }
80
+                    else
81
+                    {
82
+                        cout << "Silenced: ";
83
+                    }
84
+                    cout << result->get_string(0) << " because: ";
85
+                    cout << result->get_string(2) << '\n';
86
+                }
87
+                break;
88
+            }
89
+            case 'd':
90
+            case 'D':
91
+            {
92
+                cout << "Which user?\n";
93
+                cin >> answer;
94
+                {
95
+                    sqlite::query q(con, "SELECT * FROM blocks WHERE user = \'" + answer + "\';");
96
+                    boost::shared_ptr<sqlite::result> result = q.get_result();
97
+                    result->next_row();
98
+                    cout << answer << " is ";
99
+                    if (result->get_row_count() == 0)
100
+                    {
101
+                        cout << "not in the database.\n";
102
+                        break;
103
+                    }
104
+                    if (result->get_int(1) == 1)
105
+                    {
106
+                        cout << "blocked, because: ";
107
+                    }
108
+                    else if (result->get_int(1) == 0)
109
+                    {
110
+                        cout << "silenced, because: ";
111
+                    }
112
+                    cout << result->get_string(2) << '\n';
113
+                }
114
+                {
115
+                    sqlite::query q(con, "SELECT * FROM urls WHERE user = \'" + answer + "\';");
116
+                    boost::shared_ptr<sqlite::result> result = q.get_result();
117
+                    while(result->next_row())
118
+                    {
119
+                        cout << result->get_string(1) << '\n';
120
+                    }
121
+                }
122
+                break;
123
+            }
124
+            default:
125
+                cout << "Response not understood.\n";
126
+        }
127
+        // sqlite::execute ins(con, "INSERT INTO TEST VALUES(?, ?, ?);");
128
+        // ins % sqlite::nil % "Hello";
129
+        // ins();
130
+
131
+        // sqlite::query q(con, "SELECT * FROM blocks;");
132
+        // boost::shared_ptr<sqlite::result> result = q.get_result();
133
+        // while(result->next_row())
134
+        // {
135
+        //   std::cout << "ID:   " << result->get_int(0) << "\n"
136
+        //             << "Name: " << result->get_string(1) << std::endl;
137
+        // }
138
+    }
139
+    catch(const std::exception &e)
140
+    {
141
+        cerr << "An error occurred: " << e.what() << std::endl;
142
+        return 1;
143
+    }
144
+
145
+    return 0;
146
+}

Loading…
Cancel
Save