Advertisement
Guest User

Untitled

a guest
Oct 11th, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.14 KB | None | 0 0
  1. --[[||
  2. Wow's database API
  3. This API need:
  4. -Modem
  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:
  15. 0x5: unknown function
  16. ]]
  17. os.loadAPI("StringUtils")
  18. local channel = 644
  19. local modem = peripheral.find("modem")
  20.  
  21. local function checkopened()
  22. if not modem.isOpen(channel) then modem.open(channel) end
  23. return true
  24. end
  25.  
  26. local function addCheck(sTable, token, Username)
  27. local cryptoken = StringUtils.encrypt("true", token)
  28. sTable["tokenUsername"] = Username
  29. sTable["tokenCheck"] = cryptoken
  30. return sTable
  31. end
  32.  
  33. local function send(sTable, token, Username)
  34. checkopened()
  35. if sTable == nil then return 0x1 end
  36. local sTable = addCheck(sTable, token, Username)
  37. modem.transmit(channel, channel, sTable)
  38. local x = true
  39. while x do
  40. local event, side, sChan, rChan, msg, distance = os.pullEvent("modem_message")
  41. if sChan == channel then
  42. x = false
  43. return msg
  44. end
  45. end
  46. end
  47.  
  48. function database(action, arg1, arg2, arg3, arg4, arg5, arg6)
  49. if action == "createUser" then
  50. 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" or type(arg4) ~= "string" or type(arg5) ~= "string" then
  51. return 0x1
  52. else
  53. local TEMP_TABLE = {type = "database", todo = action, Username = arg1, Password = arg2, Biolock = arg3, accesslevel = arg4}
  54. local lol = textutils.serialise(TEMP_TABLE)
  55. return(send(TEMP_TABLE, arg4, arg5))
  56. end
  57.  
  58. elseif action == "resetDB" then
  59. if type(arg1) ~= "string" or type(arg2) ~= "string" then return 0x1 end
  60. local TEMP_TABLE = {type = "database", todo = "reset"}
  61. return(send(TEMP_TABLE, arg1,arg2))
  62.  
  63. elseif action == "getinfo" then
  64. if type(arg1) ~= "string" or type(arg2) ~= "string" or type(arg3) ~= "string" then
  65. return 0x1
  66. else
  67. local TEMP_TABLE = {type = "database", todo = "Userinfo", Username = arg1}
  68. return(send(TEMP_TABLE, arg2, arg3))
  69. end
  70.  
  71. elseif action == "update" then
  72. --arg1: what arg2: new: arg3: who arg4 Username arg5 token
  73. if type(arg1) ~= "string" or type(arg2) ~= "string" or type(arg3) ~= "string" or type(arg4) ~= "string" then return 0x5 end
  74. local TEMP_TABLE = {type = "database", todo = "update", toupdate = arg1, newupdate = arg2, who = arg3}
  75. return(send)
  76. end
  77.  
  78. function newToken(user, pass)
  79. if type(user) ~= "string" or type(pass) ~= "string" then return 0x1 end
  80. local rMath = tostring(math.random(1,0xBEEF))
  81. local encPass = StringUtils.encrypt(tostring(rMath),StringUtils.SHA1(pass))
  82. local TEMP_TABLE = {type = "token", todo = "createToken", math = rMath, Username = user, enc = encPass}
  83. return send(TEMP_TABLE)
  84. end
  85.  
  86. function getToken(user, pass)
  87. if type(user) ~= "string" or type(pass) ~= "string" then return 0x1 end
  88. local rMath = tostring(math.random(1,0xBEEF))
  89. local encPass = StringUtils.encrypt(tostring(rMath),StringUtils.SHA1(pass))
  90. local TEMP_TABLE = {type = "token", todo = "getToken", math = rMath, Username = user, enc = encPass}
  91. return send(TEMP_TABLE)
  92. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement