-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathapp.js
213 lines (175 loc) · 6.43 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/*
* Roomverse
* Modular real-time discussion platform.
*
* Copyright (C) 2013 Davide 'Folletto' Casali <folletto AT gmail DOT com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
******************************************************************************************
*
* This is the main application launcher.
*
* Install with:
* npm install
*
* Run with:
* node app.js
*
*/
var express = require('express');
var app = express();
var server = require('http').createServer(app);
var io = require('socket.io').listen(server);
var kombiner = require('kombiner').listen(server);
var irc = require('irc');
var levelup = require('levelup');
var fs = require('fs');
var pawns = require('./classes/pawns');
var db = require('./classes/socketdb');
var config = require('./config.json');
io.set('log level', 1);
// ****** Config Fallbacks
var config = null;
if (fs.existsSync("./config.json")) {
// Load local one
config = require('./config.json');
} else {
// Fallback
config = require('./config.default.json');
}
// ****************************************************************************************************
// ****** Middleware
// Post data
//app.use(express.bodyParser()); Deprecated in Connect 3.0
app.use(express.json());
app.use(express.urlencoded());
// Sessions
var SESSION_SECRET = 'chumbawamba';
var EXPRESS_SID_KEY = 'express.sid';
var cookieParser = express.cookieParser(SESSION_SECRET);
var sessionStore = new express.session.MemoryStore();
app.use(cookieParser);
app.use(express.session({
store: sessionStore,
cookie: {
httpOnly: true
},
key: EXPRESS_SID_KEY
}));
// Jade
app.engine('jade', require('jade').__express);
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
// Kombiner
kombiner.serve('js/roomverse.js', [
'./public/js/roomverse-core.js',
'./public/js/roomverse-rooms.js',
'./public/js/roomverse-room.js',
'./public/js/roomverse-roomusers.js',
'./public/js/actions.js',
'./public/js/roomverse-modules.js',
'./public/js/roomverse-widget.js',
'./public/js/socketdb.js',
]);
// DB
var DB_PATH = './db';
var database_type = 'memdown'; // default, in-memory, gets erased on termination
if (fs.existsSync(DB_PATH)) {
// Use LevelDown
console.log("DB: Using LevelDown.");
database_type = 'leveldown';
} else {
console.log("DB: Using MemDown. The data will be erased on termination.\n Create an empty 'db' folder to switch to LevelDown.");
}
var ldb = levelup(DB_PATH, { db: require(database_type) });
// Setup internal DB socket handler
db.levelup(ldb);
// ****************************************************************************************************
// ****** HTTP + Express
server.listen(process.env.PORT || 3000);
app.use(express.static(__dirname + "/public"));
// ****************************************************************************************************
// ****** Routing
app.get('/', function(req, res) {
res.render('index', {
userid: req.session.userid || '',
requirePassword: !!config.pipe.sasl, // TODO: must be made protocol-independent on River rewrite
rooms: req.query.rooms || req.session.rooms || config.defaults.rooms || 'roomverse-bots'
});
});
app.all('/c', function(req, res) {
// Session
req.session.userid = req.body.userid;
req.session.rooms = req.body.rooms;
req.session.password = req.body.password;
res.render('c', {
userid: req.body.userid,
rooms: req.body.rooms
});
});
// ****************************************************************************************************
// ****** Sharing sessions
//
// Socket.io Authorization -> https://github.com/LearnBoost/socket.io/wiki/Authorizing
// Sessions bridge -> https://github.com/leeroybrun/socketio-express-sessions/blob/master/server.js
//
io.set('authorization', function wb_authorization(data, callback) {
// Cookies?
if (data.headers.cookie) {
// Getting into the guts of Express 3.0:
// Call the initialized cookieParser(req, res, next) and handle its next() function call.
// Express cookieParser(req, res, next) is used initialy to parse data in "req.headers.cookie".
// Here our cookies are stored in "data.headers.cookie", so we just pass "data" to the first argument of function
cookieParser(data, {}, function(parseErr) {
if(parseErr) { return callback('Error parsing cookies.', false); }
// Get the SID that has been decoded by cookieParser into data
var sessionid = (data.signedCookies && data.signedCookies[EXPRESS_SID_KEY]) ||
(data.cookies && data.cookies[EXPRESS_SID_KEY]);
// Now let's load the session
sessionStore.load(sessionid, function(err, session) {
if (!err) {
if (session) {
data.session = session; // GAME ON, connecting the Express session. Look: socket.handshake.session
callback(null, true); // Required to start the connection.
} else {
callback('error: undefined session', false);
}
} else {
callback('error: ' + err, false);
}
});
});
}
});
// ****************************************************************************************************
// ****** Socket.io
//
// This prepare the pawns and initializes them when the socket events arrives
//
var pawns = new pawns.Pawns(config);
io.sockets.on('connection', function wb_iosocket(socket) {
// ****** Connect
var configPawn = {
userid: socket.handshake.session.userid,
rooms: socket.handshake.session.rooms && socket.handshake.session.rooms.split(" "),
password: socket.handshake.session.password
};
pawns.new(configPawn.userid, configPawn, socket, db); // add a better ID by using SHA on the password?
// ****** Disconnect
socket.on('disconnect', function(data) {
pawns.destroyWithHope(configPawn.userid);
//pawns.destroy(configPawn.userid);
});
});