Advertisement
bruegge

analyser

Mar 16th, 2018
1,503
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.83 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.edit("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. local message = self:create(api_paste_code, api_paste_name, api_paste_format, api_paste_private, api_paste_expire_date)
  139. return message
  140. end
  141. end
  142. local message = self:create(api_paste_code, api_paste_name, api_paste_format, api_paste_private, api_paste_expire_date)
  143. return message
  144. end
  145.  
  146. local function passwordInput(isFirstTime)
  147. if isFirstTime == true then
  148. term.clear()
  149. print("+-----------------------------------------------+")
  150. print("| You are using this software the first time on |")
  151. print("| this computer. This software needs your |")
  152. print("| Pastebin username and password |")
  153. print("+-----------------------------------------------+")
  154. write("username:")
  155. local user = io.read()
  156. term.clear()
  157. print("+-----------------------------------------------+")
  158. print("| You are using this software the first time on |")
  159. print("| this computer. This software needs your |")
  160. print("| Pastebin username and password |")
  161. print("+-----------------------------------------------+")
  162. write("password:")
  163. local password = io.read()
  164. term.clear()
  165. return user, password
  166. else
  167. term.clear()
  168. print("+-------------------------------+")
  169. print("| Username or password is wrong |")
  170. print("| Try again |")
  171. print("+-------------------------------+")
  172. write("username:")
  173. local user = io.read()
  174. term.clear()
  175. print("+-------------------------------+")
  176. print("| Username or password is wrong |")
  177. print("| Try again |")
  178. print("+-------------------------------+")
  179. write("password:")
  180. local password = io.read()
  181. term.clear()
  182. return user, password
  183. end
  184. end
  185.  
  186. local function readUserDataFromFile()
  187. if fs.exists("MCS_data.txt") then
  188. local file = fs.open("MCS_data.txt", "r")
  189. local user = file.readLine()
  190. local password = file.readLine()
  191. file.close()
  192. return user, password
  193. end
  194. return false
  195. end
  196.  
  197. local function writeUserDataToFile(user, password)
  198. local file = fs.open("MCS_data.txt", "w")
  199. file.writeLine(user)
  200. file.writeLine(password)
  201. file.close()
  202. end
  203.  
  204. function Pastebin:login(user, password)
  205. local isFirstTime = true
  206. if not user or not password then
  207. user, password = readUserDataFromFile()
  208. end
  209. while true do
  210. if user and password then
  211. self.api_user_key = self:loginToPastebin(user, password)
  212. if self.api_user_key ~= "Bad API request, invalid login" then
  213. writeUserDataToFile(user, password)
  214. term.clear()
  215. return
  216. end
  217. end
  218. user, password = passwordInput(isFirstTime)
  219. isFirstTime = false
  220. end
  221. end
  222.  
  223. Pastebin:login(user, password)
  224. return Pastebin
  225. end
  226.  
  227. function toOutput(tabulator, variable, variableName)
  228. local output = ""
  229. local tab = ""
  230. for i = 0, tabulator do
  231. tab = tab.."\t"
  232. end
  233.  
  234. if type(variable) == "table" then
  235. output = output..tab.."table:"..variableName.."\n"
  236. for a,b in pairs(variable) do
  237. output = output..toOutput(tabulator + 1, b, a)
  238. end
  239. end
  240. if type(variable) == "string" then
  241. output = output..tab.."string:"..variableName.." value:"..variable.."\n"
  242. end
  243. if type(variable) == "number" then
  244. output = output..tab.."number:"..variableName.." value:"..tostring(variable).."\n"
  245. end
  246. if type(variable) == "nil" then
  247. output = output..tab.."nil:"..variableName.." value: nil".."\n"
  248. end
  249. if type(variable) == "boolean" then
  250. output = output..tab.."boolean:"..variableName.." value:"..tostring(variable).."\n"
  251. end
  252. if type(variable) == "function" then
  253. output = output..tab.."function:"..variableName.." value:"..tostring(variable).."\n"
  254. end
  255. return output
  256. end
  257.  
  258.  
  259. pastebin = Pastebin()
  260.  
  261. ME = peripheral.wrap("back")
  262.  
  263. print(ME.getItemDetail(fingerprint))
  264.  
  265. local output = toOutput(1,ME,"ME")
  266. pastebin:edit(output, "AnalyseResult", nil, nil, "N")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement