-
Notifications
You must be signed in to change notification settings - Fork 0
/
startup.lua
282 lines (267 loc) · 9.85 KB
/
startup.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
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
local buttons = require 'modules/touchpoint'
local client = require 'client'
local JSON = require 'modules/json'
local messageRendering = require 'messages'
term.clear()
local monWidth, monHeight = term.getSize()
local whiteSpace = string.rep(' ', monWidth - 2)
local gui = buttons.new()
local token = nil
local function button(label, x, y, w, h, id)
gui:add(label, nil, x, y, (w -1) + x, (h -1) + y, colors.red, colors.lime, nil, nil, id)
end
keyMap = {
apostrophe = '\'',
comma = ',',
minus = '-',
period = '.',
slash = '/',
one = 1,
two = 2,
three = 3,
four = 4,
five = 5,
six = 6,
seven = 7,
eight = 8,
nine = 9,
zero = 0,
semicolon = ';',
equals = '=',
leftBracket = '[',
backslash = '\\',
rightBracket = ']',
grave = '`',
tab = '\t',
APOSTROPHE = '"',
COMMA = '<',
MINUS = '_',
PERIOD = '>',
SLASH = '?',
ONE = '!',
TWO = '@',
THREE = '#',
FOUR = '$',
FIVE = '%',
SIX = '^',
SEVEN = '&',
EIGHT = '*',
NINE = '(',
ZERO = ')',
SEMICOLON = ':',
EQUALS = '+',
LEFTBRACKET = '{',
BACKSLASH = '|',
RIGHTBRACKET = '}',
GRAVE = '~',
TAB = '\t',
}
if not fs.exists('/credentials.txt') then
local attemptLogin
local function attemptLogin(failed, message, nameMessage, passwordMessage)
local name = ''
local password = ''
local nameCursor = 0
local passwordCursor = 0
local nameVisualWindow = 0
local passwordVisualWindow = 0
local shiftDown = false
local capsLock = false
local numLock = false
local function clampCursor(entry)
if entry == 'name' then
nameCursor = math.max(nameCursor, 0)
nameCursor = math.min(nameCursor, #name)
if nameCursor > nameVisualWindow + #whiteSpace then
nameVisualWindow = nameVisualWindow +1
elseif nameCursor < nameVisualWindow then
nameVisualWindow = nameVisualWindow -1
end
term.setCursorPos(math.max(math.min(nameCursor, #whiteSpace - 1), 0) + 2, 6)
elseif entry == 'password' then
passwordCursor = math.max(passwordCursor, 0)
passwordCursor = math.min(passwordCursor, #password)
if passwordCursor > passwordVisualWindow + #whiteSpace then
passwordVisualWindow = passwordVisualWindow +1
elseif nameCursor < passwordVisualWindow then
passwordVisualWindow = passwordVisualWindow -1
end
term.setCursorPos(math.max(math.min(passwordCursor, #whiteSpace - 1), 0) + 2, 9)
end
end
local function inputText(text, input)
if input == 'name' then
local left = string.sub(name, 1, nameCursor)
local right = string.sub(name, nameCursor + 1, #name)
name = left .. text .. right
nameCursor = nameCursor + #text
term.setCursorPos(2, 6)
term.write(string.sub(name, nameVisualWindow, nameVisualWindow + #whiteSpace - 1))
elseif input == 'password' then
local left = string.sub(password, 1, passwordCursor)
local right = string.sub(password, passwordCursor + 1, #password)
password = left .. text .. right
passwordCursor = passwordCursor + #text
term.setCursorPos(2, 9)
term.write(string.rep('*', math.min(#password, #whiteSpace - 1)))
end
clampCursor(input)
end
term.setCursorPos(math.floor(monWidth / 2) - 6, 2)
term.setTextColor(colors.white)
term.write('please login')
term.setCursorBlink(true)
term.setCursorPos(2, 5)
if failed then
term.setTextColor(colors.red)
end
term.write(nameMessage and 'email/phone number - ' .. nameMessage or 'email/phone number')
term.setCursorPos(2, 6)
term.setBackgroundColor(colors.gray)
term.write(whiteSpace)
term.setCursorPos(2, 8)
term.setBackgroundColor(colors.black)
term.write(passwordMessage and 'password - ' .. passwordMessage or 'password')
term.setTextColor(colors.white)
term.setCursorPos(2, 9)
term.setBackgroundColor(colors.gray)
term.write(whiteSpace)
if message then
term.setCursorPos((monWidth / 2) - (#message / 2), ((monHeight - 9) / 2) + 9)
term.write(message)
end
term.setCursorPos(monWidth - 7, monHeight - 1)
term.write(' login ')
local selected = 'name'
term.setCursorPos(2, 6)
gui:draw()
repeat
local eventData = {gui:handleEvents(os.pullEvent())}
local event = eventData[1]
if name ~= '' and password ~= '' and not gui.buttonList.login then
button('login', monWidth - 7, monHeight - 1, 7, 1)
gui:draw()
end
if event == 'mouse_click' and eventData[2] == 1 and eventData[3] >= 2 and eventData[3] <= monWidth - 1 then
if eventData[4] == 6 then
term.setCursorPos(math.min(nameCursor, #whiteSpace) + 2, 6)
selected = 'name'
elseif eventData[4] == 9 then
term.setCursorPos(math.min(passwordCursor, #whiteSpace) + 2, 9)
selected = 'cursor'
end
elseif event == 'key' then
local key = keys.getName(eventData[2])
if key == 'left' then
nameCursor = nameCursor -1
clampCursor('name')
term.setCursorPos(math.min(nameCursor, #whiteSpace) + 2, 6)
elseif key == 'right' then
nameCursor = nameCursor +1
clampCursor('name')
term.setCursorPos(math.min(nameCursor, #whiteSpace) + 2, 6)
elseif key == 'enter' then
if selected == 'password' then
ready = true
end
selected = 'password'
term.setCursorPos(math.min(passwordCursor, #whiteSpace) + 2, 9)
elseif key == 'backspace' then
if selected == 'name' then
local left = string.sub(name, 1, nameCursor - 1)
local right = string.sub(name, nameCursor + 1, #name)
name = left .. right
nameCursor = nameCursor -1
term.setCursorPos(2, 6)
term.write(string.sub(name, nameVisualWindow, nameVisualWindow + #whiteSpace - 1))
if #name < #whiteSpace then
term.write(' ')
end
elseif selected == 'password' then
local left = string.sub(password, 1, passwordCursor - 1)
local right = string.sub(password, passwordCursor + 1, #password)
password = left .. right
passwordCursor = passwordCursor -1
term.setCursorPos(2, 9)
term.write(string.rep('*', math.min(#password, #whiteSpace - 1)))
if #password < #whiteSpace then
term.write(' ')
end
end
clampCursor(selected)
end
elseif event == 'char' then
inputText(eventData[2], selected)
elseif event == 'button_click' then
gui:flash(eventData[2])
if eventData[2] == 'login' then
ready = true
end
elseif event == 'paste' then
inputText(eventData[2], selected)
end
until ready
ready = false
term.setBackgroundColor(colors.black)
term.setCursorBlink(false)
term.clear()
gui:remove('login')
local reqData = JSON:encode({
login = name,
password = password,
undelete = false
})
local reqHeaders = {
['Content-Type'] = 'application/json'
}
local request = http.post(client.createApiUrl('/auth/login', 9), reqData, reqHeaders)
if not request then
attemptLogin(true, 'an unkown error accured', 'might be wrong', 'might be wrong')
return
end
local res = JSON:decode(request.readAll())
if request.getResponseCode() == 400 then
attemptLogin(true, nil, res.errors.login[0].message, res.errors.password[0].message)
else
token = res.token
end
local loginFile = fs.open('/credentials.txt', 'w')
loginFile.write(token)
loginFile.close()
end
attemptLogin()
else
local loginFile = fs.open('/credentials.txt', 'r')
token = loginFile.readAll()
loginFile.close()
end
function guiInteractions()
while true do
local eventData = {gui:handleEvents(os.pullEvent())}
local event = eventData[1]
if event ~= nil then
messageRendering(event, eventData)
end
end
end
local function doClient()
local intents = client.createIntents({
'GUILD_MESSAGES',
'DIRECT_MESSAGES',
'MESSAGE_CONTENT'
})
client.connectDiscord(token, 8189, function(e, data)
client.token = token
if e == 'READY' then
messageRendering(e, {
gui = gui,
buttonCreator = button,
client = client,
discordData = data
})
else
messageRendering(e, data)
end
end)
end
parallel.waitForAll(guiInteractions, doClient)