Cerberus/commands.cpp
2025-01-28 21:22:41 +03:00

114 lines
3.7 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include "commands.hpp"
#include "config.hpp"
#include "webserver.hpp"
#include "sound.hpp"
#include <iostream>
#include <sstream>
#include <iterator>
#include <vector>
#include <cstdio>
static std::vector<std::string> splitTokens(const std::string &line) {
std::istringstream iss(line);
return { std::istream_iterator<std::string>(iss),
std::istream_iterator<std::string>() };
}
static void generateKey(AppConfig &config) {
config.key.resize(32);
FILE* f = fopen("/dev/urandom", "rb");
if (!f) {
std::cerr << CLR_RED "[nick] Не удалось открыть /dev/urandom\n" CLR_RESET;
return;
}
fread(config.key.data(), 1, 32, f);
fclose(f);
std::cout << CLR_GREEN "[nick] 256-битный ключ сгенерирован!\n" CLR_RESET;
}
void processCommand(const std::string &input, AppConfig &config) {
if (input.empty()) return;
auto tokens = splitTokens(input);
if (tokens.empty()) return;
std::string cmd = tokens[0];
if (cmd == "nick") {
if (tokens.size() < 2) {
std::cout << CLR_YELLOW "[nick] Доступно: set <name>, generatekey\n" CLR_RESET;
return;
}
std::string sub = tokens[1];
if (sub == "set") {
if (tokens.size() < 3) {
std::cout << CLR_YELLOW "[nick] Использование: nick set <usernick>\n" CLR_RESET;
return;
}
std::string newNick;
for (size_t i = 2; i < tokens.size(); i++) {
if (i > 2) newNick += " ";
newNick += tokens[i];
}
config.nickname = newNick;
std::cout << CLR_GREEN "[nick] Установлен ник: " << newNick << "\n" CLR_RESET;
}
else if (sub == "generatekey") {
generateKey(config);
}
else {
std::cout << CLR_YELLOW "[nick] Неизвестная подкоманда: " << sub << "\n" CLR_RESET;
}
}
else if (cmd == "web") {
if (tokens.size() < 2) {
std::cout << CLR_YELLOW "[web] Доступно: start, connect pm|server <ip>, stop\n" CLR_RESET;
return;
}
std::string sub = tokens[1];
if (sub == "start") {
webServerStart(config);
}
else if (sub == "connect") {
if (tokens.size() < 4) {
std::cout << CLR_YELLOW "[web] Использование: web connect <pm|server> <ip>\n" CLR_RESET;
return;
}
std::string ctype = tokens[2];
std::string ip = tokens[3];
webServerConnect(config, ctype, ip);
}
else if (sub == "stop") {
webServerStop(config);
}
else {
std::cout << CLR_YELLOW "[web] Неизвестная подкоманда: " << sub << "\n" CLR_RESET;
}
}
else if (cmd == "sound") {
if (tokens.size() < 2) {
std::cout << CLR_YELLOW "[sound] Доступно: find, lose\n" CLR_RESET;
return;
}
std::string sub = tokens[1];
if (sub == "find") {
soundFind(config);
}
else if (sub == "lose") {
soundLose(config);
}
else {
std::cout << CLR_YELLOW "[sound] Неизвестная подкоманда: " << sub << "\n" CLR_RESET;
}
}
else if (cmd == "exit") {
std::cout << CLR_CYAN "[cli] Завершаем работу по команде 'exit'\n" CLR_RESET;
exit(0);
}
else {
std::cout << CLR_RED "Неизвестная команда: " << cmd << CLR_RESET << "\n";
}
}