This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
digitalEntity's profile picture

is there a way to revert os.pullEvent = os.pullEventRaw?

Started by digitalEntity, 13 February 2015 - 07:15 PM
digitalEntity #1
Posted 13 February 2015 - 08:15 PM
So… recently I'm working on an OS that allows admins to terminate it, but not standard users. As the title suggests, I'm looking for a way to revert os.pullEvent = os.pullEventRaw within the programming once the admin has logged in to that computer.

current coding:

--Drachma 0.1

os.pullEvent=os.pullEventRaw

--[[to-do list:
logout feature
create "user already exists" portion of newUser
develop os functions
create password changing function
admin password cannot be changed
experiment w/ h.write & h.writeLine commands
create file identification
create program licenses
admins grant licenses to .iso files to authorize their use
licenses must be verified before every attempt to run program
dangerScan level 1 failures ignored
develop dangerScan license compatibility

big: develop Drachma as OS
big: develop Wenceslas
]]

--[[file system extensions
.prg = program
.dba = database
.txt = text
.iso = isolated program
.lic = licensed program
.img = image
]]

--Variable lineup
userTrust = false
adminTrust = false
aiCheck = false

--Functions
function login()
term.clear()
term.setCursorPos(1,1)
print("Username: ")
term.setCursorPos(1,3)
usr = read()
term.setCursorPos(1,3)
print("Password: ")
term.setCursorPos(1,4)
pwd = read("*")
usr.dba = fs.open("drachma/usr.dba", "r")
pwd.dba = fs.open("drachma/pwd.dba", "r")
_usr=textutils.unserialize(usr.dba readAll())
_pwd=textutils.unserialize(pwd.dba.readAll())
for i = 1,0,1 do
  if usr = _usr[i] and pwd = _pwd[i] then
   if usr="admin" then
	adminTrust = true
	os.pullEvent = os.pullEvent
	usertrust = true
	i = 0
	home()
   else
	userTrust = true
	i = 0
	home()
   end
  else
  print("Credential Mismatch. Access Denied.")
  end
end
end

function gui()
end

function aiStatus()
term.clear()
if aiCheck == true then
  shell.run("drachma/Wenceslas.lai")
else
  x, y = term.getSize()
  _x = x-14
  term.setCursorPos(_x,y)
  io.write("AI unavailable.")
  term.setCursorPos(1,1)
end
end

function os()
input = read()
if string.sub(input,1,4)=="open"
  --extensions(input,5,#input)
else
  print("Unrecognized Command.")
  sleep(3)
end
end

function home()
while true do
  --gui()
  aiStatus()
  --os()
end
end

function newUser()
if adminTrust==true then
  term.setCursorPos(1,1)
  print("New Username:")
  term.setCursorPos(1,2)
  usr = read()
  term.setCursorPos(1,3)
  print("New Password:")
  term.setCursorPos(1,4)
  pwd = read("*")
  usr.dba = fs.open("drachma/usr.dba", "a")
  usr.dba.writeLine(usr)
  usr.dba.flush()
  usr.dba.close()
  pwd.dba = fs.open("drachma/pwd.dba", "a")
  pwd.dba.writeLine(pwd)
  pwd.dba.flush()
  pwd.dba.close()
  print("Success! User".. usr .." created.")
  sleep(5)
  term.clear()
else
  print("Access Denied. Administrator priveleges required.")
  sleep(5)
  term.clear()
end
home()
end

function dangerScan(fileName)
file = fs.open(fileName,"r")
contents = file.readAll()
lv2.dba = fs.open("drachma/lv2.dba", "r")
lv2 = textutils.unserialize(lv2.dba.readAll())
lv1.dba = fs.open("drachma/lv1.dba", "r")
lv1 = textutils.unserialize(lv1.dba.readAll())
for i = 1, #lv2 do
  status = string.search(contents, lv2[i])
  if status ~= nil then
   fs.delete(fileName)
   print("Dangerous script detected, deleting file.")
   return(2)
  else
   status = string.search(contents, lv1[i])
   if status ~= nil then
	fs.move(fileName, string.sub(fileName, 1, #fileName-3).."iso")
	print("Suspicious script detected, isolating file.")
	return(1)
   else
	return(0)
   end
  end
end
end

login()

also… apparently ^that^ doesn't get along very consistently with the tab button…
KingofGamesYami #2
Posted 13 February 2015 - 08:17 PM

local oldPull = os.pullEvent
os.pullEvent = os.pullEventRaw
...
os.pullEvent = oldPull
digitalEntity #3
Posted 13 February 2015 - 10:09 PM
I feel so dumb now… of course I could've just stored the original form in a variable immediately preceding it, then used said variable to call the original form later… -_-/>… thank you
digitalEntity #4
Posted 13 February 2015 - 10:30 PM
also, would the program error if I had an if loop that checked if something was or was not equal to nil?

example:
if string.find(usr.dba.readAll(), usr)==nil then –If user does not already exist, then

hint: I'm working on the "User already exists." error for the newUser function… gotta empty that to-do list lol…
KingofGamesYami #5
Posted 13 February 2015 - 11:26 PM
You can easily check for nil/false with the not keyword, eg:

if not string.find( user.dba.readAll(), usr) then

(==nil won't error, not is just the "better" way)
ElvishJerricco #6
Posted 13 February 2015 - 11:40 PM
For jobs like these, I like to automate. Here's what I normally do


function override(host, functionName, f, prevRevert)
    local old = host[functionName]
    host[functionName] = f
    return function()
        if prevRevert then
            prevRevert()
        end
        host[functionName] = old
    end
end


local revert = override(os, "pullEvent", os.pullEventRaw)
revert = override(something, "else", otherElse, revert)
...
revert()

Idea is that you build up one big function that ends up reverting everything you've overridden. It's kinda handy.
digitalEntity #7
Posted 13 February 2015 - 11:49 PM
I can see where you're going with that, but the only function I think I'll ever have any desire to overwrite is os.pullEvent, so I think I'll just stick with the 3 lines in this scenario.