// mologram: the UI process (SPEC section 5). Attaches to the daemon, or runs the in-memory service with --fake. // `--chat KEY` makes it a torn-off window: one conversation, attached to the same daemon as the main window (FR-73). #include #include #include #include #include #include #include #include "mologram/Config.hpp" #include #include "mologram/DaemonLauncher.hpp" #include "mologram/Log.hpp" #include "mologram/DaemonSupervisor.hpp" #include "mologram/FakeTelegramService.hpp" #include "mologram/RemoteService.hpp" #include "mologram/TerminalGuard.hpp" #include "mologram/Version.hpp" #include "mologram/ui/MologramApp.hpp" #ifdef MOLOGRAM_HAS_TAPI #include "mologram/tapi/TapiConfig.hpp" #endif static void printHelp() { std::cout << "mologram " << MOLOGRAM_VERSION << " - Telegram Desktop clone for the terminal\n" << "usage: mologram [--config PATH] [--account NAME] [--dump-config] [--help] [--version] [--no-daemon] [--fake] [--chat KEY] [--socket PATH]\n" << " --config PATH read PATH instead of the search order\n" << " --account NAME use the account profile NAME (own session and daemon socket)\n" << " --dump-config print the effective config, secrets redacted\n" << " --no-daemon run the Telegram service inside this process (no daemon, no socket)\n" << " --fake-daemon start the daemon on the in-memory service (tests and demos)\n" << " --fake use the in-memory fake service (demo and tests)\n" << " --chat KEY open one chat in this terminal, KEY like user:123 (tear-off window)\n" << " --socket PATH attach to the daemon socket at PATH\n" << " --auth-tsv PATH development only: pass an exported auth key file to the daemon\n"; } /// Restarts this program on another account profile: the same arguments with --account replaced and --chat dropped. static void restartOnAccount(char** argv, int argc, const std::string& account) { std::vector args{"/proc/self/exe"}; for (int i = 1; i < argc; i++) { std::string a = argv[i]; if ((a == "--account" || a == "--chat") && i + 1 < argc) { i++; continue; } args.push_back(a); } args.push_back("--account"); args.push_back(account); std::vector raw; for (auto& a : args) raw.push_back(a.data()); raw.push_back(nullptr); ::execv("/proc/self/exe", raw.data()); } int main(int argc, char** argv) { std::string cfgPath, socketOverride, chat, authTsv, account; bool dump = false, fake = false, noDaemon = false, fakeDaemon = false; for (int i = 1; i < argc; i++) { std::string a = argv[i]; if (a == "--help" || a == "-h") { printHelp(); return 0; } if (a == "--version") { std::cout << "mologram " << MOLOGRAM_VERSION << "\n"; return 0; } if (a == "--dump-config") dump = true; else if (a == "--fake") fake = true; else if (a == "--no-daemon") noDaemon = true; else if (a == "--fake-daemon") fakeDaemon = true; else if (a == "--config" && i + 1 < argc) cfgPath = argv[++i]; else if (a == "--chat" && i + 1 < argc) chat = argv[++i]; else if (a == "--socket" && i + 1 < argc) socketOverride = argv[++i]; else if (a == "--auth-tsv" && i + 1 < argc) authTsv = argv[++i]; else if (a == "--account" && i + 1 < argc) account = argv[++i]; } mologram::Config cfg = mologram::Config::load(cfgPath); if (!account.empty()) cfg.setAccount(account); for (const auto& w : cfg.warnings()) std::cerr << "config warning: " << w << "\n"; if (dump) { std::cout << cfg.dumpRedacted(); return 0; } mologram::TerminalGuard guard; // before the UI touches the terminal: every exit path restores it // The UI process logs too (OPS-03): what failed and why survives the screen. mologram::Log log(fake ? std::string() : cfg.str("data_dir") + "/log"); log.addSecret(cfg.str("api_hash")); log.addSecret(cfg.str("phone")); log.addSecret(std::to_string(cfg.num("api_id"))); log.info("ui", std::string("starting ") + MOLOGRAM_VERSION + (chat.empty() ? "" : ", chat " + chat)); mologram::ui::MologramApp::Options opt; opt.openChat = chat; opt.log = &log; opt.socketPath = socketOverride; if (fake) { mologram::FakeTelegramService service; mologram::ui::MologramApp app(cfg, service, opt); int rc = app.run(); if (!app.restartAccount().empty()) restartOnAccount(argv, argc, app.restartAccount()); return rc; } auto missing = cfg.missingRequired(); if (!missing.empty()) { std::cerr << "mologram: required config keys missing:"; for (const auto& m : missing) std::cerr << " " << m; std::cerr << "\n(copy .env.json as described in README.md)\n"; return 2; } if (noDaemon) { #ifdef MOLOGRAM_HAS_TAPI // The Telegram service runs inside this process behind the same ITelegramService border (SPEC section 5, --no-daemon). std::unique_ptr local; try { local = std::make_unique(mologram::tapiOptionsFromConfig(cfg, authTsv)); } catch (const std::exception& e) { std::cerr << "mologram: cannot start the Telegram service: " << e.what() << "\n"; return 1; } local->start(); mologram::ui::MologramApp app(cfg, *local, opt); int rc = app.run(); if (!app.restartAccount().empty()) restartOnAccount(argv, argc, app.restartAccount()); return rc; #else std::cerr << "mologram: this build has no Telegram service; use --fake\n"; return 2; #endif } std::string socket = socketOverride.empty() ? cfg.str("socket_path") : socketOverride; std::vector extra; // The daemon must read the same config as this process, whatever its working directory: pass the resolved absolute path. std::string daemonCfg = !cfgPath.empty() ? cfgPath : cfg.loadedPath(); if (!daemonCfg.empty()) { std::error_code ec; std::string abs = std::filesystem::absolute(daemonCfg, ec).string(); if (!ec && std::filesystem::exists(abs)) daemonCfg = abs; extra.push_back("--config"); extra.push_back(daemonCfg); } if (!authTsv.empty()) { extra.push_back("--auth-tsv"); extra.push_back(authTsv); } if (!account.empty()) { extra.push_back("--account"); extra.push_back(account); } if (fakeDaemon) extra.push_back("--fake"); // tests and demos: the daemon serves the in-memory service std::string err = mologram::DaemonLauncher::ensure(socket, extra); if (!err.empty()) { log.error("ui", "cannot reach or start the daemon: " + err); std::cerr << "mologram: " << err << "\nsee " << log.todayPath() << " and the daemon log in the same folder\n"; return 1; } mologram::RemoteService remote(socket); remote.start(); // the UI starts at once and shows "connecting" until the daemon answers (no invisible wait) mologram::DaemonSupervisor supervisor(remote, socket, extra); // relaunches a daemon that went away, every second (D-13) opt.socketPath = socket; mologram::ui::MologramApp app(cfg, remote, opt); int rc = app.run(); if (!app.restartAccount().empty()) restartOnAccount(argv, argc, app.restartAccount()); return rc; }