Added account adding, refactored a bit
This commit is contained in:
parent
c3858936d0
commit
3273434cfb
|
@ -1,6 +1,6 @@
|
||||||
cmake_minimum_required (VERSION 3.7)
|
cmake_minimum_required (VERSION 3.7)
|
||||||
project (mastobotmon
|
project (mastobotmon
|
||||||
VERSION 0.1.0
|
VERSION 0.1.1
|
||||||
LANGUAGES CXX
|
LANGUAGES CXX
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
|
@ -32,7 +32,11 @@ Install with `make install`.
|
||||||
# Usage
|
# Usage
|
||||||
|
|
||||||
Run mastobotmon once. Edit config file (`~/.config/mastobotmon.json`).
|
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
|
## Error codes
|
||||||
|
|
||||||
|
@ -51,7 +55,7 @@ If you use a debug build, you get more verbose error messages.
|
||||||
* [x] Config file
|
* [x] Config file
|
||||||
* [x] Alert if account seems inactive
|
* [x] Alert if account seems inactive
|
||||||
* Version 0.2.0
|
* Version 0.2.0
|
||||||
* [ ] Allow to add accounts later
|
* [x] Allow to add accounts later
|
||||||
* [ ] Write mentions to file
|
* [ ] Write mentions to file
|
||||||
* Version 0.3.0
|
* Version 0.3.0
|
||||||
* [ ] Write statistics to file
|
* [ ] Write statistics to file
|
||||||
|
|
111
src/config.cpp
111
src/config.cpp
|
@ -14,6 +14,8 @@
|
||||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#define RAPIDJSON_HAS_STDSTRING 1
|
||||||
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <cstring> // strlen()
|
#include <cstring> // strlen()
|
||||||
|
@ -32,9 +34,10 @@ using std::cin;
|
||||||
using std::cerr;
|
using std::cerr;
|
||||||
using std::uint16_t;
|
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::ifstream file(filepath);
|
||||||
std::stringstream json;
|
std::stringstream json;
|
||||||
|
|
||||||
|
@ -70,55 +73,17 @@ bool read_config(rapidjson::Document &document)
|
||||||
{
|
{
|
||||||
cout << "No config file found. Creating new one.\n";
|
cout << "No config file found. Creating new one.\n";
|
||||||
|
|
||||||
std::ofstream outfile(filepath);
|
rapidjson::Document::AllocatorType &allocator = document.GetAllocator();
|
||||||
if (outfile.is_open())
|
|
||||||
{
|
|
||||||
cout << "Adding accounts (user@domain), blank line to stop.\n";
|
|
||||||
rapidjson::StringBuffer buffer;
|
|
||||||
rapidjson::PrettyWriter<rapidjson::StringBuffer> writer(buffer);
|
|
||||||
string account;
|
|
||||||
string minutes;
|
|
||||||
|
|
||||||
writer.StartObject();
|
document.SetObject();
|
||||||
writer.Key("accounts");
|
rapidjson::Value object(rapidjson::kObjectType);
|
||||||
writer.StartObject();
|
document.AddMember("accounts", object.Move(), allocator);
|
||||||
|
add_account(document);
|
||||||
|
|
||||||
while (true)
|
document.AddMember("mode", "cron", allocator);
|
||||||
{
|
document.AddMember("daemon_check", 10, allocator);
|
||||||
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();
|
|
||||||
|
|
||||||
writer.Key("mode");
|
return write_config(document);
|
||||||
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 true;
|
return true;
|
||||||
|
@ -154,3 +119,53 @@ const string get_access_token(const string &account)
|
||||||
cerr << "Error: " << ret << '\n';
|
cerr << "Error: " << ret << '\n';
|
||||||
return "";
|
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<rapidjson::StringBuffer> 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;
|
||||||
|
}
|
||||||
|
|
|
@ -14,8 +14,11 @@
|
||||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#define RAPIDJSON_HAS_STDSTRING 1
|
||||||
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
#include <cstring>
|
||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include <chrono>
|
#include <chrono>
|
||||||
|
@ -40,6 +43,14 @@ int main(int argc, char *argv[])
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (argc > 1)
|
||||||
|
{
|
||||||
|
if ((std::strncmp(argv[1], "add", 3)) == 0)
|
||||||
|
{
|
||||||
|
add_account(document);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
std::vector<Account> accounts;
|
std::vector<Account> accounts;
|
||||||
|
|
||||||
for (const auto &member : document["accounts"].GetObject())
|
for (const auto &member : document["accounts"].GetObject())
|
||||||
|
|
|
@ -17,6 +17,8 @@
|
||||||
#ifndef mastobotmon_HPP
|
#ifndef mastobotmon_HPP
|
||||||
#define mastobotmon_HPP
|
#define mastobotmon_HPP
|
||||||
|
|
||||||
|
#define RAPIDJSON_HAS_STDSTRING 1
|
||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
#include <rapidjson/document.h>
|
#include <rapidjson/document.h>
|
||||||
|
@ -25,8 +27,10 @@
|
||||||
using std::uint16_t;
|
using std::uint16_t;
|
||||||
using std::string;
|
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 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
|
class Account : public Mastodon::API
|
||||||
{
|
{
|
||||||
|
|
Loading…
Reference in New Issue
Block a user