66 lines
1.9 KiB
C++
66 lines
1.9 KiB
C++
#include "config.h"
|
|
#include "manager.h"
|
|
#include "spdlog/spdlog.h"
|
|
#include "state.h"
|
|
#include <filesystem>
|
|
#include <iostream>
|
|
#include <uv.h>
|
|
#include <td/telegram/td_api.h>
|
|
#include <td/telegram/td_api.hpp>
|
|
|
|
void on_signal(uv_signal_t *h, int signum) {
|
|
uv_stop(uv_default_loop());
|
|
}
|
|
|
|
void create_signal_handles(uv_loop_t *loop, uv_signal_t handles[2]) {
|
|
uv_signal_init(loop, handles);
|
|
uv_signal_start(handles, on_signal, SIGINT);
|
|
uv_signal_init(loop, handles + 1);
|
|
uv_signal_start(handles + 1, on_signal, SIGTERM);
|
|
}
|
|
|
|
int main() {
|
|
uv_loop_t *loop = uv_default_loop();
|
|
spdlog::set_level(spdlog::level::trace);
|
|
|
|
uv_signal_t signalHandles[2] = {};
|
|
create_signal_handles(loop, signalHandles);
|
|
|
|
config::AppConfig config("bridge_config.json");
|
|
if (config.tgApiId == 0 || config.tgApiHash.empty() || config.tgPhoneNumber.empty()
|
|
|| config.vkServiceKey.empty() || config.tgDestinationId == 0) {
|
|
spdlog::error("incomplete config file");
|
|
return 2;
|
|
}
|
|
|
|
state::AppState state;
|
|
try {
|
|
state = state::AppState("bridge_state.json", &config);
|
|
spdlog::info("state: {}", state.to_string());
|
|
} catch (state::InvalidSavedStateException &e) {
|
|
spdlog::error("invalid saved state: {}", e.message);
|
|
return 1;
|
|
}
|
|
|
|
std::function<void(std::function<void(std::string)>)> tgAuthCodeProvider = [](auto set_code){
|
|
spdlog::warn("/!\\ hanging event loop to request code");
|
|
std::string code;
|
|
std::cin >> code;
|
|
set_code(code);
|
|
};
|
|
|
|
std::function<void(std::function<void(std::string)>)> tgPasswordProvider = [](auto set_password){
|
|
spdlog::warn("/!\\ hanging event loop to request password");
|
|
std::string password;
|
|
std::cin >> password;
|
|
set_password(password);
|
|
};
|
|
|
|
manager::RepostManager manager(loop, tgAuthCodeProvider, tgPasswordProvider, &state, &config);
|
|
manager.start();
|
|
|
|
uv_run(loop, UV_RUN_DEFAULT);
|
|
spdlog::info("event loop ended");
|
|
state.save();
|
|
return 0;
|
|
} |