Marlingaming

Custom Pastebin Callout

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