cmdman.cpp 34 KB

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