-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTShell.lua
675 lines (638 loc) · 22.2 KB
/
TShell.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
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
--Shell Info
Version = "BETA 0.1.8"
--Available Modules
local AvailableModules = {"attacking", "farming", "mining", "timber"}
--General Functions
--Splits string with specified character
function Split(string1, char1)
local stringInsert = ""
local splitString = {}
for char in string1:gmatch"." do
if char == char1 then
table.insert(splitString, stringInsert)
stringInsert = ""
else
stringInsert = stringInsert .. char
end
end
table.insert(splitString, stringInsert)
return splitString
end
--Formats messages to be 30 chars long.
--Adds spaces between messages
--Returns string added with spaces
function Format(string1, string2, numSpaces)
local spaces = ""
if #string1 + #string2 < numSpaces then
local neededSpaces = numSpaces - (#string1 + #string2)
for addSpaces = 1, neededSpaces do
spaces = spaces .. " "
end
end
return string1 .. spaces .. string2
end
--Shell Functions
--Restarts the OS **NOT THE TURTLE**
function Restart()
shell.run("clear")
shell.run("TShell")
end
--Check if Shell Version file is updated.
function CheckForUpdate()
print("Checking for updates...")
local download = http.get("https://raw.githubusercontent.com/goldminer127/TShell/master/Version")
local versionnum = download.readAll()
local isupToDate = versionnum == Version
local versionfile = fs.open("ShellVersion","w")
versionfile.write(versionnum)
versionfile.close()
download.close()
--Returns true if there is no update. Returns false if there is an update
return isupToDate
end
--Store initial version or store new version after update
function UpdateVersion()
local versionFile = fs.open("ShellVersion","w")
versionFile.write(Version)
versionFile.close()
--pcall(osVersion.close())
end
--Make directories for OS
function MakeDirectories()
fs.makeDir("modules")
fs.makeDir("programs")
end
--Update TShell
function UpdateShell()
print("Downloading files...")
local shellDownload = http.get("https://raw.githubusercontent.com/goldminer127/TShell/master/TShell.lua")
local downloadFile = shellDownload.readAll()
shellDownload.close()
--Install new version
print("Installing files...")
local turtleShell = fs.open("TShell","w")
turtleShell.write(downloadFile)
turtleShell.close()
print("Finalizing...")
sleep(5)
end
--Commands for TShell
function TurtleHelp()
print(Format("default [-d]", "modules [-m]", 36))
print(Format("exit", "reinstall [-r]", 36))
print(Format("help", "remove [rm]", 36))
print(Format("info", "restart", 36))
print(Format("install [-i]", "update [-u]", 36))
end
--List all modules
function ListModules()
local modules = fs.list("modules")
local list = "Modules:\n"
for _, m in ipairs(modules) do
list = list .. m .. "\n"
end
if list == "" then
list = "No modules installed."
end
print(list)
end
--List all programs
function ListPrograms()
local programs = fs.list("programs")
local list = "Programs:\n"
for _, p in ipairs(programs) do
list = list .. p .. "\n"
end
if list == "" then
list = "No programs installed."
end
print(list)
end
--Installs modules
function InstallModule(downloadLink, modulename)
print("Downloading", modulename, "module...")
local download = http.get(downloadLink)
local moduleCode = download.readAll()
download.close()
print("Download complete.\nInstalling module...")
local moduleFile = fs.open("modules/" .. modulename, "w")
moduleFile.write(moduleCode)
moduleFile.close()
print("Module successfully installed.")
end
--Checks if module exists
function CheckIfModuleExists(module)
local moduleFile = fs.open("modules/" .. module, "r")
if moduleFile ~= nil then
UpdateModule(module)
moduleFile.close()
else
print("Module not found. Use turtle -m list to view all modules.")
end
end
--Update module
function UpdateModule(moduleName)
local module = require("modules/" .. moduleName)
local versiondownload = http.get("https://raw.githubusercontent.com/goldminer127/TShell/master/modules/" .. moduleName .. "version")
local version = versiondownload.readAll()
if version == module.GetVersion() then
print("Module up to date. No changes made.")
else
print(moduleName,"version",version,"is available. A restart will be required. Do you want to update this module? (y/n)")
local loop = true
while loop do
local response = read()
if response == "y" then
local moduleFile = fs.open("modules/" .. moduleName, "w")
local download = http.get("https://raw.githubusercontent.com/goldminer127/TShell/master/modules/" .. moduleName .. ".lua")
moduleFile.write(download.readAll())
download.close()
moduleFile.close()
print("Update successful.")
print("Restarting...")
Restart()
elseif response == "n" then
print("Update postponed.")
loop = false
else
print("Invalid response. Try again.")
end
end
end
versiondownload.close()
end
--Removes specified module. Returns result as status message.
function RemoveModule(module)
local modules = fs.list("modules")
local result = ""
for _, m in ipairs(modules) do
if m == module then
fs.delete("modules/" .. m)
result = "Module " .. m .. " successfully uninstalled."
break
end
end
if result == "" then
result = "Module " .. module .. " not found."
end
print(result)
end
function InstallProgram(program, token)
print("Downloading program", program .. "...")
local download;
local doinstall = true;
if #token == 8 then
download = http.get("https://pastebin.com/raw/" .. token)
elseif string.match(token, "/blob") and string.match(token, "github.com") then
local gfront, gback = string.find(token, "github.com")
local bfront, bback = string.find(token, "/blob")
local str1 = string.sub(token, 0, gfront - 1)
local str2 = string.sub(token, gback + 1, bfront - 1)
local str3 = string.sub(token, bback + 1, #token)
token = str1 .. "raw.githubusercontent.com" .. str2 .. str3
download = http.get(token)
else
download = http.get(token)
end
--Test if program exists
if fs.open("programs/" .. program, "r") ~= nil then
print("Program",program,"already exists, do you want to reinstall it? (y/n)")
while true do
local response = read()
if response == "y" then
--Keep doinstall true
break
elseif response == "n" then
doinstall = false
break
else
print("Invalid response. Try again.")
end
end
end
if download ~= nil and doinstall == true then
print("Download complete. Installing program...")
local file = fs.open("programs/" .. program, "w")
file.write(download.readAll())
file.close()
if token ~= nil then
print("Saving token...")
local linkArchive = fs.open("programs/" .. program .. "Token", "w")
linkArchive.write(token)
linkArchive.close()
download.close()
end
print("Program installed successfully.")
elseif doinstall == false then
print("Installation cancelled")
else
print(program, "could not be downloaded.")
end
end
function CheckIfProgramExists(program)
local programFile = fs.open("programs/" .. program, "r")
if programFile ~= nil then
UpdateProgram(program)
programFile.close()
else
print("Program not found. Use turtle -m list to view all program.")
end
end
--Update non-module programs
function UpdateProgram(program)
local tokenfile = fs.open("programs/" .. program .. "Token", "r")
local token
print("Could not check", program, "version. Updating program...")
if tokenfile == nil then
print("Program link/code not found, please input link/code. Type \"cancel\" to cancel update.")
local response = read()
if response == "cancel" then
print("Canceling update...")
else
tokenfile = fs.open("programs/" .. program .. "Token", "w")
token = response
tokenfile.write(token)
tokenfile.close()
end
else
token = tokenfile.readAll()
tokenfile.close()
end
if token ~= nil then
local programFile = fs.open("programs/" .. program, "w")
local download = http.get(token)
if download ~= nil then
programFile.write(download.readAll())
download.close()
programFile.close()
print("Update successful.")
else
print("Update failed, could not download program files. Possibly provided with invalid link/code.")
end
end
end
--Turtle Commands
function Install(input)
local exists = false
--Install farming module
if input[2] == "-m" then
if input[3] ~= nil then
--Test if module exists
for module, name in pairs(AvailableModules) do
if input[3] == name then
exists = true
break
end
end
else
print("Must provide a module name.")
end
--Installs module if exists, if not tells user that shell compatable module does not exist
--provides an alternative command to install non-compatable programs.
if exists then
local fileExists = fs.open("modules/" .. input[3], "r")
local response
if fileExists ~= nil then
print(input[3],"module already exists, do you want to reinstall it? (y/n)")
response = read()
fileExists.close()
end
if (fileExists == nil) or (response == "y") then
print("Fetching " .. input[3] .. " module...")
--loop until valid input
while true do
print("Confirm installation (y/n)")
response = read()
if response == "y" then
InstallModule("https://raw.githubusercontent.com/goldminer127/TShell/master/modules/" .. input[3] .. ".lua",input[3])
break
elseif response == "n" then
print("Installation cancelled.")
break
else
print("Invalid response. Try again.")
end
end
else
print("Installation cancelled.")
end
else
print(input[3] .. " is not listed as a module. Use 'turtle -m available' to view all available modules. Use 'turtle -i unsafe <link/code> <name>' to install programs not compatable with TShell.")
end
--Install unsupported programs. Requires a link or code and name of file.
elseif input[2] == "unsafe" then
if (input[3] == nil) or (input[4] == nil) then
print("Must provide a link/code and file name.")
else
while true do
print(input[4], "is not a supported module. This program might not be accessible by TShell.")
print("Confirm installation (y/n)")
local response = read()
if response == "y" then
InstallProgram(input[4], input[3])
break
elseif response == "n" then
print("Installation cancelled.")
break
else
print("Invalid response. Try again.")
end
end
end
elseif input[2] ~= nil then
print(input[2], "is not a command.")
else
print("No command input.")
end
end
--Turtle commands
function Help(input)
if input[2] == nil then
TurtleHelp()
elseif (input[2] == "default") or (input[2] == "-d") then
print("Alias: 'default', '-d'")
print("Run a default turtle program.")
print("Example: 'turtle default tunnel 10'")
elseif input[2] == "exit" then
print("Alias: 'exit'")
print("Exit TShell.")
elseif input[2] == "help" then
print("Alias: 'help'")
print("List available commands or show info about a specific command")
elseif input[2] == "info" then
print("Alias: 'info'")
print("Get information about TShell")
elseif (input[2] == "install") or (input[2] == "-i") then
print("Alias: 'install', '-i'")
print("Install a module or third party program.")
elseif (input[2] == "modules") or (input[2] == "-m") then
print("Alias: 'modules', '-m'")
print("Subcommands: '-u', '-l'")
print("Install a module or third party program.")
print("-u update a specified module.")
print("-l list all available modules.")
elseif (input[2] == "reinstall") or (input[2] == "-r") then
print("Alias: 'reinstall', '-r'")
print("Subcommands: '-m', '-p'")
print("-m reinstall a module.")
print("-p reinstall a program.")
print("Reinstall a module or program.")
elseif (input[2] == "remove") or (input[2] == "rm") then
print("Alias: 'remove', 'rm'")
print("Remove modules or third party programs.")
elseif input[2] == "restart" then
print("Alias: 'restart'")
print("Restarts TShell.")
elseif input[2] == "update" then
print("Alias: 'update'")
print("Updates TShell if there is a new update available.")
else
print("Command not found.")
end
end
function ModulesCommands(input)
if input[2] == "list" then
ListModules()
ListPrograms()
elseif input[2] == "-rm" then
if input[3] ~= nil then
RemoveModule(input[3])
else
print("Module or program name required.")
end
elseif input[2] == "update" then
if input[3] == "module" then
CheckIfModuleExists(input[4])
elseif input[3] == "program" then
CheckIfProgramExists(input[4])
else
print("Specify if module or program")
end
elseif input[2] == "available" then
local names = ""
for _, name in pairs(AvailableModules) do
names = names .. name .. "\n"
end
print("Available modules to install:")
print(names)
elseif input[2] ~= nil then
print(input[2], "is not a command.")
else
print("No command input.")
end
end
function Update()
--True = no update, False = new update available
if CheckForUpdate() == true then
print("Shell is up to date")
else
local newVersionFile = fs.open("ShellVersion","r")
local newVersionNum = newVersionFile.readAll()
newVersionFile.close()
print("TShell", newVersionNum, "is available.\nUpdate will require a restart.\nReady to update? (y/n)")
while(true) do
local response = read()
if response == "y" then
UpdateShell()
Restart()
break
elseif response == "n" then
print("Update postponed")
newVersionFile = fs.open("ShellVersion","w")
newVersionFile.write(Version)
newVersionFile.close()
break
else
print("Invalid input")
end
end
end
end
--Used to reinstall damaged or outdated modules or programs
function Reinstall(input)
local module = fs.open(input[3], "w")
if module ~= nil then
print(input[3], "does not exist.")
elseif input[2] == "-m" then
print("Are you sure you want to reinstall" .. input[2] .. "? (y/n)")
--Loop until a valid response is given.
while true do
local response = read()
if response == "y" then
print("Downloading module...")
local download = http.get("https://raw.githubusercontent.com/goldminer127/TShell/master/modules/" .. input[2] .. ".lua")
if download ~= nil then
print("Installing module...")
module.write(download.readAll())
download.close()
module.close()
print("Module successfully reinstalled.")
break
else --If module fails to download
print("Module could not be downloaded.")
break
end
elseif response == "n" then
print("Reinstall cancelled.")
break
else
print("Not a valid input.")
end
end
elseif input[2] == "-p" then
while true do
local response = read()
if response == "y" then
local download = http.get("https://raw.githubusercontent.com/goldminer127/TShell/master/modules/" .. input[2] .. ".lua")
module.write(download.readAll())
download.close()
module.close()
break
elseif response == "n" then
print("Reinstall cancelled.")
break
else
print("Not a valid input.")
end
end
else
print("Incorrect inputs. Use 'turtle help reinstall' to view specifications.")
end
end
function Remove(input)
if input[2] == nil or input[3] == nil then
print("Type and filename must be provided. One was missing.")
elseif input[2] == "module" then
if fs.exists("modules/"..input[3]) then
print("Removing module",input[3])
fs.delete("modules/"..input[3])
fs.delete("modules/"..input[3].."version")
else
print(input[3],"does not exist.")
end
elseif input[2] == "program" then
if fs.exists("program/"..input[3]) then
print("Removing program",input[3])
fs.delete("program/"..input[3])
fs.delete("program/"..input[3].."version")
else
print(input[3],"does not exist.")
end
else
print("Invalid type specified. Must be module or program.")
end
end
--[[
All TShell commands. Runs the specified command from Listener.
Returns true if exit is not called.
]]
function Turtle(input)
if input[1] == nil then
print("Use 'turtle help' to view available commands. Use 'turtle help <command> to read about a specific command.")
--Default programs
elseif input[1] == "default" or input[1] == "-d" then
local command = ""
for x = 2,#input,1 do
command = command .. " " .. input[x]
end
shell.run(command)
--Exit
elseif input[1] == "exit" then
print("Exiting TShell...")
return false
--Help
elseif input[1] == "help" then
Help(input)
--Info
elseif input[1] == "info" then
print("TShell Version:", Version)
print("Created by goldminer127")
print("Github: https://github.com/goldminer127")
--Install
elseif (input[1] == "install") or (input[1] == "-i") then
Install(input)
--Modules
elseif (input[1] == "modules") or (input[1] == "-m") then
ModulesCommands(input)
--Update
elseif (input[1] == "update") or (input[1] == "-u") then
Update()
--Reinstall
elseif input[1] == "reinstall" then
Reinstall(input)
--remove
elseif input[1] == "remove" then
Remove(input)
--Restart
elseif input[1] == "restart" then
Restart()
--Exit
elseif input[1] == "exit" then
print("Closing terminal")
return false
elseif input[1] ~= nil then
print(input[1], "is not a command.")
else
print("No command input.")
end
--Continues loop if exit command is not used
return true
end
--Listens for input from the user. Determins what to do with prefixes from the user
function Listener()
local loop = true
while loop do
term.write("> ")
local input = Split(read(),' ')
local prefix = input[1]
table.remove(input, 1)
--Default turtle commands
if prefix == "turtle" then
loop = Turtle(input)
--farming module commands
elseif prefix == "farming" then
local farmingExists = pcall(require,"modules/farming")
if farmingExists == false then
print("Farming module not installed.\nRun 'turtle -i -m farming' to install the required module.")
else
local farming = require("modules/farming")
--farming functions and commands found in farming.lua
farming.Interpreter(input)
end
else
print("Invalid syntax")
end
end
end
--Startup stuff
function Startup()
UpdateVersion()
MakeDirectories()
--Display arbutrary messages for fun. It indicated nothing
--Loading messages
print(Format("Loading Shell", "[ OK ]", 36))
sleep(0.5)
print(Format("Loading IP", "[ OK ]", 36))
sleep(0.10)
print(Format("Loading emotions", "[ FAILED ]", 36))
print("Starting systems...\n")
--Starting messages
sleep(2)
print(Format("Starting listener", "[ OK ]", 36))
sleep(0.25)
print(Format("Starting modules", "[ OK ]", 36))
sleep(1)
print(Format("Starting interface", "[ OK ]", 36))
sleep(0.25)
print(Format("Starting other services", "[ OK ]", 36))
sleep(0.1)
print(Format("Starting emotions", "[ FAILED ]", 36))
print("\n\nWelcome to TShell", Version, "Loading...")
sleep(5)
shell.run("clear")
Listener()
end
--Main
Startup()
--shell.run("pastebin","get","1F1cR1LH","turtle")
--shell.run("pastebin get 1F1cR1LH turtle")