Advertisement
DarkZek

Untitled

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