Advertisement
Guest User

startup

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