-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchat_routes.js
152 lines (137 loc) · 4.98 KB
/
chat_routes.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
var content = require('./contents.js').content;
var fs = require('fs');
var contentType = {html:'text/html',jpg:'image/jpeg',ico:'image/x-icon'};
var handler = {};
handler['/template'] = function(req,res){
var checkPassword = function(input){
var details = content.querystring.parse(input);
console.log(details);
var userid = details.userid;
var password = details.pswrd;
res.writeHead(200, {'Content-Type': contentType.html});
if( password == content.details[userid])
res.write(content.chatPage.replace(/{MESSAGES}/,content.messages.join('\n')).replace(/{USERNAME}/,userid));
else{
res.write(content.loginPage);
res.write("<center><h2><font color = 'white'>Incorrect password or login name please try again!</h2></center></font>");
}
res.end();
}
req.setEncoding('utf8');
req.on('data',checkPassword);
};
handler['/signUp'] = function(req,res){
res.writeHead(200,{'Content-Type': contentType.html});
res.write(content.signUpPage);
res.end();
};
handler['/delete'] = function(req,res){
var deleteMsg = function(input){
var userid = input.split('&')[0].split('=')[1];
fs.writeFile(content.msgFileName,"[]");
content.messages = [];
res.write(content.chatPage.replace(/{MESSAGES}/,content.messages.join('\n')).replace(/{USERNAME}/,userid));
res.end();
}
req.setEncoding('utf8');
req.on('data',deleteMsg);
}
handler['/refresh'] = function(req,res){
var refresh = function(input){
var userid = input.split('&')[0].split('=')[1];
res.write(content.chatPage.replace(/{MESSAGES}/,content.messages.join('\n')).replace(/{USERNAME}/,userid));
res.end();
}
req.setEncoding('utf8');
req.on('data',refresh);
}
handler['/chat'] = function(req,res){
var tab = " ";
var display= function(input){
var details = content.querystring.parse(input);
var user = details.name;
var msg = details.message;
var date = new Date().toString().split('G')[0];
var item = user + " ( " + date + " ) " + " : " + tab + msg;
user && msg && content.messages.push(item) && fs.writeFile(content.msgFileName,JSON.stringify(content.messages));
res.writeHead(200, {'Content-Type': contentType.html});
res.write(content.chatPage.replace(/{MESSAGES}/,content.messages.join('\n')).replace(/{USERNAME}/,user));
res.end();
}
req.setEncoding('utf8');
req.on('data',display);
}
handler['/bg.jpg'] = function(req,res){
res.writeHead(200,{'Content-Type': contentType.jpg});
res.write(content.bg_jpg);
res.end();
};
handler['/home.jpg'] = function(req,res){
res.writeHead(200,{'Content-Type': contentType.jpg});
res.write(content.home);
res.end();
};
handler['/login.jpg'] = function(req,res){
res.writeHead(200,{'Content-Type': contentType.jpg});
res.write(content.login);
res.end();
};
handler['/signup.jpg'] = function(req,res){
res.writeHead(200,{'Content-Type': contentType.jpg});
res.write(content.signup);
res.end();
};
handler['/favicon.ico'] = function(req,res){
res.writeHead(200,{'Content-Type': contentType.ico});
res.write(content.fav_ico);
res.end();
};
handler['/login'] = function(req,res){
var req_url = content.url.parse(req.url,true);
res.writeHead(200,{'Content-Type': contentType.html});
res.write(content.loginPage);
res.end();
};
handler['/'] = function(req,res){
res.writeHead(200,{'Content-Type': contentType.html});
res.write(content.homepage);
res.end();
};
handler['/index'] = function(req,res){
var getDetails = function(input){
var details = content.querystring.parse(input);
console.log(details)
var userid = details.name;
var password = details.pass;
var confirmPass = details.repass;
var expr = /(?=^.{8,}$)((?=.*\d)|(?=.*\W+))(?![.\n])(?=.*[A-Z])(?=.*[a-z]).*$/;
if(password.match(expr)==null){
res.writeHead(200,{'Content-Type': contentType.html});
res.write(content.signUpPage);
res.write("<center><h2><font color = 'white'>Passwords must contain at least 1 upper case letter,<br>1 lower case letter,<br>1 number or special character,<br>at least 8 characters in length<font><h2/></center>");
res.end();
}
else if( content.details[userid] ){
res.writeHead(200,{'Content-Type': contentType.html});
res.write(content.signUpPage);
res.write("<center><h2><font color = 'white'> Username already exists!!<font><h2/></center>");
res.end();
}
else if(password != confirmPass ){
res.writeHead(200,{'Content-Type': contentType.html});
res.write(content.signUpPage);
res.write("<center><h2><font color = 'white'> Passwords do not match!!<font><h2/></center>");
}
else {
content.details[userid] = password;
fs.writeFile(content.detailsFileName,JSON.stringify(content.details));
res.writeHead(200,{'Content-Type': contentType.html});
res.write(content.homepage);
res.write("<center><h2><font color = 'white'> Account created successfully!<font><h2/></center>");
}
res.end();
}
req.setEncoding('utf8');
req.on('data',getDetails);
}
exports.routes = handler;