annotate rlgwebd.js @ 163:0f6da35b27a0

RLGWebD: overhaul the list of current games. The /status WebSocket now only sends a complete list when opened. At 40-second intervals, it sends a list of games that have been updated in the last minute. The client now uses this to keep its own list.
author John "Elwin" Edwards
date Sun, 04 Jan 2015 16:55:57 -0500
parents 5a7e7ec136c8
children 3a97e4ee50f0
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
1 #!/usr/bin/env node
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
2
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
3 var http = require('http');
28
2ad2b6491aa9 rlgwebd.js: become a real daemon.
John "Elwin" Edwards <elwin@sdf.org>
parents: 27
diff changeset
4 var net = require('net');
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
5 var url = require('url');
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
6 var path = require('path');
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
7 var fs = require('fs');
55
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
8 var events = require('events');
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
9 var child_process = require('child_process');
141
1a156a7746a7 RLGWebD: use NODE_PATH to find modules.
John "Elwin" Edwards
parents: 139
diff changeset
10 // Dependencies
1a156a7746a7 RLGWebD: use NODE_PATH to find modules.
John "Elwin" Edwards
parents: 139
diff changeset
11 var posix = require("posix");
1a156a7746a7 RLGWebD: use NODE_PATH to find modules.
John "Elwin" Edwards
parents: 139
diff changeset
12 var pty = require("pty.js");
1a156a7746a7 RLGWebD: use NODE_PATH to find modules.
John "Elwin" Edwards
parents: 139
diff changeset
13 var WebSocketServer = require("websocket").server;
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
14
30
b5570a594266 rlgwebd.js: listen on any address
John "Elwin" Edwards <elwin@sdf.org>
parents: 29
diff changeset
15 /* Configuration variables */
139
dcd07c1d846a Replace the daemon module with posix.
John "Elwin" Edwards
parents: 132
diff changeset
16 // The first file is NOT in the chroot.
28
2ad2b6491aa9 rlgwebd.js: become a real daemon.
John "Elwin" Edwards <elwin@sdf.org>
parents: 27
diff changeset
17 var ctlsocket = "/var/local/rlgwebd/ctl";
30
b5570a594266 rlgwebd.js: listen on any address
John "Elwin" Edwards <elwin@sdf.org>
parents: 29
diff changeset
18 var httpPort = 8080;
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
19 var chrootDir = "/var/dgl/";
157
e7f809f06c5c Use posix.getpwnam() to look up UID/GID to drop to.
John "Elwin" Edwards
parents: 156
diff changeset
20 var dropToUser = "rodney";
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
21 var serveStaticRoot = "/var/www/"; // inside the chroot
30
b5570a594266 rlgwebd.js: listen on any address
John "Elwin" Edwards <elwin@sdf.org>
parents: 29
diff changeset
22
39
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
23 /* Data on the games available. */
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
24 var games = {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
25 "rogue3": {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
26 "name": "Rogue V3",
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
27 "uname": "rogue3",
42
8f6bc0df58fa rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 40
diff changeset
28 "suffix": ".r3sav",
144
81a8e7aa4687 RLGWebD: game binaries have moved to /usr/bin.
John "Elwin" Edwards
parents: 142
diff changeset
29 "path": "/usr/bin/rogue3",
60
31bb3cf4f25f Make sure watchers start with completely drawn screens.
John "Elwin" Edwards <elwin@sdf.org>
parents: 55
diff changeset
30 "clear": new Buffer([27, 91, 72, 27, 91, 50, 74]) // CSI H CSI 2J
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
31 },
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
32 "rogue4": {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
33 "name": "Rogue V4",
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
34 "uname": "rogue4",
42
8f6bc0df58fa rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 40
diff changeset
35 "suffix": ".r4sav",
144
81a8e7aa4687 RLGWebD: game binaries have moved to /usr/bin.
John "Elwin" Edwards
parents: 142
diff changeset
36 "path": "/usr/bin/rogue4",
60
31bb3cf4f25f Make sure watchers start with completely drawn screens.
John "Elwin" Edwards <elwin@sdf.org>
parents: 55
diff changeset
37 "clear": new Buffer([27, 91, 72, 27, 91, 50, 74]) // CSI H CSI 2J
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
38 },
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
39 "rogue5": {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
40 "name": "Rogue V5",
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
41 "uname": "rogue5",
42
8f6bc0df58fa rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 40
diff changeset
42 "suffix": ".r5sav",
144
81a8e7aa4687 RLGWebD: game binaries have moved to /usr/bin.
John "Elwin" Edwards
parents: 142
diff changeset
43 "path": "/usr/bin/rogue5",
60
31bb3cf4f25f Make sure watchers start with completely drawn screens.
John "Elwin" Edwards <elwin@sdf.org>
parents: 55
diff changeset
44 "clear": new Buffer([27, 91, 72, 27, 91, 50, 74]) // CSI H CSI 2J
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
45 },
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
46 "srogue": {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
47 "name": "Super-Rogue",
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
48 "uname": "srogue",
42
8f6bc0df58fa rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 40
diff changeset
49 "suffix": ".srsav",
144
81a8e7aa4687 RLGWebD: game binaries have moved to /usr/bin.
John "Elwin" Edwards
parents: 142
diff changeset
50 "path": "/usr/bin/srogue",
121
077adfeea038 Correct the clear sequence for srogue.
John "Elwin" Edwards <elwin@sdf.org>
parents: 120
diff changeset
51 "clear": new Buffer([27, 91, 72, 27, 91, 50, 74]) // CSI H CSI 2J
120
54979d35611a Add support for Advanced Rogue 5.
John "Elwin" Edwards <elwin@sdf.org>
parents: 113
diff changeset
52 },
54979d35611a Add support for Advanced Rogue 5.
John "Elwin" Edwards <elwin@sdf.org>
parents: 113
diff changeset
53 "arogue5": {
54979d35611a Add support for Advanced Rogue 5.
John "Elwin" Edwards <elwin@sdf.org>
parents: 113
diff changeset
54 "name": "Advanced Rogue 5",
54979d35611a Add support for Advanced Rogue 5.
John "Elwin" Edwards <elwin@sdf.org>
parents: 113
diff changeset
55 "uname": "arogue5",
54979d35611a Add support for Advanced Rogue 5.
John "Elwin" Edwards <elwin@sdf.org>
parents: 113
diff changeset
56 "suffix": ".ar5sav",
144
81a8e7aa4687 RLGWebD: game binaries have moved to /usr/bin.
John "Elwin" Edwards
parents: 142
diff changeset
57 "path": "/usr/bin/arogue5",
120
54979d35611a Add support for Advanced Rogue 5.
John "Elwin" Edwards <elwin@sdf.org>
parents: 113
diff changeset
58 "clear": new Buffer([27, 91, 72, 27, 91, 50, 74]) // CSI H CSI 2J
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
59 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
60 };
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
61
39
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
62 /* Global state */
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
63 var logins = {};
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
64 var sessions = {};
156
127f9e256d02 Keep a list of dgamelaunch games and put it in the /status message.
John "Elwin" Edwards
parents: 155
diff changeset
65 var dglgames = {};
39
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
66 var allowlogin = true;
104
7d444ba4739e RLG-Web server: send status events over WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 103
diff changeset
67 var gamemux = new events.EventEmitter();
39
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
68
55
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
69 /* Constructor. A TermSession handles a pty and the game running on it.
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
70 * game: (String) Name of the game to launch.
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
71 * lkey: (String, key) The user's id, a key into logins.
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
72 * dims: (Array [Number, Number]) Height and width of the pty.
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
73 * handlers: (Object) Key-value pairs, event names and functions to
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
74 * install to handle them.
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
75 * Events:
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
76 * "open": Emitted on startup. Parameters: success (Boolean)
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
77 * "data": Data generated by child. Parameters: buf (Buffer)
87
bd2cf6dda28d RLG-Web: use the pty module.
John "Elwin" Edwards <elwin@sdf.org>
parents: 85
diff changeset
78 * "exit": Child terminated. Parameters: none
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
79 */
55
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
80 function TermSession(game, lkey, dims, handlers) {
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
81 var ss = this;
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
82 /* Subclass EventEmitter to do the hard work. */
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
83 events.EventEmitter.call(this);
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
84 for (var evname in handlers)
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
85 this.on(evname, handlers[evname]);
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
86 /* Don't launch anything that's not a real game. */
32
c75fc4b1d13d rlgwebd.js: add a status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 31
diff changeset
87 if (game in games) {
c75fc4b1d13d rlgwebd.js: add a status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 31
diff changeset
88 this.game = games[game];
c75fc4b1d13d rlgwebd.js: add a status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 31
diff changeset
89 }
c75fc4b1d13d rlgwebd.js: add a status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 31
diff changeset
90 else {
55
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
91 this.emit('open', false);
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
92 return;
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
93 }
55
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
94 if (lkey in logins) {
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
95 this.key = lkey;
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
96 this.pname = logins[lkey].name;
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
97 }
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
98 else {
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
99 this.emit('open', false);
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
100 return;
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
101 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
102 /* Grab a spot in the sessions table. */
158
9961a538c00e rlgwebd.js: get rid of numerical game identifiers.
John "Elwin" Edwards
parents: 157
diff changeset
103 sessions[this.game.uname + "/" + this.pname] = this;
16
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
104 /* Set up the sizes. */
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
105 this.w = Math.floor(Number(dims[1]));
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
106 if (!(this.w > 0 && this.w < 256))
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
107 this.w = 80;
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
108 this.h = Math.floor(Number(dims[0]));
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
109 if (!(this.h > 0 && this.h < 256))
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
110 this.h = 24;
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
111 /* Environment. */
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
112 var childenv = {};
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
113 for (var key in process.env) {
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
114 childenv[key] = process.env[key];
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
115 }
87
bd2cf6dda28d RLG-Web: use the pty module.
John "Elwin" Edwards <elwin@sdf.org>
parents: 85
diff changeset
116 var args = ["-n", this.pname];
bd2cf6dda28d RLG-Web: use the pty module.
John "Elwin" Edwards <elwin@sdf.org>
parents: 85
diff changeset
117 var spawnopts = {"env": childenv, "cwd": "/", "rows": this.h, "cols": this.w,
bd2cf6dda28d RLG-Web: use the pty module.
John "Elwin" Edwards <elwin@sdf.org>
parents: 85
diff changeset
118 "name": "xterm-256color"};
bd2cf6dda28d RLG-Web: use the pty module.
John "Elwin" Edwards <elwin@sdf.org>
parents: 85
diff changeset
119 this.term = pty.spawn(this.game.path, args, spawnopts);
158
9961a538c00e rlgwebd.js: get rid of numerical game identifiers.
John "Elwin" Edwards
parents: 157
diff changeset
120 tslog("%s playing %s (pid %d)", this.pname, this.game.uname, this.term.pid);
9961a538c00e rlgwebd.js: get rid of numerical game identifiers.
John "Elwin" Edwards
parents: 157
diff changeset
121 this.emit('open', true, this.game.uname, this.pname);
9961a538c00e rlgwebd.js: get rid of numerical game identifiers.
John "Elwin" Edwards
parents: 157
diff changeset
122 gamemux.emit('begin', this.game.uname, this.pname);
40
f7116eb3f791 rlgwebd.js: refactor some game-starting code.
John "Elwin" Edwards <elwin@sdf.org>
parents: 39
diff changeset
123 /* Set up the lockfile and ttyrec */
160
ed837da65e5f RLGWebD: Clean up code related to session timestamps.
John "Elwin" Edwards
parents: 159
diff changeset
124 this.lasttime = new Date();
ed837da65e5f RLGWebD: Clean up code related to session timestamps.
John "Elwin" Edwards
parents: 159
diff changeset
125 var ts = timestamp(this.lasttime);
142
c4304f08e35b RLGWebD: inprogress dirs have moved
John "Elwin" Edwards
parents: 141
diff changeset
126 var progressdir = path.join("/dgldir/inprogress", this.game.uname);
55
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
127 this.lock = path.join(progressdir, this.pname + ":node:" + ts + ".ttyrec");
110
18a81cc0084b RLG-Web: fix lockfile mixup.
John "Elwin" Edwards <elwin@sdf.org>
parents: 109
diff changeset
128 var lmsg = this.term.pid.toString() + '\n' + this.h + '\n' + this.w + '\n';
40
f7116eb3f791 rlgwebd.js: refactor some game-starting code.
John "Elwin" Edwards <elwin@sdf.org>
parents: 39
diff changeset
129 fs.writeFile(this.lock, lmsg, "utf8");
55
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
130 var ttyrec = path.join("/dgldir/ttyrec", this.pname, this.game.uname,
40
f7116eb3f791 rlgwebd.js: refactor some game-starting code.
John "Elwin" Edwards <elwin@sdf.org>
parents: 39
diff changeset
131 ts + ".ttyrec");
f7116eb3f791 rlgwebd.js: refactor some game-starting code.
John "Elwin" Edwards <elwin@sdf.org>
parents: 39
diff changeset
132 this.record = fs.createWriteStream(ttyrec, { mode: 0664 });
60
31bb3cf4f25f Make sure watchers start with completely drawn screens.
John "Elwin" Edwards <elwin@sdf.org>
parents: 55
diff changeset
133 /* Holds the output since the last screen clear, so watchers can begin
31bb3cf4f25f Make sure watchers start with completely drawn screens.
John "Elwin" Edwards <elwin@sdf.org>
parents: 55
diff changeset
134 * with a complete screen. */
31bb3cf4f25f Make sure watchers start with completely drawn screens.
John "Elwin" Edwards <elwin@sdf.org>
parents: 55
diff changeset
135 this.framebuf = new Buffer(1024);
31bb3cf4f25f Make sure watchers start with completely drawn screens.
John "Elwin" Edwards <elwin@sdf.org>
parents: 55
diff changeset
136 this.frameoff = 0;
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
137 /* END setup */
87
bd2cf6dda28d RLG-Web: use the pty module.
John "Elwin" Edwards <elwin@sdf.org>
parents: 85
diff changeset
138 function ttyrec_chunk(datastr) {
160
ed837da65e5f RLGWebD: Clean up code related to session timestamps.
John "Elwin" Edwards
parents: 159
diff changeset
139 ss.lasttime = new Date();
87
bd2cf6dda28d RLG-Web: use the pty module.
John "Elwin" Edwards <elwin@sdf.org>
parents: 85
diff changeset
140 var buf = new Buffer(datastr);
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
141 var chunk = new Buffer(buf.length + 12);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
142 /* TTYREC headers */
160
ed837da65e5f RLGWebD: Clean up code related to session timestamps.
John "Elwin" Edwards
parents: 159
diff changeset
143 chunk.writeUInt32LE(Math.floor(ss.lasttime.getTime() / 1000), 0);
ed837da65e5f RLGWebD: Clean up code related to session timestamps.
John "Elwin" Edwards
parents: 159
diff changeset
144 chunk.writeUInt32LE(1000 * (ss.lasttime.getTime() % 1000), 4);
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
145 chunk.writeUInt32LE(buf.length, 8);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
146 buf.copy(chunk, 12);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
147 ss.record.write(chunk);
60
31bb3cf4f25f Make sure watchers start with completely drawn screens.
John "Elwin" Edwards <elwin@sdf.org>
parents: 55
diff changeset
148 ss.framepush(buf);
55
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
149 ss.emit('data', buf);
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
150 }
87
bd2cf6dda28d RLG-Web: use the pty module.
John "Elwin" Edwards <elwin@sdf.org>
parents: 85
diff changeset
151 this.term.on("data", ttyrec_chunk);
60
31bb3cf4f25f Make sure watchers start with completely drawn screens.
John "Elwin" Edwards <elwin@sdf.org>
parents: 55
diff changeset
152 this.framepush = function(chunk) {
31bb3cf4f25f Make sure watchers start with completely drawn screens.
John "Elwin" Edwards <elwin@sdf.org>
parents: 55
diff changeset
153 /* If this chunk resets the screen, discard what preceded it. */
31bb3cf4f25f Make sure watchers start with completely drawn screens.
John "Elwin" Edwards <elwin@sdf.org>
parents: 55
diff changeset
154 if (bufncmp(chunk, this.game.clear, this.game.clear.length)) {
31bb3cf4f25f Make sure watchers start with completely drawn screens.
John "Elwin" Edwards <elwin@sdf.org>
parents: 55
diff changeset
155 this.framebuf = new Buffer(1024);
31bb3cf4f25f Make sure watchers start with completely drawn screens.
John "Elwin" Edwards <elwin@sdf.org>
parents: 55
diff changeset
156 this.frameoff = 0;
31bb3cf4f25f Make sure watchers start with completely drawn screens.
John "Elwin" Edwards <elwin@sdf.org>
parents: 55
diff changeset
157 }
31bb3cf4f25f Make sure watchers start with completely drawn screens.
John "Elwin" Edwards <elwin@sdf.org>
parents: 55
diff changeset
158 /* Make sure there's space. */
31bb3cf4f25f Make sure watchers start with completely drawn screens.
John "Elwin" Edwards <elwin@sdf.org>
parents: 55
diff changeset
159 while (this.framebuf.length < chunk.length + this.frameoff) {
31bb3cf4f25f Make sure watchers start with completely drawn screens.
John "Elwin" Edwards <elwin@sdf.org>
parents: 55
diff changeset
160 var nbuf = new Buffer(this.framebuf.length * 2);
31bb3cf4f25f Make sure watchers start with completely drawn screens.
John "Elwin" Edwards <elwin@sdf.org>
parents: 55
diff changeset
161 this.framebuf.copy(nbuf, 0, 0, this.frameoff);
31bb3cf4f25f Make sure watchers start with completely drawn screens.
John "Elwin" Edwards <elwin@sdf.org>
parents: 55
diff changeset
162 this.framebuf = nbuf;
31bb3cf4f25f Make sure watchers start with completely drawn screens.
John "Elwin" Edwards <elwin@sdf.org>
parents: 55
diff changeset
163 if (this.framebuf.length > 65536) {
162
5a7e7ec136c8 Properly print session tags in the log.
John "Elwin" Edwards
parents: 160
diff changeset
164 tslog("Warning: Game %s frame buffer at %d bytes", this.tag(),
60
31bb3cf4f25f Make sure watchers start with completely drawn screens.
John "Elwin" Edwards <elwin@sdf.org>
parents: 55
diff changeset
165 this.framebuf.length);
31bb3cf4f25f Make sure watchers start with completely drawn screens.
John "Elwin" Edwards <elwin@sdf.org>
parents: 55
diff changeset
166 }
31bb3cf4f25f Make sure watchers start with completely drawn screens.
John "Elwin" Edwards <elwin@sdf.org>
parents: 55
diff changeset
167 }
31bb3cf4f25f Make sure watchers start with completely drawn screens.
John "Elwin" Edwards <elwin@sdf.org>
parents: 55
diff changeset
168 chunk.copy(this.framebuf, this.frameoff);
31bb3cf4f25f Make sure watchers start with completely drawn screens.
John "Elwin" Edwards <elwin@sdf.org>
parents: 55
diff changeset
169 this.frameoff += chunk.length;
31bb3cf4f25f Make sure watchers start with completely drawn screens.
John "Elwin" Edwards <elwin@sdf.org>
parents: 55
diff changeset
170 };
55
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
171 this.write = function(data) {
87
bd2cf6dda28d RLG-Web: use the pty module.
John "Elwin" Edwards <elwin@sdf.org>
parents: 85
diff changeset
172 this.term.write(data);
55
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
173 };
158
9961a538c00e rlgwebd.js: get rid of numerical game identifiers.
John "Elwin" Edwards
parents: 157
diff changeset
174 this.tag = function() {
9961a538c00e rlgwebd.js: get rid of numerical game identifiers.
John "Elwin" Edwards
parents: 157
diff changeset
175 return this.game.uname + "/" + this.pname;
9961a538c00e rlgwebd.js: get rid of numerical game identifiers.
John "Elwin" Edwards
parents: 157
diff changeset
176 };
100
3dbfdaf62623 RLG-Web: begin converting to WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 94
diff changeset
177 // Teardown.
87
bd2cf6dda28d RLG-Web: use the pty module.
John "Elwin" Edwards <elwin@sdf.org>
parents: 85
diff changeset
178 this.term.on("exit", function () {
158
9961a538c00e rlgwebd.js: get rid of numerical game identifiers.
John "Elwin" Edwards
parents: 157
diff changeset
179 var tag = ss.tag();
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
180 fs.unlink(ss.lock);
55
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
181 ss.record.end();
87
bd2cf6dda28d RLG-Web: use the pty module.
John "Elwin" Edwards <elwin@sdf.org>
parents: 85
diff changeset
182 ss.emit('exit');
158
9961a538c00e rlgwebd.js: get rid of numerical game identifiers.
John "Elwin" Edwards
parents: 157
diff changeset
183 gamemux.emit('end', ss.game.uname, ss.pname);
9961a538c00e rlgwebd.js: get rid of numerical game identifiers.
John "Elwin" Edwards
parents: 157
diff changeset
184 delete sessions[tag];
9961a538c00e rlgwebd.js: get rid of numerical game identifiers.
John "Elwin" Edwards
parents: 157
diff changeset
185 tslog("Game %s ended.", tag);
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
186 });
55
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
187 this.close = function () {
158
9961a538c00e rlgwebd.js: get rid of numerical game identifiers.
John "Elwin" Edwards
parents: 157
diff changeset
188 if (this.tag() in sessions)
107
b64e31c5ec31 RLG-Web server: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 104
diff changeset
189 this.term.kill('SIGHUP');
55
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
190 };
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
191 }
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
192 TermSession.prototype = new events.EventEmitter();
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
193
100
3dbfdaf62623 RLG-Web: begin converting to WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 94
diff changeset
194 // Also known as WebSocketAndTermSessionClosureGlueFactory
3dbfdaf62623 RLG-Web: begin converting to WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 94
diff changeset
195 function wsWatcher(conn, session) {
3dbfdaf62623 RLG-Web: begin converting to WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 94
diff changeset
196 var ss = this; // is this even needed?
3dbfdaf62623 RLG-Web: begin converting to WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 94
diff changeset
197 var dataH = function(buf) {
3dbfdaf62623 RLG-Web: begin converting to WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 94
diff changeset
198 conn.sendUTF(JSON.stringify({"t": "d", "d": buf.toString("hex")}));
3dbfdaf62623 RLG-Web: begin converting to WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 94
diff changeset
199 };
3dbfdaf62623 RLG-Web: begin converting to WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 94
diff changeset
200 var exitH = function() {
3dbfdaf62623 RLG-Web: begin converting to WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 94
diff changeset
201 if (conn.connected)
3dbfdaf62623 RLG-Web: begin converting to WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 94
diff changeset
202 conn.close();
3dbfdaf62623 RLG-Web: begin converting to WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 94
diff changeset
203 }
3dbfdaf62623 RLG-Web: begin converting to WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 94
diff changeset
204 session.on('data', dataH);
3dbfdaf62623 RLG-Web: begin converting to WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 94
diff changeset
205 session.on('exit', exitH);
3dbfdaf62623 RLG-Web: begin converting to WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 94
diff changeset
206 conn.on('close', function(code, desc) {
3dbfdaf62623 RLG-Web: begin converting to WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 94
diff changeset
207 session.removeListener('data', dataH);
3dbfdaf62623 RLG-Web: begin converting to WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 94
diff changeset
208 session.removeListener('exit', exitH);
158
9961a538c00e rlgwebd.js: get rid of numerical game identifiers.
John "Elwin" Edwards
parents: 157
diff changeset
209 if (session.tag() in sessions)
162
5a7e7ec136c8 Properly print session tags in the log.
John "Elwin" Edwards
parents: 160
diff changeset
210 tslog("A WebSocket watcher has left game %s", session.tag());
100
3dbfdaf62623 RLG-Web: begin converting to WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 94
diff changeset
211 });
101
e59d68082664 RLG-Web: Complete the WebSocket watcher.
John "Elwin" Edwards <elwin@sdf.org>
parents: 100
diff changeset
212 conn.sendUTF(JSON.stringify({
e59d68082664 RLG-Web: Complete the WebSocket watcher.
John "Elwin" Edwards <elwin@sdf.org>
parents: 100
diff changeset
213 "t": "w", "w": session.w, "h": session.h,
e59d68082664 RLG-Web: Complete the WebSocket watcher.
John "Elwin" Edwards <elwin@sdf.org>
parents: 100
diff changeset
214 "p": session.pname, "g": session.game.uname
e59d68082664 RLG-Web: Complete the WebSocket watcher.
John "Elwin" Edwards <elwin@sdf.org>
parents: 100
diff changeset
215 }));
100
3dbfdaf62623 RLG-Web: begin converting to WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 94
diff changeset
216 conn.sendUTF(JSON.stringify({"t": "d",
3dbfdaf62623 RLG-Web: begin converting to WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 94
diff changeset
217 "d": session.framebuf.toString("hex", 0, session.frameoff)}));
3dbfdaf62623 RLG-Web: begin converting to WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 94
diff changeset
218 }
3dbfdaf62623 RLG-Web: begin converting to WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 94
diff changeset
219
107
b64e31c5ec31 RLG-Web server: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 104
diff changeset
220 function wsPlay(wsReq, game, lkey, dims) {
b64e31c5ec31 RLG-Web server: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 104
diff changeset
221 var conn;
b64e31c5ec31 RLG-Web server: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 104
diff changeset
222 var session;
b64e31c5ec31 RLG-Web server: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 104
diff changeset
223 /* Listeners on the WebSocket */
b64e31c5ec31 RLG-Web server: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 104
diff changeset
224 function messageH(message) {
b64e31c5ec31 RLG-Web server: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 104
diff changeset
225 var parsedMsg = getMsgWS(message);
b64e31c5ec31 RLG-Web server: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 104
diff changeset
226 if (parsedMsg.t == 'q') {
b64e31c5ec31 RLG-Web server: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 104
diff changeset
227 session.close();
b64e31c5ec31 RLG-Web server: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 104
diff changeset
228 }
b64e31c5ec31 RLG-Web server: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 104
diff changeset
229 else if (parsedMsg.t == 'd') {
b64e31c5ec31 RLG-Web server: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 104
diff changeset
230 var hexstr = parsedMsg.d.replace(/[^0-9a-f]/gi, "");
b64e31c5ec31 RLG-Web server: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 104
diff changeset
231 if (hexstr.length % 2 != 0) {
b64e31c5ec31 RLG-Web server: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 104
diff changeset
232 hexstr = hexstr.slice(0, -1);
b64e31c5ec31 RLG-Web server: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 104
diff changeset
233 }
b64e31c5ec31 RLG-Web server: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 104
diff changeset
234 var keybuf = new Buffer(hexstr, "hex");
b64e31c5ec31 RLG-Web server: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 104
diff changeset
235 session.write(keybuf);
b64e31c5ec31 RLG-Web server: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 104
diff changeset
236 }
b64e31c5ec31 RLG-Web server: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 104
diff changeset
237 }
b64e31c5ec31 RLG-Web server: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 104
diff changeset
238 function closeH() {
b64e31c5ec31 RLG-Web server: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 104
diff changeset
239 session.close();
b64e31c5ec31 RLG-Web server: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 104
diff changeset
240 }
b64e31c5ec31 RLG-Web server: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 104
diff changeset
241 /* These listen on the TermSession. */
158
9961a538c00e rlgwebd.js: get rid of numerical game identifiers.
John "Elwin" Edwards
parents: 157
diff changeset
242 function openH(success, gname, pname) {
107
b64e31c5ec31 RLG-Web server: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 104
diff changeset
243 if (success) {
158
9961a538c00e rlgwebd.js: get rid of numerical game identifiers.
John "Elwin" Edwards
parents: 157
diff changeset
244 var tag = gname + "/" + pname;
9961a538c00e rlgwebd.js: get rid of numerical game identifiers.
John "Elwin" Edwards
parents: 157
diff changeset
245 var reply = {"t": "s", "tag": tag, "w": sessions[tag].w, "h":
9961a538c00e rlgwebd.js: get rid of numerical game identifiers.
John "Elwin" Edwards
parents: 157
diff changeset
246 sessions[tag].h, "p": pname, "g": gname};
107
b64e31c5ec31 RLG-Web server: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 104
diff changeset
247 conn = wsReq.accept(null, wsReq.origin);
b64e31c5ec31 RLG-Web server: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 104
diff changeset
248 conn.sendUTF(JSON.stringify(reply));
b64e31c5ec31 RLG-Web server: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 104
diff changeset
249 conn.on('message', messageH);
b64e31c5ec31 RLG-Web server: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 104
diff changeset
250 conn.on('close', closeH);
b64e31c5ec31 RLG-Web server: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 104
diff changeset
251 }
b64e31c5ec31 RLG-Web server: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 104
diff changeset
252 else {
b64e31c5ec31 RLG-Web server: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 104
diff changeset
253 wsReq.reject(500, errorcodes[5]);
b64e31c5ec31 RLG-Web server: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 104
diff changeset
254 tslog("Unable to allocate TTY for %s", game);
b64e31c5ec31 RLG-Web server: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 104
diff changeset
255 }
b64e31c5ec31 RLG-Web server: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 104
diff changeset
256 }
b64e31c5ec31 RLG-Web server: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 104
diff changeset
257 function dataH(chunk) {
b64e31c5ec31 RLG-Web server: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 104
diff changeset
258 var msg = {};
b64e31c5ec31 RLG-Web server: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 104
diff changeset
259 msg.t = "d";
b64e31c5ec31 RLG-Web server: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 104
diff changeset
260 msg.d = chunk.toString("hex");
b64e31c5ec31 RLG-Web server: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 104
diff changeset
261 conn.sendUTF(JSON.stringify(msg));
b64e31c5ec31 RLG-Web server: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 104
diff changeset
262 }
b64e31c5ec31 RLG-Web server: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 104
diff changeset
263 function exitH() {
b64e31c5ec31 RLG-Web server: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 104
diff changeset
264 if (conn.connected)
b64e31c5ec31 RLG-Web server: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 104
diff changeset
265 conn.sendUTF(JSON.stringify({"t": "q"}));
b64e31c5ec31 RLG-Web server: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 104
diff changeset
266 conn.close();
b64e31c5ec31 RLG-Web server: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 104
diff changeset
267 session.removeListener('open', openH);
b64e31c5ec31 RLG-Web server: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 104
diff changeset
268 session.removeListener('data', dataH);
b64e31c5ec31 RLG-Web server: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 104
diff changeset
269 session.removeListener('exit', exitH);
b64e31c5ec31 RLG-Web server: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 104
diff changeset
270 }
b64e31c5ec31 RLG-Web server: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 104
diff changeset
271 var handlers = {'open': openH, 'data': dataH, 'exit': exitH};
b64e31c5ec31 RLG-Web server: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 104
diff changeset
272 session = new TermSession(game, lkey, dims, handlers);
b64e31c5ec31 RLG-Web server: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 104
diff changeset
273 }
b64e31c5ec31 RLG-Web server: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 104
diff changeset
274
b64e31c5ec31 RLG-Web server: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 104
diff changeset
275 function wsStart(wsReq) {
b64e31c5ec31 RLG-Web server: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 104
diff changeset
276 var playmatch = wsReq.resourceURL.pathname.match(/^\/play\/([^\/]*)$/);
b64e31c5ec31 RLG-Web server: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 104
diff changeset
277 if (!playmatch[1] || !(playmatch[1] in games)) {
b64e31c5ec31 RLG-Web server: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 104
diff changeset
278 wsReq.reject(404, errorcodes[2]);
b64e31c5ec31 RLG-Web server: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 104
diff changeset
279 return;
b64e31c5ec31 RLG-Web server: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 104
diff changeset
280 }
b64e31c5ec31 RLG-Web server: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 104
diff changeset
281 var gname = playmatch[1];
b64e31c5ec31 RLG-Web server: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 104
diff changeset
282 if (!allowlogin) {
b64e31c5ec31 RLG-Web server: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 104
diff changeset
283 wsReq.reject(404, errorcodes[6]);
b64e31c5ec31 RLG-Web server: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 104
diff changeset
284 return;
b64e31c5ec31 RLG-Web server: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 104
diff changeset
285 }
b64e31c5ec31 RLG-Web server: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 104
diff changeset
286 if (!("key" in wsReq.resourceURL.query)) {
b64e31c5ec31 RLG-Web server: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 104
diff changeset
287 wsReq.reject(404, "No key given.");
b64e31c5ec31 RLG-Web server: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 104
diff changeset
288 return;
b64e31c5ec31 RLG-Web server: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 104
diff changeset
289 }
b64e31c5ec31 RLG-Web server: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 104
diff changeset
290 var lkey = wsReq.resourceURL.query["key"];
b64e31c5ec31 RLG-Web server: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 104
diff changeset
291 if (!(lkey in logins)) {
b64e31c5ec31 RLG-Web server: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 104
diff changeset
292 wsReq.reject(404, errorcodes[1]);
b64e31c5ec31 RLG-Web server: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 104
diff changeset
293 return;
b64e31c5ec31 RLG-Web server: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 104
diff changeset
294 }
b64e31c5ec31 RLG-Web server: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 104
diff changeset
295 var pname = logins[lkey].name;
b64e31c5ec31 RLG-Web server: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 104
diff changeset
296 var dims = [wsReq.resourceURL.query.h, wsReq.resourceURL.query.w];
b64e31c5ec31 RLG-Web server: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 104
diff changeset
297 function progcallback(err, fname) {
b64e31c5ec31 RLG-Web server: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 104
diff changeset
298 if (fname) {
b64e31c5ec31 RLG-Web server: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 104
diff changeset
299 wsReq.reject(404, errorcodes[4]);
b64e31c5ec31 RLG-Web server: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 104
diff changeset
300 tslog("%s is already playing %s", pname, gname);
b64e31c5ec31 RLG-Web server: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 104
diff changeset
301 }
b64e31c5ec31 RLG-Web server: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 104
diff changeset
302 else
b64e31c5ec31 RLG-Web server: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 104
diff changeset
303 wsPlay(wsReq, gname, lkey, dims);
b64e31c5ec31 RLG-Web server: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 104
diff changeset
304 };
b64e31c5ec31 RLG-Web server: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 104
diff changeset
305 checkprogress(pname, games[gname], progcallback, []);
b64e31c5ec31 RLG-Web server: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 104
diff changeset
306 }
b64e31c5ec31 RLG-Web server: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 104
diff changeset
307
55
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
308 /* Some functions which check whether a player is currently playing or
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
309 * has a saved game. Maybe someday they will provide information on
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
310 * the game. */
40
f7116eb3f791 rlgwebd.js: refactor some game-starting code.
John "Elwin" Edwards <elwin@sdf.org>
parents: 39
diff changeset
311 function checkprogress(user, game, callback, args) {
142
c4304f08e35b RLGWebD: inprogress dirs have moved
John "Elwin" Edwards
parents: 141
diff changeset
312 var progressdir = path.join("/dgldir/inprogress", game.uname);
40
f7116eb3f791 rlgwebd.js: refactor some game-starting code.
John "Elwin" Edwards <elwin@sdf.org>
parents: 39
diff changeset
313 fs.readdir(progressdir, function(err, files) {
f7116eb3f791 rlgwebd.js: refactor some game-starting code.
John "Elwin" Edwards <elwin@sdf.org>
parents: 39
diff changeset
314 if (err) {
f7116eb3f791 rlgwebd.js: refactor some game-starting code.
John "Elwin" Edwards <elwin@sdf.org>
parents: 39
diff changeset
315 args.unshift(err, null);
f7116eb3f791 rlgwebd.js: refactor some game-starting code.
John "Elwin" Edwards <elwin@sdf.org>
parents: 39
diff changeset
316 callback.apply(null, args);
f7116eb3f791 rlgwebd.js: refactor some game-starting code.
John "Elwin" Edwards <elwin@sdf.org>
parents: 39
diff changeset
317 return;
f7116eb3f791 rlgwebd.js: refactor some game-starting code.
John "Elwin" Edwards <elwin@sdf.org>
parents: 39
diff changeset
318 }
f7116eb3f791 rlgwebd.js: refactor some game-starting code.
John "Elwin" Edwards <elwin@sdf.org>
parents: 39
diff changeset
319 var fre = RegExp("^" + user + ":");
f7116eb3f791 rlgwebd.js: refactor some game-starting code.
John "Elwin" Edwards <elwin@sdf.org>
parents: 39
diff changeset
320 for (var i = 0; i < files.length; i++) {
f7116eb3f791 rlgwebd.js: refactor some game-starting code.
John "Elwin" Edwards <elwin@sdf.org>
parents: 39
diff changeset
321 if (files[i].match(fre)) {
42
8f6bc0df58fa rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 40
diff changeset
322 args.unshift(null, files[i]);
40
f7116eb3f791 rlgwebd.js: refactor some game-starting code.
John "Elwin" Edwards <elwin@sdf.org>
parents: 39
diff changeset
323 callback.apply(null, args);
f7116eb3f791 rlgwebd.js: refactor some game-starting code.
John "Elwin" Edwards <elwin@sdf.org>
parents: 39
diff changeset
324 return;
f7116eb3f791 rlgwebd.js: refactor some game-starting code.
John "Elwin" Edwards <elwin@sdf.org>
parents: 39
diff changeset
325 }
f7116eb3f791 rlgwebd.js: refactor some game-starting code.
John "Elwin" Edwards <elwin@sdf.org>
parents: 39
diff changeset
326 }
42
8f6bc0df58fa rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 40
diff changeset
327 args.unshift(null, false);
8f6bc0df58fa rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 40
diff changeset
328 callback.apply(null, args);
8f6bc0df58fa rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 40
diff changeset
329 });
8f6bc0df58fa rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 40
diff changeset
330 }
8f6bc0df58fa rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 40
diff changeset
331
8f6bc0df58fa rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 40
diff changeset
332 function checksaved(user, game, callback, args) {
8f6bc0df58fa rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 40
diff changeset
333 var savedirc = game.uname + "save";
157
e7f809f06c5c Use posix.getpwnam() to look up UID/GID to drop to.
John "Elwin" Edwards
parents: 156
diff changeset
334 var basename = String(pwent.uid) + "-" + user + game.suffix;
42
8f6bc0df58fa rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 40
diff changeset
335 var savefile = path.join("/var/games/roguelike", savedirc, basename);
81
e4773ac5d4d5 Switch to node v0.8.
John "Elwin" Edwards <elwin@sdf.org>
parents: 66
diff changeset
336 fs.exists(savefile, function (exist) {
42
8f6bc0df58fa rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 40
diff changeset
337 args.unshift(exist);
40
f7116eb3f791 rlgwebd.js: refactor some game-starting code.
John "Elwin" Edwards <elwin@sdf.org>
parents: 39
diff changeset
338 callback.apply(null, args);
f7116eb3f791 rlgwebd.js: refactor some game-starting code.
John "Elwin" Edwards <elwin@sdf.org>
parents: 39
diff changeset
339 });
f7116eb3f791 rlgwebd.js: refactor some game-starting code.
John "Elwin" Edwards <elwin@sdf.org>
parents: 39
diff changeset
340 }
f7116eb3f791 rlgwebd.js: refactor some game-starting code.
John "Elwin" Edwards <elwin@sdf.org>
parents: 39
diff changeset
341
42
8f6bc0df58fa rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 40
diff changeset
342 function playerstatus(user, callback) {
8f6bc0df58fa rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 40
diff changeset
343 var sdata = {};
8f6bc0df58fa rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 40
diff changeset
344 function finishp() {
8f6bc0df58fa rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 40
diff changeset
345 for (var gname in games) {
8f6bc0df58fa rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 40
diff changeset
346 if (!(gname in sdata))
8f6bc0df58fa rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 40
diff changeset
347 return;
8f6bc0df58fa rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 40
diff changeset
348 }
8f6bc0df58fa rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 40
diff changeset
349 callback(sdata);
8f6bc0df58fa rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 40
diff changeset
350 }
8f6bc0df58fa rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 40
diff changeset
351 function regsaved(exists, game) {
8f6bc0df58fa rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 40
diff changeset
352 if (exists)
8f6bc0df58fa rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 40
diff changeset
353 sdata[game.uname] = "s";
8f6bc0df58fa rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 40
diff changeset
354 else
8f6bc0df58fa rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 40
diff changeset
355 sdata[game.uname] = "0";
8f6bc0df58fa rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 40
diff changeset
356 finishp();
8f6bc0df58fa rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 40
diff changeset
357 }
8f6bc0df58fa rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 40
diff changeset
358 function regactive(err, filename, game) {
8f6bc0df58fa rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 40
diff changeset
359 if (!err && filename) {
88
d644e7d46852 RLG-Web: make /pstatus/* differentiate between dgl and RLG-Web games.
John "Elwin" Edwards <elwin@sdf.org>
parents: 87
diff changeset
360 if (filename.match(/^[^:]*:node:/))
d644e7d46852 RLG-Web: make /pstatus/* differentiate between dgl and RLG-Web games.
John "Elwin" Edwards <elwin@sdf.org>
parents: 87
diff changeset
361 sdata[game.uname] = "p";
d644e7d46852 RLG-Web: make /pstatus/* differentiate between dgl and RLG-Web games.
John "Elwin" Edwards <elwin@sdf.org>
parents: 87
diff changeset
362 else
d644e7d46852 RLG-Web: make /pstatus/* differentiate between dgl and RLG-Web games.
John "Elwin" Edwards <elwin@sdf.org>
parents: 87
diff changeset
363 sdata[game.uname] = "d";
42
8f6bc0df58fa rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 40
diff changeset
364 finishp();
8f6bc0df58fa rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 40
diff changeset
365 }
8f6bc0df58fa rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 40
diff changeset
366 else
8f6bc0df58fa rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 40
diff changeset
367 checksaved(user, game, regsaved, [game]);
8f6bc0df58fa rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 40
diff changeset
368 }
8f6bc0df58fa rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 40
diff changeset
369 for (var gname in games) {
8f6bc0df58fa rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 40
diff changeset
370 checkprogress(user, games[gname], regactive, [games[gname]]);
8f6bc0df58fa rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 40
diff changeset
371 }
8f6bc0df58fa rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 40
diff changeset
372 }
8f6bc0df58fa rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 40
diff changeset
373
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
374 /* A few utility functions */
160
ed837da65e5f RLGWebD: Clean up code related to session timestamps.
John "Elwin" Edwards
parents: 159
diff changeset
375 function timestamp(dd) {
ed837da65e5f RLGWebD: Clean up code related to session timestamps.
John "Elwin" Edwards
parents: 159
diff changeset
376 if (!(dd instanceof Date)) {
ed837da65e5f RLGWebD: Clean up code related to session timestamps.
John "Elwin" Edwards
parents: 159
diff changeset
377 dd = new Date();
ed837da65e5f RLGWebD: Clean up code related to session timestamps.
John "Elwin" Edwards
parents: 159
diff changeset
378 }
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
379 sd = dd.toISOString();
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
380 sd = sd.slice(0, sd.indexOf("."));
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
381 return sd.replace("T", ".");
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
382 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
383
39
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
384 function randkey(words) {
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
385 if (!words || !(words > 0))
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
386 words = 1;
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
387 function rand32() {
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
388 rnum = Math.floor(Math.random() * 65536 * 65536);
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
389 hexstr = rnum.toString(16);
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
390 while (hexstr.length < 8)
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
391 hexstr = "0" + hexstr;
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
392 return hexstr;
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
393 }
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
394 var key = "";
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
395 for (var i = 0; i < words; i++)
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
396 key += rand32();
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
397 return key;
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
398 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
399
60
31bb3cf4f25f Make sure watchers start with completely drawn screens.
John "Elwin" Edwards <elwin@sdf.org>
parents: 55
diff changeset
400 /* Compares two buffers, returns true for equality up to index n */
31bb3cf4f25f Make sure watchers start with completely drawn screens.
John "Elwin" Edwards <elwin@sdf.org>
parents: 55
diff changeset
401 function bufncmp(buf1, buf2, n) {
31bb3cf4f25f Make sure watchers start with completely drawn screens.
John "Elwin" Edwards <elwin@sdf.org>
parents: 55
diff changeset
402 if (!Buffer.isBuffer(buf1) || !Buffer.isBuffer(buf2))
31bb3cf4f25f Make sure watchers start with completely drawn screens.
John "Elwin" Edwards <elwin@sdf.org>
parents: 55
diff changeset
403 return false;
31bb3cf4f25f Make sure watchers start with completely drawn screens.
John "Elwin" Edwards <elwin@sdf.org>
parents: 55
diff changeset
404 for (var i = 0; i < n; i++) {
31bb3cf4f25f Make sure watchers start with completely drawn screens.
John "Elwin" Edwards <elwin@sdf.org>
parents: 55
diff changeset
405 if (i == buf1.length && i == buf2.length)
31bb3cf4f25f Make sure watchers start with completely drawn screens.
John "Elwin" Edwards <elwin@sdf.org>
parents: 55
diff changeset
406 return true;
31bb3cf4f25f Make sure watchers start with completely drawn screens.
John "Elwin" Edwards <elwin@sdf.org>
parents: 55
diff changeset
407 if (i == buf1.length || i == buf2.length)
31bb3cf4f25f Make sure watchers start with completely drawn screens.
John "Elwin" Edwards <elwin@sdf.org>
parents: 55
diff changeset
408 return false;
31bb3cf4f25f Make sure watchers start with completely drawn screens.
John "Elwin" Edwards <elwin@sdf.org>
parents: 55
diff changeset
409 if (buf1[i] != buf2[i])
31bb3cf4f25f Make sure watchers start with completely drawn screens.
John "Elwin" Edwards <elwin@sdf.org>
parents: 55
diff changeset
410 return false;
31bb3cf4f25f Make sure watchers start with completely drawn screens.
John "Elwin" Edwards <elwin@sdf.org>
parents: 55
diff changeset
411 }
31bb3cf4f25f Make sure watchers start with completely drawn screens.
John "Elwin" Edwards <elwin@sdf.org>
parents: 55
diff changeset
412 return true;
31bb3cf4f25f Make sure watchers start with completely drawn screens.
John "Elwin" Edwards <elwin@sdf.org>
parents: 55
diff changeset
413 }
31bb3cf4f25f Make sure watchers start with completely drawn screens.
John "Elwin" Edwards <elwin@sdf.org>
parents: 55
diff changeset
414
26
9b58f8d3ea70 rlgwebd.js: add timestamps to log messages.
John "Elwin" Edwards <elwin@sdf.org>
parents: 23
diff changeset
415 function tslog() {
9b58f8d3ea70 rlgwebd.js: add timestamps to log messages.
John "Elwin" Edwards <elwin@sdf.org>
parents: 23
diff changeset
416 arguments[0] = new Date().toISOString() + ": " + String(arguments[0]);
9b58f8d3ea70 rlgwebd.js: add timestamps to log messages.
John "Elwin" Edwards <elwin@sdf.org>
parents: 23
diff changeset
417 console.log.apply(console, arguments);
9b58f8d3ea70 rlgwebd.js: add timestamps to log messages.
John "Elwin" Edwards <elwin@sdf.org>
parents: 23
diff changeset
418 }
9b58f8d3ea70 rlgwebd.js: add timestamps to log messages.
John "Elwin" Edwards <elwin@sdf.org>
parents: 23
diff changeset
419
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
420 /* Returns a list of the cookies in the request, obviously. */
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
421 function getCookies(req) {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
422 cookies = [];
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
423 if ("cookie" in req.headers) {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
424 cookstrs = req.headers["cookie"].split("; ");
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
425 for (var i = 0; i < cookstrs.length; i++) {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
426 eqsign = cookstrs[i].indexOf("=");
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
427 if (eqsign > 0) {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
428 name = cookstrs[i].slice(0, eqsign).toLowerCase();
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
429 val = cookstrs[i].slice(eqsign + 1);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
430 cookies[name] = val;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
431 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
432 else if (eqsign < 0)
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
433 cookies[cookstrs[i]] = null;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
434 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
435 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
436 return cookies;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
437 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
438
16
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
439 function getMsg(posttext) {
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
440 var jsonobj;
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
441 if (!posttext)
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
442 return {};
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
443 try {
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
444 jsonobj = JSON.parse(posttext);
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
445 }
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
446 catch (e) {
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
447 if (e instanceof SyntaxError)
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
448 return {};
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
449 }
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
450 if (typeof(jsonobj) != "object")
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
451 return {};
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
452 return jsonobj;
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
453 }
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
454
107
b64e31c5ec31 RLG-Web server: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 104
diff changeset
455 function getMsgWS(msgObj) {
b64e31c5ec31 RLG-Web server: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 104
diff changeset
456 if (msgObj.type != "utf8")
b64e31c5ec31 RLG-Web server: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 104
diff changeset
457 return {};
b64e31c5ec31 RLG-Web server: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 104
diff changeset
458 return getMsg(msgObj.utf8Data);
b64e31c5ec31 RLG-Web server: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 104
diff changeset
459 }
b64e31c5ec31 RLG-Web server: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 104
diff changeset
460
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
461 function login(req, res, formdata) {
27
83f9a799a374 rlgwebd.js: read commands from the console
John "Elwin" Edwards <elwin@sdf.org>
parents: 26
diff changeset
462 if (!allowlogin) {
55
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
463 sendError(res, 6, null, false);
27
83f9a799a374 rlgwebd.js: read commands from the console
John "Elwin" Edwards <elwin@sdf.org>
parents: 26
diff changeset
464 return;
83f9a799a374 rlgwebd.js: read commands from the console
John "Elwin" Edwards <elwin@sdf.org>
parents: 26
diff changeset
465 }
39
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
466 if (!("name" in formdata)) {
55
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
467 sendError(res, 2, "Username not given.", false);
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
468 return;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
469 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
470 else if (!("pw" in formdata)) {
55
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
471 sendError(res, 2, "Password not given.", false);
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
472 return;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
473 }
39
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
474 var username = String(formdata["name"]);
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
475 var password = String(formdata["pw"]);
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
476 function checkit(code, signal) {
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
477 /* Checks the exit status, see sqlickrypt.c for details. */
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
478 if (code != 0) {
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
479 sendError(res, 3);
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
480 if (code == 1)
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
481 tslog("Password check failed for user %s", username);
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
482 else if (code == 2)
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
483 tslog("Attempted login by nonexistent user %s", username);
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
484 else
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
485 tslog("Login failed: sqlickrypt error %d", code);
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
486 return;
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
487 }
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
488 var lkey = randkey(2);
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
489 while (lkey in logins)
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
490 lkey = randkey(2);
160
ed837da65e5f RLGWebD: Clean up code related to session timestamps.
John "Elwin" Edwards
parents: 159
diff changeset
491 logins[lkey] = {"name": username, "ts": new Date()};
39
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
492 res.writeHead(200, {'Content-Type': 'application/json'});
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
493 var reply = {"t": "l", "k": lkey, "u": username};
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
494 res.write(JSON.stringify(reply));
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
495 res.end();
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
496 tslog("%s has logged in (key %s)", username, lkey);
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
497 return;
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
498 }
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
499 /* Launch the sqlickrypt utility to check the password. */
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
500 var pwchecker = child_process.spawn("/bin/sqlickrypt", ["check"]);
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
501 pwchecker.on("exit", checkit);
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
502 pwchecker.stdin.end(username + '\n' + password + '\n', "utf8");
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
503 return;
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
504 }
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
505
20
5f785e1d5cca RLG-Web: set up user directories on registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 19
diff changeset
506 /* Sets things up for a new user, like dgamelaunch's commands[register] */
5f785e1d5cca RLG-Web: set up user directories on registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 19
diff changeset
507 function regsetup(username) {
5f785e1d5cca RLG-Web: set up user directories on registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 19
diff changeset
508 function regsetup_l2(err) {
5f785e1d5cca RLG-Web: set up user directories on registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 19
diff changeset
509 for (var g in games) {
5f785e1d5cca RLG-Web: set up user directories on registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 19
diff changeset
510 fs.mkdir(path.join("/dgldir/ttyrec", username, games[g].uname), 0755);
5f785e1d5cca RLG-Web: set up user directories on registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 19
diff changeset
511 }
5f785e1d5cca RLG-Web: set up user directories on registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 19
diff changeset
512 }
5f785e1d5cca RLG-Web: set up user directories on registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 19
diff changeset
513 fs.mkdir(path.join("/dgldir/userdata", username), 0755);
5f785e1d5cca RLG-Web: set up user directories on registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 19
diff changeset
514 fs.mkdir(path.join("/dgldir/ttyrec/", username), 0755, regsetup_l2);
5f785e1d5cca RLG-Web: set up user directories on registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 19
diff changeset
515 }
5f785e1d5cca RLG-Web: set up user directories on registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 19
diff changeset
516
19
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
517 function register(req, res, formdata) {
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
518 var uname, passwd, email;
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
519 if (typeof (formdata.name) != "string" || formdata.name === "") {
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
520 sendError(res, 2, "No name given.");
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
521 return;
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
522 }
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
523 else
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
524 uname = formdata["name"];
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
525 if (typeof (formdata.pw) != "string" || formdata.pw === "") {
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
526 sendError(res, 2, "No password given.");
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
527 return;
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
528 }
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
529 else
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
530 passwd = formdata["pw"];
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
531 if (typeof (formdata.email) != "string" || formdata.email === "") {
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
532 /* E-mail is optional */
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
533 email = "nobody@nowhere.not";
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
534 }
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
535 else
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
536 email = formdata["email"];
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
537 function checkreg(code, signal) {
39
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
538 if (code === 0) {
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
539 var lkey = randkey(2);
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
540 while (lkey in logins)
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
541 lkey = randkey(2);
160
ed837da65e5f RLGWebD: Clean up code related to session timestamps.
John "Elwin" Edwards
parents: 159
diff changeset
542 logins[lkey] = {"name": uname, "ts": new Date()};
39
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
543 var reply = {"t": "r", "k": lkey, "u": uname};
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
544 res.writeHead(200, {'Content-Type': 'application/json'});
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
545 res.write(JSON.stringify(reply));
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
546 res.end();
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
547 tslog("Added new user: %s", uname);
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
548 regsetup(uname);
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
549 }
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
550 else if (code == 4) {
19
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
551 sendError(res, 2, "Invalid characters in name or email.");
26
9b58f8d3ea70 rlgwebd.js: add timestamps to log messages.
John "Elwin" Edwards <elwin@sdf.org>
parents: 23
diff changeset
552 tslog("Attempted registration: %s %s", uname, email);
20
5f785e1d5cca RLG-Web: set up user directories on registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 19
diff changeset
553 }
5f785e1d5cca RLG-Web: set up user directories on registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 19
diff changeset
554 else if (code == 1) {
19
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
555 sendError(res, 2, "Username " + uname + " is already being used.");
26
9b58f8d3ea70 rlgwebd.js: add timestamps to log messages.
John "Elwin" Edwards <elwin@sdf.org>
parents: 23
diff changeset
556 tslog("Attempted duplicate registration: %s", uname);
20
5f785e1d5cca RLG-Web: set up user directories on registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 19
diff changeset
557 }
39
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
558 else {
19
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
559 sendError(res, 0, null);
26
9b58f8d3ea70 rlgwebd.js: add timestamps to log messages.
John "Elwin" Edwards <elwin@sdf.org>
parents: 23
diff changeset
560 tslog("sqlickrypt register failed with code %d", code);
20
5f785e1d5cca RLG-Web: set up user directories on registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 19
diff changeset
561 }
19
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
562 }
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
563 var child_adder = child_process.spawn("/bin/sqlickrypt", ["register"]);
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
564 child_adder.on("exit", checkreg);
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
565 child_adder.stdin.end(uname + '\n' + passwd + '\n' + email + '\n', "utf8");
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
566 return;
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
567 }
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
568
111
f56fdfeed01a Replace taking over games with forced saves.
John "Elwin" Edwards <elwin@sdf.org>
parents: 110
diff changeset
569 /* Stops a running game if the request has the proper key. */
f56fdfeed01a Replace taking over games with forced saves.
John "Elwin" Edwards <elwin@sdf.org>
parents: 110
diff changeset
570 function stopgame(res, formdata) {
f56fdfeed01a Replace taking over games with forced saves.
John "Elwin" Edwards <elwin@sdf.org>
parents: 110
diff changeset
571 if (!("key" in formdata) || !(formdata["key"] in logins)) {
f56fdfeed01a Replace taking over games with forced saves.
John "Elwin" Edwards <elwin@sdf.org>
parents: 110
diff changeset
572 sendError(res, 1);
f56fdfeed01a Replace taking over games with forced saves.
John "Elwin" Edwards <elwin@sdf.org>
parents: 110
diff changeset
573 return;
f56fdfeed01a Replace taking over games with forced saves.
John "Elwin" Edwards <elwin@sdf.org>
parents: 110
diff changeset
574 }
f56fdfeed01a Replace taking over games with forced saves.
John "Elwin" Edwards <elwin@sdf.org>
parents: 110
diff changeset
575 var pname = logins[formdata["key"]].name;
f56fdfeed01a Replace taking over games with forced saves.
John "Elwin" Edwards <elwin@sdf.org>
parents: 110
diff changeset
576 if (!("g" in formdata) || !(formdata["g"] in games)) {
f56fdfeed01a Replace taking over games with forced saves.
John "Elwin" Edwards <elwin@sdf.org>
parents: 110
diff changeset
577 sendError(res, 2, "No such game.");
f56fdfeed01a Replace taking over games with forced saves.
John "Elwin" Edwards <elwin@sdf.org>
parents: 110
diff changeset
578 return;
f56fdfeed01a Replace taking over games with forced saves.
John "Elwin" Edwards <elwin@sdf.org>
parents: 110
diff changeset
579 }
f56fdfeed01a Replace taking over games with forced saves.
John "Elwin" Edwards <elwin@sdf.org>
parents: 110
diff changeset
580 var gname = formdata["g"];
f56fdfeed01a Replace taking over games with forced saves.
John "Elwin" Edwards <elwin@sdf.org>
parents: 110
diff changeset
581 function checkback(err, fname) {
f56fdfeed01a Replace taking over games with forced saves.
John "Elwin" Edwards <elwin@sdf.org>
parents: 110
diff changeset
582 if (!fname) {
f56fdfeed01a Replace taking over games with forced saves.
John "Elwin" Edwards <elwin@sdf.org>
parents: 110
diff changeset
583 sendError(res, 7);
f56fdfeed01a Replace taking over games with forced saves.
John "Elwin" Edwards <elwin@sdf.org>
parents: 110
diff changeset
584 return;
f56fdfeed01a Replace taking over games with forced saves.
John "Elwin" Edwards <elwin@sdf.org>
parents: 110
diff changeset
585 }
142
c4304f08e35b RLGWebD: inprogress dirs have moved
John "Elwin" Edwards
parents: 141
diff changeset
586 var fullfile = path.join("/dgldir/inprogress", gname, fname);
111
f56fdfeed01a Replace taking over games with forced saves.
John "Elwin" Edwards <elwin@sdf.org>
parents: 110
diff changeset
587 fs.readFile(fullfile, "utf8", function(err, fdata) {
f56fdfeed01a Replace taking over games with forced saves.
John "Elwin" Edwards <elwin@sdf.org>
parents: 110
diff changeset
588 if (err) {
f56fdfeed01a Replace taking over games with forced saves.
John "Elwin" Edwards <elwin@sdf.org>
parents: 110
diff changeset
589 sendError(res, 7);
f56fdfeed01a Replace taking over games with forced saves.
John "Elwin" Edwards <elwin@sdf.org>
parents: 110
diff changeset
590 return;
f56fdfeed01a Replace taking over games with forced saves.
John "Elwin" Edwards <elwin@sdf.org>
parents: 110
diff changeset
591 }
f56fdfeed01a Replace taking over games with forced saves.
John "Elwin" Edwards <elwin@sdf.org>
parents: 110
diff changeset
592 var pid = parseInt(fdata.split('\n')[0], 10);
139
dcd07c1d846a Replace the daemon module with posix.
John "Elwin" Edwards
parents: 132
diff changeset
593 try {
dcd07c1d846a Replace the daemon module with posix.
John "Elwin" Edwards
parents: 132
diff changeset
594 process.kill(pid, 'SIGHUP');
dcd07c1d846a Replace the daemon module with posix.
John "Elwin" Edwards
parents: 132
diff changeset
595 }
dcd07c1d846a Replace the daemon module with posix.
John "Elwin" Edwards
parents: 132
diff changeset
596 catch (err) {
dcd07c1d846a Replace the daemon module with posix.
John "Elwin" Edwards
parents: 132
diff changeset
597 /* If the PID is invalid, the lockfile is stale. */
dcd07c1d846a Replace the daemon module with posix.
John "Elwin" Edwards
parents: 132
diff changeset
598 if (err.code == "ESRCH") {
dcd07c1d846a Replace the daemon module with posix.
John "Elwin" Edwards
parents: 132
diff changeset
599 var nodere = RegExp("^" + pname + ":node:");
dcd07c1d846a Replace the daemon module with posix.
John "Elwin" Edwards
parents: 132
diff changeset
600 if (fname.match(nodere)) {
dcd07c1d846a Replace the daemon module with posix.
John "Elwin" Edwards
parents: 132
diff changeset
601 fs.unlink(fullfile);
dcd07c1d846a Replace the daemon module with posix.
John "Elwin" Edwards
parents: 132
diff changeset
602 }
dcd07c1d846a Replace the daemon module with posix.
John "Elwin" Edwards
parents: 132
diff changeset
603 }
dcd07c1d846a Replace the daemon module with posix.
John "Elwin" Edwards
parents: 132
diff changeset
604 }
111
f56fdfeed01a Replace taking over games with forced saves.
John "Elwin" Edwards <elwin@sdf.org>
parents: 110
diff changeset
605 /* The response doesn't mean that the game is gone. The only way
f56fdfeed01a Replace taking over games with forced saves.
John "Elwin" Edwards <elwin@sdf.org>
parents: 110
diff changeset
606 * to make sure a dgamelaunch-supervised game is over would be to
f56fdfeed01a Replace taking over games with forced saves.
John "Elwin" Edwards <elwin@sdf.org>
parents: 110
diff changeset
607 * poll fname until it disappears. */
f56fdfeed01a Replace taking over games with forced saves.
John "Elwin" Edwards <elwin@sdf.org>
parents: 110
diff changeset
608 res.writeHead(200, {'Content-Type': 'application/json'});
f56fdfeed01a Replace taking over games with forced saves.
John "Elwin" Edwards <elwin@sdf.org>
parents: 110
diff changeset
609 res.write(JSON.stringify({"t": "q"}));
f56fdfeed01a Replace taking over games with forced saves.
John "Elwin" Edwards <elwin@sdf.org>
parents: 110
diff changeset
610 res.end();
f56fdfeed01a Replace taking over games with forced saves.
John "Elwin" Edwards <elwin@sdf.org>
parents: 110
diff changeset
611 });
f56fdfeed01a Replace taking over games with forced saves.
John "Elwin" Edwards <elwin@sdf.org>
parents: 110
diff changeset
612 }
f56fdfeed01a Replace taking over games with forced saves.
John "Elwin" Edwards <elwin@sdf.org>
parents: 110
diff changeset
613 checkprogress(pname, games[gname], checkback, []);
f56fdfeed01a Replace taking over games with forced saves.
John "Elwin" Edwards <elwin@sdf.org>
parents: 110
diff changeset
614 }
f56fdfeed01a Replace taking over games with forced saves.
John "Elwin" Edwards <elwin@sdf.org>
parents: 110
diff changeset
615
155
245a2959f504 Begin support for watching dgamelaunch games.
John "Elwin" Edwards
parents: 148
diff changeset
616 function startProgressWatcher() {
245a2959f504 Begin support for watching dgamelaunch games.
John "Elwin" Edwards
parents: 148
diff changeset
617 var watchdirs = [];
245a2959f504 Begin support for watching dgamelaunch games.
John "Elwin" Edwards
parents: 148
diff changeset
618 for (var gname in games) {
245a2959f504 Begin support for watching dgamelaunch games.
John "Elwin" Edwards
parents: 148
diff changeset
619 watchdirs.push(path.join("/dgldir/inprogress", gname));
245a2959f504 Begin support for watching dgamelaunch games.
John "Elwin" Edwards
parents: 148
diff changeset
620 }
245a2959f504 Begin support for watching dgamelaunch games.
John "Elwin" Edwards
parents: 148
diff changeset
621 var subproc = child_process.spawn("/bin/watcher", watchdirs);
245a2959f504 Begin support for watching dgamelaunch games.
John "Elwin" Edwards
parents: 148
diff changeset
622 subproc.stdout.setEncoding('utf8');
245a2959f504 Begin support for watching dgamelaunch games.
John "Elwin" Edwards
parents: 148
diff changeset
623 subproc.stdout.on('data', function (chunk) {
245a2959f504 Begin support for watching dgamelaunch games.
John "Elwin" Edwards
parents: 148
diff changeset
624 var fname = chunk.slice(2, -1);
245a2959f504 Begin support for watching dgamelaunch games.
John "Elwin" Edwards
parents: 148
diff changeset
625 var filere = /.*\/([^\/]*)\/([^\/:]*):(node:)?(.*)/;
245a2959f504 Begin support for watching dgamelaunch games.
John "Elwin" Edwards
parents: 148
diff changeset
626 var matchresult = fname.match(filere);
245a2959f504 Begin support for watching dgamelaunch games.
John "Elwin" Edwards
parents: 148
diff changeset
627 if (!matchresult || matchresult[3])
245a2959f504 Begin support for watching dgamelaunch games.
John "Elwin" Edwards
parents: 148
diff changeset
628 return;
156
127f9e256d02 Keep a list of dgamelaunch games and put it in the /status message.
John "Elwin" Edwards
parents: 155
diff changeset
629 var gname = matchresult[1];
127f9e256d02 Keep a list of dgamelaunch games and put it in the /status message.
John "Elwin" Edwards
parents: 155
diff changeset
630 var pname = matchresult[2];
127f9e256d02 Keep a list of dgamelaunch games and put it in the /status message.
John "Elwin" Edwards
parents: 155
diff changeset
631 var tag = gname + "/" + pname;
155
245a2959f504 Begin support for watching dgamelaunch games.
John "Elwin" Edwards
parents: 148
diff changeset
632 if (chunk[0] == "E") {
245a2959f504 Begin support for watching dgamelaunch games.
John "Elwin" Edwards
parents: 148
diff changeset
633 tslog("DGL: %s is playing %s: %s", pname, gname, fname)
156
127f9e256d02 Keep a list of dgamelaunch games and put it in the /status message.
John "Elwin" Edwards
parents: 155
diff changeset
634 dglgames[tag] = fname;
155
245a2959f504 Begin support for watching dgamelaunch games.
John "Elwin" Edwards
parents: 148
diff changeset
635 }
245a2959f504 Begin support for watching dgamelaunch games.
John "Elwin" Edwards
parents: 148
diff changeset
636 else if (chunk[0] == "C") {
245a2959f504 Begin support for watching dgamelaunch games.
John "Elwin" Edwards
parents: 148
diff changeset
637 tslog("DGL: %s started playing %s: %s", pname, gname, fname)
156
127f9e256d02 Keep a list of dgamelaunch games and put it in the /status message.
John "Elwin" Edwards
parents: 155
diff changeset
638 dglgames[tag] = fname;
155
245a2959f504 Begin support for watching dgamelaunch games.
John "Elwin" Edwards
parents: 148
diff changeset
639 }
245a2959f504 Begin support for watching dgamelaunch games.
John "Elwin" Edwards
parents: 148
diff changeset
640 else if (chunk[0] == "D") {
245a2959f504 Begin support for watching dgamelaunch games.
John "Elwin" Edwards
parents: 148
diff changeset
641 tslog("DGL: %s finished playing %s: %s", pname, gname, fname)
156
127f9e256d02 Keep a list of dgamelaunch games and put it in the /status message.
John "Elwin" Edwards
parents: 155
diff changeset
642 delete dglgames[tag];
155
245a2959f504 Begin support for watching dgamelaunch games.
John "Elwin" Edwards
parents: 148
diff changeset
643 }
245a2959f504 Begin support for watching dgamelaunch games.
John "Elwin" Edwards
parents: 148
diff changeset
644 else {
245a2959f504 Begin support for watching dgamelaunch games.
John "Elwin" Edwards
parents: 148
diff changeset
645 tslog("Watcher says: %s", chunk)
245a2959f504 Begin support for watching dgamelaunch games.
John "Elwin" Edwards
parents: 148
diff changeset
646 }
245a2959f504 Begin support for watching dgamelaunch games.
John "Elwin" Edwards
parents: 148
diff changeset
647 });
245a2959f504 Begin support for watching dgamelaunch games.
John "Elwin" Edwards
parents: 148
diff changeset
648 subproc.stdout.resume();
245a2959f504 Begin support for watching dgamelaunch games.
John "Elwin" Edwards
parents: 148
diff changeset
649 return subproc;
245a2959f504 Begin support for watching dgamelaunch games.
John "Elwin" Edwards
parents: 148
diff changeset
650 }
245a2959f504 Begin support for watching dgamelaunch games.
John "Elwin" Edwards
parents: 148
diff changeset
651
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
652 function serveStatic(req, res, fname) {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
653 var nname = path.normalize(fname);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
654 if (nname == "" || nname == "/")
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
655 nname = "index.html";
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
656 if (nname.match(/\/$/))
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
657 path.join(nname, "index.html"); /* it was a directory */
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
658 var realname = path.join(serveStaticRoot, nname);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
659 var extension = path.extname(realname);
81
e4773ac5d4d5 Switch to node v0.8.
John "Elwin" Edwards <elwin@sdf.org>
parents: 66
diff changeset
660 fs.exists(realname, function (exists) {
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
661 var resheaders = {};
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
662 if (!exists || !extension || extension == ".html")
31
7dd6becf9ce9 rlgwebd.js: improve MIME types.
John "Elwin" Edwards <elwin@sdf.org>
parents: 30
diff changeset
663 resheaders["Content-Type"] = "text/html; charset=utf-8";
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
664 else if (extension == ".png")
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
665 resheaders["Content-Type"] = "image/png";
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
666 else if (extension == ".css")
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
667 resheaders["Content-Type"] = "text/css";
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
668 else if (extension == ".js")
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
669 resheaders["Content-Type"] = "text/javascript";
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
670 else if (extension == ".svg")
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
671 resheaders["Content-Type"] = "image/svg+xml";
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
672 else
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
673 resheaders["Content-Type"] = "application/octet-stream";
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
674 if (exists) {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
675 fs.readFile(realname, function (error, data) {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
676 if (error) {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
677 res.writeHead(500, {});
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
678 res.end();
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
679 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
680 else {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
681 res.writeHead(200, resheaders);
34
57f4b36ef06b rlgwebd.js: handle HTTP HEAD properly.
John "Elwin" Edwards <elwin@sdf.org>
parents: 32
diff changeset
682 if (req.method != 'HEAD')
57f4b36ef06b rlgwebd.js: handle HTTP HEAD properly.
John "Elwin" Edwards <elwin@sdf.org>
parents: 32
diff changeset
683 res.write(data);
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
684 res.end();
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
685 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
686 });
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
687 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
688 else {
125
5ad15380f851 Improve the /uinfo interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 124
diff changeset
689 send404(res, nname, req.method == 'HEAD');
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
690 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
691 });
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
692 return;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
693 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
694
160
ed837da65e5f RLGWebD: Clean up code related to session timestamps.
John "Elwin" Edwards
parents: 159
diff changeset
695 /* Currently, this doesn't do anything blocking, but keep the callback */
103
f30495f7ede8 RLG-Web server: refactor statusmsg() to work with WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 102
diff changeset
696 function getStatus(callback) {
66
57bf0dcd080e Display idle time of games in progress.
John "Elwin" Edwards <elwin@sdf.org>
parents: 64
diff changeset
697 var now = new Date();
103
f30495f7ede8 RLG-Web server: refactor statusmsg() to work with WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 102
diff changeset
698 var statusinfo = {"s": allowlogin, "g": []};
158
9961a538c00e rlgwebd.js: get rid of numerical game identifiers.
John "Elwin" Edwards
parents: 157
diff changeset
699 for (var tag in sessions) {
55
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
700 var gamedesc = {};
158
9961a538c00e rlgwebd.js: get rid of numerical game identifiers.
John "Elwin" Edwards
parents: 157
diff changeset
701 gamedesc["p"] = sessions[tag].pname;
9961a538c00e rlgwebd.js: get rid of numerical game identifiers.
John "Elwin" Edwards
parents: 157
diff changeset
702 gamedesc["g"] = sessions[tag].game.uname;
160
ed837da65e5f RLGWebD: Clean up code related to session timestamps.
John "Elwin" Edwards
parents: 159
diff changeset
703 gamedesc["i"] = now - sessions[tag].lasttime;
103
f30495f7ede8 RLG-Web server: refactor statusmsg() to work with WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 102
diff changeset
704 statusinfo["g"].push(gamedesc);
32
c75fc4b1d13d rlgwebd.js: add a status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 31
diff changeset
705 }
156
127f9e256d02 Keep a list of dgamelaunch games and put it in the /status message.
John "Elwin" Edwards
parents: 155
diff changeset
706 statusinfo["dgl"] = [];
127f9e256d02 Keep a list of dgamelaunch games and put it in the /status message.
John "Elwin" Edwards
parents: 155
diff changeset
707 for (var tag in dglgames) {
163
0f6da35b27a0 RLGWebD: overhaul the list of current games.
John "Elwin" Edwards
parents: 162
diff changeset
708 var dglinfo = {};
0f6da35b27a0 RLGWebD: overhaul the list of current games.
John "Elwin" Edwards
parents: 162
diff changeset
709 var slash = tag.search('/');
0f6da35b27a0 RLGWebD: overhaul the list of current games.
John "Elwin" Edwards
parents: 162
diff changeset
710 dglinfo["g"] = tag.slice(0, slash);
0f6da35b27a0 RLGWebD: overhaul the list of current games.
John "Elwin" Edwards
parents: 162
diff changeset
711 dglinfo["p"] = tag.slice(slash + 1);
0f6da35b27a0 RLGWebD: overhaul the list of current games.
John "Elwin" Edwards
parents: 162
diff changeset
712 dglinfo["i"] = -1;
0f6da35b27a0 RLGWebD: overhaul the list of current games.
John "Elwin" Edwards
parents: 162
diff changeset
713 statusinfo["dgl"].push(dglinfo);
156
127f9e256d02 Keep a list of dgamelaunch games and put it in the /status message.
John "Elwin" Edwards
parents: 155
diff changeset
714 }
160
ed837da65e5f RLGWebD: Clean up code related to session timestamps.
John "Elwin" Edwards
parents: 159
diff changeset
715 callback(statusinfo);
32
c75fc4b1d13d rlgwebd.js: add a status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 31
diff changeset
716 }
c75fc4b1d13d rlgwebd.js: add a status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 31
diff changeset
717
103
f30495f7ede8 RLG-Web server: refactor statusmsg() to work with WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 102
diff changeset
718 function statusmsg(req, res) {
f30495f7ede8 RLG-Web server: refactor statusmsg() to work with WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 102
diff changeset
719 function respond(info) {
f30495f7ede8 RLG-Web server: refactor statusmsg() to work with WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 102
diff changeset
720 res.writeHead(200, { "Content-Type": "application/json" });
f30495f7ede8 RLG-Web server: refactor statusmsg() to work with WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 102
diff changeset
721 if (req.method != 'HEAD')
f30495f7ede8 RLG-Web server: refactor statusmsg() to work with WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 102
diff changeset
722 res.write(JSON.stringify(info));
f30495f7ede8 RLG-Web server: refactor statusmsg() to work with WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 102
diff changeset
723 res.end();
f30495f7ede8 RLG-Web server: refactor statusmsg() to work with WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 102
diff changeset
724 }
f30495f7ede8 RLG-Web server: refactor statusmsg() to work with WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 102
diff changeset
725 getStatus(respond);
f30495f7ede8 RLG-Web server: refactor statusmsg() to work with WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 102
diff changeset
726 }
f30495f7ede8 RLG-Web server: refactor statusmsg() to work with WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 102
diff changeset
727
42
8f6bc0df58fa rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 40
diff changeset
728 function pstatusmsg(req, res) {
8f6bc0df58fa rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 40
diff changeset
729 if (req.method == 'HEAD') {
8f6bc0df58fa rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 40
diff changeset
730 res.writeHead(200, { "Content-Type": "application/json" });
8f6bc0df58fa rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 40
diff changeset
731 res.end();
8f6bc0df58fa rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 40
diff changeset
732 return;
8f6bc0df58fa rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 40
diff changeset
733 }
8f6bc0df58fa rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 40
diff changeset
734 var target = url.parse(req.url).pathname;
8f6bc0df58fa rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 40
diff changeset
735 var pmatch = target.match(/^\/pstatus\/(.*)/);
8f6bc0df58fa rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 40
diff changeset
736 if (pmatch && pmatch[1])
8f6bc0df58fa rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 40
diff changeset
737 var pname = pmatch[1];
8f6bc0df58fa rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 40
diff changeset
738 else {
8f6bc0df58fa rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 40
diff changeset
739 sendError(res, 2, "No name given.");
8f6bc0df58fa rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 40
diff changeset
740 return;
8f6bc0df58fa rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 40
diff changeset
741 }
8f6bc0df58fa rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 40
diff changeset
742 var reply = {"name": pname};
8f6bc0df58fa rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 40
diff changeset
743 playerstatus(pname, function (pdata) {
8f6bc0df58fa rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 40
diff changeset
744 reply["stat"] = pdata;
8f6bc0df58fa rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 40
diff changeset
745 res.writeHead(200, { "Content-Type": "application/json" });
8f6bc0df58fa rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 40
diff changeset
746 res.write(JSON.stringify(reply));
8f6bc0df58fa rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 40
diff changeset
747 res.end();
8f6bc0df58fa rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 40
diff changeset
748 });
8f6bc0df58fa rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 40
diff changeset
749 }
8f6bc0df58fa rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 40
diff changeset
750
124
fbeb0bf2b51d Add a user information interface at /uinfo.
John "Elwin" Edwards <elwin@sdf.org>
parents: 121
diff changeset
751 function getuinfo(req, res) {
125
5ad15380f851 Improve the /uinfo interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 124
diff changeset
752 var urlobj = url.parse(req.url, true);
5ad15380f851 Improve the /uinfo interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 124
diff changeset
753 var query = urlobj.query;
124
fbeb0bf2b51d Add a user information interface at /uinfo.
John "Elwin" Edwards <elwin@sdf.org>
parents: 121
diff changeset
754 if (!("key" in query) || !(query["key"] in logins)) {
fbeb0bf2b51d Add a user information interface at /uinfo.
John "Elwin" Edwards <elwin@sdf.org>
parents: 121
diff changeset
755 sendError(res, 1);
fbeb0bf2b51d Add a user information interface at /uinfo.
John "Elwin" Edwards <elwin@sdf.org>
parents: 121
diff changeset
756 return;
fbeb0bf2b51d Add a user information interface at /uinfo.
John "Elwin" Edwards <elwin@sdf.org>
parents: 121
diff changeset
757 }
125
5ad15380f851 Improve the /uinfo interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 124
diff changeset
758 var match = urlobj.pathname.match(/^\/[^\/]*\/(.*)/);
5ad15380f851 Improve the /uinfo interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 124
diff changeset
759 if (!match || !match[1]) {
5ad15380f851 Improve the /uinfo interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 124
diff changeset
760 send404(res, urlobj.pathname, req.method == 'HEAD');
5ad15380f851 Improve the /uinfo interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 124
diff changeset
761 return;
5ad15380f851 Improve the /uinfo interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 124
diff changeset
762 }
5ad15380f851 Improve the /uinfo interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 124
diff changeset
763 var which = match[1];
124
fbeb0bf2b51d Add a user information interface at /uinfo.
John "Elwin" Edwards <elwin@sdf.org>
parents: 121
diff changeset
764 var name = logins[query["key"]].name;
fbeb0bf2b51d Add a user information interface at /uinfo.
John "Elwin" Edwards <elwin@sdf.org>
parents: 121
diff changeset
765 var reply = { "u": name };
fbeb0bf2b51d Add a user information interface at /uinfo.
John "Elwin" Edwards <elwin@sdf.org>
parents: 121
diff changeset
766 function send() {
fbeb0bf2b51d Add a user information interface at /uinfo.
John "Elwin" Edwards <elwin@sdf.org>
parents: 121
diff changeset
767 res.writeHead(200, { "Content-Type": "application/json" });
fbeb0bf2b51d Add a user information interface at /uinfo.
John "Elwin" Edwards <elwin@sdf.org>
parents: 121
diff changeset
768 res.write(JSON.stringify(reply));
fbeb0bf2b51d Add a user information interface at /uinfo.
John "Elwin" Edwards <elwin@sdf.org>
parents: 121
diff changeset
769 res.end();
fbeb0bf2b51d Add a user information interface at /uinfo.
John "Elwin" Edwards <elwin@sdf.org>
parents: 121
diff changeset
770 }
125
5ad15380f851 Improve the /uinfo interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 124
diff changeset
771 if (which == "pw") {
124
fbeb0bf2b51d Add a user information interface at /uinfo.
John "Elwin" Edwards <elwin@sdf.org>
parents: 121
diff changeset
772 /* Don't actually divulge passwords. */
fbeb0bf2b51d Add a user information interface at /uinfo.
John "Elwin" Edwards <elwin@sdf.org>
parents: 121
diff changeset
773 reply["pw"] = "";
125
5ad15380f851 Improve the /uinfo interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 124
diff changeset
774 send();
124
fbeb0bf2b51d Add a user information interface at /uinfo.
John "Elwin" Edwards <elwin@sdf.org>
parents: 121
diff changeset
775 }
125
5ad15380f851 Improve the /uinfo interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 124
diff changeset
776 else if (which == "email") {
124
fbeb0bf2b51d Add a user information interface at /uinfo.
John "Elwin" Edwards <elwin@sdf.org>
parents: 121
diff changeset
777 var address;
fbeb0bf2b51d Add a user information interface at /uinfo.
John "Elwin" Edwards <elwin@sdf.org>
parents: 121
diff changeset
778 function finish(code, signal) {
fbeb0bf2b51d Add a user information interface at /uinfo.
John "Elwin" Edwards <elwin@sdf.org>
parents: 121
diff changeset
779 if (code != 0) {
fbeb0bf2b51d Add a user information interface at /uinfo.
John "Elwin" Edwards <elwin@sdf.org>
parents: 121
diff changeset
780 tslog("sqlickrypt: %d with name %s", code, name);
fbeb0bf2b51d Add a user information interface at /uinfo.
John "Elwin" Edwards <elwin@sdf.org>
parents: 121
diff changeset
781 sendError(res, 2);
fbeb0bf2b51d Add a user information interface at /uinfo.
John "Elwin" Edwards <elwin@sdf.org>
parents: 121
diff changeset
782 }
fbeb0bf2b51d Add a user information interface at /uinfo.
John "Elwin" Edwards <elwin@sdf.org>
parents: 121
diff changeset
783 else {
fbeb0bf2b51d Add a user information interface at /uinfo.
John "Elwin" Edwards <elwin@sdf.org>
parents: 121
diff changeset
784 reply["email"] = address;
fbeb0bf2b51d Add a user information interface at /uinfo.
John "Elwin" Edwards <elwin@sdf.org>
parents: 121
diff changeset
785 send();
fbeb0bf2b51d Add a user information interface at /uinfo.
John "Elwin" Edwards <elwin@sdf.org>
parents: 121
diff changeset
786 }
fbeb0bf2b51d Add a user information interface at /uinfo.
John "Elwin" Edwards <elwin@sdf.org>
parents: 121
diff changeset
787 }
fbeb0bf2b51d Add a user information interface at /uinfo.
John "Elwin" Edwards <elwin@sdf.org>
parents: 121
diff changeset
788 var subproc = child_process.spawn("/bin/sqlickrypt", ["getmail"]);
fbeb0bf2b51d Add a user information interface at /uinfo.
John "Elwin" Edwards <elwin@sdf.org>
parents: 121
diff changeset
789 subproc.stdout.on("data", function (data) {
fbeb0bf2b51d Add a user information interface at /uinfo.
John "Elwin" Edwards <elwin@sdf.org>
parents: 121
diff changeset
790 address = data.toString().replace(/\n/g, "");
fbeb0bf2b51d Add a user information interface at /uinfo.
John "Elwin" Edwards <elwin@sdf.org>
parents: 121
diff changeset
791 });
fbeb0bf2b51d Add a user information interface at /uinfo.
John "Elwin" Edwards <elwin@sdf.org>
parents: 121
diff changeset
792 subproc.on("exit", finish);
fbeb0bf2b51d Add a user information interface at /uinfo.
John "Elwin" Edwards <elwin@sdf.org>
parents: 121
diff changeset
793 subproc.stdin.end(name + '\n', "utf8");
fbeb0bf2b51d Add a user information interface at /uinfo.
John "Elwin" Edwards <elwin@sdf.org>
parents: 121
diff changeset
794 }
125
5ad15380f851 Improve the /uinfo interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 124
diff changeset
795 else {
5ad15380f851 Improve the /uinfo interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 124
diff changeset
796 send404(res, urlobj.pathname, req.method == 'HEAD');
5ad15380f851 Improve the /uinfo interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 124
diff changeset
797 return;
5ad15380f851 Improve the /uinfo interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 124
diff changeset
798 }
124
fbeb0bf2b51d Add a user information interface at /uinfo.
John "Elwin" Edwards <elwin@sdf.org>
parents: 121
diff changeset
799 }
fbeb0bf2b51d Add a user information interface at /uinfo.
John "Elwin" Edwards <elwin@sdf.org>
parents: 121
diff changeset
800
126
3e3824711791 RLG-Web server: allow changing e-mail and password.
John "Elwin" Edwards <elwin@sdf.org>
parents: 125
diff changeset
801 function setuinfo(req, res, postdata) {
3e3824711791 RLG-Web server: allow changing e-mail and password.
John "Elwin" Edwards <elwin@sdf.org>
parents: 125
diff changeset
802 var urlobj = url.parse(req.url, true);
3e3824711791 RLG-Web server: allow changing e-mail and password.
John "Elwin" Edwards <elwin@sdf.org>
parents: 125
diff changeset
803 var query = urlobj.query;
3e3824711791 RLG-Web server: allow changing e-mail and password.
John "Elwin" Edwards <elwin@sdf.org>
parents: 125
diff changeset
804 if (!("key" in query) || !(query["key"] in logins)) {
3e3824711791 RLG-Web server: allow changing e-mail and password.
John "Elwin" Edwards <elwin@sdf.org>
parents: 125
diff changeset
805 sendError(res, 1);
3e3824711791 RLG-Web server: allow changing e-mail and password.
John "Elwin" Edwards <elwin@sdf.org>
parents: 125
diff changeset
806 return;
3e3824711791 RLG-Web server: allow changing e-mail and password.
John "Elwin" Edwards <elwin@sdf.org>
parents: 125
diff changeset
807 }
3e3824711791 RLG-Web server: allow changing e-mail and password.
John "Elwin" Edwards <elwin@sdf.org>
parents: 125
diff changeset
808 var name = logins[query["key"]].name;
3e3824711791 RLG-Web server: allow changing e-mail and password.
John "Elwin" Edwards <elwin@sdf.org>
parents: 125
diff changeset
809 var match = urlobj.pathname.match(/^\/[^\/]*\/(.*)/);
3e3824711791 RLG-Web server: allow changing e-mail and password.
John "Elwin" Edwards <elwin@sdf.org>
parents: 125
diff changeset
810 if (!match || !match[1]) {
3e3824711791 RLG-Web server: allow changing e-mail and password.
John "Elwin" Edwards <elwin@sdf.org>
parents: 125
diff changeset
811 send404(res, urlobj.pathname, true);
3e3824711791 RLG-Web server: allow changing e-mail and password.
John "Elwin" Edwards <elwin@sdf.org>
parents: 125
diff changeset
812 return;
3e3824711791 RLG-Web server: allow changing e-mail and password.
John "Elwin" Edwards <elwin@sdf.org>
parents: 125
diff changeset
813 }
3e3824711791 RLG-Web server: allow changing e-mail and password.
John "Elwin" Edwards <elwin@sdf.org>
parents: 125
diff changeset
814 var which = match[1];
3e3824711791 RLG-Web server: allow changing e-mail and password.
John "Elwin" Edwards <elwin@sdf.org>
parents: 125
diff changeset
815 if (!("v" in postdata)) {
3e3824711791 RLG-Web server: allow changing e-mail and password.
John "Elwin" Edwards <elwin@sdf.org>
parents: 125
diff changeset
816 sendError(res, 2, "No value provided");
3e3824711791 RLG-Web server: allow changing e-mail and password.
John "Elwin" Edwards <elwin@sdf.org>
parents: 125
diff changeset
817 return;
3e3824711791 RLG-Web server: allow changing e-mail and password.
John "Elwin" Edwards <elwin@sdf.org>
parents: 125
diff changeset
818 }
3e3824711791 RLG-Web server: allow changing e-mail and password.
John "Elwin" Edwards <elwin@sdf.org>
parents: 125
diff changeset
819 if (which == "email" || which == "pw") {
3e3824711791 RLG-Web server: allow changing e-mail and password.
John "Elwin" Edwards <elwin@sdf.org>
parents: 125
diff changeset
820 var args;
3e3824711791 RLG-Web server: allow changing e-mail and password.
John "Elwin" Edwards <elwin@sdf.org>
parents: 125
diff changeset
821 if (which == "email")
3e3824711791 RLG-Web server: allow changing e-mail and password.
John "Elwin" Edwards <elwin@sdf.org>
parents: 125
diff changeset
822 args = ["setmail"];
3e3824711791 RLG-Web server: allow changing e-mail and password.
John "Elwin" Edwards <elwin@sdf.org>
parents: 125
diff changeset
823 else
3e3824711791 RLG-Web server: allow changing e-mail and password.
John "Elwin" Edwards <elwin@sdf.org>
parents: 125
diff changeset
824 args = ["setpw"];
3e3824711791 RLG-Web server: allow changing e-mail and password.
John "Elwin" Edwards <elwin@sdf.org>
parents: 125
diff changeset
825 var child = child_process.execFile("/bin/sqlickrypt", args,
3e3824711791 RLG-Web server: allow changing e-mail and password.
John "Elwin" Edwards <elwin@sdf.org>
parents: 125
diff changeset
826 function (err, stdout, stderr) {
3e3824711791 RLG-Web server: allow changing e-mail and password.
John "Elwin" Edwards <elwin@sdf.org>
parents: 125
diff changeset
827 if (err) {
3e3824711791 RLG-Web server: allow changing e-mail and password.
John "Elwin" Edwards <elwin@sdf.org>
parents: 125
diff changeset
828 tslog("Could not set %s: sqlickrypt error %d", which, err.code);
3e3824711791 RLG-Web server: allow changing e-mail and password.
John "Elwin" Edwards <elwin@sdf.org>
parents: 125
diff changeset
829 sendError(res, 2);
3e3824711791 RLG-Web server: allow changing e-mail and password.
John "Elwin" Edwards <elwin@sdf.org>
parents: 125
diff changeset
830 }
3e3824711791 RLG-Web server: allow changing e-mail and password.
John "Elwin" Edwards <elwin@sdf.org>
parents: 125
diff changeset
831 else {
3e3824711791 RLG-Web server: allow changing e-mail and password.
John "Elwin" Edwards <elwin@sdf.org>
parents: 125
diff changeset
832 tslog("User %s has changed %s", name, which);
3e3824711791 RLG-Web server: allow changing e-mail and password.
John "Elwin" Edwards <elwin@sdf.org>
parents: 125
diff changeset
833 res.writeHead(200, { "Content-Type": "application/json" });
3e3824711791 RLG-Web server: allow changing e-mail and password.
John "Elwin" Edwards <elwin@sdf.org>
parents: 125
diff changeset
834 res.end(JSON.stringify({"t": "t"}));
3e3824711791 RLG-Web server: allow changing e-mail and password.
John "Elwin" Edwards <elwin@sdf.org>
parents: 125
diff changeset
835 }
3e3824711791 RLG-Web server: allow changing e-mail and password.
John "Elwin" Edwards <elwin@sdf.org>
parents: 125
diff changeset
836 });
3e3824711791 RLG-Web server: allow changing e-mail and password.
John "Elwin" Edwards <elwin@sdf.org>
parents: 125
diff changeset
837 child.stdin.end(name + "\n" + postdata.v + "\n", "utf8");
3e3824711791 RLG-Web server: allow changing e-mail and password.
John "Elwin" Edwards <elwin@sdf.org>
parents: 125
diff changeset
838 }
3e3824711791 RLG-Web server: allow changing e-mail and password.
John "Elwin" Edwards <elwin@sdf.org>
parents: 125
diff changeset
839 else {
3e3824711791 RLG-Web server: allow changing e-mail and password.
John "Elwin" Edwards <elwin@sdf.org>
parents: 125
diff changeset
840 send404(res, urlobj.pathname, true);
3e3824711791 RLG-Web server: allow changing e-mail and password.
John "Elwin" Edwards <elwin@sdf.org>
parents: 125
diff changeset
841 }
3e3824711791 RLG-Web server: allow changing e-mail and password.
John "Elwin" Edwards <elwin@sdf.org>
parents: 125
diff changeset
842 }
3e3824711791 RLG-Web server: allow changing e-mail and password.
John "Elwin" Edwards <elwin@sdf.org>
parents: 125
diff changeset
843
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
844 var errorcodes = [ "Generic Error", "Not logged in", "Invalid data",
27
83f9a799a374 rlgwebd.js: read commands from the console
John "Elwin" Edwards <elwin@sdf.org>
parents: 26
diff changeset
845 "Login failed", "Already playing", "Game launch failed",
39
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
846 "Server shutting down", "Game not in progress" ];
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
847
55
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
848 function sendError(res, ecode, msg, box) {
31
7dd6becf9ce9 rlgwebd.js: improve MIME types.
John "Elwin" Edwards <elwin@sdf.org>
parents: 30
diff changeset
849 res.writeHead(200, { "Content-Type": "application/json" });
16
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
850 var edict = {"t": "E"};
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
851 if (!(ecode < errorcodes.length && ecode > 0))
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
852 ecode = 0;
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
853 edict["c"] = ecode;
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
854 edict["s"] = errorcodes[ecode];
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
855 if (msg)
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
856 edict["s"] += ": " + msg;
55
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
857 if (box)
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
858 res.write(JSON.stringify([edict]));
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
859 else
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
860 res.write(JSON.stringify(edict));
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
861 res.end();
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
862 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
863
125
5ad15380f851 Improve the /uinfo interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 124
diff changeset
864 function send404(res, path, nopage) {
5ad15380f851 Improve the /uinfo interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 124
diff changeset
865 res.writeHead(404, {"Content-Type": "text/html; charset=utf-8"});
5ad15380f851 Improve the /uinfo interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 124
diff changeset
866 if (!nopage) {
5ad15380f851 Improve the /uinfo interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 124
diff changeset
867 res.write("<html><head><title>" + path + "</title></head>\n<body><h1>"
5ad15380f851 Improve the /uinfo interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 124
diff changeset
868 + path + " Not Found</h1></body></html>\n");
5ad15380f851 Improve the /uinfo interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 124
diff changeset
869 }
5ad15380f851 Improve the /uinfo interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 124
diff changeset
870 res.end();
5ad15380f851 Improve the /uinfo interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 124
diff changeset
871 }
5ad15380f851 Improve the /uinfo interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 124
diff changeset
872
27
83f9a799a374 rlgwebd.js: read commands from the console
John "Elwin" Edwards <elwin@sdf.org>
parents: 26
diff changeset
873 function webHandler(req, res) {
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
874 /* default headers for the response */
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
875 var resheaders = {'Content-Type': 'text/html'};
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
876 /* The request body will be added to this as it arrives. */
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
877 var reqbody = "";
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
878 var formdata;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
879
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
880 /* Register a listener to get the body. */
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
881 function moredata(chunk) {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
882 reqbody += chunk;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
883 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
884 req.on('data', moredata);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
885
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
886 /* This will send the response once the whole request is here. */
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
887 function respond() {
16
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
888 formdata = getMsg(reqbody);
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
889 var target = url.parse(req.url).pathname;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
890 /* First figure out if the client is POSTing to a command interface. */
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
891 if (req.method == 'POST') {
159
a613380ffdc2 RLGWebD: excise polling.
John "Elwin" Edwards
parents: 158
diff changeset
892 if (target == "/login") {
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
893 login(req, res, formdata);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
894 }
19
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
895 else if (target == "/addacct") {
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
896 register(req, res, formdata);
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
897 }
111
f56fdfeed01a Replace taking over games with forced saves.
John "Elwin" Edwards <elwin@sdf.org>
parents: 110
diff changeset
898 else if (target == "/quit") {
f56fdfeed01a Replace taking over games with forced saves.
John "Elwin" Edwards <elwin@sdf.org>
parents: 110
diff changeset
899 stopgame(res, formdata);
f56fdfeed01a Replace taking over games with forced saves.
John "Elwin" Edwards <elwin@sdf.org>
parents: 110
diff changeset
900 }
126
3e3824711791 RLG-Web server: allow changing e-mail and password.
John "Elwin" Edwards <elwin@sdf.org>
parents: 125
diff changeset
901 else if (target.match(/^\/uinfo\//)) {
3e3824711791 RLG-Web server: allow changing e-mail and password.
John "Elwin" Edwards <elwin@sdf.org>
parents: 125
diff changeset
902 setuinfo(req, res, formdata);
3e3824711791 RLG-Web server: allow changing e-mail and password.
John "Elwin" Edwards <elwin@sdf.org>
parents: 125
diff changeset
903 }
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
904 else {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
905 res.writeHead(405, resheaders);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
906 res.end();
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
907 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
908 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
909 else if (req.method == 'GET' || req.method == 'HEAD') {
159
a613380ffdc2 RLGWebD: excise polling.
John "Elwin" Edwards
parents: 158
diff changeset
910 if (target == '/status') {
32
c75fc4b1d13d rlgwebd.js: add a status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 31
diff changeset
911 statusmsg(req, res);
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
912 }
125
5ad15380f851 Improve the /uinfo interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 124
diff changeset
913 else if (target.match(/^\/uinfo\//)) {
124
fbeb0bf2b51d Add a user information interface at /uinfo.
John "Elwin" Edwards <elwin@sdf.org>
parents: 121
diff changeset
914 getuinfo(req, res);
fbeb0bf2b51d Add a user information interface at /uinfo.
John "Elwin" Edwards <elwin@sdf.org>
parents: 121
diff changeset
915 }
42
8f6bc0df58fa rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 40
diff changeset
916 else if (target.match(/^\/pstatus\//)) {
8f6bc0df58fa rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 40
diff changeset
917 pstatusmsg(req, res);
8f6bc0df58fa rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 40
diff changeset
918 }
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
919 else /* Go look for it in the filesystem */
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
920 serveStatic(req, res, target);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
921 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
922 else { /* Some other method */
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
923 res.writeHead(501, resheaders);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
924 res.write("<html><head><title>501</title></head>\n<body><h1>501 Not Implemented</h1></body></html>\n");
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
925 res.end();
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
926 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
927 return;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
928 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
929 req.on('end', respond);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
930 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
931
100
3dbfdaf62623 RLG-Web: begin converting to WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 94
diff changeset
932 function wsHandler(wsRequest) {
158
9961a538c00e rlgwebd.js: get rid of numerical game identifiers.
John "Elwin" Edwards
parents: 157
diff changeset
933 var watchmatch = wsRequest.resource.match(/^\/watch\/(.*)$/);
107
b64e31c5ec31 RLG-Web server: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 104
diff changeset
934 var playmatch = wsRequest.resource.match(/^\/play\//);
104
7d444ba4739e RLG-Web server: send status events over WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 103
diff changeset
935 if (watchmatch !== null) {
158
9961a538c00e rlgwebd.js: get rid of numerical game identifiers.
John "Elwin" Edwards
parents: 157
diff changeset
936 if (!(watchmatch[1] in sessions)) {
9961a538c00e rlgwebd.js: get rid of numerical game identifiers.
John "Elwin" Edwards
parents: 157
diff changeset
937 wsRequest.reject(404, errorcodes[7]);
9961a538c00e rlgwebd.js: get rid of numerical game identifiers.
John "Elwin" Edwards
parents: 157
diff changeset
938 return;
104
7d444ba4739e RLG-Web server: send status events over WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 103
diff changeset
939 }
158
9961a538c00e rlgwebd.js: get rid of numerical game identifiers.
John "Elwin" Edwards
parents: 157
diff changeset
940 var tsession = sessions[watchmatch[1]];
9961a538c00e rlgwebd.js: get rid of numerical game identifiers.
John "Elwin" Edwards
parents: 157
diff changeset
941 var conn = wsRequest.accept(null, wsRequest.origin);
9961a538c00e rlgwebd.js: get rid of numerical game identifiers.
John "Elwin" Edwards
parents: 157
diff changeset
942 new wsWatcher(conn, tsession);
162
5a7e7ec136c8 Properly print session tags in the log.
John "Elwin" Edwards
parents: 160
diff changeset
943 tslog("Game %s is being watched via WebSockets", tsession.tag());
104
7d444ba4739e RLG-Web server: send status events over WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 103
diff changeset
944 }
107
b64e31c5ec31 RLG-Web server: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 104
diff changeset
945 else if (playmatch !== null) {
b64e31c5ec31 RLG-Web server: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 104
diff changeset
946 wsStart(wsRequest);
b64e31c5ec31 RLG-Web server: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 104
diff changeset
947 }
b64e31c5ec31 RLG-Web server: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 104
diff changeset
948 else if (wsRequest.resourceURL.pathname == "/status") {
100
3dbfdaf62623 RLG-Web: begin converting to WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 94
diff changeset
949 var conn = wsRequest.accept(null, wsRequest.origin);
104
7d444ba4739e RLG-Web server: send status events over WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 103
diff changeset
950 var tell = function () {
7d444ba4739e RLG-Web server: send status events over WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 103
diff changeset
951 getStatus(function (info) {
7d444ba4739e RLG-Web server: send status events over WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 103
diff changeset
952 info["t"] = "t";
7d444ba4739e RLG-Web server: send status events over WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 103
diff changeset
953 conn.sendUTF(JSON.stringify(info));
7d444ba4739e RLG-Web server: send status events over WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 103
diff changeset
954 });
7d444ba4739e RLG-Web server: send status events over WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 103
diff changeset
955 }
158
9961a538c00e rlgwebd.js: get rid of numerical game identifiers.
John "Elwin" Edwards
parents: 157
diff changeset
956 var beginH = function (gname, pname) {
9961a538c00e rlgwebd.js: get rid of numerical game identifiers.
John "Elwin" Edwards
parents: 157
diff changeset
957 conn.sendUTF(JSON.stringify({"t": "b", "p": pname, "g": gname}));
104
7d444ba4739e RLG-Web server: send status events over WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 103
diff changeset
958 };
7d444ba4739e RLG-Web server: send status events over WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 103
diff changeset
959 var listH = function (list) {
7d444ba4739e RLG-Web server: send status events over WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 103
diff changeset
960 conn.sendUTF(JSON.stringify(list));
7d444ba4739e RLG-Web server: send status events over WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 103
diff changeset
961 };
158
9961a538c00e rlgwebd.js: get rid of numerical game identifiers.
John "Elwin" Edwards
parents: 157
diff changeset
962 var endH = function (gname, pname) {
9961a538c00e rlgwebd.js: get rid of numerical game identifiers.
John "Elwin" Edwards
parents: 157
diff changeset
963 conn.sendUTF(JSON.stringify({"t": "e", "p": pname, "g": gname}));
104
7d444ba4739e RLG-Web server: send status events over WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 103
diff changeset
964 };
7d444ba4739e RLG-Web server: send status events over WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 103
diff changeset
965 gamemux.on('begin', beginH);
7d444ba4739e RLG-Web server: send status events over WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 103
diff changeset
966 gamemux.on('list', listH);
7d444ba4739e RLG-Web server: send status events over WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 103
diff changeset
967 gamemux.on('end', endH);
7d444ba4739e RLG-Web server: send status events over WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 103
diff changeset
968 conn.on('message', tell);
7d444ba4739e RLG-Web server: send status events over WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 103
diff changeset
969 conn.on('close', function () {
7d444ba4739e RLG-Web server: send status events over WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 103
diff changeset
970 gamemux.removeListener('begin', beginH);
7d444ba4739e RLG-Web server: send status events over WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 103
diff changeset
971 gamemux.removeListener('list', listH);
7d444ba4739e RLG-Web server: send status events over WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 103
diff changeset
972 gamemux.removeListener('end', endH);
7d444ba4739e RLG-Web server: send status events over WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 103
diff changeset
973 });
7d444ba4739e RLG-Web server: send status events over WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 103
diff changeset
974 tell();
100
3dbfdaf62623 RLG-Web: begin converting to WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 94
diff changeset
975 }
3dbfdaf62623 RLG-Web: begin converting to WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 94
diff changeset
976 else
104
7d444ba4739e RLG-Web server: send status events over WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 103
diff changeset
977 wsRequest.reject(404, "No such resource.");
7d444ba4739e RLG-Web server: send status events over WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 103
diff changeset
978 }
7d444ba4739e RLG-Web server: send status events over WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 103
diff changeset
979
163
0f6da35b27a0 RLGWebD: overhaul the list of current games.
John "Elwin" Edwards
parents: 162
diff changeset
980 /* Only games with low idle time are included. Use getStatus() for the
0f6da35b27a0 RLGWebD: overhaul the list of current games.
John "Elwin" Edwards
parents: 162
diff changeset
981 * complete list. */
104
7d444ba4739e RLG-Web server: send status events over WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 103
diff changeset
982 function pushStatus() {
163
0f6da35b27a0 RLGWebD: overhaul the list of current games.
John "Elwin" Edwards
parents: 162
diff changeset
983 var now = new Date();
0f6da35b27a0 RLGWebD: overhaul the list of current games.
John "Elwin" Edwards
parents: 162
diff changeset
984 var statusinfo = {"t": "p", "s": allowlogin, "g": [], "dgl": []};
0f6da35b27a0 RLGWebD: overhaul the list of current games.
John "Elwin" Edwards
parents: 162
diff changeset
985 for (var tag in sessions) {
0f6da35b27a0 RLGWebD: overhaul the list of current games.
John "Elwin" Edwards
parents: 162
diff changeset
986 var delta = now - sessions[tag].lasttime;
0f6da35b27a0 RLGWebD: overhaul the list of current games.
John "Elwin" Edwards
parents: 162
diff changeset
987 if (delta < 60000) {
0f6da35b27a0 RLGWebD: overhaul the list of current games.
John "Elwin" Edwards
parents: 162
diff changeset
988 var gamedesc = {};
0f6da35b27a0 RLGWebD: overhaul the list of current games.
John "Elwin" Edwards
parents: 162
diff changeset
989 gamedesc["p"] = sessions[tag].pname;
0f6da35b27a0 RLGWebD: overhaul the list of current games.
John "Elwin" Edwards
parents: 162
diff changeset
990 gamedesc["g"] = sessions[tag].game.uname;
0f6da35b27a0 RLGWebD: overhaul the list of current games.
John "Elwin" Edwards
parents: 162
diff changeset
991 gamedesc["i"] = delta;
0f6da35b27a0 RLGWebD: overhaul the list of current games.
John "Elwin" Edwards
parents: 162
diff changeset
992 statusinfo["g"].push(gamedesc);
0f6da35b27a0 RLGWebD: overhaul the list of current games.
John "Elwin" Edwards
parents: 162
diff changeset
993 }
0f6da35b27a0 RLGWebD: overhaul the list of current games.
John "Elwin" Edwards
parents: 162
diff changeset
994 }
0f6da35b27a0 RLGWebD: overhaul the list of current games.
John "Elwin" Edwards
parents: 162
diff changeset
995 for (var tag in dglgames) {
0f6da35b27a0 RLGWebD: overhaul the list of current games.
John "Elwin" Edwards
parents: 162
diff changeset
996 var dglinfo = {};
0f6da35b27a0 RLGWebD: overhaul the list of current games.
John "Elwin" Edwards
parents: 162
diff changeset
997 var slash = tag.search('/');
0f6da35b27a0 RLGWebD: overhaul the list of current games.
John "Elwin" Edwards
parents: 162
diff changeset
998 dglinfo["g"] = tag.slice(0, slash);
0f6da35b27a0 RLGWebD: overhaul the list of current games.
John "Elwin" Edwards
parents: 162
diff changeset
999 dglinfo["p"] = tag.slice(slash + 1);
0f6da35b27a0 RLGWebD: overhaul the list of current games.
John "Elwin" Edwards
parents: 162
diff changeset
1000 dglinfo["i"] = -1;
0f6da35b27a0 RLGWebD: overhaul the list of current games.
John "Elwin" Edwards
parents: 162
diff changeset
1001 statusinfo["dgl"].push(dglinfo);
0f6da35b27a0 RLGWebD: overhaul the list of current games.
John "Elwin" Edwards
parents: 162
diff changeset
1002 }
0f6da35b27a0 RLGWebD: overhaul the list of current games.
John "Elwin" Edwards
parents: 162
diff changeset
1003 gamemux.emit('list', statusinfo);
100
3dbfdaf62623 RLG-Web: begin converting to WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 94
diff changeset
1004 }
3dbfdaf62623 RLG-Web: begin converting to WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 94
diff changeset
1005
27
83f9a799a374 rlgwebd.js: read commands from the console
John "Elwin" Edwards <elwin@sdf.org>
parents: 26
diff changeset
1006 function shutdown () {
83f9a799a374 rlgwebd.js: read commands from the console
John "Elwin" Edwards <elwin@sdf.org>
parents: 26
diff changeset
1007 httpServer.close();
83f9a799a374 rlgwebd.js: read commands from the console
John "Elwin" Edwards <elwin@sdf.org>
parents: 26
diff changeset
1008 httpServer.removeAllListeners('request');
28
2ad2b6491aa9 rlgwebd.js: become a real daemon.
John "Elwin" Edwards <elwin@sdf.org>
parents: 27
diff changeset
1009 ctlServer.close();
27
83f9a799a374 rlgwebd.js: read commands from the console
John "Elwin" Edwards <elwin@sdf.org>
parents: 26
diff changeset
1010 tslog("Shutting down...");
83f9a799a374 rlgwebd.js: read commands from the console
John "Elwin" Edwards <elwin@sdf.org>
parents: 26
diff changeset
1011 process.exit();
83f9a799a374 rlgwebd.js: read commands from the console
John "Elwin" Edwards <elwin@sdf.org>
parents: 26
diff changeset
1012 }
83f9a799a374 rlgwebd.js: read commands from the console
John "Elwin" Edwards <elwin@sdf.org>
parents: 26
diff changeset
1013
158
9961a538c00e rlgwebd.js: get rid of numerical game identifiers.
John "Elwin" Edwards
parents: 157
diff changeset
1014 function consoleHandler(chunk) {
27
83f9a799a374 rlgwebd.js: read commands from the console
John "Elwin" Edwards <elwin@sdf.org>
parents: 26
diff changeset
1015 var msg = chunk.toString().split('\n')[0];
83f9a799a374 rlgwebd.js: read commands from the console
John "Elwin" Edwards <elwin@sdf.org>
parents: 26
diff changeset
1016 if (msg == "quit") {
83f9a799a374 rlgwebd.js: read commands from the console
John "Elwin" Edwards <elwin@sdf.org>
parents: 26
diff changeset
1017 allowlogin = false;
83f9a799a374 rlgwebd.js: read commands from the console
John "Elwin" Edwards <elwin@sdf.org>
parents: 26
diff changeset
1018 tslog("Disconnecting...");
158
9961a538c00e rlgwebd.js: get rid of numerical game identifiers.
John "Elwin" Edwards
parents: 157
diff changeset
1019 for (var tag in sessions) {
9961a538c00e rlgwebd.js: get rid of numerical game identifiers.
John "Elwin" Edwards
parents: 157
diff changeset
1020 sessions[tag].close();
27
83f9a799a374 rlgwebd.js: read commands from the console
John "Elwin" Edwards <elwin@sdf.org>
parents: 26
diff changeset
1021 }
155
245a2959f504 Begin support for watching dgamelaunch games.
John "Elwin" Edwards
parents: 148
diff changeset
1022 progressWatcher.stdin.end("\n");
55
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
1023 setTimeout(shutdown, 2000);
27
83f9a799a374 rlgwebd.js: read commands from the console
John "Elwin" Edwards <elwin@sdf.org>
parents: 26
diff changeset
1024 }
83f9a799a374 rlgwebd.js: read commands from the console
John "Elwin" Edwards <elwin@sdf.org>
parents: 26
diff changeset
1025 }
83f9a799a374 rlgwebd.js: read commands from the console
John "Elwin" Edwards <elwin@sdf.org>
parents: 26
diff changeset
1026
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
1027 process.on("exit", function () {
158
9961a538c00e rlgwebd.js: get rid of numerical game identifiers.
John "Elwin" Edwards
parents: 157
diff changeset
1028 for (var tag in sessions) {
9961a538c00e rlgwebd.js: get rid of numerical game identifiers.
John "Elwin" Edwards
parents: 157
diff changeset
1029 sessions[tag].term.kill('SIGHUP');
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
1030 }
26
9b58f8d3ea70 rlgwebd.js: add timestamps to log messages.
John "Elwin" Edwards <elwin@sdf.org>
parents: 23
diff changeset
1031 tslog("Quitting...");
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
1032 return;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
1033 });
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
1034
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
1035 /* Initialization STARTS HERE */
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
1036 process.env["TERM"] = "xterm-256color";
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
1037
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
1038 if (process.getuid() != 0) {
26
9b58f8d3ea70 rlgwebd.js: add timestamps to log messages.
John "Elwin" Edwards <elwin@sdf.org>
parents: 23
diff changeset
1039 tslog("Not running as root, cannot chroot.");
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
1040 process.exit(1);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
1041 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
1042
29
cf9d294bc52f rlgwebd.js: fix reference error
John "Elwin" Edwards <elwin@sdf.org>
parents: 28
diff changeset
1043 var httpServer; // declare here so shutdown() can find it
100
3dbfdaf62623 RLG-Web: begin converting to WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 94
diff changeset
1044 var wsServer;
155
245a2959f504 Begin support for watching dgamelaunch games.
John "Elwin" Edwards
parents: 148
diff changeset
1045 var progressWatcher;
29
cf9d294bc52f rlgwebd.js: fix reference error
John "Elwin" Edwards <elwin@sdf.org>
parents: 28
diff changeset
1046
157
e7f809f06c5c Use posix.getpwnam() to look up UID/GID to drop to.
John "Elwin" Edwards
parents: 156
diff changeset
1047 var pwent;
e7f809f06c5c Use posix.getpwnam() to look up UID/GID to drop to.
John "Elwin" Edwards
parents: 156
diff changeset
1048 try {
e7f809f06c5c Use posix.getpwnam() to look up UID/GID to drop to.
John "Elwin" Edwards
parents: 156
diff changeset
1049 pwent = posix.getpwnam(dropToUser);
e7f809f06c5c Use posix.getpwnam() to look up UID/GID to drop to.
John "Elwin" Edwards
parents: 156
diff changeset
1050 }
e7f809f06c5c Use posix.getpwnam() to look up UID/GID to drop to.
John "Elwin" Edwards
parents: 156
diff changeset
1051 catch (err) {
e7f809f06c5c Use posix.getpwnam() to look up UID/GID to drop to.
John "Elwin" Edwards
parents: 156
diff changeset
1052 tslog("Could not drop to user %s: user does not exist", dropToUser);
e7f809f06c5c Use posix.getpwnam() to look up UID/GID to drop to.
John "Elwin" Edwards
parents: 156
diff changeset
1053 process.exit(1);
e7f809f06c5c Use posix.getpwnam() to look up UID/GID to drop to.
John "Elwin" Edwards
parents: 156
diff changeset
1054 }
e7f809f06c5c Use posix.getpwnam() to look up UID/GID to drop to.
John "Elwin" Edwards
parents: 156
diff changeset
1055
85
4303d94d87a2 rlgwebd.js: Unlink control socket at startup.
John "Elwin" Edwards <elwin@sdf.org>
parents: 84
diff changeset
1056 /* This could be nonblocking, but nothing else can start yet anyway. */
4303d94d87a2 rlgwebd.js: Unlink control socket at startup.
John "Elwin" Edwards <elwin@sdf.org>
parents: 84
diff changeset
1057 if (fs.existsSync(ctlsocket)) {
4303d94d87a2 rlgwebd.js: Unlink control socket at startup.
John "Elwin" Edwards <elwin@sdf.org>
parents: 84
diff changeset
1058 fs.unlinkSync(ctlsocket);
4303d94d87a2 rlgwebd.js: Unlink control socket at startup.
John "Elwin" Edwards <elwin@sdf.org>
parents: 84
diff changeset
1059 }
4303d94d87a2 rlgwebd.js: Unlink control socket at startup.
John "Elwin" Edwards <elwin@sdf.org>
parents: 84
diff changeset
1060
28
2ad2b6491aa9 rlgwebd.js: become a real daemon.
John "Elwin" Edwards <elwin@sdf.org>
parents: 27
diff changeset
1061 /* Open the control socket before chrooting where it can't be found */
2ad2b6491aa9 rlgwebd.js: become a real daemon.
John "Elwin" Edwards <elwin@sdf.org>
parents: 27
diff changeset
1062 var ctlServer = net.createServer(function (sock) {
158
9961a538c00e rlgwebd.js: get rid of numerical game identifiers.
John "Elwin" Edwards
parents: 157
diff changeset
1063 sock.on('data', consoleHandler);
28
2ad2b6491aa9 rlgwebd.js: become a real daemon.
John "Elwin" Edwards <elwin@sdf.org>
parents: 27
diff changeset
1064 });
2ad2b6491aa9 rlgwebd.js: become a real daemon.
John "Elwin" Edwards <elwin@sdf.org>
parents: 27
diff changeset
1065 ctlServer.listen(ctlsocket, function () {
139
dcd07c1d846a Replace the daemon module with posix.
John "Elwin" Edwards
parents: 132
diff changeset
1066 /* rlgwebd.js now assumes that it has been started by the rlgwebd shell
dcd07c1d846a Replace the daemon module with posix.
John "Elwin" Edwards
parents: 132
diff changeset
1067 * script, or some other method that detaches it and sets up stdio. */
dcd07c1d846a Replace the daemon module with posix.
John "Elwin" Edwards
parents: 132
diff changeset
1068 /* chroot and drop permissions. posix.chroot() does chdir() itself. */
28
2ad2b6491aa9 rlgwebd.js: become a real daemon.
John "Elwin" Edwards <elwin@sdf.org>
parents: 27
diff changeset
1069 try {
139
dcd07c1d846a Replace the daemon module with posix.
John "Elwin" Edwards
parents: 132
diff changeset
1070 posix.chroot(chrootDir);
28
2ad2b6491aa9 rlgwebd.js: become a real daemon.
John "Elwin" Edwards <elwin@sdf.org>
parents: 27
diff changeset
1071 }
2ad2b6491aa9 rlgwebd.js: become a real daemon.
John "Elwin" Edwards <elwin@sdf.org>
parents: 27
diff changeset
1072 catch (err) {
2ad2b6491aa9 rlgwebd.js: become a real daemon.
John "Elwin" Edwards <elwin@sdf.org>
parents: 27
diff changeset
1073 tslog("chroot to %s failed: %s", chrootDir, err);
2ad2b6491aa9 rlgwebd.js: become a real daemon.
John "Elwin" Edwards <elwin@sdf.org>
parents: 27
diff changeset
1074 process.exit(1);
2ad2b6491aa9 rlgwebd.js: become a real daemon.
John "Elwin" Edwards <elwin@sdf.org>
parents: 27
diff changeset
1075 }
2ad2b6491aa9 rlgwebd.js: become a real daemon.
John "Elwin" Edwards <elwin@sdf.org>
parents: 27
diff changeset
1076 try {
2ad2b6491aa9 rlgwebd.js: become a real daemon.
John "Elwin" Edwards <elwin@sdf.org>
parents: 27
diff changeset
1077 // drop gid first, that requires UID=0
157
e7f809f06c5c Use posix.getpwnam() to look up UID/GID to drop to.
John "Elwin" Edwards
parents: 156
diff changeset
1078 process.setgid(pwent.gid);
e7f809f06c5c Use posix.getpwnam() to look up UID/GID to drop to.
John "Elwin" Edwards
parents: 156
diff changeset
1079 process.setuid(pwent.uid);
28
2ad2b6491aa9 rlgwebd.js: become a real daemon.
John "Elwin" Edwards <elwin@sdf.org>
parents: 27
diff changeset
1080 }
2ad2b6491aa9 rlgwebd.js: become a real daemon.
John "Elwin" Edwards <elwin@sdf.org>
parents: 27
diff changeset
1081 catch (err) {
2ad2b6491aa9 rlgwebd.js: become a real daemon.
John "Elwin" Edwards <elwin@sdf.org>
parents: 27
diff changeset
1082 tslog("Could not drop permissions: %s", err);
2ad2b6491aa9 rlgwebd.js: become a real daemon.
John "Elwin" Edwards <elwin@sdf.org>
parents: 27
diff changeset
1083 process.exit(1);
2ad2b6491aa9 rlgwebd.js: become a real daemon.
John "Elwin" Edwards <elwin@sdf.org>
parents: 27
diff changeset
1084 }
29
cf9d294bc52f rlgwebd.js: fix reference error
John "Elwin" Edwards <elwin@sdf.org>
parents: 28
diff changeset
1085 httpServer = http.createServer(webHandler);
30
b5570a594266 rlgwebd.js: listen on any address
John "Elwin" Edwards <elwin@sdf.org>
parents: 29
diff changeset
1086 httpServer.listen(httpPort);
b5570a594266 rlgwebd.js: listen on any address
John "Elwin" Edwards <elwin@sdf.org>
parents: 29
diff changeset
1087 tslog('rlgwebd running on port %d', httpPort);
100
3dbfdaf62623 RLG-Web: begin converting to WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 94
diff changeset
1088 wsServer = new WebSocketServer({"httpServer": httpServer});
3dbfdaf62623 RLG-Web: begin converting to WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 94
diff changeset
1089 wsServer.on("request", wsHandler);
3dbfdaf62623 RLG-Web: begin converting to WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 94
diff changeset
1090 tslog('WebSockets are online');
155
245a2959f504 Begin support for watching dgamelaunch games.
John "Elwin" Edwards
parents: 148
diff changeset
1091 progressWatcher = startProgressWatcher();
163
0f6da35b27a0 RLGWebD: overhaul the list of current games.
John "Elwin" Edwards
parents: 162
diff changeset
1092 setInterval(pushStatus, 40000);
28
2ad2b6491aa9 rlgwebd.js: become a real daemon.
John "Elwin" Edwards <elwin@sdf.org>
parents: 27
diff changeset
1093 });
2ad2b6491aa9 rlgwebd.js: become a real daemon.
John "Elwin" Edwards <elwin@sdf.org>
parents: 27
diff changeset
1094