Advertisement
goldminer127

TShell

Sep 6th, 2021 (edited)
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 22.86 KB | None | 0 0
  1. --Shell Info
  2. Version = "BETA 0.1.8"
  3.  
  4. --Available Modules
  5. local AvailableModules = {"attacking", "farming", "mining", "timber"}
  6.  
  7. --General Functions
  8.  
  9. --Splits string with specified character
  10. function Split(string1, char1)
  11.     local stringInsert = ""
  12.     local splitString = {}
  13.     for char in string1:gmatch"." do
  14.         if char == char1 then
  15.             table.insert(splitString, stringInsert)
  16.             stringInsert = ""
  17.         else
  18.             stringInsert = stringInsert .. char
  19.         end
  20.     end
  21.     table.insert(splitString, stringInsert)
  22.     return splitString
  23. end
  24.  
  25. --Formats messages to be 30 chars long.
  26. --Adds spaces between messages
  27. --Returns string added with spaces
  28. function Format(string1, string2, numSpaces)
  29.     local spaces = ""
  30.     if #string1 + #string2 < numSpaces then
  31.         local neededSpaces = numSpaces - (#string1 + #string2)
  32.         for addSpaces = 1, neededSpaces do
  33.             spaces = spaces .. " "
  34.         end
  35.     end
  36.     return string1 .. spaces .. string2
  37. end
  38.  
  39. --Shell Functions
  40.  
  41. --Restarts the OS **NOT THE TURTLE**
  42. function Restart()
  43.     shell.run("clear")
  44.     shell.run("TShell")
  45. end
  46.  
  47. --Check if Shell Version file is updated.
  48. function CheckForUpdate()
  49.     print("Checking for updates...")
  50.     local download = http.get("https://raw.githubusercontent.com/goldminer127/TShell/master/Version")
  51.     local versionnum = download.readAll()
  52.     local isupToDate = versionnum == Version
  53.     local versionfile = fs.open("ShellVersion","w")
  54.     versionfile.write(versionnum)
  55.     versionfile.close()
  56.     download.close()
  57.     --Returns true if there is no update. Returns false if there is an update
  58.     return isupToDate
  59. end
  60.  
  61. --Store initial version or store new version after update
  62. function UpdateVersion()
  63.     local versionFile = fs.open("ShellVersion","w")
  64.     versionFile.write(Version)
  65.     versionFile.close()
  66.     --pcall(osVersion.close())
  67. end
  68.  
  69. --Make directories for OS
  70. function MakeDirectories()
  71.     fs.makeDir("modules")
  72.     fs.makeDir("programs")
  73. end
  74.  
  75. --Update TShell
  76. function UpdateShell()
  77.     print("Downloading files...")
  78.     local shellDownload = http.get("https://raw.githubusercontent.com/goldminer127/TShell/master/TShell.lua")
  79.     local downloadFile = shellDownload.readAll()
  80.     shellDownload.close()
  81.  
  82.     --Install new version
  83.     print("Installing files...")
  84.     local turtleShell = fs.open("TShell","w")
  85.     turtleShell.write(downloadFile)
  86.     turtleShell.close()
  87.     print("Finalizing...")
  88.     sleep(5)
  89. end
  90.  
  91. --Commands for TShell
  92. function TurtleHelp()
  93.     print(Format("default [-d]", "modules [-m]", 36))
  94.     print(Format("exit", "reinstall [-r]", 36))
  95.     print(Format("help", "remove [rm]", 36))
  96.     print(Format("info", "restart", 36))
  97.     print(Format("install [-i]", "update [-u]", 36))
  98. end
  99.  
  100. --List all modules
  101. function ListModules()
  102.     local modules = fs.list("modules")
  103.     local list = "Modules:\n"
  104.     for _, m in ipairs(modules) do
  105.         list = list .. m .. "\n"
  106.     end
  107.     if list == "" then
  108.         list = "No modules installed."
  109.     end
  110.     print(list)
  111. end
  112.  
  113. --List all programs
  114. function ListPrograms()
  115.     local programs = fs.list("programs")
  116.     local list = "Programs:\n"
  117.     for _, p in ipairs(programs) do
  118.         list = list .. p .. "\n"
  119.     end
  120.     if list == "" then
  121.         list = "No programs installed."
  122.     end
  123.     print(list)
  124. end
  125.  
  126. --Installs modules
  127. function InstallModule(downloadLink, modulename)
  128.     print("Downloading", modulename, "module...")
  129.     local download = http.get(downloadLink)
  130.     local moduleCode = download.readAll()
  131.     download.close()
  132.     print("Download complete.\nInstalling module...")
  133.     local moduleFile = fs.open("modules/" .. modulename, "w")
  134.     moduleFile.write(moduleCode)
  135.     moduleFile.close()
  136.     print("Module successfully installed.")
  137. end
  138.  
  139. --Checks if module exists
  140. function CheckIfModuleExists(module)
  141.     local moduleFile = fs.open("modules/" .. module, "r")
  142.     if moduleFile ~= nil then
  143.         UpdateModule(module)
  144.         moduleFile.close()
  145.     else
  146.         print("Module not found. Use turtle -m list to view all modules.")
  147.     end
  148. end
  149.  
  150. --Update module
  151. function UpdateModule(moduleName)
  152.     local module = require("modules/" .. moduleName)
  153.     local versiondownload = http.get("https://raw.githubusercontent.com/goldminer127/TShell/master/modules/" .. moduleName .. "version")
  154.     local version = versiondownload.readAll()
  155.     if version == module.GetVersion() then
  156.         print("Module up to date. No changes made.")
  157.     else
  158.         print(moduleName,"version",version,"is available. A restart will be required. Do you want to update this module? (y/n)")
  159.         local loop = true
  160.         while loop do
  161.             local response = read()
  162.             if response == "y" then
  163.                 local moduleFile = fs.open("modules/" .. moduleName, "w")
  164.                 local download = http.get("https://raw.githubusercontent.com/goldminer127/TShell/master/modules/" .. moduleName .. ".lua")
  165.                 moduleFile.write(download.readAll())
  166.                 download.close()
  167.                 moduleFile.close()
  168.                 print("Update successful.")
  169.                 print("Restarting...")
  170.                 Restart()
  171.             elseif response == "n" then
  172.                 print("Update postponed.")
  173.                 loop = false
  174.             else
  175.                 print("Invalid response. Try again.")
  176.             end
  177.         end
  178.     end
  179.     versiondownload.close()
  180. end
  181.  
  182. --Removes specified module. Returns result as status message.
  183. function RemoveModule(module)
  184.     local modules = fs.list("modules")
  185.     local result = ""
  186.     for _, m in ipairs(modules) do
  187.         if m == module then
  188.             fs.delete("modules/" .. m)
  189.             result = "Module " .. m .. " successfully uninstalled."
  190.             break
  191.         end
  192.     end
  193.     if result == "" then
  194.         result = "Module " .. module .. " not found."
  195.     end
  196.     print(result)
  197. end
  198.  
  199. function InstallProgram(program, token)
  200.         print("Downloading program", program .. "...")
  201.         local download;
  202.         local doinstall = true;
  203.         if #token == 8 then
  204.             download = http.get("https://pastebin.com/raw/" .. token)
  205.         elseif string.match(token, "/blob") and string.match(token, "github.com") then
  206.             local gfront, gback = string.find(token, "github.com")
  207.             local bfront, bback = string.find(token, "/blob")
  208.             local str1 = string.sub(token, 0, gfront - 1)
  209.             local str2 = string.sub(token, gback + 1, bfront - 1)
  210.             local str3 = string.sub(token, bback + 1, #token)
  211.             token = str1 .. "raw.githubusercontent.com" .. str2 .. str3
  212.             download = http.get(token)
  213.         else
  214.             download = http.get(token)
  215.         end
  216.  
  217.         --Test if program exists
  218.         if fs.open("programs/" .. program, "r") ~= nil then
  219.             print("Program",program,"already exists, do you want to reinstall it? (y/n)")
  220.             while true do
  221.                 local response = read()
  222.                 if response == "y" then
  223.                     --Keep doinstall true
  224.                     break
  225.                 elseif response == "n" then
  226.                     doinstall = false
  227.                     break
  228.                 else
  229.                     print("Invalid response. Try again.")
  230.                 end
  231.             end
  232.         end
  233.                
  234.         if download ~= nil and doinstall == true then
  235.             print("Download complete. Installing program...")
  236.             local file = fs.open("programs/" .. program, "w")
  237.             file.write(download.readAll())
  238.             file.close()
  239.             if token ~= nil then
  240.                 print("Saving token...")
  241.                 local linkArchive = fs.open("programs/" .. program .. "Token", "w")
  242.                 linkArchive.write(token)
  243.                 linkArchive.close()
  244.                 download.close()
  245.             end
  246.             print("Program installed successfully.")
  247.         elseif doinstall == false then
  248.             print("Installation cancelled")
  249.         else
  250.             print(program, "could not be downloaded.")
  251.         end
  252. end
  253.  
  254. function CheckIfProgramExists(program)
  255.     local programFile = fs.open("programs/" .. program, "r")
  256.     if programFile ~= nil then
  257.         UpdateProgram(program)
  258.         programFile.close()
  259.     else
  260.         print("Program not found. Use turtle -m list to view all program.")
  261.     end
  262. end
  263.  
  264. --Update non-module programs
  265. function UpdateProgram(program)
  266.     local tokenfile = fs.open("programs/" .. program .. "Token", "r")
  267.     local token
  268.     print("Could not check", program, "version. Updating program...")
  269.     if tokenfile == nil then
  270.         print("Program link/code not found, please input link/code. Type \"cancel\" to cancel update.")
  271.         local response = read()
  272.         if response == "cancel" then
  273.             print("Canceling update...")
  274.         else
  275.             tokenfile = fs.open("programs/" .. program .. "Token", "w")
  276.             token = response
  277.             tokenfile.write(token)
  278.             tokenfile.close()
  279.         end
  280.     else
  281.         token = tokenfile.readAll()
  282.         tokenfile.close()
  283.     end
  284.     if token ~= nil then
  285.         local programFile = fs.open("programs/" .. program, "w")
  286.         local download = http.get(token)
  287.         if download ~= nil then
  288.             programFile.write(download.readAll())
  289.             download.close()
  290.             programFile.close()
  291.             print("Update successful.")
  292.         else
  293.             print("Update failed, could not download program files. Possibly provided with invalid link/code.")
  294.         end
  295.     end
  296. end
  297.  
  298. --Turtle Commands
  299.  
  300. function Install(input)
  301.     local exists = false
  302.     --Install farming module
  303.     if input[2] == "-m" then
  304.         if input[3] ~= nil then
  305.             --Test if module exists
  306.             for module, name in pairs(AvailableModules) do
  307.                 if input[3] == name then
  308.                     exists = true
  309.                     break
  310.                 end
  311.             end
  312.         else
  313.             print("Must provide a module name.")
  314.         end
  315.         --Installs module if exists, if not tells user that shell compatable module does not exist
  316.         --provides an alternative command to install non-compatable programs.
  317.         if exists then
  318.             local fileExists = fs.open("modules/" .. input[3], "r")
  319.             local response
  320.             if fileExists ~= nil then
  321.                 print(input[3],"module already exists, do you want to reinstall it? (y/n)")
  322.                 response = read()
  323.                 fileExists.close()
  324.             end
  325.             if (fileExists == nil) or (response == "y") then
  326.                 print("Fetching " .. input[3] .. " module...")
  327.                 --loop until valid input
  328.                 while true do
  329.                     print("Confirm installation (y/n)")
  330.                     response = read()
  331.                     if response == "y" then
  332.                         InstallModule("https://raw.githubusercontent.com/goldminer127/TShell/master/modules/" .. input[3] .. ".lua",input[3])
  333.                         break
  334.                     elseif response == "n" then
  335.                         print("Installation cancelled.")
  336.                         break
  337.                     else
  338.                         print("Invalid response. Try again.")
  339.                     end
  340.                 end
  341.             else
  342.                 print("Installation cancelled.")
  343.             end
  344.         else
  345.             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.")
  346.         end
  347.     --Install unsupported programs. Requires a link or code and name of file.
  348.     elseif input[2] == "unsafe" then
  349.         if (input[3] == nil) or (input[4] == nil) then
  350.             print("Must provide a link/code and file name.")
  351.         else
  352.             while true do
  353.                 print(input[4], "is not a supported module. This program might not be accessible by TShell.")
  354.                 print("Confirm installation (y/n)")
  355.                 local response = read()
  356.                 if response == "y" then
  357.                     InstallProgram(input[4], input[3])
  358.                     break
  359.                 elseif response == "n" then
  360.                     print("Installation cancelled.")
  361.                     break
  362.                 else
  363.                     print("Invalid response. Try again.")
  364.                 end
  365.             end
  366.         end
  367.     elseif input[2] ~= nil then
  368.         print(input[2], "is not a command.")
  369.     else
  370.         print("No command input.")
  371.     end
  372. end
  373.  
  374. --Turtle commands
  375. function Help(input)
  376.     if input[2] == nil then
  377.         TurtleHelp()
  378.     elseif (input[2] == "default") or (input[2] == "-d") then
  379.         print("Alias: 'default', '-d'")
  380.         print("Run a default turtle program.")
  381.         print("Example: 'turtle default tunnel 10'")
  382.     elseif input[2] == "exit" then
  383.         print("Alias: 'exit'")
  384.         print("Exit TShell.")
  385.     elseif input[2] == "help" then
  386.         print("Alias: 'help'")
  387.         print("List available commands or show info about a specific command")
  388.     elseif input[2] == "info" then
  389.         print("Alias: 'info'")
  390.         print("Get information about TShell")
  391.     elseif (input[2] == "install") or (input[2] == "-i") then
  392.         print("Alias: 'install', '-i'")
  393.         print("Install a module or third party program.")
  394.     elseif (input[2] == "modules") or (input[2] == "-m") then
  395.         print("Alias: 'modules', '-m'")
  396.         print("Subcommands: '-u', '-l'")
  397.         print("Install a module or third party program.")
  398.         print("-u update a specified module.")
  399.         print("-l list all available modules.")
  400.     elseif (input[2] == "reinstall") or (input[2] == "-r") then
  401.         print("Alias: 'reinstall', '-r'")
  402.         print("Subcommands: '-m', '-p'")
  403.         print("-m reinstall a module.")
  404.         print("-p reinstall a program.")
  405.         print("Reinstall a module or program.")
  406.     elseif (input[2] == "remove") or (input[2] == "rm") then
  407.         print("Alias: 'remove', 'rm'")
  408.         print("Remove modules or third party programs.")
  409.     elseif input[2] == "restart" then
  410.         print("Alias: 'restart'")
  411.         print("Restarts TShell.")
  412.     elseif input[2] == "update" then
  413.         print("Alias: 'update'")
  414.         print("Updates TShell if there is a new update available.")
  415.     else
  416.         print("Command not found.")
  417.     end
  418. end
  419.  
  420. function ModulesCommands(input)
  421.     if input[2] == "list" then
  422.         ListModules()
  423.         ListPrograms()
  424.     elseif input[2] == "-rm" then
  425.         if input[3] ~= nil then
  426.             RemoveModule(input[3])
  427.         else
  428.             print("Module or program name required.")
  429.         end
  430.     elseif input[2] == "update" then
  431.         if input[3] == "module" then
  432.             CheckIfModuleExists(input[4])
  433.         elseif input[3] == "program" then
  434.             CheckIfProgramExists(input[4])
  435.         else
  436.             print("Specify if module or program")
  437.         end
  438.     elseif input[2] == "available" then
  439.         local names = ""
  440.         for _, name in pairs(AvailableModules) do
  441.             names = names .. name .. "\n"
  442.         end
  443.         print("Available modules to install:")
  444.         print(names)
  445.     elseif input[2] ~= nil then
  446.         print(input[2], "is not a command.")
  447.     else
  448.         print("No command input.")
  449.     end
  450. end
  451.  
  452. function Update()
  453.     --True = no update, False = new update available
  454.     if CheckForUpdate() == true then
  455.         print("Shell is up to date")
  456.     else
  457.         local newVersionFile = fs.open("ShellVersion","r")
  458.         local newVersionNum = newVersionFile.readAll()
  459.         newVersionFile.close()
  460.         print("TShell", newVersionNum, "is available.\nUpdate will require a restart.\nReady to update? (y/n)")
  461.         while(true) do
  462.             local response = read()
  463.             if response == "y" then
  464.                 UpdateShell()
  465.                 Restart()
  466.                 break
  467.             elseif response == "n" then
  468.                 print("Update postponed")
  469.                 newVersionFile = fs.open("ShellVersion","w")
  470.                 newVersionFile.write(Version)
  471.                 newVersionFile.close()
  472.                 break
  473.             else
  474.                 print("Invalid input")
  475.             end
  476.         end
  477.     end
  478. end
  479.  
  480. --Used to reinstall damaged or outdated modules or programs
  481. function Reinstall(input)
  482.     local module = fs.open(input[3], "w")
  483.     if module ~= nil then
  484.         print(input[3], "does not exist.")
  485.     elseif input[2] == "-m" then
  486.         print("Are you sure you want to reinstall" .. input[2] .. "? (y/n)")
  487.         --Loop until a valid response is given.
  488.         while true do
  489.             local response = read()
  490.             if response == "y" then
  491.                 print("Downloading module...")
  492.                 local download = http.get("https://raw.githubusercontent.com/goldminer127/TShell/master/modules/" .. input[2] .. ".lua")
  493.                 if download ~= nil then
  494.                     print("Installing module...")
  495.                     module.write(download.readAll())
  496.                     download.close()
  497.                     module.close()
  498.                     print("Module successfully reinstalled.")
  499.                     break
  500.                 else --If module fails to download
  501.                     print("Module could not be downloaded.")
  502.                     break
  503.                 end
  504.             elseif response == "n" then
  505.                 print("Reinstall cancelled.")
  506.                 break
  507.             else
  508.                 print("Not a valid input.")
  509.             end
  510.         end
  511.     elseif input[2] == "-p" then
  512.         while true do
  513.             local response = read()
  514.             if response == "y" then
  515.                 local download = http.get("https://raw.githubusercontent.com/goldminer127/TShell/master/modules/" .. input[2] .. ".lua")
  516.                 module.write(download.readAll())
  517.                 download.close()
  518.                 module.close()
  519.                 break
  520.             elseif response == "n" then
  521.                 print("Reinstall cancelled.")
  522.                 break
  523.             else
  524.                 print("Not a valid input.")
  525.             end
  526.         end
  527.     else
  528.         print("Incorrect inputs. Use 'turtle help reinstall' to view specifications.")
  529.     end
  530. end
  531.  
  532. function Remove(input)
  533.     if input[2] == nil or input[3] == nil then
  534.         print("Type and filename must be provided. One was missing.")
  535.     elseif input[2] == "module" then
  536.         if fs.exists("modules/"..input[3]) then
  537.             print("Removing module",input[3])
  538.             fs.delete("modules/"..input[3])
  539.             fs.delete("modules/"..input[3].."version")
  540.         else
  541.             print(input[3],"does not exist.")
  542.         end
  543.     elseif input[2] == "program" then
  544.         if fs.exists("program/"..input[3]) then
  545.             print("Removing program",input[3])
  546.             fs.delete("program/"..input[3])
  547.             fs.delete("program/"..input[3].."version")
  548.         else
  549.             print(input[3],"does not exist.")
  550.         end
  551.     else
  552.         print("Invalid type specified. Must be module or program.")
  553.     end
  554. end
  555.  
  556. --[[
  557.     All TShell commands. Runs the specified command from Listener.
  558.     Returns true if exit is not called.
  559. ]]
  560. function Turtle(input)
  561.     if input[1] == nil then
  562.         print("Use 'turtle help' to view available commands. Use 'turtle help <command> to read about a specific command.")
  563.     --Default programs
  564.     elseif input[1] == "default" or input[1] == "-d" then
  565.         local command = ""
  566.         for x = 2,#input,1 do
  567.             command = command .. " " .. input[x]
  568.         end
  569.         shell.run(command)
  570.     --Exit
  571.     elseif input[1] == "exit" then
  572.         print("Exiting TShell...")
  573.         return false
  574.     --Help
  575.     elseif input[1] == "help" then
  576.         Help(input)
  577.     --Info
  578.     elseif input[1] == "info" then
  579.         print("TShell Version:", Version)
  580.         print("Created by goldminer127")
  581.         print("Github: https://github.com/goldminer127")
  582.     --Install
  583.     elseif (input[1] == "install") or (input[1] == "-i") then
  584.         Install(input)
  585.     --Modules
  586.     elseif (input[1] == "modules") or (input[1] == "-m") then
  587.         ModulesCommands(input)
  588.     --Update
  589.     elseif (input[1] == "update") or (input[1] == "-u") then
  590.         Update()
  591.     --Reinstall
  592.     elseif input[1] == "reinstall" then
  593.         Reinstall(input)
  594.     --remove
  595.     elseif input[1] == "remove" then
  596.         Remove(input)
  597.     --Restart
  598.     elseif input[1] == "restart" then
  599.         Restart()
  600.     --Exit
  601.     elseif input[1] == "exit" then
  602.         print("Closing terminal")
  603.         return false
  604.     elseif input[1] ~= nil then
  605.         print(input[1], "is not a command.")
  606.     else
  607.         print("No command input.")
  608.     end
  609.     --Continues loop if exit command is not used
  610.     return true
  611. end
  612.  
  613.  
  614. --Listens for input from the user. Determins what to do with prefixes from the user
  615. function Listener()
  616.     local loop = true
  617.     while loop do
  618.         term.write("> ")
  619.         local input = Split(read(),' ')
  620.         local prefix = input[1]
  621.         table.remove(input, 1)
  622.         --Default turtle commands
  623.         if prefix == "turtle" then
  624.             loop = Turtle(input)
  625.         --farming module commands
  626.         elseif prefix == "farming" then
  627.             local farmingExists = pcall(require,"modules/farming")
  628.             if farmingExists == false then
  629.                 print("Farming module not installed.\nRun 'turtle -i -m farming' to install the required module.")
  630.             else
  631.                 local farming = require("modules/farming")
  632.                 --farming functions and commands found in farming.lua
  633.                 farming.Interpreter(input)
  634.             end
  635.         else
  636.             print("Invalid syntax")
  637.         end
  638.     end
  639. end
  640.  
  641.  
  642. --Startup stuff
  643.  
  644. function Startup()
  645.     UpdateVersion()
  646.     MakeDirectories()
  647.     --Display arbutrary messages for fun. It indicated nothing
  648.     --Loading messages
  649.     print(Format("Loading Shell", "[ OK ]", 36))
  650.     sleep(0.5)
  651.     print(Format("Loading IP", "[ OK ]", 36))
  652.     sleep(0.10)
  653.     print(Format("Loading emotions", "[ FAILED ]", 36))
  654.     print("Starting systems...\n")
  655.     --Starting messages
  656.     sleep(2)
  657.     print(Format("Starting listener", "[ OK ]", 36))
  658.     sleep(0.25)
  659.     print(Format("Starting modules", "[ OK ]", 36))
  660.     sleep(1)
  661.     print(Format("Starting interface", "[ OK ]", 36))
  662.     sleep(0.25)
  663.     print(Format("Starting other services", "[ OK ]", 36))
  664.     sleep(0.1)
  665.     print(Format("Starting emotions", "[ FAILED ]", 36))
  666.     print("\n\nWelcome to TShell", Version, "Loading...")
  667.     sleep(5)
  668.     shell.run("clear")
  669.     Listener()
  670. end
  671.  
  672. --Main
  673. Startup()
  674. --shell.run("pastebin","get","1F1cR1LH","turtle")
  675. --shell.run("pastebin get 1F1cR1LH turtle")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement