-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathserver.js
135 lines (112 loc) · 3.46 KB
/
server.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
/*
* TODO
- public _realtime_ log
- change nick with /nick
- broadcast message on changenick
- nick list on /who
- /me action command
*/
var carrier = require('carrier'),
net = require('net'),
fs = require('fs');
require('./date.format.js');
var connections = [],
date,
logsdir = '/logs/';
net.createServer(function(conn) {
connections.forEach(function(one_conn) {
one_conn.write('* Someone joined!\r\n');
});
connections.push(conn);
console.log('Someone connected, '+ connections.length +' total.');
conn.write("***********************************************\r\n");
conn.write("* Hello and welcome to Marvelous Chat server! *\r\n");
conn.write("***********************************************\r\n\r\n");
conn.write("Who are you?:\r\n> ");
var username;
carrier.carry(conn, function(line) {
if (!username) {
username = line;
conn.write("\r\n* Hello "+ username +"!\r\n");
conn.write("*\tUse /help to see all the available commands.\r\n");
conn.write("*\tThere are: "+ connections.length +" people connected. \r\n\r\n");
return;
}
//Special commands
switch (line) {
case '/nick':
conn.write('Not implemented\r\n');
break;
case '/help':
conn.write('\n* Marvelous Chat help section *\r\n\r\n');
conn.write('/nick newnick - Change nick to "newnick".\r\n');
conn.write('/quit - Quit the server.\r\n');
conn.write('/who - Number of people on the server.\r\n');
conn.write('/motd - METAL GEAR???\r\n');
conn.write('/help - This help page.\r\n\r\n');
break;
case '/who':
conn.write('* There are currently '+ connections.length +' people connected.\r\n');
break;
case '/quit':
connections.forEach(function(one_conn) {
one_conn.write('* '+ username +' quitted!\r\n');
});
conn.end();
console.log('[Log] '+ username +' quitted!\r\n');
break;
case '/motd':
conn.write('Not implemented\r\n');
break;
case '/me':
conn.write('Not implemented\r\n');
break;
default:
/*if (line.substring(0, 4) == '/all ') {
connections.forEach(function(one_conn) {
one_conn.write('* Broadcast: '+ line +'\r\n');
});
}*/
date = new Date();
var newline = "["+ date.format("isoTime") +"] "+ username +": "+ line + "\r\n";
connections.forEach(function(one_conn) {
one_conn.write(newline);
});
var log = fs.createWriteStream(__dirname + logsdir + 'log_'+ date.format("isoDate") +'.txt', { 'flags': 'a'});
log.write(newline);
break;
}
});
conn.on('close', function() {
var pos = connections.indexOf(conn);
if (pos >= 0) {
connections.splice(pos, 1);
console.log('[Log] '+ username + ' disconnected.');
}
});
}).listen(8080);
console.log('[Log] Chat server started.');
/* Show daily chatlog */
var http = require('http'),
path = require('path'),
date = new Date();
http.createServer(function(req, res) {
var filename = __dirname + logsdir + '/log_'+ date.format("isoDate") +'.txt';
path.exists(filename, function(exists) {
if (!exists) {
res.writeHead(200, {"Content-Type": "text/plain"});
res.end("No daily log found.\n");
return;
}
fs.readFile(filename, function(err, data) {
if (err) {
res.writeHead(500, {"Content-Type": "text/plain"});
res.end(err);
return;
}
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.write(data, 'utf8');
res.end();
});
});
}).listen(8000);