#include #include #include "../include/climanager.h" #include "../include/cmdmanager.h" #include "../include/config.h" #include #include #include #include using boost::lexical_cast; using boost::asio::buffer; using namespace std; namespace CmdManager { QMLHandler *qmlHandler; map cmdmap; } // namespace CmdManager void CmdManager::init() { cmdmap["error"] = &CmdManager::handleError; cmdmap["status"] = &CmdManager::handleStatus; cmdmap["close"] = &CmdManager::handleClose; cmdmap["list"] = &CmdManager::handleList; 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::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) { 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().c_str()), boost::filesystem::exists(fileNames[i].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("list"); 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) { // 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().c_str())); } 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().c_str())); } 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); } }