|
@@ -14,8 +14,10 @@ CmdMan::CmdMan(FileMan &fm, void (*dpf)(string)) : fileman(fm) {
|
|
wbuilder.settings_["indentation"] = "";
|
|
wbuilder.settings_["indentation"] = "";
|
|
reader = rbuilder.newCharReader();
|
|
reader = rbuilder.newCharReader();
|
|
|
|
|
|
- dologin = false;
|
|
|
|
doversion = false;
|
|
doversion = false;
|
|
|
|
+ loginpossible = false;
|
|
|
|
+ dologin = false;
|
|
|
|
+ dosignup = false;
|
|
|
|
|
|
/* initialize execute command map */
|
|
/* initialize execute command map */
|
|
execmap["help"] = &CmdMan::cmdHelp;
|
|
execmap["help"] = &CmdMan::cmdHelp;
|
|
@@ -26,6 +28,7 @@ CmdMan::CmdMan(FileMan &fm, void (*dpf)(string)) : fileman(fm) {
|
|
execmap["list"] = &CmdMan::cmdList;
|
|
execmap["list"] = &CmdMan::cmdList;
|
|
execmap["version"] = &CmdMan::cmdVersion;
|
|
execmap["version"] = &CmdMan::cmdVersion;
|
|
execmap["login"] = &CmdMan::cmdLogin;
|
|
execmap["login"] = &CmdMan::cmdLogin;
|
|
|
|
+ execmap["signup"] = &CmdMan::cmdSignup;
|
|
execmap["putdata"] = &CmdMan::cmdPutdata;
|
|
execmap["putdata"] = &CmdMan::cmdPutdata;
|
|
execmap["getdata"] = &CmdMan::cmdGetdata;
|
|
execmap["getdata"] = &CmdMan::cmdGetdata;
|
|
|
|
|
|
@@ -47,6 +50,7 @@ CmdMan::CmdMan(FileMan &fm, void (*dpf)(string)) : fileman(fm) {
|
|
handlemap["list"] = &CmdMan::handleList;
|
|
handlemap["list"] = &CmdMan::handleList;
|
|
handlemap["version"] = &CmdMan::handleVersion;
|
|
handlemap["version"] = &CmdMan::handleVersion;
|
|
handlemap["login"] = &CmdMan::handleLogin;
|
|
handlemap["login"] = &CmdMan::handleLogin;
|
|
|
|
+ handlemap["signup"] = &CmdMan::handleSignup;
|
|
|
|
|
|
debugprintfunc = dpf;
|
|
debugprintfunc = dpf;
|
|
}
|
|
}
|
|
@@ -83,8 +87,17 @@ CmdMan::CmdRet CmdMan::cmdDisconnect(vector<string> args) {
|
|
CmdRet retval;
|
|
CmdRet retval;
|
|
DEBUGPRINT(string(__PRETTY_FUNCTION__) + " begin");
|
|
DEBUGPRINT(string(__PRETTY_FUNCTION__) + " begin");
|
|
Json::Value root;
|
|
Json::Value root;
|
|
- root["command"] = "close";
|
|
|
|
retval.type = send;
|
|
retval.type = send;
|
|
|
|
+ if (loginpossible) {
|
|
|
|
+ // not logged in, send appropriate login message instead of normal close
|
|
|
|
+ root["login"] = false;
|
|
|
|
+ root["user"] = "";
|
|
|
|
+ root["pass"] = "";
|
|
|
|
+ root["cancel"] = true;
|
|
|
|
+ retval.type |= close;
|
|
|
|
+ } else {
|
|
|
|
+ root["command"] = "close";
|
|
|
|
+ }
|
|
retval.msg = root;
|
|
retval.msg = root;
|
|
|
|
|
|
return retval;
|
|
return retval;
|
|
@@ -204,61 +217,113 @@ CmdMan::CmdRet CmdMan::execute(string cmd, vector<string> args) {
|
|
"\" with arguments [ ");
|
|
"\" with arguments [ ");
|
|
for (string s : args)
|
|
for (string s : args)
|
|
DEBUGPRINT(s + " ");
|
|
DEBUGPRINT(s + " ");
|
|
- DEBUGPRINT("]\n");
|
|
|
|
|
|
+ DEBUGPRINT("]");
|
|
map<string, CmdRet (CmdMan::*)(vector<string>)>::iterator it =
|
|
map<string, CmdRet (CmdMan::*)(vector<string>)>::iterator it =
|
|
execmap.find(cmd);
|
|
execmap.find(cmd);
|
|
- string retmsg;
|
|
|
|
|
|
+ CmdRet retval;
|
|
|
|
+ Json::Value root;
|
|
|
|
+ root["command"] = cmd;
|
|
if (it == execmap.end()) {
|
|
if (it == execmap.end()) {
|
|
- return {error, string(__PRETTY_FUNCTION__) + " unknown command \"" + cmd +
|
|
|
|
- "\".\ntype help to list available commands."};
|
|
|
|
|
|
+ retval.type = error;
|
|
|
|
+ root["command"] = "error";
|
|
|
|
+ root["error"] = string(__PRETTY_FUNCTION__) + " unknown command \"" + cmd +
|
|
|
|
+ "\".\ntype help to list available commands.";
|
|
|
|
+ retval.msg = root;
|
|
|
|
+ return retval;
|
|
|
|
+ } else if (loginpossible || dologin || dosignup) {
|
|
|
|
+ DEBUGPRINT("execute does login");
|
|
|
|
+ DEBUGPRINT(string("comparison is ") +
|
|
|
|
+ std::to_string(cmd.compare("login") && cmd.compare("signup") &&
|
|
|
|
+ cmd.compare("disconnect") &&
|
|
|
|
+ cmd.compare("help")));
|
|
|
|
+ if (cmd.compare("login") && cmd.compare("signup") &&
|
|
|
|
+ cmd.compare("disconnect") && cmd.compare("help")) {
|
|
|
|
+ retval.type = error;
|
|
|
|
+ root["command"] = "error";
|
|
|
|
+ root["error"] =
|
|
|
|
+ string("Not logged in. Available commands are limited to ") +
|
|
|
|
+ "login" + ", " + "signup" + " and " + "disconnect" + "\n" +
|
|
|
|
+ "Use help for usage of these commands.";
|
|
|
|
+ retval.msg = root;
|
|
|
|
+ return retval;
|
|
|
|
+ }
|
|
}
|
|
}
|
|
return (this->*(execmap[cmd]))(args);
|
|
return (this->*(execmap[cmd]))(args);
|
|
}
|
|
}
|
|
|
|
|
|
-/* internal commands */
|
|
|
|
-CmdMan::CmdRet CmdMan::cmdVersion(vector<string> args) {
|
|
|
|
|
|
+/* login and signup commands */
|
|
|
|
+CmdMan::CmdRet CmdMan::cmdLogin(vector<string> args) {
|
|
CmdRet retval;
|
|
CmdRet retval;
|
|
DEBUGPRINT(string(__PRETTY_FUNCTION__) + " begin");
|
|
DEBUGPRINT(string(__PRETTY_FUNCTION__) + " begin");
|
|
Json::Value root;
|
|
Json::Value root;
|
|
- root["version"] = protocolVersion;
|
|
|
|
- retval.type = send;
|
|
|
|
|
|
+
|
|
|
|
+ if (loginpossible) {
|
|
|
|
+ dologin = true;
|
|
|
|
+ loginpossible = false;
|
|
|
|
+ root["user"] = 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;
|
|
retval.msg = root;
|
|
|
|
+ return retval;
|
|
|
|
+}
|
|
|
|
|
|
- doversion = true;
|
|
|
|
|
|
+CmdMan::CmdRet CmdMan::cmdSignup(vector<string> args) {
|
|
|
|
+ CmdRet retval;
|
|
|
|
+ DEBUGPRINT(string(__PRETTY_FUNCTION__) + " begin");
|
|
|
|
+ Json::Value root;
|
|
|
|
+
|
|
|
|
+ if (loginpossible) {
|
|
|
|
+ dosignup = true;
|
|
|
|
+ loginpossible = false;
|
|
|
|
+ root["user"] = args[0];
|
|
|
|
+ root["pass"] = args[1];
|
|
|
|
+ root["login"] = true;
|
|
|
|
+ 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;
|
|
return retval;
|
|
}
|
|
}
|
|
|
|
|
|
-CmdMan::CmdRet CmdMan::cmdLogin(vector<string> args) {
|
|
|
|
|
|
+/* internal commands */
|
|
|
|
+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["user"] = args[0];
|
|
|
|
- root["pass"] = args[1];
|
|
|
|
- root["login"] = true;
|
|
|
|
- root["cancel"] = false;
|
|
|
|
|
|
+ root["version"] = protocolVersion;
|
|
retval.type = send;
|
|
retval.type = send;
|
|
retval.msg = root;
|
|
retval.msg = root;
|
|
|
|
|
|
- dologin = true;
|
|
|
|
|
|
+ doversion = true;
|
|
|
|
|
|
return retval;
|
|
return retval;
|
|
}
|
|
}
|
|
|
|
|
|
-/*
|
|
|
|
- handlemap["status"] = &CmdMan::handleDefault;
|
|
|
|
- handlemap["disconnect"] = &CmdMan::handleDefault;
|
|
|
|
- handlemap["put"] = &CmdMan::handleDefault;
|
|
|
|
- handlemap["get"] = &CmdMan::handleDefault;
|
|
|
|
- handlemap["list"] = NULL;
|
|
|
|
- handlemap["version"] = NULL;
|
|
|
|
- handlemap["login"] = NULL;
|
|
|
|
-*/
|
|
|
|
-
|
|
|
|
CmdMan::CmdRet CmdMan::handle(Json::Value root) {
|
|
CmdMan::CmdRet CmdMan::handle(Json::Value root) {
|
|
|
|
+ CmdRet retval;
|
|
|
|
+ Json::Value output;
|
|
DEBUGPRINT(string(__PRETTY_FUNCTION__) + " begin");
|
|
DEBUGPRINT(string(__PRETTY_FUNCTION__) + " begin");
|
|
if (doversion)
|
|
if (doversion)
|
|
root["command"] = "version";
|
|
root["command"] = "version";
|
|
|
|
+ else if (dosignup)
|
|
|
|
+ root["command"] = "signup";
|
|
else if (dologin)
|
|
else if (dologin)
|
|
root["command"] = "login";
|
|
root["command"] = "login";
|
|
DEBUGPRINT(string(__PRETTY_FUNCTION__) + " using json\n" +
|
|
DEBUGPRINT(string(__PRETTY_FUNCTION__) + " using json\n" +
|
|
@@ -266,10 +331,15 @@ CmdMan::CmdRet CmdMan::handle(Json::Value root) {
|
|
string retmsg;
|
|
string retmsg;
|
|
map<string, CmdRet (CmdMan::*)(Json::Value)>::iterator it =
|
|
map<string, CmdRet (CmdMan::*)(Json::Value)>::iterator it =
|
|
handlemap.find(root["command"].asString());
|
|
handlemap.find(root["command"].asString());
|
|
|
|
+
|
|
if (it == handlemap.end()) {
|
|
if (it == handlemap.end()) {
|
|
- return {error, string(__PRETTY_FUNCTION__) + " unknown command \"" +
|
|
|
|
- root["command"].asString() +
|
|
|
|
- "\".\nensure code is implemented."};
|
|
|
|
|
|
+ retval.type = error;
|
|
|
|
+ output["command"] = "error";
|
|
|
|
+ output["error"] = string(__PRETTY_FUNCTION__) + " unknown command \"" +
|
|
|
|
+ root["command"].asString() +
|
|
|
|
+ "\".\nEnsure code is implemented.";
|
|
|
|
+ retval.msg = output;
|
|
|
|
+ return retval;
|
|
}
|
|
}
|
|
return (this->*(handlemap[root["command"].asString()]))(root);
|
|
return (this->*(handlemap[root["command"].asString()]))(root);
|
|
}
|
|
}
|
|
@@ -523,6 +593,7 @@ CmdMan::CmdRet CmdMan::handleVersion(Json::Value root) {
|
|
retval.type = print | seton;
|
|
retval.type = print | seton;
|
|
output["accept"] = true;
|
|
output["accept"] = true;
|
|
doversion = false;
|
|
doversion = false;
|
|
|
|
+ loginpossible = true;
|
|
}
|
|
}
|
|
retval.msg = output;
|
|
retval.msg = output;
|
|
|
|
|
|
@@ -540,6 +611,8 @@ 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;
|
|
} else {
|
|
} else {
|
|
retval.type = print | seton;
|
|
retval.type = print | seton;
|
|
output["error"] = "";
|
|
output["error"] = "";
|
|
@@ -550,3 +623,27 @@ CmdMan::CmdRet CmdMan::handleLogin(Json::Value root) {
|
|
|
|
|
|
return retval;
|
|
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;
|
|
|
|
+ loginpossible = true;
|
|
|
|
+ dosignup = false;
|
|
|
|
+ } else {
|
|
|
|
+ retval.type = print | seton;
|
|
|
|
+ output["error"] = "";
|
|
|
|
+ output["accept"] = true;
|
|
|
|
+ dosignup = false;
|
|
|
|
+ }
|
|
|
|
+ retval.msg = output;
|
|
|
|
+
|
|
|
|
+ return retval;
|
|
|
|
+}
|