Advertisement
Guest User

dbAPI

a guest
Oct 2nd, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.16 KB | None | 0 0
  1. --[[||
  2.   Wow's database API
  3.   This API need:
  4.   -Rednet (Opened)
  5.   -StringUtilsAPI
  6. ||]]
  7.  
  8. --[[
  9.   Error codes:
  10.   0x0: no error
  11.   0x1: nil arg
  12.   0x2: not connected
  13.   0x3: Rednet not opened
  14.   0x4: Bad ID
  15.   0x5: unknown function
  16. ]]
  17. os.loadAPI("StringUtils")
  18. local id = 78
  19. function findServerID()
  20.   --id = rednet.lookup("HomePI", "main")
  21.   return id
  22. end
  23.  
  24. local function send(sTable)
  25.   if sTable == nil then return 0x1 end
  26.   rednet.send(id, sTable)
  27.   sID, msg = rednet.receive()
  28.   if sID ~= findServerID() then return 0x4 end
  29.   return msg
  30. end
  31.  
  32. function database(action, arg1, arg2, arg3, arg4, arg5)
  33.   if action == "createUser" then
  34.     if arg1 == nil or arg2 == nil or arg3 == nil or arg4 == nil or type(arg4) ~= "number" or type(arg1) ~= "string" or type(arg2) ~= "string" or type(arg3) ~= "string" then
  35.       return 0x1
  36.     else
  37.       local TEMP_TABLE = {type = "database", todo = action, Username = arg1, Password = arg2, Biolock = arg3, accesslevel = arg4}
  38.       local lol = textutils.serialise(TEMP_TABLE)
  39.       return(send(TEMP_TABLE))
  40.     end
  41.   elseif action == "resetDB" then
  42.     local TEMP_TABLE = {type = "database", todo = "reset"}
  43.     return(send(TEMP_TABLE))
  44.   elseif action == "getinfo" then
  45.     if type(arg1) ~= "string" then
  46.       return 0x1
  47.     else
  48.       local TEMP_TABLE = {type = "database", todo = "Userinfo", Username = arg1}
  49.       return(send(TEMP_TABLE))
  50.     end
  51.   else
  52.     return 0x5
  53.   end
  54. end
  55.  
  56. function newToken(user, pass)
  57.   if type(user) ~= "string" or type(pass) ~= "string" then return 0x1 end
  58.   local rMath = tostring(math.random(1,0xBEEF))
  59.   local encPass = StringUtils.encrypt(tostring(rMath),StringUtils.SHA1(pass))
  60.   local TEMP_TABLE = {type = "token", todo = "createToken", math = rMath, Username = user, enc = encPass}
  61.   return send(TEMP_TABLE)
  62. end
  63.  
  64. function getToken(user, pass)
  65.   if type(user) ~= "string" or type(pass) ~= "string" then return 0x1 end
  66.   local rMath = tostring(math.random(1,0xBEEF))
  67.   local encPass = StringUtils.encrypt(tostring(rMath),StringUtils.SHA1(pass))
  68.   local TEMP_TABLE = {type = "token", todo = "getToken", math = rMath, Username = user, enc = encPass}
  69.   return send(TEMP_TABLE)
  70. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement