Guest User

startup

a guest
Sep 24th, 2017
22
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.75 KB | None | 0 0
  1. --Var init
  2. local RP_BOOL = true
  3. local NETWORK_LIST = {}
  4. --Log
  5. local function log(ID, oMSG, reason)
  6.   local FILE = fs.open("/log", "a")
  7.   if type(oMSG) == "string" then
  8.     local toMSG = oMSG
  9.   elseif type(oMSG) == "table" then
  10.     local toMSG = textutils.serialise(oMSG)
  11.    
  12.   else
  13.     toMSG = "nil"
  14.   end
  15.   toMSG = "nil"
  16.   local TEMP = "[LOG] ID:"..ID.." toMSG: "..toMSG.." Reason: "..reason
  17.   print(TEMP)
  18.   FILE.writeLine(TEMP)
  19.   FILE.close()
  20.  
  21.   return true
  22. end
  23.  
  24. --Users management
  25. local USER_FILE = "/users"
  26. local DB_SCHEM = {Users = {}, Password = {}, Biolock = {}, AccessLevel = {}, Token= {}}
  27. local function USERS_CREATEDB()
  28.   local USER_TEMP = fs.open(USER_FILE, "w")
  29.   USER_TEMP.write(textutils.serialise(DB_SCHEM))
  30.   USER_TEMP.close()
  31. end
  32.  
  33. local function USERS_GET()
  34.   local OBJECT_UF = fs.open(USER_FILE, "r")
  35.   local USERS = textutils.unserialise(OBJECT_UF.readAll())
  36.   if USERS == nil then
  37.     USERS_CREATEDB()
  38.     USERS = textutils.unserialise(OBJECT_UF.readAll())
  39.   end
  40.   OBJECT_UF.close()
  41.   return USERS
  42. end
  43. local function USERS_ID(users, tofind)
  44.   local ID = nil
  45.   for i = 1,#users["Users"] do
  46.     if tofind == nil then
  47.       ID = i
  48.     else
  49.       if users["Users"][i] == tofind then
  50.         ID = i
  51.       end
  52.     end
  53.   end
  54.   return ID
  55. end
  56. local function USERS_CREATE(name, pass, biolock, accesslevel)
  57.   local oUSERS = USERS_GET()
  58.   local fUSERS = oUSERS
  59.   local lastID = USERS_ID(oUSERS)
  60.   if lastID == nil then lastID = 1 else lastID = lastID + 1 end
  61.   if name == nil or pass == nil or accesslevel == nil then
  62.     return false
  63.   end
  64.   if biolock == nil then biolock = "unknown" end
  65.   fUSERS["Users"][lastID] = name
  66.   fUSERS["Password"][lastID] = pass
  67.   fUSERS["Biolock"][lastID] = biolock
  68.   fUSERS["Token"][lastID] = "unknown" --add token generator
  69.   fUSERS["AccessLevel"][lastID] = accesslevel
  70.   local TO_SAVE = fs.open(USER_FILE,"w")
  71.   TO_SAVE.write(textutils.serialise(fUSERS))
  72.   TO_SAVE.close()
  73. end
  74. --We need the famous Gitano Parser !
  75. local function gitanoParser(sTable,sID)
  76.   print(type(sTable))
  77.   print(sTable)
  78.   if sTable.type == nil then
  79.     log(sID, sTable, "bad type")
  80.     return false
  81.   elseif sTable.type == "token" then
  82.   elseif sTable.type == "entry" then
  83.   elseif sTable.type == "log" then
  84.   elseif sTable.type == "ping" then
  85.     rednet.send(sID, true)
  86.     log(sID, sTable, "ping")
  87.   elseif sTable.type == "WAS" then --Wow address system
  88.   elseif sTable.type == "database" then
  89.     if sTable.todo == "createUser" then
  90.       if sTable.Username == nil or sTable["Password"] == nil or sTable["Biolock"] == nil or sTable["accesslevel"] == nil then
  91.      
  92.       else
  93.         if USERS_CREATE(sTable.Username,sTable.Password,sTable.Biolock,sTable.accesslevel) then
  94.           print("User Created")
  95.           rednet.send(sID, true)
  96.         else
  97.           print("Failed to create")
  98.           rednet.send(sID, true)
  99.         end
  100.       end
  101.     elseif sTable.todo == "Userinfo" then
  102.     elseif sTable.todo == "reset" then
  103.       USERS_CREATEDB()
  104.     end
  105.   elseif sTable.type == "network" then
  106.   end
  107. end
  108. --Database
  109. term.write("Rednet state: ")
  110. if rednet.isOpen() then print("true") else print("false") end
  111. if rednet.isOpen() ~= true then rednet.open("top") end
  112. rednet.host("HomePI", "main")
  113. print("Rednet opened\nID: ".. os.computerID())
  114.  
  115. --USERS_CREATEDB()
  116. --print(USERS_CREATE("admin", "test", "unknown", 5))
  117.  
  118.  
  119. local function securityinit()
  120.   term.clear()  --To finish
  121. end
  122.  
  123. local function receiveparse()
  124.   while RP_BOOL do
  125.     local sID, msg = rednet.receive()
  126.     if type(msg) ~= "table" then rednet.send(sID, "Please use API") log(sID, msg, "Dont use a table") else
  127.       gitanoParser(msg,sID)       -- redirect to gitanoParser
  128.     end
  129.   end
  130. end
  131.  
  132. parallel.waitForAll(receiveparse)
Add Comment
Please, Sign In to add comment