Advertisement
goldfiction

pastebin_fix_mc

Mar 11th, 2022
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.15 KB | None | 0 0
  1.  
  2. local function printUsage()
  3. print( "Usages:" )
  4. print( "pastebin put <filename>" )
  5. print( "pastebin get <code> <filename>" )
  6. print( "pastebin run <code> <arguments>" )
  7. end
  8.  
  9. local tArgs = { ... }
  10. if #tArgs < 2 then
  11. printUsage()
  12. return
  13. end
  14.  
  15. if not http then
  16. printError( "Pastebin requires http API" )
  17. printError( "Set http_enable to true in ComputerCraft.cfg" )
  18. return
  19. end
  20.  
  21. local function get(paste)
  22. write( "Connecting to pastebin.com... " )
  23. local response = http.get(
  24. "https://pastebin.com/raw/"..textutils.urlEncode( paste )
  25. )
  26.  
  27. if response then
  28. print( "Success." )
  29.  
  30. local sResponse = response.readAll()
  31. response.close()
  32. return sResponse
  33. else
  34. print( "Failed." )
  35. end
  36. end
  37.  
  38. local sCommand = tArgs[1]
  39. if sCommand == "put" then
  40. -- Upload a file to pastebin.com
  41. -- Determine file to upload
  42. local sFile = tArgs[2]
  43. local sPath = shell.resolve( sFile )
  44. if not fs.exists( sPath ) or fs.isDir( sPath ) then
  45. print( "No such file" )
  46. return
  47. end
  48.  
  49. -- Read in the file
  50. local sName = fs.getName( sPath )
  51. local file = fs.open( sPath, "r" )
  52. local sText = file.readAll()
  53. file.close()
  54.  
  55. -- POST the contents to pastebin
  56. write( "Connecting to pastebin.com... " )
  57. local key = "0ec2eb25b6166c0c27a394ae118ad829"
  58. local response = http.post(
  59. "http://pastebin.com/api/api_post.php",
  60. "api_option=paste&"..
  61. "api_dev_key="..key.."&"..
  62. "api_paste_format=lua&"..
  63. "api_paste_name="..textutils.urlEncode(sName).."&"..
  64. "api_paste_code="..textutils.urlEncode(sText)
  65. )
  66.  
  67. if response then
  68. print( "Success." )
  69.  
  70. local sResponse = response.readAll()
  71. response.close()
  72.  
  73. local sCode = string.match( sResponse, "[^/]+$" )
  74. print( "Uploaded as "..sResponse )
  75. print( "Run \"pastebin get "..sCode.."\" to download anywhere" )
  76.  
  77. else
  78. print( "Failed." )
  79. end
  80.  
  81. elseif sCommand == "get" then
  82. -- Download a file from pastebin.com
  83. if #tArgs < 3 then
  84. printUsage()
  85. return
  86. end
  87.  
  88. -- Determine file to download
  89. local sCode = tArgs[2]
  90. local sFile = tArgs[3]
  91. local sPath = shell.resolve( sFile )
  92. if fs.exists( sPath ) then
  93. print( "File already exists" )
  94. return
  95. end
  96.  
  97. -- GET the contents from pastebin
  98. local res = get(sCode)
  99. if res then
  100. local file = fs.open( sPath, "w" )
  101. file.write( res )
  102. file.close()
  103.  
  104. print( "Downloaded as "..sFile )
  105. end
  106. elseif sCommand == "run" then
  107. local sCode = tArgs[2]
  108.  
  109. local res = get(sCode)
  110. if res then
  111. local func, err = load(res, sCode, "t", _ENV)
  112. if not func then
  113. printError( err )
  114. return
  115. end
  116. local success, msg = pcall(func, table.unpack(tArgs, 3))
  117. if not success then
  118. printError( msg )
  119. end
  120. end
  121. else
  122. printUsage()
  123. return
  124. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement