Advertisement
Guest User

Untitled

a guest
Mar 10th, 2014
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.42 KB | None | 0 0
  1. local component = require("component")
  2. local modem = component.modem
  3. local fs = require("filesystem")
  4. local event = require("event")
  5. local serial = require("serialization")
  6. require("functions") --helper functions
  7.  
  8. --These need to be the same between the client and server
  9. local portNum = 462
  10. local msgDelim = "[:|:]"
  11.  
  12. --This one can be different from client / server
  13. local programDir = "programs/"
  14.  
  15. if not fs.isDirectory("programs") then
  16.     fs.makeDirectory("programs")
  17. end
  18.  
  19. function getFileList()
  20.     local files = fs.list("programs/")
  21.    
  22.     local out = {}
  23.     for file in files do
  24.         if not fs.isDirectory(file) then
  25.             out[fs.name(file)] = fs.lastModified(file)
  26.         end
  27.     end
  28. end
  29.  
  30. function getFile(file)
  31.     local fl = io.open(file, "r")
  32.     local out = ""
  33.    
  34.     local buffer = ""
  35.     while true do
  36.         buffer = fl:read(1024)
  37.         if buffer == nil then break end
  38.         out = out .. buffer
  39.     end
  40.    
  41.     fl:close()
  42.     return out
  43. end
  44.  
  45. function putFile(file, data)
  46.     local fl = io.open(file, "w")
  47.     local out = fl:write(data) --get the output in case it fails
  48.     fl:close()
  49.    
  50.     return out
  51. end
  52.  
  53. function handleClient(localAddress, remoteAddress, port, distance, message)
  54.     if(port ~= portNum) then return end
  55.    
  56.     local arg = explode(msgDelim, message)
  57.     if arg[1] == "getFileList" then
  58.         modem.send(remoteAddress, portNum, "fileList" .. msgDelim .. serial.serialize(getFileList()))
  59.     elseif arg[1] == "getFile" then
  60.         local file = programDir .. fs.name(arg[2]) --make sure they don't try and get files elsewhere
  61.         if fs.exists(file) and not fs.isDirectory(file) then
  62.             modem.send(remoteAddress, portNum, "putFile" .. msgDelim .. arg[2] .. msgDelim .. getFile(file))
  63.         else
  64.             modem.send(remoteAddress, portNum, "error" .. msgDelim .. "Invalid file.")
  65.         end
  66.     elseif arg[1] == "putFile" then
  67.         local file = programDir .. fs.name(arg[2]) --make sure they don't try and put files elsewhere
  68.         local marg = explode(msgDelim,message,2) --only split the first two(command, file, then code)
  69.         local out = putFile(file, marg[3])
  70.         local ret = "success"..msgDelim.."Successfully saved file"
  71.         if not out then
  72.             ret = "error"..msgDelim.."Failed to save file"
  73.         end
  74.         modem.send(remoteAddress, portNum, ret)
  75.     end
  76. end
  77.  
  78. local args = {...}
  79. if(#args < 1) then
  80.     print("Usage: fileserver [start/stop]")
  81. else
  82.     if args[1] == "start" then
  83.         event.listen("modem_message", handleClient)
  84.     elseif args[1] == "stop" then
  85.         event.ignore("modem_message", handleClient)
  86.     end
  87. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement