From 3273434cfb3fc6c3c7919d06f491cb9d521c2ae7 Mon Sep 17 00:00:00 2001 From: tastytea Date: Fri, 2 Mar 2018 08:48:02 +0100 Subject: [PATCH] Added account adding, refactored a bit --- CMakeLists.txt | 2 +- README.md | 8 +++- src/config.cpp | 111 +++++++++++++++++++++++++------------------- src/mastobotmon.cpp | 11 +++++ src/mastobotmon.hpp | 6 ++- 5 files changed, 86 insertions(+), 52 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index f640222..582b27a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,6 +1,6 @@ cmake_minimum_required (VERSION 3.7) project (mastobotmon - VERSION 0.1.0 + VERSION 0.1.1 LANGUAGES CXX ) diff --git a/README.md b/README.md index 5a4d38a..9fd6f85 100644 --- a/README.md +++ b/README.md @@ -32,7 +32,11 @@ Install with `make install`. # Usage Run mastobotmon once. Edit config file (`~/.config/mastobotmon.json`). -Call mastobotmon from cron. +Call mastobotmon from cron. If a bot is inactive for longer than the specified +minutes, it prints an alert to stdout. + +## Commands +* add – Add accounts ## Error codes @@ -51,7 +55,7 @@ If you use a debug build, you get more verbose error messages. * [x] Config file * [x] Alert if account seems inactive * Version 0.2.0 - * [ ] Allow to add accounts later + * [x] Allow to add accounts later * [ ] Write mentions to file * Version 0.3.0 * [ ] Write statistics to file diff --git a/src/config.cpp b/src/config.cpp index 0d4e772..5d4c367 100644 --- a/src/config.cpp +++ b/src/config.cpp @@ -14,6 +14,8 @@ * along with this program. If not, see . */ +#define RAPIDJSON_HAS_STDSTRING 1 + #include #include #include // strlen() @@ -32,9 +34,10 @@ using std::cin; using std::cerr; using std::uint16_t; -bool read_config(rapidjson::Document &document) +const string filepath = string(std::getenv("HOME")) + "/.config/mastobotmon.json"; + +const bool read_config(rapidjson::Document &document) { - const string filepath = string(std::getenv("HOME")) + "/.config/mastobotmon.json"; std::ifstream file(filepath); std::stringstream json; @@ -70,55 +73,17 @@ bool read_config(rapidjson::Document &document) { cout << "No config file found. Creating new one.\n"; - std::ofstream outfile(filepath); - if (outfile.is_open()) - { - cout << "Adding accounts (user@domain), blank line to stop.\n"; - rapidjson::StringBuffer buffer; - rapidjson::PrettyWriter writer(buffer); - string account; - string minutes; + rapidjson::Document::AllocatorType &allocator = document.GetAllocator(); - writer.StartObject(); - writer.Key("accounts"); - writer.StartObject(); + document.SetObject(); + rapidjson::Value object(rapidjson::kObjectType); + document.AddMember("accounts", object.Move(), allocator); + add_account(document); - while (true) - { - cout << "Add Account: "; - std::getline(cin, account); - if (account.empty()) - { - break; - } - cout << "Minutes af allowed inactivity: "; - std::getline(cin, minutes); - writer.Key(account.c_str()); - writer.StartObject(); - writer.Key("minutes"); - writer.Uint(std::stoi(minutes)); - writer.Key("access_token"); - writer.String(get_access_token(account).c_str()); - writer.EndObject(); - } - writer.EndObject(); + document.AddMember("mode", "cron", allocator); + document.AddMember("daemon_check", 10, allocator); - writer.Key("mode"); - writer.String("cron"); - writer.Key("daemon_check"); - writer.Uint(10); - writer.EndObject(); - - outfile.write(buffer.GetString(), std::strlen(buffer.GetString())); - outfile.close(); - - if (!document.Parse(buffer.GetString()).HasParseError()) - { - return true; - } - } - - return false; + return write_config(document); } return true; @@ -154,3 +119,53 @@ const string get_access_token(const string &account) cerr << "Error: " << ret << '\n'; return ""; } + +const bool add_account(rapidjson::Document &document) +{ + string account; + string minutes; + string access_token; + rapidjson::Document::AllocatorType &allocator = document.GetAllocator(); + + cout << "Adding accounts (user@domain), blank line to stop.\n"; + while (true) + { + cout << "Add Account: "; + std::getline(cin, account); + if (account.empty()) + { + break; + } + cout << "Minutes af allowed inactivity: "; + std::getline(cin, minutes); + access_token = get_access_token(account); + + rapidjson::Value vobject(rapidjson::kObjectType); + rapidjson::Value vaccount(account, allocator); + rapidjson::Value vaccess_token(access_token, allocator); + + vobject.AddMember("minutes", std::stoi(minutes), allocator); + vobject.AddMember("access_token", vaccess_token.Move(), allocator); + document["accounts"].AddMember(vaccount.Move(), vobject, allocator); + } + + return write_config(document); +} + +const bool write_config(rapidjson::Document &document) +{ + rapidjson::StringBuffer buffer; + rapidjson::PrettyWriter writer(buffer); + document.Accept(writer); + + std::ofstream outfile(filepath); + if (outfile.is_open()) + { + outfile.write(buffer.GetString(), std::strlen(buffer.GetString())); + outfile.close(); + + return true; + } + + return false; +} diff --git a/src/mastobotmon.cpp b/src/mastobotmon.cpp index 9f3a8a2..2b156be 100644 --- a/src/mastobotmon.cpp +++ b/src/mastobotmon.cpp @@ -14,8 +14,11 @@ * along with this program. If not, see . */ +#define RAPIDJSON_HAS_STDSTRING 1 + #include #include +#include #include #include #include @@ -40,6 +43,14 @@ int main(int argc, char *argv[]) return 1; } + if (argc > 1) + { + if ((std::strncmp(argv[1], "add", 3)) == 0) + { + add_account(document); + } + } + std::vector accounts; for (const auto &member : document["accounts"].GetObject()) diff --git a/src/mastobotmon.hpp b/src/mastobotmon.hpp index 8784294..14d9a0f 100644 --- a/src/mastobotmon.hpp +++ b/src/mastobotmon.hpp @@ -17,6 +17,8 @@ #ifndef mastobotmon_HPP #define mastobotmon_HPP +#define RAPIDJSON_HAS_STDSTRING 1 + #include #include #include @@ -25,8 +27,10 @@ using std::uint16_t; using std::string; -bool read_config(rapidjson::Document &document); +const bool read_config(rapidjson::Document &document); const string get_access_token(const string &account); +const bool add_account(rapidjson::Document &document); +const bool write_config(rapidjson::Document &document); class Account : public Mastodon::API {