Advertisement
DarkZek

Untitled

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