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.

142 lines
3.3 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 term = require("term")
  7. local m = component.proxy(component.list("modem")())
  8. args = {...}
  9. function doCommand(server, port, cwd, scwd)
  10. term.write(">> ")
  11. input = io.read()
  12. if input == "exit" or input == "bye" or input == "quit" then
  13. print("Bye")
  14. return
  15. end
  16. t = split(input)
  17. if t[1] == "clear" then
  18. shell.execute("clear")
  19. elseif t[1] == "lls" then
  20. local ls = {}
  21. if t[2] == nil then
  22. ls = fs.list(cwd)
  23. else
  24. ls = fs.list(cwd.."/"..t[2])
  25. end
  26. for _,v in ipairs(ls) do
  27. print(v)
  28. end
  29. elseif t[1] == "ls" then
  30. if t[2] == nil then
  31. m.send(server, port, t[1].." "..scwd)
  32. else
  33. m.send(server, port, t[1].." "..scwd.."/"..t[2])
  34. end
  35. _, _, from, port, _, msg = event.pull("modem_message")
  36. for _, v in ipairs(msg) do
  37. print(v)
  38. end
  39. elseif t[1] == "lcd" then
  40. if t[2] == nil then
  41. cwd = "/"
  42. else
  43. if t[2]:gmatch(".") == "/" then
  44. cwd = t[2]
  45. --Didn't handle .. operator
  46. else
  47. if cwd == "/" then
  48. cwd = cwd..t[2]
  49. else
  50. cwd = cwd.."/"..t[2]
  51. end
  52. end
  53. end
  54. elseif t[1] == "cd" then
  55. if t[2] == nil then
  56. scwd = "/"
  57. else
  58. if t[2]:gmatch(".") == "/" then
  59. scwd = t[2]
  60. --Didn't handle .. operator
  61. else
  62. if scwd == "/" then
  63. scwd = scwd..t[2]
  64. else
  65. scwd = scwd.."/"..t[2]
  66. end
  67. end
  68. end
  69. elseif t[1] == "put" then
  70. if t[2] == nil then
  71. print("Please supply a file to put")
  72. else
  73. ls = fs.list(cwd)
  74. --Bad search algorithm
  75. isFile = false
  76. for _,v in ipairs(ls) do
  77. if v == t[2] then
  78. isFile = true
  79. end
  80. end
  81. if isFile then
  82. if cwd == "/" then
  83. file = cwd..t[2]
  84. else
  85. file = cwd.."/"..t[2]
  86. end
  87. f = fs.open(file, "r")
  88. m.send(server, port, t[1].." "..file.." "..f.readAll())
  89. _, _, from, port, _, msg = event.pull("modem_message")
  90. print(msg)
  91. else
  92. print("File not found")
  93. end
  94. end
  95. elseif t[1] == "pull" then
  96. if t[2] == nil then
  97. print("Please supply a file to pull")
  98. else
  99. if scwd == "/" then
  100. file = scwd..t[2]
  101. else
  102. file = scwd.."/"..t[2]
  103. end
  104. m.send(server, port, t[1].." "..file)
  105. _, _, from, port, _, msg = event.pull("modem_message")
  106. if msg == "ERROR" then
  107. print("File not found")
  108. else
  109. print(msg)
  110. _, _, from, port, _, msg = event.pull("modem_message")
  111. f = io.open(file, "w")
  112. f:write(msg)
  113. f:close()
  114. end
  115. end
  116. end
  117. doCommand(m, id, from, cwd, scwd)
  118. end
  119. if m == nil then
  120. print("No modems found")
  121. return
  122. end
  123. local port = 21
  124. if args[1] ~= nil then
  125. port = tonumber(args[1])
  126. end
  127. local server = nil
  128. if args[2] == nil then
  129. f = io.open("addr", "r")
  130. server = f:read()
  131. f:close()
  132. else
  133. server = args[2]
  134. end
  135. print("FTP CLIENT")
  136. doCommand(server, port, shell.getPath(), "/")