cmdman.cpp 27 KB

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