Advertisement
DarkZek

Untitled

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