Advertisement
Guest User

dacstorage.lua

a guest
Sep 3rd, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 9.70 KB | None | 0 0
  1. --Variables
  2. local env = getfenv()
  3. local width,height = term.getSize()
  4. local username
  5. local password
  6. local token
  7. local SKIPLOGIN = true
  8. local selected = false
  9. local scroll = 0
  10. local refresh = false
  11. local baseOptions = {"Upload","New"}
  12. local fileOptions = {"Upload","New"," -----","Download","Delete","Rename","Edit","Run"}
  13. --Functions
  14. local drawBanner = function()
  15.     paintutils.drawLine(1,1,width,1,colors.green)
  16.     term.setCursorPos(math.ceil((width-10)/2),1)
  17.     term.setBackgroundColor(colors.green)
  18.     term.setTextColor(colors.white)
  19.     term.write("DacStorage")
  20. end
  21. local info = function(txt,t)
  22.     paintutils.drawLine(1,height,width,height,colors.lightGray)
  23.     term.setTextColor(colors.gray)
  24.     term.setBackgroundColor(colors.lightGray)
  25.     term.setCursorPos(2,height)
  26.     term.write(txt)
  27.     if not t then
  28.         while true do
  29.             local e = os.pullEvent()
  30.             if e == "mouse_click" or e == "char" then
  31.                 break
  32.             end
  33.         end
  34.     end
  35. end
  36. local prompt = function(txt)
  37.     info(txt..": ",true)
  38.     return read()
  39. end
  40. local runprogram = function(selection)
  41.     local p = http.post("https://dacpages.tk/dpserver.php","f=filedownload&filename="..selection.."&username="..username.."&token="..token)
  42.     local msg = p.readAll()
  43.     if msg == "File Does Not Exist!" then
  44.         info(msg)
  45.         return
  46.     end
  47.     loadstring(msg)()
  48. end
  49. local rclick = function(x,y,tbl,selection)
  50.     if y == 1 then
  51.         return
  52.     end
  53.     local bw,bh,value
  54.     bh = #tbl
  55.     bw = 0
  56.     for i = 1,#tbl do
  57.         if #tbl[i]+2 > bw then
  58.             bw = #tbl[i]+2
  59.         end
  60.     end
  61.     if x+bw > width then
  62.         x = width-bw+1
  63.     end
  64.     if y+bh > height then
  65.         y = height-bh+1
  66.     end
  67.     paintutils.drawFilledBox(x+1,y+1,x+bw,y+bh,colors.black)
  68.     paintutils.drawFilledBox(x,y,x+bw-1,y+bh-1,colors.green)
  69.     term.setTextColor(colors.white)
  70.     for i = 1,#tbl do
  71.         term.setCursorPos(x+1,y+i-1)
  72.         term.write(tbl[i])
  73.     end
  74.     local e,a,b,c = os.pullEvent("mouse_click")
  75.     if b > x+bw-1 or b < x or c > y+bh-1 or c < y then
  76.         return
  77.     else
  78.         value = tbl[c-y+1]
  79.     end
  80.     if value == "Download" then
  81.         local p = http.post("https://dacpages.tk/dpserver.php","f=filedownload&filename="..selection.."&username="..username.."&token="..token)
  82.         local msg = p.readAll()
  83.         if msg == "File Does Not Exist!" then
  84.             info(msg)
  85.         else
  86.             local f = fs.open(selection,"w")
  87.             f.write(p.readAll())
  88.             f.close()
  89.         end
  90.         p.close()
  91.     elseif value == "Upload" then
  92.         local file = prompt("Enter file location")
  93.         if not fs.exists(file) then
  94.             info("File Does Not Exist!")
  95.             return
  96.         end
  97.         local f = fs.open(file,"r")
  98.         local data = textutils.urlEncode(f.readAll())
  99.         f.close()
  100.         local p = http.post("https://dacpages.tk/dpserver.php","f=rawupload&filename="..fs.getName(file).."&username="..username.."&token="..token.."&filedata="..data)
  101.         local msg = p.readAll()
  102.         if msg == "success" then
  103.             refresh = true
  104.         else
  105.             info(msg)
  106.         end
  107.         p.close()
  108.     elseif value == "Delete" then
  109.         if string.lower(string.sub(prompt("Are you sure? (y/n)"),1,1)) ~= "y" then
  110.             return
  111.         end
  112.         local p = http.post("https://dacpages.tk/dpserver.php","f=filedelete&filename="..selection.."&username="..username.."&token="..token)
  113.         local msg = p.readAll()
  114.         if msg == "success" then
  115.             refresh = true
  116.         else
  117.             info(msg)
  118.         end
  119.         p.close()
  120.     elseif value == "Rename" then
  121.         local newfilename = prompt("Enter New Filename")
  122.         local p = http.post("https://dacpages.tk/dpserver.php","f=filerename&filename="..selection.."&username="..username.."&token="..token.."&newfilename="..newfilename)
  123.         local msg = p.readAll()
  124.         if msg == "success" then
  125.             refresh = true
  126.         else
  127.             info(msg)
  128.         end
  129.         p.close()
  130.     elseif value == "Edit" then
  131.         local p = http.post("https://dacpages.tk/dpserver.php","f=filedownload&filename="..selection.."&username="..username.."&token="..token)
  132.         local msg = p.readAll()
  133.         if msg == "File Does Not Exist!" then
  134.             info(msg)
  135.             p.close()
  136.             return
  137.         end
  138.         local f = fs.open(".temp","w")
  139.         f.write(msg)
  140.         f.close()
  141.         p.close()
  142.         shell.run("edit",".temp")
  143.         f = fs.open(".temp","r")
  144.         local data = textutils.urlEncode(f.readAll())
  145.         f.close()
  146.         local p = http.post("https://dacpages.tk/dpserver.php","f=filedelete&filename="..selection.."&username="..username.."&token="..token)
  147.         local msg = p.readAll()
  148.         if msg ~= "success" then
  149.             info(msg)
  150.             p.close()
  151.             return
  152.         end
  153.         p.close()
  154.         local p = http.post("https://dacpages.tk/dpserver.php","f=rawupload&filename="..selection.."&username="..username.."&token="..token.."&filedata="..data)
  155.         local msg = p.readAll()
  156.         if msg == "success" then
  157.             refresh = true
  158.         else
  159.             info(msg)
  160.             refresh = true
  161.         end
  162.         p.close()
  163.     elseif value == "New" then
  164.         local filename = prompt("Enter filename")
  165.         local p = http.post("https://dacpages.tk/dpserver.php","f=rawupload&filename="..filename.."&username="..username.."&token="..token.."&filedata=")
  166.         local msg = p.readAll()
  167.         if msg == "success" then
  168.             refresh = true
  169.         else
  170.             info(msg)
  171.         end
  172.         p.close()
  173.     elseif value == "Run" then
  174.         runprogram(selection)
  175.     end
  176. end
  177. --Code
  178. while not SKIPLOGIN do
  179.     term.setBackgroundColor(colors.white)
  180.     term.clear()
  181.     drawBanner()
  182.     term.setTextColor(colors.black)
  183.     term.setBackgroundColor(colors.white)
  184.     term.setCursorPos(3,4)
  185.     term.write("Enter Username for DacPages Account:")
  186.     term.setCursorPos(5,6)
  187.     term.setTextColor(colors.gray)
  188.     username = textutils.urlEncode(read())
  189.     term.setCursorPos(3,8)
  190.     term.setTextColor(colors.black)
  191.     term.write("Enter DacPages Account Password:")
  192.     term.setCursorPos(5,10)
  193.     term.setTextColor(colors.gray)
  194.     password = textutils.urlEncode(read("*"))
  195.     local page = http.post("https://dacpages.tk/dpserver.php","f=signin&password="..password.."&username="..username)
  196.     local out = page.readAll()
  197.     if string.sub(out,1,7) == "token: " then
  198.         token = string.sub(out,8,#out)
  199.         break
  200.     else
  201.         term.setCursorPos(3,height)
  202.         term.setTextColor(colors.red)
  203.         term.write(out)
  204.         sleep(3)
  205.     end
  206. end
  207. if SKIPLOGIN then
  208.     username = "TheDacinator"
  209.     token = "b3ca861b12142c2c"
  210. end
  211. while true do
  212.     term.setBackgroundColor(colors.white)
  213.     term.clear()
  214.     drawBanner()
  215.     local page = http.post("https://dacpages.tk/dpserver.php","f=listfiles&username="..username.."&token="..token)
  216.     local list = {}
  217.     for i in string.gmatch(page.readAll(),"[^,]+") do
  218.         if not (i == "." or i == "..") then
  219.             list[#list+1] = i
  220.         end
  221.     end
  222.     while true do
  223.         for i = 2,height do
  224.             term.setCursorPos(2,i)
  225.             if list[i-1+scroll] then
  226.                 if i-1+scroll == selected then
  227.                     term.setBackgroundColor(colors.lightBlue)
  228.                     paintutils.drawLine(1,i,width,i,colors.lightBlue)
  229.                     term.setTextColor(colors.white)
  230.                 else
  231.                     paintutils.drawLine(1,i,width,i,colors.white)
  232.                     term.setTextColor(colors.black)
  233.                     term.setBackgroundColor(colors.white)
  234.                 end
  235.                 term.setCursorPos(2,i)
  236.                 term.write(list[i-1+scroll])
  237.             else
  238.                 paintutils.drawLine(1,i,width,i,colors.white)
  239.             end
  240.         end
  241.         while true do
  242.             local e,a,b,c = os.pullEvent()
  243.             if e == "mouse_click" then
  244.                 if list[c-1] and a == 1 then
  245.                     selected = c-1+scroll
  246.                     break
  247.                 elseif a == 2 then
  248.                     if list[c-1] then
  249.                         rclick(b,c,fileOptions,list[c-1+scroll])
  250.                     else
  251.                         rclick(b,c,baseOptions)
  252.                     end
  253.                     break
  254.                 end
  255.             elseif e == "mouse_scroll" then
  256.                 scroll = scroll + a
  257.                 if scroll > #list-(height-1) then
  258.                     scroll = #list-(height-1)
  259.                 end
  260.                 if scroll < 0 then
  261.                     scroll = 0
  262.                 end
  263.                 break
  264.             elseif e == "key" then
  265.                 if a == 200 and selected and selected > 1 then
  266.                     selected = selected - 1
  267.                     if selected <= scroll then
  268.                         scroll = selected - 1
  269.                     end
  270.                     break
  271.                 elseif a == 208 and ((not selected) or selected < #list) then
  272.                     if not selected then
  273.                         selected = 0
  274.                     end
  275.                     selected = selected + 1
  276.                     if selected > height-1+scroll then
  277.                         scroll = selected-height+1
  278.                     end
  279.                     break
  280.                 elseif a == 28 and selected and list[selected] then
  281.                     runprogram(list[selected])
  282.                 end
  283.             end
  284.         end
  285.         if refresh then
  286.             refresh = false
  287.             break
  288.         end
  289.     end
  290. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement