Advertisement
programcreator

FTP: client [WIP][Requires IPnet]

Feb 9th, 2015
462
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 25.45 KB | None | 0 0
  1. -------------FTP Client v1.0---------
  2. ---------------Program---------------
  3. --------------by Creator-------------
  4.  
  5. --Variables--
  6. term.redirect(term.native())
  7. local settDir = "FTPconfig"
  8. local textutilsserialize = textutils.serialize
  9. local textutilsunserialize = textutils.unserialize
  10. local w, h = term.getSize()
  11. local pathToCopy = ""
  12. local userWindow = {
  13.     term.current(),
  14.     math.floor((w-30)/2),
  15.     math.floor((h-6)/2),
  16.     30,
  17.     6,
  18.     true,
  19.     "Username",
  20.     colors.white,
  21.     colors.blue,
  22.     "Input your username: ",
  23.     colors.black,
  24.     colors.lightGray,
  25.     colors.white,
  26.     colors.black,
  27. }
  28. local passwordWindow = {
  29.     term.current(),
  30.     math.floor((w-30)/2),
  31.     math.floor((h-6)/2),
  32.     30,
  33.     6,
  34.     true,
  35.     "Password",
  36.     colors.white,
  37.     colors.blue,
  38.     "Input your password: ",
  39.     colors.black,
  40.     colors.lightGray,
  41.     colors.white,
  42.     colors.black,
  43. }
  44. local domainWindow = {
  45.     term.current(),
  46.     math.floor((w-32)/2),
  47.     math.floor((h-8)/2),
  48.     32,
  49.     8,
  50.     true,
  51.     "Domain of the FTP server",
  52.     colors.white,
  53.     colors.blue,
  54.     "Write the domain in the format:%xPos.yPos.zPos.id <== numbers%or%domainName.com",
  55.     colors.black,
  56.     colors.lightGray,
  57.     colors.white,
  58.     colors.black,
  59. }
  60. local folderMenu = {
  61.     term.current(),
  62.     2,
  63.     math.floor((h-6)/2),
  64.     48,
  65.     6,
  66.     true,
  67.     "Folder Options",
  68.     colors.white,
  69.     colors.blue,
  70.     "What would you%like to do?",
  71.     colors.black,
  72.     colors.lightGray,
  73.     {
  74.         {
  75.             "Open",
  76.             colors.black,
  77.             colors.blue,
  78.             "open",
  79.         },
  80.         {
  81.             "Delete",
  82.             colors.black,
  83.             colors.blue,
  84.             "delete",
  85.         },
  86.         {
  87.             "Rename",
  88.             colors.black,
  89.             colors.blue,
  90.             "rename",
  91.         },
  92.         {
  93.             "Move",
  94.             colors.black,
  95.             colors.blue,
  96.             "move",
  97.         },
  98.         {
  99.             "Cancel",
  100.             colors.black,
  101.             colors.blue,
  102.             "cancel",
  103.         },
  104.     },
  105. }
  106. local fileMenu = {
  107.     term.current(),
  108.     2,
  109.     math.floor((h-6)/2),
  110.     48,
  111.     6,
  112.     true,
  113.     "File Options",
  114.     colors.white,
  115.     colors.blue,
  116.     "What would you%like to do?",
  117.     colors.black,
  118.     colors.lightGray,
  119.     {
  120.         {
  121.             "Open",
  122.             colors.black,
  123.             colors.blue,
  124.             "open",
  125.         },
  126.         {
  127.             "Delete",
  128.             colors.black,
  129.             colors.blue,
  130.             "delete",
  131.         },
  132.         {
  133.             "Rename",
  134.             colors.black,
  135.             colors.blue,
  136.             "rename",
  137.         },
  138.         {
  139.             "Download",
  140.             colors.black,
  141.             colors.blue,
  142.             "download",
  143.         },
  144.         {
  145.             "Move",
  146.             colors.black,
  147.             colors.blue,
  148.             "move",
  149.         },
  150.         {
  151.             "Cancel",
  152.             colors.black,
  153.             colors.blue,
  154.             "cancel",
  155.         },
  156.     },
  157. }
  158. local nilMenu = {
  159.     term.current(),
  160.     2,
  161.     math.floor((h-6)/2),
  162.     48,
  163.     6,
  164.     true,
  165.     "Options",
  166.     colors.white,
  167.     colors.blue,
  168.     "What would you%like to do?",
  169.     colors.black,
  170.     colors.lightGray,
  171.     {
  172.         {
  173.             "Make a folder",
  174.             colors.black,
  175.             colors.blue,
  176.             "makeFolder",
  177.         },
  178.         {
  179.             "Make a file",
  180.             colors.black,
  181.             colors.blue,
  182.             "makeFile",
  183.         },
  184.         {
  185.             "Upload",
  186.             colors.black,
  187.             colors.blue,
  188.             "uploadFile",
  189.         },
  190.         {
  191.             "Cancel",
  192.             colors.black,
  193.             colors.blue,
  194.             "cancel",
  195.         },
  196.     },
  197. }
  198. local newFolderWindow = {
  199.     term.current(),
  200.     math.floor((w-32)/2),
  201.     math.floor((h-8)/2),
  202.     32,
  203.     8,
  204.     true,
  205.     "New folder name",
  206.     colors.white,
  207.     colors.blue,
  208.     "Write the name of%the new folder.",
  209.     colors.black,
  210.     colors.lightGray,
  211.     colors.white,
  212.     colors.black,
  213. }
  214. local newFileWindow = {
  215.     term.current(),
  216.     math.floor((w-32)/2),
  217.     math.floor((h-8)/2),
  218.     32,
  219.     8,
  220.     true,
  221.     "New file name",
  222.     colors.white,
  223.     colors.blue,
  224.     "Write the name of%the new file.",
  225.     colors.black,
  226.     colors.lightGray,
  227.     colors.white,
  228.     colors.black,
  229. }
  230. local upFileWindow = {
  231.     term.current(),
  232.     math.floor((w-32)/2),
  233.     math.floor((h-8)/2),
  234.     32,
  235.     8,
  236.     true,
  237.     "File path",
  238.     colors.white,
  239.     colors.blue,
  240.     "Write the path of%the file.",
  241.     colors.black,
  242.     colors.lightGray,
  243.     colors.white,
  244.     colors.black,
  245. }
  246. local moveFolderWindow = {
  247.     term.current(),
  248.     math.floor((w-32)/2),
  249.     math.floor((h-8)/2),
  250.     32,
  251.     8,
  252.     true,
  253.     "New folder path",
  254.     colors.white,
  255.     colors.blue,
  256.     "Write the name of%the new folder path.",
  257.     colors.black,
  258.     colors.lightGray,
  259.     colors.white,
  260.     colors.black,
  261. }
  262. local bgColor = colors.black
  263. local txtColor = colors.white
  264. local domain = {}
  265. local currDir = ""
  266. local files = {}
  267. local Buttons = {
  268.     {
  269.         2,
  270.         2,
  271.         7,
  272.         1,
  273.         "Log Out",
  274.         colors.white,
  275.         colors.green,
  276.         "logout"
  277.     },
  278.     {
  279.         11,
  280.         2,
  281.         7,
  282.         1,
  283.         "Refresh",
  284.         colors.white,
  285.         colors.green,
  286.         "refresh"
  287.     },
  288.     {
  289.         20,
  290.         2,
  291.         2,
  292.         1,
  293.         "Up",
  294.         colors.white,
  295.         colors.green,
  296.         "up"
  297.     },
  298. }
  299. local folderIcon = paintutils.loadImage(settDir.."/folderIcon")
  300. local fileIcon = paintutils.loadImage(settDir.."/fileIcon")
  301. local scroll = 0
  302. local globalButtonList = {}
  303. local notEnded = true
  304. local fileExtensions = {
  305. nfp = "nPaintPro",
  306. nfa = "nPaintPro",
  307. txt = "edit",
  308. }
  309.  
  310. --APIs--
  311. os.loadAPI("IPclient")
  312. os.loadAPI("apis/sha")
  313. os.loadAPI("apis/encrypt")
  314. IPclient.toReturn()
  315.  
  316. --Functions--
  317.  
  318. function detectButtonHit(buttonsToTest)
  319.     local event, button, x, y
  320.     repeat
  321.         event, button, x, y = os.pullEvent()
  322.     until event == "mouse_click" or event == "key"
  323.     if event == "mouse_click" then
  324.         for i,v in pairs(buttonsToTest) do
  325.             if v[1] <= x and x <= v[3] and v[2] <= y and y <= v[4] then
  326.                 return {v[5], button}
  327.             end
  328.         end
  329.     elseif event == "key" then
  330.         return {"key:"..tostring(button)}
  331.     end
  332.     return {"pong"}
  333. end
  334.  
  335. function mkStrShort(str,lenght,n)
  336.     local bufferTable = {}
  337.     local toReturnTable = {}
  338.     local lenghtN = tonumber(lenght)
  339.     if lenghtN == nil then return false end
  340.     for i = 0,n-1 do
  341.         bufferTable[i+1] = string.sub(tostring(str),(i*lenghtN)+1,(i*lenghtN)+lenghtN)
  342.     end
  343.     for i,v in pairs(bufferTable) do
  344.         if v ~= nil then
  345.             toReturnTable[#toReturnTable+1] = v
  346.         end
  347.     end
  348.     return toReturnTable
  349. end
  350.  
  351. function clear(bgColorArg)
  352.     term.setBackgroundColor(bgColorArg or bgColor)
  353.     term.setTextColor(txtColor)
  354.     term.setCursorPos(1,1)
  355.     term.clear()
  356.     globalButtonList = {}
  357. end
  358.  
  359. function dialogBox(tableInput,clearBg)
  360.     if clearBg ~= false then
  361.         clear(colors.cyan)
  362.     end
  363.     local nTable = {}
  364.     dialogBoxWindow = window.create(
  365.     tableInput[1],
  366.     tableInput[2],
  367.     tableInput[3],
  368.     tableInput[4],
  369.     tableInput[5])
  370.     dialogBoxWindow.setBackgroundColor(tableInput[9])
  371.     dialogBoxWindow.setCursorPos(2,1)
  372.     dialogBoxWindow.clearLine()
  373.     dialogBoxWindow.setTextColor(tableInput[8])
  374.     dialogBoxWindow.write(tableInput[7])
  375.     dialogBoxWindow.setCursorPos(1,2)
  376.     dialogBoxWindow.setBackgroundColor(tableInput[12])
  377.     for i = 2 , tableInput[5] do
  378.         dialogBoxWindow.setCursorPos(1,i)
  379.         dialogBoxWindow.clearLine()
  380.     end
  381.     dialogBoxWindow.setCursorPos(1,2)
  382.     dialogBoxWindow.setTextColor(tableInput[11])
  383.     for token in string.gmatch(tableInput[10],"[^%%]+") do
  384.         nTable[#nTable+1] = token
  385.     end
  386.     for i,v in pairs(nTable) do
  387.         dialogBoxWindow.setCursorPos(2,1+i)
  388.         dialogBoxWindow.write(v)
  389.     end
  390.     local totalLenght = 0
  391.     globalButtonList = {}
  392.     for i,v in pairs(tableInput[13]) do
  393.         dialogBoxWindow.setCursorPos(2+totalLenght,tableInput[5]-1)
  394.         dialogBoxWindow.setTextColor(v[2])
  395.         dialogBoxWindow.setBackgroundColor(v[3])
  396.         local toWrite = " "..v[1].." "
  397.         dialogBoxWindow.write(toWrite)
  398.         if globalButtonList == nil then
  399.             globalButtonList = {{tableInput[2]+1+totalLenght,tableInput[3] + tableInput[5]-2,tableInput[2]+totalLenght + #toWrite,tableInput[3] + tableInput[5]-1,v[4]}}
  400.         else
  401.             globalButtonList[#globalButtonList+1] = {tableInput[2]+1+totalLenght,tableInput[3] + tableInput[5]-2,tableInput[2]+totalLenght + #toWrite,tableInput[3] + tableInput[5]-1,v[4]}
  402.         end
  403.         totalLenght = #toWrite + totalLenght + 2
  404.     end
  405.     local repeatIt = true
  406.     while repeatIt == true do
  407.         local unparsedResult = detectButtonHit(globalButtonList)
  408.         result = unparsedResult[1]
  409.         if result ~= "pong" then
  410.             repeatIt = false
  411.         end
  412.     end
  413.     return result
  414. end
  415.  
  416. function textBox(tableInput,secret,clearBg)
  417.     if clearBg ~= false then
  418.         clear(colors.cyan)
  419.     end
  420.     local nTable = {}
  421.     textBoxWindow = window.create(tableInput[1],tableInput[2],tableInput[3],tableInput[4],tableInput[5])
  422.     textBoxWindow.setCursorPos(2,1)
  423.     textBoxWindow.setBackgroundColor(tableInput[9])
  424.     textBoxWindow.clearLine()
  425.     textBoxWindow.setTextColor(tableInput[8])
  426.     textBoxWindow.write(tableInput[7])
  427.     textBoxWindow.setCursorPos(1,2)
  428.     textBoxWindow.setBackgroundColor(tableInput[12])
  429.     for i = 2 , tableInput[5] do
  430.         textBoxWindow.setCursorPos(1,i)
  431.         textBoxWindow.clearLine()
  432.     end
  433.     textBoxWindow.setTextColor(tableInput[11])
  434.     for token in string.gmatch(tableInput[10],"[^%%]+") do
  435.         nTable[#nTable+1] = token
  436.     end
  437.     for i,v in pairs(nTable) do
  438.         textBoxWindow.setCursorPos(2,1+i)
  439.         textBoxWindow.write(v)
  440.     end
  441.     textBoxWindow.setTextColor(tableInput[13])
  442.     textBoxWindow.setBackgroundColor(tableInput[14])
  443.     textBoxWindow.setCursorPos(2,tableInput[5]-1)
  444.     textBoxWindow.clearLine()
  445.     textBoxWindow.setCursorPos(2,tableInput[5]-1)
  446.     textBoxWindow.setTextColor(tableInput[13])
  447.     textBoxWindow.setBackgroundColor(tableInput[14])
  448.     if secret then
  449.         return read("*")
  450.     else
  451.         return read()
  452.     end
  453. end
  454.  
  455. function dns()
  456.     textutils.slowPrint("Contacting DNS server...",3)
  457.     sleep(2)
  458. end
  459.  
  460. function getDomainName()
  461.     local nTable = {}
  462.     local srtAna = ""
  463.     local repeatBoolean = true
  464.     local number = true
  465.    
  466.     local function domainValidation()
  467.         local sDomain = textBox(domainWindow)
  468.         local checkIfNumOrLetter = string.sub(sDomain,1,1)
  469.         checkIfNumOrLetter = tonumber(checkIfNumOrLetter)
  470.         if type(checkIfNumOrLetter) == nil then
  471.             dns()
  472.         else
  473.             for token in string.gmatch(sDomain,"[^%.]+") do
  474.                 nTable[#nTable + 1] = token
  475.             end
  476.         end
  477.     end
  478.    
  479.     repeat
  480.         domainValidation()
  481.         if #nTable == 4 then
  482.             for i,v in pairs(nTable) do
  483.                 if type(tonumber(v)) ~= "number" then
  484.                     number = false
  485.                 end
  486.             end
  487.             print(number)
  488.             if number == true then
  489.                 for i,v in pairs(nTable) do
  490.                     nTable[i] = tonumber(v)
  491.                 end
  492.                 if 0 <= nTable[2] and nTable[2] <= 255 and 0 <= nTable[4] then
  493.                     repeatBoolean = false
  494.                 end
  495.             end
  496.         end
  497.     until repeatBoolean == false
  498.     domain = nTable
  499.     term.write(textutilsserialize(domain))
  500. end
  501.  
  502. function login()
  503.     currDir = "/"
  504.     files = {}
  505.     local username = ""
  506.     local password = ""
  507.     local answer = dialogBox({
  508.         term.current(),
  509.         math.floor((w-30)/2),
  510.         math.floor((h-6)/2),
  511.         30,
  512.         6,
  513.         true,
  514.         "Login Info",
  515.         colors.white,
  516.         colors.blue,
  517.         "Do you have an account%at the server?",
  518.         colors.black,
  519.         colors.lightGray,
  520.         {
  521.             {
  522.                 "Yes",
  523.                 colors.black,
  524.                 colors.blue,
  525.                 "yes",
  526.             },
  527.             {
  528.                 "No",
  529.                 colors.black,
  530.                 colors.blue,
  531.                 "no",
  532.             },
  533.         },
  534.     })
  535.     if answer == "no" then
  536.         username = textBox({
  537.             term.current(),
  538.             math.floor((w-30)/2),
  539.             math.floor((h-6)/2),
  540.             30,
  541.             6,
  542.             true,
  543.             "Username",
  544.             colors.white,
  545.             colors.blue,
  546.             "Input your new username: ",
  547.             colors.black,
  548.             colors.lightGray,
  549.             colors.white,
  550.             colors.black,
  551.         },false)
  552.         password = textBox({
  553.             term.current(),
  554.             math.floor((w-30)/2),
  555.             math.floor((h-6)/2),
  556.             30,
  557.             6,
  558.             true,
  559.             "Password",
  560.             colors.white,
  561.             colors.blue,
  562.             "Input your new password: ",
  563.             colors.black,
  564.             colors.lightGray,
  565.             colors.white,
  566.             colors.black,
  567.         },true)
  568.         --shaOfPass = sha.digestStr(password)
  569.         IPclient.send(domain,{"ftp",23,{username,password}})
  570.         msgToParse = IPclient.receive()
  571.         if pcall(textutilsunserialize,msgToParse) then
  572.             messageTable = textutilsunserialize(msgToParse)
  573.             sender = messageTable[2]
  574.             message = messageTable[3]
  575.             if message[1] == "ftp" then
  576.                 if message[2] == 22 then
  577.                     if message[3][1] == "success" then
  578.                         dialogBox({
  579.                             term.current(),
  580.                             math.floor((w-30)/2),
  581.                             math.floor((h-6)/2),
  582.                             30,
  583.                             6,
  584.                             true,
  585.                             "Succes",
  586.                             colors.white,
  587.                             colors.blue,
  588.                             "The registration was%successful!",
  589.                             colors.black,
  590.                             colors.lightGray,
  591.                             {
  592.                                 {
  593.                                     "OK",
  594.                                     colors.black,
  595.                                     colors.blue,
  596.                                     "ok",
  597.                                 },
  598.                             },
  599.                         })
  600.                     elseif message[3][1] == "fail" then
  601.                         dialogBox({
  602.                             term.current(),
  603.                             math.floor((w-30)/2),
  604.                             math.floor((h-6)/2),
  605.                             30,
  606.                             6,
  607.                             true,
  608.                             "Failure",
  609.                             colors.white,
  610.                             colors.blue,
  611.                             "The registration was%unsuccessful!",
  612.                             colors.black,
  613.                             colors.lightGray,
  614.                             {
  615.                                 {
  616.                                     "OK",
  617.                                     colors.black,
  618.                                     colors.blue,
  619.                                     "ok",
  620.                                 },
  621.                             },
  622.                         })
  623.                         error("The registration was unsuccessful!")
  624.                     end
  625.                 end
  626.             end
  627.         end
  628.     end
  629.    
  630.     username = textBox(userWindow,false)
  631.         password = textBox(passwordWindow,true)
  632.         IPclient.send(domain,{"ftp",1,{username,password}})
  633.         msgToParse = IPclient.receive()
  634.         if pcall(textutilsunserialize,msgToParse) then
  635.             messageTable = textutilsunserialize(msgToParse)
  636.             sender = messageTable[2]
  637.             message = messageTable[3]
  638.             if message[1] == "ftp" then
  639.                 if message[2] == 0 then
  640.                     if message[3][1] == "success" then
  641.                         dialogBox({
  642.                             term.current(),
  643.                             math.floor((w-30)/2),
  644.                             math.floor((h-6)/2),
  645.                             30,
  646.                             6,
  647.                             true,
  648.                             "Succes",
  649.                             colors.white,
  650.                             colors.blue,
  651.                             "The login was successful!",
  652.                             colors.black,
  653.                             colors.lightGray,
  654.                             {
  655.                                 {
  656.                                     "OK",
  657.                                     colors.black,
  658.                                     colors.blue,
  659.                                     "ok",
  660.                                 },
  661.                             },
  662.                         })
  663.                     elseif message[3][1] == "fail" then
  664.                         dialogBox({
  665.                             term.current(),
  666.                             math.floor((w-30)/2),
  667.                             math.floor((h-6)/2),
  668.                             30,
  669.                             6,
  670.                             true,
  671.                             "Failure",
  672.                             colors.white,
  673.                             colors.blue,
  674.                             "The login was unsuccessful!",
  675.                             colors.black,
  676.                             colors.lightGray,
  677.                             {
  678.                                 {
  679.                                     "OK",
  680.                                     colors.black,
  681.                                     colors.blue,
  682.                                     "ok",
  683.                                 },
  684.                             },
  685.                         })
  686.                         error("The login was unsuccessful!")
  687.                     end
  688.                 end
  689.             end
  690.            
  691.         end
  692.     files = refresh(currDir)
  693. end
  694.  
  695. function getFiles(dir)
  696.     local bufferFiles = {}
  697.     for i,v in pairs(fs.list(dir)) do
  698.         if fs.isDir(currDir.."/"..v) then
  699.             bufferFiles[#bufferFiles+1] = {v,"folder"}
  700.         else
  701.             bufferFiles[#bufferFiles+1] = {v,"file"}
  702.         end
  703.     end
  704.     files = bufferFiles
  705. end
  706.  
  707. function drawOptions(tableInputDrawOptions)
  708.     for i,v in pairs(tableInputDrawOptions) do
  709.         term.setCursorPos(v[1],v[2])
  710.         paintutils.drawFilledBox(v[1],v[2],v[3]+v[1]-1,v[4]+v[2]-1,v[7])
  711.         term.setCursorPos(v[1],v[2])
  712.         term.setTextColor(v[6])
  713.         term.write(v[5])
  714.         if globalButtonList == nil then
  715.             globalButtonList = {{v[1],v[2],v[3]+v[1]-1,v[4]+v[2]-1,"button:"..v[8]}}
  716.         else
  717.             globalButtonList[#globalButtonList+1] = {v[1],v[2],v[3]+v[1]-1,v[4]+v[2]-1,"button:"..v[8]}
  718.         end
  719.     end
  720. end
  721.  
  722. function drawFiles(filesToDisplay)
  723.     local numItemsX = math.floor((w-1)/10)
  724.     numItemsY = math.ceil(#filesToDisplay/numItemsX)
  725.     local currFile = 1
  726.     for i = 0 , numItemsY-1 do
  727.         for k = 0 , numItemsX - 1 do
  728.             if currFile > #filesToDisplay then
  729.                 break
  730.             else
  731.                 term.setTextColor(colors.black)
  732.                 if filesToDisplay[currFile][2] == "file" then
  733.                     paintutils.drawImage(fileIcon,(k*10)+2,(i*5)+4-scroll)
  734.                     for l,m in pairs(mkStrShort(filesToDisplay[currFile][1],6,3)) do
  735.                         term.setCursorPos((k*10)+3,(i*5)+4+l-scroll)
  736.                         term.write(m)
  737.                     end
  738.                     if ((i*5)+4-scroll) < 4 then
  739.                         if ((i*5)+7-scroll) >= 4 then
  740.                             if globalButtonList == nil then
  741.                                 globalButtonList = {{((k*10)+2),4,((k*10)+9),((i*5)+7-scroll),"file:"..filesToDisplay[currFile][1]}}
  742.                             else
  743.                                 globalButtonList[#globalButtonList+1] = {((k*10)+2),4,((k*10)+9),((i*5)+7-scroll),"file:"..filesToDisplay[currFile][1]}
  744.                             end
  745.                         end
  746.                     else
  747.                         if globalButtonList == nil then
  748.                             globalButtonList = {{((k*10)+2),((i*5)+4-scroll),((k*10)+9),((i*5)+7-scroll),"file:"..filesToDisplay[currFile][1]}}
  749.                         else
  750.                             globalButtonList[#globalButtonList+1] = {((k*10)+2),((i*5)+4-scroll),((k*10)+9),((i*5)+7-scroll),"file:"..filesToDisplay[currFile][1]}
  751.                         end
  752.                     end
  753.                 elseif filesToDisplay[currFile][2] == "folder" then
  754.                     paintutils.drawImage(folderIcon,(k*10)+2,(i*5)+4-scroll)
  755.                     for l,m in pairs(mkStrShort(filesToDisplay[currFile][1],6,3)) do
  756.                         term.setCursorPos((k*10)+3,(i*5)+4+l-scroll)
  757.                         term.write(m)
  758.                     end
  759.                     if ((i*5)+4-scroll) < 4 then
  760.                         if ((i*5)+7-scroll) >= 4 then
  761.                             if globalButtonList == nil then
  762.                                 globalButtonList = {{((k*10)+2),4,((k*10)+9),((i*5)+7-scroll),"folder:"..filesToDisplay[currFile][1]}}
  763.                             else
  764.                                 globalButtonList[#globalButtonList+1] = {((k*10)+2),4,((k*10)+9),((i*5)+7-scroll),"folder:"..filesToDisplay[currFile][1]}
  765.                             end
  766.                         end
  767.                     else
  768.                         if globalButtonList == nil then
  769.                             globalButtonList = {{((k*10)+2),((i*5)+4-scroll),((k*10)+9),((i*5)+7-scroll),"folder:"..filesToDisplay[currFile][1]}}
  770.                         else
  771.                             globalButtonList[#globalButtonList+1] = {((k*10)+2),((i*5)+4-scroll),((k*10)+9),((i*5)+7-scroll),"folder:"..filesToDisplay[currFile][1]}
  772.                         end
  773.                     end
  774.                 end
  775.                 currFile = currFile + 1
  776.             end
  777.         end
  778.     end
  779. end
  780.  
  781. function refresh(dir)
  782.     IPclient.send(domain,{"ftp",5,{dir}})
  783.     filesListSer = IPclient.receive()
  784.     filesList = textutilsunserialize(filesListSer)
  785.     return filesList[3][3][1]
  786. end
  787.  
  788. function drawSideBar()
  789.     local lenghtSideBar = h-4
  790.     if numItemsY ~= 0 then
  791.          lenghtSideBar = math.ceil(((h-5)*(h-5))/(numItemsY*5))
  792.     end
  793.     paintutils.drawLine(w,3,w,3+lenghtSideBar,colors.green)
  794.     term.setBackgroundColor(colors.blue)
  795.     term.setCursorPos(w,3)
  796.     term.write("^")
  797.     term.setCursorPos(w,h-1)
  798.     term.write("v")
  799.     if globalButtonList == nil then
  800.         globalButtonList = {{w,3,w,3,"button:scrollUp"}}
  801.     else
  802.         globalButtonList[#globalButtonList+1] = {w,3,w,3,"button:scrollUp"}
  803.     end
  804.     if globalButtonList == nil then
  805.         globalButtonList = {{w,h-1,w,h-1,"button:scrollDown"}}
  806.     else
  807.         globalButtonList[#globalButtonList+1] = {w,h-1,w,h-1,"button:scrollDown"}
  808.     end
  809. end
  810.  
  811. function program(extension)
  812.     if fileExtensions[extension] ~= nil then
  813.         return fileExtensions[extension]
  814.     else
  815.         return "edit"
  816.     end
  817. end
  818.  
  819. function main(filesToDisplay,buttonsToDisplay)
  820.     clear(colors.white)
  821.     drawFiles(filesToDisplay)
  822.     drawSideBar()
  823.     term.setBackgroundColor(colors.orange)
  824.     for i = 1,2 do
  825.         term.setCursorPos(1,i)
  826.         term.clearLine()
  827.     end
  828.     term.setCursorPos(1,1)
  829.     term.setTextColor(colors.black)
  830.     term.write("Creator\'s FTPclient v1.0")
  831.     term.setCursorPos(w,1)
  832.     term.setBackgroundColor(colors.magenta)
  833.     term.write("X")
  834.     if globalButtonList == nil then
  835.         globalButtonList = {{w,1,w,1,"button:x"}}
  836.     else
  837.         globalButtonList[#globalButtonList+1] = {w,1,w,1,"button:x"}
  838.     end
  839.     if globalButtonList == nil then
  840.         globalButtonList = {{1,3,w,h,"nil:nil"}}
  841.     else
  842.         globalButtonList[#globalButtonList+1] = {1,4,w,h,"nil:nil"}
  843.     end
  844.     drawOptions(buttonsToDisplay)
  845.     if globalButtonList == nil then
  846.         globalButtonList = {{1,1,w,3,"nil:nilnil"}}
  847.     else
  848.         globalButtonList[#globalButtonList+1] = {1,1,w,3,"nil:nilnil"}
  849.     end
  850.     term.setCursorPos(1,h)
  851.     term.setBackgroundColor(colors.orange)
  852.     term.clearLine()
  853.     term.setTextColor(colors.black)
  854.     term.write(currDir)
  855.     term.setCursorPos(1,1)
  856.     local detectedButtonUnparsedTable = detectButtonHit(globalButtonList)
  857.     local detectedButtonUnparsed = detectedButtonUnparsedTable[1]
  858.     local button = detectedButtonUnparsedTable[2]
  859.     local detectedButtonParsedTable = {}
  860.     for token in string.gmatch(detectedButtonUnparsed,"[^:]+") do
  861.         detectedButtonParsedTable[#detectedButtonParsedTable + 1] = token
  862.     end
  863.     local action = detectedButtonParsedTable[2]
  864.     if detectedButtonParsedTable[1] == "button" then
  865.         if action == "x" then
  866.             term.setBackgroundColor(colors.black)
  867.             clear()
  868.             print("Thank you for using Creator\'s FTPclient. More coming soon...")
  869.             notEnded = false
  870.         elseif action == "up" then
  871.             scroll = 0
  872.             if currDir == "/" then
  873.             else
  874.                 local currDirBuffer = {}
  875.                 for token in string.gmatch(currDir,"(/[^/]+)") do
  876.                     currDirBuffer[#currDirBuffer + 1] = token
  877.                 end
  878.                 currDir = ""
  879.                 if #currDirBuffer == 1 then
  880.                     currDir = ""
  881.                 else
  882.                     for i = 1, #currDirBuffer-1 do
  883.                         if i == 1 then
  884.                             currDir = currDirBuffer[1]
  885.                         else
  886.                             currDir = currDir..currDirBuffer[i]
  887.                         end
  888.                     end
  889.                 end
  890.             end
  891.             files = refresh(currDir)
  892.         elseif action == "logout" then
  893.             login()
  894.             files = refresh(currDir)
  895.         elseif action == "refresh" then
  896.             files = refresh(currDir)
  897.         elseif action == "scrollUp" then
  898.             scroll = scroll - 1
  899.             if scroll < 0 then
  900.                 scroll = 0
  901.             end
  902.         elseif action == "scrollDown" then
  903.             scroll = scroll + 1
  904.             if scroll > numItemsY*6 - h then
  905.                 scroll = scroll - 1
  906.             end
  907.         end
  908.     elseif detectedButtonParsedTable[1] == "key" then
  909.         if tonumber(action) == keys.up then
  910.             scroll = scroll - 1
  911.             if scroll < 0 then
  912.                 scroll = 0
  913.             end
  914.         elseif tonumber(action) == keys.down then
  915.             scroll = scroll + 1
  916.             if scroll > numItemsY*6 - h then
  917.                 scroll = scroll - 1
  918.             end
  919.         end
  920.     elseif detectedButtonParsedTable[1] == "folder" then
  921.         if button == 1 then
  922.             currDir = currDir.."/"..action
  923.             files = refresh(currDir)
  924.             scroll = 0
  925.         elseif button == 2 then
  926.             local result = dialogBox(folderMenu,false)
  927.             if result == "open" then
  928.                 currDir = currDir.."/"..action
  929.                 files = refresh(currDir)
  930.                 scroll = 0
  931.             elseif result == "copy" then
  932.                 pathToCopy = currDir..action
  933.             elseif result == "delete" then
  934.                 IPclient.send(domain,{"ftp",7,{currDir.."/"..action}})
  935.                 files = refresh(currDir)
  936.             elseif result == "rename" then
  937.                 local answ = textBox(newFolderWindow,false,false)
  938.                 IPclient.send(domain,{"ftp",15,{currDir.."/"..action,currDir.."/"..answ}})
  939.                 files = refresh(currDir)
  940.             elseif result == "move" then
  941.                 local answ = textBox(moveFolderWindow,false,false)
  942.                 if string.sub(answ,1,1) ~= "/" then
  943.                     answ = "/"..answ
  944.                 end
  945.                 IPclient.send(domain,{"ftp",15,{currDir.."/"..action,answ}})
  946.                 files = refresh(currDir)
  947.             end
  948.         end
  949.     elseif detectedButtonParsedTable[1] == "file" then
  950.         if button == 1 then
  951.             IPclient.send(domain,{"ftp",13,{currDir.."/"..action}})
  952.             msgToParse = IPclient.receive()
  953.             if pcall(textutilsunserialize,msgToParse) then
  954.                 messageTable = textutilsunserialize(msgToParse)
  955.                 message = messageTable[3]
  956.                 file = fs.open("ram/"..action,"w")
  957.                 file.write(message[3][1])
  958.                 file.close()
  959.                 local fileExtension
  960.                 for token in string.gmatch(action,"[^%.]+") do
  961.                     fileExtension = token
  962.                 end
  963.                 if fileExtension == action then
  964.                     fileExtension = "txt"
  965.                 end
  966.                 programT = program(fileExtension)
  967.                 shell.run("subPrograms/"..programT.." ram/"..action)
  968.                 file = fs.open("ram/"..action,"r")
  969.                 fileData = file.readAll()
  970.                 file.close()
  971.                 IPclient.send(domain,{"ftp",11,{currDir.."/"..action,fileData}})
  972.                 fs.delete("ram/"..action)
  973.             end
  974.         elseif button == 2 then
  975.             local result = dialogBox(fileMenu,false)
  976.             print(result)
  977.             if result == "open" then
  978.                 IPclient.send(domain,{"ftp",13,{currDir.."/"..action}})
  979.                 msgToParse = IPclient.receive()
  980.                 if pcall(textutilsunserialize,msgToParse) then
  981.                     messageTable = textutilsunserialize(msgToParse)
  982.                     message = messageTable[3]
  983.                     file = fs.open("ram/"..action,"w")
  984.                     file.write(message[3][1])
  985.                     file.close()
  986.                     local fileExtension
  987.                     for token in string.gmatch(action,"[^%.]+") do
  988.                         fileExtension = token
  989.                     end
  990.                     if fileExtension == action then
  991.                         fileExtension = "txt"
  992.                     end
  993.                     programT = program(fileExtension)
  994.                     shell.run("subPrograms/"..programT.." ram/"..action)
  995.                     file = fs.open("ram/"..action,"r")
  996.                     fileData = file.readAll()
  997.                     file.close()
  998.                     IPclient.send(domain,{"ftp",11,{currDir.."/"..action,fileData}})
  999.                     fs.delete("ram/"..action)
  1000.                 end
  1001.             elseif result == "delete" then
  1002.                 IPclient.send(domain,{"ftp",7,{currDir.."/"..action}})
  1003.                 files = refresh(currDir)
  1004.             elseif result == "rename" then
  1005.                 local answ = textBox(newFolderWindow,false,false)
  1006.                 IPclient.send(domain,{"ftp",15,{currDir.."/"..action,currDir.."/"..answ}})
  1007.                 files = refresh(currDir)
  1008.             elseif result == "move" then
  1009.                 local answ = textBox(moveFolderWindow,false,false)
  1010.                 if string.sub(answ,1,1) ~= "/" then
  1011.                     answ = "/"..answ
  1012.                 end
  1013.                 IPclient.send(domain,{"ftp",15,{currDir.."/"..action,answ}})
  1014.                 files = refresh(currDir)
  1015.             elseif result == "download" then
  1016.                 IPclient.send(domain,{"ftp",13,{currDir.."/"..action}})
  1017.                 msgToParse = IPclient.receive()
  1018.                 if pcall(textutilsunserialize,msgToParse) then
  1019.                     messageTable = textutilsunserialize(msgToParse)
  1020.                     message = messageTable[3]
  1021.                     file = fs.open("Downloads/"..action,"w")
  1022.                     file.write(message[3][1])
  1023.                     file.close()
  1024.                 end
  1025.             end
  1026.         end
  1027.     elseif detectedButtonParsedTable[1] == "nil" then
  1028.         if button == 2 then
  1029.             if action == "nil" then
  1030.                 local result = dialogBox(nilMenu,false)
  1031.                 if result == "makeFolder" then
  1032.                     local answ = textBox(newFolderWindow,false,false)
  1033.                     IPclient.send(domain,{"ftp",9,{currDir.."/"..answ}})
  1034.                     files = refresh(currDir)
  1035.                     scroll = 0
  1036.                 elseif result == "makeFile" then
  1037.                     local answ = textBox(newFileWindow,false,false)
  1038.                     IPclient.send(domain,{"ftp",11,{currDir.."/"..answ,""}})
  1039.                     files = refresh(currDir)
  1040.                     scroll = 0
  1041.                 elseif result == "uploadFile" then
  1042.                     local path
  1043.                     local fileNotExists = true
  1044.                     while fileNotExists == true do
  1045.                         path = textBox(upFileWindow,false,false)
  1046.                         if fs.exists(path) then
  1047.                             fileNotExists = false
  1048.                         end
  1049.                     end
  1050.                     file = fs.open(path,"r")
  1051.                     fileData = file.readAll()
  1052.                     file.close()
  1053.                     local answ = textBox(newFileWindow,false,false)
  1054.                     IPclient.send(domain,{"ftp",11,{currDir.."/"..answ,fileData}})
  1055.                     files = refresh(currDir)
  1056.                     scroll = 0
  1057.                 end
  1058.             end
  1059.         end
  1060.     end
  1061. end
  1062.  
  1063. --Code--
  1064.  
  1065. clear()
  1066. getDomainName()
  1067. login()
  1068. while notEnded do
  1069.     main(files,Buttons)
  1070. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement