cmdmanager.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. #include <QDebug>
  2. #include <QGuiApplication>
  3. #include "../include/climanager.h"
  4. #include "../include/cmdmanager.h"
  5. #include "../include/config.h"
  6. #include <boost/asio.hpp>
  7. #include <boost/filesystem.hpp>
  8. #include <boost/lexical_cast.hpp>
  9. #include <json/json.h>
  10. using boost::lexical_cast;
  11. using boost::asio::buffer;
  12. using namespace std;
  13. namespace CmdManager {
  14. QMLHandler *qmlHandler;
  15. map<string, void (*)(Json::Value)> cmdmap;
  16. } // namespace CmdManager
  17. void CmdManager::init() {
  18. cmdmap["error"] = &CmdManager::handleError;
  19. cmdmap["status"] = &CmdManager::handleStatus;
  20. cmdmap["close"] = &CmdManager::handleClose;
  21. cmdmap["list"] = &CmdManager::handleList;
  22. cmdmap["connect"] = &CmdManager::handleConnect;
  23. cmdmap["version"] = &CmdManager::handleVersion;
  24. cmdmap["login"] = &CmdManager::handleLogin;
  25. cmdmap["signup"] = &CmdManager::handleSignup;
  26. cmdmap["put"] = &CmdManager::handlePut;
  27. cmdmap["putdata"] = &CmdManager::handlePutData;
  28. cmdmap["get"] = &CmdManager::handleGet;
  29. cmdmap["getdata"] = &CmdManager::handleGetData;
  30. cmdmap["deleteme"] = &CmdManager::handleDeleteMe;
  31. cmdmap["deletefile"] = &CmdManager::handleDeleteFile;
  32. cmdmap["notifications"] = &CmdManager::handleNotifications;
  33. cmdmap["queue"] = &CmdManager::handleQueue;
  34. cmdmap["dequeue"] = &CmdManager::handleDequeue;
  35. }
  36. void CmdManager::setQmlHandler(QMLHandler *q) { qmlHandler = q; }
  37. void CmdManager::executeCmd(string cmd, Json::Value root) {
  38. map<string, void (*)(Json::Value)>::iterator it = cmdmap.find(cmd);
  39. if (it == cmdmap.end()) {
  40. return;
  41. }
  42. cmdmap[cmd](root);
  43. }
  44. void CmdManager::handleError(Json::Value root) { emit qmlHandler->log(root["error"].asString().c_str()); }
  45. void CmdManager::handleStatus(Json::Value root) { emit qmlHandler->footerSetStatus(root["response"].asString().c_str()); }
  46. void CmdManager::handleClose(Json::Value root) { CliManager::setProgramActive(false); }
  47. void CmdManager::handleList(Json::Value root) {
  48. if (root["accept"] == true) {
  49. emit qmlHandler->receivingClearFileList();
  50. // Get the array of file Names
  51. auto fileNames = root["names"];
  52. for (int i = 0; i < fileNames.size(); i++) {
  53. emit qmlHandler->receivingListFile(QString::fromStdString(fileNames[i].asString().c_str()), boost::filesystem::exists(fileNames[i].asString()));
  54. }
  55. } else {
  56. emit qmlHandler->log(root["error"].asString().c_str());
  57. }
  58. }
  59. void CmdManager::handleConnect(Json::Value root) {
  60. if (!root["accept"].asBool()) {
  61. emit qmlHandler->ipPopupSetStatus(root["error"].asString().c_str());
  62. emit qmlHandler->ipPopupEnableConnectButton();
  63. }
  64. }
  65. void CmdManager::handleVersion(Json::Value root) {
  66. if (root["accept"] == true) {
  67. emit qmlHandler->ipPopupClose();
  68. emit qmlHandler->loginSignupPopupOpen();
  69. } else {
  70. QString errorMessage =
  71. QString::fromStdString(string("Version mismatch: \nClient: " + root["clientversion"].asString() + "\nServer: " + root["serverversion"].asString()));
  72. emit qmlHandler->ipPopupSetStatus(errorMessage);
  73. emit qmlHandler->ipPopupEnableConnectButton();
  74. }
  75. }
  76. void CmdManager::handleLogin(Json::Value root) {
  77. if (root["accept"] == true) {
  78. emit qmlHandler->loginSignupPopupClose();
  79. CliManager::writeToCli("list");
  80. qmlHandler->loadSettingsToGUI();
  81. } else {
  82. emit qmlHandler->loginSetStatus(root["error"].asString().c_str());
  83. emit qmlHandler->loginEnableLoginButton();
  84. }
  85. }
  86. void CmdManager::handleSignup(Json::Value root) {
  87. if (root["accept"] == true) {
  88. emit qmlHandler->loginSignupPopupClose();
  89. } else {
  90. emit qmlHandler->signupSetStatus(root["error"].asString().c_str());
  91. emit qmlHandler->signupEnableRegisterButton();
  92. }
  93. }
  94. void CmdManager::handlePut(Json::Value root) {
  95. if (root["accept"] == false) {
  96. QString errorMessage = QString::fromStdString(string("Error when uploading file " + root["file"].asString() + ":\n" + root["error"].asString()));
  97. emit qmlHandler->log(errorMessage);
  98. }
  99. }
  100. void CmdManager::handlePutData(Json::Value root) {
  101. // TODO: Show speed and handle Error
  102. }
  103. void CmdManager::handleGet(Json::Value root) {
  104. if (root["accept"] == false) {
  105. QString errorMessage = QString::fromStdString(string("Error when downloading file " + root["file"].asString() + ":\n" + root["error"].asString()));
  106. emit qmlHandler->log(errorMessage);
  107. } else {
  108. string fileName = root["file"].asString();
  109. // TODO: Only do this in getdata when remaining is 0 (when the file is fully downloaded) - maybe set text to "downloading.." in between
  110. emit qmlHandler->receivingDisableDownloadButton(QString::fromStdString(fileName));
  111. }
  112. }
  113. void CmdManager::handleGetData(Json::Value root) {
  114. // TODO: Show speed and handle Error
  115. }
  116. void CmdManager::handleDeleteMe(Json::Value root) {
  117. if (root["accept"] == true) {
  118. qmlHandler->setRestart(true);
  119. emit qmlHandler->closeWindow();
  120. } else {
  121. QString errorMessage = QString::fromStdString(root["error"].asString());
  122. emit qmlHandler->deleteMePopupSetStatus(errorMessage);
  123. }
  124. }
  125. void CmdManager::handleDeleteFile(Json::Value root) {
  126. emit qmlHandler->receivingCloseConfirmDeletePopup();
  127. if (root["accept"] == false) {
  128. QString errorMessage = QString::fromStdString(string("Error when deleting file " + root["file"].asString() + ":\n" + root["error"].asString()));
  129. emit qmlHandler->log(errorMessage);
  130. } else {
  131. QString message = QString::fromStdString(string("Deleted file " + root["file"].asString() + " from the server!"));
  132. emit qmlHandler->log(message);
  133. }
  134. }
  135. void CmdManager::handleNotifications(Json::Value root) {
  136. if (root["accept"] == true) {
  137. // Get the array of notifications
  138. auto notifications = root["messages"];
  139. for (int i = 0; i < notifications.size(); i++) {
  140. emit qmlHandler->notification(QString::fromStdString(notifications[i].asString().c_str()));
  141. }
  142. if (notifications.size() > 1)
  143. emit qmlHandler->showDesktopNotification("Covert Channel - New Notifications",
  144. "You have multiple new notifications. Open the program to see them.");
  145. else if (notifications.size() == 1)
  146. emit qmlHandler->showDesktopNotification("Covert Channel - New Notification", QString::fromStdString(notifications[0].asString().c_str()));
  147. } else {
  148. emit qmlHandler->log(root["error"].asString().c_str());
  149. }
  150. }
  151. void CmdManager::handleQueue(Json::Value root) {
  152. if (root["accept"] == false) {
  153. QString errorMessage = QString::fromStdString(string("Error when queueing file " + root["file"].asString() + ":\n" + root["error"].asString()));
  154. emit qmlHandler->log(errorMessage);
  155. }
  156. }
  157. void CmdManager::handleDequeue(Json::Value root) {
  158. if (root["accept"] == false) {
  159. QString errorMessage = QString::fromStdString(string("Error when dequeueing file " + root["file"].asString() + ":\n" + root["error"].asString()));
  160. emit qmlHandler->log(errorMessage);
  161. }
  162. }