cmdmanager.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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["extendedlist"] = &CmdManager::handleExtendedList;
  23. cmdmap["connect"] = &CmdManager::handleConnect;
  24. cmdmap["version"] = &CmdManager::handleVersion;
  25. cmdmap["login"] = &CmdManager::handleLogin;
  26. cmdmap["signup"] = &CmdManager::handleSignup;
  27. cmdmap["put"] = &CmdManager::handlePut;
  28. cmdmap["putdata"] = &CmdManager::handlePutData;
  29. cmdmap["get"] = &CmdManager::handleGet;
  30. cmdmap["getdata"] = &CmdManager::handleGetData;
  31. cmdmap["deleteme"] = &CmdManager::handleDeleteMe;
  32. cmdmap["deletefile"] = &CmdManager::handleDeleteFile;
  33. cmdmap["notifications"] = &CmdManager::handleNotifications;
  34. cmdmap["queue"] = &CmdManager::handleQueue;
  35. cmdmap["dequeue"] = &CmdManager::handleDequeue;
  36. }
  37. void CmdManager::setQmlHandler(QMLHandler *q) { qmlHandler = q; }
  38. void CmdManager::executeCmd(string cmd, Json::Value root) {
  39. map<string, void (*)(Json::Value)>::iterator it = cmdmap.find(cmd);
  40. if (it == cmdmap.end()) {
  41. return;
  42. }
  43. cmdmap[cmd](root);
  44. }
  45. void CmdManager::handleError(Json::Value root) { emit qmlHandler->log(root["error"].asString().c_str()); }
  46. void CmdManager::handleStatus(Json::Value root) { emit qmlHandler->footerSetStatus(root["response"].asString().c_str()); }
  47. void CmdManager::handleClose(Json::Value root) { CliManager::setProgramActive(false); }
  48. void CmdManager::handleList(Json::Value root) {
  49. char sizebuf[256];
  50. snprintf(sizebuf, 256, "%.2f", 4.2f);
  51. if (root["accept"] == true) {
  52. emit qmlHandler->receivingClearFileList();
  53. // Get the array of file Names
  54. auto fileNames = root["names"];
  55. for (int i = 0; i < fileNames.size(); i++) {
  56. emit qmlHandler->receivingListFile(QString::fromStdString(fileNames[i].asString()), QString::fromStdString(sizebuf), QString("Decryptable"),
  57. boost::filesystem::exists(fileNames[i].asString()));
  58. }
  59. } else {
  60. emit qmlHandler->log(root["error"].asString().c_str());
  61. }
  62. }
  63. void CmdManager::handleExtendedList(Json::Value root) {
  64. char sizebuf[256];
  65. if (root["accept"] == true) {
  66. emit qmlHandler->receivingClearFileList();
  67. // Get the array of file Names
  68. auto files = root["files"];
  69. for (Json::Value f : files) {
  70. snprintf(sizebuf, 256, "%.2f", f["size"].asFloat());
  71. emit qmlHandler->receivingListFile(QString::fromStdString(f["name"].asString()), QString::fromStdString(sizebuf),
  72. QString::fromStdString(f["encrypted"].asString()), boost::filesystem::exists(f["name"].asString()));
  73. }
  74. } else {
  75. emit qmlHandler->log(root["error"].asString().c_str());
  76. }
  77. }
  78. void CmdManager::handleConnect(Json::Value root) {
  79. if (!root["accept"].asBool()) {
  80. emit qmlHandler->ipPopupSetStatus(root["error"].asString().c_str());
  81. emit qmlHandler->ipPopupEnableConnectButton();
  82. }
  83. }
  84. void CmdManager::handleVersion(Json::Value root) {
  85. if (root["accept"] == true) {
  86. emit qmlHandler->ipPopupClose();
  87. emit qmlHandler->loginSignupPopupOpen();
  88. } else {
  89. QString errorMessage =
  90. QString::fromStdString(string("Version mismatch: \nClient: " + root["clientversion"].asString() + "\nServer: " + root["serverversion"].asString()));
  91. emit qmlHandler->ipPopupSetStatus(errorMessage);
  92. emit qmlHandler->ipPopupEnableConnectButton();
  93. }
  94. }
  95. void CmdManager::handleLogin(Json::Value root) {
  96. if (root["accept"] == true) {
  97. emit qmlHandler->loginSignupPopupClose();
  98. CliManager::writeToCli("extendedlist");
  99. qmlHandler->loadSettingsToGUI();
  100. } else {
  101. emit qmlHandler->loginSetStatus(root["error"].asString().c_str());
  102. emit qmlHandler->loginEnableLoginButton();
  103. }
  104. }
  105. void CmdManager::handleSignup(Json::Value root) {
  106. if (root["accept"] == true) {
  107. emit qmlHandler->loginSignupPopupClose();
  108. } else {
  109. emit qmlHandler->signupSetStatus(root["error"].asString().c_str());
  110. emit qmlHandler->signupEnableRegisterButton();
  111. }
  112. }
  113. void CmdManager::handlePut(Json::Value root) {
  114. if (root["accept"] == false) {
  115. QString errorMessage = QString::fromStdString(string("Error when uploading file " + root["file"].asString() + ":\n" + root["error"].asString()));
  116. emit qmlHandler->log(errorMessage);
  117. }
  118. }
  119. void CmdManager::handlePutData(Json::Value root) {
  120. // TODO: Show speed and handle Error
  121. }
  122. void CmdManager::handleGet(Json::Value root) {
  123. if (root["accept"] == false) {
  124. QString errorMessage = QString::fromStdString(string("Error when downloading file " + root["file"].asString() + ":\n" + root["error"].asString()));
  125. emit qmlHandler->log(errorMessage);
  126. } else {
  127. string fileName = root["file"].asString();
  128. // TODO: Only do this in getdata when remaining is 0 (when the file is fully downloaded) - maybe set text to "downloading.." in between
  129. emit qmlHandler->receivingDisableDownloadButton(QString::fromStdString(fileName));
  130. }
  131. }
  132. void CmdManager::handleGetData(Json::Value root) {
  133. // TODO: Show speed and handle Error
  134. }
  135. void CmdManager::handleDeleteMe(Json::Value root) {
  136. if (root["accept"] == true) {
  137. qmlHandler->setRestart(true);
  138. emit qmlHandler->closeWindow();
  139. } else {
  140. QString errorMessage = QString::fromStdString(root["error"].asString());
  141. emit qmlHandler->deleteMePopupSetStatus(errorMessage);
  142. }
  143. }
  144. void CmdManager::handleDeleteFile(Json::Value root) {
  145. emit qmlHandler->receivingCloseConfirmDeletePopup();
  146. if (root["accept"] == false) {
  147. QString errorMessage = QString::fromStdString(string("Error when deleting file " + root["file"].asString() + ":\n" + root["error"].asString()));
  148. emit qmlHandler->log(errorMessage);
  149. } else {
  150. QString message = QString::fromStdString(string("Deleted file " + root["file"].asString() + " from the server!"));
  151. emit qmlHandler->log(message);
  152. }
  153. }
  154. void CmdManager::handleNotifications(Json::Value root) {
  155. if (root["accept"] == true) {
  156. if (!root["messages"].isNull()) {
  157. // Get the array of notifications
  158. auto notifications = root["messages"];
  159. for (int i = 0; i < notifications.size(); i++) {
  160. emit qmlHandler->notification(QString::fromStdString(notifications[i].asString()));
  161. }
  162. if (notifications.size() > 1)
  163. emit qmlHandler->showDesktopNotification("Covert Channel - New Notifications",
  164. "You have multiple new notifications. Open the program to see them.");
  165. else if (notifications.size() == 1)
  166. emit qmlHandler->showDesktopNotification("Covert Channel - New Notification", QString::fromStdString(notifications[0].asString()));
  167. }
  168. } else {
  169. emit qmlHandler->log(root["error"].asString().c_str());
  170. }
  171. }
  172. void CmdManager::handleQueue(Json::Value root) {
  173. if (root["accept"] == false) {
  174. QString errorMessage = QString::fromStdString(string("Error when queueing file " + root["file"].asString() + ":\n" + root["error"].asString()));
  175. emit qmlHandler->log(errorMessage);
  176. }
  177. }
  178. void CmdManager::handleDequeue(Json::Value root) {
  179. if (root["accept"] == false) {
  180. QString errorMessage = QString::fromStdString(string("Error when dequeueing file " + root["file"].asString() + ":\n" + root["error"].asString()));
  181. emit qmlHandler->log(errorMessage);
  182. }
  183. }