Advertisement
Guest User

startup

a guest
Oct 1st, 2017
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.46 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.   return true
  78. end
  79. --We need the famous Gitano Parser !
  80. local function gitanoParser(sTable,sID)
  81.   print(sTable.type)
  82.   if sTable.type == "mdr" then
  83.     log(sID, sTable, "bad type")
  84.     return false
  85.   elseif sTable.type == "token" 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.       if sTable.Username == nil then
  106.       --finish error
  107.       else
  108.         --[[
  109.         Table to resend:
  110.         id
  111.         biolockid
  112.         accesslevel
  113.         ]]
  114.         local TUserTable = USERS_GET()
  115.         local Tid = USERS_ID(TUserTable, sTable.Username)
  116.         if Tid == nil then print("Nil TID") return false end    
  117.         local Tbiolock = TUserTable[Biolock][Tid]
  118.         local tAccessL = TUserTable[AccessLevel][Tid]
  119.        
  120.         local TEMP_TABLE{id = Tid, biolockid = Tbiolock, accesslevel = TAccessL}
  121.         print(textutils.serialise(TEMP_TABLE))
  122.         --rednet.send(sID, TEMP_TABLE)
  123.       end
  124.     elseif sTable.todo == "checkpassword" then
  125.      
  126.     elseif sTable.todo == "reset" then
  127.       USERS_CREATEDB()
  128.       print("Database reseted") --Pls add a Terminal confirm
  129.     end
  130.   elseif sTable.type == "network" then
  131.   end
  132. end
  133. --Database
  134. term.write("Rednet state: ")
  135. if rednet.isOpen() then print("true") else print("false") end
  136. if rednet.isOpen() ~= true then rednet.open("top") end
  137. rednet.host("HomePI", "main")
  138. print("Rednet opened\nID: ".. os.computerID())
  139.  
  140. --USERS_CREATEDB()
  141. --print(USERS_CREATE("admin", "test", "unknown", 5)
  142.  
  143. local function receiveparse()
  144.   while RP_BOOL do
  145.     local sID, msg = rednet.receive()
  146.     if type(msg) ~= "table" then rednet.send(sID, "Please use API") log(sID, msg, "Dont use a table") else
  147.       gitanoParser(msg,sID, "received")       -- redirect to gitanoParser
  148.     end
  149.   end
  150. end
  151.  
  152. parallel.waitForAll(receiveparse)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement