Advertisement
Tatantyler

PatchworkOS Installer

Oct 27th, 2012
472
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 57.52 KB | None | 0 0
  1. local programs = {}
  2. local drivers = {}
  3. local sysFiles = {}
  4.  
  5. programs["setperm"] = [[
  6. local args = {...}
  7.  
  8. if #args == 3 then
  9.     local targetFile = args[1]
  10.     local targetUser = args[2]
  11.     local targetPerms = args[3]
  12.  
  13.     local perms = {
  14.         ["write"] = false,
  15.         ["read"] = false,
  16.         ["execute"] = false,
  17.         ["delete"] = false,
  18.     }
  19.  
  20.     for char in targetPerms:gmatch(".") do
  21.         if char == "w" or char == "W" then
  22.             perms["write"] = true
  23.         elseif char == "r" or char == "R" then
  24.             perms["read"] = true
  25.         elseif char == "x" or char == "X" then
  26.             perms["execute"] = true
  27.         elseif char == "d" or char == "D" then
  28.             perms["delete"] = true
  29.         end
  30.     end
  31.  
  32.     for i,v in pairs(perms) do
  33.         local ok, err = os.setPerm(targetFile, i, v, targetUser)
  34.         if not ok then
  35.             error("Could not set permission: "..i)
  36.         end
  37.     end
  38. else
  39.     print("USAGE: setperm [file] [user] [perms]")
  40.     print("[perms] is a string of letters describing what permissions said user has.")
  41.     print("Example:")
  42.     print("setperm test Test rx")
  43.     print("That would allow the user \"Test\" to do look at and execute the file \"test\",")
  44.     print("but he/she wouldn't be able to change it or delete it.")
  45. end
  46. ]]
  47.  
  48. programs["setowner"] = [[
  49. local args = {...}
  50.  
  51. if #args == 2 then
  52.     if os.setOwner(args[1], args[2]) then
  53.         print("Done!")
  54.     else
  55.         term.setTextColor(colors.red)
  56.         print("Permission denied.")
  57.         term.setTextColor(colors.white)
  58.     end
  59. else
  60.     print("USAGE: setowner [file] [new owner]")
  61. end
  62. ]]
  63.  
  64. programs["turtleSettings"] = [[
  65. -- Turtle Settings Manager
  66.  
  67. local args = {...}
  68.  
  69. local function listSettings()
  70.     local settingsFile = fs.open(".os/turtle", "r")
  71.     local canMove = settingsFile.readLine()
  72.     local canDig = settingsFile.readLine()
  73.     local canPlace = settingsFile.readLine()
  74.     local canSuck = settingsFile.readLine()
  75.     settingsFile.close()
  76.     return canMove, canDig, canPlace, canSuck
  77. end
  78.  
  79. local function changeSettings(canMove, canDig, canPlace, canSuck)
  80.     local settingsFile = fs.open(".os/turtle", "w")
  81.     settingsFile.writeLine(canMove)
  82.     settingsFile.writeLine(canDig)
  83.     settingsFile.writeLine(canPlace)
  84.     settingsFile.writeLine(canSuck)
  85.     settingsFile.close()
  86. end
  87.  
  88. if os.getCurrentUser() == "root" then
  89.     if args[1] == "set" then
  90.         if #args == 5 then
  91.             changeSettings(args[2], args[3], args[4], args[5])
  92.         else
  93.             print("USAGE: turtleSettings [get/set] [canMove] [canDig] [canPlace] [canSuck]")
  94.         end
  95.     elseif args[1] == "get" then
  96.         local canMove,canDig,canPlace,canSuck = listSettings()
  97.         print("Can Move: "..canMove)
  98.         print("Can Dig: "..canDig)
  99.         print("Can Place: "..canPlace)
  100.         print("Can Suck: "..canSuck)
  101.     end
  102. else
  103.     print("You need to be root to do this!")
  104. end
  105. ]]
  106.  
  107. programs["backup"] = [=[
  108. args = {}
  109.  
  110. local function getAllFilesInDir(dir, includeDirs)
  111.     dir = dir or ""
  112.     local files = {}
  113.     for i,v in ipairs(fs.list(dir)) do
  114.         if fs.isDir(dir.."/"..v) then
  115.             if string.sub(v, 1,4) ~= "disk" and v ~= "rom" then
  116.                 if includeDirs then
  117.                     table.insert(files, v)
  118.                 end
  119.                 for i2, v2 in ipairs(getAllFilesInDir(dir.."/"..v, includeDirs)) do
  120.                     table.insert(files, v.."/"..v2)
  121.                 end
  122.             end
  123.         else
  124.             table.insert(files, v)
  125.         end
  126.     end
  127.     return files
  128. end
  129.  
  130. local function getDriveOne()
  131.     for i,v in ipairs(rs.getSides()) do
  132.         if peripheral.isPresent(v) and peripheral.getType(v) == "drive" and disk.getMountPath(v) == "disk" then
  133.             return v
  134.         end
  135.     end
  136. end
  137.  
  138. local restoreProgram = [[
  139.  
  140. local function getDriveOne()
  141.     for i,v in ipairs(rs.getSides()) do
  142.         if peripheral.isPresent(v) and peripheral.getType(v) == "drive" and disk.getMountPath(v) == "disk" then
  143.             return v
  144.         end
  145.     end
  146. end
  147.  
  148. local function restore()
  149.     if fs.exists("disk/backup") then
  150.         local backup = fs.open("disk/backup", "r")
  151.         local fileTable = backup.readAll()
  152.         backup.close()
  153.         local fileTable = textutils.unserialize(fileTable)
  154.         write("This disk contains a stored backup of computer ")
  155.         term.setTextColor(colors.lime)
  156.         print(tostring(fileTable[1])..".")
  157.         term.setTextColor(colors.white)
  158.         if type(fileTable[4]) == "string" then
  159.             write("Label: ")
  160.             term.setTextColor(colors.lime)
  161.             print(fileTable[4]..".")
  162.             term.setTextColor(colors.white)
  163.             os.setComputerLabel(fileTable[4])
  164.             hasLabel = true
  165.         end
  166.         write("Total files: ")
  167.         term.setTextColor(colors.lime)
  168.         print(tostring(fileTable[3]))
  169.         term.setTextColor(colors.white)
  170.         write("Total file size: ")
  171.         term.setTextColor(colors.lime)
  172.         print(tostring(fileTable[2]))
  173.         term.setTextColor(colors.white)
  174.         if fs.getFreeSpace("/") < fileTable[2] then
  175.             print("This computer does not have enough space to restore from the backup.")
  176.             print("Exiting...")
  177.             return
  178.         else
  179.             while true do
  180.                 print("Restore from backup? (Y/N) >")
  181.                 local yn = read()
  182.                 yn = string.upper(yn)
  183.                 if yn == "Y" then
  184.                     break
  185.                 elseif yn == "N" then
  186.                     return
  187.                 else
  188.                     term.setTextColor(colors.red)
  189.                     print("Please provide a valid response.")
  190.                     term.setTextColor(colors.white)
  191.                 end
  192.             end
  193.             os.sleep(1.5)
  194.             for i,v in ipairs(fileTable) do
  195.                 if i ~= 1 and i~= 2 and i ~= 3 then
  196.                     term.clear()
  197.                     term.setCursorPos(1,1)
  198.                     print("Restoring file...")
  199.                     write("ID: ")
  200.                     term.setTextColor(colors.lime)
  201.                     print(v[1])
  202.                     term.setTextColor(colors.white)
  203.                     write("Path: ")
  204.                     term.setTextColor(colors.lime)
  205.                     print(v[2])
  206.                     term.setTextColor(colors.white)
  207.                     if v[3] then
  208.                         print("Is a directory.")
  209.                     else
  210.                         print("Is a file.")
  211.                         write("Size: ")
  212.                         term.setTextColor(colors.lime)
  213.                         print(tostring(v[4]))
  214.                         term.setTextColor(colors.white)
  215.                     end
  216.                    
  217.                     if v[3] then
  218.                         fs.makeDir(v[2])
  219.                     else
  220.                         local file = fs.open(v[2], "w")
  221.                         file.write(v[5])
  222.                         file.close()
  223.                     end
  224.                     os.sleep(0.5)
  225.                 end
  226.             end
  227.             print(" ")
  228.             print("Restore complete.")
  229.             os.sleep(1)
  230.             disk.eject(getDriveOne())
  231.             os.reboot()
  232.         end
  233.     end
  234. end
  235.  
  236. restore()
  237. ]]
  238.  
  239. local function writeBackupToDisk(entries)
  240.     local backup = fs.open("disk/backup", "w")
  241.     backup.write(textutils.serialize(entries))
  242.     backup.close()
  243.     disk.setLabel(getDriveOne(), "Backup of computer "..tostring(os.computerID()))
  244.     local programHandle = fs.open("disk/startup", "w")
  245.     programHandle.write(restoreProgram)
  246.     programHandle.close()
  247.     disk.eject(getDriveOne())
  248. end
  249.  
  250. local function makeBackup()
  251.     local files = getAllFilesInDir("", true)
  252.     local entries = {}
  253.     local size = 0
  254.     for i,v in ipairs(files) do
  255.         if not fs.isDir(v) then
  256.             size = size + fs.getSize(v)
  257.         end
  258.     end
  259.     table.insert(entries, os.getComputerID())
  260.     table.insert(entries, size)
  261.     table.insert(entries, #files)
  262.     for i,v in ipairs(files) do
  263.         local fileEntry = {}
  264.         table.insert(fileEntry, i) --v[1]
  265.         table.insert(fileEntry, v) --v[2]
  266.         table.insert(fileEntry, fs.isDir(v)) --v[3]
  267.         if not fs.isDir(v) then
  268.             table.insert(fileEntry, fs.getSize(v)) --v[4]
  269.             local fileHandle = fs.open(v, "r")
  270.             table.insert(fileEntry, fileHandle.readAll()) --v[5]
  271.             fileHandle.close()
  272.         end
  273.         table.insert(entries, fileEntry)
  274.     end
  275.     return entries
  276. end
  277.  
  278. local function int_main()
  279.     if args[1] == "-disk" then
  280.         while true do
  281.             if getDriveOne() ~= nil then
  282.                 write("Starting backup to disk in drive on side: ")
  283.                 term.setTextColor(colors.lime)
  284.                 print(getDriveOne())
  285.                 term.setTextColor(colors.white)
  286.                 entries = makeBackup()
  287.                 writeBackupToDisk(entries)
  288.                 break
  289.             else
  290.                 term.setTextColor(colors.red)
  291.                 print("Please insert a disk.")
  292.                 term.setTextColor(colors.white)
  293.                 os.pullEvent("disk")
  294.             end
  295.         end
  296.     end
  297.     if args[1] == "-rednet" and (args[2] == "-backup_server" or args[2] == nil) then
  298.         entries = makeBackup()
  299.        
  300.         for i,v in ipairs(rs.getSides()) do
  301.             rednet.open(v)
  302.         end
  303.        
  304.         write "Transmitting via "
  305.         term.setTextColor(colors.lime)
  306.         write "Backup-Server Protocol"
  307.         term.setTextColor(colors.white)
  308.         print "..."
  309.        
  310.         local packet = {}
  311.         table.insert(packet, "backup")
  312.         table.insert(packet, textutils.serialize(entries))
  313.         packet = textutils.serialize(packet)
  314.        
  315.         if args[3] == nil then
  316.             rednet.broadcast(packet)
  317.         else
  318.             rednet.send(tonumber(args[3]), packet)
  319.         end
  320.     end
  321.     if args[1] == "-rednet" and args[2] == "-CommuteOS" then
  322.         entries = makeBackup()
  323.        
  324.         for i,v in ipairs(rs.getSides()) do
  325.             rednet.open(v)
  326.         end
  327.        
  328.         write("Transmitting via ")
  329.         term.setTextColor(colors.lightBlue)
  330.         write("Project Patchwork / CommuteOS")
  331.         term.setTextColor(colors.lime)
  332.         write(" RemoteFile Protocol")
  333.         term.setTextColor(colors.white)
  334.         print("...")
  335.        
  336.         local packet = {"FILE", "BACKUP_"..tostring(os.computerID()), textutils.serialize(entries)}
  337.         packet = textutils.serialize(packet)
  338.         if args[3] == nil then
  339.             rednet.broadcast(packet)
  340.         else
  341.             rednet.send(tonumber(args[3]), packet)
  342.         end
  343.     end
  344.     if args[1] == "-local" then
  345.         entries = makeBackup()
  346.         local handle = fs.open("local_backup", "w")
  347.         handle.write(textutils.serialize(entries))
  348.         handle.close()
  349.     end
  350.     return 0
  351.    
  352. end
  353.  
  354. local function set_params()
  355.     local option_1 = 1
  356.     local option_2 = 1
  357.     local option_3 = -1
  358.     while true do
  359.         term.clear()
  360.         term.setCursorPos(1,1)
  361.         print("Backup - Client")
  362.         print("Step 1: Choose a backup medium:")
  363.         print("1 - Rednet") -- y: 5, x: 1-10
  364.         print("2 - Disk") -- y: 6, x: 1-8
  365.         print("3 - Local") -- y: 7, x: 1-9
  366.         local event, x, y, btn = os.pullEvent()
  367.         if event == "key" then
  368.             option_1 = x - 1
  369.             if option_1 <= 3 and option_1 > 0 then
  370.                 break
  371.             end
  372.         elseif event == "click" then
  373.             if (x <= 10 and x >= 1) and y == 3 then
  374.                 option_1 = 1
  375.                 break
  376.             elseif (x <= 8 and x >= 1) and y == 4 then
  377.                 option_1 = 2
  378.                 break
  379.             elseif (x <= 9 and x >= 1) and y == 5 then
  380.                 option_1 = 3
  381.                 break
  382.             end
  383.         end
  384.     end
  385.     if option_1 == 1 then
  386.         while true do
  387.             term.clear()
  388.             term.setCursorPos(1,1)
  389.             print("Backup - Client")
  390.             print("Step 2: Choose a backup server type:")
  391.             print("1 - Backup Server") -- y: 3, x: 1-17
  392.             print("2 - Project Patchwork RemoteFile Protocol") -- y: 4, x: 1-42
  393.             local event, x, y, btn = os.pullEvent()
  394.             if event == "key" then
  395.                 option_2 = x - 1
  396.                 if option_2 <= 2 and option_2 > 0 then
  397.                     break
  398.                 end
  399.             elseif event == "click" then
  400.                 if (x <= 17 and x >= 1) and y == 3 then
  401.                     option_2 = 1
  402.                     break
  403.                 elseif (x <= 42 and x >= 1) and y == 4 then
  404.                     option_2 = 2
  405.                     break
  406.                 end
  407.             end
  408.         end
  409.         while true do
  410.             term.clear()
  411.             term.setCursorPos(1,1)
  412.             print("Backup - Client")
  413.             print("Step 3: Choose a server to backup to.")
  414.             print("(or you can choose 'broadcast'):")
  415.             local server = read()
  416.             if server == "broadcast" then
  417.                 option_3 = -1
  418.                 break
  419.             elseif type(tonumber(server)) == "number" then
  420.                 option_3 = tonumber(server)
  421.                 break
  422.             end
  423.         end
  424.     end
  425.     if option_1 == 1 then
  426.         args[1] = "-rednet"
  427.         if option_2 == 1 then
  428.             args[2] = "-backup_server"
  429.         elseif option_2 == 2 then
  430.             args[2] = "-CommuteOS"
  431.         end
  432.         if option_3 == -1 then
  433.             args[3] = nil
  434.         else
  435.             args[3] = option_3
  436.         end
  437.     elseif option_1 == 2 then
  438.         args[1] = "-disk"
  439.     elseif option_1 == 3 then
  440.         args[1] = "-local"
  441.     end
  442. end
  443.  
  444. set_params()
  445. int_main()
  446. ]=]
  447.  
  448. programs["addUser"] = [[
  449. os.addNewUser_dialog()
  450. term.clear()
  451. term.setCursorPos(1,1)
  452. ]]
  453.  
  454. programs["whoami"] = [[
  455. print("You are: "..os.getCurrentUser())
  456. ]]
  457.  
  458. programs["su"] = [[
  459. args = {...}
  460.  
  461. local function switchUser()
  462.     if os.getCurrentUser() == args[1] then
  463.         print("You are already "..args[1].."!")
  464.         return
  465.     end
  466.     if fs.exists("os/users/"..args[1]) then
  467.         if os.switchUser(args[2], args[1]) then
  468.             print("Successfully switched user!")
  469.             print("You are now: "..args[1])
  470.         end
  471.     else
  472.         print("ERROR: User does not exist!")
  473.     end
  474. end
  475.  
  476. if #args == 2 then
  477.     switchUser()
  478. else
  479.     print("USAGE: su [target user] [target password]")
  480. end
  481. ]]
  482.  
  483. programs["delUser"] = [[
  484. args = {...}
  485.  
  486. function delUser()
  487.     if os.getCurrentUser() == "root" then
  488.         if args[1] == "root" then
  489.             print("ERROR: Cannot delete root!")
  490.             return
  491.         end
  492.         if not fs.exists(".os/users/"..args[1]) then
  493.             print("ERROR: User does not exist!")
  494.             return
  495.         end
  496.         io.write("Are you sure you want to delete user "..args[1].."? (Y/N)>")
  497.         local yn = io.read()
  498.         yn = string.upper(yn)
  499.         if yn == "Y" then
  500.             fs.delete(".os/users/"..args[1])
  501.             if fs.exists(args[1]) then
  502.                 fs.delete(args[1])
  503.             end
  504.         else
  505.             return
  506.         end
  507.     else
  508.         print("You need to be root to do this!")
  509.         return
  510.     end
  511. end
  512.  
  513. if #args == 1 then
  514.     delUser()
  515. else
  516.     print("USAGE: delUser [username]")
  517. end
  518. ]]
  519.  
  520. programs["changePassword"] = [[
  521. args = {...}
  522.  
  523. local function writePassword(file, password)
  524.     local handle = fs.open(file, "wb")
  525.     local bytes = {}
  526.     for i=1, #password do
  527.         handle.write(string.byte(password, i, i))
  528.     end
  529.     handle.close()
  530. end
  531.  
  532. if #args == 3 then
  533.     if fs.exists(".os/users/"..args[1]) then
  534.         if os.authenticate(args[1], args[2]) then
  535.             local ciphertext = secure.rc4_cipher(args[3], args[1])
  536.             writePassword(".os/users/"..args[1], ciphertext)
  537.             write("Done. Your new password is: ")
  538.             term.setTextColor(colors.lime)
  539.             print(args[3])
  540.             term.setTextColor(colors.white)
  541.         else
  542.         term.setTextColor(colors.red)
  543.             error("Incorrect password!")
  544.             term.setTextColor(colors.white)
  545.         end
  546.     else
  547.         error("That user doesn't exist!")
  548.     end
  549. else
  550.     term.setTextColor(colors.lime)
  551.     print("USAGE: changePassword [target] [old password] [new password]")
  552.     term.setTextColor(colors.white)
  553. end
  554. ]]
  555.  
  556. programs["backupServer"] = [=[
  557. local args = {...}
  558. -- backup packet structure:
  559. -- {identifier, originator_id, files}
  560.  
  561. local function getDriveOne()
  562.     for i,v in ipairs(rs.getSides()) do
  563.         if peripheral.isPresent(v) and peripheral.getType(v) == "drive" and disk.getMountPath(v) == "disk" then
  564.             return v
  565.         end
  566.     end
  567. end
  568.  
  569. local restoreProgram = [[
  570.  
  571. local function getDriveOne()
  572.     for i,v in ipairs(rs.getSides()) do
  573.         if peripheral.isPresent(v) and peripheral.getType(v) == "drive" and disk.getMountPath(v) == "disk" then
  574.             return v
  575.         end
  576.     end
  577. end
  578.  
  579. local function restore()
  580.     if fs.exists("disk/backup") then
  581.         local backup = fs.open("disk/backup", "r")
  582.         local fileTable = backup.readAll()
  583.         backup.close()
  584.         local fileTable = textutils.unserialize(fileTable)
  585.         print("This disk contains a stored backup of computer "..tostring(fileTable[1])..".")
  586.         if type(fileTable[4]) == "string" then
  587.             print("Label: "..fileTable[4]..".")
  588.             os.setComputerLabel(fileTable[4])
  589.             hasLabel = true
  590.         end
  591.         print("Total files: "..tostring(fileTable[3]))
  592.         print("Total file size: "..tostring(fileTable[2]))
  593.         while true do
  594.             print("Restore from backup? (Y/N) >")
  595.             local yn = read()
  596.             yn = string.upper(yn)
  597.             if yn == "Y" then
  598.                 break
  599.             elseif yn == "N" then
  600.                 return
  601.             else
  602.                 print("Please provide a valid response.")
  603.             end
  604.         end
  605.         os.sleep(1.5)
  606.         for i,v in ipairs(fileTable) do
  607.             if i ~= 1 and i~= 2 and i ~= 3 then
  608.                 term.clear()
  609.                 term.setCursorPos(1,1)
  610.                 print("Restoring file.")
  611.                 print("ID: "..v[1])
  612.                 print("Path: "..v[2])
  613.                 if v[3] then
  614.                     print("Is a directory.")
  615.                 else
  616.                     print("Is a file.")
  617.                     print("Size: "..tostring(v[4]))
  618.                 end
  619.                
  620.                 if v[3] then
  621.                     fs.makeDir(v[2])
  622.                 else
  623.                     local file = fs.open(v[2], "w")
  624.                     file.write(v[5])
  625.                     file.close()
  626.                 end
  627.                 os.sleep(0.5)
  628.             end
  629.         end
  630.         print("Restore complete.")
  631.         os.sleep(1)
  632.         disk.eject(getDriveOne())
  633.         os.reboot()
  634.     end
  635. end
  636.  
  637. restore()
  638. ]]
  639.  
  640. local function writeBackupToDisk(entries, id)
  641.     local backup = fs.open("disk/backup", "w")
  642.     backup.write(entries)
  643.     backup.close()
  644.     disk.setLabel(getDriveOne(), "Backup of computer "..id)
  645.     local programHandle = fs.open("disk/startup", "w")
  646.     programHandle.write(restoreProgram)
  647.     programHandle.close()
  648.     disk.eject(getDriveOne())
  649. end
  650.  
  651. local function handleIncomingPackets()
  652.     for i,v in ipairs(rs.getSides()) do
  653.         rednet.open(v)
  654.     end
  655.     while true do
  656.         id, message = rednet.receive()
  657.         local packet = textutils.unserialize(message)
  658.         if type(packet) == "table" then -- valid packet
  659.             if packet[1] == "backup" then
  660.                 if not fs.exists("backups") then
  661.                     fs.makeDir("backups")
  662.                 end
  663.                 print("Received backup from computer "..tostring(id)..".")
  664.                 local backupHandle = fs.open("backups/"..tostring(id), "w")
  665.                 backupHandle.write(packet[2])
  666.                 backupHandle.close()
  667.             end
  668.         end
  669.     end
  670. end
  671.  
  672. local function handleInput()
  673.     while true do
  674.         term.clear()
  675.         term.setCursorPos(1,1)
  676.         print("Please enter a backup ID:")
  677.         local backup = read()
  678.         if type(tonumber(backup)) == "number" then
  679.             if fs.exists("backups/"..backup) then
  680.                 local handle = fs.open("backups/"..backup, "r")
  681.                 local files = handle.readAll()
  682.                 handle.close()
  683.                 writeBackupToDisk(files, backup)
  684.             else
  685.                 print("That backup does not exist.")
  686.                 os.sleep(2)
  687.             end
  688.         else
  689.             print("Please type in a valid ID.")
  690.             os.sleep(2)
  691.         end
  692.     end
  693. end
  694.  
  695. parallel.waitForAny(handleIncomingPackets, handleInput)
  696. ]=]
  697.  
  698. drivers["modem"] = [[
  699. local modemSide = "none"
  700.  
  701. function initialize(side)
  702.     rednet.open(side)
  703.     modemSide = side
  704.     while true do
  705.         local id, msg = rednet.receive()
  706.         msg = textutils.unserialize(msg) -- File Packet Structure: {"FILE", fileName, fileContents}
  707.         if type(msg) == "table" then
  708.             if msg[1] == "FILE" then
  709.                 if not fs.exists("remoteFiles/"..os.getCurrentUser()) then
  710.                     fs.makeDir("remoteFiles/"..os.getCurrentUser())
  711.                 end
  712.                 local handle = fs.open(fs.combine("remoteFiles/"..os.getCurrentUser(), msg[2]), "w")
  713.                 handle.write(msg[3])
  714.                 handle.close()
  715.                 os.queueEvent("file_received", id, msg[2], msg[3])
  716.                 local oldX, oldY = term.getCursorPos()
  717.                 term.setCursorPos(1,1)
  718.                 term.setTextColor(colors.lime)
  719.                 term.write("File Received!")
  720.                 term.setTextColor(colors.white)
  721.                 term.setCursorPos(oldX, oldY)
  722.                 os.sleep(2)
  723.                 oldX, oldY = term.getCursorPos()
  724.                 term.setCursorPos(1,1)
  725.                 term.clearLine()
  726.                 term.setCursorPos(oldX, oldY)
  727.             end
  728.         end
  729.     end
  730. end
  731.  
  732. function exit()
  733.     rednet.close(modemSide)
  734. end
  735. ]]
  736.  
  737. drivers["printer"] = [[
  738. local modemSide = "none"
  739.  
  740. function initialize(side)
  741.     rednet.open(side)
  742.     modemSide = side
  743.     while true do
  744.         local id, msg = rednet.receive()
  745.         msg = textutils.unserialize(msg) -- File Packet Structure: {"FILE", fileName, fileContents}
  746.         if type(msg) == "table" then
  747.             if msg[1] == "FILE" then
  748.                 if not fs.exists("remoteFiles/"..os.getCurrentUser()) then
  749.                     fs.makeDir("remoteFiles/"..os.getCurrentUser())
  750.                 end
  751.                 local handle = fs.open(fs.combine("remoteFiles/"..os.getCurrentUser(), msg[2]), "w")
  752.                 handle.write(msg[3])
  753.                 handle.close()
  754.                 os.queueEvent("file_received", id, msg[2], msg[3])
  755.                 local oldX, oldY = term.getCursorPos()
  756.                 term.setCursorPos(1,1)
  757.                 term.setTextColor(colors.lime)
  758.                 term.write("File Received!")
  759.                 term.setTextColor(colors.white)
  760.                 term.setCursorPos(oldX, oldY)
  761.                 os.sleep(2)
  762.                 oldX, oldY = term.getCursorPos()
  763.                 term.setCursorPos(1,1)
  764.                 term.clearLine()
  765.                 term.setCursorPos(oldX, oldY)
  766.             end
  767.         end
  768.     end
  769. end
  770.  
  771. function exit()
  772.     rednet.close(modemSide)
  773. end
  774. ]]
  775.  
  776. sysFiles[".os/OS"] = [=[
  777. local version = "PatchworkOS v1.2"
  778.  
  779. local osBase = ".os/"
  780. local fileMetadataBase = osBase.."metadata/"
  781. local permBase = fileMetadataBase.."perms/"
  782. local ownerBase = fileMetadataBase.."owners/"
  783. local userBase = osBase.."users/"
  784. local logBase = osBase.."logs/"
  785. local PNPDriverBase = osBase.."PNP/"
  786.  
  787. local oldFSOpen = fs.open
  788. local oldOSPullEvent = os.pullEvent
  789. local oldOSPullEventRaw = os.pullEventRaw
  790. os.pullEvent = oldOSPullEventRaw
  791.  
  792. -- BEGIN ARC4 CODE:
  793.  
  794. -- ARCFOUR implementation in pure Lua
  795. -- Copyright 2008 Rob Kendrick <rjek@rjek.com>
  796. -- Distributed under the MIT licence
  797. --[[
  798. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated document
  799. ation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to
  800.  use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
  801.  to whom the Software is furnished to do so, subject to the following conditions:
  802.  
  803. The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
  804. Software.
  805.  
  806. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  807. WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  808. COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  809. OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  810. ]]--
  811.  
  812. local function make_byte_table(bits)
  813.     local f = { }
  814.     for i = 0, 255 do
  815.         f[i] = { }
  816.     end
  817.    
  818.     f[0][0] = bits[1] * 255
  819.  
  820.     local m = 1
  821.    
  822.     for k = 0, 7 do
  823.         for i = 0, m - 1 do
  824.             for j = 0, m - 1 do
  825.                 local fij = f[i][j] - bits[1] * m
  826.                 f[i  ][j+m] = fij + bits[2] * m
  827.                 f[i+m][j  ] = fij + bits[3] * m
  828.                 f[i+m][j+m] = fij + bits[4] * m
  829.             end
  830.         end
  831.         m = m * 2
  832.     end
  833.    
  834.     return f
  835. end
  836.  
  837. local byte_xor = make_byte_table { 0, 1, 1, 0 }
  838.  
  839. local function generate(self, count)
  840.     local S, i, j = self.S, self.i, self.j
  841.     local o = { }
  842.     local char = string.char
  843.    
  844.     for z = 1, count do
  845.         i = (i + 1) % 256
  846.         j = (j + S[i]) % 256
  847.         S[i], S[j] = S[j], S[i]
  848.         o[z] = char(S[(S[i] + S[j]) % 256])
  849.     end
  850.    
  851.     self.i, self.j = i, j
  852.     return table.concat(o)
  853. end
  854.  
  855. local function cipher(self, plaintext)
  856.     local pad = generate(self, #plaintext)
  857.     local r = { }
  858.     local byte = string.byte
  859.     local char = string.char
  860.    
  861.     for i = 1, #plaintext do
  862.         r[i] = char(byte_xor[byte(plaintext, i)][byte(pad, i)])
  863.     end
  864.    
  865.     return table.concat(r)
  866. end
  867.  
  868. local function schedule(self, key)
  869.     local S = self.S
  870.     local j, kz = 0, #key
  871.     local byte = string.byte
  872.    
  873.     for i = 0, 255 do
  874.         j = (j + S[i] + byte(key, (i % kz) + 1)) % 256;
  875.         S[i], S[j] = S[j], S[i]
  876.     end
  877. end
  878.  
  879. local function new_arc4(key)
  880.     local S = { }
  881.     local r = {
  882.         S = S, i = 0, j = 0,
  883.         generate = generate,
  884.         cipher = cipher,
  885.         schedule = schedule
  886.     }
  887.    
  888.     for i = 0, 255 do
  889.         S[i] = i
  890.     end
  891.    
  892.     if key then
  893.         r:schedule(key)
  894.     end
  895.    
  896.     return r   
  897. end
  898.  
  899. -- Gee, I sure do love putting arc4 everywhere, don't I?
  900.  
  901. local function rc4_cipher(key, text)
  902.     local rc4 = new_arc4(key)
  903.     rc4:generate(3072)
  904.     local encrypted = rc4:cipher(text)
  905.     return encrypted
  906. end
  907.  
  908. local function readPassword(file)
  909.     local handle = oldFSOpen(file, "rb")
  910.     local bytes = {}
  911.     while true do
  912.         local byte = handle.read()
  913.         if byte ~= nil then
  914.             table.insert(bytes, byte)
  915.         else
  916.             break
  917.         end
  918.     end
  919.     handle.close()
  920.     return string.char(unpack(bytes))
  921. end
  922.  
  923. local function writePassword(file, password)
  924.     local handle = oldFSOpen(file, "wb")
  925.     local bytes = {}
  926.     for i=1, #password do
  927.         handle.write(string.byte(password, i, i))
  928.     end
  929.     handle.close()
  930. end
  931.  
  932. local currentUser = "root"
  933.  
  934. local function writeToLog(logFile, text)
  935.     local logHandle = oldFSOpen(logBase..logFile, "a")
  936.     logHandle.writeLine("["..currentUser.."] "..text)
  937.     logHandle.close()
  938. end
  939.  
  940. fs.makeDir(permBase)
  941. fs.makeDir(ownerBase)
  942. fs.makeDir(userBase)
  943. fs.makeDir(logBase)
  944. fs.makeDir(PNPDriverBase)
  945.  
  946. local function makeFileSafe(file)
  947.     file = string.gsub(file, "%/", "#002F")
  948.     file = string.gsub(file, "%\\", "#005C")
  949.     file = string.gsub(file, "%:", "#003A")
  950.     file = string.gsub(file, "%*", "#002A")
  951.     file = string.gsub(file, "%?", "#003F")
  952.     file = string.gsub(file, "%\"", "#0022")
  953.     file = string.gsub(file, "%<", "#003C")
  954.     file = string.gsub(file, "%>", "#003E")
  955.     file = string.gsub(file, "%|", "#007C")
  956.     return file
  957. end
  958.  
  959. local function safeFilenameToTrueName(file)
  960.     file = string.gsub(file, "#002F", "/")
  961.     file = string.gsub(file, "#005C", "\\")
  962.     file = string.gsub(file, "#003A", ":")
  963.     file = string.gsub(file, "#002A", "*")
  964.     file = string.gsub(file, "#003F", "?")
  965.     file = string.gsub(file, "#0022", "\"")
  966.     file = string.gsub(file, "#003C", "<")
  967.     file = string.gsub(file, "#003E", ">")
  968.     file = string.gsub(file, "#007C", "|")
  969.     return file
  970. end
  971.  
  972. local oldFSDelete = fs.delete
  973. local oldFSMove = fs.move
  974. local oldFSCopy = fs.copy
  975. local oldLoadFile = loadfile
  976. local oldDoFile = dofile
  977. local oldOSLoadAPI = os.loadAPI
  978.  
  979. local function newFilePrep(file)
  980.     if not fs.exists(file) then
  981.         local permsHandle = oldFSOpen(fs.combine(permBase, makeFileSafe(file)), "w")
  982.         permsHandle.write(textutils.serialize({[currentUser]={["delete"]=true, ["read"]=true, ["write"]=true, ["execute"]=true}}))
  983.         permsHandle.close()
  984.         local ownerHandle = oldFSOpen(fs.combine(ownerBase, makeFileSafe(file)), "w")
  985.         ownerHandle.write(currentUser)
  986.         ownerHandle.close()
  987.         return true
  988.     else
  989.         return false, "file already exists"
  990.     end
  991. end
  992.  
  993. local function getFileMetadata(file)
  994.     if not fs.exists(fs.combine(ownerBase, makeFileSafe(file))) then
  995.         local ownerHandle = oldFSOpen(fs.combine(ownerBase, makeFileSafe(file)), "w")
  996.         ownerHandle.write("root")
  997.         ownerHandle.close()
  998.     end
  999.     if not fs.exists(fs.combine(permBase, makeFileSafe(file))) then
  1000.         local permsHandle = oldFSOpen(fs.combine(permBase, makeFileSafe(file)), "w")
  1001.         permsHandle.write(textutils.serialize({["root"]={["delete"]=true, ["read"]=true, ["write"]=true, ["execute"]=true}}))
  1002.         permsHandle.close()
  1003.     end
  1004.     local permsHandle = oldFSOpen(fs.combine(permBase, makeFileSafe(file)), "r")
  1005.     local ownerHandle = oldFSOpen(fs.combine(ownerBase, makeFileSafe(file)), "r")
  1006.     local perms = textutils.unserialize(permsHandle.readAll())
  1007.     local owner = ownerHandle.readAll()
  1008.     permsHandle.close()
  1009.     ownerHandle.close()
  1010.     return perms, owner
  1011. end
  1012.  
  1013. local function isAllowed(file, action)
  1014.     if currentUser == "root" then
  1015.         return true
  1016.     end
  1017.     if fs.exists(file) then
  1018.         if fs.isReadOnly(file) and (action == "read" or action == "execute") then
  1019.             return true
  1020.         elseif (string.find(file, ".metadata") ~= nil or string.find(file, ".os") ~= nil) then
  1021.             return false
  1022.         elseif (string.sub(file,1, 4) == "bin/") and (action == "read" or action == "execute") then
  1023.             return true
  1024.         end
  1025.         local perms, owner = getFileMetadata(file)
  1026.         if currentUser == owner then
  1027.             return true
  1028.         elseif action == "delete" or action == "write" or action == "read" or action == "execute" then
  1029.             if perms[currentUser] then
  1030.                 return perms[currentUser][action]
  1031.             else
  1032.                 return false
  1033.             end
  1034.         elseif action == "modperms" then
  1035.             return (owner == currentUser)
  1036.         else
  1037.             error("isAllowed: Invalid action parameter")
  1038.         end
  1039.     else
  1040.         return true
  1041.     end
  1042. end
  1043.  
  1044. os.getPerms = function(file)
  1045.     local perms, owner = getFileMetadata(file)
  1046.     return perms[currentUser], owner
  1047. end
  1048.  
  1049. os.setPerm = function(file, perm, value, user)
  1050.     if user == nil then
  1051.         user = currentUser
  1052.     end
  1053.     if (perm ~= "delete") and (perm ~= "read") and (perm ~= "write") and (perm ~= "execute") then
  1054.         return false, "invalid permission"
  1055.     end
  1056.     if fs.exists(file) then
  1057.         if isAllowed(file, "modperms") and not fs.isReadOnly(file) then
  1058.             writeToLog("FilePerm", "Setting perms for file: "..file..", user: "..user)
  1059.             writeToLog("FilePerm", "Perm: "..perm..", value: "..tostring(value))
  1060.             local perms = getFileMetadata(file)
  1061.             if perms[user] == nil then
  1062.                 perms[user] = {["delete"]=false, ["read"]=false, ["write"]=false, ["execute"]=false}
  1063.             end
  1064.             perms[user][perm] = value
  1065.             local permsHandle = oldFSOpen(fs.combine(permBase, makeFileSafe(file)), "w")
  1066.             permsHandle.write(textutils.serialize(perms))
  1067.             permsHandle.close()
  1068.             return true
  1069.         else
  1070.             writeToLog("FilePerm", "Unauthorized user attempted to set perms for file: "..file..".")
  1071.             return false, "not owner"
  1072.         end
  1073.     else
  1074.         writeToLog("FilePerm", "User attempted to set perms for nonexistent file.")
  1075.         return false, "file does not exist"
  1076.     end
  1077. end
  1078.  
  1079. os.setOwner = function(file, newOwner)
  1080.     if isAllowed(file, "modperms") then
  1081.         writeToLog("FilePerm", "File "..file.." is switching owners to "..newOwher)
  1082.         local ownerHandle = oldFSOpen(fs.combine(ownerBase, makeFileSafe(file)), "r")
  1083.         ownerHandle.write(newOwner)
  1084.         ownerHandle.close()
  1085.         return true
  1086.     else
  1087.         writeToLog("FilePerm", "Unauthorized user attempted to change the ownership of file:"..file..".")
  1088.         return false, "not owner"
  1089.     end
  1090. end
  1091.  
  1092. -- begin FS rewrites
  1093.  
  1094. fs.open = function(file, mode)
  1095.     if fs.exists(file) then
  1096.         if mode == "a" or mode == "ab" or mode == "w" or mode == "wb" then
  1097.             if isAllowed(file, "write") then
  1098.                 writeToLog("FileAccess", "User opened "..file.." for writing.")
  1099.                 return oldFSOpen(file, mode)
  1100.             else
  1101.                 writeToLog("FileAccess", "User attempted to write to file "..file.." without permission.")
  1102.             end
  1103.         elseif mode == "r" or mode == "rb" then
  1104.             if isAllowed(file, "read") then
  1105.                 writeToLog("FileAccess", "User opened "..file.." for reading.")
  1106.                 return oldFSOpen(file, mode)
  1107.             else
  1108.                 writeToLog("FileAccess", "User attempted to read from file "..file.." without permission.")
  1109.             end
  1110.         end
  1111.     else
  1112.         writeToLog("FileAccess", "User opened new file "..file..".")
  1113.         newFilePrep(file)
  1114.         return oldFSOpen(file, mode)
  1115.     end
  1116. end
  1117.  
  1118. fs.delete = function(file)
  1119.     if fs.exists(file) then
  1120.         if isAllowed(file, "delete") then
  1121.             writeToLog("FileAccess", "User deleted "..file..".")
  1122.             return oldFSDelete(file)
  1123.         else
  1124.             writeToLog("FileAccess", "User tried to delete "..file.." without permission.")
  1125.         end
  1126.     end
  1127. end
  1128.  
  1129. fs.copy = function(oldFile, newFile)
  1130.     if not fs.exists(newFile) then
  1131.         if isAllowed(oldFile, "read") then
  1132.             writeToLog("FileAccess", "User copied "..oldFile.." to "..newFile..".")
  1133.             return oldFSCopy(oldFile, newFile)
  1134.         else
  1135.             writeToLog("FileAccess", "User tried to copy "..oldFile.." to "..newFile.." without permission.")
  1136.         end
  1137.     end
  1138. end
  1139.  
  1140. fs.move = function(oldFile, newFile)
  1141.     if not fs.exists(newFile) then
  1142.         if isAllowed(oldFile, "read") then
  1143.             writeToLog("FileAccess", "User moved "..oldFile.." to "..newFile..".")
  1144.             return oldFSMove(oldFile, newFile)
  1145.         else
  1146.             writeToLog("FileAccess", "User tried to move "..oldFile.." to "..newFile.." without permission.")
  1147.         end
  1148.     end
  1149. end
  1150.  
  1151. loadfile = function(file)
  1152.     if fs.exists(file) then
  1153.         if isAllowed(file, "execute") then
  1154.             writeToLog("FileExecute", "Executing: "..file)
  1155.             local handle = oldFSOpen(file, "r")
  1156.             local func, err = loadstring(handle.readAll(), fs.getName(file))
  1157.             handle.close()
  1158.             return func, err
  1159.         else
  1160.             writeToLog("FileExecute", "User tried to execute "..file.." without permission.")
  1161.             return nil, "Cannot execute"
  1162.         end
  1163.     else
  1164.         return nil, "File not found"
  1165.     end
  1166. end
  1167.  
  1168. dofile = function(file)
  1169.     local func, err = loadfile(file)
  1170.     if not func then
  1171.         error("dofile: "..err, 2)
  1172.     else
  1173.         setfenv(func, getfenv(2))
  1174.         return func()
  1175.     end
  1176. end
  1177.  
  1178. local function trustedLoadAPI(path)
  1179.     local handle = oldFSOpen(path, "r")
  1180.     local data = handle.readAll()
  1181.     handle.close()
  1182.     local func = loadstring(data)
  1183.     local env = {}
  1184.     setmetatable(env, {__index = _G})
  1185.     setfenv(func, env)
  1186.     func()
  1187.     local api = {}
  1188.     for i,v in pairs(env) do
  1189.         api[i] = v
  1190.     end
  1191.     _G[fs.getName(path)] = api
  1192. end
  1193.  
  1194. local oldOSRun = os.run
  1195.  
  1196. os.run = function(env, path, ...)
  1197.     if isAllowed(path, "execute") then
  1198.         writeToLog("FileExecute", "Executing: "..path)
  1199.         return oldOSRun(env, path, unpack(arg))
  1200.     else
  1201.         writeToLog("FileExecute", "User tried to execute "..path.." without permission.")
  1202.         return false, "cannot execute"
  1203.     end
  1204. end
  1205.  
  1206. if fs.exists(osBase.."arcfour") then -- of course
  1207.     os.loadAPI(osBase.."arcfour")
  1208.     os.sleep(1)
  1209. else
  1210.     if http then
  1211.         local webHandle = http.get("http://pastebin.com/raw.php?i=73q8zCPc")
  1212.         local data = webHandle.readAll()
  1213.         webHandle.close()
  1214.         local fileHandle = fs.open(osBase.."arcfour", "w")
  1215.         fileHandle.write(data)
  1216.         fileHandle.close()
  1217.         os.loadAPI(osBase.."arcfour")
  1218.     else
  1219.         print("arcfour API not found!")
  1220.     end
  1221. end
  1222.  
  1223. local loadUsers = function()
  1224.     local users = {}
  1225.     local passwordCiphertext = {}
  1226.     for i,v in ipairs(fs.list(userBase)) do
  1227.         print(i..": "..v)
  1228.         local handle = oldFSOpen(userBase..v, "rb")
  1229.         local bytes = {}
  1230.         while true do
  1231.             local byte = handle.read()
  1232.             if byte then
  1233.                 table.insert(bytes, byte)
  1234.             else
  1235.                 break
  1236.             end
  1237.         end
  1238.         handle.close()
  1239.         users[v] = true
  1240.         passwordCiphertext[v] = string.char(unpack(bytes))
  1241.     end
  1242.     return users, passwordCiphertext
  1243. end
  1244.  
  1245. local oldRSSetOutput = rs.setOutput
  1246. local oldRSSetBundledOutput = rs.setBundledOutput
  1247.  
  1248. rs.setOutput = function(side, value)
  1249.     writeToLog("RS", "Set redstone output on "..side.." to "..tostring(value)..".")
  1250.     return oldRSSetOutput(side, value)
  1251. end
  1252.  
  1253. rs.setBundledOutput = function(side, value)
  1254.     writeToLog("RS", "Set bundled redstone output on "..side.." to "..tostring(value)..".")
  1255.     return oldRSSetBundledOutput(side, value)
  1256. end
  1257.  
  1258. redstone.setOutput = rs.setOutput
  1259. redstone.setBundledOutput = rs.setBundledOutput
  1260.  
  1261. if http then
  1262.     local oldHTTPRequest = http.request
  1263.     local oldHTTPGet = http.get
  1264.     local oldHTTPPost = http.post
  1265.    
  1266.     http.request = function(url, postData)
  1267.         if postData == nil then
  1268.             writeToLog("HTTP", "Request sent to "..url..", no data sent.")
  1269.         else
  1270.             writeToLog("HTTP", "Request sent to "..url..", data:"..postData)
  1271.         end
  1272.         return oldHTTPRequest(url, postData)
  1273.     end
  1274.    
  1275.     http.get = function(url)
  1276.         writeToLog("HTTP", "Fetched data from "..url..".")
  1277.         return oldHTTPGet(url)
  1278.     end
  1279.    
  1280.     http.post = function(url, postData)
  1281.         writeToLog("HTTP", "Sent data:"..postData.." to url:"..url..".")
  1282.         return oldHTTPPost(url, postData)
  1283.     end
  1284.     print("Done.")
  1285. end
  1286.  
  1287. os.version = function()
  1288.     return version
  1289. end
  1290.  
  1291. local oldShellRun = shell.run
  1292.  
  1293. local function existsInFolder(dir, file)
  1294.     for _,v in ipairs(fs.list(dir)) do
  1295.         if v == file then
  1296.             return true -- found a match in the toplevel dir
  1297.         end
  1298.     end
  1299. end
  1300.  
  1301.  
  1302. shell.run = function(program, ...)
  1303.     writeToLog("FileExecute", "shell.run called, program:"..program)
  1304.     oldShellRun(program, unpack(arg))
  1305. end
  1306.  
  1307. local oldRednetSend = rednet.send
  1308. local oldRednetBroadcast = rednet.broadcast
  1309. local oldRednetAnnounce = rednet.announce
  1310.  
  1311. rednet.send = function(senderID, message)
  1312.     writeToLog("Rednet", "Sent message to id "..senderID.." with message "..message)
  1313.     return oldRednetSend(senderID, message)
  1314. end
  1315.  
  1316. rednet.broadcast = function(message)
  1317.     writeToLog("Rednet", "Broadcasted message "..message)
  1318.     return oldRednetBroadcast(message)
  1319. end
  1320.  
  1321. rednet.announce = function()
  1322.     writeToLog("Rednet", "Sent announce message.")
  1323.     return oldRednetAnnounce()
  1324. end
  1325.  
  1326. local oldOSShutdown = os.shutdown
  1327. local oldOSReboot = os.reboot
  1328. local oldOSLoadAPI = os.loadAPI
  1329. local oldOSSetLabel = os.setComputerLabel
  1330.  
  1331. os.shutdown = function()
  1332.     writeToLog("OS", "Shutting down "..version..". Uptime:"..tostring(os.clock()))
  1333.     oldOSShutdown()
  1334. end
  1335.  
  1336. os.reboot = function()
  1337.     writeToLog("OS", "Rebooting "..version..". Uptime:"..tostring(os.clock()))
  1338.     oldOSReboot()
  1339. end
  1340.  
  1341. os.loadAPI = function(api)
  1342.     writeToLog("OS", "Loading API: "..api)
  1343.     oldOSLoadAPI(api)
  1344. end
  1345.  
  1346. os.setComputerLabel = function(label)
  1347.     writeToLog("OS", "Setting computer label to:"..label)
  1348.     oldOSSetLabel(label)
  1349. end
  1350.  
  1351. if turtle then
  1352.     if fs.exists(osBase.."turtle") then
  1353.         local settingsFile = fs.open(osBase.."turtle", "r")
  1354.         local canMove = settingsFile.readLine()
  1355.         local canDig = settingsFile.readLine()
  1356.         local canPlace = settingsFile.readLine()
  1357.         local canSuck = settingsFile.readLine()
  1358.         settingsFile.close()
  1359.     else
  1360.         local handle = fs.open(osBase.."turtle", "w")
  1361.         handle.write("true")
  1362.         handle.write("true")
  1363.         handle.write("true")
  1364.         handle.write("true")
  1365.         handle.close()
  1366.         local canMove = "true"
  1367.         local canDig = "true"
  1368.         local canPlace = "true"
  1369.         local canSuck = "true"
  1370.     end
  1371.    
  1372.     local oldTurtleDig = turtle.dig
  1373.     local oldTurtleDigUp = turtle.digUp
  1374.     local oldTurtleDigDown = turtle.digDown
  1375.     local oldTurtlePlaceDown = turtle.placeDown
  1376.     local oldTurtlePlaceUp = turtle.placeUp
  1377.     local oldTurtlePlace = turtle.place
  1378.     local oldTurtleForward = turtle.forward
  1379.     local oldTurtleBack = turtle.back
  1380.     local oldTurtleTurnLeft = turtle.turnLeft
  1381.     local oldTurtleTurnRight = turtle.turnRight
  1382.     local oldTurtleUp = turtle.up
  1383.     local oldTurtleDown = turtle.down
  1384.     local oldTurtleSuck = turtle.suck
  1385.     local oldTurtleSuckUp = turtle.suckUp
  1386.     local oldTurtleSuckDown = turtle.suckDown
  1387.     local oldTurtleRefuel = turtle.refuel
  1388.    
  1389.     turtle.dig = function()
  1390.         if canDig == "true" or currentUser == "root" then
  1391.             writeToLog("Turtle", "Broke block in front.")
  1392.             return oldTurtleDig()
  1393.         else
  1394.             writeToLog("Turtle", "Tried to dig, but digging has been diabled.")
  1395.             return false
  1396.         end
  1397.     end
  1398.    
  1399.     turtle.digUp = function()
  1400.         if canDig == "true" or currentUser == "root" then
  1401.             writeToLog("Turtle", "Broke block on top.")
  1402.             return oldTurtleDigUp()
  1403.         else
  1404.             writeToLog("Turtle", "Tried to dig, but digging has been diabled.")
  1405.             return false
  1406.         end
  1407.     end
  1408.    
  1409.     turtle.digDown = function()
  1410.         if canDig == "true" or currentUser == "root" then
  1411.             writeToLog("Turtle", "Broke block under.")
  1412.             return oldTurtleDigDown()
  1413.         else
  1414.             writeToLog("Turtle", "Tried to dig, but digging has been diabled.")
  1415.             return false
  1416.         end
  1417.     end
  1418.    
  1419.    
  1420.     turtle.place = function()
  1421.         if canPlace == "true" or currentUser == "root" then
  1422.             writeToLog("Turtle", "Placed block in front.")
  1423.             return oldTurtlePlace()
  1424.         else
  1425.             writeToLog("Turtle", "Tried to place, but placing has been diabled.")
  1426.             return false
  1427.         end
  1428.     end
  1429.    
  1430.     turtle.placeDown = function()
  1431.         if canPlace == "true" or currentUser == "root" then
  1432.             writeToLog("Turtle", "Placed block under.")
  1433.             return oldTurtlePlaceDown()
  1434.         else
  1435.             writeToLog("Turtle", "Tried to place, but placing has been diabled.")
  1436.             return false
  1437.         end
  1438.     end
  1439.    
  1440.     turtle.placeUp = function()
  1441.         if canPlace == "true" or currentUser == "root" then
  1442.             writeToLog("Turtle", "Placed block on top.")
  1443.             return oldTurtlePlaceUp()
  1444.         else
  1445.             writeToLog("Turtle", "Tried to place, but placing has been diabled.")
  1446.             return false
  1447.         end
  1448.     end
  1449.    
  1450.     turtle.forward = function()
  1451.         if canMove == "true" or currentUser == "root" then
  1452.             writeToLog("Turtle", "Moved forward.")
  1453.             return oldTurtleForward()
  1454.         else
  1455.             writeToLog("Turtle", "Tried to move, but movement has been diabled.")
  1456.             return false
  1457.         end
  1458.     end
  1459.    
  1460.     turtle.back = function()
  1461.         if canMove == "true" or currentUser == "root" then
  1462.             writeToLog("Turtle", "Moved backwards.")
  1463.             return oldTurtleBack()
  1464.         else
  1465.             writeToLog("Turtle", "Tried to move, but movement has been diabled.")
  1466.             return false
  1467.         end
  1468.     end
  1469.    
  1470.     turtle.turnLeft = function()
  1471.         if canMove == "true" or currentUser == "root" then
  1472.             writeToLog("Turtle", "Turned left.")
  1473.             return oldTurtleTurnLeft()
  1474.         else
  1475.             writeToLog("Turtle", "Tried to move, but movement has been diabled.")
  1476.             return false
  1477.         end
  1478.     end
  1479.    
  1480.     turtle.up = function()
  1481.         if canMove == "true" or currentUser == "root" then
  1482.             writeToLog("Turtle", "Went up.")
  1483.             return oldTurtleUp()
  1484.         else
  1485.             writeToLog("Turtle", "Tried to move, but movement has been diabled.")
  1486.             return false
  1487.         end
  1488.     end
  1489.    
  1490.    
  1491.     turtle.down = function()
  1492.         if canMove == "true" or currentUser == "root" then
  1493.             writeToLog("Turtle", "Went down.")
  1494.             return oldTurtleDown()
  1495.         else
  1496.             writeToLog("Turtle", "Tried to move, but movement has been diabled.")
  1497.             return false
  1498.         end
  1499.     end
  1500.    
  1501.    
  1502.     turtle.turnRight = function()
  1503.         if canMove == "true" or currentUser == "root" then
  1504.             writeToLog("Turtle", "Turned right.")
  1505.             return oldTurtleTurnRight()
  1506.         else
  1507.             writeToLog("Turtle", "Tried to move, but movement has been diabled.")
  1508.             return false
  1509.         end
  1510.     end
  1511.    
  1512.     turtle.suck = function()
  1513.         if canSuck == "true" or currentUser == "root" then
  1514.             writeToLog("Turtle", "Retrieved items.")
  1515.             return oldTurtleSuck()
  1516.         else
  1517.             writeToLog("Turtle", "Tried to retrieve items, but item retrieval has been diabled.")
  1518.             return false
  1519.         end
  1520.     end
  1521.    
  1522.     turtle.suckUp = function()
  1523.         if canSuck == "true" or currentUser == "root" then
  1524.             writeToLog("Turtle", "Retrieved items from above.")
  1525.             return oldTurtleSuckUp()
  1526.         else
  1527.             writeToLog("Turtle", "Tried to retrieve items, but item retrieval has been diabled.")
  1528.             return false
  1529.         end
  1530.     end
  1531.    
  1532.     turtle.suckDown = function()
  1533.         if canSuck == "true" or currentUser == "root" then
  1534.             writeToLog("Turtle", "Retrieved items from underneath.")
  1535.             return oldTurtleSuckDown()
  1536.         else
  1537.             writeToLog("Turtle", "Tried to retrieve items, but item retrieval has been diabled.")
  1538.             return false
  1539.         end
  1540.     end
  1541.    
  1542.     turtle.refuel = function()
  1543.         writeToLog("Turtle", "Refueled turtle, current fuel level is: "..turtle.getFuelLevel())
  1544.         return oldTurtleRefuel()
  1545.     end
  1546. end
  1547.  
  1548. local users, pwCtext = loadUsers()
  1549.  
  1550. os.authenticate = function(username, password)
  1551.     if users[username] then
  1552.         local pText = arcfour.rc4_cipher(password, pwCtext[username])
  1553.         return (pText == username)
  1554.     else
  1555.         return false, "user not found"
  1556.     end
  1557. end
  1558.  
  1559. os.switchUser = function(targetUser, password)
  1560.     if os.authenticate(targetUser, password) then
  1561.         currentUser = targetUser
  1562.         return true
  1563.     else
  1564.         return false
  1565.     end
  1566. end
  1567.  
  1568. os.newUser = function(user, password)
  1569.     if currentUser == "root" then
  1570.         local handle = oldFSOpen(userBase..user, "wb")
  1571.         local cText = arcfour.rc4_cipher(password, user)
  1572.         for i=1, #cText do
  1573.             handle.write(string.byte(string.sub(cText, i, i)))
  1574.         end
  1575.         handle.close()
  1576.         return true
  1577.     else
  1578.         return false
  1579.     end
  1580. end
  1581.  
  1582. os.getCurrentUser = function()
  1583.     return currentUser
  1584. end
  1585.  
  1586. os.retrieveCredentials = function()
  1587.     local function readAdv(maxChars, startX, startY, replaceChar, returnToStart, backgroundChar)
  1588.         term.setCursorBlink(true)
  1589.         local function clearArea(clearLine, clearX, clearArea, replaceChar)
  1590.             local formerX,formerY = term.getCursorPos()
  1591.             for i=0, clearArea do
  1592.                 term.setCursorPos(clearX+i, clearLine)
  1593.                 if replaceChar ~= nil then
  1594.                     io.write(replaceChar)
  1595.                 else
  1596.                     io.write(" ")
  1597.                 end
  1598.             end
  1599.                 term.setCursorPos(formerX,formerY)
  1600.         end
  1601.  
  1602.         local x,y = term.getCursorPos()
  1603.  
  1604.         term.setCursorPos(startX, startY)
  1605.         clearArea(startY, startX, maxChars, backgroundChar)
  1606.  
  1607.         local input = ""
  1608.         while true do
  1609.             local event, key = os.pullEvent()
  1610.             if event == "key" then
  1611.                 if key == 28 or key == 156 then
  1612.                     if returnToStart then
  1613.                     term.setCursorPos(x,y)
  1614.                     end
  1615.                     return input
  1616.                 elseif key == 14 then
  1617.                     input = string.sub(input, 1, #input-1)
  1618.                     term.setCursorPos(startX, startY)
  1619.                     clearArea(startY, startX, maxChars, backgroundChar)
  1620.                     if replaceChar == nil then
  1621.                         io.write(input)
  1622.                     else
  1623.                         for i=1, #input do
  1624.                             io.write(replaceChar)
  1625.                         end
  1626.                     end
  1627.                 end
  1628.             elseif event == "char" then
  1629.                 if maxChars ~= nil then
  1630.                     if #input <= maxChars then
  1631.                         input = input..key
  1632.                         term.setCursorPos(startX, startY)
  1633.                         clearArea(startY, startX, maxChars, backgroundChar)
  1634.                         if replaceChar == nil then
  1635.                             io.write(input)
  1636.                         else
  1637.                             for i=1, #input do
  1638.                                 io.write(replaceChar)
  1639.                             end
  1640.                         end
  1641.                     end
  1642.                 end
  1643.             end
  1644.         end
  1645.     end
  1646.  
  1647.     local function printLoginScreen(startX, startY)
  1648.         term.setCursorPos(startX, startY)
  1649.         print("===========================")
  1650.         term.setCursorPos(startX, startY+1)
  1651.         write("|      ")
  1652.         term.setTextColor(colors.green)
  1653.         write("Please log in")
  1654.         term.setTextColor(colors.white)
  1655.         print(":     |")
  1656.         term.setCursorPos(startX, startY+2)
  1657.         print("===========================")
  1658.         term.setCursorPos(startX, startY+3)
  1659.         print("|        Username:        |")
  1660.         term.setCursorPos(startX, startY+4)
  1661.         print("|                         |")
  1662.         term.setCursorPos(startX, startY+5)
  1663.         print("===========================")
  1664.         term.setCursorPos(startX, startY+6)
  1665.         print("|        Password:        |")
  1666.         term.setCursorPos(startX, startY+7)
  1667.         print("|                         |")
  1668.         term.setCursorPos(startX, startY+8)
  1669.         print("===========================")
  1670.         term.setCursorPos(1,0)
  1671.     end
  1672.     --LOGINSCRN SPECS:
  1673.     --XLen: 27
  1674.     --YLen: 11
  1675.     --Textbox Space: 26
  1676.     --Username XStart:2
  1677.     --Username YStart:5
  1678.     --Password XStart:2
  1679.     --Password YStart:8
  1680.     term.clear()
  1681.     if not turtle then
  1682.         printLoginScreen(13,4)
  1683.     else
  1684.         printLoginScreen(1,1)
  1685.     end
  1686.     term.setTextColor(colors.lightGray)
  1687.     if not turtle then
  1688.         username = readAdv(24, 14, 8, nil, true, nil)
  1689.         password = readAdv(24, 14, 11, "#", true, nil)
  1690.     else
  1691.         username = readAdv(24, 2, 5, nil, true, nil)
  1692.         password = readAdv(24, 2, 8, "#", true, nil)
  1693.     end
  1694.     term.setTextColor(colors.white)
  1695.     term.clear()
  1696.     term.setCursorPos(1,1)
  1697.     term.setCursorBlink(false)
  1698.     return username, password
  1699. end
  1700.  
  1701. os.addNewUser_screen = function()
  1702.     if currentUser == "root" then
  1703.         term.clear()
  1704.         term.setCursorPos(1,1)
  1705.  
  1706.         local function readAdv(maxChars, startX, startY, replaceChar, returnToStart, backgroundChar)
  1707.             term.setCursorBlink(true)
  1708.             local function clearArea(clearLine, clearX, clearArea, replaceChar)
  1709.                 local formerX,formerY = term.getCursorPos()
  1710.                 for i=0, clearArea do
  1711.                     term.setCursorPos(clearX+i, clearLine)
  1712.                     if replaceChar ~= nil then
  1713.                         io.write(replaceChar)
  1714.                     else
  1715.                         io.write(" ")
  1716.                     end
  1717.                 end
  1718.                 term.setCursorPos(formerX,formerY)
  1719.             end
  1720.  
  1721.             local x,y = term.getCursorPos()
  1722.  
  1723.             term.setCursorPos(startX, startY)
  1724.             clearArea(startY, startX, maxChars, backgroundChar)
  1725.  
  1726.             local input = ""
  1727.             while true do
  1728.                 local event, key = os.pullEvent()
  1729.                 if event == "key" then
  1730.                     if key == 28 or key == 156 then
  1731.                         if returnToStart then
  1732.                             term.setCursorPos(x,y)
  1733.                         end
  1734.                         return input
  1735.                     elseif key == 14 then
  1736.                         input = string.sub(input, 1, #input-1)
  1737.                         term.setCursorPos(startX, startY)
  1738.                         clearArea(startY, startX, maxChars, backgroundChar)
  1739.                         if replaceChar == nil then
  1740.                             io.write(input)
  1741.                         else
  1742.                             for i=1, #input do
  1743.                                 io.write(replaceChar)
  1744.                             end
  1745.                         end
  1746.                     end
  1747.                 elseif event == "char" then
  1748.                     if maxChars ~= nil then
  1749.                         if #input <= maxChars then
  1750.                             input = input..key
  1751.                             term.setCursorPos(startX, startY)
  1752.                             clearArea(startY, startX, maxChars, backgroundChar)
  1753.                             if replaceChar == nil then
  1754.                                 io.write(input)
  1755.                             else
  1756.                                 for i=1, #input do
  1757.                                     io.write(replaceChar)
  1758.                                 end
  1759.                             end
  1760.                         end
  1761.                     end
  1762.                 end
  1763.             end
  1764.         end
  1765.        
  1766.         local startX = 1
  1767.         local startY = 1
  1768.  
  1769.         if not turtle then
  1770.             startX = 13
  1771.             startY = 3
  1772.         end
  1773.         term.setCursorPos(startX, startY)
  1774.         print("===========================")
  1775.         term.setCursorPos(startX, startY+1)
  1776.         write("|        ")
  1777.         term.setTextColor(colors.lime)
  1778.         write("New User")
  1779.         term.setTextColor(colors.white)
  1780.         print(":        |")
  1781.         term.setCursorPos(startX, startY+2)
  1782.         print("===========================")
  1783.         term.setCursorPos(startX, startY+3)
  1784.         print("|        Username:        |")
  1785.         term.setCursorPos(startX, startY+4)
  1786.         print("|                         |")
  1787.         term.setCursorPos(startX, startY+5)
  1788.         print("===========================")
  1789.         term.setCursorPos(startX, startY+6)
  1790.         print("|        Password:        |")
  1791.         term.setCursorPos(startX, startY+7)
  1792.         print("|                         |")
  1793.         term.setCursorPos(startX, startY+8)
  1794.         print("===========================")
  1795.  
  1796.         term.setTextColor(colors.lightGray)
  1797.         if not turtle then
  1798.             username = readAdv(24, 14, 8, nil, true, nil)
  1799.             password = readAdv(24, 14, 11, "#", true, nil)
  1800.         else
  1801.             user = readAdv(24, 2, 5, nil, true, nil)
  1802.             pass = readAdv(24, 2, 8, nil, true, nil)
  1803.         end
  1804.         term.setTextColor(colors.white)
  1805.         return user, pass
  1806.     end
  1807. end
  1808.  
  1809. local function makeRootAcct()
  1810.         term.clear()
  1811.         term.setCursorPos(1,1)
  1812.  
  1813.         local function readAdv(maxChars, startX, startY, replaceChar, returnToStart, backgroundChar)
  1814.             term.setCursorBlink(true)
  1815.             local function clearArea(clearLine, clearX, clearArea, replaceChar)
  1816.                 local formerX,formerY = term.getCursorPos()
  1817.                 for i=0, clearArea do
  1818.                     term.setCursorPos(clearX+i, clearLine)
  1819.                     if replaceChar ~= nil then
  1820.                         io.write(replaceChar)
  1821.                     else
  1822.                         io.write(" ")
  1823.                     end
  1824.                 end
  1825.                 term.setCursorPos(formerX,formerY)
  1826.             end
  1827.  
  1828.             local x,y = term.getCursorPos()
  1829.  
  1830.             term.setCursorPos(startX, startY)
  1831.             clearArea(startY, startX, maxChars, backgroundChar)
  1832.  
  1833.             local input = ""
  1834.             while true do
  1835.                 local event, key = os.pullEvent()
  1836.                 if event == "key" then
  1837.                     if key == 28 or key == 156 then
  1838.                         if returnToStart then
  1839.                             term.setCursorPos(x,y)
  1840.                         end
  1841.                         return input
  1842.                     elseif key == 14 then
  1843.                         input = string.sub(input, 1, #input-1)
  1844.                         term.setCursorPos(startX, startY)
  1845.                         clearArea(startY, startX, maxChars, backgroundChar)
  1846.                         if replaceChar == nil then
  1847.                             io.write(input)
  1848.                         else
  1849.                             for i=1, #input do
  1850.                                 io.write(replaceChar)
  1851.                             end
  1852.                         end
  1853.                     end
  1854.                 elseif event == "char" then
  1855.                     if maxChars ~= nil then
  1856.                         if #input <= maxChars then
  1857.                             input = input..key
  1858.                             term.setCursorPos(startX, startY)
  1859.                             clearArea(startY, startX, maxChars, backgroundChar)
  1860.                             if replaceChar == nil then
  1861.                                 io.write(input)
  1862.                             else
  1863.                                 for i=1, #input do
  1864.                                     io.write(replaceChar)
  1865.                                 end
  1866.                             end
  1867.                         end
  1868.                     end
  1869.                 end
  1870.             end
  1871.         end
  1872.     local startX = 1
  1873.     local startY = 1
  1874.     local pass = "root"
  1875.     if not turtle then
  1876.         startX = 13
  1877.         startY = 4
  1878.     end
  1879.     term.setCursorPos(startX, startY)
  1880.     print("===========================")
  1881.     term.setCursorPos(startX, startY+1)
  1882.     write("|        ")
  1883.     term.setTextColor(colors.lime)
  1884.     write("New User")
  1885.     term.setTextColor(colors.white)
  1886.     print(":        |")
  1887.     term.setCursorPos(startX, startY+2)
  1888.     print("===========================")
  1889.     term.setCursorPos(startX, startY+3)
  1890.     print("|        Username:        |")
  1891.     term.setCursorPos(startX, startY+4)
  1892.     print("|root                     |")
  1893.     term.setCursorPos(startX, startY+5)
  1894.     print("===========================")
  1895.     term.setCursorPos(startX, startY+6)
  1896.     print("|        Password:        |")
  1897.     term.setCursorPos(startX, startY+7)
  1898.     print("|                         |")
  1899.     term.setCursorPos(startX, startY+8)
  1900.     print("===========================")
  1901.    
  1902.     term.setTextColor(colors.lightGray)
  1903.     if turtle then
  1904.         pass = readAdv(24, 2, 8, nil, true, nil)
  1905.     else
  1906.         pass = readAdv(24, 14, 11, nil, true, nil)
  1907.     end
  1908.     term.setTextColor(colors.white)
  1909.     return pass
  1910. end
  1911.  
  1912. os.addNewUser_noDialog = function(user, pass)
  1913.     if currentUser == "root" then
  1914.         local ciphertext = rc4_cipher(pass, user)
  1915.         writePassword(userBase..user, ciphertext)
  1916.     end
  1917. end
  1918.  
  1919. os.addNewUser_dialog = function()
  1920.     if currentUser == "root" then
  1921.         os.addNewUser_noDialog(os.addNewUser_screen())
  1922.     end
  1923. end
  1924.  
  1925. local function drawLine(char)
  1926.     local maxX = term.getSize()
  1927.     print(" "..string.rep(char, maxX-2))
  1928. end
  1929.  
  1930. local function printCentered(input)
  1931.     local maxX = term.getSize()
  1932.     print(string.rep(" ", math.floor((maxX - string.len(input))/2))..input)
  1933. end
  1934.  
  1935. local function printColored(...)
  1936.     for i=1, arg["n"], 2 do
  1937.         if term.isColor() then
  1938.             term.setTextColor(arg[i])
  1939.         end
  1940.         write(arg[i+1])
  1941.     end
  1942.     print("")
  1943.     if term.isColor() then
  1944.         term.setTextColor(colors.white)
  1945.     end
  1946. end
  1947.  
  1948. local PNPDriverBase = osBase.."PNP/"
  1949. local peripherals = {}
  1950.  
  1951. local function loadPeripheral(peripheral)
  1952.     local function copy()
  1953.         if not fs.exists(".temp/PNPDrivers/") then
  1954.             fs.makeDir(".temp/PNPDrivers/")
  1955.         end
  1956.         local handle1 = oldFSOpen(PNPDriverBase..peripheral, "r")
  1957.         local handle2 = oldFSOpen(".temp/PNPDrivers/"..peripheral, "w")
  1958.         handle2.write(handle1.readAll())
  1959.         handle1.close()
  1960.         handle2.close()
  1961.     end
  1962.  
  1963.     if fs.exists(PNPDriverBase..peripheral) then
  1964.         copy()
  1965.         trustedLoadAPI(".temp/PNPDrivers/"..peripheral)
  1966.         return true
  1967.     else
  1968.         return false
  1969.     end
  1970. end
  1971.  
  1972. function runPNPFunc(peripheral, func, arg1)
  1973.     arg1 = arg1 or ""
  1974.     local chkString = "local function func() if type("..peripheral..") ~= \"table\" then return false else return type("..peripheral.."."..func.."()) == \"function\" end end return func()"
  1975.     local checkFunc = loadstring(chkString)
  1976.     if checkFunc() then
  1977.         local funcString = "local function func_call() "..peripheral.."."..func.."("..arg1..")".." end return func_call()"
  1978.         local func = loadstring(funcString)
  1979.         return func()
  1980.     end
  1981. end
  1982.  
  1983. function getPNPFunc(peripheral, func, arg1)
  1984.     arg1 = arg1 or ""
  1985.     local chkString = "local function func() if type("..peripheral..") ~= \"table\" then return false else return type("..peripheral.."."..func..") == \"function\" end end return func()"
  1986.     local checkFunc = loadstring(chkString)
  1987.     if checkFunc() then
  1988.         local funcString = "local function func_call() "..peripheral.."."..func.."("..arg1..")".." end return func_call()"
  1989.         local func = loadstring(funcString)
  1990.         return func
  1991.     end
  1992. end
  1993.  
  1994. local function pnp_waitForPeripheralEvent()
  1995.     while true do
  1996.         local event, side = os.pullEvent()
  1997.         if event == "peripheral" or event == "peripheral_detach" then
  1998.             return
  1999.         end
  2000.     end
  2001. end
  2002.  
  2003. local function pnp_checkOnce()
  2004.     for i,v in ipairs(rs.getSides()) do
  2005.         if peripheral.isPresent(v) then
  2006.             runPNPFunc(peripheral.getType(v), "exit")
  2007.             os.unloadAPI(peripheral.getType(v))
  2008.         end
  2009.     end
  2010.     peripherals = {}
  2011.     for index,side in ipairs(rs.getSides()) do
  2012.         if peripheral.isPresent(side) then
  2013.             if loadPeripheral(peripheral.getType(side)) then
  2014.                 table.insert(peripherals, getPNPFunc(peripheral.getType(side), "initialize", "\""..side.."\""))
  2015.             end
  2016.         end
  2017.     end
  2018.     parallel.waitForAny(pnp_waitForPeripheralEvent, parallel.waitForAll(unpack(peripherals)))
  2019. end
  2020.  
  2021. term.clear()
  2022. term.setCursorPos(1,1)
  2023. if #fs.list(userBase) == 0 then
  2024.     printCentered("Hello.")
  2025.     printCentered("Welcome to "..version..".")
  2026.     printCentered("We'll need you to make a root account")
  2027.     printCentered("Before you can go on.")
  2028.     os.sleep(2)
  2029.     os.addNewUser_noDialog("root", makeRootAcct())
  2030.     os.reboot()
  2031. end
  2032.  
  2033. local function readCommands()
  2034.     local commandHistory = {}
  2035.     while true do
  2036.         write(os.getCurrentUser().."@")
  2037.         if os.getComputerLabel() == nil then
  2038.             write(tostring(os.getComputerID()))
  2039.         else
  2040.             write(os.getComputerLabel())
  2041.         end
  2042.         write(":")
  2043.         if os.getCurrentUser() == "root" then
  2044.             if shell.dir() == "" then
  2045.                 write("-# ")
  2046.             else
  2047.                 write("/"..shell.dir().."# ")
  2048.             end
  2049.         else
  2050.             if shell.dir() == "" then
  2051.                 write("-$ ")
  2052.             else
  2053.                 write("/"..shell.dir().."$ ")
  2054.             end
  2055.         end
  2056.         local input = read(nil, commandHistory)
  2057.         table.insert(commandHistory, input)
  2058.         local input_words = {}
  2059.         for i in input:gmatch("%S+") do
  2060.             table.insert(input_words, i)
  2061.         end
  2062.         if input_words[1] ~= nil then
  2063.             os.pullEvent = oldOSPullEvent
  2064.             shell.run(input_words[1], unpack(input_words, 2))
  2065.             os.pullEvent = oldOSPullEventRaw
  2066.         end
  2067.         os.sleep(0)
  2068.     end
  2069. end
  2070.  
  2071. if os.switchUser(os.retrieveCredentials()) then
  2072.     term.clear()
  2073.     term.setCursorPos(1,1)
  2074.     shell.setPath(shell.path()..":/bin")
  2075.     parallel.waitForAll(readCommands, function() while true do pnp_checkOnce()  end end)
  2076. end
  2077. ]=]
  2078.  
  2079. sysFiles["startup"] = [[
  2080. local oldTermSetTextColor = term.setTextColor
  2081. local oldTermSetBGColor = term.setBackgroundColor
  2082.  
  2083. if not term.isColor or not term.isColor() then
  2084.     term.setTextColor = function() oldTermSetTextColor(colors.white) end
  2085.     term.setBackgroundColor = function() oldTermSetTextColor(colors.black) end
  2086. end
  2087.  
  2088. if not fs.exists(".os/rpc") then
  2089.     local handle = http.get("http://pastebin.com/raw.php?i=AJEnqBtA")
  2090.     local data = handle.readAll()
  2091.     handle.close()
  2092.     local file = fs.open(".os/rpc", "w")
  2093.     file.write(data)
  2094.     file.close()
  2095. end
  2096.  
  2097. os.loadAPI(".os/rpc")
  2098.  
  2099. if turtle and fs.exists(".os/allowTurtleRPC") then
  2100.     rpc.new("forward", "return turtle.forward()")
  2101.     rpc.new("turnLeft", "return turtle.turnLeft()")
  2102.     rpc.new("turnRight", "return turtle.turnRight()")
  2103.     rpc.new("back", "return turtle.back()")
  2104.     rpc.new("up", "return turtle.up()")
  2105.     rpc.new("down", "return turtle.down()")
  2106.     rpc.new("dig", "return turtle.dig()")
  2107.     rpc.new("digUp", "return turtle.digUp()")
  2108.     rpc.new("digDown", "return turtle.digDown()")
  2109.     rpc.new("place", "local args = {...} return turtle.place(args[1])")
  2110.     rpc.new("placeUp", "return turtle.placeUp()")
  2111.     rpc.new("placeDown", "return turtle.placeDown()")
  2112.     rpc.new("detect", "return turtle.detect()")
  2113.     rpc.new("detectUp", "return turtle.detectUp()")
  2114.     rpc.new("detectDown", "return turtle.detectDown()")
  2115.     rpc.new("compare", "return turtle.compare()")
  2116.     rpc.new("compareTo", "local args = {...} return turtle.compareTo(tonumber(args[1]))")
  2117.     rpc.new("compareUp", "return turtle.compareUp()")
  2118.     rpc.new("compareDown", "return turtle.compareDown()")
  2119.     rpc.new("suck", "return turtle.suck()")
  2120.     rpc.new("suckUp", "return turtle.suckUp()")
  2121.     rpc.new("suckDown", "return turtle.suckDown()")
  2122.     rpc.new("drop", "return turtle.drop()")
  2123.     rpc.new("craft", "return turtle.craft()")
  2124.     rpc.new("dropUp", "return turtle.dropUp()")
  2125.     rpc.new("dropDown", "return turtle.dropDown()")
  2126.     rpc.new("select", "local args = {...} return turtle.select(tonumber(args[1]))")
  2127.     rpc.new("getItemCount", "local args = {...} return turtle.getItemCount(tonumber(args[1]))")
  2128.     rpc.new("getItemSpace", "local args = {...} return turtle.getItemSpace(tonumber(args[1]))")
  2129.     rpc.new("refuel", "local args = {...} return turtle.refuel(tonumber(args[1]))")
  2130.     rpc.new("transferTo", "local args = {...} return turtle.transferTo(tonumber(args[1]), tonumber(args[2]))")
  2131.     rpc.new("getFuelLevel", "return turtle.getFuelLevel()")
  2132. end
  2133.  
  2134. shell.run(".os/OS")
  2135.  
  2136. term.setTextColor = oldTermSetTextColor
  2137. term.setBackgroundColor = oldTermSetBGColor
  2138. ]]
  2139.  
  2140. local osBase = ".os/"
  2141. local fileMetadataBase = osBase.."metadata/"
  2142. local permBase = fileMetadataBase.."perms/"
  2143. local ownerBase = fileMetadataBase.."owners/"
  2144. local userBase = osBase.."users/"
  2145. local logBase = osBase.."logs/"
  2146. local PNPDriverBase = osBase.."PNP/"
  2147.  
  2148. fs.makeDir(permBase)
  2149. fs.makeDir(ownerBase)
  2150. fs.makeDir(userBase)
  2151. fs.makeDir(logBase)
  2152. fs.makeDir(PNPDriverBase)
  2153. fs.makeDir("bin")
  2154.  
  2155. for i,v in pairs(programs) do
  2156.     if i ~= "turtleSettings" then
  2157.         local handle = fs.open("bin/"..i, "w")
  2158.         handle.write(v)
  2159.         handle.close()
  2160.     elseif turtle then
  2161.         local handle = fs.open("bin/"..i, "w")
  2162.         handle.write(v)
  2163.         handle.close()
  2164.     end
  2165. end
  2166.  
  2167. for i,v in pairs(drivers) do
  2168.     local handle = fs.open(fs.combine(PNPDriverBase, i), "w")
  2169.     handle.write(v)
  2170.     handle.close()
  2171. end
  2172.  
  2173. for i,v in pairs(sysFiles) do
  2174.     local handle = fs.open(i, "w")
  2175.     handle.write(v)
  2176.     handle.close()
  2177. end
  2178.  
  2179. fs.delete(shell.resolveProgram(shell.getRunningProgram()))
  2180. os.reboot()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement