-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.js
More file actions
87 lines (79 loc) · 2.34 KB
/
cli.js
File metadata and controls
87 lines (79 loc) · 2.34 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
import { apds } from './apds.js'
await apds.start(Deno.args[0] || 'default')
const pk = await apds.pubkey()
const sockets = new Set()
const send = async (hash) => {
const msg = await apds.get(hash)
const opened = await apds.open(msg)
const blob = await apds.get(opened.substring(13))
sockets.forEach(s => s.send(blob))
sockets.forEach(s => s.send(msg))
sockets.forEach(s => s.send(hash))
}
const render = async (h) => {
const get = await apds.get(h)
const opened = await apds.open(get)
const blob = await apds.get(opened.substring(13))
const yaml = await apds.parseYaml(blob)
//console.log(yaml)
const handle = yaml.name ? get.substring(0, 10) + ' ' + yaml.name : get.substring(0, 10)
console.log(`%c${handle} %c| ${yaml.body} -- %c${opened.substring(0, 13)}`, 'color: magenta', 'color: white', 'color: cyan')
//console.log(opened)
//console.log(blob)
}
const commands = async (c) => {
if (c.startsWith('nick')) {
const ar = c.split(' ')
if (ar[1]) {
await apds.put('name', ar[1])
console.log('You are now known as "' + ar[1] + '"')
} else {
console.log('ERROR: no nick to set')
}
}
if (c.startsWith('connect')) {
const ar = c.split(' ')
const s = ar[1]
console.log('Connecting to ' + s)
const ws = new WebSocket(s)
ws.onopen = () => {
console.log('Connected to: ' + s)
sockets.add(ws)
}
}
if (c.startsWith('clear')) {
const cleared = await apds.clear()
console.log('ERASING ALL DATA')
}
if (c.startsWith('exit')) {
console.log('EXITING APDS')
Deno.exit()
}
}
const flow = async () => {
const name = await apds.get('name')
const pub = await apds.pubkey()
const handle = name ? pub.substring(0, 10) + ' ' + name : pub.substring(0, 10)
const command = prompt(handle + '>')
if (command.startsWith('/')) {
await commands(command.substring(1))
await flow()
} else if (command.length > 0) {
const anmsg = await apds.compose(command)
await render(anmsg)
await send(anmsg)
await flow()
} else {
await flow()
}
}
if (pk) {
console.log('ANPROTO APDS ACTIVATED -- Your pubkey is: ' + pk)
await flow()
} else {
const kp = await apds.generate()
await apds.put('keypair', kp)
console.log('ANPROTO APDS ACTIVATED -- No keypair found!')
console.log('New keypair generated! ' + await apds.pubkey())
await flow()
}