Advertisement
wow0

userAPI: API

Oct 21st, 2017
33
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.90 KB | None | 0 0
  1. --[[||
  2.   Wow's users API
  3.   This API need:
  4.   -Modem
  5.   -StringUtilsAPI
  6. ||]]
  7.  
  8. --[[
  9. 0 = Nothing
  10. 1 = nil / bad type
  11. 2 = unknown user
  12. 3 = nil token
  13. 4 = Bad Token
  14. 5 = Bad Password
  15. 6 = Bad accesslevel
  16. 7 = ?
  17. ]]
  18. if not fs.exists("/StringUtils") then shell.run("pastebin get ad3aUsVw StringUtils") end
  19. if not os.loadAPI("StringUtils") then print("Need StringUtils") return end
  20. local channel = 644
  21. local modem = peripheral.find("modem")
  22.  
  23. local function checkopened()
  24.     if not modem.isOpen(channel) then modem.open(channel) end
  25.     return true
  26. end
  27.  
  28. local function addCheck(sTable, Username, token)
  29.     local cryptoken = StringUtils.encrypt("true", token)
  30.     sTable["tokenUsername"] = Username
  31.     sTable["tokenCheck"] = cryptoken
  32.     return sTable
  33. end
  34.  
  35. local function send(sTable, token, Username)
  36.   checkopened()
  37.   if sTable == nil then return 0x1 end
  38.   local sTable = addCheck(sTable, token, Username)
  39.   modem.transmit(channel, channel, sTable)
  40.   local x = true
  41.   while x do
  42.     local event, side, sChan, rChan, msg, distance = os.pullEvent("modem_message")
  43.     if sChan == channel then
  44.         x = false
  45.         return msg
  46.     end
  47.   end
  48. end
  49.  
  50. local pingInfo
  51. local function timeout()
  52.   sleep(3)
  53.   pingInfo = 1
  54.   return false
  55. end
  56.  
  57. local function localPing()
  58.   checkopened()
  59.   local TEMP_TABLE = {type = "ping"}
  60.   pingInfo = send(TEMP_TABLE)
  61.   return true
  62. end
  63.  
  64. function ping()
  65.   local sTime = os.clock()
  66.   parallel.waitForAny(timeout, localPing)
  67.   local fTime = os.clock() - sTime
  68.   return pingInfo, fTime*1000 --Return the time in ms
  69. end
  70.  
  71. function createUser(username, password, biolock, al, uName, uToken)
  72.     if username == nil or password == nil or biolock == nil or type(al) ~= "number" or type(uName) ~= "string" or type(uToken) ~= "string" then
  73.         return 0x1
  74.     else
  75.       local TEMP_TABLE = {type = "database", todo = "createUser", Username = username, Password = password, Biolock = biolock, accesslevel = al}
  76.       local lol = textutils.serialise(TEMP_TABLE)
  77.       return send(TEMP_TABLE, uName, uToken)
  78.     end
  79. end
  80.  
  81. function resetDB(uName, uToken)
  82.     if type(uName) ~= "string" or type(uToken) ~= "string" then return 0x1 end
  83.     local TEMP_TABLE = {type = "database", todo = "reset"}
  84.     return send(TEMP_TABLE, uName,uToken)
  85. end
  86.  
  87. function getInfo(who, uName, uToken)
  88.     if type(who) ~= "string" or type(uName) ~= "string" or type(uToken) ~= "string" then
  89.       return 0x1
  90.     else
  91.       local TEMP_TABLE = {type = "database", todo = "Userinfo", Username = who}
  92.       return send(TEMP_TABLE, uName, uToken)
  93.     end
  94. end
  95.  
  96. local function updateInfo(whatupdate, new, who, uName, uToken)
  97.     if type(whatupdate) ~= "string" or type(new) ~= "string" or type(who) ~= "string" or type(uName) ~= "string" or type(uToken) ~= "string"  then return 0x5 end
  98.     local TEMP_TABLE = {type = "database", todo = "update", toupdate = whatupdate, newupdate = new, who = who}
  99.     return send(TEMP_TABLE, uName, uToken)
  100. end --Not finished
  101.  
  102. function findBiolock(bioID, uName, uToken)
  103.     if type(bioID) ~= "string" or type(uName) ~= "string" or type(uToken) ~= "string" then return 0x1 end
  104.     local TEMP_TABLE = {type = "database", todo = "findBL", toFind = bioID}
  105.     return send(TEMP_TABLE, uName, uToken)
  106. end
  107.  
  108. function newToken(user, pass)
  109.   if type(user) ~= "string" or type(pass) ~= "string" then return 0x1 end
  110.   local rMath = tostring(math.random(1,0xBEEF))
  111.   local encPass = StringUtils.encrypt(tostring(rMath),StringUtils.SHA1(pass))
  112.   local TEMP_TABLE = {type = "token", todo = "createToken", math = rMath, Username = user, enc = encPass}
  113.   return send(TEMP_TABLE)
  114. end
  115.  
  116. function getToken(user, pass)
  117.   if type(user) ~= "string" or type(pass) ~= "string" then return 0x1 end
  118.   local rMath = tostring(math.random(1,0xBEEF))
  119.   local encPass = StringUtils.encrypt(tostring(rMath),StringUtils.SHA1(pass))
  120.   local TEMP_TABLE = {type = "token", todo = "getToken", math = rMath, Username = user, enc = encPass}
  121.   return send(TEMP_TABLE)
  122. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement