// mologramd: the daemon. Owns the Telegram session and serves every UI process over a unix socket (SPEC section 5). #include #include #include #include #include #include "mologram/Config.hpp" #include "mologram/Daemon.hpp" #include "mologram/FakeTelegramService.hpp" #include "mologram/Log.hpp" #include "mologram/Version.hpp" #ifdef MOLOGRAM_HAS_TAPI #include "mologram/tapi/TapiService.hpp" #endif static mologram::Daemon* g_daemon = nullptr; static void onSignal(int) { if (g_daemon) g_daemon->stop(); } int main(int argc, char** argv) { std::string cfgPath, socketPath, authTsv, account; std::string importAuth; bool fake = false, userbotStopped = false; for (int i = 1; i < argc; i++) { std::string a = argv[i]; if (a == "--help" || a == "-h") { std::cout << "mologramd " << MOLOGRAM_VERSION << "\nusage: mologramd [--config PATH] [--socket PATH] [--account NAME] [--fake] [--foreground]\n" << " --fake serve the in-memory fake service (tests and demos)\n" << " --auth-tsv PATH development only: use an exported auth key instead of the session file\n" << " --import-auth PATH --i-stopped-the-userbot one-time: store an exported auth key in the session file (no login code needed afterwards)\n"; return 0; } if (a == "--version") { std::cout << "mologramd " << MOLOGRAM_VERSION << "\n"; return 0; } if (a == "--config" && i + 1 < argc) cfgPath = argv[++i]; else if (a == "--socket" && i + 1 < argc) socketPath = argv[++i]; else if (a == "--fake") fake = true; else if (a == "--auth-tsv" && i + 1 < argc) authTsv = argv[++i]; else if (a == "--account" && i + 1 < argc) account = argv[++i]; else if (a == "--import-auth" && i + 1 < argc) importAuth = argv[++i]; else if (a == "--i-stopped-the-userbot") userbotStopped = true; } mologram::Config cfg = mologram::Config::load(cfgPath); if (!account.empty()) cfg.setAccount(account); // an account profile has its own session and socket (FR-02) for (const auto& w : cfg.warnings()) std::cerr << "config warning: " << w << "\n"; if (socketPath.empty()) socketPath = cfg.str("socket_path"); std::signal(SIGPIPE, SIG_IGN); // The daemon runs detached from any terminal, so its log file is where it explains itself (OPS-03). mologram::Log log(fake ? std::string() : cfg.str("data_dir") + "/log"); // the fake service (tests, demos) must not write into the real log log.addSecret(cfg.str("api_hash")); log.addSecret(cfg.str("phone")); log.addSecret(std::to_string(cfg.num("api_id"))); if (!importAuth.empty()) { #ifdef MOLOGRAM_HAS_TAPI if (!userbotStopped) { std::cerr << "mologramd: --import-auth copies the auth key of the Python userbot; two clients on one key log each other out.\n" << "Stop the userbot, then repeat the command with --i-stopped-the-userbot.\n"; return 2; } std::string why = mologram::TapiService::importAuth(importAuth, cfg.str("session_path")); if (!why.empty()) { log.error("import", "failed: " + why); std::cerr << "mologramd: import failed: " << why << "\n"; return 1; } log.info("import", "auth key stored in the session file"); std::cout << "Imported. Start mologram without --auth-tsv: no login code is needed.\n"; return 0; #else std::cerr << "mologramd: built without the Telegram service\n"; return 2; #endif } log.info("daemon", std::string("starting ") + MOLOGRAM_VERSION + (fake ? " (fake service)" : "") + ", socket " + socketPath); std::unique_ptr service; std::function startService = []() {}; if (fake) { service.reset(new mologram::FakeTelegramService()); } else { #ifdef MOLOGRAM_HAS_TAPI auto missing = cfg.missingRequired(); if (!missing.empty()) { std::string names; for (const auto& m : missing) names += " " + m; log.error("daemon", "required config keys missing:" + names); std::cerr << "mologramd: required config keys missing:" << names << "\n"; return 2; } mologram::TapiService::Options o; o.apiId = cfg.num("api_id"); o.apiHash = cfg.str("api_hash"); o.phone = cfg.str("phone"); o.sessionPath = cfg.str("session_path"); o.authTsv = authTsv; o.cacheDir = cfg.str("data_dir") + "/cache"; o.secretStorePath = cfg.str("data_dir") + "/secret_chats.bin"; o.secretHistoryPath = cfg.str("data_dir") + "/secret_history.json"; o.log = &log; try { auto* t = new mologram::TapiService(o); service.reset(t); startService = [t]() { t->start(); }; } catch (const std::exception& e) { log.error("daemon", std::string("cannot start the Telegram service: ") + e.what()); std::cerr << "mologramd: cannot start the Telegram service: " << e.what() << "\n"; return 1; } #else std::cerr << "mologramd: built without the Telegram service; use --fake\n"; return 2; #endif } mologram::Daemon daemon(*service, socketPath); std::string err = daemon.start(); if (!err.empty()) { log.error("daemon", err); std::cerr << "mologramd: " << err << "\n"; return 1; } startService(); daemon.setStopWhenEmpty(!cfg.flag("keep_daemon")); g_daemon = &daemon; std::signal(SIGINT, onSignal); std::signal(SIGTERM, onSignal); daemon.run(); log.info("daemon", "stopped"); return 0; }