Advertisement
CCCoder

Paste LF

Jul 22nd, 2015
935
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 16.79 KB | None | 0 0
  1. --Minecrosoft Paste--
  2. --Most of the upload/download code is from the pastebin program.--
  3. --Lead Programmer: Eric Bernard--
  4.  
  5. if fs.exists("LimeFyre/APIs/fdialog") then
  6. os.loadAPI("LimeFyre/APIs/fdialog")
  7. end
  8.  
  9. local dx, dy = term.getSize()
  10.  
  11. local function dl(path,dest)
  12. local resp = http.get("http://pastebin.com/raw.php?i="..textutils.urlEncode(path))
  13. if resp then
  14. f = fs.open(dest,"w")
  15. f.write(resp.readAll())
  16. f.close()
  17. return true
  18. else
  19. return false
  20. end
  21. end
  22.  
  23. local function get(path)
  24. local resp = http.get("http://pastebin.com/raw.php?i="..textutils.urlEncode(path))
  25. if resp then
  26. local sResponse = resp.readAll()
  27. resp.close()
  28. return sResponse
  29. else
  30. return false
  31. end
  32. end
  33.  
  34. local function runProgram(code)
  35. local sCode = code
  36.  
  37. local res = get(sCode)
  38. if res then
  39. local func, err = load(res, sCode, "t", _ENV)
  40. if not func then
  41. return false
  42. end
  43. local success, msg = pcall(func, table.unpack(tArgs, 3))
  44. if not success then
  45. printError( msg )
  46. end
  47. end
  48. end
  49.  
  50. function clear(color)
  51. if color ~= nil then
  52. term.setBackgroundColor(color)
  53. end
  54. term.clear()
  55. term.setCursorPos(1,1)
  56. end
  57.  
  58. cursor = term.setCursorPos
  59. bg = term.setBackgroundColor
  60. text = term.setTextColor
  61. line = paintutils.drawLine
  62.  
  63. function centerPrint(text,y,distance)
  64. if text == nil then text = "Hello World!" end
  65. if y == nil then y = 1 end
  66. if distance == nil then distance = 0 end
  67.  
  68. term.setCursorPos(dx/2-#tostring(text)/2-distance,y)
  69. print(" "..text)
  70. end
  71.  
  72. function tile(txt,x,y,bgcolor,textcolor)
  73. if textcolor == bgcolor then
  74. if bgcolor == colors.black then
  75. textcolor = colors.white
  76. else
  77. textcolor = colors.black
  78. end
  79. end
  80. if x == "center" then
  81. x = dx/2-#txt/2
  82. end
  83. local width = #txt+x+1
  84. local height = y+2
  85. paintutils.drawFilledBox(x,y,width,height,bgcolor)
  86. bg(bgcolor)
  87. term.setCursorPos(x+1,y+1)
  88. text(textcolor)
  89. write(txt)
  90. return x,y,width,height
  91. end
  92.  
  93. local loop = true
  94. while loop do
  95. clear(colors.gray)
  96. text(colors.yellow)
  97. centerPrint("Minecrosoft Paste",6)
  98. local newx,newy,neww,newh = tile(" Upload ",16,8,colors.green,colors.white)
  99. local getx,gety,getw,geth = tile(" Download ",28,8,colors.cyan,colors.white)
  100. local ex,ey,ew,eh = tile(" Exit ",28,12,colors.red,colors.white)
  101. --local runx,runy,runw,runh = tile(" Run ",23,8,colors.blue,colors.white)
  102. local lx,ly,lw,lh = tile("My Uploads",14,12,colors.magenta,colors.white)
  103. local event, button, x, y = os.pullEventRaw("mouse_click")
  104. --[=[if x >= runx and x <= runw and y >= runy and y <= runh then
  105. local runloop = true
  106. local input = ""
  107. while runloop do
  108. clear(colors.gray)
  109. text(colors.yellow)
  110. centerPrint("Pastebin ID",8)
  111. line(dx/2-4,9,dx/2+6,9,colors.white)
  112. cursor(dx/2-4,9)
  113. text(colors.black)
  114. write("> "..input:sub(1,8).."_")
  115. local bx, by, bw, bh = tile("Back",27,10,colors.red,colors.white)
  116. local yx, yy, yw, yh = tile("Run",15,10,colors.green,colors.white)
  117. local event, a,b,c = os.pullEventRaw()
  118. if event == "char" then
  119. input = input..a
  120. elseif event == "key" then
  121. if a == 14 then
  122. input = input:sub(1,#input-1)
  123. elseif a == 28 then
  124. clear(colors.gray)
  125. text(colors.white)
  126. centerPrint("Getting pastebin data",9)
  127. text(colors.lightGray)
  128. centerPrint("This may take a few seconds",11)
  129. local success = runProgram(input)
  130. if not success then
  131. clear(colors.gray)
  132. text(colors.red)
  133. centerPrint("An error occurred",8)
  134. text(colors.lightGray)
  135. centerPrint("This may be because of an invalid pastebin",10)
  136. centerPrint("ID or that the connection was interrupted.",11)
  137. sleep(2)
  138. centerPrint("Press any key to continue.",15)
  139. os.pullEvent("key_up")
  140. runloop = false
  141. break
  142. end
  143.  
  144. end
  145. elseif event == "mouse_click" then
  146. local x,y = b,c
  147. if x >= bx and x <= bw and y >= by and y <= bh then
  148. runloop = false
  149. break
  150. elseif x >= yx and x <= yw and y >= yy and y <= yh then
  151. clear(colors.gray)
  152. text(colors.white)
  153. centerPrint("Getting pastebin data",9)
  154. text(colors.lightGray)
  155. centerPrint("This may take a few seconds",11)
  156. local success = runProgram(input)
  157. if not success then
  158. clear(colors.gray)
  159. text(colors.red)
  160. centerPrint("An error occurred",8)
  161. text(colors.lightGray)
  162. centerPrint("This may be because of an invalid pastebin",10)
  163. centerPrint("ID or that the connection was interrupted.",11)
  164. sleep(2)
  165. centerPrint("Press any key to continue.",15)
  166. os.pullEvent("key_up")
  167. runloop = false
  168. break
  169. end
  170. end
  171. end
  172. end]=]
  173. --[=[else]=]if x >= lx and x <= lw and y >= ly and y <= lh then
  174. local uloop = true
  175. local files = false
  176. while uloop do
  177. clear(colors.gray)
  178. text(colors.yellow)
  179. centerPrint("My Recent Paste Uploads",2)
  180. text(colors.white)
  181. if fs.exists("LimeFyre/User/Misc/paste") then
  182. local f = fs.open("LimeFyre/User/Misc/paste","r")
  183. pinfo = textutils.unserialize(f.readAll())
  184. f.close()
  185. files = true
  186. elseif fs.exists("pasteuploads") then
  187. local f = fs.open("pasteuploads","r")
  188. pinfo = textutils.unserialize(f.readAll())
  189. f.close()
  190. files = true
  191. end
  192. local cpos = 4
  193. if files then
  194. local istart = 1
  195. if #pinfo > 4 then istart = #pinfo-3 end
  196. for i=istart,#pinfo do
  197. term.setTextColor(colors.orange)
  198. centerPrint(pinfo[i].link,cpos)
  199. term.setTextColor(colors.lightGray)
  200. centerPrint("File: "..pinfo[i].name,cpos+1)
  201. cpos = cpos+3
  202. end
  203. else
  204. text(colors.white)
  205. centerPrint("No uploads!",3)
  206. end
  207. local bx, by, bw, bh = tile(" Back ","center",16,colors.blue,colors.white)
  208. local event, button, x, y = os.pullEventRaw("mouse_click")
  209. if x >= bx and x <= bw and y >= by and y <= bh then
  210. uloop = false
  211. break
  212. end
  213. end
  214. elseif x >= newx and x <= neww and y >= newy and y <= newh then
  215. local input = ""
  216. local uploop = true
  217. while uploop do
  218. clear(colors.gray)
  219. text(colors.yellow)
  220. centerPrint("Path to file",9)
  221. line(2,10,dx-2,10,colors.white)
  222. cursor(2,10)
  223. text(colors.black)
  224. local charlimit = dx-8
  225. disin = input
  226. if #input > charlimit then
  227. disin = disin:sub(#disin-charlimit,#disin)
  228. end
  229. write("> "..disin.."_")
  230.  
  231. local ux,uy,uw,uh = tile("Upload",10,12,colors.green,colors.white)
  232. local bx,by,bw,bh = tile(" Back ",36,12,colors.red,colors.white)
  233. local fx,fy,fw,fh = 50
  234. if fs.exists("LimeFyre/APIs/fdialog") then
  235. fx,fy,fw,fh = tile(" Find ",23,12,colors.cyan,colors.white)
  236. end
  237. local event, a,b,c = os.pullEventRaw()
  238. if event == "char" then
  239. input = input..a
  240. elseif event == "key" then
  241. if a == 14 then
  242. input = input:sub(1,#input-1)
  243. elseif a == 28 then
  244. if fs.exists(input) and input ~= "" then
  245. local sName = fs.getName(input)
  246. local f = fs.open(input,"r")
  247. local content = f.readAll()
  248. f.close()
  249. local key = "0ec2eb25b6166c0c27a394ae118ad829"
  250. local response = http.post(
  251. "http://pastebin.com/api/api_post.php",
  252. "api_option=paste&"..
  253. "api_dev_key="..key.."&"..
  254. "api_paste_format=lua&"..
  255. "api_paste_name="..textutils.urlEncode(sName).."&"..
  256. "api_paste_code="..textutils.urlEncode(content)
  257. )
  258. if response then
  259. local sResponse = response.readAll()
  260. response.close()
  261. local sCode = string.match( sResponse, "[^/]+$" )
  262. clear(colors.gray)
  263. text(colors.lime)
  264. centerPrint("Uploaded to "..sResponse,9)
  265. text(colors.green)
  266. if fs.exists("LimeFyre/User") then
  267. if fs.exists("LimeFyre/User/Misc/paste") then
  268. local f = fs.open("LimeFyre/User/Misc/paste","r")
  269. pinfo = textutils.unserialize(f.readAll())
  270. f.close()
  271. else
  272. pinfo = {}
  273. end
  274. local n = #pinfo+1
  275. pinfo[n] = {}
  276. pinfo[n].name = input
  277. pinfo[n].link = sCode
  278. local f = fs.open("LimeFyre/User/Misc/paste","w")
  279. f.write(textutils.serialize(pinfo))
  280. f.close()
  281. else
  282. if fs.exists("pasteuploads") then
  283. local f = fs.open("LimeFyre/User/Misc/paste","r")
  284. local pinfo = textutils.unserialize(f.readAll())
  285. f.close()
  286. else
  287. pinfo = {}
  288. end
  289. local n = #pinfo+1
  290. pinfo[n] = {}
  291. pinfo[n].name = input
  292. pinfo[n].link = sCode
  293. local f = fs.open("LimeFyre/User/Misc/paste","w")
  294. f.write(textutils.serialize(pinfo))
  295. f.close()
  296. end
  297. centerPrint("Use "..sCode.." to download it elsewhere.",11)
  298. centerPrint("The code has been saved in case you lose it.",12)
  299. centerPrint("Press any key to return",13)
  300. os.pullEvent("key_up")
  301. uploop = false
  302. else
  303. clear(colors.gray)
  304. text(colors.red)
  305. centerPrint("Something went wrong.",5)
  306. text(colors.lightGray)
  307. centerPrint("Connection with pastebin could not be",7)
  308. centerPrint("established or maintained long enough",8)
  309. centerPrint("to upload. Please try again later.",9)
  310. centerPrint("We apologize for the inconvenience.",10)
  311. centerPrint("Press any key to return.",15)
  312. os.pullEvent("key_up")
  313. uploop = false
  314. end
  315. end
  316. end
  317. elseif event == "mouse_click" then
  318. local button, x, y = a,b,c
  319. if x >= bx and x <= bw and y >= by and y <= bh then
  320. uploop = false
  321. break
  322. elseif x >= ux and x <= uw and y >= uy and y <= uh then
  323. if fs.exists(input) and input ~= "" then
  324. local sName = fs.getName(input)
  325. local f = fs.open(input,"r")
  326. local content = f.readAll()
  327. f.close()
  328. local key = "0ec2eb25b6166c0c27a394ae118ad829"
  329. local response = http.post(
  330. "http://pastebin.com/api/api_post.php",
  331. "api_option=paste&"..
  332. "api_dev_key="..key.."&"..
  333. "api_paste_format=lua&"..
  334. "api_paste_name="..textutils.urlEncode(sName).."&"..
  335. "api_paste_code="..textutils.urlEncode(content)
  336. )
  337. if response then
  338. local sResponse = response.readAll()
  339. response.close()
  340. local sCode = string.match( sResponse, "[^/]+$" )
  341. clear(colors.gray)
  342. text(colors.lime)
  343. centerPrint("Uploaded to "..sResponse,9)
  344. text(colors.green)
  345. if fs.exists("LimeFyre/User") then
  346. if fs.exists("LimeFyre/User/Misc/paste") then
  347. local f = fs.open("LimeFyre/User/Misc/paste","r")
  348. pinfo = textutils.unserialize(f.readAll())
  349. f.close()
  350. else
  351. pinfo = {}
  352. end
  353. local n = #pinfo+1
  354. pinfo[n] = {}
  355. pinfo[n].name = input
  356. pinfo[n].link = sCode
  357. local f = fs.open("LimeFyre/User/Misc/paste","w")
  358. f.write(textutils.serialize(pinfo))
  359. f.close()
  360. else
  361. if fs.exists("pasteuploads") then
  362. local f = fs.open("LimeFyre/User/Misc/paste","r")
  363. local pinfo = textutils.unserialize(f.readAll())
  364. f.close()
  365. else
  366. pinfo = {}
  367. end
  368. local n = #pinfo+1
  369. pinfo[n] = {}
  370. pinfo[n].name = input
  371. pinfo[n].link = sCode
  372. local f = fs.open("LimeFyre/User/Misc/paste","w")
  373. f.write(textutils.serialize(pinfo))
  374. f.close()
  375. end
  376. centerPrint("Use "..sCode.." to download it elsewhere.",11)
  377. centerPrint("The code has been saved in case you lose it.",12)
  378. centerPrint("Press any key to return",13)
  379. os.pullEvent("key_up")
  380. uploop = false
  381. else
  382. clear(colors.gray)
  383. text(colors.red)
  384. centerPrint("Something went wrong.",5)
  385. text(colors.lightGray)
  386. centerPrint("Connection with pastebin could not be",7)
  387. centerPrint("established or maintained long enough",8)
  388. centerPrint("to upload. Please try again later.",9)
  389. centerPrint("We apologize for the inconvenience.",10)
  390. centerPrint("Press any key to return.",15)
  391. os.pullEvent("key_up")
  392. uploop = false
  393. end
  394. end
  395. elseif x >= fx and x <= fw and y >= fy and y <= fh then
  396. local file = fdialog.open("/")
  397. if file ~= nil then
  398. input = file
  399. end
  400. end
  401. end
  402. end
  403. elseif x >= getx and x <= getw and y >= gety and y <= geth then
  404. local pasteinput = ""
  405. local nameinput = ""
  406. local input = ""
  407. local selected = "paste"
  408. local pchar = "_"
  409. local nchar = ""
  410. local getloop = true
  411. while getloop do
  412. if selected == "paste" then
  413. if #pasteinput > 8 then
  414. input = input:sub(1,8)
  415. end
  416. pasteinput = input
  417. pchar = "_"
  418. nchar = ""
  419. else
  420. nameinput = input
  421. pchar = ""
  422. nchar = "_"
  423. end
  424. clear(colors.gray)
  425. text(colors.yellow)
  426. centerPrint("Pastebin code",3)
  427. centerPrint("Destination path",8)
  428. line(dx/2-4,4,dx/2+6,4,colors.white)
  429. line(2,9,dx-2,9,colors.white)
  430. text(colors.gray)
  431. term.setCursorPos(dx/2-4,4)
  432. write("> "..pasteinput..pchar)
  433. local charlimit = dx-8
  434. term.setCursorPos(2,9)
  435. if #nameinput >= charlimit then
  436. write("> "..nameinput:sub(#nameinput-charlimit,#nameinput)..nchar)
  437. else
  438. write("> "..nameinput..nchar)
  439. end
  440. local okx,oky,okw,okh = tile("Download","center",11,colors.green,colors.white)
  441. local bx,by,bw,bh = tile("Cancel","center",15,colors.red,colors.white)
  442. local event, p1, p2, p3 = os.pullEvent()
  443. if event == "char" then
  444. local character = p1
  445. input = input..character
  446. elseif event == "paste" then
  447. local pasted = p1
  448. input = input..pasted
  449. elseif event == "key" then
  450. local key = p1
  451. if key == 14 then
  452. input = input:sub(1,#input-1)
  453. elseif key == 28 then
  454. bg(colors.black)
  455. textColor(colors.white)
  456. clear()
  457. shell.run(input)
  458. local _,cy = term.getCursorPos()
  459. textColor(colors.white)
  460. centerPrint("Press any key to return to LimeFyre",cy)
  461. local event, key = os.pullEventRaw("key")
  462. runloop = false
  463. break
  464. end
  465. elseif event == "mouse_click" then
  466. local button = p1
  467. local x = p2
  468. local y = p3
  469. if y == 4 then
  470. if selected == "name" then
  471. nameinput = input
  472. end
  473. input = pasteinput
  474. selected = "paste"
  475. elseif y == 9 then
  476. if selected == "paste" then
  477. pasteinput = input
  478. end
  479. input = nameinput
  480. selected = "name"
  481. elseif x >= okx and x <= okw and y >= oky and y <= okh then
  482. if fs.exists(nameinput) then
  483. clear(colors.gray)
  484. text(colors.red)
  485. centerPrint("The file '"..nameinput:sub(1,20).."' already exists.",10)
  486. sleep(2)
  487. else
  488. local ok = dl(pasteinput,nameinput)
  489. if pasteinput == "" then ok = false end
  490. if ok then
  491. clear(colors.gray)
  492. text(colors.lime)
  493. centerPrint("The program was successfully downloaded!",9)
  494. text(colors.green)
  495. centerPrint("Press any key to return to the main screen.",11)
  496. os.pullEvent("key")
  497. getloop = false
  498. break
  499. else
  500. clear(colors.gray)
  501. text(colors.red)
  502. centerPrint("Something went wrong.",5)
  503. text(colors.lightGray)
  504. centerPrint("Either the pastebin ID entered "..'("'..pasteinput..'")',7)
  505. centerPrint("doesn't exist, or your connection to pastebin was",8)
  506. centerPrint("interrupted or not established. Please try again.",9)
  507. centerPrint("Press any key to continue.",15)
  508. os.pullEvent("key_up")
  509. end
  510. end
  511. elseif x >= bx and x <= bw and y >= by and y <= bh then
  512. getloop = false
  513. break
  514. end
  515. end
  516. end
  517. elseif x >= ex and x <= ew and y >= ey and y <= eh then
  518. loop = false
  519. clear(colors.black)
  520. text(colors.white)
  521. centerPrint("Thank you for using Minecrosoft Paste",2)
  522. break
  523. end
  524. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement