OpenComputers Von Neumann Machine Programs
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

67 lines
1.6 KiB

2 years ago
  1. local component = require("component")
  2. local event = require("event")
  3. local fs = require("filesystem")
  4. local io = require("io")
  5. local shell = require("shell")
  6. local m = component.proxy(component.list("modem")())
  7. local os = require("os")
  8. arg = {...}
  9. function listen(m)
  10. while true do
  11. local _, _, from, port, _, msg = event.pull("modem_message")
  12. print(e, from, port, msg, type(msg))
  13. local pos = msg:find("%s")
  14. local sub = msg:sub(pos+1)
  15. --Get command and file name
  16. local command = msg:sub(0, pos-1)
  17. pos = sub:find("%s")
  18. local file = ""
  19. if pos ~= nil then
  20. file = sub:sub(0, pos-1)
  21. else
  22. file = sub
  23. end
  24. os.sleep(.1)
  25. if command == "ls" then
  26. m.send(from, port, fs.list(file))
  27. elseif command == "cd" then
  28. m.send(from, port, shell.setPath(file))
  29. elseif command == "pull" then
  30. f = fs.open(file, "r")
  31. if f == nil then
  32. m.send(from, port, "ERROR")
  33. else
  34. m.send(from, port, "OK")
  35. os.sleep(.1)
  36. m.send(from, port, f.readAll())
  37. f:close()
  38. end
  39. elseif command == "put" then
  40. f = io.open(file, "w")
  41. if pos == nil then
  42. m.send(from, port, "ERROR")
  43. else
  44. f:write(sub:sub(pos+1))
  45. f:close()
  46. m.send(from, port, "OK")
  47. end
  48. else
  49. m.send(from, port, "ERROR")
  50. end
  51. end
  52. end
  53. local port = 21
  54. if arg[1] ~= nil then
  55. port = tonumber(arg[1])
  56. end
  57. if m == nil then
  58. print("No modems found")
  59. return
  60. end
  61. if not m.isOpen(21) then
  62. m.open(21)
  63. end
  64. listen(m)