Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --Var init
- local RP_BOOL = true
- local NETWORK_LIST = {}
- --Log
- local function log(ID, oMSG, reason)
- local FILE = fs.open("/log", "a")
- if type(oMSG) == "string" then
- toMSG = "bad type(string)"
- elseif type(oMSG) == "table" then
- if oMSG["type"] ~= nil then
- toMSG = "bad table"
- else
- toMSG = oMSG["type"]
- end
- else
- toMSG = "unknown"
- end
- local TEMP = "[LOG] ID:"..ID.." toMSG: "..toMSG.." Reason: "..reason
- print(TEMP)
- FILE.writeLine(TEMP)
- FILE.close()
- return true
- end
- --Users management
- local USER_FILE = "/users"
- local DB_SCHEM = {Users = {}, Password = {}, Biolock = {}, AccessLevel = {}, Token= {}}
- local function USERS_CREATEDB()
- local USER_TEMP = fs.open(USER_FILE, "w")
- USER_TEMP.write(textutils.serialise(DB_SCHEM))
- USER_TEMP.close()
- end
- local function USERS_GET()
- local OBJECT_UF = fs.open(USER_FILE, "r")
- local USERS = textutils.unserialise(OBJECT_UF.readAll())
- if USERS == nil then
- USERS_CREATEDB()
- USERS = textutils.unserialise(OBJECT_UF.readAll())
- end
- OBJECT_UF.close()
- return USERS
- end
- local function USERS_ID(users, tofind)
- local ID = nil
- for i = 1,#users["Users"] do
- if tofind == nil then
- ID = i
- else
- if users["Users"][i] == tofind then
- ID = i
- end
- end
- end
- return ID
- end
- local function USERS_CREATE(name, pass, biolock, accesslevel)
- local oUSERS = USERS_GET()
- local fUSERS = oUSERS
- local lastID = USERS_ID(oUSERS)
- if lastID == nil then lastID = 1 else lastID = lastID + 1 end
- if name == nil or pass == nil or accesslevel == nil then
- return false
- end
- if biolock == nil then biolock = "unknown" end
- fUSERS["Users"][lastID] = name
- fUSERS["Password"][lastID] = pass
- fUSERS["Biolock"][lastID] = biolock
- fUSERS["Token"][lastID] = "unknown" --add token generator
- fUSERS["AccessLevel"][lastID] = accesslevel
- local TO_SAVE = fs.open(USER_FILE,"w")
- TO_SAVE.write(textutils.serialise(fUSERS))
- TO_SAVE.close()
- return true
- end
- --We need the famous Gitano Parser !
- local function gitanoParser(sTable,sID)
- print(sTable.type)
- if sTable.type == "mdr" then
- log(sID, sTable, "bad type")
- return false
- elseif sTable.type == "token" then
- elseif sTable.type == "log" then
- elseif sTable.type == "ping" then
- rednet.send(sID, true)
- log(sID, sTable, "ping")
- elseif sTable.type == "WAS" then --Wow address system
- elseif sTable.type == "database" then
- if sTable.todo == "createUser" then
- if sTable.Username == nil or sTable.Password == nil or sTable.Biolock == nil or type(sTable.accesslevel) == number then
- print("nop")
- else
- if USERS_CREATE(sTable.Username,sTable.Password,sTable.Biolock,sTable.accesslevel) then
- print("User Created")
- rednet.send(sID, true)
- else
- print("Failed to create")
- rednet.send(sID, true)
- end
- end
- elseif sTable.todo == "Userinfo" then
- if sTable.Username == nil then
- --finish error
- else
- --[[
- Table to resend:
- id
- biolockid
- accesslevel
- ]]
- local TUserTable = USERS_GET()
- local Tid = USERS_ID(TUserTable, sTable.Username)
- if Tid == nil then print("Nil TID") return false end
- local Tbiolock = TUserTable[Biolock][Tid]
- local tAccessL = TUserTable[AccessLevel][Tid]
- local TEMP_TABLE{id = Tid, biolockid = Tbiolock, accesslevel = TAccessL}
- print(textutils.serialise(TEMP_TABLE))
- --rednet.send(sID, TEMP_TABLE)
- end
- elseif sTable.todo == "checkpassword" then
- elseif sTable.todo == "reset" then
- USERS_CREATEDB()
- print("Database reseted") --Pls add a Terminal confirm
- end
- elseif sTable.type == "network" then
- end
- end
- --Database
- term.write("Rednet state: ")
- if rednet.isOpen() then print("true") else print("false") end
- if rednet.isOpen() ~= true then rednet.open("top") end
- rednet.host("HomePI", "main")
- print("Rednet opened\nID: ".. os.computerID())
- --USERS_CREATEDB()
- --print(USERS_CREATE("admin", "test", "unknown", 5)
- local function receiveparse()
- while RP_BOOL do
- local sID, msg = rednet.receive()
- if type(msg) ~= "table" then rednet.send(sID, "Please use API") log(sID, msg, "Dont use a table") else
- gitanoParser(msg,sID, "received") -- redirect to gitanoParser
- end
- end
- end
- parallel.waitForAll(receiveparse)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement