Advertisement
Tatantyler

MuSeOS

Sep 13th, 2012
616
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 23.01 KB | None | 0 0
  1. local oldOSPullEvent = os.pullEvent
  2. local oldOSPullEventRaw = os.pullEventRaw
  3. local protectedFolders = {}
  4. local version = "MuSeOS v8.1"
  5. local debugMode = false
  6.  
  7. os.pullEvent = oldOSPullEventRaw
  8.  
  9. local currentUser = "root"
  10.  
  11. local logo = function(startX, startY)
  12.     term.clear()
  13.     term.setCursorPos(startX,startY)
  14.     term.write(" #### ####        ######        ##### ######")
  15.     term.setCursorPos(startX,startY+1)
  16.     term.write("#    #    #       #             #   # #     ")
  17.     term.setCursorPos(startX,startY+2)
  18.     term.write("#    #    # #  #  #       ####  #   # #     ")
  19.     term.setCursorPos(startX,startY+3)
  20.     term.write("#    #    # #  #  ######  #  #  #   # ######")
  21.     term.setCursorPos(startX,startY+4)
  22.     term.write("#    #    # #  #       #  ####  #   #      #")
  23.     term.setCursorPos(startX,startY+5)
  24.     term.write("#    #    # #  #       #  #     #   #      #")
  25.     term.setCursorPos(startX,startY+6)
  26.     term.write("#    #    # ####  ######  ####  ##### ######")
  27.     term.setCursorPos(startX,startY+7)
  28. end
  29.  
  30. local turtleLogo = function(startX, startY)
  31.   term.clear()
  32.   term.setCursorPos(startX,startY+1)
  33.   term.write("  #####      ######  ##   ###### ######")
  34.   term.setCursorPos(startX,startY+2)
  35.   term.write(" #  #  #     #      #  #  #    # #     ")
  36.   term.setCursorPos(startX,startY+3)
  37.   term.write(" #  #  # # # ###### ####  #    # ######")
  38.   term.setCursorPos(startX,startY+4)
  39.   term.write(" #  #  # # #      # #     #    #      #")
  40.   term.setCursorPos(startX,startY+5)
  41.   term.write(" #  #  # ### ###### ####  ###### ######")
  42.   term.setCursorPos(startX,startY+6)
  43. end
  44.  
  45.  
  46. local function getUserInfo()
  47.     local oldUser = currentUser
  48.     currentUser = "root"
  49.     local users = {}
  50.     local passwords = {}
  51.     local ids = {}
  52.    
  53.     for i,v in ipairs(fs.list("os/users")) do
  54.         local userHandle = fs.open("os/users/"..v, "r")
  55.         local password = userHandle.readLine()
  56.         table.insert(users, fs.getName(v))
  57.         table.insert(passwords, password)
  58.         if v == root then
  59.             table.insert(ids, 0)
  60.         else
  61.             table.insert(ids, i)
  62.         end
  63.         if not fs.exists(v) then
  64.             fs.makeDir(v)
  65.         end
  66.         userHandle.close()
  67.     end
  68.     currentUser = oldUser
  69.     return users, passwords, ids
  70. end
  71.  
  72.  
  73. local users, passwords, ids = getUserInfo()
  74.  
  75. local function checkUsers(users, passwords, username, password)
  76.     for i,v in ipairs(users) do
  77.         if username == v then
  78.             if passwords[i] == password then
  79.                 return true
  80.             else
  81.                 return false
  82.             end
  83.         end
  84.     end
  85.     return false
  86. end
  87.  
  88.  
  89. local function loadWrite(toPrint)
  90.     if turtle then
  91.         turtleLogo(1,2)
  92.     else
  93.         logo(4,5)
  94.     end
  95.     io.write(toPrint)
  96. end
  97.  
  98. local function retrieve(code, file)
  99.     local url = "http://pastebin.com/raw.php?i="..code
  100.     local urlHandle = http.get(url)
  101.     if urlHandle == nil then
  102.         return nil
  103.     end
  104.     if file ~= nil then
  105.         local fileHandle = fs.open(file, "w")
  106.         fileHandle.write(urlHandle.readAll())
  107.         fileHandle.close()
  108.         urlHandle.close()
  109.         return true
  110.     else
  111.         local data = urlHandle.readAll()
  112.         urlHandle.close()
  113.         return data
  114.     end
  115. end
  116.  
  117.  
  118. loadWrite("Loading OS...")
  119.  
  120. loadWrite("Checking for updates...")
  121. local versionHandle = http.get("http://pastebin.com/raw.php?i=j8qj82d1")
  122. local latestVersion = versionHandle.readAll()
  123. versionHandle.close()
  124.  
  125. if fs.exists("os/version") then
  126.     local versionHandle = fs.open("os/version", "r")
  127.     local currentVersion = versionHandle.readAll()
  128.     versionHandle.close()
  129.    
  130.     currentVersion = tonumber(currentVersion)
  131.     latestVersion = tonumber(latestVersion)
  132.    
  133.     if latestVersion > currentVersion then
  134.         local versionHandle = fs.open("os/version", "w")
  135.         versionHandle.write(tostring(latestVersion))
  136.         versionHandle.close()
  137.         loadWrite("There is a new version available!")
  138.         fs.delete("MuSeOS")
  139.         fs.delete("bin/man")
  140.  
  141.         retrieve("fquZgJ5p", "MuSeOS")
  142.         retrieve("G33dymxf", "programInstaller")
  143.         retrieve("wfeGgwQH", "bin/man")
  144.  
  145.         shell.run("programInstaller")
  146.         fs.delete("programInstaller")
  147.         os.reboot()
  148.     end
  149. else
  150.     loadWrite("No version file found, skipping...")
  151.     local versionHandle = fs.open("os/version", "w")
  152.     versionHandle.write(latestVersion)
  153.     versionHandle.close()
  154. end
  155.  
  156. loadWrite("Installing security hooks...")
  157.  
  158. local oldFSOpen = fs.open
  159.  
  160. local function isSuperuser()
  161.     if currentUser == "root" then
  162.         return true
  163.     else
  164.         return false
  165.     end
  166. end
  167.  
  168. local function writeToLog(logFile, text)
  169.     local logHandle = oldFSOpen("os/logs/"..logFile, "a")
  170.     logHandle.writeLine("["..currentUser.."] "..text)
  171.     logHandle.close()
  172. end
  173.  
  174. os.sleep(1)
  175.  
  176. loadWrite("Step 1: Installing FS hooks...")
  177.  
  178. local protectedFolderList = fs.open("os/protected", "r")
  179.  
  180. while true do
  181.     local line = protectedFolderList.readLine()
  182.     if line == nil then
  183.         break
  184.     else
  185.         table.insert(protectedFolders, line)
  186.     end
  187. end
  188.  
  189. protectedFolderList.close()
  190.  
  191. local function getBasePath(file)
  192.     local location1, location2 = string.find(file, fs.getName(file))
  193.     location1 = location1-1
  194.     return string.sub(file,1,location1)
  195. end
  196.  
  197. local function isInHomeFolder(file)
  198.     local location1, location2 = string.find(file, "home/"..currentUser.."/")
  199.     if location1 == 1 then
  200.         return true
  201.     else
  202.         return false
  203.     end
  204. end
  205.  
  206. function checkInFolder(dir, file)
  207.     for _,v in ipairs(fs.list(dir)) do
  208.         if fs.getName(v) == file then
  209.             if not isSuperuser() then
  210.                 return true
  211.             else
  212.                 return false
  213.             end
  214.         end
  215.     end
  216. end
  217.  
  218. local function debugprint(text)
  219.     if debugMode then
  220.         print(text)
  221.     end
  222. end
  223.  
  224. local function pathIsProtected(file)
  225.     fileName = fs.getName(file)
  226.     if isSuperuser() then
  227.         return false -- root sees all
  228.     elseif fileName == "startup" then
  229.         return true
  230.     elseif checkInFolder("os", fileName) or getBasePath(file) == "os/" then
  231.         return true
  232.     elseif checkInFolder("os/logs", fileName) or getBasePath(file) == "os/logs/" then
  233.         return true
  234.     elseif checkInFolder("os/users", fileName) or getBasePath(file) == "os/users/" then
  235.         return true
  236.     elseif checkInFolder("os/userSettings/", fileName) or getBasePath(file) == "os/userSettings/" then
  237.         return true -- user settings folder
  238.     elseif checkInFolder("os/userSettings/"..currentUser) or getBasePath(file) == "os/userSettings/"..currentUser.."/" then
  239.         return false -- current user's settings
  240.     elseif checkInFolder(currentUser, fileName) or getBasePath(file) == currentUser.."/" then
  241.         return false
  242.     elseif fileName == "MuSeOS" then
  243.         return true
  244.     else
  245.         for i,v in ipairs(protectedFolders) do
  246.             if getBasePath(file) == v then
  247.                 return true
  248.             end
  249.             if checkInFolder(v, fileName) then
  250.                 return true
  251.             end
  252.         end
  253.         return false
  254.     end
  255.    
  256.     return false
  257. end
  258.  
  259. local oldFSDelete = fs.delete
  260. local oldFSMakeDir = fs.makeDir
  261. local oldFSCopy = fs.copy
  262. local oldFSMove = fs.move
  263. local oldFSIsReadOnly = fs.isReadOnly
  264.  
  265. fs.open = function(file, mode)
  266.     if pathIsProtected(file) then
  267.         print("ERROR: Not priviledged enough to modify protected folder!")
  268.         writeToLog("OSLog", "Attempted to access protected folder!")
  269.         return nil
  270.     end
  271.     writeToLog("fileLog", "Opened file:"..file.." with mode "..mode..".")
  272.     return oldFSOpen(file,mode)
  273. end
  274.  
  275. fs.delete = function(file)
  276.     if file == "os" then -- idiot proofing
  277.         print("ERROR: Cannot delete OS folder!")
  278.         writeToLog("OSLog", "Attempted to delete OS folder!")
  279.         return false
  280.     end
  281.     if pathIsProtected(file) then
  282.         print("ERROR: Not priviledged enough to modify protected folder!")
  283.         writeToLog("OSLog", "Attempted to access protected folder!")
  284.         return false
  285.     end
  286.     writeToLog("fileLog", "Deleted file:"..file)
  287.     oldFSDelete(file)
  288. end
  289.  
  290. fs.makeDir = function(dir)
  291.     writeToLog("fileLog", "Made directory:"..dir..".")
  292.     oldFSMakeDir(dir)
  293. end
  294.  
  295. fs.move = function(fromPath, toPath)
  296.     if pathIsProtected(fromPath) or pathIsProtected(toPath) then
  297.         print("ERROR: Not priviledged enough to modify protected folder!")
  298.         writeToLog("OSLog", "Attempted to access protected folder!")
  299.         return false
  300.     end
  301.     writeToLog("fileLog", "Moved file:"..fromPath.." to "..toPath..".")
  302.     oldFSMove(fromPath, toPath)
  303. end
  304.  
  305. fs.move = function(fromPath, toPath)
  306.     if pathIsProtected(fromPath) or pathIsProtected(toPath) then
  307.         print("ERROR: Not priviledged enough to modify protected folder!")
  308.         writeToLog("OSLog", "Attempted to access protected folder!")
  309.         return false
  310.     end
  311.     writeToLog("fileLog", "Moved file:"..fromPath.." to "..toPath..".")
  312.     oldFSCopy(fromPath, toPath)
  313. end
  314.  
  315. fs.isReadOnly = function(file)
  316.     if pathIsProtected(file) and not currentUser == "root" then
  317.         return true
  318.     else
  319.         return oldFSIsReadOnly(file)
  320.     end
  321. end
  322.  
  323. loadWrite("Step 2: Installing RS Hooks...")
  324.  
  325. local oldRSSetOutput = rs.setOutput
  326. local oldRSSetBundledOutput = rs.setBundledOutput
  327.  
  328. rs.setOutput = function(side, value)
  329.     writeToLog("RSLog", "Set redstone output on "..side.." to "..tostring(value)..".")
  330.     return oldRSSetOutput(side, value)
  331. end
  332.  
  333. rs.setBundledOutput = function(side, value)
  334.     writeToLog("RSLog", "Set bundled redstone output on "..side.." to "..tostring(value)..".")
  335.     return oldRSSetBundledOutput(side, value)
  336. end
  337.  
  338. redstone.setOutput = rs.setOutput
  339. redstone.setBundledOutput = rs.setBundledOutput
  340.  
  341. if not http then
  342.     loadWrite("HTTP disabled, not installing hooks.")
  343. else
  344.     loadWrite("Step 3: Installing HTTP hooks...")
  345.    
  346.     local oldHTTPRequest = http.request
  347.     local oldHTTPGet = http.get
  348.     local oldHTTPPost = http.post
  349.    
  350.     http.request = function(url, postData)
  351.         if postData == nil then
  352.             writeToLog("HTTPLog", "Request sent to "..url..", no data sent.")
  353.         else
  354.             writeToLog("HTTPLog", "Request sent to "..url..", data:"..postData)
  355.         end
  356.         return oldHTTPRequest(url, postData)
  357.     end
  358.    
  359.     http.get = function(url)
  360.         writeToLog("HTTPLog", "Fetched data from "..url..".")
  361.         return oldHTTPGet(url)
  362.     end
  363.    
  364.     http.post = function(url, postData)
  365.         writeToLog("HTTPLog", "Sent data:"..postData.." to url:"..url..".")
  366.         return oldHTTPPost(url, postData)
  367.     end
  368.     print("Done.")
  369. end
  370.  
  371. os.version = function()
  372.     return version
  373. end
  374.  
  375. loadWrite("Step 4: Installing shell hooks and new functions...")
  376.  
  377. local oldShellRun = shell.run
  378.  
  379. local function existsInFolder(dir, file)
  380.     for _,v in ipairs(fs.list(dir)) do
  381.         if v == file then
  382.             return true -- found a match in the toplevel dir
  383.         end
  384.     end
  385. end
  386.  
  387.  
  388. shell.run = function(program, ...)
  389.     writeToLog("OSLog", "shell.run called, program:"..program)
  390.     if shell.resolveProgram(program) == nil then
  391.         oldShellRun(fs.combine("bin/", program), unpack(arg))
  392.     else
  393.         oldShellRun(program, unpack(arg))
  394.     end
  395. end
  396.  
  397. loadWrite("Step 5: Installing Rednet Hooks...")
  398.  
  399. local oldRednetSend = rednet.send
  400. local oldRednetBroadcast = rednet.broadcast
  401. local oldRednetAnnounce = rednet.announce
  402.  
  403. rednet.send = function(senderID, message)
  404.     writeToLog("RednetLog", "Sent message to id "..senderID.." with message "..message)
  405.     return oldRednetSend(senderID, message)
  406. end
  407.  
  408. rednet.broadcast = function(message)
  409.     writeToLog("RednetLog", "Broadcasted message "..message)
  410.     return oldRednetBroadcast(message)
  411. end
  412.  
  413. rednet.announce = function()
  414.     writeToLog("RednetLog", "Sent announce message.")
  415.     return oldRednetAnnounce()
  416. end
  417.  
  418. loadWrite("Step 6: Installing OS Hooks...")
  419.  
  420. local oldOSShutdown = os.shutdown
  421. local oldOSReboot = os.reboot
  422. local oldOSLoadAPI = os.loadAPI
  423. local oldOSSetLabel = os.setComputerLabel
  424.  
  425. os.shutdown = function()
  426.     writeToLog("OSLog", "Shutting down MuSeOS. Uptime:"..tostring(os.clock()))
  427.     oldOSShutdown()
  428. end
  429.  
  430. os.reboot = function()
  431.     writeToLog("OSLog", "Rebooting MuSeOS. Uptime:"..tostring(os.clock()))
  432.     oldOSReboot()
  433. end
  434.  
  435. os.loadAPI = function(api)
  436.     writeToLog("OSLog", "Loading API: "..api)
  437.     oldOSLoadAPI(api)
  438. end
  439.  
  440. os.setComputerLabel = function(label)
  441.     writeToLog("OSLog", "Setting computer label to:"..label)
  442.     oldOSSetLabel(label)
  443. end
  444.  
  445. if turtle then
  446.  
  447.     loadWrite("Step 7: Installing Turtle API hooks...")
  448.    
  449.     local settingsFile = fs.open("os/turtle", "r")
  450.     local canMove = settingsFile.readLine()
  451.     local canDig = settingsFile.readLine()
  452.     local canPlace = settingsFile.readLine()
  453.     local canSuck = settingsFile.readLine()
  454.     settingsFile.close()
  455.    
  456.     local oldTurtleDig = turtle.dig
  457.     local oldTurtleDigUp = turtle.digUp
  458.     local oldTurtleDigDown = turtle.digDown
  459.     local oldTurtlePlaceDown = turtle.placeDown
  460.     local oldTurtlePlaceUp = turtle.placeUp
  461.     local oldTurtlePlace = turtle.place
  462.     local oldTurtleForward = turtle.forward
  463.     local oldTurtleBack = turtle.back
  464.     local oldTurtleTurnLeft = turtle.turnLeft
  465.     local oldTurtleTurnRight = turtle.turnRight
  466.     local oldTurtleUp = turtle.up
  467.     local oldTurtleDown = turtle.down
  468.     local oldTurtleSuck = turtle.suck
  469.     local oldTurtleSuckUp = turtle.suckUp
  470.     local oldTurtleSuckDown = turtle.suckDown
  471.     local oldTurtleRefuel = turtle.refuel
  472.    
  473.     turtle.dig = function()
  474.         if canDig == "true" or currentUser == "root" then
  475.             writeToLog("TurtleLog", "Broke block in front.")
  476.             return oldTurtleDig()
  477.         else
  478.             writeToLog("TurtleLog", "Tried to dig, but digging has been diabled.")
  479.             return false
  480.         end
  481.     end
  482.    
  483.     turtle.digUp = function()
  484.         if canDig == "true" or currentUser == "root" then
  485.             writeToLog("TurtleLog", "Broke block on top.")
  486.             return oldTurtleDigUp()
  487.         else
  488.             writeToLog("TurtleLog", "Tried to dig, but digging has been diabled.")
  489.             return false
  490.         end
  491.     end
  492.    
  493.     turtle.digDown = function()
  494.         if canDig == "true" or currentUser == "root" then
  495.             writeToLog("TurtleLog", "Broke block under.")
  496.             return oldTurtleDigDown()
  497.         else
  498.             writeToLog("TurtleLog", "Tried to dig, but digging has been diabled.")
  499.             return false
  500.         end
  501.     end
  502.    
  503.    
  504.     turtle.place = function()
  505.         if canPlace == "true" or currentUser == "root" then
  506.             writeToLog("TurtleLog", "Placed block in front.")
  507.             return oldTurtlePlace()
  508.         else
  509.             writeToLog("TurtleLog", "Tried to place, but placing has been diabled.")
  510.             return false
  511.         end
  512.     end
  513.    
  514.     turtle.placeDown = function()
  515.         if canPlace == "true" or currentUser == "root" then
  516.             writeToLog("TurtleLog", "Placed block under.")
  517.             return oldTurtlePlaceDown()
  518.         else
  519.             writeToLog("TurtleLog", "Tried to place, but placing has been diabled.")
  520.             return false
  521.         end
  522.     end
  523.    
  524.     turtle.placeUp = function()
  525.         if canPlace == "true" or currentUser == "root" then
  526.             writeToLog("TurtleLog", "Placed block on top.")
  527.             return oldTurtlePlaceUp()
  528.         else
  529.             writeToLog("TurtleLog", "Tried to place, but placing has been diabled.")
  530.             return false
  531.         end
  532.     end
  533.    
  534.     turtle.forward = function()
  535.         if canMove == "true" or currentUser == "root" then
  536.             writeToLog("TurtleLog", "Moved forward.")
  537.             return oldTurtleForward()
  538.         else
  539.             writeToLog("TurtleLog", "Tried to move, but movement has been diabled.")
  540.             return false
  541.         end
  542.     end
  543.    
  544.     turtle.back = function()
  545.         if canMove == "true" or currentUser == "root" then
  546.             writeToLog("TurtleLog", "Moved backwards.")
  547.             return oldTurtleBack()
  548.         else
  549.             writeToLog("TurtleLog", "Tried to move, but movement has been diabled.")
  550.             return false
  551.         end
  552.     end
  553.    
  554.     turtle.turnLeft = function()
  555.         if canMove == "true" or currentUser == "root" then
  556.             writeToLog("TurtleLog", "Turned left.")
  557.             return oldTurtleTurnLeft()
  558.         else
  559.             writeToLog("TurtleLog", "Tried to move, but movement has been diabled.")
  560.             return false
  561.         end
  562.     end
  563.    
  564.     turtle.up = function()
  565.         if canMove == "true" or currentUser == "root" then
  566.             writeToLog("TurtleLog", "Went up.")
  567.             return oldTurtleUp()
  568.         else
  569.             writeToLog("TurtleLog", "Tried to move, but movement has been diabled.")
  570.             return false
  571.         end
  572.     end
  573.    
  574.    
  575.     turtle.down = function()
  576.         if canMove == "true" or currentUser == "root" then
  577.             writeToLog("TurtleLog", "Went down.")
  578.             return oldTurtleDown()
  579.         else
  580.             writeToLog("TurtleLog", "Tried to move, but movement has been diabled.")
  581.             return false
  582.         end
  583.     end
  584.    
  585.    
  586.     turtle.turnRight = function()
  587.         if canMove == "true" or currentUser == "root" then
  588.             writeToLog("TurtleLog", "Turned right.")
  589.             return oldTurtleTurnRight()
  590.         else
  591.             writeToLog("TurtleLog", "Tried to move, but movement has been diabled.")
  592.             return false
  593.         end
  594.     end
  595.    
  596.     turtle.suck = function()
  597.         if canSuck == "true" or currentUser == "root" then
  598.             writeToLog("TurtleLog", "Retrieved items.")
  599.             return oldTurtleSuck()
  600.         else
  601.             writeToLog("TurtleLog", "Tried to retrieve items, but item retrieval has been diabled.")
  602.             return false
  603.         end
  604.     end
  605.    
  606.     turtle.suckUp = function()
  607.         if canSuck == "true" or currentUser == "root" then
  608.             writeToLog("TurtleLog", "Retrieved items from above.")
  609.             return oldTurtleSuckUp()
  610.         else
  611.             writeToLog("TurtleLog", "Tried to retrieve items, but item retrieval has been diabled.")
  612.             return false
  613.         end
  614.     end
  615.    
  616.     turtle.suckDown = function()
  617.         if canSuck == "true" or currentUser == "root" then
  618.             writeToLog("TurtleLog", "Retrieved items from underneath.")
  619.             return oldTurtleSuckDown()
  620.         else
  621.             writeToLog("TurtleLog", "Tried to retrieve items, but item retrieval has been diabled.")
  622.             return false
  623.         end
  624.     end
  625.    
  626.     turtle.refuel = function()
  627.         writeToLog("TurtleLog", "Refueled turtle, current fuel level is: "..turtle.getFuelLevel())
  628.         return oldTurtleRefuel()
  629.     end
  630. end
  631. loadWrite("Loaded OS!")
  632.  
  633. loadWrite("Loading usernames/passwords...")
  634.  
  635. museOS = {}
  636.  
  637. museOS.switchUser = function(password, user)
  638.     local users, passwords = getUserInfo()
  639.     if checkUsers(users,passwords,user,password) then
  640.         writeToLog("OSLog", "User "..currentUser.." has switched to account "..user)
  641.         currentUser = user
  642.         return true
  643.     else
  644.         writeToLog("OSLog", "User "..currentUser.." attempted to switch to account "..user.." but the password was incorrect or the target user did not exist.")
  645.         print("ERROR: User switch failed: target user does not exist/incorrect password!")
  646.         return false
  647.     end
  648. end
  649.  
  650. museOS.sudo = function(password, program, ...)
  651.     local users, passwords = getUserInfo()
  652.     if checkUsers(users, passwords, "root", password) then
  653.         writeToLog("OSLog", "sudo successful, raising to root...")
  654.         local oldUser = currentUser
  655.         currentUser = "root"
  656.         shell.run(program, unpack(arg))
  657.         currentUser = oldUser
  658.     else
  659.         writeToLog("OSLog", "sudo attempt failed!")
  660.         print("ERROR: sudo attempt failed!")
  661.     end
  662. end
  663.  
  664. museOS.getUserID = function()
  665.     local users, passwords, ids = getUserInfo()
  666.     for i,v in ipairs(users) do
  667.         if museOS.getCurrentUser() == v then
  668.             return ids[i]
  669.         end
  670.     end
  671. end
  672.  
  673. museOS.authenticate = function(user, password)
  674.     writeToLog("OSLog", "Authentication request for user "..user..".")
  675.     local users, passwords, ids = getUserInfo()
  676.     return checkUsers(users,passwords,user,password)
  677. end
  678.  
  679. museOS.getUsers = function()
  680.     writeToLog("OSLog", "Userlist requested.")
  681.     local users, passwords, ids = getUserInfo()
  682.     return users, ids
  683. end
  684.  
  685. museOS.getCurrentUser = function()
  686.     return currentUser
  687. end
  688.  
  689. local function showLoginMenu()
  690.     local function readAdv(maxChars, startX, startY, replaceChar, returnToStart, backgroundChar)
  691.         term.setCursorBlink(true)
  692.         local function clearArea(clearLine, clearX, clearArea, replaceChar)
  693.             local formerX,formerY = term.getCursorPos()
  694.             for i=0, clearArea do
  695.                 term.setCursorPos(clearX+i, clearLine)
  696.                 if replaceChar ~= nil then
  697.                     io.write(replaceChar)
  698.                 else
  699.                     io.write(" ")
  700.                 end
  701.             end
  702.             term.setCursorPos(formerX,formerY)
  703.         end
  704.        
  705.         local x,y = term.getCursorPos()
  706.        
  707.         term.setCursorPos(startX, startY)
  708.         clearArea(startY, startX, maxChars, backgroundChar)
  709.        
  710.         local input = ""
  711.         while true do
  712.             local event, key = os.pullEvent()
  713.             if event == "key" then
  714.                 if key == 28 or key == 156 then
  715.                     if returnToStart then
  716.                         term.setCursorPos(x,y)
  717.                     end
  718.                     return input
  719.                 elseif key == 14 then
  720.                     input = string.sub(input, 1, #input-1)
  721.                     term.setCursorPos(startX, startY)
  722.                     clearArea(startY, startX, maxChars, backgroundChar)
  723.                     if replaceChar == nil then
  724.                         io.write(input)
  725.                     else
  726.                         for i=1, #input do
  727.                             io.write(replaceChar)
  728.                         end
  729.                     end
  730.                 end
  731.             elseif event == "char" then
  732.                 if maxChars ~= nil then
  733.                     if #input <= maxChars then
  734.                         input = input..key
  735.                         term.setCursorPos(startX, startY)
  736.                         clearArea(startY, startX, maxChars, backgroundChar)
  737.                         if replaceChar == nil then
  738.                             io.write(input)
  739.                         else
  740.                             for i=1, #input do
  741.                                 io.write(replaceChar)
  742.                             end
  743.                         end
  744.                     end
  745.                 else
  746.                     input = input..key
  747.                     term.setCursorPos(startX, startY)
  748.                     clearArea(startY, startX, maxChars, backgroundChar)
  749.                     if replaceChar == nil then
  750.                         io.write(input)
  751.                     else
  752.                         for i=1, #input do
  753.                             io.write(replaceChar)
  754.                         end
  755.                     end
  756.                 end
  757.             end
  758.         end
  759.     end
  760.  
  761.     local function printLoginScreen(startX, startY)
  762.         term.setCursorPos(startX, startY)
  763.         print("===========================")
  764.         term.setCursorPos(startX, startY+1)
  765.         print("|      Please log in:     |")
  766.         term.setCursorPos(startX, startY+2)
  767.         print("===========================")
  768.         term.setCursorPos(startX, startY+3)
  769.         print("|        Username:        |")
  770.         term.setCursorPos(startX, startY+4)
  771.         print("|                         |")
  772.         term.setCursorPos(startX, startY+5)
  773.         print("===========================")
  774.         term.setCursorPos(startX, startY+6)
  775.         print("|        Password:        |")
  776.         term.setCursorPos(startX, startY+7)
  777.         print("|                         |")
  778.         term.setCursorPos(startX, startY+8)
  779.         print("===========================")
  780.        
  781.     end
  782.  
  783.     --LOGINSCRN SPECS:
  784.     --XLen: 27
  785.     --YLen: 9
  786.     --Textbox Space: 26
  787.     --Username XStart:2
  788.     --Username YStart:5
  789.     --Password XStart:2
  790.     --Password YStart:8
  791.  
  792.     term.clear()
  793.     printLoginScreen(13,4)
  794.     username = readAdv(24, 14, 8, nil, true, nil)
  795.     password = readAdv(24, 14, 11, "#", true, nil)
  796.     return username, password
  797. end
  798.  
  799. term.clear()
  800. term.setCursorPos(1,1)
  801. loadWrite("Loading APIs...")
  802. for _,v in ipairs(fs.list("apis/")) do
  803.     loadWrite("Loading api:"..v)
  804.     os.loadAPI("apis/"..v)
  805. end
  806.  
  807. local username, password = showLoginMenu()
  808. local successful = museOS.authenticate(username, password)
  809.  
  810. if successful then
  811.     writeToLog("OSLog", "User "..username.." has successfully logged in.")
  812.     term.clear()
  813.     term.setCursorPos(1,1)
  814.     currentUser = username
  815.     print(os.version())
  816.     for _,v in ipairs(fs.list("modules/")) do
  817.         shell.run("modules/"..v)
  818.     end
  819.     shell.setDir(currentUser)
  820.     shell.setPath(shell.path()..":/bin")
  821. else
  822.     writeToLog("OSLog", "An attempt was made to log into "..username..", but the password was incorrect, or the user was not found.")
  823.     print("Incorrect password or username. Shutting down...")
  824.     os.sleep(2)
  825.     os.shutdown()
  826. end
  827.  
  828. local function readCommands()
  829.     local commandHistory = {}
  830.     while true do
  831.         write(museOS.getCurrentUser().."@")
  832.         if os.getComputerLabel() == nil then
  833.             write(tostring(os.getComputerID()))
  834.         else
  835.             write(os.getComputerLabel())
  836.         end
  837.         write(":")
  838.         if museOS.getCurrentUser() == "root" then
  839.             if shell.dir() == "" then
  840.                 write("-# ")
  841.             else
  842.                 write("/"..shell.dir().."# ")
  843.             end
  844.         else
  845.             if shell.dir() == "" then
  846.                 write("-$ ")
  847.             else
  848.                 write("/"..shell.dir().."$ ")
  849.             end
  850.         end
  851.         local input = read(nil, commandHistory)
  852.         table.insert(commandHistory, input)
  853.         local input_words = {}
  854.         for i in input:gmatch("%S+") do
  855.             table.insert(input_words, i)
  856.         end
  857.         if input_words[1] ~= nil then
  858.             os.pullEvent = oldOSPullEvent
  859.             shell.run(input_words[1], unpack(input_words, 2))
  860.             os.pullEvent = oldOSPullEventRaw
  861.         end
  862.         os.sleep(0.1)
  863.     end
  864. end
  865.  
  866. readCommands()
  867.  
  868. os.shutdown()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement