JsonCommander.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743
  1. #include "../include/JsonCommander.h"
  2. #include "../include/Config.h"
  3. #include "../include/Notifications.h"
  4. #include "../include/Queue.h"
  5. #include "../include/UserManager.h"
  6. #include "../include/base64.h"
  7. JsonCommander::JsonCommander(FileManager &fileManager) : fileManager(fileManager) {
  8. commandsMap["status"] = &JsonCommander::executeStatus;
  9. commandsMap["close"] = &JsonCommander::executeClose;
  10. commandsMap["list"] = &JsonCommander::executeList;
  11. commandsMap["listdata"] = &JsonCommander::executeListData;
  12. commandsMap["put"] = &JsonCommander::executePut;
  13. commandsMap["putdata"] = &JsonCommander::executePutdata;
  14. commandsMap["get"] = &JsonCommander::executeGet;
  15. commandsMap["getdata"] = &JsonCommander::executeGetdata;
  16. commandsMap["head"] = &JsonCommander::executeHead;
  17. commandsMap["deleteme"] = &JsonCommander::executeDeleteMe;
  18. commandsMap["deletefile"] = &JsonCommander::executeDeleteFile;
  19. commandsMap["extendedstatus"] = &JsonCommander::executeExtendedStatus;
  20. commandsMap["notifications"] = &JsonCommander::executeNotifications;
  21. commandsMap["extendedlist"] = &JsonCommander::executeExtendedList;
  22. commandsMap["extendedlistdata"] = &JsonCommander::executeExtendedListData;
  23. commandsMap["queue"] = &JsonCommander::executeQueue;
  24. commandsMap["dequeue"] = &JsonCommander::executeDequeue;
  25. }
  26. JsonCommander::~JsonCommander() {}
  27. JsonCommander::Response JsonCommander::execute(const Json::Value &message) {
  28. JsonCommander::Response response;
  29. Response (JsonCommander::*commandExecutor)(const Json::Value &) = commandsMap[message["command"].asString()];
  30. if (commandExecutor != nullptr) {
  31. response = (this->*commandExecutor)(message);
  32. } else {
  33. // command does not exist
  34. response.action = close;
  35. }
  36. return response;
  37. }
  38. JsonCommander::Response JsonCommander::executeStatus(const Json::Value &message) {
  39. JsonCommander::Response response;
  40. response.action = send;
  41. response.json["command"] = "status";
  42. // answer a real status message
  43. std::string status;
  44. if (this->fileManager.isUploading() && this->fileManager.isDownloading()) {
  45. status = "download and upload running";
  46. } else if (this->fileManager.isUploading()) {
  47. status = "upload running";
  48. } else if (this->fileManager.isDownloading()) {
  49. status = "download running";
  50. } else {
  51. status = "ok";
  52. }
  53. response.json["response"] = status;
  54. return response;
  55. }
  56. JsonCommander::Response JsonCommander::executeClose(const Json::Value &message) {
  57. JsonCommander::Response response;
  58. response.action = closeAndSend;
  59. response.json["command"] = "close";
  60. response.json["response"] = "bye";
  61. return response;
  62. }
  63. JsonCommander::Response JsonCommander::executeList(const Json::Value &message) {
  64. JsonCommander::Response response;
  65. response.action = send;
  66. response.json["command"] = "list";
  67. int chunks;
  68. if (fileManager.getRemainingListChunks() > 0) {
  69. response.json["accept"] = false;
  70. response.json["chunks"] = -1;
  71. response.json["items"] = -1;
  72. response.json["error"] = "there is already an open list command";
  73. } else if ((chunks = fileManager.openList()) == -1) { // TODO do we need to check? maybe. Think about it
  74. response.json["accept"] = false;
  75. response.json["chunks"] = -1;
  76. response.json["items"] = -1;
  77. response.json["error"] = "there is a filename which is too long";
  78. } else {
  79. response.json["accept"] = true;
  80. response.json["chunks"] = chunks;
  81. response.json["items"] = fileManager.getListSize();
  82. response.json["error"] = "";
  83. }
  84. return response;
  85. }
  86. JsonCommander::Response JsonCommander::executeListData(const Json::Value &message) {
  87. JsonCommander::Response response;
  88. response.action = send;
  89. response.json["command"] = "listdata";
  90. Json::Value array;
  91. const int remainingListchunks = fileManager.getRemainingListChunks();
  92. if (!message["chunk"].isInt() || !message["cancel"].isBool()) {
  93. response.action = closeAndSend;
  94. response.json["cancel"] = true;
  95. response.json["remaining"] = -1;
  96. response.json["names"] = Json::arrayValue;
  97. response.json["error"] = "incorrect listdata command request";
  98. } else if (remainingListchunks == 0) {
  99. response.json["cancel"] = true;
  100. response.json["remaining"] = -1;
  101. response.json["names"] = Json::arrayValue;
  102. response.json["error"] = "there are no chunks to send";
  103. } else if (message["cancel"].asBool()) {
  104. response.json["cancel"] = true;
  105. response.json["remaining"] = message["chunk"].asInt(); // so it can be associated to the request
  106. response.json["names"] = Json::arrayValue;
  107. response.json["error"] = "";
  108. fileManager.cancelList();
  109. } else if (remainingListchunks - 1 != message["chunk"].asInt()) {
  110. response.action = closeAndSend;
  111. response.json["cancel"] = true;
  112. response.json["remaining"] = -1;
  113. response.json["names"] = Json::arrayValue;
  114. response.json["error"] = "wrong chunk number";
  115. } else {
  116. std::vector<std::string> v = fileManager.getNextChunkFromList();
  117. for (int i = 0; i < v.size(); i++)
  118. array.append(v.at(i));
  119. response.json["remaining"] = message["chunk"].asInt();
  120. response.json["cancel"] = false;
  121. response.json["names"] = array;
  122. response.json["error"] = "";
  123. }
  124. return response;
  125. }
  126. JsonCommander::Response JsonCommander::executePut(const Json::Value &message) {
  127. JsonCommander::Response response;
  128. response.json["command"] = "put";
  129. response.json["file"] = message["file"].asString();
  130. if (!message["file"].isString() || !message["size"].isInt() || !message["chunks"].isInt()) {
  131. // if request is incomplete close connection
  132. response.action = closeAndSend;
  133. response.json["accept"] = false;
  134. response.json["error"] = "incorrect put command request";
  135. } else if (fileManager.isUploading()) {
  136. // if an upload is alread running deny request
  137. response.action = send;
  138. response.json["accept"] = false;
  139. response.json["error"] = "upload already running";
  140. } else if (message["chunks"].asInt() <= 0) {
  141. response.action = send;
  142. response.json["accept"] = false;
  143. response.json["error"] = "there must be at least one chunk";
  144. } else if (fileManager.checkFilename(message["file"].asString())) {
  145. // accept request
  146. response.action = send;
  147. bool opened = fileManager.openPutFile(message["file"].asString());
  148. if (opened) {
  149. response.json["accept"] = true;
  150. response.json["error"] = "";
  151. this->putFileReceived = message["chunks"].asInt();
  152. this->putSize = message["chunks"].asInt();
  153. } else {
  154. response.json["accept"] = false;
  155. response.json["error"] = "file already exists";
  156. }
  157. } else {
  158. // deny request if file name is not valid
  159. response.action = send;
  160. response.json["accept"] = false;
  161. response.json["error"] = "invalid file name";
  162. }
  163. return response;
  164. }
  165. JsonCommander::Response JsonCommander::executePutdata(const Json::Value &message) {
  166. JsonCommander::Response response;
  167. response.json["command"] = "putdata";
  168. response.json["file"] = message["file"].asString();
  169. response.json["received"] = message["remaining"].asInt();
  170. if (!message["file"].isString() || !message["data"].isString() || !message["remaining"].isInt() || !message["cancel"].isBool()) {
  171. // if request is incomplete close connection
  172. response.action = closeAndSend;
  173. response.json["cancel"] = true;
  174. response.json["error"] = "incorrect putdata command request";
  175. this->fileManager.cancelPut();
  176. } else if (!fileManager.isUploading()) {
  177. // no upload running -> command
  178. response.action = send;
  179. response.json["cancel"] = true;
  180. response.json["error"] = "no upload running";
  181. } else if (message["cancel"].asBool()) {
  182. response.action = send;
  183. response.json["cancel"] = true;
  184. response.json["error"] = "";
  185. this->fileManager.cancelPut();
  186. } else if (message["file"].asString().compare(fileManager.getPutBaseFileName()) == 0) {
  187. if (--this->putFileReceived == message["remaining"].asInt()) {
  188. // accept request
  189. response.action = send;
  190. response.json["cancel"] = false;
  191. response.json["error"] = "";
  192. const std::vector<char> data = base64::decode<std::vector<char>>(message["data"].asString());
  193. fileManager.writePut(data);
  194. this->putFileReceived = message["remaining"].asInt();
  195. if (this->putFileReceived == 0) {
  196. // close file after last chunk was received
  197. this->fileManager.closePutFile();
  198. }
  199. } else {
  200. // wrong remaining number
  201. response.action = send;
  202. response.json["cancel"] = true;
  203. response.json["error"] = "wrong remaining number";
  204. this->fileManager.cancelPut();
  205. }
  206. } else {
  207. // wrong file name
  208. response.action = send;
  209. response.json["cancel"] = true;
  210. response.json["error"] = "another file was already being uploaded";
  211. this->fileManager.cancelPut();
  212. }
  213. return response;
  214. }
  215. JsonCommander::Response JsonCommander::executeGet(const Json::Value &message) {
  216. JsonCommander::Response response;
  217. response.json["command"] = "get";
  218. response.json["file"] = message["file"].asString();
  219. if (!message["file"].isString()) {
  220. // if request is incomplete close connection
  221. response.action = closeAndSend;
  222. response.json["accept"] = false;
  223. response.json["chunks"] = -1;
  224. response.json["error"] = "incorrect get command request";
  225. } else if (fileManager.isDownloading()) {
  226. // if an upload is alread running deny request
  227. response.action = send;
  228. response.json["accept"] = false;
  229. response.json["chunks"] = -1;
  230. response.json["error"] = "download already running";
  231. } else if (fileManager.checkFilename(message["file"].asString())) {
  232. // accept request
  233. response.action = send;
  234. std::pair<bool, int> opened = fileManager.openGetFile(message["file"].asString());
  235. if (opened.first) {
  236. this->getFileRemaining = opened.second;
  237. this->getSize = opened.second;
  238. response.json["accept"] = true;
  239. response.json["chunks"] = this->getFileRemaining;
  240. response.json["error"] = "";
  241. } else {
  242. response.json["accept"] = false;
  243. response.json["chunks"] = -1;
  244. response.json["error"] = "file does not exist";
  245. }
  246. } else {
  247. // deny request if file name is not valid
  248. response.action = send;
  249. response.json["accept"] = false;
  250. response.json["chunks"] = -1;
  251. response.json["error"] = "invalid file name";
  252. }
  253. return response;
  254. }
  255. JsonCommander::Response JsonCommander::executeGetdata(const Json::Value &message) {
  256. JsonCommander::Response response;
  257. response.json["command"] = "getdata";
  258. response.json["file"] = message["file"].asString();
  259. response.json["remaining"] = message["chunk"].asInt();
  260. if (!message["file"].isString() || !message["chunk"].isInt() || !message["cancel"].isBool()) {
  261. // if request is incomplete close connection
  262. response.action = closeAndSend;
  263. response.json["cancel"] = true;
  264. response.json["data"] = "";
  265. response.json["error"] = "incorrect putdata command request";
  266. this->fileManager.closeGetFile();
  267. } else if (!fileManager.isDownloading()) {
  268. // no upload running -> command
  269. response.action = send;
  270. response.json["cancel"] = true;
  271. response.json["data"] = "";
  272. response.json["error"] = "no download running";
  273. } else if (message["cancel"].asBool()) {
  274. response.action = send;
  275. response.json["cancel"] = true;
  276. response.json["data"] = "";
  277. response.json["error"] = "";
  278. this->fileManager.closeGetFile();
  279. } else if (message["file"].asString().compare(fileManager.getGetBaseFileName()) == 0) {
  280. if (--this->getFileRemaining == message["chunk"].asInt()) {
  281. // accept request
  282. response.action = send;
  283. response.json["cancel"] = false;
  284. response.json["error"] = "";
  285. const std::vector<char> data = fileManager.readGet();
  286. response.json["data"] = base64::encode<std::vector<char>>(data);
  287. fileManager.writePut(data);
  288. if (this->getFileRemaining == 0) {
  289. // close file after last chunk was sent
  290. this->fileManager.closeGetFile();
  291. }
  292. } else {
  293. // wrong chunk number
  294. response.action = send;
  295. response.json["cancel"] = true;
  296. response.json["data"] = "";
  297. response.json["error"] = "wrong chunk number";
  298. this->fileManager.closeGetFile();
  299. }
  300. } else {
  301. // wrong file name
  302. response.action = send;
  303. response.json["cancel"] = true;
  304. response.json["data"] = "";
  305. response.json["error"] = "another file was already being downloaded";
  306. this->fileManager.closeGetFile();
  307. }
  308. return response;
  309. }
  310. JsonCommander::Response JsonCommander::executeHead(const Json::Value &message) {
  311. JsonCommander::Response response;
  312. response.action = send;
  313. response.json["command"] = "head";
  314. if (!message["file"].isString()) {
  315. response.json["accept"] = false;
  316. response.json["file"] = "";
  317. response.json["data"] = "";
  318. response.json["error"] = "incorrect head command request";
  319. } else {
  320. std::pair<std::vector<char>, FileManager::Error> res = fileManager.getBytesFromFile(message["file"].asString(), 4);
  321. switch (res.second) {
  322. case FileManager::Error::no_error:
  323. response.json["accept"] = true;
  324. response.json["file"] = message["file"].asString();
  325. response.json["data"] = base64::encode<std::vector<char>>(res.first);
  326. response.json["error"] = "";
  327. break;
  328. case FileManager::Error::no_such_file:
  329. response.json["accept"] = false;
  330. response.json["file"] = message["file"].asString();
  331. response.json["data"] = "";
  332. response.json["error"] = "no such file";
  333. break;
  334. case FileManager::Error::file_too_small:
  335. response.json["accept"] = false;
  336. response.json["file"] = message["file"].asString();
  337. response.json["data"] = "";
  338. response.json["error"] = "file is smaller than specified size";
  339. break;
  340. default:
  341. response.action = closeAndSend;
  342. response.json["accept"] = false;
  343. response.json["file"] = message["file"];
  344. response.json["data"] = "";
  345. response.json["error"] = "internal error. Please fix this!!!";
  346. }
  347. }
  348. return response;
  349. }
  350. JsonCommander::Response JsonCommander::executeDeleteMe(const Json::Value &message) {
  351. JsonCommander::Response response;
  352. response.json["command"] = "deleteme";
  353. if (!message["pass"].isString()) {
  354. response.action = closeAndSend;
  355. response.json["accept"] = false;
  356. response.json["error"] = "incorrect deleteme command request";
  357. } else if (UserManager::deleteUser(currentUser, message["pass"].asString())) {
  358. // success
  359. response.action = closeAndSend;
  360. response.json["accept"] = true;
  361. response.json["error"] = "";
  362. } else {
  363. response.action = send;
  364. response.json["accept"] = false;
  365. response.json["error"] = "wrong password";
  366. }
  367. return response;
  368. }
  369. JsonCommander::Response JsonCommander::testVersion(const Json::Value &message) {
  370. JsonCommander::Response response;
  371. response.json["major"] = this->protocolMajorVersion;
  372. response.json["minor"] = this->protocolMinorVersion;
  373. if (!message["major"].isInt() || !message["minor"].isInt()) {
  374. response.action = closeAndSend;
  375. response.json["accept"] = false;
  376. } else if (message["major"].asInt() == this->protocolMajorVersion && message["minor"].asInt() <= this->protocolMinorVersion) {
  377. response.action = send;
  378. response.json["accept"] = true;
  379. } else {
  380. response.action = closeAndSend;
  381. response.json["accept"] = false;
  382. }
  383. return response;
  384. }
  385. JsonCommander::Response JsonCommander::checkLogin(const Json::Value &message) {
  386. JsonCommander::Response response;
  387. if (!message["login"].isBool() || !message["user"].isString() || !message["pass"].isString() || !message["cancel"].isBool()) {
  388. // invalid login request
  389. response.action = closeAndSend;
  390. response.json["accept"] = false;
  391. response.json["error"] = "invalid login request";
  392. } else if (message["cancel"].asBool()) {
  393. response.action = close;
  394. } else if (message["login"].asBool() && UserManager::isAllowed(message["user"].asString(), message["pass"].asString())) {
  395. // credential check
  396. response.action = send;
  397. response.json["accept"] = true;
  398. response.json["error"] = "";
  399. currentUser = message["user"].asString();
  400. } else if (!message["login"].asBool()) {
  401. // add user. Check if already exists before
  402. if (!UserManager::addUser(message["user"].asString(), message["pass"].asString())) {
  403. response.action = closeAndSend;
  404. response.json["accept"] = false;
  405. response.json["error"] = "user does already exist";
  406. } else {
  407. response.action = send;
  408. response.json["accept"] = true;
  409. response.json["error"] = "";
  410. currentUser = message["user"].asString();
  411. }
  412. } else {
  413. // reject user
  414. response.action = closeAndSend;
  415. response.json["accept"] = false;
  416. response.json["error"] = "wrong username or password";
  417. }
  418. return response;
  419. }
  420. JsonCommander::Response JsonCommander::executeDeleteFile(const Json::Value &message) {
  421. JsonCommander::Response response;
  422. response.json["command"] = "deletefile";
  423. if (!message["file"].isString()) {
  424. response.action = closeAndSend;
  425. response.json["accept"] = false;
  426. response.json["error"] = "incorrect deletefile command request";
  427. response.json["file"] = "";
  428. } else {
  429. response.action = send;
  430. FileManager::Error err = fileManager.deleteFile(message["file"].asString());
  431. switch (err) {
  432. case FileManager::Error::no_error:
  433. response.json["accept"] = true;
  434. response.json["error"] = "";
  435. response.json["file"] = message["file"];
  436. break;
  437. case FileManager::Error::not_allowed:
  438. response.json["accept"] = false;
  439. response.json["error"] = "deleting files is disabled";
  440. response.json["file"] = message["file"];
  441. break;
  442. case FileManager::Error::no_such_file:
  443. response.json["accept"] = false;
  444. response.json["error"] = "no such file";
  445. response.json["file"] = message["file"];
  446. break;
  447. default:
  448. response.action = closeAndSend;
  449. response.json["accept"] = false;
  450. response.json["error"] = "internal error. Please fix this!!!";
  451. response.json["file"] = message["file"];
  452. }
  453. }
  454. return response;
  455. }
  456. JsonCommander::Response JsonCommander::executeExtendedStatus(const Json::Value &message) {
  457. JsonCommander::Response response;
  458. response.json["command"] = "extendedstatus";
  459. // get status from client server transfers
  460. int index = 0;
  461. if (fileManager.isUploading()) {
  462. response.json["transfersclientserver"][index]["upload"] = true;
  463. response.json["transfersclientserver"][index]["file"] = fileManager.getPutBaseFileName();
  464. int progress = 0;
  465. if (this->putSize != 0) {
  466. double d = (double)this->putFileReceived / (double)this->putSize;
  467. progress = (int)(d * 100);
  468. }
  469. response.json["transfersclientserver"][index]["progress"] = progress;
  470. index++;
  471. }
  472. if (fileManager.isDownloading()) {
  473. response.json["transfersclientserver"][index]["upload"] = false;
  474. response.json["transfersclientserver"][index]["file"] = fileManager.getGetBaseFileName();
  475. int progress = 0;
  476. if (this->getSize != 0) {
  477. double d = 1 - ((double)this->getFileRemaining / (double)this->getSize);
  478. progress = (int)(d * 100);
  479. }
  480. response.json["transfersclientserver"][index]["progress"] = progress;
  481. }
  482. // get status from covert channel
  483. index = 0;
  484. if (Queue::channel != nullptr && Queue::channel->isTransferRunning()) {
  485. response.json["transfersserverserver"][index]["type"] = Config::getValue("passiveMode").compare("true") == 0 ? "download" : "upload";
  486. response.json["transfersserverserver"][index]["file"] = Queue::curTransfer();
  487. auto rawprogress = Queue::channel->getProgress();
  488. double d;
  489. if (rawprogress.second == 0)
  490. d = 0.0;
  491. else
  492. d = ((double)rawprogress.first / (double)rawprogress.second);
  493. int progress = (int)(d * 100);
  494. response.json["transfersserverserver"][index]["progress"] = progress;
  495. std::time_t startTime = Queue::channel->getTransferStart();
  496. std::time_t curTime = std::time(nullptr);
  497. std::time_t diffTime = curTime - startTime;
  498. double speed = (double)rawprogress.first / (double)diffTime;
  499. response.json["transfersserverserver"][index]["speed"] = speed;
  500. response.json["transfersserverserver"][index]["method"] = Config::getValue("covertChannelMode");
  501. index++;
  502. }
  503. for (int i = 0; i < Queue::queue.size(); i++) {
  504. response.json["transfersserverserver"][index]["type"] = "queued";
  505. response.json["transfersserverserver"][index]["file"] = Queue::queue[i];
  506. response.json["transfersserverserver"][index]["progress"] = 0;
  507. response.json["transfersserverserver"][index]["speed"] = 0;
  508. response.json["transfersserverserver"][index]["method"] = Config::getValue("covertChannelMode");
  509. index++;
  510. }
  511. response.json["accept"] = true;
  512. response.json["error"] = "";
  513. response.action = send;
  514. return response;
  515. }
  516. JsonCommander::Response JsonCommander::executeNotifications(const Json::Value &message) {
  517. JsonCommander::Response response;
  518. response.json["command"] = "notifications";
  519. Json::Value array;
  520. std::vector<std::string> v = Notifications::getMessages(currentUser);
  521. for (int i = 0; i < v.size(); i++)
  522. array.append(v.at(i));
  523. response.action = send;
  524. response.json["messages"] = array;
  525. response.json["accept"] = true;
  526. response.json["error"] = "";
  527. return response;
  528. }
  529. JsonCommander::Response JsonCommander::executeExtendedList(const Json::Value &message) {
  530. JsonCommander::Response response;
  531. response.action = send;
  532. response.json["command"] = "extendedlist";
  533. int chunks;
  534. if (fileManager.getRemainingExtendedListChunks() > 0) {
  535. response.json["accept"] = false;
  536. response.json["chunks"] = -1;
  537. response.json["items"] = -1;
  538. response.json["error"] = "there is already an open extendedlist command";
  539. } else if ((chunks = fileManager.openExtendedList()) == -1) {
  540. response.json["accept"] = false;
  541. response.json["chunks"] = -1;
  542. response.json["items"] = -1;
  543. response.json["error"] = "there is a filename which is too long";
  544. } else {
  545. response.json["accept"] = true;
  546. response.json["chunks"] = chunks;
  547. response.json["items"] = fileManager.getExtendedListSize();
  548. response.json["error"] = "";
  549. }
  550. return response;
  551. }
  552. JsonCommander::Response JsonCommander::executeQueue(const Json::Value &message) {
  553. JsonCommander::Response response;
  554. response.json["command"] = "queue";
  555. if (!message["file"].isString()) {
  556. response.action = closeAndSend;
  557. response.json["file"] = "";
  558. response.json["accept"] = false;
  559. response.json["error"] = "invalid request";
  560. } else {
  561. bool res = Queue::push(message["file"].asString());
  562. if (res) {
  563. response.action = send;
  564. response.json["file"] = message["file"].asString();
  565. response.json["accept"] = true;
  566. response.json["error"] = "";
  567. } else {
  568. response.action = send;
  569. response.json["file"] = message["file"].asString();
  570. response.json["accept"] = false;
  571. response.json["error"] = "file could not be queued";
  572. }
  573. }
  574. return response;
  575. }
  576. JsonCommander::Response JsonCommander::executeExtendedListData(const Json::Value &message) {
  577. JsonCommander::Response response;
  578. response.action = send;
  579. response.json["command"] = "extendedlistdata";
  580. Json::Value array;
  581. const int remainingListchunks = fileManager.getRemainingExtendedListChunks();
  582. if (!message["chunk"].isInt() || !message["cancel"].isBool()) {
  583. response.action = closeAndSend;
  584. response.json["cancel"] = true;
  585. response.json["remaining"] = -1;
  586. response.json["files"] = Json::arrayValue;
  587. response.json["error"] = "incorrect listdata command request";
  588. } else if (remainingListchunks == 0) {
  589. response.json["cancel"] = true;
  590. response.json["remaining"] = -1;
  591. response.json["files"] = Json::arrayValue;
  592. response.json["error"] = "there are no chunks to send";
  593. } else if (message["cancel"].asBool()) {
  594. response.json["cancel"] = true;
  595. response.json["remaining"] = message["chunk"].asInt();
  596. response.json["files"] = Json::arrayValue;
  597. response.json["error"] = "";
  598. fileManager.cancelExtendedList();
  599. } else if (remainingListchunks - 1 != message["chunk"].asInt()) {
  600. response.action = closeAndSend;
  601. response.json["cancel"] = true;
  602. response.json["remaining"] = -1;
  603. response.json["files"] = Json::arrayValue;
  604. response.json["error"] = "wrong chunk number";
  605. } else {
  606. auto v = fileManager.getNextChunkFromExtendedList();
  607. for (int i = 0; i < v.size(); i++) {
  608. Json::Value obj;
  609. obj["name"] = std::get<0>(v.at(i));
  610. obj["head"] = std::get<1>(v.at(i)).compare("") == 0 ? "" : base64::encode(std::get<1>(v.at(i)));
  611. obj["size"] = std::get<2>(v.at(i));
  612. array.append(obj);
  613. }
  614. response.json["remaining"] = message["chunk"].asInt();
  615. response.json["cancel"] = false;
  616. response.json["files"] = array;
  617. response.json["error"] = "";
  618. }
  619. return response;
  620. }
  621. JsonCommander::Response JsonCommander::executeDequeue(const Json::Value &message) {
  622. JsonCommander::Response response;
  623. response.json["command"] = "dequeue";
  624. if (!message["file"].isString()) {
  625. response.action = closeAndSend;
  626. response.json["file"] = "";
  627. response.json["accept"] = false;
  628. response.json["error"] = "invalid request";
  629. } else {
  630. bool res = Queue::remove(message["file"].asString());
  631. if (res) {
  632. response.action = send;
  633. response.json["file"] = message["file"].asString();
  634. response.json["accept"] = true;
  635. response.json["error"] = "";
  636. } else {
  637. response.action = send;
  638. response.json["file"] = message["file"].asString();
  639. response.json["accept"] = false;
  640. response.json["error"] = "cannot remove this file from queue";
  641. }
  642. }
  643. return response;
  644. }