-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
186 lines (164 loc) · 4.45 KB
/
index.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
#! /usr/bin/env node
/*
TermTalk - A simple and straightforward way of talking to your peers in the terminal.
Copyright (C) 2020 Terminalfreaks
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 <https://www.gnu.org/licenses/>.
*/
/*
THIS PART OF THE CODE, OR THE CLI CLIENT IS DEPRECATED. REFER TO tui/index.js
WE WILL NOT PROVIDE SUPPORT FOR THIS.
*/
const io = require('socket.io-client')
const fs = require("fs")
const { Input, prompt } = require('enquirer')
const defaultConfig = fs.readFileSync("config.json")
const Utils = require("./src/Utils")
const args = process.argv.slice(2).join(" ")
let loggedIn = false
if(!Utils.config) fs.appendFileSync(`${require("os").userInfo().homedir}/termtalk/.termtalkconf.json`, defaultConfig)
if(args.includes("--tui")) return require("./tui/index.js")
process.title = "TermTalk"
new Input({
name: "ip",
message: "IP to connect to (with port)."
}).run().then(ip => {
socket = io(ip.startsWith("http") ? ip : `http://${ip}`)
run()
})
function run() {
let user;
socket.on('connect', async () => {
if (!loggedIn) {
console.log(`Connected to the server.`)
try {
const ans = await prompt({
type: "select",
name: "checkpoint",
message: "What would you like to do?",
choices: ["Register", "Login"]
})
if (ans.checkpoint === "Register") {
_register()
} else {
_login()
}
} catch (err) {
console.error(err)
process.exit(0)
}
socket.on("authResult", (data) => {
if (!data.success) {
console.log("\u001b[31;1m" + data.message + "\u001b[0m")
if (data.method === "login") {
_login()
} else {
_register()
}
} else {
loggedIn = true
console.log("\u001b[32m" + data.message + "\u001b[0m")
user = data.user
_awaitMessage()
}
})
socket.on("methodResult", (data) => {
if (!data.success) {
console.log("\u001b[31;1m" + data.message + "\u001b[0m")
}
})
} else {
_logPromptPrefix()
}
socket.on("disconnect", () => {
console.log(`Client > You have been disconnected.`)
})
socket.on("reconnect", (attempt) => {
console.log(`Client > Reconnected after ${attempt} attempt(s).`)
})
socket.on("reconnect_attempt", (attempt) => {
console.log(`Client > Attempting reconnect. #${attempt}`)
})
socket.on('msg', (data) => {
console.log(`\u001b[1A\u001b[${process.stdout.columns}D\u001b[2K${data.username}#${data.tag} > ${data.msg}`)
_logPromptPrefix()
})
socket.on("getUserData", () => {
socket.emit("returnUserData", user)
})
process.on("beforeExit", () => {
socket.close(true)
})
})
async function _awaitMessage() {
try {
let { msg } = await prompt({ type: "input", name: "msg", message: `\u001b[${process.stdout.columns}D\u001b[2K` })
socket.emit("msg", { msg, uid: user.uid, username: user.username, tag: user.tag, sessionID: user.sessionID })
_awaitMessage()
} catch (e) {
console.error(e)
process.exit(0)
}
}
async function _login() {
try {
const login = await prompt([
{
type: "input",
name: "uid",
message: "UID (Account Name)?"
},
{
type: "password",
name: "password",
message: "Password?"
}
])
socket.emit("login", login)
} catch (e) {
console.error(e)
process.exit(0)
}
}
async function _register() {
try {
const reg = await prompt([
{
type: "input",
name: "uid",
message: "What do you want your Account Name (UID) to be? (This is used to login.)"
},
{
type: "input",
name: "username",
message: "Username?"
},
{
type: "input",
name: "tag",
message: "Tag? (This will appear after your name. 4 characters.)"
},
{
type: "password",
name: "password",
message: "Password?"
}
])
socket.emit("register", reg)
} catch (e) {
console.error(e)
process.exit(0)
}
}
function _logPromptPrefix() {
console.log(`»`)
}
}