1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258 |
- #include "../include/cmdman.h"
- #include "../include/global.h"
- #include <iostream>
- #define DEBUGPRINT(x) debugprintfunc(x)
- //~ #define DEBUGPRINT(x) std::cerr << x
- //~ #define DEBUGPRINT(x)
- CmdMan::CmdMan(FileMan &fm, void (*dpf)(string)) : fileman(fm) {
- /* setup json stuff */
- Json::CharReaderBuilder rbuilder;
- wbuilder.settings_["indentation"] = "";
- reader = rbuilder.newCharReader();
- currentState = connectionpossible;
- /* initialize execute command map */
- execmap["help"] = &CmdMan::cmdHelp;
- execmap["status"] = &CmdMan::cmdStatus;
- execmap["extendedstatus"] = &CmdMan::cmdExtendedstatus;
- execmap["disconnect"] = &CmdMan::cmdDisconnect;
- execmap["put"] = &CmdMan::cmdPut;
- execmap["get"] = &CmdMan::cmdGet;
- execmap["list"] = &CmdMan::cmdList;
- execmap["extendedlist"] = &CmdMan::cmdExtendedlist;
- execmap["version"] = &CmdMan::cmdVersion;
- execmap["login"] = &CmdMan::cmdLogin;
- execmap["signup"] = &CmdMan::cmdSignup;
- execmap["putdata"] = &CmdMan::cmdPutdata;
- execmap["getdata"] = &CmdMan::cmdGetdata;
- execmap["listdata"] = &CmdMan::cmdListdata;
- execmap["extendedlistdata"] = &CmdMan::cmdExtendedlistdata;
- execmap["head"] = &CmdMan::cmdHead;
- execmap["deletefile"] = &CmdMan::cmdDeletefile;
- execmap["deleteme"] = &CmdMan::cmdDeleteme;
- execmap["keyfile"] = &CmdMan::cmdKeyfile;
- execmap["closekey"] = &CmdMan::cmdClosekey;
- execmap["queue"] = &CmdMan::cmdQueue;
- execmap["dequeue"] = &CmdMan::cmdDequeue;
- execmap["notifications"] = &CmdMan::cmdNotifications;
- execmap["connect"] = &CmdMan::cmdConnect;
- execmap["exit"] = &CmdMan::cmdExit;
- /* initialize description map */
- helpmap["help"] = descHelp;
- helpmap["status"] = descStatus;
- helpmap["extendedstatus"] = descExtendedstatus;
- helpmap["disconnect"] = descDisconnect;
- helpmap["put"] = descPut;
- helpmap["get"] = descGet;
- helpmap["list"] = descList;
- helpmap["extendedlist"] = descExtendedlist;
- helpmap["head"] = descHead;
- helpmap["login"] = descLogin;
- helpmap["signup"] = descSignup;
- helpmap["deletefile"] = descDeletefile;
- helpmap["deleteme"] = descDeleteme;
- helpmap["keyfile"] = descKeyfile;
- helpmap["closekey"] = descClosekey;
- helpmap["queue"] = descQueue;
- helpmap["dequeue"] = descDequeue;
- helpmap["notifications"] = descNotifications;
- helpmap["connect"] = descConnect;
- helpmap["exit"] = descExit;
- /* initialize handle command map */
- handlemap["status"] = &CmdMan::handleStatus;
- handlemap["extendedstatus"] = &CmdMan::handleExtendedstatus;
- handlemap["close"] = &CmdMan::handleClose;
- handlemap["put"] = &CmdMan::handlePut;
- handlemap["get"] = &CmdMan::handleGet;
- handlemap["putdata"] = &CmdMan::handlePutdata;
- handlemap["getdata"] = &CmdMan::handleGetdata;
- handlemap["list"] = &CmdMan::handleList;
- handlemap["extendedlist"] = &CmdMan::handleExtendedlist;
- handlemap["version"] = &CmdMan::handleVersion;
- handlemap["login"] = &CmdMan::handleLogin;
- handlemap["signup"] = &CmdMan::handleSignup;
- handlemap["listdata"] = &CmdMan::handleListdata;
- handlemap["extendedlistdata"] = &CmdMan::handleExtendedlistdata;
- handlemap["head"] = &CmdMan::handleHead;
- handlemap["deletefile"] = &CmdMan::handleDeletefile;
- handlemap["deleteme"] = &CmdMan::handleDeleteme;
- handlemap["queue"] = &CmdMan::handleQueue;
- handlemap["dequeue"] = &CmdMan::handleDequeue;
- handlemap["notifications"] = &CmdMan::handleNotifications;
- debugprintfunc = dpf;
- }
- CmdMan::~CmdMan() { delete reader; }
- void CmdMan::stateSetConnectionOk() { currentState = versionpossible; }
- void CmdMan::stateSetDisconnected() {
- currentState = connectionpossible;
- fileman.cancelGet();
- fileman.cancelPut();
- fileman.cancelList();
- }
- CmdMan::CmdRet CmdMan::cmdHelp(vector<string> args) {
- CmdRet retval;
- Json::Value root, arr;
- DEBUGPRINT(string(__PRETTY_FUNCTION__) + " begin");
- map<string, string>::iterator it;
- root["command"] = "help";
- for (it = helpmap.begin(); it != helpmap.end(); it++) {
- arr.append(it->first + " - " + it->second);
- }
- root["names"] = arr;
- retval.type = print;
- retval.msg = root;
- return retval;
- }
- CmdMan::CmdRet CmdMan::cmdStatus(vector<string> args) {
- CmdRet retval;
- DEBUGPRINT(string(__PRETTY_FUNCTION__) + " begin");
- Json::Value root;
- root["command"] = "status";
- if (currentState == normal) {
- retval.type = send;
- } else if (currentState == connectionpossible || currentState == disconnecttoexit || currentState == disconnecttoexitearly) {
- retval.type = print;
- root["response"] = "You are not connected.";
- } else {
- retval.type = print;
- root["response"] = "Connected to " + ip + ":" + std::to_string(port) + ". Not logged in.";
- }
- retval.msg = root;
- return retval;
- }
- CmdMan::CmdRet CmdMan::cmdExtendedstatus(vector<string> args) {
- CmdRet retval;
- DEBUGPRINT(string(__PRETTY_FUNCTION__) + " begin");
- Json::Value root;
- root["command"] = "extendedstatus";
- retval.type = send;
- retval.msg = root;
- return retval;
- }
- CmdMan::CmdRet CmdMan::cmdDisconnect(vector<string> args) {
- CmdRet retval;
- DEBUGPRINT(string(__PRETTY_FUNCTION__) + " begin");
- Json::Value root;
- retval.type = send;
- if (currentState == loginpossible || currentState == disconnecttoexitearly) {
- // not logged in, send appropriate login message instead of normal close
- root["login"] = false;
- root["user"] = "";
- root["pass"] = "";
- root["cancel"] = true;
- retval.type |= close | noanswerexpected;
- if (currentState == disconnecttoexitearly) {
- retval.nextcommand = "exit";
- }
- currentState = connectionpossible;
- } else {
- root["command"] = "close";
- }
- retval.msg = root;
- return retval;
- }
- CmdMan::CmdRet CmdMan::cmdPut(vector<string> args) {
- CmdRet retval;
- DEBUGPRINT(string(__PRETTY_FUNCTION__) + " begin");
- Json::Value root;
- if (args.size() < 1) {
- retval.type = error;
- root["command"] = "error";
- root["error"] = "Not enough arguments, put requires at least 1 argument.";
- } else {
- root["command"] = "put";
- if (fileman.isPutting()) {
- retval.type = error;
- root["file"] = args[0];
- root["accept"] = false;
- root["error"] = "already putting file \"" + fileman.getPutName() + "\"";
- } else {
- bool opened = fileman.openPut(args[0]);
- if (opened) {
- root["file"] = fileman.getPutName();
- root["size"] = fileman.getPutSize();
- root["chunks"] = fileman.getPutChunks();
- retval.type = send;
- } else {
- retval.type = error;
- root["file"] = fileman.pathToFilename(args[0]);
- root["accept"] = false;
- root["error"] = "couldnt open local file \"" + args[0] + "\"";
- }
- }
- }
- retval.msg = root;
- return retval;
- }
- CmdMan::CmdRet CmdMan::cmdPutdata(vector<string> args) {
- CmdRet retval;
- DEBUGPRINT(string(__PRETTY_FUNCTION__) + " begin");
- Json::Value root;
- root["command"] = "putdata";
- if (!fileman.isPutting()) {
- root["error"] = "Client cannot handle input (received command \"putdata\").";
- retval.type = error;
- } else {
- root["file"] = fileman.getPutName();
- root["cancel"] = false;
- root["data"] = fileman.readBase64();
- root["remaining"] = fileman.getPutRemainingChunks(); // number already decremented by readBase64
- retval.type = send;
- }
- retval.msg = root;
- return retval;
- }
- CmdMan::CmdRet CmdMan::cmdGet(vector<string> args) {
- CmdRet retval;
- DEBUGPRINT(string(__PRETTY_FUNCTION__) + " begin");
- Json::Value root;
- if (args.size() < 1) {
- retval.type = error;
- root["command"] = "error";
- root["error"] = "Not enough arguments, get requires at least 1 argument.";
- } else {
- root["command"] = "get";
- if (fileman.isGetting()) {
- retval.type = error;
- root["file"] = args[0];
- root["accept"] = false;
- root["error"] = "already getting file \"" + fileman.getGetName() + "\"";
- } else {
- bool opened = fileman.openGet(args[0]);
- root["file"] = fileman.getGetName();
- if (opened) {
- root["file"] = fileman.getGetName();
- retval.type = send;
- } else {
- root["file"] = fileman.pathToFilename(args[0]);
- root["accept"] = false;
- root["error"] = "local file \"" + args[0] + "\" already exists";
- retval.type = error;
- }
- }
- }
- retval.msg = root;
- return retval;
- }
- CmdMan::CmdRet CmdMan::cmdGetdata(vector<string> args) {
- CmdRet retval;
- DEBUGPRINT(string(__PRETTY_FUNCTION__) + " begin");
- Json::Value root;
- root["command"] = "getdata";
- if (!fileman.isGetting()) {
- root["error"] = "Client cannot handle input (received command \"getdata\").";
- retval.type = error;
- } else {
- root["file"] = fileman.getGetName();
- root["chunk"] = fileman.getGetRemainingChunks();
- root["cancel"] = false;
- retval.type = send;
- }
- retval.msg = root;
- return retval;
- }
- CmdMan::CmdRet CmdMan::cmdList(vector<string> args) {
- CmdRet retval;
- DEBUGPRINT(string(__PRETTY_FUNCTION__) + " begin");
- Json::Value root;
- bool opened = fileman.openList(false);
- root["command"] = "list";
- if (opened) {
- retval.type = send;
- } else {
- retval.type = error;
- root["accept"] = false;
- root["names"] = "";
- root["error"] = "cannot list, already listing";
- }
- retval.msg = root;
- return retval;
- }
- CmdMan::CmdRet CmdMan::cmdListdata(vector<string> args) {
- CmdRet retval;
- DEBUGPRINT(string(__PRETTY_FUNCTION__) + " begin");
- Json::Value root;
- if (!fileman.isListingSimple()) {
- root["command"] = "list";
- root["error"] = "Client cannot handle input (received command \"listdata\").";
- retval.type = error;
- } else {
- root["command"] = "listdata";
- root["chunk"] = fileman.getListRemainingChunks();
- root["cancel"] = false;
- retval.type = send;
- }
- retval.msg = root;
- return retval;
- }
- CmdMan::CmdRet CmdMan::cmdExtendedlist(vector<string> args) {
- CmdRet retval;
- DEBUGPRINT(string(__PRETTY_FUNCTION__) + " begin");
- Json::Value root;
- bool opened = fileman.openList(true);
- root["command"] = "extendedlist";
- if (opened) {
- retval.type = send;
- } else {
- retval.type = error;
- root["accept"] = false;
- root["names"] = "";
- root["error"] = "cannot list, already listing";
- }
- retval.msg = root;
- return retval;
- }
- CmdMan::CmdRet CmdMan::cmdExtendedlistdata(vector<string> args) {
- CmdRet retval;
- DEBUGPRINT(string(__PRETTY_FUNCTION__) + " begin");
- Json::Value root;
- if (!fileman.isListingExtended()) {
- root["command"] = "extendedlist";
- root["error"] = "Client cannot handle input (received command \"listdata\").";
- retval.type = error;
- } else {
- root["command"] = "extendedlistdata";
- root["chunk"] = fileman.getListRemainingChunks();
- root["cancel"] = false;
- retval.type = send;
- }
- retval.msg = root;
- return retval;
- }
- CmdMan::CmdRet CmdMan::cmdHead(vector<string> args) {
- CmdRet retval;
- DEBUGPRINT(string(__PRETTY_FUNCTION__) + " begin");
- Json::Value root;
- if (args.size() < 1) {
- retval.type = error;
- root["command"] = "error";
- root["accept"] = false;
- root["error"] = "Not enough arguments, head requires at least 1 argument.";
- } else {
- root["command"] = "head";
- root["file"] = args[0];
- retval.type = send;
- }
- retval.msg = root;
- return retval;
- }
- CmdMan::CmdRet CmdMan::cmdDeletefile(vector<string> args) {
- CmdRet retval;
- DEBUGPRINT(string(__PRETTY_FUNCTION__) + " begin");
- Json::Value root;
- if (args.size() < 1) {
- retval.type = error;
- root["command"] = "error";
- root["accept"] = false;
- root["error"] = "Not enough arguments, deletefile requires at least 1 argument.";
- } else {
- root["command"] = "deletefile";
- root["file"] = args[0];
- retval.type = send;
- }
- retval.msg = root;
- return retval;
- }
- CmdMan::CmdRet CmdMan::cmdConnect(vector<string> args) {
- CmdRet retval;
- DEBUGPRINT(string(__PRETTY_FUNCTION__) + " begin");
- Json::Value root;
- if (args.size() < 1) {
- retval.type = error;
- root["command"] = "error";
- root["accept"] = false;
- root["error"] = "Not enough arguments, connect requires at least 1 argument.";
- } else if (currentState != connectionpossible) {
- retval.type = error;
- root["command"] = "error"; // analogous to execute() method when command unavailable
- root["error"] = "Connecting not possible, you are already connected to a server.";
- } else {
- // set internal ip and port fields first
- ip = args[0];
- if (args.size() < 2) {
- port = 1234;
- } else {
- port = (unsigned int)stoul(args[1]);
- }
- // construct json
- retval.type = connect;
- root["command"] = "connect";
- root["address"] = ip;
- root["port"] = port;
- }
- retval.msg = root;
- return retval;
- }
- CmdMan::CmdRet CmdMan::execute(string cmd, vector<string> args) {
- DEBUGPRINT(string(__PRETTY_FUNCTION__) + " begin");
- DEBUGPRINT(string(__PRETTY_FUNCTION__) + " using command \"" + cmd + "\" with arguments [ ");
- for (string s : args)
- DEBUGPRINT(s + " ");
- DEBUGPRINT("]");
- cmdmutex.lock();
- map<string, CmdRet (CmdMan::*)(vector<string>)>::iterator execit = execmap.find(cmd);
- vector<string>::const_iterator alwaysit;
- vector<string>::const_iterator connectit;
- for (alwaysit = cmdAllowAlways.cbegin(); alwaysit != cmdAllowAlways.cend(); alwaysit++)
- if (*alwaysit == cmd)
- break;
- for (connectit = cmdAllowAfterConnect.cbegin(); connectit != cmdAllowAfterConnect.cend(); connectit++)
- if (*connectit == cmd)
- break;
- CmdRet retval;
- Json::Value root;
- root["command"] = cmd;
- if (execit == execmap.end()) {
- retval.type = error;
- root["command"] = "error";
- root["error"] = string(__PRETTY_FUNCTION__) + " unknown command \"" + cmd + "\".\ntype help to list available commands.";
- retval.msg = root;
- cmdmutex.unlock();
- return retval;
- } else if (alwaysit != cmdAllowAlways.cend()) {
- // Command should be usable in all cases
- } else if (currentState == loginpossible || currentState == dologin || currentState == dosignup) {
- DEBUGPRINT("execute does login");
- DEBUGPRINT(string("cmd is in usable commands ") + std::to_string(connectit != cmdAllowAfterConnect.end()));
- if (connectit == cmdAllowAfterConnect.cend()) {
- // Command was NOT in list of usable commands after login
- string allowedCommands;
- for (string s : cmdAllowAlways)
- allowedCommands += s + " ";
- for (string s : cmdAllowAfterConnect)
- allowedCommands += s + " ";
- retval.type = error;
- root["command"] = "error";
- root["error"] = string("Not logged in. Available commands are limited to ") + allowedCommands + "\n" + "Use help for usage of these commands.";
- retval.msg = root;
- cmdmutex.unlock();
- return retval;
- }
- } else if (currentState == versionpossible || currentState == doversion) {
- DEBUGPRINT("execute does version");
- DEBUGPRINT(string("comparison is ") + std::to_string(cmd.compare("version")));
- if (cmd.compare("version")) {
- retval.type = error;
- root["command"] = "error";
- root["error"] = string("Version not checked yet. No commands avalable.");
- retval.msg = root;
- cmdmutex.unlock();
- return retval;
- }
- } else if (currentState == connectionpossible) {
- DEBUGPRINT("execute does connect");
- DEBUGPRINT(string("comparison is ") + std::to_string(cmd.compare("connect")));
- if (cmd.compare("version") && cmd.compare("connect")) {
- retval.type = error;
- root["command"] = "error";
- root["error"] = "Not connected. Please connect.";
- retval.msg = root;
- cmdmutex.unlock();
- return retval;
- }
- }
- retval = (this->*(execmap[cmd]))(args);
- cmdmutex.unlock();
- return retval;
- }
- CmdMan::CmdRet CmdMan::cmdDeleteme(vector<string> args) {
- CmdRet retval;
- DEBUGPRINT(string(__PRETTY_FUNCTION__) + " begin");
- Json::Value root;
- if (args.size() < 1) {
- retval.type = error;
- root["command"] = "error";
- root["error"] = "Not enough arguments, deleteme requires at least 1 argument.";
- } else {
- retval.type = send;
- root["command"] = "deleteme";
- root["pass"] = args[0];
- }
- retval.msg = root;
- return retval;
- }
- CmdMan::CmdRet CmdMan::cmdKeyfile(vector<string> args) {
- CmdRet retval;
- Json::Value root;
- DEBUGPRINT(string(__PRETTY_FUNCTION__) + " begin");
- if (args.size() < 1) {
- retval.type = error;
- root["command"] = "error";
- root["error"] = "Not enough arguments, keyfile requires at least 1 argument.";
- } else {
- DEBUGPRINT(string(__PRETTY_FUNCTION__) + " haveargs");
- root["command"] = "keyfile";
- if (!fileman.openKey(args[0])) {
- DEBUGPRINT(string(__PRETTY_FUNCTION__) + " openkey fail");
- root["accept"] = false;
- root["file"] = args[0];
- root["error"] = string("couldnt open keyfile, openssl reports: ") + fileman.getOpensslError();
- retval.type = error;
- } else {
- DEBUGPRINT(string(__PRETTY_FUNCTION__) + " openkey good");
- root["accept"] = true;
- root["file"] = args[0];
- retval.type = print;
- }
- }
- retval.msg = root;
- return retval;
- }
- CmdMan::CmdRet CmdMan::cmdClosekey(vector<string> args) {
- CmdRet retval;
- Json::Value root;
- DEBUGPRINT(string(__PRETTY_FUNCTION__) + " begin");
- root["command"] = "closekey";
- if (!fileman.closeKey()) {
- root["accept"] = false;
- root["error"] = "couldnt close keyfile. ensure no put or get is running";
- retval.type = error;
- } else {
- root["accept"] = true;
- retval.type = print;
- }
- retval.msg = root;
- return retval;
- }
- CmdMan::CmdRet CmdMan::cmdExit(vector<string> args) {
- CmdRet retval;
- Json::Value root;
- DEBUGPRINT(string(__PRETTY_FUNCTION__) + " begin");
- root["command"] = "exit";
- if (currentState != connectionpossible) {
- // we are connected, disconnect first
- retval.nextcommand = "disconnect";
- if (currentState == loginpossible)
- currentState = disconnecttoexitearly;
- else
- currentState = disconnecttoexit;
- retval.type = none;
- } else {
- retval.type = exit;
- }
- retval.msg = root;
- return retval;
- }
- /* login and signup commands */
- CmdMan::CmdRet CmdMan::cmdLogin(vector<string> args) {
- CmdRet retval;
- DEBUGPRINT(string(__PRETTY_FUNCTION__) + " begin");
- Json::Value root;
- if (args.size() < 2) {
- retval.type = error;
- root["command"] = "error";
- root["error"] = "Not enough arguments, login requires at least 2 arguments.";
- } else {
- if (currentState == loginpossible) {
- currentState = dologin;
- root["user"] = username = args[0];
- root["pass"] = args[1];
- root["login"] = true;
- root["cancel"] = false;
- retval.type = send;
- } else {
- root["command"] = "login";
- root["error"] = "Login not possible, because you already requested a login "
- "or you are logged in";
- root["accept"] = false;
- retval.type = error;
- }
- }
- retval.msg = root;
- return retval;
- }
- CmdMan::CmdRet CmdMan::cmdSignup(vector<string> args) {
- CmdRet retval;
- DEBUGPRINT(string(__PRETTY_FUNCTION__) + " begin");
- Json::Value root;
- if (args.size() < 2) {
- retval.type = error;
- root["command"] = "error";
- root["error"] = "Not enough arguments, signup requires at least 2 arguments.";
- } else {
- if (currentState == loginpossible) {
- currentState = dosignup;
- root["user"] = username = args[0];
- root["pass"] = args[1];
- root["login"] = false;
- root["cancel"] = false;
- retval.type = send;
- } else {
- root["command"] = "signup";
- root["error"] = "Signup not possible, because you already requested a "
- "login or you are logged in";
- root["accept"] = false;
- retval.type = error;
- }
- }
- retval.msg = root;
- return retval;
- }
- CmdMan::CmdRet CmdMan::cmdQueue(vector<string> args) {
- CmdRet retval;
- DEBUGPRINT(string(__PRETTY_FUNCTION__) + " begin");
- Json::Value root;
- if (args.size() < 1) {
- retval.type = error;
- root["command"] = "error";
- root["error"] = "Not enough arguments, queue requires at least 1 argument.";
- } else {
- root["command"] = "queue";
- root["file"] = args[0];
- retval.type = send;
- }
- retval.msg = root;
- return retval;
- }
- CmdMan::CmdRet CmdMan::cmdDequeue(vector<string> args) {
- CmdRet retval;
- DEBUGPRINT(string(__PRETTY_FUNCTION__) + " begin");
- Json::Value root;
- if (args.size() < 1) {
- retval.type = error;
- root["command"] = "error";
- root["error"] = "Not enough arguments, dequeue requires at least 1 argument.";
- } else {
- root["command"] = "dequeue";
- root["file"] = args[0];
- retval.type = send;
- }
- retval.msg = root;
- return retval;
- }
- /* internal commands */
- CmdMan::CmdRet CmdMan::cmdVersion(vector<string> args) {
- CmdRet retval;
- DEBUGPRINT(string(__PRETTY_FUNCTION__) + " begin");
- Json::Value root;
- if (currentState == versionpossible) {
- root["major"] = protocolMajorVersion;
- root["minor"] = protocolMinorVersion;
- retval.type = send;
- currentState = doversion;
- } else {
- retval.type = error;
- root["command"] = "error";
- root["error"] = "Executing version command not possible. Type help to list available commands.";
- }
- retval.msg = root;
- return retval;
- }
- CmdMan::CmdRet CmdMan::cmdNotifications(vector<string> args) {
- CmdRet retval;
- DEBUGPRINT(string(__PRETTY_FUNCTION__) + " begin");
- Json::Value root;
- root["command"] = "notifications";
- retval.type = send;
- retval.msg = root;
- return retval;
- }
- CmdMan::CmdRet CmdMan::handle(Json::Value root) {
- CmdRet retval;
- Json::Value output;
- DEBUGPRINT(string(__PRETTY_FUNCTION__) + " begin");
- cmdmutex.lock();
- if (currentState == doversion)
- root["command"] = "version";
- else if (currentState == dosignup)
- root["command"] = "signup";
- else if (currentState == dologin)
- root["command"] = "login";
- DEBUGPRINT(string(__PRETTY_FUNCTION__) + " using json\n" + Json::writeString(wbuilder, root) + "\n");
- string retmsg;
- map<string, CmdRet (CmdMan::*)(Json::Value)>::iterator it = handlemap.find(root["command"].asString());
- if (it == handlemap.end()) {
- retval.type = error;
- output["command"] = "error";
- output["error"] = string(__PRETTY_FUNCTION__) + " unknown command \"" + root["command"].asString() + "\".\nEnsure code is implemented.";
- retval.msg = output;
- cmdmutex.unlock();
- return retval;
- }
- retval = (this->*(handlemap[root["command"].asString()]))(root);
- cmdmutex.unlock();
- return retval;
- }
- CmdMan::CmdRet CmdMan::handleStatus(Json::Value root) {
- CmdRet retval;
- Json::Value output;
- DEBUGPRINT(string(__PRETTY_FUNCTION__) + " begin");
- retval.type = print;
- output["command"] = "status";
- output["response"] = username + "@" + ip + ":" + std::to_string(port) + " - Status: " + root["response"].asString();
- retval.msg = output;
- return retval;
- }
- CmdMan::CmdRet CmdMan::handleExtendedstatus(Json::Value root) {
- CmdRet retval;
- DEBUGPRINT(string(__PRETTY_FUNCTION__) + " begin");
- if (!root["accept"].asBool()) {
- retval.type = error;
- } else {
- retval.type = print;
- }
- retval.msg = root;
- return retval;
- }
- CmdMan::CmdRet CmdMan::handleClose(Json::Value root) {
- CmdRet retval;
- Json::Value output;
- DEBUGPRINT(string(__PRETTY_FUNCTION__) + " begin");
- output["command"] = "disconnect";
- output["accept"] = true;
- retval.type = close | print;
- retval.msg = output;
- if (currentState == disconnecttoexit) {
- retval.nextcommand = "exit";
- }
- currentState = connectionpossible;
- return retval;
- }
- CmdMan::CmdRet CmdMan::handlePut(Json::Value root) {
- CmdRet retval;
- Json::Value output;
- output["command"] = "put";
- output["file"] = fileman.getPutName();
- output["accept"] = false;
- DEBUGPRINT(string(__PRETTY_FUNCTION__) + " begin");
- if (!root["accept"].asBool()) {
- retval.type = error;
- output["error"] = "Server reports: " + root["error"].asString();
- fileman.cancelPut();
- } else if (!fileman.isPutting()) {
- retval.type = error;
- output["error"] = "Server responds to put message which was never sent.";
- } else if (root["file"].asString() != fileman.getPutName()) {
- retval.type = error;
- output["error"] = "Server reports filename " + root["file"].asString() + " but actual filename is " + fileman.getPutName();
- fileman.cancelPut();
- } else {
- output["accept"] = true;
- output["error"] = "";
- retval.type = print | send;
- retval.nextcommand = "putdata";
- }
- retval.msg = output;
- return retval;
- }
- CmdMan::CmdRet CmdMan::handlePutdata(Json::Value root) {
- CmdRet retval;
- Json::Value output;
- DEBUGPRINT(string(__PRETTY_FUNCTION__) + " begin");
- output["command"] = "putdata";
- output["file"] = fileman.getPutName();
- output["cancel"] = true;
- if (root["cancel"].asBool()) {
- retval.type = error;
- output["error"] = "Server reports: " + root["error"].asString();
- fileman.cancelPut();
- } else if (!fileman.isPutting()) {
- retval.type = error;
- output["error"] = "Server responds to put message which was never sent.";
- } else if (root["received"].asInt() != fileman.getPutRemainingChunks()) {
- // the number of remaining chunks received from the daemon does not equal
- // the number stored at the client side
- retval.type = error;
- output["error"] = std::string("Server reports number of "
- "remaining chunks as ") +
- std::to_string(root["received"].asInt()) + " but actual number is " + std::to_string(fileman.getPutRemainingChunks());
- fileman.cancelPut();
- } else if (root["file"].asString() != fileman.getPutName()) {
- retval.type = error;
- output["error"] = "Server reports filename " + root["file"].asString() + " but actual filename is " + fileman.getPutName();
- fileman.cancelPut();
- } else {
- output["cancel"] = false;
- output["error"] = "";
- // sent successfully
- if (!root["received"].asInt()) {
- // everything sent
- retval.type = print;
- fileman.closePut();
- } else {
- retval.type = print | send;
- retval.nextcommand = "putdata";
- }
- }
- retval.msg = output;
- return retval;
- }
- CmdMan::CmdRet CmdMan::handleGet(Json::Value root) {
- CmdRet retval;
- Json::Value output;
- output["command"] = "get";
- output["file"] = fileman.getGetName();
- output["accept"] = false;
- DEBUGPRINT(string(__PRETTY_FUNCTION__) + " begin");
- if (!root["accept"].asBool()) {
- retval.type = error;
- output["error"] = "Server reports: " + root["error"].asString();
- fileman.cancelGet();
- } else if (!fileman.isGetting()) {
- retval.type = error;
- output["error"] = "Server responds to get message which was never sent.";
- } else if (root["file"].asString() != fileman.getGetName()) {
- retval.type = error;
- output["error"] = "Server reports filename " + root["file"].asString() + " but actual filename is " + fileman.getGetName();
- fileman.cancelGet();
- } else {
- fileman.setGetChunks(root["chunks"].asInt());
- output["accept"] = true;
- output["error"] = "";
- retval.type = print | send;
- retval.nextcommand = "getdata";
- }
- retval.msg = output;
- return retval;
- }
- CmdMan::CmdRet CmdMan::handleGetdata(Json::Value root) {
- CmdRet retval;
- Json::Value output;
- DEBUGPRINT(string(__PRETTY_FUNCTION__) + " begin");
- output["command"] = "getdata";
- output["file"] = fileman.getGetName();
- output["cancel"] = true;
- if (root["cancel"].asBool()) {
- retval.type = error;
- output["error"] = "Server reports: " + root["error"].asString();
- fileman.cancelGet();
- } else if (!fileman.isGetting()) {
- retval.type = error;
- output["error"] = "Server responds to get message which was never sent.";
- } else if (root["remaining"].asInt() != fileman.getGetRemainingChunks()) {
- retval.type = error;
- output["error"] = std::string("Server reports number of remaining chunks as ") + std::to_string(root["remaining"].asInt()) + " but actual number is " +
- std::to_string(fileman.getGetRemainingChunks());
- fileman.cancelGet();
- } else if (root["file"].asString() != fileman.getGetName()) {
- retval.type = error;
- output["error"] = "Server reports filename " + root["file"].asString() + " but actual filename is " + fileman.getGetName();
- fileman.cancelGet();
- } else {
- output["cancel"] = false;
- output["error"] = "";
- fileman.writeBase64(root["data"].asString());
- // loaded successfully
- if (!root["remaining"].asInt()) {
- // everything received
- retval.type = print;
- fileman.closeGet();
- } else {
- retval.type = print | send;
- retval.nextcommand = "getdata";
- }
- }
- retval.msg = output;
- return retval;
- }
- CmdMan::CmdRet CmdMan::handleList(Json::Value root) {
- CmdRet retval;
- Json::Value output; // LOCALOUTPUT
- DEBUGPRINT(string(__PRETTY_FUNCTION__) + " begin");
- output["command"] = "list";
- output["names"] = "";
- if (!root["accept"].asBool()) {
- retval.type = error;
- output["accept"] = false;
- output["error"] = "Server reports: " + root["error"].asString();
- fileman.cancelList();
- } else if (root["items"].asInt() == 0) {
- retval.type = print;
- output["accept"] = false;
- output["error"] = "There are no files stored on the server.";
- fileman.closeList();
- } else if (!fileman.isListingSimple()) {
- retval.type = error;
- output["accept"] = false;
- output["error"] = "Server responds to list message which was never sent.";
- } else {
- fileman.setListChunks(root["chunks"].asInt());
- retval.type = send;
- output["accept"] = true;
- retval.nextcommand = "listdata";
- }
- retval.msg = output;
- return retval;
- }
- CmdMan::CmdRet CmdMan::handleListdata(Json::Value root) {
- CmdRet retval;
- Json::Value output, arr;
- DEBUGPRINT(string(__PRETTY_FUNCTION__) + " begin");
- vector<Json::Value> toadd;
- output["command"] = "list";
- output["names"] = "";
- output["accept"] = false;
- if (root["cancel"].asBool()) {
- retval.type = error;
- output["error"] = "Server reports: " + root["error"].asString();
- fileman.cancelList();
- } else if (!fileman.isListingSimple()) {
- retval.type = error;
- output["error"] = "Server responds to list message which was never sent.";
- } else if (root["remaining"].asInt() != fileman.getListRemainingChunks()) {
- // the passed number of recieved chunks should equal the number of sent chunks
- retval.type = error;
- output["error"] = std::string("Server reports number of "
- "remaining chunks as ") +
- std::to_string(root["remaining"].asInt()) + " but actual number is " + std::to_string(fileman.getListRemainingChunks());
- fileman.cancelList();
- } else {
- output["accept"] = true;
- for (Json::Value i : root["names"])
- toadd.push_back(i);
- fileman.putListData(toadd);
- // loaded successfully
- if (root["remaining"] <= 0) {
- // everything sent
- retval.type = print;
- for (Json::Value s : fileman.getListData())
- arr.append(s.asString());
- output["names"] = arr;
- fileman.closeList();
- } else {
- retval.type = send;
- retval.nextcommand = "listdata";
- }
- }
- retval.msg = output;
- return retval;
- }
- CmdMan::CmdRet CmdMan::handleExtendedlist(Json::Value root) {
- CmdRet retval;
- Json::Value output, files; // LOCALOUTPUT
- DEBUGPRINT(string(__PRETTY_FUNCTION__) + " begin");
- output["command"] = "extendedlist";
- output["files"] = files;
- if (!root["accept"].asBool()) {
- retval.type = error;
- output["accept"] = false;
- output["error"] = "Server reports: " + root["error"].asString();
- fileman.cancelList();
- } else if (root["items"].asInt() == 0) {
- retval.type = print;
- output["accept"] = false;
- output["error"] = "There are no files stored on the server.";
- fileman.closeList();
- } else if (!fileman.isListingExtended()) {
- retval.type = error;
- output["accept"] = false;
- output["error"] = "Server responds to list message which was never sent.";
- } else {
- fileman.setListChunks(root["chunks"].asInt());
- retval.type = send;
- output["accept"] = true;
- retval.nextcommand = "extendedlistdata";
- }
- retval.msg = output;
- return retval;
- }
- CmdMan::CmdRet CmdMan::handleExtendedlistdata(Json::Value root) {
- CmdRet retval;
- Json::Value output, arr, files;
- DEBUGPRINT(string(__PRETTY_FUNCTION__) + " begin");
- vector<Json::Value> toadd;
- output["command"] = "extendedlist";
- output["files"] = files;
- output["accept"] = false;
- if (root["cancel"].asBool()) {
- retval.type = error;
- output["error"] = "Server reports: " + root["error"].asString();
- fileman.cancelList();
- } else if (!fileman.isListingExtended()) {
- retval.type = error;
- output["error"] = "Server responds to list message which was never sent.";
- } else if (root["remaining"].asInt() != fileman.getListRemainingChunks()) {
- // the passed number of recieved chunks should equal the number of sent chunks
- retval.type = error;
- output["error"] = std::string("Server reports number of "
- "remaining chunks as ") +
- std::to_string(root["remaining"].asInt()) + " but actual number is " + std::to_string(fileman.getListRemainingChunks());
- fileman.cancelList();
- } else {
- output["accept"] = true;
- for (Json::Value i : root["files"])
- toadd.push_back(i);
- fileman.putListData(toadd);
- // loaded successfully
- if (root["remaining"] <= 0) {
- // everything sent
- retval.type = print;
- for (Json::Value s : fileman.getListData())
- arr.append(s);
- output["files"] = arr;
- fileman.closeList();
- } else {
- retval.type = send;
- retval.nextcommand = "extendedlistdata";
- }
- }
- retval.msg = output;
- return retval;
- }
- CmdMan::CmdRet CmdMan::handleVersion(Json::Value root) {
- CmdRet retval;
- Json::Value output; // LOCALOUTPUT
- DEBUGPRINT(string(__PRETTY_FUNCTION__) + " begin");
- output["command"] = "version";
- output["serverversion"] = std::to_string(root["major"].asInt()) + "." + std::to_string(root["minor"].asInt());
- output["clientversion"] = std::to_string(protocolMajorVersion) + "." + std::to_string(protocolMinorVersion);
- if (!root["accept"].asBool()) {
- retval.type = error | close;
- output["accept"] = false;
- currentState = connectionpossible;
- } else {
- retval.type = print;
- output["accept"] = true;
- currentState = loginpossible;
- }
- retval.msg = output;
- return retval;
- }
- CmdMan::CmdRet CmdMan::handleLogin(Json::Value root) {
- CmdRet retval;
- Json::Value output; // LOCALOUTPUT
- DEBUGPRINT(string(__PRETTY_FUNCTION__) + " begin");
- output["command"] = "login";
- if (!root["accept"].asBool()) {
- retval.type = error | close;
- output["error"] = root["error"].asString();
- output["accept"] = false;
- currentState = connectionpossible;
- } else {
- retval.type = print;
- output["error"] = "";
- output["accept"] = true;
- currentState = normal;
- }
- retval.msg = output;
- return retval;
- }
- CmdMan::CmdRet CmdMan::handleSignup(Json::Value root) {
- CmdRet retval;
- Json::Value output; // LOCALOUTPUT
- DEBUGPRINT(string(__PRETTY_FUNCTION__) + " begin");
- output["command"] = "signup";
- if (!root["accept"].asBool()) {
- retval.type = error;
- output["error"] = root["error"].asString();
- output["accept"] = false;
- currentState = loginpossible;
- } else {
- retval.type = print;
- output["error"] = "";
- output["accept"] = true;
- currentState = normal;
- }
- retval.msg = output;
- return retval;
- }
- CmdMan::CmdRet CmdMan::handleHead(Json::Value root) {
- CmdRet retval;
- DEBUGPRINT(string(__PRETTY_FUNCTION__) + " begin");
- if (!root["accept"].asBool()) {
- Json::Value output;
- output["command"] = "head";
- output["file"] = root["file"];
- output["error"] = "Server reports: " + root["error"].asString();
- output["accept"] = false;
- retval.type = error;
- retval.msg = output;
- } else {
- retval.type = print;
- retval.msg = root;
- }
- return retval;
- }
- CmdMan::CmdRet CmdMan::handleDeletefile(Json::Value root) {
- CmdRet retval;
- DEBUGPRINT(string(__PRETTY_FUNCTION__) + " begin");
- if (!root["accept"].asBool()) {
- Json::Value output;
- output["command"] = "deletefile";
- output["file"] = root["file"];
- output["error"] = "Server reports: " + root["error"].asString();
- output["accept"] = false;
- retval.type = error;
- retval.msg = output;
- } else {
- retval.type = print;
- retval.msg = root;
- }
- return retval;
- }
- CmdMan::CmdRet CmdMan::handleDeleteme(Json::Value root) {
- CmdRet retval;
- DEBUGPRINT(string(__PRETTY_FUNCTION__) + " begin");
- if (!root["accept"].asBool()) {
- retval.type = error;
- } else {
- retval.type = close | print;
- currentState = connectionpossible;
- }
- retval.msg = root;
- return retval;
- }
- CmdMan::CmdRet CmdMan::handleQueue(Json::Value root) {
- CmdRet retval;
- DEBUGPRINT(string(__PRETTY_FUNCTION__) + " begin");
- if (root["accept"].asBool()) {
- retval.type = print;
- } else {
- retval.type = error;
- }
- retval.msg = root;
- return retval;
- }
- CmdMan::CmdRet CmdMan::handleDequeue(Json::Value root) {
- CmdRet retval;
- DEBUGPRINT(string(__PRETTY_FUNCTION__) + " begin");
- if (root["accept"].asBool()) {
- retval.type = print;
- } else {
- retval.type = error;
- }
- retval.msg = root;
- return retval;
- }
- CmdMan::CmdRet CmdMan::handleNotifications(Json::Value root) {
- CmdRet retval;
- DEBUGPRINT(string(__PRETTY_FUNCTION__) + " begin");
- if (root["accept"].asBool()) {
- retval.type = print;
- } else {
- retval.type = error;
- }
- retval.msg = root;
- return retval;
- }
|