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)
|
||||
project (mastobotmon
|
||||
VERSION 0.1.0
|
||||
VERSION 0.1.1
|
||||
LANGUAGES CXX
|
||||
)
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
111
src/config.cpp
111
src/config.cpp
|
@ -14,6 +14,8 @@
|
|||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#define RAPIDJSON_HAS_STDSTRING 1
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <cstring> // 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<rapidjson::StringBuffer> 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<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/>.
|
||||
*/
|
||||
|
||||
#define RAPIDJSON_HAS_STDSTRING 1
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <cstring>
|
||||
#include <cstdint>
|
||||
#include <vector>
|
||||
#include <chrono>
|
||||
|
@ -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<Account> accounts;
|
||||
|
||||
for (const auto &member : document["accounts"].GetObject())
|
||||
|
|
|
@ -17,6 +17,8 @@
|
|||
#ifndef mastobotmon_HPP
|
||||
#define mastobotmon_HPP
|
||||
|
||||
#define RAPIDJSON_HAS_STDSTRING 1
|
||||
|
||||
#include <string>
|
||||
#include <cstdint>
|
||||
#include <rapidjson/document.h>
|
||||
|
@ -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
|
||||
{
|
||||
|
|
Loading…
Reference in New Issue