Advertisement
Guest User

Untitled

a guest
Jun 30th, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.10 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 setBalance(username, password, balance)
  10. local file = fs.open("/data/"..username,"w")
  11. --Rewrite password and balance
  12. file.writeLine(password)
  13. file.writeLine(balance)
  14. end
  15.  
  16. function getBalance(username)
  17. local file = fs.open("/data/"..username,"r")
  18. --We only want the second line
  19. file.readLine()
  20. return file.readLine()
  21. end
  22.  
  23. function verify(username, password)
  24. if fs.exists("/data/"..username) == false then
  25. return "404";
  26. else
  27. --the user exists
  28. local file = fs.open("/data/"..username,"r")
  29. if password == file.readLine() then
  30. --correct password
  31. return "0";
  32. else
  33. --incorrect password
  34. return "403";
  35. end
  36. end
  37. end
  38. local modem = peripheral.wrap("top")
  39.  
  40. textutils.slowPrint("Starting Server...")
  41.  
  42. while true do
  43. modem.open(1)
  44.  
  45. local _, side, freq, rfreq, message = os.pullEvent("modem_message")
  46. msg = split(message, "`")
  47. for key, value in pairs(msg) do
  48. if key == 1 then
  49. action = value
  50. elseif key == 2 then
  51. var1 = value
  52. elseif key == 3 then
  53. var2 = value
  54. elseif key == 4 then
  55. var3 = value
  56. elseif key == 5 then
  57. var4 = value
  58. end
  59. end
  60. if fs.exists("/data") == false then
  61. fs.makeDir("/data")
  62. end
  63. --Do the requested action
  64. if action == "balance" then
  65. user = verify(var1, var2)
  66. if user == "0" then
  67. --Give balance
  68. modem.transmit(1,1,getBalance(var1))
  69. elseif user == "403" then
  70. print("Failed login attempt for user "..var1)
  71. modem.transmit(1,1,"403")
  72. elseif user == "404" then
  73. print("Non existant "..var1.." just tried to login")
  74. modem.transmit(1,1,"404")
  75. end
  76. elseif action == "register" then
  77. --Checking if the user exists
  78. if verify(var1,"") == "404" then
  79. --its a new user!
  80. local file = fs.open("/data/"..var1,"a")
  81. file.writeLine(var2)
  82. file.writeLine("0")
  83. file.close()
  84. modem.transmit(1,1,"0")
  85. print("Registered user "..var1)
  86. else
  87. --user already exists
  88. modem.transmit(1,1,"409")
  89. end
  90. elseif action == "pay" then
  91. --User wants to pay another user
  92. --var1 is the user that wants to pay
  93. --var2 is the users password
  94. --var3 is the user that is being paid
  95. --var4 is the amount that is being processed
  96. print(var1.." paid "..var3.." $"var4)
  97. --Check they have the correct password
  98. local file = fs.open("/data/"..var1,"r")
  99. pass = file.readLine()
  100. balance = file.readLine()
  101. file.close()
  102. newBalance = tonumber(balance) - var4
  103. if newBalance > 0 then
  104. --User has enough money
  105. if pass == var2 then
  106. --Authenticated
  107. setBalance(var1,var2,newBalance)
  108. local file = fs.open("/data/"..var1,"r")
  109. payeePassword = file.readLine()
  110. payeeBalance = file.readLine()
  111. setBalance(payee, payeePassword, tonumber(payeeBalance) + amount)
  112. end
  113. end
  114. end
  115. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement