From 0f84c4f1239305414e95aabadf736f4451e9e446 Mon Sep 17 00:00:00 2001 From: tastytea Date: Wed, 28 Feb 2018 06:23:42 +0100 Subject: [PATCH] Added account class --- CMakeLists.txt | 2 +- README.md | 2 +- src/config.cpp | 43 +++++++++++++++++++++++++++++++++++++++++++ src/mastobotmon.cpp | 32 +++++++++++++++++++++++++++++--- src/mastobotmon.hpp | 18 ++++++++++++++++++ src/mastodon.cpp | 41 +++++++++++++++++++++++++++++++++++++++++ 6 files changed, 133 insertions(+), 5 deletions(-) create mode 100644 src/mastodon.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index c4152cd..b9b1d19 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,6 +1,6 @@ cmake_minimum_required (VERSION 3.7) project (mastobotmon - VERSION 0.0.1 + VERSION 0.0.2 LANGUAGES CXX ) diff --git a/README.md b/README.md index da73ccd..5920bb9 100644 --- a/README.md +++ b/README.md @@ -7,7 +7,7 @@ * Tested OS: Linux * C++ compiler (tested: gcc 6.4, clang 5.0) * [cmake](https://cmake.org/) (tested: 3.9.6) - * [mastodon-cpp](https://github.com/tastytea/mastodon-cpp) (tested: 0.4.3) + * [mastodon-cpp](https://github.com/tastytea/mastodon-cpp) (tested: 0.4.4) * [rapidjson](http://rapidjson.org/) (tested: 1.1.0) ## Get sourcecode diff --git a/src/config.cpp b/src/config.cpp index 2ce06d5..0a676b5 100644 --- a/src/config.cpp +++ b/src/config.cpp @@ -30,6 +30,7 @@ using std::string; using std::cout; using std::cin; using std::cerr; +using std::uint16_t; bool read_config(rapidjson::Document &document) { @@ -53,6 +54,12 @@ bool read_config(rapidjson::Document &document) return false; } + if (!document["mode"].IsString()) + { + cerr << "ERROR: \"mode\" not found\n"; + return false; + } + if (!document["daemon_check"].IsUint()) { cerr << "ERROR: \"daemon_check\" not found\n"; @@ -87,10 +94,17 @@ bool read_config(rapidjson::Document &document) 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"); + writer.String("cron"); writer.Key("daemon_check"); writer.Uint(10); writer.EndObject(); @@ -110,3 +124,32 @@ bool read_config(rapidjson::Document &document) return true; } +const string get_access_token(const string &account) +{ + const string instance = account.substr(account.find('@') + 1); + Account acc(instance, ""); + uint16_t ret; + string client_id; + string client_secret; + string url; + string code; + string access_token = ""; + + ret = acc.register_app1("mastobotmon", "urn:ietf:wg:oauth:2.0:oob", "read", + "https://github.com/tastytea/mastobotmon", + client_id, client_secret, url); + if (ret == 0) + { + cout << "Visit " << url << " and paste the generated code\nhere: "; + cin >> code; + ret = acc.register_app2(client_id, client_secret, "urn:ietf:wg:oauth:2.0:oob", + code, access_token); + if (ret == 0) + { + return access_token; + } + } + + cerr << "Error: " << ret << '\n'; + return ""; +} diff --git a/src/mastobotmon.cpp b/src/mastobotmon.cpp index 23b5f08..0b6c616 100644 --- a/src/mastobotmon.cpp +++ b/src/mastobotmon.cpp @@ -17,12 +17,11 @@ #include #include #include -#include +#include #include #include "version.hpp" #include "mastobotmon.hpp" -using Mastodon::API; using std::cout; using std::cerr; using std::cin; @@ -36,9 +35,36 @@ int main(int argc, char *argv[]) return 1; } + std::vector accounts; + for (const auto &member : document["accounts"].GetObject()) { - cout << member.name.GetString() << ": " << member.value.GetUint() << '\n'; + // Construct an Account object for every account + string instance = member.name.GetString(); + instance = instance.substr(instance.find('@') + 1); + Account acc(instance, member.value["access_token"].GetString()); + acc.set_minutes(member.value["minutes"].GetUint()); + accounts.push_back(acc); + } + + cout << "DEBUG\n"; + if (document["mode"] == "cron") + { + cout << "DEBUG\n"; + for (Account &acc : accounts) + { + cout << "DEBUG\n"; + std::string answer; + // std::string id; + + // Account::parametermap parameters( + // { + // { "limit", { "1" } } + // }); + // cout << acc.get(Mastodon::API::v1::statuses, id, parameters, answer); + cout << acc.get(Mastodon::API::v1::accounts_verify_credentials, answer); + cout << answer << '\n'; + } } return 0; diff --git a/src/mastobotmon.hpp b/src/mastobotmon.hpp index 2deee95..0ff39ad 100644 --- a/src/mastobotmon.hpp +++ b/src/mastobotmon.hpp @@ -17,8 +17,26 @@ #ifndef mastobotmon_HPP #define mastobotmon_HPP +#include +#include #include +#include + +using std::uint16_t; +using std::string; bool read_config(rapidjson::Document &document); +const string get_access_token(const string &account); + +class Account : public Mastodon::API +{ +public: + explicit Account(const string &instance, const string &access_token); + void set_minutes(uint16_t minutes); + +private: + uint16_t _minutes; + string _access_token; +}; #endif // mastobotmon_HPP diff --git a/src/mastodon.cpp b/src/mastodon.cpp new file mode 100644 index 0000000..c9131b0 --- /dev/null +++ b/src/mastodon.cpp @@ -0,0 +1,41 @@ +/* This file is part of mastobotmon. + * Copyright © 2018 tastytea + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, version 3. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include +#include +#include +#include +#include "version.hpp" +#include "mastobotmon.hpp" + +using std::cout; +using std::cerr; +using std::cin; +using std::string; +using std::uint16_t; + +Account::Account(const string &instance, const string &access_token) +: API(instance, access_token) +, _minutes(0) +, _access_token("") +{ + // +} + +void Account::set_minutes(uint16_t minutes) +{ + _minutes = minutes; +}