cmdman.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840
  1. #include "../include/cmdman.h"
  2. #include "../include/global.h"
  3. #include <iostream>
  4. #define DEBUGPRINT(x) debugprintfunc(x)
  5. //~ #define DEBUGPRINT(x) std::cerr << x
  6. //~ #define DEBUGPRINT(x)
  7. CmdMan::CmdMan(FileMan &fm, void (*dpf)(string)) : fileman(fm) {
  8. /* setup json stuff */
  9. Json::CharReaderBuilder rbuilder;
  10. wbuilder.settings_["indentation"] = "";
  11. reader = rbuilder.newCharReader();
  12. doversion = false;
  13. loginpossible = false;
  14. dologin = false;
  15. dosignup = false;
  16. /* initialize execute command map */
  17. execmap["help"] = &CmdMan::cmdHelp;
  18. execmap["status"] = &CmdMan::cmdStatus;
  19. execmap["disconnect"] = &CmdMan::cmdDisconnect;
  20. execmap["put"] = &CmdMan::cmdPut;
  21. execmap["get"] = &CmdMan::cmdGet;
  22. execmap["list"] = &CmdMan::cmdList;
  23. execmap["version"] = &CmdMan::cmdVersion;
  24. execmap["login"] = &CmdMan::cmdLogin;
  25. execmap["signup"] = &CmdMan::cmdSignup;
  26. execmap["putdata"] = &CmdMan::cmdPutdata;
  27. execmap["getdata"] = &CmdMan::cmdGetdata;
  28. execmap["listdata"] = &CmdMan::cmdListdata;
  29. execmap["head"] = &CmdMan::cmdHead;
  30. execmap["deletefile"] = &CmdMan::cmdDeletefile;
  31. execmap["deleteme"] = &CmdMan::cmdDeleteme;
  32. execmap["keyfile"] = &CmdMan::cmdKeyfile;
  33. execmap["closekey"] = &CmdMan::cmdClosekey;
  34. /* initialize description map */
  35. helpmap["help"] = descHelp;
  36. helpmap["status"] = descStatus;
  37. helpmap["disconnect"] = descDisconnect;
  38. helpmap["put"] = descPut;
  39. helpmap["get"] = descGet;
  40. helpmap["list"] = descList;
  41. helpmap["head"] = descHead;
  42. helpmap["login"] = descLogin;
  43. helpmap["signup"] = descSignup;
  44. helpmap["deletefile"] = descDeletefile;
  45. helpmap["deleteme"] = descDeleteme;
  46. helpmap["keyfile"] = descKeyfile;
  47. helpmap["closekey"] = descClosekey;
  48. /* initialize handle command map */
  49. handlemap["status"] = &CmdMan::handleStatus;
  50. handlemap["close"] = &CmdMan::handleClose;
  51. handlemap["put"] = &CmdMan::handlePut;
  52. handlemap["get"] = &CmdMan::handleGet;
  53. handlemap["putdata"] = &CmdMan::handlePutdata;
  54. handlemap["getdata"] = &CmdMan::handleGetdata;
  55. handlemap["list"] = &CmdMan::handleList;
  56. handlemap["version"] = &CmdMan::handleVersion;
  57. handlemap["login"] = &CmdMan::handleLogin;
  58. handlemap["signup"] = &CmdMan::handleSignup;
  59. handlemap["listdata"] = &CmdMan::handleListdata;
  60. handlemap["head"] = &CmdMan::handleHead;
  61. handlemap["deletefile"] = &CmdMan::handleDeletefile;
  62. handlemap["deleteme"] = &CmdMan::handleDeleteme;
  63. debugprintfunc = dpf;
  64. }
  65. CmdMan::~CmdMan() { delete reader; }
  66. CmdMan::CmdRet CmdMan::cmdHelp(vector<string> args) {
  67. CmdRet retval;
  68. Json::Value root, arr;
  69. DEBUGPRINT(string(__PRETTY_FUNCTION__) + " begin");
  70. map<string, string>::iterator it;
  71. root["command"] = "help";
  72. for (it = helpmap.begin(); it != helpmap.end(); it++) {
  73. arr.append(it->first + " - " + it->second);
  74. }
  75. root["names"] = arr;
  76. retval.type = print;
  77. retval.msg = root;
  78. return retval;
  79. }
  80. CmdMan::CmdRet CmdMan::cmdStatus(vector<string> args) {
  81. CmdRet retval;
  82. DEBUGPRINT(string(__PRETTY_FUNCTION__) + " begin");
  83. Json::Value root;
  84. root["command"] = "status";
  85. retval.type = send;
  86. retval.msg = root;
  87. return retval;
  88. }
  89. CmdMan::CmdRet CmdMan::cmdDisconnect(vector<string> args) {
  90. CmdRet retval;
  91. DEBUGPRINT(string(__PRETTY_FUNCTION__) + " begin");
  92. Json::Value root;
  93. retval.type = send;
  94. if (loginpossible) {
  95. // not logged in, send appropriate login message instead of normal close
  96. root["login"] = false;
  97. root["user"] = "";
  98. root["pass"] = "";
  99. root["cancel"] = true;
  100. retval.type |= close;
  101. } else {
  102. root["command"] = "close";
  103. }
  104. retval.msg = root;
  105. return retval;
  106. }
  107. CmdMan::CmdRet CmdMan::cmdPut(vector<string> args) {
  108. CmdRet retval;
  109. DEBUGPRINT(string(__PRETTY_FUNCTION__) + " begin");
  110. Json::Value root;
  111. root["command"] = "put";
  112. if (args.size() < 1) {
  113. retval.type = error;
  114. root["accept"] = false;
  115. root["error"] = "not enough arguments, at least 1 argument required";
  116. } else {
  117. if (fileman.isPutting()) {
  118. retval.type = error;
  119. root["file"] = args[0];
  120. root["accept"] = false;
  121. root["error"] = "already putting file \"" + fileman.getPutName() + "\"";
  122. } else {
  123. bool opened = fileman.openPut(args[0]);
  124. root["file"] = fileman.getPutName();
  125. if (opened) {
  126. root["size"] = fileman.getPutSize();
  127. root["chunks"] = fileman.getPutChunks();
  128. retval.type = send;
  129. } else {
  130. retval.type = error;
  131. root["accept"] = false;
  132. root["error"] = "couldnt open local file \"" + args[0] + "\"";
  133. }
  134. }
  135. }
  136. retval.msg = root;
  137. return retval;
  138. }
  139. CmdMan::CmdRet CmdMan::cmdPutdata(vector<string> args) {
  140. CmdRet retval;
  141. DEBUGPRINT(string(__PRETTY_FUNCTION__) + " begin");
  142. Json::Value root;
  143. root["command"] = "putdata";
  144. root["file"] = fileman.getPutName();
  145. root["cancel"] = false;
  146. root["data"] = fileman.readBase64();
  147. root["remaining"] = fileman.getPutRemainingChunks(); // number already decremented by readBase64
  148. retval.type = send;
  149. retval.msg = root;
  150. return retval;
  151. }
  152. CmdMan::CmdRet CmdMan::cmdGet(vector<string> args) {
  153. CmdRet retval;
  154. DEBUGPRINT(string(__PRETTY_FUNCTION__) + " begin");
  155. Json::Value root;
  156. root["command"] = "get";
  157. if (args.size() < 1) {
  158. retval.type = error;
  159. root["accept"] = false;
  160. root["error"] = "not enough arguments, at least 1 argument required";
  161. } else {
  162. if (fileman.isGetting()) {
  163. retval.type = error;
  164. root["file"] = args[0];
  165. root["accept"] = false;
  166. root["error"] = "already getting file \"" + fileman.getGetName() + "\"";
  167. } else {
  168. bool opened = fileman.openGet(args[0]);
  169. root["file"] = fileman.getGetName();
  170. if (opened) {
  171. retval.type = send;
  172. } else {
  173. retval.type = error;
  174. root["accept"] = false;
  175. root["error"] = "local file \"" + args[0] + "\" already exists";
  176. }
  177. }
  178. }
  179. retval.msg = root;
  180. return retval;
  181. }
  182. CmdMan::CmdRet CmdMan::cmdGetdata(vector<string> args) {
  183. CmdRet retval;
  184. DEBUGPRINT(string(__PRETTY_FUNCTION__) + " begin");
  185. Json::Value root;
  186. root["command"] = "getdata";
  187. root["file"] = fileman.getGetName();
  188. root["chunk"] = fileman.getGetRemainingChunks();
  189. root["cancel"] = false;
  190. retval.type = send;
  191. retval.msg = root;
  192. return retval;
  193. }
  194. CmdMan::CmdRet CmdMan::cmdList(vector<string> args) {
  195. CmdRet retval;
  196. DEBUGPRINT(string(__PRETTY_FUNCTION__) + " begin");
  197. Json::Value root;
  198. bool opened = fileman.openList();
  199. root["command"] = "list";
  200. if (opened) {
  201. retval.type = send;
  202. } else {
  203. retval.type = error;
  204. root["accept"] = false;
  205. root["names"] = "";
  206. root["error"] = "cannot list, already listing";
  207. }
  208. retval.msg = root;
  209. return retval;
  210. }
  211. CmdMan::CmdRet CmdMan::cmdListdata(vector<string> args) {
  212. CmdRet retval;
  213. DEBUGPRINT(string(__PRETTY_FUNCTION__) + " begin");
  214. Json::Value root;
  215. root["command"] = "listdata";
  216. root["chunk"] = fileman.getListRemainingChunks();
  217. root["cancel"] = false;
  218. retval.type = send;
  219. retval.msg = root;
  220. return retval;
  221. }
  222. CmdMan::CmdRet CmdMan::cmdHead(vector<string> args) {
  223. CmdRet retval;
  224. DEBUGPRINT(string(__PRETTY_FUNCTION__) + " begin");
  225. Json::Value root;
  226. root["command"] = "head";
  227. if (args.size() < 1) {
  228. retval.type = error;
  229. root["accept"] = false;
  230. root["error"] = "not enough arguments, at least 1 argument required";
  231. } else {
  232. root["file"] = args[0];
  233. retval.type = send;
  234. }
  235. retval.msg = root;
  236. return retval;
  237. }
  238. CmdMan::CmdRet CmdMan::cmdDeletefile(vector<string> args) {
  239. CmdRet retval;
  240. DEBUGPRINT(string(__PRETTY_FUNCTION__) + " begin");
  241. Json::Value root;
  242. root["command"] = "deletefile";
  243. if (args.size() < 1) {
  244. retval.type = error;
  245. root["accept"] = false;
  246. root["error"] = "not enough arguments, at least 1 argument required";
  247. } else {
  248. root["file"] = args[0];
  249. retval.type = send;
  250. }
  251. retval.msg = root;
  252. return retval;
  253. }
  254. CmdMan::CmdRet CmdMan::execute(string cmd, vector<string> args) {
  255. DEBUGPRINT(string(__PRETTY_FUNCTION__) + " begin");
  256. DEBUGPRINT(string(__PRETTY_FUNCTION__) + " using command \"" + cmd + "\" with arguments [ ");
  257. for (string s : args)
  258. DEBUGPRINT(s + " ");
  259. DEBUGPRINT("]");
  260. map<string, CmdRet (CmdMan::*)(vector<string>)>::iterator it = execmap.find(cmd);
  261. CmdRet retval;
  262. Json::Value root;
  263. root["command"] = cmd;
  264. if (it == execmap.end()) {
  265. retval.type = error;
  266. root["command"] = "error";
  267. root["error"] = string(__PRETTY_FUNCTION__) + " unknown command \"" + cmd + "\".\ntype help to list available commands.";
  268. retval.msg = root;
  269. return retval;
  270. } else if (loginpossible || dologin || dosignup) {
  271. DEBUGPRINT("execute does login");
  272. DEBUGPRINT(string("comparison is ") +
  273. std::to_string(cmd.compare("login") && cmd.compare("signup") && cmd.compare("disconnect") && cmd.compare("help")));
  274. if (cmd.compare("login") && cmd.compare("signup") && cmd.compare("disconnect") && cmd.compare("help")) {
  275. retval.type = error;
  276. root["command"] = "error";
  277. root["error"] = string("Not logged in. Available commands are limited to ") + "login" + ", " + "signup" + " and " + "disconnect" + "\n" +
  278. "Use help for usage of these commands.";
  279. retval.msg = root;
  280. return retval;
  281. }
  282. }
  283. return (this->*(execmap[cmd]))(args);
  284. }
  285. CmdMan::CmdRet CmdMan::cmdDeleteme(vector<string> args) {
  286. CmdRet retval;
  287. DEBUGPRINT(string(__PRETTY_FUNCTION__) + " begin");
  288. Json::Value root;
  289. root["command"] = "deleteme";
  290. if (args.size() < 1) {
  291. retval.type = error;
  292. root["accept"] = false;
  293. root["error"] = "not enough arguments, at least 1 argument required";
  294. } else {
  295. retval.type = send;
  296. root["pass"] = args[0];
  297. }
  298. retval.msg = root;
  299. return retval;
  300. }
  301. CmdMan::CmdRet CmdMan::cmdKeyfile(vector<string> args) {
  302. CmdRet retval;
  303. Json::Value root;
  304. DEBUGPRINT(string(__PRETTY_FUNCTION__) + " begin");
  305. root["command"] = "keyfile";
  306. if (args.size() < 1) {
  307. retval.type = error;
  308. root["accept"] = false;
  309. root["error"] = "not enough arguments, at least 1 argument required";
  310. } else {
  311. DEBUGPRINT(string(__PRETTY_FUNCTION__) + " haveargs");
  312. if (!fileman.openKey(args[0])) {
  313. DEBUGPRINT(string(__PRETTY_FUNCTION__) + " openkey fail");
  314. root["accept"] = false;
  315. root["file"] = args[0];
  316. root["error"] = string("couldnt open keyfile, openssl reports: ") + fileman.getOpensslError();
  317. retval.type = error;
  318. } else {
  319. DEBUGPRINT(string(__PRETTY_FUNCTION__) + " openkey good");
  320. root["accept"] = true;
  321. root["file"] = args[0];
  322. retval.type = print;
  323. }
  324. }
  325. retval.msg = root;
  326. return retval;
  327. }
  328. CmdMan::CmdRet CmdMan::cmdClosekey(vector<string> args) {
  329. CmdRet retval;
  330. Json::Value root;
  331. DEBUGPRINT(string(__PRETTY_FUNCTION__) + " begin");
  332. root["command"] = "closekey";
  333. if (!fileman.closeKey()) {
  334. root["accept"] = false;
  335. root["error"] = "couldnt close keyfile. ensure no put or get is running";
  336. retval.type = error;
  337. } else {
  338. root["accept"] = true;
  339. retval.type = print;
  340. }
  341. retval.msg = root;
  342. return retval;
  343. }
  344. /* login and signup commands */
  345. CmdMan::CmdRet CmdMan::cmdLogin(vector<string> args) {
  346. CmdRet retval;
  347. DEBUGPRINT(string(__PRETTY_FUNCTION__) + " begin");
  348. Json::Value root;
  349. if (args.size() < 2) {
  350. retval.type = error;
  351. root["command"] = "login";
  352. root["accept"] = false;
  353. root["error"] = "not enough arguments, at least 2 argument required";
  354. } else {
  355. if (loginpossible) {
  356. dologin = true;
  357. loginpossible = false;
  358. root["user"] = args[0];
  359. root["pass"] = args[1];
  360. root["login"] = true;
  361. root["cancel"] = false;
  362. retval.type = send;
  363. } else {
  364. root["command"] = "login";
  365. root["error"] = "Login not possible, because you already requested a login "
  366. "or you are logged in";
  367. root["accept"] = false;
  368. retval.type = error;
  369. }
  370. }
  371. retval.msg = root;
  372. return retval;
  373. }
  374. CmdMan::CmdRet CmdMan::cmdSignup(vector<string> args) {
  375. CmdRet retval;
  376. DEBUGPRINT(string(__PRETTY_FUNCTION__) + " begin");
  377. Json::Value root;
  378. if (args.size() < 2) {
  379. retval.type = error;
  380. root["command"] = "signup";
  381. root["accept"] = false;
  382. root["error"] = "not enough arguments, at least 2 argument required";
  383. } else {
  384. if (loginpossible) {
  385. dosignup = true;
  386. loginpossible = false;
  387. root["user"] = args[0];
  388. root["pass"] = args[1];
  389. root["login"] = false;
  390. root["cancel"] = false;
  391. retval.type = send;
  392. } else {
  393. root["command"] = "signup";
  394. root["error"] = "Signup not possible, because you already requested a "
  395. "login or you are logged in";
  396. root["accept"] = false;
  397. retval.type = error;
  398. }
  399. }
  400. retval.msg = root;
  401. return retval;
  402. }
  403. /* internal commands */
  404. CmdMan::CmdRet CmdMan::cmdVersion(vector<string> args) {
  405. CmdRet retval;
  406. DEBUGPRINT(string(__PRETTY_FUNCTION__) + " begin");
  407. Json::Value root;
  408. root["major"] = protocolMajorVersion;
  409. root["minor"] = protocolMinorVersion;
  410. retval.type = send;
  411. retval.msg = root;
  412. doversion = true;
  413. return retval;
  414. }
  415. CmdMan::CmdRet CmdMan::handle(Json::Value root) {
  416. CmdRet retval;
  417. Json::Value output;
  418. DEBUGPRINT(string(__PRETTY_FUNCTION__) + " begin");
  419. if (doversion)
  420. root["command"] = "version";
  421. else if (dosignup)
  422. root["command"] = "signup";
  423. else if (dologin)
  424. root["command"] = "login";
  425. DEBUGPRINT(string(__PRETTY_FUNCTION__) + " using json\n" + Json::writeString(wbuilder, root) + "\n");
  426. string retmsg;
  427. map<string, CmdRet (CmdMan::*)(Json::Value)>::iterator it = handlemap.find(root["command"].asString());
  428. if (it == handlemap.end()) {
  429. retval.type = error;
  430. output["command"] = "error";
  431. output["error"] = string(__PRETTY_FUNCTION__) + " unknown command \"" + root["command"].asString() + "\".\nEnsure code is implemented.";
  432. retval.msg = output;
  433. return retval;
  434. }
  435. return (this->*(handlemap[root["command"].asString()]))(root);
  436. }
  437. CmdMan::CmdRet CmdMan::handleStatus(Json::Value root) {
  438. CmdRet retval;
  439. DEBUGPRINT(string(__PRETTY_FUNCTION__) + " begin");
  440. retval.type = print;
  441. retval.msg = root;
  442. return retval;
  443. }
  444. CmdMan::CmdRet CmdMan::handleClose(Json::Value root) {
  445. CmdRet retval;
  446. Json::Value output;
  447. DEBUGPRINT(string(__PRETTY_FUNCTION__) + " begin");
  448. output["command"] = "disconnect";
  449. output["accept"] = true;
  450. retval.type = close | print;
  451. retval.msg = output;
  452. return retval;
  453. }
  454. CmdMan::CmdRet CmdMan::handlePut(Json::Value root) {
  455. CmdRet retval;
  456. Json::Value output;
  457. output["command"] = "put";
  458. output["file"] = fileman.getPutName();
  459. output["accept"] = false;
  460. DEBUGPRINT(string(__PRETTY_FUNCTION__) + " begin");
  461. if (!root["accept"].asBool()) {
  462. retval.type = error;
  463. output["error"] = "Server reports: " + root["error"].asString();
  464. fileman.cancelPut();
  465. } else if (root["file"].asString() != fileman.getPutName()) {
  466. retval.type = error;
  467. output["error"] = "Server reports filename " + root["file"].asString() + " but actual filename is " + fileman.getPutName();
  468. fileman.cancelPut();
  469. } else {
  470. output["accept"] = true;
  471. output["error"] = "";
  472. retval.type = print | send;
  473. retval.nextcommand = "putdata";
  474. }
  475. retval.msg = output;
  476. return retval;
  477. }
  478. CmdMan::CmdRet CmdMan::handlePutdata(Json::Value root) {
  479. CmdRet retval;
  480. Json::Value output;
  481. DEBUGPRINT(string(__PRETTY_FUNCTION__) + " begin");
  482. output["command"] = "putdata";
  483. output["file"] = fileman.getPutName();
  484. output["speed"] = 0.0f; // TODO
  485. output["cancel"] = true;
  486. if (root["received"].asInt() != fileman.getPutRemainingChunks()) {
  487. // the number of remaining chunks received from the daemon does not equal
  488. // the number stored at the client side
  489. retval.type = error;
  490. output["error"] = std::string("Server reports number of "
  491. "remaining chunks as ") +
  492. std::to_string(root["received"].asInt()) + " but actual number is " + std::to_string(fileman.getPutRemainingChunks());
  493. fileman.cancelPut();
  494. } else if (root["cancel"].asBool()) {
  495. retval.type = error;
  496. output["error"] = "Server reports: " + root["error"].asString();
  497. fileman.cancelPut();
  498. } else if (root["file"].asString() != fileman.getPutName()) {
  499. retval.type = error;
  500. output["error"] = "Server reports filename " + root["file"].asString() + " but actual filename is " + fileman.getPutName();
  501. fileman.cancelPut();
  502. } else {
  503. output["cancel"] = false;
  504. output["error"] = "";
  505. // sent successfully
  506. if (!root["received"].asInt()) {
  507. // everything sent
  508. retval.type = print;
  509. // TODO
  510. //~ retval.msg = "succesfully uploaded file " + fileman.getPutName();
  511. fileman.closePut();
  512. } else {
  513. retval.type = print | send;
  514. retval.nextcommand = "putdata";
  515. }
  516. }
  517. retval.msg = output;
  518. return retval;
  519. }
  520. CmdMan::CmdRet CmdMan::handleGet(Json::Value root) {
  521. CmdRet retval;
  522. Json::Value output;
  523. output["command"] = "get";
  524. output["file"] = fileman.getGetName();
  525. output["accept"] = false;
  526. DEBUGPRINT(string(__PRETTY_FUNCTION__) + " begin");
  527. if (!root["accept"].asBool()) {
  528. retval.type = error;
  529. output["error"] = "Server reports: " + root["error"].asString();
  530. } else if (root["file"].asString() != fileman.getGetName()) {
  531. retval.type = error;
  532. output["error"] = "Server reports filename " + root["file"].asString() + " but actual filename is " + fileman.getGetName();
  533. } else {
  534. fileman.setGetChunks(root["chunks"].asInt());
  535. output["accept"] = true;
  536. output["error"] = "";
  537. retval.type = print | send;
  538. retval.nextcommand = "getdata";
  539. }
  540. retval.msg = output;
  541. return retval;
  542. }
  543. CmdMan::CmdRet CmdMan::handleGetdata(Json::Value root) {
  544. CmdRet retval;
  545. Json::Value output;
  546. DEBUGPRINT(string(__PRETTY_FUNCTION__) + " begin");
  547. output["command"] = "getdata";
  548. output["file"] = fileman.getGetName();
  549. output["speed"] = 0.0f; // TODO
  550. output["cancel"] = true;
  551. // the passed number of recieved chunks should equal the number of sent chunks
  552. if (root["remaining"].asInt() != fileman.getGetRemainingChunks()) {
  553. retval.type = error;
  554. output["error"] = std::string("Server reports number of remaining chunks as ") + std::to_string(root["remaining"].asInt()) + " but actual number is " +
  555. std::to_string(fileman.getGetRemainingChunks());
  556. fileman.cancelGet();
  557. } else if (root["cancel"].asBool()) {
  558. retval.type = error;
  559. output["error"] = "Server reports: " + root["error"].asString();
  560. fileman.cancelGet();
  561. } else if (root["file"].asString() != fileman.getGetName()) {
  562. retval.type = error;
  563. output["error"] = "Server reports filename " + root["file"].asString() + " but actual filename is " + fileman.getGetName();
  564. fileman.cancelGet();
  565. } else {
  566. output["cancel"] = false;
  567. output["error"] = "";
  568. fileman.writeBase64(root["data"].asString());
  569. // loaded successfully
  570. if (fileman.getGetRemainingChunks() < 0) {
  571. // everything sent
  572. retval.type = print;
  573. //~ retval.msg = "succesfully downloaded file " + fileman.getGetName();
  574. fileman.closeGet();
  575. } else {
  576. retval.type = print | send;
  577. retval.nextcommand = "getdata";
  578. }
  579. }
  580. retval.msg = output;
  581. return retval;
  582. }
  583. CmdMan::CmdRet CmdMan::handleList(Json::Value root) {
  584. CmdRet retval;
  585. Json::Value output; // LOCALOUTPUT
  586. DEBUGPRINT(string(__PRETTY_FUNCTION__) + " begin");
  587. output["command"] = "list";
  588. output["names"] = "";
  589. if (!root["accept"].asBool()) {
  590. retval.type = error;
  591. output["accept"] = false;
  592. output["error"] = "Server reports: " + root["error"].asString();
  593. fileman.cancelList();
  594. } else {
  595. fileman.setListChunks(root["chunks"].asInt());
  596. retval.type = send;
  597. output["accept"] = true;
  598. retval.nextcommand = "listdata";
  599. }
  600. retval.msg = output;
  601. return retval;
  602. }
  603. CmdMan::CmdRet CmdMan::handleListdata(Json::Value root) {
  604. CmdRet retval;
  605. Json::Value output, arr;
  606. DEBUGPRINT(string(__PRETTY_FUNCTION__) + " begin");
  607. vector<string> toadd;
  608. output["command"] = "list";
  609. output["names"] = "";
  610. output["accept"] = false;
  611. // the passed number of recieved chunks should equal the number of sent chunks
  612. if (root["remaining"].asInt() != fileman.getListRemainingChunks()) {
  613. retval.type = error;
  614. output["error"] = std::string("Server reports number of "
  615. "remaining chunks as ") +
  616. std::to_string(root["remaining"].asInt()) + " but actual number is " + std::to_string(fileman.getListRemainingChunks());
  617. fileman.cancelList();
  618. } else if (root["cancel"].asBool()) {
  619. retval.type = error;
  620. output["error"] = "Server reports: " + root["error"].asString();
  621. fileman.cancelList();
  622. } else {
  623. output["accept"] = true;
  624. for (Json::Value i : root["names"])
  625. toadd.push_back(i.asString());
  626. fileman.putListData(toadd);
  627. // loaded successfully
  628. if (fileman.getListRemainingChunks() < 0) {
  629. // everything sent
  630. retval.type = print;
  631. for (string s : fileman.getListData())
  632. arr.append(s);
  633. output["names"] = arr;
  634. fileman.closeList();
  635. } else {
  636. retval.type = send;
  637. retval.nextcommand = "listdata";
  638. }
  639. }
  640. retval.msg = output;
  641. return retval;
  642. }
  643. CmdMan::CmdRet CmdMan::handleVersion(Json::Value root) {
  644. CmdRet retval;
  645. Json::Value output; // LOCALOUTPUT
  646. DEBUGPRINT(string(__PRETTY_FUNCTION__) + " begin");
  647. output["command"] = "version";
  648. output["serverversion"] = root["major"].asString() + "." + root["minor"].asString();
  649. output["clientversion"] = std::to_string(protocolMajorVersion) + "." + std::to_string(protocolMinorVersion);
  650. if (!root["accept"].asBool()) {
  651. retval.type = error;
  652. output["accept"] = false;
  653. } else {
  654. retval.type = print | seton;
  655. output["accept"] = true;
  656. doversion = false;
  657. loginpossible = true;
  658. }
  659. retval.msg = output;
  660. return retval;
  661. }
  662. CmdMan::CmdRet CmdMan::handleLogin(Json::Value root) {
  663. CmdRet retval;
  664. Json::Value output; // LOCALOUTPUT
  665. DEBUGPRINT(string(__PRETTY_FUNCTION__) + " begin");
  666. output["command"] = "login";
  667. if (!root["accept"].asBool()) {
  668. retval.type = error;
  669. output["error"] = root["error"].asString();
  670. output["accept"] = false;
  671. loginpossible = true;
  672. dologin = false;
  673. } else {
  674. retval.type = print | seton;
  675. output["error"] = "";
  676. output["accept"] = true;
  677. dologin = false;
  678. }
  679. retval.msg = output;
  680. return retval;
  681. }
  682. CmdMan::CmdRet CmdMan::handleSignup(Json::Value root) {
  683. CmdRet retval;
  684. Json::Value output; // LOCALOUTPUT
  685. DEBUGPRINT(string(__PRETTY_FUNCTION__) + " begin");
  686. output["command"] = "signup";
  687. if (!root["accept"].asBool()) {
  688. retval.type = error;
  689. output["error"] = root["error"].asString();
  690. output["accept"] = false;
  691. loginpossible = true;
  692. dosignup = false;
  693. } else {
  694. retval.type = print | seton;
  695. output["error"] = "";
  696. output["accept"] = true;
  697. dosignup = false;
  698. }
  699. retval.msg = output;
  700. return retval;
  701. }
  702. CmdMan::CmdRet CmdMan::handleHead(Json::Value root) {
  703. CmdRet retval;
  704. DEBUGPRINT(string(__PRETTY_FUNCTION__) + " begin");
  705. if (!root["accept"].asBool()) {
  706. Json::Value output;
  707. output["command"] = "head";
  708. output["file"] = root["file"];
  709. output["error"] = "Server reports: " + root["error"].asString();
  710. output["accept"] = false;
  711. retval.type = error;
  712. retval.msg = output;
  713. } else {
  714. retval.type = print;
  715. retval.msg = root;
  716. }
  717. return retval;
  718. }
  719. CmdMan::CmdRet CmdMan::handleDeletefile(Json::Value root) {
  720. CmdRet retval;
  721. DEBUGPRINT(string(__PRETTY_FUNCTION__) + " begin");
  722. if (!root["accept"].asBool()) {
  723. Json::Value output;
  724. output["command"] = "deletefile";
  725. output["file"] = root["file"];
  726. output["error"] = "Server reports: " + root["error"].asString();
  727. output["accept"] = false;
  728. retval.type = error;
  729. retval.msg = output;
  730. } else {
  731. retval.type = print;
  732. retval.msg = root;
  733. }
  734. return retval;
  735. }
  736. CmdMan::CmdRet CmdMan::handleDeleteme(Json::Value root) {
  737. CmdRet retval;
  738. DEBUGPRINT(string(__PRETTY_FUNCTION__) + " begin");
  739. if (!root["accept"].asBool()) {
  740. retval.type = error;
  741. } else {
  742. retval.type = close | print;
  743. }
  744. retval.msg = root;
  745. return retval;
  746. }