ThommyCraft

server

Mar 3rd, 2015
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.52 KB | None | 0 0
  1. --[[
  2.     CC-Bank
  3.     Copyright © 2012  Yingtong Li
  4.  
  5.     CC-Bank is free software: you can redistribute it and/or modify
  6.     it under the terms of the GNU General Public License as published by
  7.     the Free Software Foundation, either version 3 of the License, or
  8.     (at your option) any later version.
  9.  
  10.     CC-Bank is distributed in the hope that it will be useful,
  11.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.     GNU General Public License for more details.
  14.  
  15.     You should have received a copy of the GNU General Public License
  16.     along with CC-Bank.  If not, see <http://www.gnu.org/licenses/>.
  17. --]]
  18.  
  19. os.loadAPI("crypt")
  20.  
  21. local modemSide = "left"
  22. local version = "CC-Bank-0.2.0-Compatible"
  23.  
  24. local function log(level, info)
  25.     local severity = "?"
  26.     if level == 1 then  severity = "INFO"   end
  27.     if level == 2 then  severity = "WARNING"    end
  28.     if level == 3 then  severity = "ERROR"  end
  29.     print("[" .. severity .. "] " .. textutils.formatTime(os.time()) .. " - " .. info)
  30. end
  31.  
  32. local function serve()
  33.     rednet.open(modemSide)
  34.     local senderId, message = rednet.receive()
  35.  
  36.     if string.len(message) >= string.len(version) then
  37.         if string.sub(message, 1, string.len(version)) == version then
  38.             local bits = textutils.unserialize(string.sub(message, string.len(version) + 1))
  39.             local command = bits[1]
  40.             local acc = bits[2]
  41.             local pass = bits[3]
  42.  
  43.             log (1, command .. " from " .. acc)
  44.  
  45.             if command == "BALANCE" then
  46.                 if fs.exists("acc/" .. acc .. ".txt") then
  47.                     local file = fs.open("acc/" .. acc .. ".txt", "r")
  48.                     local passC = file.readLine()
  49.                     local bal = file.readLine()
  50.                     file.close()
  51.  
  52.                     if crypt.checkPassword(pass, passC) then
  53.                         rednet.send(senderId, bal)
  54.                         return
  55.                     end
  56.                 end
  57.             elseif command == "TRANSFER" then
  58.                 local dest = bits[4]
  59.                 local amt = tonumber(bits[5])
  60.                 if fs.exists("acc/" .. acc .. ".txt") and fs.exists("acc/" .. dest .. ".txt") then
  61.                     local fileFrom = fs.open("acc/" .. acc .. ".txt", "r")
  62.                     local passC = fileFrom.readLine()
  63.                     local bal = tonumber(fileFrom.readLine())
  64.                     fileFrom.close()
  65.  
  66.                     local fileTo = fs.open("acc/" .. dest .. ".txt", "r")
  67.                     local passT = fileTo.readLine()
  68.                     local balT = tonumber(fileTo.readLine())
  69.                     fileTo.close()
  70.  
  71.                     if crypt.checkPassword(pass, passC) and bal >= amt then
  72.                         fileFrom = fs.open("acc/" .. acc .. ".txt", "w")
  73.                         fileTo = fs.open("acc/" .. dest .. ".txt", "w")
  74.  
  75.                         fileFrom.writeLine(passC)
  76.                         fileFrom.writeLine(bal - amt)
  77.                         fileFrom.close()
  78.  
  79.                         fileTo.writeLine(passT)
  80.                         fileTo.writeLine(balT + amt)
  81.                         fileTo.close()
  82.  
  83.                         log (1, "Transferred " .. amt .. " from " .. acc .. " to " .. dest)
  84.  
  85.                         rednet.send(senderId, "+")
  86.                         return
  87.                     end
  88.                 end
  89.             elseif command == "WITHDRAW" then
  90.                 local amt = tonumber(bits[4])
  91.                 if fs.exists("acc/" .. acc .. ".txt") then
  92.                     local file = fs.open("acc/" .. acc .. ".txt", "r")
  93.                     local passC = file.readLine()
  94.                     local bal = tonumber(file.readLine())
  95.                     file.close()
  96.  
  97.                     if crypt.checkPassword(pass, passC) and bal >= amt then
  98.                         file = fs.open("acc/" .. acc .. ".txt", "w")
  99.  
  100.                         file.writeLine(passC)
  101.                         file.writeLine(bal - amt)
  102.                         file.close()
  103.  
  104.                         log (1, "Withdrew " .. amt .. " from " .. acc)
  105.  
  106.                         rednet.send(senderId, "+")
  107.                         return
  108.                     end
  109.                 end
  110.             end
  111.         end
  112.         log (2, "Request denied")
  113.         rednet.send(senderId, "-")
  114.     end
  115. end
  116.  
  117. while true do
  118.     serve()
  119. end
Add Comment
Please, Sign In to add comment