-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathyunomakefriend.moon
86 lines (62 loc) · 1.65 KB
/
yunomakefriend.moon
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
json = require "json"
process = require "process"
argparse = require "argparse"
moonscript = require "moonscript"
util = require "moonscript.util"
colors = require "term.colors"
Friend = require("friend")
parser = with argparse "yunomakefriend", "Time to make self-hosted friends with YunoRock."
\command "list"
with \command "request-friendship"
with \argument "domain"
\args 1
arguments = parser\parse!
friendClass = require "friend.moon"
friends = do
code = moonscript.loadfile "myfriends.moon"
friends = {}
util.setfenv code, {
friend: (name, opt) ->
table.insert friends, friendClass name, opt
}
code!
friends
if arguments.list
for friend in *friends
friend\print!
os.exit 0
ssh, reason = process.exec "ssh", {"anonymous@#{arguments.domain}"}
unless ssh
print reason
os.exit 1
sshInput = ""
readSSHLine = (ssh) ->
coroutine.wrap ->
while process.waitpid ssh\pid!, process.WNOHANG
s = ssh\stderr!
if s
io.stderr\write "ssh: ", s
s = ssh\stdout!
if s
sshInput ..= s
while sshInput\match "\n"
line = sshInput\match "^[^\n]*"
sshInput = sshInput\sub line\len! + 2, sshInput\len!
coroutine.yield line
beyondMotd = false
for line in readSSHLine ssh
unless beyondMotd
if line\match '"I am a friendly server"'
beyondMotd = true
else
print "MOTD: ", line
else
ssh\stdin json.encode("let's be friends").."\n"
success, input = pcall -> json.decode line
unless success
print "message not understood: ", line
os.exit 1
print "received #{require("pl.pretty").write input}"
if input == "hi"
print "sending friendship request™"
ssh\stdin json.encode("let's be friends").."\n"