Advertisement
ReDestroyDeR

Open FTP for OpenComputers

May 7th, 2018
303
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 10.11 KB | None | 0 0
  1. local cm = require("component")
  2. local sr = require("serialization")
  3. local os = require("os")
  4. local ss = require("term")
  5. local buffer = require("doubleBuffering")
  6. local gui = require("GUI")
  7. local event = require("event")
  8. local con = cm.modem
  9. local gpu = cm.gpu
  10. local bg = 0xE4E3E3
  11. local header = 0x135D96
  12. local fontcolor = 0x3E5567
  13. local active = 0x0B44A4
  14. local success = 0x3AF230
  15. local failure = 0xBC0000
  16. local information = {
  17.     ["UA"] = "Не выбран!",
  18.     ["FILE"] = "Не выбран!",
  19.     ["OWN"] = "Не обозначен!"
  20. }
  21.  
  22. buffer.setResolution(100, 30)
  23. local mainContainer = gui.fullScreenContainer()
  24.  
  25. local function exitProcedure()
  26.     if information["UA"] ~= "Не выбран!" then
  27.         con.send(information["UA"], 8080, "abort")
  28.     end
  29.     ss.clear()
  30.     gpu.setForeground(0xFFFFFF)
  31.     gpu.setBackground(0x000000)
  32.     gpu.setResolution(160, 50)
  33.     ss.clear()
  34.     os.exit()
  35. end
  36.  
  37. local function keyexit(eventID, ...)
  38.     if code == 17 then exitProcedure() end
  39. end
  40.  
  41. local function exitButton(func)
  42.     mainContainer:addChild(gui.roundedButton(2, 1, 10, 3, bg, fontcolor, active, bg, "Выйти")).onTouch = func
  43. end
  44.  
  45. local function setClearScreen(logotext)
  46.     buffer.clear()
  47.     mainContainer:deleteChildren()
  48.     buffer.square(0, 0, 160, 50, bg, fontcolor, " ", 0.0)
  49.     buffer.square(0, 0, 160, 5, header, fontcolor, " ", 0.0)
  50.     buffer.text(43, 2, fontcolor, logotext, 0.0)
  51. end
  52.  
  53. local function menu()
  54.     local function backButton()
  55.         setClearScreen("Open FTP")
  56.         exitButton(exitProcedure)
  57.         menu()
  58.     end
  59.  
  60.     --Выбор адреса
  61.     mainContainer:addChild(gui.roundedButton(71, 6, 15, 5, header, bg, active, fontcolor, "Выбор адреса")).onTouch = function()
  62.  
  63.         setClearScreen("Выбор Адреса")
  64.         exitButton(backButton)
  65.  
  66.         con.open(8080)
  67.         if con.isOpen(8080) then
  68.             mainContainer:addChild(gui.label(41, 10, 20, 2, success, "Порт 8080 - Открыт"))
  69.             mainContainer:addChild(gui.roundedButton(41, 20, 20, 3, header, bg, active, fontcolor, "Broadcast")).onTouch = function()
  70.                 setClearScreen("Отправка")
  71.                 exitButton(backButton)
  72.                 con.broadcast(8080, true)
  73.                 local _, own, from, port, distance, message = event.pull("modem_message")
  74.                 if message == "acproc" then
  75.                     mainContainer:addChild(gui.label(31, 14, 20, 2, header, "Адрес цели - " .. from))
  76.                     mainContainer:addChild(gui.label(36, 15, 20, 2, header, "Дистанция до цели - " .. math.round(distance) .. " метров"))
  77.                     mainContainer:addChild(gui.roundedButton(41, 10, 20, 3, header, bg, active, fontcolor, "Принять адресата")).onTouch = function()
  78.                         con.send(from, port, "acc")
  79.                         local _, own, from, port, distance, message = event.pull("modem_message")
  80.                         if message == "aprocced" then
  81.                             information["UA"] = from
  82.                             information["OWN"] = own
  83.                             gui.error("Ожидание готовности собеседника . . .")
  84.                             con.send(from, port, "AOK")
  85.                             local _, own, from, port, distance, message = event.pull("modem_message")
  86.                             if message == "ready" then
  87.                                 backButton()
  88.                             end
  89.                         end
  90.                     end
  91.                 else
  92.                     backButton()
  93.                     gui.error("Непредвиденная ошибка. Попробуйте снова")
  94.                 end
  95.             end
  96.             if information["UA"] ~= "Не выбран!" then
  97.                 backButton()
  98.                 gui.error("Уже выбрана цель! Если Вы хотите выбрать дургую цель, перезапустите программу.")
  99.             end
  100.         else
  101.             mainContainer:addChild(gui.label(41, 10, 20, 2, failure, "Порт 8080 - Закрыт"))
  102.             mainContainer:addChild(gui.label(37, 12, 20, 2, failure, "Перезапустите устройство и попробуйте снова"))
  103.         end
  104.     end
  105.  
  106.     --Выбор файла
  107.     mainContainer:addChild(gui.roundedButton(71, 11, 15, 5, header, bg, active, fontcolor, "Выбор файла")).onTouch = function()
  108.         setClearScreen("Выбор Файла")
  109.         exitButton(backButton)
  110.         mainContainer:addChild(gui.label(41, 6, 20, 1, header, "Выберете файл для отправки"))
  111.         local filesystemChooser = mainContainer:addChild(gui.filesystemChooser(41, 15, 20, 3, 0xE1E1E1, 0x888888, 0x3C3C3C, 0x888888, nil, "Подтвердить", "Отменить", "Выбрать", "/"))
  112.         filesystemChooser:setMode(gui.filesystemModes.open, gui.filesystemModes.file)
  113.         filesystemChooser.onSubmit = function(path)
  114.             information["FILE"] = path
  115.             con.send(information["UA"], 8080, "selected")
  116.             backButton()
  117.             gui.error("Вы выбрали файл. Теперь нажмите кнопку 'Отправка' для того что бы начать отправку файла.")
  118.         end
  119.         if information["FILE"] ~= "Не выбран!" then
  120.             gui.error("Вы уже выбрали файл! Если Вы хотите отправить что-то другое, начните все заного!")
  121.             backButton()
  122.         end
  123.     end
  124.  
  125.     --Отправка файла
  126.     mainContainer:addChild(gui.roundedButton(71, 16, 15, 5, header, bg, active, fontcolor, "Отправка")).onTouch = function()
  127.         setClearScreen("Отправка")
  128.         exitButton(backButton)
  129.         if information["UA"] ~= "Не выбран!" and information["OWN"] ~= "Не обозначен!" and information["FILE"] ~= "Не выбран!" then
  130.             mainContainer:addChild(gui.roundedButton(41, 10, 15, 5, header, bg, active, fontcolor, "Отправить!")).onTouch = function()
  131.                 local file = io.open(information["FILE"])
  132.                 local lines = {}
  133.                 for line in io.lines(information["FILE"]) do table.insert(lines, line) end
  134.                 con.send(information["UA"], 8080, false)
  135.                 con.send(information["UA"], 8080, sr.serialize(lines))
  136.                 local _, own, from, port, distance, message = event.pull("modem_message")
  137.                 if message == "success" then
  138.                     os.sleep(5)
  139.                     exitProcedure()
  140.                 end
  141.             end
  142.         else
  143.             backButton()
  144.             gui.error("Не выбран файл/адресат! Попробуйте снова когда заполните данную информацию.")
  145.         end
  146.     end
  147.  
  148.     --Прием файла
  149.     mainContainer:addChild(gui.roundedButton(71, 21, 15, 5, header, bg, active, fontcolor, "Приём")).onTouch = function()
  150.         setClearScreen("Приём")
  151.         exitButton(backButton)
  152.         con.open(8080)
  153.         if con.isOpen(8080) then
  154.             mainContainer:addChild(gui.label(41, 10, 20, 2, success, "Порт 8080 - Открыт"))
  155.             mainContainer:addChild(gui.roundedButton(37, 12, 20, 3, header, bg, active, fontcolor, "Начать прослушку порта")).onTouch = function()
  156.                 local _, own, from, port, distance, message = event.pull("modem_message")
  157.                 if message then
  158.                     information["OWN"] = own
  159.                     con.send(from, port, "acproc")
  160.                     local _, own, from, port, distance, message = event.pull("modem_message")
  161.                     if message == "acc" then
  162.                         setClearScreen("Получение данных от " .. from)
  163.                         exitButton(backButton)
  164.                         con.send(from, port, "aprocced")
  165.                         local _, own, from, port, distance, message = event.pull("modem_message")
  166.                         if message == "AOK" then
  167.                             mainContainer:addChild(gui.label(41, 16, 20, 2, header, "Выберите путь сохранения получаемых файлов"))
  168.                             local filesystemChooser = mainContainer:addChild(gui.filesystemChooser(37, 17, 10, 3, 0xE1E1E1, 0x888888, 0x3C3C3C, 0x888888, "downloaded.lua", "Подтвердить", "Отменить", "Выбрать", "/"))
  169.                             filesystemChooser:setMode(gui.filesystemModes.save, gui.filesystemModes.directory)
  170.                             filesystemChooser.onSubmit = function(path)
  171.                                 mainContainer:deleteChildren()
  172.                                 buffer.clear()
  173.                                 mainContainer:stopEventHandling()
  174.                                 mainContainer:drawOnScreen(false)
  175.                                 buffer.draw(false)
  176.                                 ss.clear()
  177.                                 gpu.setBackground(bg)
  178.                                 gpu.setForeground(header)
  179.                                 con.send(from, 8080, "ready")
  180.                                 print("Переход в минималистичный режим . . . Выполнен")
  181.                                 while true do
  182.                                     local size = 0
  183.                                     local current = 0
  184.                                     local lines = {}
  185.                                     local _, own, from, port, distance, message = event.pull("modem_message")
  186.                                     if message == "selected" then
  187.                                         gpu.setBackground(bg)
  188.                                         gpu.setForeground(header)
  189.                                         print("Отправитель выбрал файл . . .")
  190.                                     end
  191.                                     if message == false then
  192.                                         local _, own, from, port, distance, message = event.pull("modem_message")
  193.                                         local lines = sr.unserialize(message)
  194.                                         file = io.open(path, "w")
  195.                                         for i = 1, #lines do file:write(lines[i] .. "\n") end
  196.                                         file:close()
  197.                                         gpu.setForeground(success)
  198.                                         print("Файл получен и сохранен по пути - " .. path)
  199.                                         con.send(from, 8080, "success")
  200.                                         os.sleep(5)
  201.                                         exitProcedure()
  202.                                         break
  203.                                     end
  204.                                 end
  205.                             end
  206.                         end
  207.                     else
  208.                         backButton()
  209.                         gui.error("Непредвиденная ошибка. Попробуйте снова")
  210.                     end    
  211.                 end
  212.             end
  213.             if information["UA"] ~= "Не выбран!" then
  214.                 backButton()
  215.                 gui.error("Вы являетесь отправителем! Если Вы хотите принимать файлы - перезапустите программу.")
  216.             end
  217.         else
  218.             mainContainer:addChild(gui.label(41, 10, 20, 2, failure, "Порт 8080 - Закрыт"))
  219.             mainContainer:addChild(gui.label(37, 12, 20, 2, failure, "Перезапустите устройство и попробуйте снова"))
  220.         end
  221.     end
  222.  
  223.     --Все что было введено пользователем
  224.     mainContainer:addChild(gui.label(2, 6, 20, 2, fontcolor, "Адрес цели: " .. information["UA"]))
  225.     mainContainer:addChild(gui.label(2, 8, 20, 2, fontcolor, "Файл для отправки: " .. information["FILE"]))
  226.     mainContainer:addChild(gui.label(2, 10, 20, 2, fontcolor, "Ваш адрес: " .. information["OWN"]))
  227. end
  228.  
  229. event.listen("key_up", keyexit)
  230. setClearScreen("Open FTP")
  231. exitButton(exitProcedure)
  232. menu()
  233.  
  234. buffer.draw(true)
  235.  
  236. mainContainer:drawOnScreen(true)
  237. mainContainer:startEventHandling()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement