Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .clang-format
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ BasedOnStyle: llvm
IndentWidth: 4
---
Language: Cpp
BreakBeforeBraces: Stroustrup
BreakBeforeBraces: Attach
AccessModifierOffset: -4

28 changes: 10 additions & 18 deletions src/bots/shinxbot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@
namespace fs = fs;

// ===== Logging =====
void shinxbot::refresh_log_stream()
{
void shinxbot::refresh_log_stream() {
std::time_t nt =
std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());
tm tt = *std::localtime(&nt);
Expand All @@ -35,22 +34,17 @@ void shinxbot::refresh_log_stream()
}

shinxbot::shinxbot(int recv_port, int send_port, const std::string &tk)
: bot(recv_port, send_port, tk)
{
}
: bot(recv_port, send_port, tk) {}
shinxbot::shinxbot(const Json::Value &J)
: bot(J["recv_port"].asInt(), J["send_port"].asInt(), J["token"].asString())
{
}
: bot(J["recv_port"].asInt(), J["send_port"].asInt(),
J["token"].asString()) {}

bool shinxbot::is_op(const userid_t a) const
{
bool shinxbot::is_op(const userid_t a) const {
return op_list.find(a) != op_list.end();
}

// ===== Runtime logging and teardown =====
void shinxbot::setlog(LOG type, std::string message)
{
void shinxbot::setlog(LOG type, std::string message) {
std::lock_guard<std::mutex> lock(log_lock);

std::time_t nt =
Expand All @@ -69,24 +63,22 @@ void shinxbot::setlog(LOG type, std::string message)
tt.tm_sec, LOG_name[type], message);

if (type == LOG::ERROR)
fmt::print(stderr, formatted_message);
fmt::print(stderr, "{}", formatted_message);
else
fmt::print(formatted_message);
fmt::print("{}", formatted_message);
LOG_output[type] << formatted_message;
LOG_output[type].flush();
}

void shinxbot::cq_send_all_op(const std::string &message)
{
void shinxbot::cq_send_all_op(const std::string &message) {
msg_meta conf = (msg_meta){"private", 0, 0, 0, this};
for (userid_t uid : op_list) {
conf.user_id = uid;
cq_send(message, conf);
}
}

shinxbot::~shinxbot()
{
shinxbot::~shinxbot() {
if (this->mytimer != nullptr) {
this->mytimer->timer_stop();
delete this->mytimer;
Expand Down
34 changes: 11 additions & 23 deletions src/bots/shinxbot.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,57 +19,45 @@ class blockItem {
blockItem() : mode(true) {}
blockItem(const std::set<std::string> &blocklist,
const std::set<std::string> &whitelist, bool mode)
: blocklist(blocklist), whitelist(whitelist), mode(mode)
{
}
blockItem(const Json::Value &J)
{
: blocklist(blocklist), whitelist(whitelist), mode(mode) {}
blockItem(const Json::Value &J) {
if (J.isMember("block") && J.isMember("white") && J.isMember("mode")) {
parse_json_to_set(J["block"], blocklist);
parse_json_to_set(J["white"], whitelist);
mode = J["mode"].asBool();
}
else {
} else {
mode = true;
}
}
bool is_blocked(const std::string &message)
{
bool is_blocked(const std::string &message) {
if (mode) {
return blocklist.find(message) != blocklist.end();
}
else {
} else {
return whitelist.find(message) == whitelist.end();
}
}
void add_block(const std::string &message)
{
void add_block(const std::string &message) {
blocklist.insert(message);
mode = true;
}
void remove_block(const std::string &message)
{
void remove_block(const std::string &message) {
blocklist.erase(message);
mode = true;
}
void add_white(const std::string &message)
{
void add_white(const std::string &message) {
whitelist.insert(message);
mode = false;
}
void remove_white(const std::string &message)
{
void remove_white(const std::string &message) {
whitelist.erase(message);
mode = false;
}
void clear()
{
void clear() {
blocklist.clear();
whitelist.clear();
mode = true;
}
Json::Value to_json() const
{
Json::Value to_json() const {
Json::Value J;
J["block"] = parse_set_to_json(blocklist);
J["white"] = parse_set_to_json(whitelist);
Expand Down
Loading