changeset 126:3e3824711791

RLG-Web server: allow changing e-mail and password. Logged-in users can change their e-mail addresses and passwords with a POST to the /uinfo interface.
author John "Elwin" Edwards <elwin@sdf.org>
date Sun, 26 Aug 2012 19:04:57 -0700
parents 5ad15380f851
children e54018b26ed8
files rlgwebd.js
diffstat 1 files changed, 46 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/rlgwebd.js	Sat Aug 25 19:33:31 2012 -0700
+++ b/rlgwebd.js	Sun Aug 26 19:04:57 2012 -0700
@@ -1137,6 +1137,49 @@
   }
 }
 
+function setuinfo(req, res, postdata) {
+  var urlobj = url.parse(req.url, true);
+  var query = urlobj.query;
+  if (!("key" in query) || !(query["key"] in logins)) {
+    sendError(res, 1);
+    return;
+  }
+  var name = logins[query["key"]].name;
+  var match = urlobj.pathname.match(/^\/[^\/]*\/(.*)/);
+  if (!match || !match[1]) {
+    send404(res, urlobj.pathname, true);
+    return;
+  }
+  var which = match[1];
+  if (!("v" in postdata)) {
+    sendError(res, 2, "No value provided");
+    return;
+  }
+  if (which == "email" || which == "pw") {
+    var args;
+    if (which == "email")
+      args = ["setmail"];
+    else
+      args = ["setpw"];
+    var child = child_process.execFile("/bin/sqlickrypt", args, 
+                  function (err, stdout, stderr) {
+      if (err) {
+        tslog("Could not set %s: sqlickrypt error %d", which, err.code);
+        sendError(res, 2);
+      }
+      else {
+        tslog("User %s has changed %s", name, which);
+        res.writeHead(200, { "Content-Type": "application/json" });
+        res.end(JSON.stringify({"t": "t"}));
+      }
+    });
+    child.stdin.end(name + "\n" + postdata.v + "\n", "utf8");
+  }
+  else {
+    send404(res, urlobj.pathname, true);
+  }
+}
+
 var errorcodes = [ "Generic Error", "Not logged in", "Invalid data", 
         "Login failed", "Already playing", "Game launch failed",
         "Server shutting down", "Game not in progress" ];
@@ -1227,6 +1270,9 @@
       else if (target == "/quit") {
         stopgame(res, formdata);
       }
+      else if (target.match(/^\/uinfo\//)) {
+        setuinfo(req, res, formdata);
+      }
       else {
         res.writeHead(405, resheaders);
         res.end();