DeliciousJaffa

Turtle program updater

Mar 29th, 2013
1,255
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 9.76 KB | None | 0 0
  1.   --[[
  2.   ------------------------
  3.  
  4.   Originally made by 'Engineer' on the ComputerCraft forums
  5.   http://www.computercraft.info/forums2/index.php?/topic/11484-/
  6.  
  7.   Edited by DeliciousJaffa to fix bugs, add documentation and add a whole load of new functionality
  8.   http://twitter.com/DeliciousJaffa
  9.  
  10.   ------------------------
  11.  
  12.   Current version: 1.0.1.3
  13.  
  14.   Documentation:
  15.   1. Installation
  16.     Run the following commands:
  17.       pastebin get JW4uj2Jw startup
  18.       reboot
  19.  
  20.   2. Usage
  21.     "startup auto list" - This command will list all auto updating programs
  22.     "startup auto add <paste bin code> <program>" - This command will add a program to check for updates from a pastebin page
  23.     "startup auto remove <program>" - This will remove a program from the auto update list by it's name (also known as it's command)
  24.     "startup auto reset" - This will reset the auto update list to the current defaults
  25.    
  26.     "startup set <program>" - This command will set a program to be launcher after update checking when the computer restarts
  27.       (or when the chunk becomes loaded again)
  28.     "startup unset" - This will wipe the startup so that no program is ran automatically.
  29.  
  30.     "startup update" - This command will check if the code at the specified pastebin id
  31.       is different from the named program, if it is, it'll automatically download the update before anything else
  32.  
  33.   3. Preinstalled programs
  34.     This auto-updater installs 2 programs by default.
  35.       - "EpicMine" by Princetommen http://pastebin.com/6qmBBLpz
  36.         Mines out tunnels or an area
  37.       - "Attack" by DeliciousJaffa http://pastebin.com/vNNYrHUp
  38.         Attacks mobs forever and drops items
  39.   ------------------------
  40.   ]]
  41.  
  42. local tArgs = { ... }
  43. local autoFile = "/startup-updatelist"
  44. local prefFile = "/startup-prefs"
  45. local defaultsPbinCode = "B2jBQTU2"
  46.  
  47. function usage()
  48.   print( "Usage:" )
  49.   print( "- startup auto list" )
  50.   print( "- startup auto add <pastebin code> <program>" )
  51.   print( "- startup auto remove <program>" )
  52.   print( "- startup auto reset" )
  53.   print( "- startup set <program>" )
  54.   print( "- startup unset" )
  55.   print( "- startup update" )
  56.   print( "See the documentation for more information")
  57. end
  58.  
  59. function getPastebin( code )
  60.   local response = http.get(
  61.     "http://pastebin.com/raw.php?i=" .. textutils.urlEncode( code )
  62.   )
  63.   if response then
  64.     local contents = response.readAll()
  65.     local file = fs.open( code, "w" )
  66.     file.write( contents )
  67.     file.close()
  68.     return true, contents
  69.   else
  70.     return false
  71.   end
  72. end
  73.  
  74. function setDefaults()
  75.   if fs.exists( autoFile ) then
  76.     fs.delete( autoFile )
  77.   end
  78.   local file = fs.open( autoFile, "w" )
  79.   local condition, raw = getPastebin(defaultsPbinCode)
  80.   fs.delete(defaultsPbinCode)
  81.   local default = textutils.unserialize(raw)
  82.   local tPrograms = {}
  83.   if condition == true then
  84.     tPrograms = default
  85.     file.write( textutils.serialize(tPrograms))
  86.     file.close()
  87.     print("Default autoupdate list installed")
  88.   else
  89.     print("Failed to obtain the default update list")
  90.   end
  91. end
  92.  
  93. function update()
  94.   if fs.exists( autoFile ) then
  95.     local file = fs.open( autoFile, "r" )
  96.     local handler = file.readAll()
  97.     file.close()
  98.     local tPrograms = textutils.unserialize( handler )
  99.     if type( tPrograms ) == "table" then
  100.       for i = 1, #tPrograms do
  101.         local condition, content = getPastebin( tPrograms[i][1] )
  102.         if condition then
  103.           local reader = "needsupdateornot"
  104.           if fs.exists(tPrograms[i][2]) then
  105.             local installed = fs.open(tPrograms[i][2], "r")
  106.             reader = installed.readAll()
  107.             installed.close()
  108.           end
  109.           if reader ~= content then
  110.             fs.delete( tPrograms[i][2] )
  111.             fs.move( tPrograms[i][1], tPrograms[i][2] )
  112.             print( "Updated " .. tPrograms[i][2] .. " succesfully!" )
  113.           else
  114.             print( "No update needed for: "..tPrograms[i][2] )
  115.             fs.delete( tPrograms[i][1] )
  116.           end
  117.         else
  118.           print( "Update for " .. tPrograms[i][2] .. " unsuccesfull!" )
  119.         end
  120.       end
  121.     end
  122.   end
  123.   print("Update check completed")
  124. end
  125.  
  126. if not fs.exists( autoFile ) then
  127.   setDefaults()
  128. end
  129.  
  130. local sCommand = "noCommand"
  131. if #tArgs == 0 then
  132.   update()
  133.   print("See documentation for commands (pastebin page)")
  134.   if turtle then
  135.     if os.getComputerLabel() then
  136.       print(os.getComputerLabel() .. " reporting for duty with " .. turtle.getFuelLevel() .. " fuel")
  137.     else
  138.       print("Reporting for duty with " .. turtle.getFuelLevel() .. " fuel")
  139.     end
  140.   else
  141.     if os.getComputerLabel() then
  142.       print(os.getComputerLabel() .. " reporting for duty!")
  143.     end
  144.   end
  145.   if fs.exists( prefFile ) then
  146.     local file = fs.open( prefFile, "r" )
  147.     local startupname = file.readAll()
  148.     if fs.exists(startupname) then
  149.       print("Launching startup program")
  150.       shell.run(startupname)
  151.     else
  152.       print("Startup program could not be found.")
  153.     end
  154.   end
  155. elseif #tArgs > 4 then
  156.   usage()
  157. else
  158.   if string.lower( tArgs[1] ) == "manual" and tArgs[3] then
  159.     sCommand = "manual"
  160.   elseif string.lower(tArgs[1]) == "auto" then
  161.     if string.lower( tArgs[2] ) == "remove" and tArgs[3] then
  162.       sCommand = "auto_getoff"
  163.     elseif string.lower( tArgs[2] ) == "add" and tArgs[4] then
  164.       sCommand = "auto_add"
  165.     elseif string.lower( tArgs[2] ) == "list" then
  166.       sCommand = "auto_list"
  167.     elseif string.lower( tArgs[2] ) == "reset" then
  168.       setDefaults()
  169.     else
  170.       usage()
  171.     end
  172.   elseif string.lower(tArgs[1]) == "set" then
  173.     sCommand = "setstartup"
  174.   elseif string.lower(tArgs[1]) == "unset" then
  175.     sCommand = "delstartup"
  176.   elseif string.lower(tArgs[1]) == "update" then
  177.     update()
  178.   else
  179.     usage()
  180.   end
  181. end
  182.  
  183. if not http then
  184.   print( "HTTP not enabled" )
  185.   print( "Enable this in the config" )
  186.   error()
  187. end
  188.  
  189. if sCommand == "manual" then
  190.   local sFile = tArgs[3]
  191.   local sCode = tArgs[2]
  192.   if not fs.exists( sFile ) then
  193.     local rAnswer, download
  194.     term.clear()
  195.     term.setCursorPos( 1,1 )
  196.     print( "File doesn't exist!\nStill download " .. sCode .. " to "..sFile.." ? (Y/N)" )
  197.     while true do
  198.       local evt, key = os.pullEvent("char")
  199.       if key == "y" then
  200.         download = true
  201.         break
  202.       elseif key == "n" then
  203.         download = false
  204.         break
  205.       end
  206.     end
  207.     if download then
  208.       if getPastebin( sCode ) then
  209.         fs.move( sCode, sFile )
  210.         print( "Succesfully downloaded " .. sCode .. " as " .. sFile )
  211.       elseif not getPastebin( sCode, sFile ) then
  212.         print( "Could not download " .. sCode .. " from pastebin.com" )
  213.       end
  214.     end
  215.   else
  216.     if getPastebin( sCode ) then
  217.       fs.delete( sFile )
  218.       fs.move( sCode, sFile )
  219.       print( "Succesfully updated " .. sFile )
  220.     elseif not getPastebin( sCode ) then
  221.       print( "Could not update " .. sFile .. " with " .. sCode )
  222.     end
  223.   end
  224. elseif sCommand == "auto_list" then
  225.   if not fs.exists( autoFile ) then
  226.     print( "Nothing is added yet" )
  227.   else
  228.     local file = fs.open( autoFile, "r" )
  229.     local reader = file.readAll()
  230.     file.close()
  231.     local tPrograms = textutils.unserialize( reader )
  232.     term.clear()
  233.     term.setCursorPos( 1,1 )
  234.     print( "Pastebin Code:    Program Name:" )
  235.     if type( tPrograms ) == "table" then
  236.       for i = 1, #tPrograms do
  237.         term.setCursorPos( 1, i + 2 )
  238.         write( tPrograms[i][1] )
  239.         term.setCursorPos( 19, i + 2 )
  240.         write( tPrograms[i][2] )
  241.         term.setCursorPos( 1, i + 3 )
  242.       end
  243.     else
  244.       print( "None              None")
  245.     end
  246.   end
  247. elseif sCommand == "auto_add" then
  248.   local sCode = tArgs[3]
  249.   local sFile = tArgs[4]
  250.   if sFile and getPastebin( sCode ) then
  251.     fs.delete( sCode )
  252.     local file = fs.open( autoFile, "r" )
  253.     local handler = file.readAll()
  254.     file.close()
  255.     local tPrograms = textutils.unserialize( handler )
  256.     if type( tPrograms ) ~= "table" then
  257.       local file = fs.open( autoFile, "w" )
  258.       tPrograms = {}
  259.       file.write( textutils.serialize( tPrograms ) )
  260.       file.close()
  261.     end
  262.     local codeintable = false
  263.     for i = 1, #tPrograms do
  264.       if tPrograms[i][2] == sFile then
  265.         codeintable = true
  266.       end
  267.     end
  268.     if codeintable == false then
  269.       table.insert( tPrograms, { sCode, sFile } )
  270.     end
  271.     fs.delete( autoFile )
  272.     file = fs.open( autoFile, "w" )
  273.     file.write( textutils.serialize( tPrograms ))
  274.     file.close()
  275.     if codeintable == false then
  276.       print("Succesfully added " .. sFile .. " to the update list." )
  277.     else
  278.       print("Please remove " .. sFile .. " from the list before adding a new code for it")
  279.     end
  280.     update()
  281.   elseif not getPastebin( sCode ) then
  282.     print( "Your pastebin code is invalid" )
  283.   end
  284. elseif sCommand == "auto_getoff" then
  285.   local sFile = tArgs[3]
  286.   local file = fs.open( autoFile, "r" )
  287.   local handler = file.readAll()
  288.   file.close()
  289.   local tPrograms = textutils.unserialize( handler )
  290.   for i = 1, #tPrograms do
  291.     if tPrograms[i][2] == sFile then
  292.       table.remove( tPrograms, i)
  293.       table.remove( tPrograms, i)
  294.       break
  295.     end
  296.   end
  297.   fs.delete( autoFile )
  298.   file = fs.open( autoFile, "w" )
  299.   file.write( textutils.serialize( tPrograms ) )
  300.   file.close()
  301.   print( "Succesfully removed " .. sFile .. " from the auto update list." )
  302. elseif sCommand == "setstartup" then
  303.   local file = fs.open( prefFile, "w" )
  304.   file.write( tArgs[2] )
  305.   file.close()
  306.   print("" .. tArgs[2] .. " will now open on startup")
  307. elseif sCommand == "delstartup" then
  308.   if fs.exists(prefFile) then
  309.     fs.delete(prefFile)
  310.     print("This computer will no longer auto launch a program on startup")
  311.   end
  312.     print("Error: No launch program set")
  313. end
Advertisement
Add Comment
Please, Sign In to add comment