cmdman.cpp 20 KB

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