Advertisement
DarkZek

Untitled

Jun 30th, 2017
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.02 KB | None | 0 0
  1. function split (s, splitter)
  2. result = {};
  3. for match in (s..splitter):gmatch("(.-)"..splitter) do
  4. table.insert(result, match);
  5. end
  6. return result;
  7. end
  8.  
  9. function verify(username, password)
  10. if fs.exists("/data/"..username) == false then
  11. return "404"
  12. print("Invalid user "..username.." just tried to login!")
  13. else
  14. --the user exists
  15. local file = fs.open("/data/"..username,"r")
  16. print(password)
  17. if password == file.readLine() then
  18. --correct password
  19. modem.transmit(1,1,"Logging into "..username)
  20. print("User "..username.." just logged in!")
  21. else
  22. --incorrect password
  23. return "403"
  24. end
  25. end
  26. end
  27.  
  28. local modem = peripheral.wrap("top")
  29. textutils.slowPrint("Starting Server...")
  30.  
  31. while true do
  32. modem.open(1)
  33.  
  34. local _, side, freq, rfreq, message = os.pullEvent("modem_message")
  35. print(message)
  36. msg = split(message, "`")
  37. for key, value in pairs(msg) do
  38. if key == 0 then
  39. action = value
  40. else if key == 1 then
  41. var1 = value
  42. else if key == 2 then
  43. var2 = value
  44. end
  45. end
  46. print(action..var1..var2)
  47. if fs.exists("/data") == false then
  48. fs.makeDir("/data")
  49. end
  50. --Do the requested action
  51. if action == "balance" then
  52. user = verify(var1, var2)
  53. if user == "0" then
  54. --Correct user just a fake balance for now because we do not actually store it
  55. modem.transmit(1,1,"1200")
  56. else if user == "403" then
  57. print("Failed login attempt for user "..username)
  58. modem.transmit(1,1,"403")
  59. else if user == "404" then
  60. print("Non existant "..username.." just tried to login")
  61. modem.transmit(1,1,"404")
  62. end
  63. end
  64. else if action == "register" then
  65. --Checking if the user exists
  66. if verify(var1,"") != "404" then
  67. --user already exists
  68. modem.transmit(1,1,"409")
  69. else
  70. --its a new user!
  71. local file = fs.open("/data/"..username,"a")
  72. file.writeLine(password)
  73. file.close()
  74. end
  75. end
  76. end
  77. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement