Advertisement
DarkZek

Stupid code that doesnt work

Jun 30th, 2017
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.90 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 == 0 then
  36. action = value
  37. else if key == 1 then
  38. var1 = value
  39. else if key == 2 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. else if user == "403" then
  54. print("Failed login attempt for user "..username)
  55. modem.transmit(1,1,"403")
  56. else if user == "404" then
  57. print("Non existant "..username.." just tried to login")
  58. modem.transmit(1,1,"404")
  59.  
  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
  75. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement