Advertisement
FNCxPro

fixed pastebin

May 27th, 2015
274
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.78 KB | None | 0 0
  1. local function printUsage()
  2.     if term.isColor then
  3.         term.setTextColor(colors.orange)
  4.         print("Usages: ")
  5.         term.setTextColor(colors.red)
  6.         print("pastebin put <filename>")
  7.         print("pastebin get <code> <filename>")
  8.         print("pastebin run <code> <arguments>")
  9.         term.setTextColor(colors.white)
  10.     else
  11.         print("Usages: ")
  12.         print("pastebin put <filename>")
  13.         print("pastebin get <code> <filename>")
  14.         print("pastebin run <code> <arguments>")
  15.     end
  16. end
  17.  
  18. local tArgs = {...}
  19. if #tArgs < 2 then
  20.     printUsage()
  21.     return
  22. end
  23.  
  24. if not http then
  25.     printError("Pastebin requires http API")
  26.     printError("Set http_enable to true in ComputerCraft.cfg")
  27.     return
  28. end
  29.  
  30. local function get(paste)
  31.     write("Connecting to pastebin.com")
  32.     local response = http.get("http://pastebin.com/raw.php?i="..textutils.urlEncode(paste))
  33.    
  34.     if response then
  35.         if term.isColor then
  36.             term.setTextColor(colors.green)
  37.             print("Success.")
  38.             term.setTextColor(colors.white)
  39.         else
  40.             print("Success.")
  41.         end
  42.         local sResponse = response.readAll()
  43.         response.close()
  44.         return sResponse
  45.     else
  46.         printError("Failed.")
  47.     end
  48. end
  49.  
  50. local sCommand = tArgs[1]
  51. if sCommand == "put" then
  52.     -- Upload a file to pastebin.com (through relay)
  53.     -- Determine file to upload
  54.     local sFile = tArgs[2]
  55.     local sPath = shell.resolve(sFile)
  56.     if not fs.exists(sPath) then
  57.         if term.isColor() then
  58.             term.setTextColor(colors.red)
  59.             print("No such file.")
  60.             term.setTextColor(colors.white)
  61.         else
  62.             print("No such file.")
  63.         end
  64.         return
  65.     elseif fs.isDir(sPath) then
  66.         if term.isColor() then
  67.             term.setTextColor(colors.red)
  68.             print("Cannot upload a directory.")
  69.             term.setTextColor(colors.white)
  70.         else
  71.             print("Cannot upload a directory.")
  72.         end
  73.         return
  74.     end
  75.        
  76.     --Read in the file
  77.     local sName = fs.getName(sPath)
  78.     local file = fs.open(sPath,"r")
  79.     local sText = file.readAll()
  80.     file.close()
  81.        
  82.     -- POST the contents to pastebin (through relay)
  83.     local response = http.post(
  84.         "http://104.131.75.188/node/paste.php",
  85.         "name="..sName.."&"..
  86.         "code="..sText
  87.     )
  88.                
  89.        
  90.     if response then
  91.         if term.isColor then
  92.             term.setTextColor(colors.green)
  93.             print("Success.")
  94.             term.setTextColor(colors.white)
  95.             local sResponse = response.readAll()
  96.             response.close()
  97.             local sCode = string.match(sResponse,"[^/]+$")
  98.             print("Uploaded as "..sResponse)
  99.             print("Run \"paste get "..sCode.."\" to download anywhere")
  100.         else
  101.             print("Success.")
  102.             local sResponse = response.readAll()
  103.             response.close()
  104.             local sCode = string.match(sResponse,"[^/]+$")
  105.             print("Uploaded as "..sResponse)
  106.             print("Run \"paste get "..sCode.."\" to download anywhere")
  107.         end
  108.     else
  109.         if term.isColor then
  110.             term.setTextColor(colors.red)
  111.             print("Failed.")
  112.             term.setTextColor(colors.white)
  113.         else
  114.             print("Failed.")
  115.         end
  116.         return
  117.     end
  118. elseif sCommand == "get" then
  119.     -- Download a file from pastebin.com
  120.     if #tArgs < 3 then
  121.         printUsage()
  122.         return
  123.     end
  124.    
  125.     -- Determine file to download
  126.     local sCode = tArgs[2]
  127.     local sFile = tArgs[3]
  128.     local sPath = shell.resolve(sFile)
  129.     if fs.exists(sPath) then
  130.         if term.isColor then
  131.             term.setTextColor(colors.red)
  132.             print("File already exists.")
  133.             term.setTextColor(colors.white)
  134.             return
  135.         else
  136.             print("File already exists.")
  137.             return
  138.         end
  139.     end
  140.     local res = get(sCode)
  141.     if res then
  142.         local file = fs.open(sPath,"w")
  143.         file.write(res)
  144.         file.close()
  145.         print("Downloaded as "..sFile)
  146.     end
  147. elseif sCommand == "run" then
  148.     local sCode = tArgs[2]
  149.     local res = get(sCode)
  150.     if res then
  151.         local func, err = load(res,sCode,"t", _ENV)
  152.         if not func then
  153.             printError(err)
  154.             return
  155.         end
  156.         local success,msg = pcall(func, table.unpack(tArgs,3))
  157.         if not success then
  158.             printError(msg)
  159.         end
  160.     end
  161. else
  162.     printUsage()
  163.     return
  164. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement