123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210 |
- #include <QDebug>
- #include <QGuiApplication>
- #include "../include/climanager.h"
- #include "../include/cmdmanager.h"
- #include "../include/config.h"
- #include <boost/asio.hpp>
- #include <boost/filesystem.hpp>
- #include <boost/lexical_cast.hpp>
- #include <json/json.h>
- using boost::lexical_cast;
- using boost::asio::buffer;
- using namespace std;
- namespace CmdManager {
- QMLHandler *qmlHandler;
- map<string, void (*)(Json::Value)> cmdmap;
- } // namespace CmdManager
- void CmdManager::init() {
- cmdmap["error"] = &CmdManager::handleError;
- cmdmap["status"] = &CmdManager::handleStatus;
- cmdmap["close"] = &CmdManager::handleClose;
- cmdmap["list"] = &CmdManager::handleList;
- cmdmap["extendedlist"] = &CmdManager::handleExtendedList;
- cmdmap["connect"] = &CmdManager::handleConnect;
- cmdmap["version"] = &CmdManager::handleVersion;
- cmdmap["login"] = &CmdManager::handleLogin;
- cmdmap["signup"] = &CmdManager::handleSignup;
- cmdmap["put"] = &CmdManager::handlePut;
- cmdmap["putdata"] = &CmdManager::handlePutData;
- cmdmap["get"] = &CmdManager::handleGet;
- cmdmap["getdata"] = &CmdManager::handleGetData;
- cmdmap["deleteme"] = &CmdManager::handleDeleteMe;
- cmdmap["deletefile"] = &CmdManager::handleDeleteFile;
- cmdmap["notifications"] = &CmdManager::handleNotifications;
- cmdmap["queue"] = &CmdManager::handleQueue;
- cmdmap["dequeue"] = &CmdManager::handleDequeue;
- }
- void CmdManager::setQmlHandler(QMLHandler *q) { qmlHandler = q; }
- void CmdManager::executeCmd(string cmd, Json::Value root) {
- map<string, void (*)(Json::Value)>::iterator it = cmdmap.find(cmd);
- if (it == cmdmap.end()) {
- return;
- }
- cmdmap[cmd](root);
- }
- void CmdManager::handleError(Json::Value root) { emit qmlHandler->log(root["error"].asString().c_str()); }
- void CmdManager::handleStatus(Json::Value root) { emit qmlHandler->footerSetStatus(root["response"].asString().c_str()); }
- void CmdManager::handleClose(Json::Value root) { CliManager::setProgramActive(false); }
- void CmdManager::handleList(Json::Value root) {
- char sizebuf[256];
- snprintf(sizebuf, 256, "%.2f", 4.2f);
- if (root["accept"] == true) {
- emit qmlHandler->receivingClearFileList();
- // Get the array of file Names
- auto fileNames = root["names"];
- for (int i = 0; i < fileNames.size(); i++) {
- emit qmlHandler->receivingListFile(QString::fromStdString(fileNames[i].asString()), QString::fromStdString(sizebuf), QString("Decryptable"),
- boost::filesystem::exists(fileNames[i].asString()));
- }
- } else {
- emit qmlHandler->log(root["error"].asString().c_str());
- }
- }
- void CmdManager::handleExtendedList(Json::Value root) {
- char sizebuf[256];
- if (root["accept"] == true) {
- emit qmlHandler->receivingClearFileList();
- // Get the array of file Names
- auto files = root["files"];
- for (Json::Value f : files) {
- snprintf(sizebuf, 256, "%.2f", f["size"].asFloat());
- emit qmlHandler->receivingListFile(QString::fromStdString(f["name"].asString()), QString::fromStdString(sizebuf),
- QString::fromStdString(f["encrypted"].asString()), boost::filesystem::exists(f["name"].asString()));
- }
- } else {
- emit qmlHandler->log(root["error"].asString().c_str());
- }
- }
- void CmdManager::handleConnect(Json::Value root) {
- if (!root["accept"].asBool()) {
- emit qmlHandler->ipPopupSetStatus(root["error"].asString().c_str());
- emit qmlHandler->ipPopupEnableConnectButton();
- }
- }
- void CmdManager::handleVersion(Json::Value root) {
- if (root["accept"] == true) {
- emit qmlHandler->ipPopupClose();
- emit qmlHandler->loginSignupPopupOpen();
- } else {
- QString errorMessage =
- QString::fromStdString(string("Version mismatch: \nClient: " + root["clientversion"].asString() + "\nServer: " + root["serverversion"].asString()));
- emit qmlHandler->ipPopupSetStatus(errorMessage);
- emit qmlHandler->ipPopupEnableConnectButton();
- }
- }
- void CmdManager::handleLogin(Json::Value root) {
- if (root["accept"] == true) {
- emit qmlHandler->loginSignupPopupClose();
- CliManager::writeToCli("extendedlist");
- qmlHandler->loadSettingsToGUI();
- } else {
- emit qmlHandler->loginSetStatus(root["error"].asString().c_str());
- emit qmlHandler->loginEnableLoginButton();
- }
- }
- void CmdManager::handleSignup(Json::Value root) {
- if (root["accept"] == true) {
- emit qmlHandler->loginSignupPopupClose();
- } else {
- emit qmlHandler->signupSetStatus(root["error"].asString().c_str());
- emit qmlHandler->signupEnableRegisterButton();
- }
- }
- void CmdManager::handlePut(Json::Value root) {
- if (root["accept"] == false) {
- QString errorMessage = QString::fromStdString(string("Error when uploading file " + root["file"].asString() + ":\n" + root["error"].asString()));
- emit qmlHandler->log(errorMessage);
- }
- }
- void CmdManager::handlePutData(Json::Value root) {
- // TODO: Show speed and handle Error
- }
- void CmdManager::handleGet(Json::Value root) {
- if (root["accept"] == false) {
- QString errorMessage = QString::fromStdString(string("Error when downloading file " + root["file"].asString() + ":\n" + root["error"].asString()));
- emit qmlHandler->log(errorMessage);
- } else {
- string fileName = root["file"].asString();
- // TODO: Only do this in getdata when remaining is 0 (when the file is fully downloaded) - maybe set text to "downloading.." in between
- emit qmlHandler->receivingDisableDownloadButton(QString::fromStdString(fileName));
- }
- }
- void CmdManager::handleGetData(Json::Value root) {
- // TODO: Show speed and handle Error
- }
- void CmdManager::handleDeleteMe(Json::Value root) {
- if (root["accept"] == true) {
- qmlHandler->setRestart(true);
- emit qmlHandler->closeWindow();
- } else {
- QString errorMessage = QString::fromStdString(root["error"].asString());
- emit qmlHandler->deleteMePopupSetStatus(errorMessage);
- }
- }
- void CmdManager::handleDeleteFile(Json::Value root) {
- emit qmlHandler->receivingCloseConfirmDeletePopup();
- if (root["accept"] == false) {
- QString errorMessage = QString::fromStdString(string("Error when deleting file " + root["file"].asString() + ":\n" + root["error"].asString()));
- emit qmlHandler->log(errorMessage);
- } else {
- QString message = QString::fromStdString(string("Deleted file " + root["file"].asString() + " from the server!"));
- emit qmlHandler->log(message);
- }
- }
- void CmdManager::handleNotifications(Json::Value root) {
- if (root["accept"] == true) {
- if (!root["messages"].isNull()) {
- // Get the array of notifications
- auto notifications = root["messages"];
- for (int i = 0; i < notifications.size(); i++) {
- emit qmlHandler->notification(QString::fromStdString(notifications[i].asString()));
- }
- if (notifications.size() > 1)
- emit qmlHandler->showDesktopNotification("Covert Channel - New Notifications",
- "You have multiple new notifications. Open the program to see them.");
- else if (notifications.size() == 1)
- emit qmlHandler->showDesktopNotification("Covert Channel - New Notification", QString::fromStdString(notifications[0].asString()));
- }
- } else {
- emit qmlHandler->log(root["error"].asString().c_str());
- }
- }
- void CmdManager::handleQueue(Json::Value root) {
- if (root["accept"] == false) {
- QString errorMessage = QString::fromStdString(string("Error when queueing file " + root["file"].asString() + ":\n" + root["error"].asString()));
- emit qmlHandler->log(errorMessage);
- }
- }
- void CmdManager::handleDequeue(Json::Value root) {
- if (root["accept"] == false) {
- QString errorMessage = QString::fromStdString(string("Error when dequeueing file " + root["file"].asString() + ":\n" + root["error"].asString()));
- emit qmlHandler->log(errorMessage);
- }
- }
|