-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbans.cpp
50 lines (43 loc) · 1.57 KB
/
bans.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include "bans.hpp"
#include "sqlite.hpp"
#include "util.hpp"
#include <algorithm>
#include <iterator>
namespace bans {
sqlite::Connection db("bans.db");
void init() {
db.exec("CREATE TABLE IF NOT EXISTS bans (username TEXT UNIQUE)");
}
std::vector<std::string> get_all_bans() {
thread_local sqlite::Statement stmt(db, "SELECT username FROM bans ORDER BY username");
auto table = stmt.exec<sqlite::Text>();
stmt.reset();
std::vector<std::string> ret;
ret.reserve(table.size());
std::transform(table.begin(), table.end(), std::back_inserter(ret), [](const auto& row) {
return std::get<0>(row);
});
return ret;
}
void ban(pn::StringView username) {
thread_local sqlite::Statement stmt(db, "INSERT OR IGNORE INTO bans (username) VALUES (?)");
stmt.bind(username, 1);
stmt.exec_void();
INFO("User " << std::quoted(username) << " has been BANNED");
stmt.reset();
}
void unban(pn::StringView username) {
thread_local sqlite::Statement stmt(db, "DELETE FROM bans WHERE username = ?");
stmt.bind(username, 1);
stmt.exec_void();
INFO("User " << std::quoted(username) << " has been unbanned");
stmt.reset();
}
bool is_banned(pn::StringView username) {
thread_local sqlite::Statement stmt(db, "SELECT username FROM bans WHERE username = ?");
stmt.bind(username, 1);
auto table = stmt.exec<sqlite::Text>();
stmt.reset();
return !table.empty();
}
} // namespace bans