Advertisement
bruegge

MCS:Test:Code

Mar 8th, 2018
1,417
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 13.80 KB | None | 0 0
  1. function Pastebin(user, password)
  2.     local Pastebin = {}
  3.     Pastebin.api_user_key = 0
  4.     Pastebin.API_version = 0.8
  5.      
  6.     -- Internal function that parses the pastebin XML response.
  7.     function Pastebin:parse(response)
  8.         local objs = {}
  9.         for prefix, objstr in string.gmatch(response, "<(.-)>(.-)</%1>") do
  10.             local obj = {}
  11.             for key, value in string.gmatch(objstr, "<"..prefix.."_(.-)>(.-)</"..prefix.."_%1>") do
  12.                 obj[key] = value
  13.             end
  14.             objs[#objs+1] = obj
  15.         end
  16.         return objs
  17.     end
  18.      
  19.     -- Used to be a custom function, but I realized CC-Lua had one already.
  20.     Pastebin.url_encode = textutils.urlEncode
  21.      
  22.     -- My Pastebin API key.
  23.     Pastebin.api_dev_key = "c09548865c8dd0c2bf57d0d060b5e96a"
  24.      
  25.     -- Allowable inputs for paste creation options.
  26.     Pastebin.paste_formats = {["4cs"]=true,["6502acme"]=true,["6502kickass"]=true,["6502tasm"]=true,abap=true,actionscript=true,actionscript3=true,ada=true,algol68=true,apache=true,applescript=true,apt_sources=true,arm=true,asm=true,asp=true,asymptote=true,autoconf=true,autohotkey=true,autoit=true,avisynth=true,awk=true,bascomavr=true,bash=true,basic4gl=true,bibtex=true,blitzbasic=true,bnf=true,boo=true,bf=true,c=true,c_mac=true,cil=true,csharp=true,cpp=true,["cpp-qt"]=true,c_loadrunner=true,caddcl=true,cadlisp=true,cfdg=true,chaiscript=true,clojure=true,klonec=true,klonecpp=true,cmake=true,cobol=true,coffeescript=true,cfm=true,css=true,cuesheet=true,d=true,dcl=true,dcpu16=true,dcs=true,delphi=true,oxygene=true,diff=true,div=true,dos=true,dot=true,e=true,ecmascript=true,eiffel=true,email=true,epc=true,erlang=true,fsharp=true,falcon=true,fo=true,f1=true,fortran=true,freebasic=true,freeswitch=true,gambas=true,gml=true,gdb=true,genero=true,genie=true,gettext=true,go=true,groovy=true,gwbasic=true,haskell=true,haxe=true,hicest=true,hq9plus=true,html4strict=true,html5=true,icon=true,idl=true,ini=true,inno=true,intercal=true,io=true,j=true,java=true,java5=true,javascript=true,jquery=true,kixtart=true,latex=true,ldif=true,lb=true,lsl2=true,lisp=true,llvm=true,locobasic=true,logtalk=true,lolcode=true,lotusformulas=true,lotusscript=true,lscript=true,lua=true,m68k=true,magiksf=true,make=true,mapbasic=true,matlab=true,mirc=true,mmix=true,modula2=true,modula3=true,["68000devpac"]=true,mpasm=true,mxml=true,mysql=true,nagios=true,newlisp=true,text=true,nsis=true,oberon2=true,objeck=true,objc=true,["ocaml-brief"]=true,ocaml=true,octave=true,pf=true,glsl=true,oobas=true,oracle11=true,oracle8=true,oz=true,parasail=true,parigp=true,pascal=true,pawn=true,pcre=true,per=true,perl=true,perl6=true,php=true,["php-brief"]=true,pic16=true,pike=true,pixelbender=true,plsql=true,postgresql=true,povray=true,powershell=true,powerbuilder=true,proftpd=true,progress=true,prolog=true,properties=true,providex=true,purebasic=true,pycon=true,python=true,pys60=true,q=true,qbasic=true,rsplus=true,rails=true,rebol=true,reg=true,rexx=true,robots=true,rpmspec=true,ruby=true,gnuplot=true,sas=true,scala=true,scheme=true,scilab=true,sdlbasic=true,smalltalk=true,smarty=true,spark=true,sparql=true,sql=true,stonescript=true,systemverilog=true,tsql=true,tcl=true,teraterm=true,thinbasic=true,typoscript=true,unicon=true,uscript=true,ups=true,urbi=true,vala=true,vbnet=true,vedit=true,verilog=true,vhdl=true,vim=true,visualprolog=true,vb=true,visualfoxpro=true,whitespace=true,whois=true,winbatch=true,xbasic=true,xml=true,xorg_conf=true,xpp=true,yaml=true,z80=true,zxbasic=true}
  27.     Pastebin.paste_private_options = {[0]=true,[1]=true,[2]="Private"}
  28.     Pastebin.paste_expire_date_options = {["N"]=true,["10M"]=true,["1H"]=true,["1D"]=true,["1W"]=true,["2W"]=true,["1M"]=true}
  29.      
  30.     -- Creates a new paste. First argument required. All others optional. Pass explicit nils to pass over an argument.
  31.     -- Example: local paste = pastebin.create("This is my awesome paste text!", "Super cool name", nil, nil, "N")
  32.     --
  33.     -- Returns paste URL or error code.
  34.     function Pastebin:create(api_paste_code, api_paste_name, api_paste_format, api_paste_private, api_paste_expire_date)
  35.       if not api_paste_expire_date then
  36.         api_paste_expire_date = "N"
  37.       end
  38.       assert(type(api_paste_code) == "string", "Only strings can be uploaded as pastes! Use `tostring()` if necessary!")
  39.       local urlstr = {"api_dev_key=", self.url_encode(self.api_dev_key), "&api_option=paste", "&api_paste_code=", self.url_encode(api_paste_code)}
  40.       if self.api_user_key then if type(self.api_user_key) == "string" then local len = #urlstr urlstr[len+1] = "&api_user_key=" urlstr[len+2] = self.url_encode(self.api_user_key) else io.stderr:write("Invalid user key!\n") end end
  41.       if api_paste_name then if type(api_paste_name) == "string" then local len = #urlstr urlstr[len+1] = "&api_paste_name=" urlstr[len+2] = self.url_encode(api_paste_name) else io.stderr:write("Only strings can be used to name pastes! Use `tostring()` if necessary!") end end
  42.       if api_paste_format then if self.paste_formats[api_paste_format] then local len = #urlstr urlstr[len+1] = "&api_paste_format=" urlstr[len+2] = self.url_encode(api_paste_format) else io.stderr:write("Invalid paste format!") end end
  43.       if api_paste_private then local paste_private_long = self.paste_private_options[api_paste_private] if paste_private_long then if paste_private_long == "Private" and not self.api_user_key then io.stderr:write("User key necessary for private pastes!") else local len = #urlstr urlstr[len+1] = "&api_paste_private=" urlstr[len+2] = self.url_encode(api_paste_private) end else io.stderr:write("Invalid privacy level") end end
  44.       if api_paste_expire_date then if self.paste_expire_date_options[api_paste_expire_date] then local len = #urlstr urlstr[len+1] = "&api_paste_expire_date=" urlstr[len+2] = api_paste_expire_date else io.stderr:write("Invalid paste expiration date!") end end
  45.      
  46.       return http.post("http://pastebin.com/api/api_post.php", table.concat(urlstr)).readAll()
  47.     end
  48.      
  49.     -- Authenticates with Pastebin.
  50.     -- Example: local agente382 = pastebin.login("AgentE382", "thisisnotmypassword")
  51.     --
  52.     -- Returns user key for use with other API functions.
  53.     function Pastebin:loginToPastebin(api_user_name, api_user_password)
  54.         assert(type(api_user_name) == "string" and type(api_user_password) == "string", "Both arguments are required and must be strings!")
  55.         return http.post("http://pastebin.com/api/api_login.php", "api_dev_key="..self.url_encode(self.api_dev_key).."&api_user_name="..self.url_encode(api_user_name).."&api_user_password="..self.url_encode(api_user_password)).readAll()
  56.     end
  57.      
  58.     -- Deletes a paste. Uses user key from login(). api_paste_key must be a string containing the end of the paste's URL.
  59.     -- Example: local response = pastebin.delete("Rxe673BJ")
  60.     --
  61.     -- Returns "Paste Removed" or error message.
  62.     function Pastebin:delete(api_paste_key)
  63.         assert(type(self.api_user_key) == "string" and type(api_paste_key) == "string", "Both arguments required and must be strings!")
  64.         return http.post("http://pastebin.com/api/api_post.php", "api_dev_key="..self.url_encode(self.api_dev_key).."&api_user_key="..self.url_encode(self.api_user_key).."&api_paste_key="..self.url_encode(api_paste_key).."&api_option=delete").readAll()
  65.     end
  66.      
  67.     -- Fetches a paste from Pastebin.
  68.     -- Example: local rc4 = pastebin.get("Rxe673BJ")
  69.     --
  70.     -- Returns requested paste as a string or an error message.
  71.     function Pastebin:get(api_paste_key)
  72.         assert(type(api_paste_key) == "string", "Enter a valid paste key as a string!")
  73.         local response = http.get("http://pastebin.com/raw.php?i="..self.url_encode(api_paste_key))
  74.         return response and response.readAll()
  75.     end
  76.      
  77.     -- Fetches a private paste from Pastebin. Paste must belong to user logged in with api_user_key
  78.     -- Example: local test = pastebin.getprivate("fhLGZm3i")
  79.     --
  80.     -- Returns requested paste as a string or an error message.
  81.     function Pastebin:getprivate(api_paste_key)
  82.         assert(type(self.api_user_key) == "string" and type(api_paste_key) == "string", "Both arguments required and must be strings!")
  83.         return http.post("http://pastebin.com/api/api_raw.php", "api_dev_key="..self.url_encode(self.api_dev_key).."&api_user_key="..self.url_encode(self.api_user_key).."&api_paste_key="..self.url_encode(api_paste_key).."&api_option=show_paste").readAll()
  84.     end
  85.      
  86.     -- Lists data about all of a user's pastes. Uses user key from login(). Note that this makes it impossible to get data about random people's pastes.
  87.     -- Example: local allpastes = pastebin.list(1000)
  88.     --
  89.     -- Returns a list of data about all pastes associated with a user's account, using the table format described at the end of the document.
  90.     function Pastebin:list(api_results_limit)
  91.         assert(type(self.api_user_key) == "string", "Enter a valid user key as a string!")
  92.         local urlstr = {"api_dev_key=", self.url_encode(self.api_dev_key), "&api_user_key=", self.url_encode(self.api_user_key), "&api_option=list"}
  93.         if api_results_limit then if type(api_results_limit) == "number" then if api_results_limit > 1000 then api_results_limit = 1000 elseif api_results_limit < 1 then api_results_limit = 1 end local len = #urlstr urlstr[len+1] = "&api_results_limit=" urlstr[len+2] = self.url_encode(api_results_limit) else io.stderr:write("Results limit must be a number!\n") end end
  94.         local response = http.post("http://pastebin.com/api/api_post.php", table.concat(urlstr)).readAll()
  95.         if string.find(response,"<") then
  96.             return self:parse(response)
  97.         else
  98.             return response
  99.         end
  100.     end
  101.      
  102.     -- Lists data about the 18 currently trending pastes.
  103.     -- Example: local trendingpastes = pastebin.trending()
  104.     --
  105.     -- Returns a list of data about the 18 currently trending pastes, using the table format described at the end of this document.
  106.     function Pastebin:trending()
  107.         local response = http.post("http://pastebin.com/api/api_post.php", "api_dev_key="..self.url_encode(self.api_dev_key).."&api_option=trends").readAll()
  108.         if string.find(response,"<") then
  109.             return self:parse(response)
  110.         else
  111.             return response
  112.         end
  113.     end
  114.      
  115.     -- Lists all of a user's settings. Uses user key from login().
  116.     -- Example: local settings = pastebin.settings()
  117.     --
  118.     -- Returns a list of the user's settings, using the table format described at the end of this document.
  119.     function Pastebin:settings()
  120.         assert(type(self.api_user_key) == "string", "Enter a valid user key as a string!")
  121.         local response = http.post("http://pastebin.com/api/api_post.php", "api_dev_key="..self.url_encode(self.api_dev_key).."&api_user_key="..self.url_encode(self.api_user_key).."&api_option=userdetails").readAll()
  122.         if string.find(response,"<") then
  123.             return unpack(self:parse(response))
  124.         else
  125.             return response
  126.         end
  127.     end
  128.      
  129.     -- Edit a paste. First argument required. All others optional. Pass explicit nils to pass over an argument.
  130.     -- Example: local paste = pastebin.create("This is my awesome paste text!", "Super cool name", nil, nil, "N")
  131.     --
  132.     -- Returns paste URL or error code.
  133.     function Pastebin:edit(api_paste_code, api_paste_name, api_paste_format, api_paste_private, api_paste_expire_date)
  134.         local list = self:list(1000)
  135.         for a, b in pairs(list) do
  136.             if b and b["title"] == api_paste_name then
  137.                 self:delete(b["key"])
  138.                 self:create(api_paste_code, api_paste_name, api_paste_format, api_paste_private, api_paste_expire_date)
  139.                 return
  140.             end
  141.         end
  142.         self:create(api_paste_code, api_paste_name, api_paste_format, api_paste_private, api_paste_expire_date)                    
  143.     end
  144.    
  145.     local function passwordInput(isFirstTime)
  146.         if isFirstTime == true then    
  147.             term.clear()
  148.             print("+-----------------------------------------------+")
  149.             print("| You are using this software the first time on |")
  150.             print("| this computer. This software needs your       |")
  151.             print("| Pastebin username and password                |")
  152.             print("+-----------------------------------------------+")
  153.             write("username:")
  154.             local user = io.read()
  155.             term.clear()
  156.             print("+-----------------------------------------------+")
  157.             print("| You are using this software the first time on |")
  158.             print("| this computer. This software needs your       |")
  159.             print("| Pastebin username and password                |")
  160.             print("+-----------------------------------------------+")
  161.             write("password:")
  162.             local password = io.read()
  163.             term.clear()
  164.             return user, password
  165.         else
  166.             term.clear()
  167.             print("+-------------------------------+")
  168.             print("| Username or password is wrong |")
  169.             print("|           Try again           |")
  170.             print("+-------------------------------+")
  171.             write("username:")
  172.             local user = io.read()
  173.             term.clear()
  174.             print("+-------------------------------+")
  175.             print("| Username or password is wrong |")
  176.             print("|           Try again           |")
  177.             print("+-------------------------------+")
  178.             write("password:")
  179.             local password = io.read()
  180.             term.clear()
  181.             return user, password
  182.         end
  183.     end
  184.    
  185.     local function readUserDataFromFile()
  186.         if fs.exists("MCS_data.txt") then
  187.             local file = fs.open("MCS_data.txt", "r")
  188.             local user = file.readLine()
  189.             local password = file.readLine()
  190.             file.close()
  191.             return user, password
  192.         end
  193.         return false
  194.     end
  195.    
  196.     local function writeUserDataToFile(user, password)
  197.         local file = fs.open("MCS_data.txt", "w")
  198.         file.writeLine(user)
  199.         file.writeLine(password)
  200.         file.close()
  201.     end
  202.    
  203.     function Pastebin:login(user, password)
  204.         local isFirstTime = true
  205.         if not user or not password then
  206.             user, password = readUserDataFromFile()
  207.         end
  208.         while true do
  209.             if user and password then
  210.                 self.api_user_key = self:loginToPastebin(user, password)
  211.                 if self.api_user_key ~= "Bad API request, invalid login" then
  212.                     writeUserDataToFile(user, password)
  213.                     term.clear()
  214.                     return
  215.                 end
  216.             end
  217.             user, password = passwordInput(isFirstTime)
  218.             isFirstTime = false
  219.         end
  220.     end
  221.    
  222.     Pastebin:login(user, password)
  223.     return Pastebin
  224. end
  225.  
  226. pastebin = Pastebin("bruegge", "FW225vftc")
  227. local count = 0
  228. while true do
  229.     sleep(5)
  230.     count = count + 1
  231.     pastebin:edit(tostring(count), "MCS:Test", nil, nil, "N")
  232. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement