TheBinaryLoop

ComputerCraft | userpastebin

Aug 15th, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.34 KB | None | 0 0
  1. function cprint(...)
  2.         local s = "&1"
  3.         for k, v in ipairs(arg) do
  4.                 s = s .. v
  5.         end
  6.         s = s .. "&0"
  7.  
  8.         local fields = {}
  9.         local lastcolor, lastpos = "0", 0
  10.         for pos, clr in s:gmatch"()&(%x)" do
  11.                 table.insert(fields, {s:sub(lastpos + 2, pos - 1), lastcolor})
  12.                 lastcolor, lastpos = clr , pos
  13.         end
  14.  
  15.         for i = 2, #fields do
  16.                 term.setTextColor(2 ^ (tonumber(fields[i][2], 16)))
  17.                 io.write(fields[i][1])
  18.         end
  19. end
  20.  
  21.  
  22.  
  23.  
  24.  
  25. local key = "0ec2eb25b6166c0c27a394ae118ad829"
  26. local username = "noUsername"
  27. local password = "noPassword"
  28.  
  29. if fs.exists(".userpastebin.developerapikey") then
  30.   local h = fs.open(".userpastebin.developerapikey","r")
  31.   key = h.readAll()
  32.   h.close()
  33. else
  34.   local h = fs.open(".userpastebin.developerapikey","w")
  35.   h.write(key)
  36.   h.close()
  37. end
  38.  
  39. if fs.exists(".userpastebin.username") then
  40.   local h = fs.open(".userpastebin.username", "r")
  41.   username = h.readAll()
  42.   h.close()
  43. end
  44.  
  45. if fs.exists(".userpastebin.password") then
  46.   local h = fs.open(".userpastebin.password", "r")
  47.   password = h.readAll()
  48.   h.close()
  49. end
  50.  
  51. local function printUsage()
  52.     cprint( "&1Usages&0:\n" )
  53.     cprint( "&1userpastebin &3put &1<&5filename&1>\n" )
  54.     cprint( "&1userpastebin &3get &1<&5code&1> <&5filename&1>\n" )
  55.     cprint( "&1userpastebin &3run &1<&5code&1> <&5arguments&1>\n" )
  56.     cprint( "&1userpastebin &3update get\n" )
  57.     cprint( "&1userpastebin &3login &1<&5username&1>\n" )
  58.     cprint( "&1userpastebin &3login logout &1\n" )
  59.     cprint( "&1userpastebin &3setkey &1<&5key&1>\n" )
  60.     cprint( "&1userpastebin &3setkey default\n\n" )
  61.     cprint( "&1The Current Key Is&0: &d"..key.."\n\n" )
  62.     cprint( "&1userpastebin &3key help\n" )
  63. end
  64.  
  65. local tArgs = { ... }
  66. if #tArgs < 2 then
  67.     printUsage()
  68.     return
  69. end
  70.  
  71. if not http then
  72.     printError( "Pastebin requires http API" )
  73.     printError( "Set http_enable to true in ComputerCraft.cfg" )
  74.     return
  75. end
  76.  
  77. local function get(paste)
  78.     cprint( "&1Connecting to pastebin.com... " )
  79.     local response = http.get(
  80.         "http://pastebin.com/raw.php?i="..textutils.urlEncode( paste )
  81.     )
  82.        
  83.     if response then
  84.         cprint( "&1Success.\n" )
  85.        
  86.         local sResponse = response.readAll()
  87.         response.close()
  88.         return sResponse
  89.     else
  90.         printError( "Failed." )
  91.     end
  92. end
  93.  
  94. local sCommand = tArgs[1]
  95. if sCommand == "put" then
  96.     local sFile = tArgs[2]
  97.     local sPath = shell.resolve( sFile )
  98.     if not fs.exists( sPath ) or fs.isDir( sPath ) then
  99.         cprint( "&1No such file\n" )
  100.         return
  101.     end
  102.    
  103.     local sName = fs.getName( sPath )
  104.     local file = fs.open( sPath, "r" )
  105.     local sText = file.readAll()
  106.     file.close()
  107.    
  108.     cprint( "&1Connecting to pastebin.com... \n" )
  109.     local skey = "0ec2eb25b6166c0c27a394ae118ad829"
  110.     if key ~= skey then
  111.       if username == "noUsername" then
  112.         cprint("&5Please Enter Your Username: ")
  113.         username = read()
  114.         cprint("\n&5Password: ")
  115.         password = read("*")
  116.         cprint("&1\n")
  117.       else
  118.         cprint("&1Getting User Key...\n")
  119.         local skeyr = http.post(
  120.             "http://pastebin.com/api/api_login.php",
  121.             "api_dev_key="..textutils.urlEncode(key)..
  122.             "&api_user_name="..textutils.urlEncode(username)..
  123.             "&api_user_password="..textutils.urlEncode(password)
  124.         )
  125.         skey = skeyr.readAll()
  126.         cprint("&1Received User Key...\n")
  127.     cprint("&1Username: &5"..username.."&1\n")
  128.       end
  129.     end
  130.     local response = http.post(
  131.         "http://pastebin.com/api/api_post.php",
  132.         "api_option=paste&"..
  133.         "api_dev_key="..key.."&"..
  134.         "api_paste_format=lua&"..
  135.         "api_paste_name="..textutils.urlEncode(sName).."&"..
  136.         "api_paste_code="..textutils.urlEncode(sText).."&"..
  137.     "api_user_key="..textutils.urlEncode(skey)
  138.     )
  139.        
  140.     if response then
  141.         cprint( "&1Success.\n" )
  142.        
  143.         local sResponse = response.readAll()
  144.         response.close()
  145.                
  146.         local sCode = string.match( sResponse, "[^/]+$" )
  147.         cprint( "&1Uploaded as "..sResponse.."\n" )
  148.         cprint( "&1Run \"userpastebin get "..sCode.."\" to download anywhere\n" )
  149.  
  150.     else
  151.         cprint( "&1Failed.\n" )
  152.     end
  153.    
  154. elseif sCommand == "get" then
  155.     if #tArgs < 3 then
  156.         printUsage()
  157.         return
  158.     end
  159.  
  160.     local sCode = tArgs[2]
  161.     local sFile = tArgs[3]
  162.     local sPath = shell.resolve( sFile )
  163.     if fs.exists( sPath ) then
  164.         cprint( "&1File already exists. If you want to replace the existing file, type 'YES'. Otherwise, type 'NO'.\n" )
  165.         local replaceb = read()
  166.         if replaceb == "NO" then
  167.       return
  168.     elseif replaceb == "YES" then
  169.       cprint( "&1Okay, replacing file...\n" )
  170.     else
  171.       return
  172.     end
  173.     end
  174.    
  175.     local res = get(sCode)
  176.     if res then        
  177.         local file = fs.open( sPath, "w" )
  178.         file.write( res )
  179.         file.close()
  180.        
  181.         cprint( "&1Downloaded as "..sFile.."\n" )
  182.     end
  183. elseif sCommand == "run" then
  184.     local sCode = tArgs[2]
  185.  
  186.     local res = get(sCode)
  187.     if res then
  188.         local func, err = loadstring(res)
  189.         if not func then
  190.             printError( err )
  191.             return
  192.         end
  193.         setfenv(func, getfenv())
  194.         local success, msg = pcall(func, unpack(tArgs, 3))
  195.         if not success then
  196.             printError( msg )
  197.         end
  198.     end
  199.  
  200. elseif sCommand == "setkey" then
  201.     local sCode = tArgs[2]
  202.     if sCode == "default" then
  203.       sCode = "0ec2eb25b6166c0c27a394ae118ad829"
  204.     end
  205.     h = fs.open(".userpastebin.developerapikey","w")
  206.     h.write(sCode)
  207.     h.close()
  208.     h = fs.open(".userpastebin.developerapikey","r")
  209.     if h.readAll() == sCode then
  210.       cprint("&1Key Updated Successfully&0.&1 New Key&0:\n&d"..sCode.."\n")
  211.     else
  212.       cprint("&eFailed&0!")
  213.     end
  214.     h.close()
  215. elseif sCommand == "key" then
  216.     local sCode = tArgs[2]
  217.     if sCode == "help" then
  218.       cprint("\n&1Using an API key will let you upload right to your account&0.&1 Go to &3pastebin.com/api &1after logging in to find your key&0.\n")
  219.     end
  220.  
  221. elseif sCommand == "update" then
  222.     local sCode = tArgs[2]
  223.     if sCode == "get" then
  224.       shell.run("userpastebin", "get", "jHSJ6cf1", ".upb-updater")
  225.       shell.run(".upb-updater")
  226.     elseif sCode == "rm" then
  227.       shell.run("rm", ".upb-updater")
  228.     end
  229.  
  230. elseif sCommand == "login" then
  231.     local sName = tArgs[2]
  232.     if sName == "logout" then
  233.       if username == "noUsername" then
  234.         cprint("&1You are not logged in!\n")
  235.         return
  236.       else
  237.         username = "noUsername"
  238.         password = "noPassword"
  239.         local h = fs.open(".userpastebin.username", "w")
  240.         h.write("noUsername")
  241.         h.close()
  242.         local h = fs.open(".userpastebin.password", "w")
  243.         h.write("noPassword")
  244.         h.close()
  245.         return
  246.       end
  247.     else
  248.       cprint("&5Password: ")
  249.       sPass = read("*")
  250.       cprint("&1\n")
  251.       local h = fs.open(".userpastebin.username", "w")
  252.       h.write(sName)
  253.       h.close()
  254.       local h = fs.open(".userpastebin.password", "w")
  255.       h.write(sPass)
  256.       h.close()
  257.       return
  258.     end
  259. else
  260.     printUsage()
  261.     return
  262. end
Add Comment
Please, Sign In to add comment