-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkarelDial.lua
441 lines (391 loc) · 10 KB
/
karelDial.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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
-- variables
local running = true
local tArgs = {...}
local configWL
local configGL
local configTG
local version = "beta 1.543"
-- functions
print("Karel Dial V"..tostring(versionCode))
function isInit()
return sg.isInitiator() == "true"
end
function glasses()
tGlasses.clear()
box1 = tGlasses.addBox(20, 10, 200, 65,0x48db18,0.7)
box1.setZIndex(1)
text1 = tGlasses.addText(26,13,"this is karelDial V: "..version,0xFFFFF)
text1.setZIndex(2)
text2 = tGlasses.addText(26,57,"type: $$commands for all commands",0xFFFFF)
text2.setZIndex(2)
end
function resetConfig()
config = fs.open("KarelDialDir/config", "w")
config.writeLine('{')
config.writeLine('["whitelist"] = '..tostring(configWL)..",")
config.writeLine('["greylist"] = '..tostring(configGL)..",")
config.writeLine('["stargate"] = '.."'"..configStargate.."',")
config.writeLine('["terminalGlasses"] = '..tostring(configTG)..",")
config.writeLine('["TGB"] = '.."'"..configTGlasses.."',")
config.writeLine('}')
config.close()
end
function loadConfig()
readConfig = fs.open("KarelDialDir/config", "r")
configFile = readConfig.readAll()
configRead = textutils.unserialize(configFile)
readConfig.close()
end
function writeWhitelist(str)
whitelist = fs.open("KarelDialDir/whitelist", "r")
readWhitelist = whitelist.readAll()
whitelist.close()
tableWhitelist = textutils.unserialize(readWhitelist)
table.insert(tableWhitelist, tostring(str))
whitelist = fs.open("KarelDialDir/whitelist", "w")
whitelist.writeLine('{')
for i = 1, #tableWhitelist do
whitelist.writeLine('"'..tableWhitelist[i]..'",')
end
whitelist.writeLine('}')
whitelist.close()
loadWhitelist()
end
function loadWhitelist()
readWhitelist2 = fs.open("KarelDialDir/whitelist", "r")
WhitelistFile = readWhitelist2.readAll()
whitelistRead = textutils.unserialize(WhitelistFile)
readWhitelist2.close()
end
function writeGreylist(str)
greylist = fs.open("KarelDialDir/greylist", "r")
readGreylist = greylist.readAll()
greylist.close()
tableGreylist = textutils.unserialize(readGreylist)
table.insert(tableGreylist, tostring(str))
greylist = fs.open("KarelDialDir/greylist", "w")
greylist.writeLine('{')
for i = 1, #tableGreylist do
greylist.writeLine('"'..tableGreylist[i]..'",')
end
greylist.writeLine('}')
greylist.close()
loadGreylist()
end
function loadGreylist()
readGreylist2 = fs.open("KarelDialDir/greylist", "r")
GreylistFile = readGreylist2.readAll()
greylistRead = textutils.unserialize(GreylistFile)
readGreylist2.close()
end
function startLock()
function lock()
while true do
sleep(.5)
sg.disconnect()
end
end
function eventPKey()
os.pullEvent("key")
end
parallel.waitForAny(lock,eventPKey)
end
-- code ------------------------------------------------
term.setCursorPos(1,1)
term.clear()
if fs.exists("KarelDialDir") == false then
webV = http.get("https://raw.github.com/karelmikie3/karelDial/master/version")
versionCode = webV.readAll()
configTGlasses = 'first'
fs.makeDir("KarelDialDir")
print("created directory 'KarelDialDir'")
fs.makeDir("KarelDialDir/temp")
y = fs.open("KarelDialDir/greylist", "w")
y.writeLine('{')
y.writeLine('}')
print("created greylist")
y.close()
configGL = false
z = fs.open("KarelDialDir/whitelist", "w")
z.writeLine('{')
z.writeLine('}')
z.close()
configWL = false
print("created whitelist")
t = fs.open("KarelDialDir/config", "w")
t.close()
print("created config")
print("enter the side or name from the stargate: ")
configStargate = tostring(read())
print("")
resetConfig()
elseif fs.exists("KarelDialDir") then
print("loaded directory")
if fs.exists("KarelDialDir/greylist") == false then
y = fs.open("KarelDialDir/greylist", "w")
y.writeLine('{')
y.writeLine('}')
print("created greylist")
y.close()
configGL = false
end
if fs.exists("KarelDialDir/whitelist") == false then
z = fs.open("KarelDialDir/whitelist", "w")
z.writeLine('{')
z.writeLine('}')
print("created whitelist")
z.close()
configWL = false
end
if fs.exists("KarelDialDir/config") == false then
configTGlasses = 'first'
t = fs.open("KarelDialDir/config", "w")
t.close()
resetConfig()
print("created config")
print("enter the side or name from the stargate: ")
configStargate = tostring(read())
end
end
loadConfig()
configWL = configRead.whitelist
configGL = configRead.greylist
configTG = configRead.terminalGlasses
configStargate = configRead.stargate
configTGlasses = configRead.TGB
loadWhitelist()
loadGreylist()
resetConfig()
loadConfig()
fs.makeDir("KarelDialDir/temp")
response = http.get("https://raw.github.com/karelmikie3/karelDial/master/updater")
updater = fs.open("KarelDialDir/updater", "w")
rResponse = response.readAll()
updater.write(rResponse)
updater.close()
response2 = http.get("https://raw.github.com/karelmikie3/karelDial/master/version")
versionNew = response2.readAll()
if tostring(versionNew) ~= version then
print("switching to update")
sleep(3)
shell.run("KarelDialDir/updater")
return
end
if configTG == nil or configRead.TGB == nil then
write("do you wanna enable terminal glasses: ")
answer = read()
if answer == "yes" then
configTG = true
print("at what side or on what address is the TGB?")
configTGlasses = read()
else
configTG = false
end
elseif config ~= nil then
if configTG == true then
print("")
print("terminal glasses are enabled")
elseif configTG == false then
print("")
print("terminal glasses are disabled")
end
end
resetConfig()
loadConfig()
print("")
print("")
if peripheral.wrap(tostring(configRead.stargate)) then
sg = peripheral.wrap(tostring(configRead.stargate))
print("stargate attached")
else
error("could not wrap stargate")
end
if configTG == true then
if peripheral.wrap(tostring(configRead.TGB)) then
tGlasses = peripheral.wrap(tostring(configRead.TGB))
print("terminal glasses bridge attached")
else
error("could not wrap terminal glasses bridge")
end
end
if configRead.whitelist then
print("only people that are on your whitelist are allowed to dial you")
elseif configRead.greylist then
print("only people that are not on your greylist are allowed to dial you")
else
print("everybody is allowed to dial you")
end
function func1()
if tArgs[1] == "lock" then
print("press a key to allow the computer and the portal")
startLock()
end
while running do
sleep(.1)
print("options of main menu: 'dial', 'options', 'close', 'lock', 'leave'")
input1 = read()
if input1 == "dial" then
write("enter an address to dial: ")
dialAddress = string.upper(read())
return1, errorP = pcall(sg.connect,dialAddress)
if return1 == false then
print(errorP)
end
elseif input1 == "close" then
sg.disconnect()
elseif input1 == "lock" then
function lock()
while running do
sleep(.5)
sg.disconnect()
end
end
function eventPKey()
os.pullEvent("key")
end
parallel.waitForAny(lock,eventPKey)
elseif input1 == "leave" then
term.clear()
term.setCursorPos(1,1)
running = false
break
elseif input1 == "options" then
print("")
print("options of options menu: 'whitelist', greylist")
input2 = read()
if input2 == "whitelist" then
print("options of the whitelist menu: 'add', 'edit'")
input3 = read()
if input3 == "add" then
write("person (code) you wanna add: ")
person = read()
writeWhitelist(person)
print(person.." is added")
elseif input3 == "edit" then
shell.run("edit", "KarelDialDir/whitelist")
end
elseif input2 == "greylist" then
print("options of the whitelist menu: 'add', 'edit'")
input3 = read
if input3 == "add" then
write("person (code) you wanna add: ")
person = read()
writeWhitelist(person)
print(person.." is added")
elseif input3 == "edit" then
shell.run("edit", "KarelDialDir/greylist")
end
end
end
end
end
if configRead.whitelist or configRead.greylist then
function func2()
if configRead.whitelist then
while running do
sleep(.6)
if sg.getDialledAddress() ~= "" then
allowed = false
for i = 1,#whitelistRead do
if sg.getDialledAddress() == whitelistRead[i] then
allowed = true
end
end
end
if allowed == false and isInit() == false then
sg.disconnect()
end
end
elseif configRead.greylist then
while running do
sleep(.6)
if sg.getDialledAddress() ~= "" then
notAllowed = true
for i = 1,#whitelistRead do
if sg.getDialledAddress() ~= greylistRead[i] then
notAllowed = false
else
notAllowed = true
break
end
end
end
if notAllowed == true and isInit() == false then
sg.disconnect()
end
end
end
end
else
-- ignore this, this is just for letting things behave normal
function func2()
iets = 342
os.pullEvent("redstone")
end
end
function func2Use()
while running do
sleep(.1)
func2()
end
end
if configRead.terminalGlasses then
function func3()
glasses()
while running do
sleep(.1)
event, msg, player = os.pullEvent("chat_command")
local argsChat = {}
for match in string.gmatch(msg,"[^ ]+") do
table.insert(argsChat,match)
end
if argsChat[1] == "commands" then
text3 = tGlasses.addText(30,24,"commands: dial, disconnect, hide, show",0x4000A3)
text3.setZIndex(2)
sleep(3)
text3.delete()
elseif argsChat[1] == "dial" then
termAddress = string.upper(tostring(argsChat[2]))
return1,errorP = pcall(sg.connect,termAddress)
if return1 == false then
text3 = tGlasses.addText(1,85,"ERROR: "..errorP,0xFF0600)
text3.setScale(1.5)
text3.setZIndex(2)
sleep(3)
text3.delete()
end
elseif argsChat[1] == "disconnect" then
sg.disconnect()
elseif argsChat[1] == "hide" then
tGlasses.clear()
elseif argsChat[1] == "show" then
glasses()
end
end
end
else
function func3()
iets = 342
os.pullEvent("redstone")
end
end
function func4()
iets = 342
os.pullEvent("redstone")
end
function funcpar1()
while running do
parallel.waitForAny(func1, func2)
sleep(.6)
end
end
function funcpar2()
while running do
parallel.waitForAny(func3, func4)
sleep(.6)
end
end
while running do
sleep(.1)
parallel.waitForAny(funcpar1, funcpar2)
end