cmdman.cpp 22 KB

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