Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- NSA logger by nuaoa.
- --save as 'startup'
- local directions = {"back","front","left","right","top","bottom"}
- local sensor
- local data = {}
- -- Makes a copy of a table
- function deepcopy(orig)
- local orig_type = type(orig)
- local copy
- if orig_type == 'table' then
- copy = {}
- for orig_key, orig_value in next, orig, nil do
- copy[deepcopy(orig_key)] = deepcopy(orig_value)
- end
- setmetatable(copy, deepcopy(getmetatable(orig)))
- else -- number, string, boolean, etc
- copy = orig
- end
- return copy
- end
- --Log stuff to a file.
- function Log(mes)
- local file
- local prev = ""
- if fs.exists("log.txt") then
- file = fs.open("log.txt","r")
- prev = file.readAll()
- file.close()
- else
- prev = "Theft Log".."\n"
- end
- local tab =sensor.getPlayerNames()
- local names = tostring(tab[1])
- if #tab>1 then
- for i=2,#tab do
- names = names..","..tab[i]
- end
- end
- file = fs.open("log.txt","w")
- file.write(prev.."\n")
- file.write("["..os.day()..","..os.time().."]["..names.."]"..mes)
- file.close()
- end
- --get a table of items in a chest.
- function getChestData()
- local p = peripheral.wrap("top")
- --p.condenseItems()
- local retTab = p.getAllStacks()
- --sometimes retTab is a number, tested this loop and found it runs only 3 times, so no idea whats going on.
- while type(retTab) ~= "table" do
- retTab = p.getAllStacks()
- end
- local retTab2 = {}
- for k,v in pairs(retTab) do
- if retTab2[v.name] == nil then
- retTab2[v.name]=deepcopy(v)
- else
- retTab2[v.name].qty = retTab2[v.name].qty +v.qty
- end
- end
- return deepcopy(retTab2)
- end
- function antiHack()
- while true do
- local event,side = os.pullEventRaw()
- if event == "peripheral" then
- if peripheral.getType(side) == "drive" then
- -- we got someone putting a drive next to the terminal
- elseif peripheral.getType(side) == "computer" then
- end
- elseif event == "disk" then
- if fs.exists("disk/startup") then
- fs.delete("disk/startup")
- disk.setLabel(side,"Broken Floppy")
- end
- end
- end
- end
- function lockTerminal()
- local last_key = 0
- while true do
- local pullEvent = os.pullEvent
- os.pullEvent = os.pullEventRaw
- local event,key = os.pullEventRaw()
- if event == "terminate" then
- Log("[Computer] Terminate attempt")
- elseif event == "key" then
- if last_key == 29 and key == 19 then --attempting a restart
- Log("[Computer] Restart")
- elseif last_key== 29 and key == 41 then -- ask for password
- Log("[Computer] Password screen accessed")
- term.write("Please enter the password: ")
- local pass = read("*")
- if pass == data.password then
- shell.run("edit","log.txt")
- term.clear()
- else
- Log("[Computer] Incorrect password attempt")
- print("Password accpeted: Opening secret door!")
- sleep(2)
- term.clear()
- end
- end
- last_key = key
- end
- end
- end
- function startup()
- term.clear()
- for i=1,#directions do
- if peripheral.getType(directions[i]) == "openperipheral_sensor" then
- sensor = peripheral.wrap(directions[i])
- break
- end
- end
- if not fs.exists("settings") then
- term.setCursorPos(1,1)
- Log("First Boot! Chest contains: "..textutils.serialize(getChestData()))
- print("First Boot!, lets setup some crap!")
- term.write("Password used to access logs: ")
- data["password"] = read()
- term.setCursorPos(1,3)
- term.write("Amount of seconds inbetween scanning for")
- term.setCursorPos(1,4)
- term.write("local players: ")
- data["scanUpdate"] = tonumber(read())
- term.setCursorPos(1,6)
- term.write("Setup finished. To access logs press 'Ctrl ~'.")
- term.setCursorPos(1,7)
- term.write("Press enter to restart ")
- local file = fs.open("settings","w")
- file.write(textutils.serialize(data))
- file.close()
- read()
- os.reboot()
- else
- Log("[Computer] Startup")
- local file = fs.open("settings","r")
- data = textutils.unserialize(file.readAll())
- file.close()
- end
- term.clear()
- end
- function watchChest()
- end
- function playerListener()
- local active = false
- local function listen()
- while true do
- if #sensor.getPlayerNames() > 0 then
- active = true
- else
- active = false
- end
- sleep(data.scanUpdate)
- end
- end
- local function logChest()
- local tab = getChestData()
- sleep(0) -- offset program
- while true do
- if not active then sleep(data.scanUpdate)
- else
- --active player
- local tab2 = {}
- local logTab = {}
- tab2 = getChestData()
- for k,v in pairs(tab2) do
- if tab[k] == nil then
- table.insert(logTab,("[ ADDED ] "..v.name.." x"..(v.qty)))
- elseif tab[k].qty<v.qty then --items added
- --table.insert(logTab,("[ ADDED ] "..v.name.." x"..(v.qty-tab[k].qty)))
- elseif tab[k].qty > v.qty then -- items removed
- --table.insert(logTab,("[REMOVED] "..v.name.." x"..(tab[k].qty-v.qty)))
- end
- end
- for k,v in pairs(tab) do
- if tab2[k] == nil then
- table.insert(logTab,("[REMOVED] "..v.name.." x"..(v.qty)))
- elseif tab2[k].qty<v.qty then --items removed
- table.insert(logTab,("[REMOVED] "..v.name.." x"..(v.qty-tab2[k].qty)))
- elseif tab2[k].qty > v.qty then -- items added
- table.insert(logTab,("[ ADDED ] "..v.name.." x"..(tab2[k].qty-v.qty)))
- end
- end
- tab ={}
- tab = getChestData()
- local stab = textutils.serialize(tab)
- local stab2 = textutils.serialize(tab2)
- if stab ~= stab2 then
- Log("[ ERROR ] Items updated while CPU performed tasks Compare following tables to determine event:")
- Log("[ ERROR ][Before] "..stab2)
- Log("[ ERROR ][After ] "..stab1)
- end
- for k,v in pairs(logTab) do
- Log(v)
- end
- sleep(1)
- end
- end
- end
- parallel.waitForAny(logChest,listen)
- end
- startup()
- while true do
- parallel.waitForAny(playerListener,lockTerminal,antiHack)
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement