|
@@ -14,10 +14,7 @@ CmdMan::CmdMan(FileMan &fm, void (*dpf)(string)) : fileman(fm) {
|
|
wbuilder.settings_["indentation"] = "";
|
|
wbuilder.settings_["indentation"] = "";
|
|
reader = rbuilder.newCharReader();
|
|
reader = rbuilder.newCharReader();
|
|
|
|
|
|
- doversion = false;
|
|
|
|
- loginpossible = false;
|
|
|
|
- dologin = false;
|
|
|
|
- dosignup = false;
|
|
|
|
|
|
+ currentState = connectionpossible;
|
|
|
|
|
|
/* initialize execute command map */
|
|
/* initialize execute command map */
|
|
execmap["help"] = &CmdMan::cmdHelp;
|
|
execmap["help"] = &CmdMan::cmdHelp;
|
|
@@ -37,6 +34,8 @@ CmdMan::CmdMan(FileMan &fm, void (*dpf)(string)) : fileman(fm) {
|
|
execmap["deleteme"] = &CmdMan::cmdDeleteme;
|
|
execmap["deleteme"] = &CmdMan::cmdDeleteme;
|
|
execmap["keyfile"] = &CmdMan::cmdKeyfile;
|
|
execmap["keyfile"] = &CmdMan::cmdKeyfile;
|
|
execmap["closekey"] = &CmdMan::cmdClosekey;
|
|
execmap["closekey"] = &CmdMan::cmdClosekey;
|
|
|
|
+ execmap["connect"] = &CmdMan::cmdConnect;
|
|
|
|
+ execmap["exit"] = &CmdMan::cmdExit;
|
|
|
|
|
|
/* initialize description map */
|
|
/* initialize description map */
|
|
helpmap["help"] = descHelp;
|
|
helpmap["help"] = descHelp;
|
|
@@ -52,6 +51,8 @@ CmdMan::CmdMan(FileMan &fm, void (*dpf)(string)) : fileman(fm) {
|
|
helpmap["deleteme"] = descDeleteme;
|
|
helpmap["deleteme"] = descDeleteme;
|
|
helpmap["keyfile"] = descKeyfile;
|
|
helpmap["keyfile"] = descKeyfile;
|
|
helpmap["closekey"] = descClosekey;
|
|
helpmap["closekey"] = descClosekey;
|
|
|
|
+ helpmap["connect"] = descConnect;
|
|
|
|
+ helpmap["exit"] = descExit;
|
|
|
|
|
|
/* initialize handle command map */
|
|
/* initialize handle command map */
|
|
handlemap["status"] = &CmdMan::handleStatus;
|
|
handlemap["status"] = &CmdMan::handleStatus;
|
|
@@ -74,6 +75,8 @@ CmdMan::CmdMan(FileMan &fm, void (*dpf)(string)) : fileman(fm) {
|
|
|
|
|
|
CmdMan::~CmdMan() { delete reader; }
|
|
CmdMan::~CmdMan() { delete reader; }
|
|
|
|
|
|
|
|
+void CmdMan::stateSetConnectionOk() { currentState = versionpossible; }
|
|
|
|
+
|
|
CmdMan::CmdRet CmdMan::cmdHelp(vector<string> args) {
|
|
CmdMan::CmdRet CmdMan::cmdHelp(vector<string> args) {
|
|
CmdRet retval;
|
|
CmdRet retval;
|
|
Json::Value root, arr;
|
|
Json::Value root, arr;
|
|
@@ -105,13 +108,17 @@ CmdMan::CmdRet CmdMan::cmdDisconnect(vector<string> args) {
|
|
DEBUGPRINT(string(__PRETTY_FUNCTION__) + " begin");
|
|
DEBUGPRINT(string(__PRETTY_FUNCTION__) + " begin");
|
|
Json::Value root;
|
|
Json::Value root;
|
|
retval.type = send;
|
|
retval.type = send;
|
|
- if (loginpossible) {
|
|
|
|
|
|
+ if (currentState == loginpossible || currentState == disconnecttoexitearly) {
|
|
// not logged in, send appropriate login message instead of normal close
|
|
// not logged in, send appropriate login message instead of normal close
|
|
root["login"] = false;
|
|
root["login"] = false;
|
|
root["user"] = "";
|
|
root["user"] = "";
|
|
root["pass"] = "";
|
|
root["pass"] = "";
|
|
root["cancel"] = true;
|
|
root["cancel"] = true;
|
|
retval.type |= close;
|
|
retval.type |= close;
|
|
|
|
+ if (currentState == disconnecttoexitearly) {
|
|
|
|
+ retval.nextcommand = "exit";
|
|
|
|
+ }
|
|
|
|
+ currentState = connectionpossible;
|
|
} else {
|
|
} else {
|
|
root["command"] = "close";
|
|
root["command"] = "close";
|
|
}
|
|
}
|
|
@@ -290,6 +297,30 @@ CmdMan::CmdRet CmdMan::cmdDeletefile(vector<string> args) {
|
|
return retval;
|
|
return retval;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+CmdMan::CmdRet CmdMan::cmdConnect(vector<string> args) {
|
|
|
|
+ CmdRet retval;
|
|
|
|
+ DEBUGPRINT(string(__PRETTY_FUNCTION__) + " begin");
|
|
|
|
+ Json::Value root;
|
|
|
|
+
|
|
|
|
+ root["command"] = "connect";
|
|
|
|
+
|
|
|
|
+ if (args.size() < 1) {
|
|
|
|
+ retval.type = error;
|
|
|
|
+ root["error"] = "not enough arguments, at least 1 argument required";
|
|
|
|
+ } else if (args.size() < 2) {
|
|
|
|
+ retval.type = connect;
|
|
|
|
+ root["address"] = args[0];
|
|
|
|
+ root["port"] = 1234;
|
|
|
|
+ } else {
|
|
|
|
+ retval.type = connect;
|
|
|
|
+ root["address"] = args[0];
|
|
|
|
+ root["port"] = (unsigned int)stoul(args[1]);
|
|
|
|
+ }
|
|
|
|
+ retval.msg = root;
|
|
|
|
+
|
|
|
|
+ return retval;
|
|
|
|
+}
|
|
|
|
+
|
|
CmdMan::CmdRet CmdMan::execute(string cmd, vector<string> args) {
|
|
CmdMan::CmdRet CmdMan::execute(string cmd, vector<string> args) {
|
|
DEBUGPRINT(string(__PRETTY_FUNCTION__) + " begin");
|
|
DEBUGPRINT(string(__PRETTY_FUNCTION__) + " begin");
|
|
DEBUGPRINT(string(__PRETTY_FUNCTION__) + " using command \"" + cmd + "\" with arguments [ ");
|
|
DEBUGPRINT(string(__PRETTY_FUNCTION__) + " using command \"" + cmd + "\" with arguments [ ");
|
|
@@ -306,7 +337,9 @@ CmdMan::CmdRet CmdMan::execute(string cmd, vector<string> args) {
|
|
root["error"] = string(__PRETTY_FUNCTION__) + " unknown command \"" + cmd + "\".\ntype help to list available commands.";
|
|
root["error"] = string(__PRETTY_FUNCTION__) + " unknown command \"" + cmd + "\".\ntype help to list available commands.";
|
|
retval.msg = root;
|
|
retval.msg = root;
|
|
return retval;
|
|
return retval;
|
|
- } else if (loginpossible || dologin || dosignup) {
|
|
|
|
|
|
+ } else if (!cmd.compare("help") || !cmd.compare("exit")) {
|
|
|
|
+ // allow help and exit in all cases
|
|
|
|
+ } else if (currentState == loginpossible || currentState == dologin || currentState == dosignup) {
|
|
DEBUGPRINT("execute does login");
|
|
DEBUGPRINT("execute does login");
|
|
DEBUGPRINT(string("comparison is ") +
|
|
DEBUGPRINT(string("comparison is ") +
|
|
std::to_string(cmd.compare("login") && cmd.compare("signup") && cmd.compare("disconnect") && cmd.compare("help")));
|
|
std::to_string(cmd.compare("login") && cmd.compare("signup") && cmd.compare("disconnect") && cmd.compare("help")));
|
|
@@ -318,6 +351,26 @@ CmdMan::CmdRet CmdMan::execute(string cmd, vector<string> args) {
|
|
retval.msg = root;
|
|
retval.msg = root;
|
|
return retval;
|
|
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;
|
|
|
|
+ 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"] = string("Not connected. Connect using \"connect ip [port]\".");
|
|
|
|
+ retval.msg = root;
|
|
|
|
+ return retval;
|
|
|
|
+ }
|
|
}
|
|
}
|
|
return (this->*(execmap[cmd]))(args);
|
|
return (this->*(execmap[cmd]))(args);
|
|
}
|
|
}
|
|
@@ -387,6 +440,26 @@ CmdMan::CmdRet CmdMan::cmdClosekey(vector<string> args) {
|
|
return retval;
|
|
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 */
|
|
/* login and signup commands */
|
|
CmdMan::CmdRet CmdMan::cmdLogin(vector<string> args) {
|
|
CmdMan::CmdRet CmdMan::cmdLogin(vector<string> args) {
|
|
CmdRet retval;
|
|
CmdRet retval;
|
|
@@ -399,9 +472,8 @@ CmdMan::CmdRet CmdMan::cmdLogin(vector<string> args) {
|
|
root["accept"] = false;
|
|
root["accept"] = false;
|
|
root["error"] = "not enough arguments, at least 2 argument required";
|
|
root["error"] = "not enough arguments, at least 2 argument required";
|
|
} else {
|
|
} else {
|
|
- if (loginpossible) {
|
|
|
|
- dologin = true;
|
|
|
|
- loginpossible = false;
|
|
|
|
|
|
+ if (currentState == loginpossible) {
|
|
|
|
+ currentState = dologin;
|
|
root["user"] = args[0];
|
|
root["user"] = args[0];
|
|
root["pass"] = args[1];
|
|
root["pass"] = args[1];
|
|
root["login"] = true;
|
|
root["login"] = true;
|
|
@@ -431,9 +503,8 @@ CmdMan::CmdRet CmdMan::cmdSignup(vector<string> args) {
|
|
root["accept"] = false;
|
|
root["accept"] = false;
|
|
root["error"] = "not enough arguments, at least 2 argument required";
|
|
root["error"] = "not enough arguments, at least 2 argument required";
|
|
} else {
|
|
} else {
|
|
- if (loginpossible) {
|
|
|
|
- dosignup = true;
|
|
|
|
- loginpossible = false;
|
|
|
|
|
|
+ if (currentState == loginpossible) {
|
|
|
|
+ currentState = dosignup;
|
|
root["user"] = args[0];
|
|
root["user"] = args[0];
|
|
root["pass"] = args[1];
|
|
root["pass"] = args[1];
|
|
root["login"] = false;
|
|
root["login"] = false;
|
|
@@ -457,13 +528,18 @@ CmdMan::CmdRet CmdMan::cmdVersion(vector<string> args) {
|
|
CmdRet retval;
|
|
CmdRet retval;
|
|
DEBUGPRINT(string(__PRETTY_FUNCTION__) + " begin");
|
|
DEBUGPRINT(string(__PRETTY_FUNCTION__) + " begin");
|
|
Json::Value root;
|
|
Json::Value root;
|
|
- root["major"] = protocolMajorVersion;
|
|
|
|
- root["minor"] = protocolMinorVersion;
|
|
|
|
- retval.type = send;
|
|
|
|
|
|
+ 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;
|
|
retval.msg = root;
|
|
|
|
|
|
- doversion = true;
|
|
|
|
-
|
|
|
|
return retval;
|
|
return retval;
|
|
}
|
|
}
|
|
|
|
|
|
@@ -471,11 +547,11 @@ CmdMan::CmdRet CmdMan::handle(Json::Value root) {
|
|
CmdRet retval;
|
|
CmdRet retval;
|
|
Json::Value output;
|
|
Json::Value output;
|
|
DEBUGPRINT(string(__PRETTY_FUNCTION__) + " begin");
|
|
DEBUGPRINT(string(__PRETTY_FUNCTION__) + " begin");
|
|
- if (doversion)
|
|
|
|
|
|
+ if (currentState == doversion)
|
|
root["command"] = "version";
|
|
root["command"] = "version";
|
|
- else if (dosignup)
|
|
|
|
|
|
+ else if (currentState == dosignup)
|
|
root["command"] = "signup";
|
|
root["command"] = "signup";
|
|
- else if (dologin)
|
|
|
|
|
|
+ else if (currentState == dologin)
|
|
root["command"] = "login";
|
|
root["command"] = "login";
|
|
DEBUGPRINT(string(__PRETTY_FUNCTION__) + " using json\n" + Json::writeString(wbuilder, root) + "\n");
|
|
DEBUGPRINT(string(__PRETTY_FUNCTION__) + " using json\n" + Json::writeString(wbuilder, root) + "\n");
|
|
string retmsg;
|
|
string retmsg;
|
|
@@ -509,6 +585,11 @@ CmdMan::CmdRet CmdMan::handleClose(Json::Value root) {
|
|
retval.type = close | print;
|
|
retval.type = close | print;
|
|
retval.msg = output;
|
|
retval.msg = output;
|
|
|
|
|
|
|
|
+ if (currentState == disconnecttoexit) {
|
|
|
|
+ retval.nextcommand = "exit";
|
|
|
|
+ }
|
|
|
|
+ currentState = connectionpossible;
|
|
|
|
+
|
|
return retval;
|
|
return retval;
|
|
}
|
|
}
|
|
|
|
|
|
@@ -723,17 +804,17 @@ CmdMan::CmdRet CmdMan::handleVersion(Json::Value root) {
|
|
DEBUGPRINT(string(__PRETTY_FUNCTION__) + " begin");
|
|
DEBUGPRINT(string(__PRETTY_FUNCTION__) + " begin");
|
|
|
|
|
|
output["command"] = "version";
|
|
output["command"] = "version";
|
|
- output["serverversion"] = root["major"].asString() + "." + root["minor"].asString();
|
|
|
|
|
|
+ output["serverversion"] = std::to_string(root["major"].asInt()) + "." + std::to_string(root["minor"].asInt());
|
|
output["clientversion"] = std::to_string(protocolMajorVersion) + "." + std::to_string(protocolMinorVersion);
|
|
output["clientversion"] = std::to_string(protocolMajorVersion) + "." + std::to_string(protocolMinorVersion);
|
|
|
|
|
|
if (!root["accept"].asBool()) {
|
|
if (!root["accept"].asBool()) {
|
|
- retval.type = error;
|
|
|
|
|
|
+ retval.type = error | close;
|
|
output["accept"] = false;
|
|
output["accept"] = false;
|
|
|
|
+ currentState = connectionpossible;
|
|
} else {
|
|
} else {
|
|
- retval.type = print | seton;
|
|
|
|
|
|
+ retval.type = print;
|
|
output["accept"] = true;
|
|
output["accept"] = true;
|
|
- doversion = false;
|
|
|
|
- loginpossible = true;
|
|
|
|
|
|
+ currentState = loginpossible;
|
|
}
|
|
}
|
|
retval.msg = output;
|
|
retval.msg = output;
|
|
|
|
|
|
@@ -751,13 +832,12 @@ CmdMan::CmdRet CmdMan::handleLogin(Json::Value root) {
|
|
retval.type = error;
|
|
retval.type = error;
|
|
output["error"] = root["error"].asString();
|
|
output["error"] = root["error"].asString();
|
|
output["accept"] = false;
|
|
output["accept"] = false;
|
|
- loginpossible = true;
|
|
|
|
- dologin = false;
|
|
|
|
|
|
+ currentState = loginpossible;
|
|
} else {
|
|
} else {
|
|
- retval.type = print | seton;
|
|
|
|
|
|
+ retval.type = print;
|
|
output["error"] = "";
|
|
output["error"] = "";
|
|
output["accept"] = true;
|
|
output["accept"] = true;
|
|
- dologin = false;
|
|
|
|
|
|
+ currentState = normal;
|
|
}
|
|
}
|
|
retval.msg = output;
|
|
retval.msg = output;
|
|
|
|
|
|
@@ -775,13 +855,12 @@ CmdMan::CmdRet CmdMan::handleSignup(Json::Value root) {
|
|
retval.type = error;
|
|
retval.type = error;
|
|
output["error"] = root["error"].asString();
|
|
output["error"] = root["error"].asString();
|
|
output["accept"] = false;
|
|
output["accept"] = false;
|
|
- loginpossible = true;
|
|
|
|
- dosignup = false;
|
|
|
|
|
|
+ currentState = loginpossible;
|
|
} else {
|
|
} else {
|
|
- retval.type = print | seton;
|
|
|
|
|
|
+ retval.type = print;
|
|
output["error"] = "";
|
|
output["error"] = "";
|
|
output["accept"] = true;
|
|
output["accept"] = true;
|
|
- dosignup = false;
|
|
|
|
|
|
+ currentState = normal;
|
|
}
|
|
}
|
|
retval.msg = output;
|
|
retval.msg = output;
|
|
|
|
|
|
@@ -834,6 +913,7 @@ CmdMan::CmdRet CmdMan::handleDeleteme(Json::Value root) {
|
|
retval.type = error;
|
|
retval.type = error;
|
|
} else {
|
|
} else {
|
|
retval.type = close | print;
|
|
retval.type = close | print;
|
|
|
|
+ currentState = connectionpossible;
|
|
}
|
|
}
|
|
retval.msg = root;
|
|
retval.msg = root;
|
|
return retval;
|
|
return retval;
|