Advertisement
ValveCantCount

userpastebindev

May 7th, 2015
317
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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> <&5password&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:\n")
  113.         username = read()
  114.         cprint("&5Password:\n")
  115.         password = read("*")
  116.       else
  117.         cprint("&1Getting User Key...\n")
  118.         local skeyr = http.post(
  119.             "http://pastebin.com/api/api_login.php",
  120.             "api_dev_key="..textutils.urlEncode(key)..
  121.             "&api_user_name="..textutils.urlEncode(username)..
  122.             "&api_user_password="..textutils.urlEncode(password)
  123.         )
  124.         skey = skeyr.readAll()
  125.         cprint("&1Received User Key...\n")
  126.     cprint("&1Username: &5"..username.."&1Password: &5"..password.."&1\n")
  127.       end
  128.     end
  129.     local response = http.post(
  130.         "http://pastebin.com/api/api_post.php",
  131.         "api_option=paste&"..
  132.         "api_dev_key="..key.."&"..
  133.         "api_paste_format=lua&"..
  134.         "api_paste_name="..textutils.urlEncode(sName).."&"..
  135.         "api_paste_code="..textutils.urlEncode(sText).."&"..
  136.     "api_user_key="..textutils.urlEncode(skey)
  137.     )
  138.        
  139.     if response then
  140.         cprint( "&1Success.\n" )
  141.        
  142.         local sResponse = response.readAll()
  143.         response.close()
  144.                
  145.         local sCode = string.match( sResponse, "[^/]+$" )
  146.         cprint( "&1Uploaded as "..sResponse.."\n" )
  147.         cprint( "&1Run \"userpastebin get "..sCode.."\" to download anywhere\n" )
  148.  
  149.     else
  150.         cprint( "&1Failed.\n" )
  151.     end
  152.    
  153. elseif sCommand == "get" then
  154.     if #tArgs < 3 then
  155.         printUsage()
  156.         return
  157.     end
  158.  
  159.     local sCode = tArgs[2]
  160.     local sFile = tArgs[3]
  161.     local sPath = shell.resolve( sFile )
  162.     if fs.exists( sPath ) then
  163.         cprint( "&1File already exists. If you want to replace the existing file, type 'YES'. Otherwise, type 'NO'.\n" )
  164.         local replaceb = read()
  165.         if replaceb == "NO" then
  166.       return
  167.     elseif replaceb == "YES" then
  168.       cprint( "&1Okay, replacing file...\n" )
  169.     else
  170.       return
  171.     end
  172.     end
  173.    
  174.     local res = get(sCode)
  175.     if res then        
  176.         local file = fs.open( sPath, "w" )
  177.         file.write( res )
  178.         file.close()
  179.        
  180.         cprint( "&1Downloaded as "..sFile.."\n" )
  181.     end
  182. elseif sCommand == "run" then
  183.     local sCode = tArgs[2]
  184.  
  185.     local res = get(sCode)
  186.     if res then
  187.         local func, err = loadstring(res)
  188.         if not func then
  189.             printError( err )
  190.             return
  191.         end
  192.         setfenv(func, getfenv())
  193.         local success, msg = pcall(func, unpack(tArgs, 3))
  194.         if not success then
  195.             printError( msg )
  196.         end
  197.     end
  198.  
  199. elseif sCommand == "setkey" then
  200.     local sCode = tArgs[2]
  201.     if sCode == "default" then
  202.       sCode = "0ec2eb25b6166c0c27a394ae118ad829"
  203.     end
  204.     h = fs.open(".userpastebin.developerapikey","w")
  205.     h.write(sCode)
  206.     h.close()
  207.     h = fs.open(".userpastebin.developerapikey","r")
  208.     if h.readAll() == sCode then
  209.       cprint("&1Key Updated Successfully&0.&1 New Key&0:\n&d"..sCode.."\n")
  210.     else
  211.       cprint("&eFailed&0!")
  212.     end
  213.     h.close()
  214. elseif sCommand == "key" then
  215.     local sCode = tArgs[2]
  216.     if sCode == "help" then
  217.       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")
  218.     end
  219.  
  220. elseif sCommand == "update" then
  221.     local sCode = tArgs[2]
  222.     if sCode == "get" then
  223.       shell.run("userpastebin", "get", "jHSJ6cf1", ".upb-updater")
  224.       shell.run(".upb-updater")
  225.     elseif sCode == "rm" then
  226.       shell.run("rm", ".upb-updater")
  227.     end
  228.  
  229. elseif sCommand == "login" then
  230.     local sName = tArgs[2]
  231.     if sName == "logout" then
  232.       if username == "noUsername" then
  233.         cprint("&1You are not logged in!\n")
  234.         return
  235.       else
  236.         username = "noUsername"
  237.         password = "noPassword"
  238.         local h = fs.open(".userpastebin.username", "w")
  239.         h.write("noUsername")
  240.         h.close()
  241.         local h = fs.open(".userpastebin.password", "w")
  242.         h.write("noPassword")
  243.         h.close()
  244.         return
  245.       end
  246.     else
  247.       sPass = tArgs[3]
  248.       local h = fs.open(".userpastebin.username", "w")
  249.       h.write(sName)
  250.       h.close()
  251.       local h = fs.open(".userpastebin.password", "w")
  252.       h.write(sPass)
  253.       h.close()
  254.       return
  255.     end
  256. else
  257.     printUsage()
  258.     return
  259. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement