-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchatclient.js
196 lines (97 loc) · 2.64 KB
/
chatclient.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
var net = require('net');
if(process.argv.length != 4){
console.log(process.argv.length);
console.log("Usage: node %s <host> <port>", process.argv[1]);
process.exit(1);
}
var host=process.argv[2];
var port=process.argv[3];
if(host.length >253 || port.length >5 ){
console.log("Invalid host or port. Try again!\nUsage: node %s <port>", process.argv[1]);
process.exit(1);
}
var readlineSync =require('readline-sync');
var user ={};
var validateRegex = /[0-9a-zA-Z]{5,}/;
var client = new net.Socket();
console.log("Simple telnet.js developed by Kartik Desai, SecAD");
console.log("Connecting to localhost:%s", port);
console.log("Connected to: %s:%s", host, port);
console.log("You need to login before sending/receiving messages");
client.connect(port,host, connected);
function connected(){
loginsync();
}
var counter = 0;
client.on("data",(data) => {
var Message = (data.toString());
var failedLogin = "Authentication_failed_Please_Try_Again! Invalid username or password";
if(Message!==failedLogin){
console.log("Received data:" + Message);
if(!counter){
console.log("Welcome to chat system. Type anything to send to public chat");
console.log("Type .userlist to see online users");
console.log("Type .exit to logout and close the connection");
console.log("Type pm for private chat");
callCommand()
}
counter++;
}
else {
console.log(failedLogin);
loginsync();
}
})
client.on("error",(err) => {
console.log("Error");
process.exit(2);
})
client.on("close",(data) => {
console.log("Connection has been disconnected");
process.exit(3);
})
function loginsync(){
user.username = readlineSync.question('Username ',{
});
user.password = readlineSync.question('Password ',{
hideEchoBack: true
});
client.write(JSON.stringify(user));
}
function callCommand(){
const keyboard = require('readline').createInterface({
input: process.stdin,
output:process.stdout
})
keyboard.on('line', (input) => {
if (input ===".exit"){
client.destroy();
console.log("Disconnected");
process.exit();
}
else if(input ===".userlist") {
var userList={}
userList.Type = "userlist";
userList.Message = "getUserList";
client.write(JSON.stringify(userList));
}
else if(input ==="pm") {
var privateChat={}
console.log("Now you can send message to specific user")
privateChat.Type = "private";
keyboard.question('To ', (answer) => {
privateChat.To = answer;
keyboard.question('Message ', (answer) => {
privateChat.Message = answer;
client.write(JSON.stringify(privateChat));
})
})
}
else{
var publicChat={}
publicChat.Type = "public"
publicChat.Message = input;
client.write(JSON.stringify(publicChat));
}
})
}