-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.js
More file actions
192 lines (157 loc) · 5.31 KB
/
Copy pathserver.js
File metadata and controls
192 lines (157 loc) · 5.31 KB
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
var settings = require('./settings.json');
var express = require('express');
var socket = require('socket.io')({
username: 'anonymous',
email: 'none',
_id: ''
});
var http = require('http');
var path = require('path');
var cm = require('./api/channels');
var rauth = require('./api/auth');
var channels = require('./api/channels');
var users = require('./modules/collections/users');
var exec = require('child_process').exec;
var db = require('./modules/db/mongo.js');
var app = express();
var emit = function(room,type,message) {
var to = room || "global";
if(io.sockets){
io.sockets.in(to).emit(type,message);
}
};
//Get the current git revision number used for forcing clients to reload if different.
var commit_command = 'git rev-parse HEAD';
exec(commit_command, function(error, stdout, stderr){
console.log("revision is: "+stdout);
settings.revision = stdout;
});
app.use(express.cookieParser());
app.use(express.bodyParser());
app.use(express.static(path.join(__dirname,'public')));
var socketServer = http.createServer(app);
var io = require('socket.io').listen(socketServer);
io.sockets.on('connection',function(socket){
socket.on('auth', function(data){
if(!data.user || !data.pass){
socket.emit("rejected due to missing credentials");
}
rauth.socketAuthentication(data.user, data.pass, function(ret){
if(ret.success && ret.user._id){
console.log(ret.user._id+" has successfully logged on!");
users.addUser(data.user, socket.id);
socket.username = ret.user.username;
socket.email = ret.user.email;
socket._id = ret.user._id;
socket.join('global');
io.sockets.in("global").emit("info",ret.user.username+" connected!");
socket.join(ret.user.username);
/*socket.set("username", ret.user.username, function(){
socket.set("email", ret.user.email, function(){});
socket.set("_id", ret.user._id, function(){});
socket.join('global');
socket.join(ret.user.username);
});*/
} else {
socket.emit("rejected due auth failure");
}
});
});
socket.on('disconnect', function() {
var data = socket.username;
io.sockets.in("global").emit("info",data+" disconnected!");
socket.leave('global');
delete io.sockets[socket.id];
});
// On a command, lookup the appropriate module and call execute on it. The
// arguments for execute should always be the same.
socket.on('command', function (data) {
try {
// Remember the command already has an / in it
var cmd = require('./modules/commands'+data.command.toLowerCase());
var context = {
sockets: io.sockets,
command: data.command,
user: data.user,
args: data.args,
channel: data.channel,
socket: socket,
timestamp: new Date().toJSON()
};
cmd.execute(context,function(data){
});
} catch(e) {
console.log("Command not found: "+e);
socket.emit("global","info","Command not found");
}
});
socket.on('join', function(data){
socket.join(data.room);
socket.broadcast.in(data.room).emit("joined", socket.store.data.username);
});
socket.on('leave', function(data){
socket.leave(data.room);
socket.broadcast.in(data.room).emit("left", socket.store.data.username);
});
});
// HTTP Definitions
// TODO: Refactor these somewhere more elegant
/*
app.get('/rooms/list', function(req,res){
cm.getActiveRooms(req,res,io.sockets);
});
*/
app.get("/rooms/:room", function(req,res){
channels.getSocketsInRoom(req,res,io);
});
/*
app.get("/rooms/:channel/:limit",function(req,res){
history.getLastMessages(req,res);
});
app.get("/rooms/:channel/:uid/:limit",function(req,res){
history.getLastMessages(req,res);
});
app.get("/locations/:year/:month/:day",function(req,res){
profile.getLocationReport(req,res);
});
*/
app.post("/users/authenticate",rauth.authenticateUser);
app.post("/users/register",rauth.registerUser);
app.get('/health', function(req, res){
var body = 'ok';
res.setHeader('Content-Type', 'text/plain');
res.setHeader('Content-Length', body.length);
emit('health-check',{success: true});
res.end(body);
});
app.get('/login', function(req,res){
res.sendfile('./views/login.html');
});
app.get('/register', function(req,res){
res.sendfile('./views/register.html');
});
app.get('/logout', function(req,res){
res.clearCookie('sec',null);
res.redirect('/login');
});
app.get('/', function(req, res){
if(req.cookies){
if(req.cookies.sec){
res.sendfile('./views/index.html');
} else {
res.redirect('/login');
}
}
});
app.get('/revision', function(req, res){
console.log(settings.revision);
res.end(settings.revision);
});
var port = settings.port;
if(process.argv[2]){
port = process.argv[2];
}
db.connect();
// Clears out all db data rooms before the server starts
socketServer.listen(port);
console.log('Listening on port '+port+', use http://localhost:'+port+'/health to test.');