-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.lua
145 lines (121 loc) · 3.16 KB
/
main.lua
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
table.remove(args, 1)
local mode = 0
if args[1]:lower() == "get" then
mode = 0
elseif args[1]:lower() == "set" then
mode = 1
elseif args[1]:lower() == "lst" then
mode = 2
elseif args[1]:lower() == "del" then
mode = 3
elseif args[1]:lower() == "help" then
process.stdout:write([[Usage: netrc [GET/SET/DEL] [OPTIONS]
Options:
-c clears the netrc file
--machine [name] specifies a machine
--login [name] sets the login to write
--password [name] sets the password to write
--account [name] sets the account password to write]])
return
else
process.stdout:write("invalid command: " .. args[1])
return
end
table.remove(args, 1)
local options = {}
do -- PARSE OPTIONS --
local command
for i,v in ipairs(args) do
if command then
options[command] = v
command = nil
elseif v:sub(1,2) == "--" then
command = v:sub(3,-1)
elseif v:sub(1,1) == "-" then
for tag in v:gmatch("[^%-]") do
options[tag] = true
end
end
end
end
local logins, default = {}
if not options.c then -- READ FILE CONTENT
function parse(l)
return l:match("machine%s*([^%s]+)"), {login = l:match("login%s*([^%s]+)"),
password = l:match("password%s*([^%s]+)"),
account = l:match("account%s*([^%s]+)"),
macdef = l:match("macdef%s*([^%s]+)")}
end
for l in io.lines(".netrc") do
local d = l:match("^%s*default")
if d then
_, default = parse(l)
else
local index, data = parse(l)
logins[index] = data
end
end
end
local uv = require("uv")
local login
if options.machine then
logins[options.machine] = logins[options.machine] or {}
login = logins[options.machine]
else
default = default or {}
login = default
end
if mode > 0 then
if mode == 1 then
local charset = {n = 0} -- init charset
for char in ("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"):gmatch(".") do table.insert(charset, char) charset.n = charset.n + 1 end
login.login = options.login or login.login
if options.password then
login.password = options.password:gsub("%%auto(%b[])", function(args)
local pass = {}
for i=1,32 do
math.randomseed(string.unpack("I4", uv.random(4)))
pass[i] = charset[math.random(charset.n)]
end
return table.concat(pass)
end)
end
elseif mode == 3 then
logins[options.machine] = nil
end
local file = io.open(".netrc", "wb")
for machine,login in pairs(logins) do
local str = {"machine", machine}
for i,v in pairs(login) do
table.insert(str, i)
table.insert(str, v)
end
file:write(table.concat(str, " "), "\n")
end
if default then
local str = {"default"}
for i,v in pairs(default) do
table.insert(str, i)
table.insert(str, v)
end
file:write(table.concat(str, " "))
end
file:close()
end
if mode < 2 then
local str = {}
for i,v in pairs(login) do
table.insert(str, i)
table.insert(str, ": ")
table.insert(str, v)
table.insert(str, "\n")
end
process.stdout:write(table.concat(str))
elseif mode == 2 then
local str = {}
for i,v in pairs(logins) do
table.insert(str, i)
end
process.stdout:write(table.concat(str, "\n"))
end
os.exit()