Advertisement
craniumkid22

SmartPaste

Nov 13th, 2012
604
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 27.85 KB | None | 0 0
  1. local tArgs = {...}
  2. local key = "0ec2eb25b6166c0c27a394ae118ad829"
  3. local x, y = term.getSize()
  4. local loginTab = {
  5. ".------------------------------------------------.",
  6. "|                                                |",
  7. "|                                                |",
  8. "|                                                |",
  9. "|                                                |",
  10. "|          .-------------------------.           |",
  11. "|          |      Please login       |           |",
  12. "|          |                         |           |",
  13. "|          | User>                   |           |",
  14. "|          | Pass>                   |           |",
  15. "|          |                         |           |",
  16. "|          '-------------------------'           |",
  17. "|                                                |",
  18. "|                                                |",
  19. "|                                                |",
  20. "|                                                |",
  21. "|                                                |",
  22. "|________________________________________________|",
  23. }
  24.  
  25. --menu functions
  26. local function newmenu(tList,x,y,height,menu)
  27.     local function maxlen(t)
  28.         local len=0
  29.         for i=1,#t do
  30.             local curlen=string.len(type(t[i])=='table' and t[i][1] or t[i])
  31.             if curlen>len then len=curlen end
  32.         end
  33.         return len
  34.     end
  35.    
  36.     local max=maxlen(tList)
  37.     x=x or 1
  38.     y=y or 1
  39.     y=y-1
  40.     height=height or #tList
  41.     height=height+1
  42.     local selected=1
  43.     local scrolled=0
  44.     local function render()
  45.         for num,item in ipairs(tList) do
  46.             if num>scrolled and num<scrolled+height then
  47.                 term.setCursorPos(x,y+num-scrolled)
  48.                 local current=(type(item)=='table' and item[1] or item)
  49.                 if num == selected then
  50.                     term.setBackgroundColor(colors.black)
  51.                     term.setTextColor(colors.white)
  52.                     write("[")
  53.                     term.setTextColor(colors.blue)
  54.                     term.setBackgroundColor(colors.white)
  55.                     write(current)
  56.                     term.setBackgroundColor(colors.black)
  57.                     term.setTextColor(colors.white)
  58.                     write("]"..(max-#current>0 and string.rep(' ',max-#current) or ''))
  59.                 else
  60.                     term.setBackgroundColor(colors.black)
  61.                     term.setTextColor(colors.red)
  62.                     write(" "..current..(max-#current>0 and string.rep(' ',max-#current + 1) or ''))
  63.                 end
  64.             end
  65.         end
  66.     end
  67.     while true do
  68.         render()
  69.         local events = {os.pullEvent()}
  70.         if events[1] == "key" and events[2] == 200 and selected > 1 then
  71.             if selected - 1 <= scrolled then scrolled = scrolled - 1 end
  72.             selected = selected - 1
  73.         elseif events[1] == "key" and events[2] == 208 and selected < #tList then
  74.             selected = selected + 1
  75.             if selected >= height + scrolled then scrolled = scrolled + 1 end
  76.         elseif events[1] == "key" and events[2] == 28 or events[2] == 156 then
  77.             return (type(tList[selected]) == 'table' and tList[selected][2](tList[selected][1]) or tList[selected])
  78.         elseif events[1] == "mouse_click" and events[2] == 1 then
  79.             local tX, tY = term.getSize()
  80.             if events[3] >= 7 and events[3] <= 3 and events[4] == tY - 7 then
  81.                 print("Success")
  82.                 sleep(5)
  83.             elseif events[3] == 48 and events[4] == 2 then
  84.                 term.setCursorPos(events[3], events[4])
  85.                 term.setBackgroundColor(colors.gray)
  86.                 term.setTextColor(colors.red)
  87.                 write("X")
  88.                 return "Exit SmartPaste"
  89.             end
  90.         elseif events[1] == "mouse_scroll" and events[2] == - 1 and selected > 1 then
  91.             if selected - 1 <= scrolled then scrolled = scrolled - 1 end
  92.             selected = selected - 1
  93.         elseif events[1] == "mouse_scroll" and events[2] == 1 and selected < #tList then
  94.             selected = selected + 1
  95.             if selected >= height + scrolled then scrolled = scrolled + 1 end
  96.         end
  97.     end
  98. end
  99.  
  100. local function mainMenu(logged)
  101.     local menu = {(logged == true and "Logout" or "Login"), "Create post", "Delete post", "Retrieve post", "Exit SmartPaste"}
  102.     local n = 1
  103.     local old = 1
  104.     while true do
  105.         term.setBackgroundColor(colors.black)
  106.         for i = 1, #menu do
  107.             term.setCursorPos(3,y - 7 + i)
  108.             term.setTextColor(colors.red)
  109.             write(menu[i])
  110.         end
  111.         term.setCursorPos(2, y - 7 + old)
  112.         term.setBackgroundColor(colors.black)
  113.         term.setTextColor(colors.red)
  114.         write(" "..menu[old].." ")
  115.         term.setCursorPos(2, y - 7 + n)
  116.         term.setTextColor(colors.yellow)
  117.         write("[")
  118.         term.setBackgroundColor(colors.white)
  119.         term.setTextColor(colors.blue)
  120.         write(menu[n])
  121.         term.setTextColor(colors.yellow)
  122.         term.setBackgroundColor(colors.black)
  123.         write("]")
  124.         term.setTextColor(colors.red)
  125.         local events = {os.pullEvent()}
  126.         if events[1] == "key" and events[2] == 200 or events[1] == "mouse_scroll" and events[2] == - 1 then
  127.             if n > 1 then
  128.                 old = n
  129.                 n = n - 1
  130.             else
  131.                 old = n
  132.                 n = 1
  133.             end
  134.         elseif events[1] == "key" and events[2] == 208 or events[1] == "mouse_scroll" and events[2] == 1 then
  135.             if n < #menu then
  136.                 old = n
  137.                 n = n + 1
  138.             else
  139.                 old = n
  140.                 n = #menu
  141.             end
  142.         elseif events[1] == "key" and events[2] == 28 or events[2] == 156 then
  143.             return menu[n]
  144.         elseif events[1] == "mouse_click" and events[2] == 1 then
  145.             if events[3] >= 3 and events[3] <= 7 and events[4] == y - 6 then
  146.                 old = n
  147.                 term.setCursorPos(2, y - 7 + old)
  148.                 term.setBackgroundColor(colors.black)
  149.                 term.setTextColor(colors.red)
  150.                 write(" "..menu[old].." ")
  151.                 n = 1
  152.                 term.setCursorPos(2, y - 7 + n)
  153.                 term.setTextColor(colors.yellow)
  154.                 write("[")
  155.                 term.setBackgroundColor(colors.white)
  156.                 term.setTextColor(colors.blue)
  157.                 write(menu[n])
  158.                 term.setTextColor(colors.yellow)
  159.                 term.setBackgroundColor(colors.black)
  160.                 write("]")
  161.                 sleep(.5)
  162.                 return menu[n]
  163.             elseif events[3] >= 3 and events[3] <= 13 and events[4] == y - 5 then
  164.                 old = n
  165.                 term.setCursorPos(2, y - 7 + old)
  166.                 term.setBackgroundColor(colors.black)
  167.                 term.setTextColor(colors.red)
  168.                 write(" "..menu[old].." ")
  169.                 n = 2
  170.                 term.setCursorPos(2, y - 7 + n)
  171.                 term.setTextColor(colors.yellow)
  172.                 write("[")
  173.                 term.setBackgroundColor(colors.white)
  174.                 term.setTextColor(colors.blue)
  175.                 write(menu[n])
  176.                 term.setTextColor(colors.yellow)
  177.                 term.setBackgroundColor(colors.black)
  178.                 write("]")
  179.                 sleep(.5)
  180.                 return menu[n]
  181.             elseif events[3] >= 3 and events[3] <= 13 and events[4] == y - 4 then
  182.                 old = n
  183.                 term.setCursorPos(2, y - 7 + old)
  184.                 term.setBackgroundColor(colors.black)
  185.                 term.setTextColor(colors.red)
  186.                 write(" "..menu[old].." ")
  187.                 n = 3
  188.                 term.setCursorPos(2, y - 7 + n)
  189.                 term.setTextColor(colors.yellow)
  190.                 write("[")
  191.                 term.setBackgroundColor(colors.white)
  192.                 term.setTextColor(colors.blue)
  193.                 write(menu[n])
  194.                 term.setTextColor(colors.yellow)
  195.                 term.setBackgroundColor(colors.black)
  196.                 write("]")
  197.                 sleep(.5)
  198.                 return menu[n]
  199.             elseif events[3] >= 3 and events[3] <= 15 and events[4] == y - 3 then
  200.                 old = n
  201.                 term.setCursorPos(2, y - 7 + old)
  202.                 term.setBackgroundColor(colors.black)
  203.                 term.setTextColor(colors.red)
  204.                 write(" "..menu[old].." ")
  205.                 n = 4
  206.                 term.setCursorPos(2, y - 7 + n)
  207.                 term.setTextColor(colors.yellow)
  208.                 write("[")
  209.                 term.setBackgroundColor(colors.white)
  210.                 term.setTextColor(colors.blue)
  211.                 write(menu[n])
  212.                 term.setTextColor(colors.yellow)
  213.                 term.setBackgroundColor(colors.black)
  214.                 write("]")
  215.                 sleep(.5)
  216.                 return menu[n]
  217.             elseif events[3] >= 3 and events[3] <= 17 and events[4] == y - 2 then
  218.                 old = n
  219.                 term.setCursorPos(2, y - 7 + old)
  220.                 term.setBackgroundColor(colors.black)
  221.                 term.setTextColor(colors.red)
  222.                 write(" "..menu[old].." ")
  223.                 n = 5
  224.                 term.setCursorPos(2, y - 7 + n)
  225.                 term.setTextColor(colors.yellow)
  226.                 write("[")
  227.                 term.setBackgroundColor(colors.white)
  228.                 term.setTextColor(colors.blue)
  229.                 write(menu[n])
  230.                 term.setTextColor(colors.yellow)
  231.                 term.setBackgroundColor(colors.black)
  232.                 write("]")
  233.                 sleep(.5)
  234.                 return menu[n]
  235.             elseif events[2] == 1 and events[3] == 48 and events[4] == 2 then
  236.                 term.setCursorPos(events[3], events[4])
  237.                 term.setBackgroundColor(colors.white)
  238.                 term.setTextColor(colors.red)
  239.                 write("X")
  240.                 return "Exit SmartPaste"
  241.             end
  242.         end
  243.     end
  244. end
  245.  
  246. -- limit read function
  247. local function limitRead(limX, rChar)
  248. term.setCursorBlink(true)
  249. local origX, origY = term.getCursorPos()
  250. local returnString = ""
  251. while true do
  252.     local xPos, yPos = term.getCursorPos()
  253.     local event, p1, p2 = os.pullEvent()
  254.     if event == "char" then
  255.         returnString = returnString..p1
  256.         if not rChar then
  257.             if not limX then
  258.                 term.setTextColor(colors.blue)
  259.                 write(p1)
  260.             else
  261.                 if string.len(returnString) >= limX then
  262.                     term.setTextColor(colors.blue)
  263.                     term.setCursorPos(origX, origY)
  264.                     write(string.sub(returnString, (string.len(returnString)-limX)+1))
  265.                 elseif string.len(returnString) < limX then
  266.                     term.setTextColor(colors.blue)
  267.                     write(p1)
  268.                 end
  269.             end
  270.         else
  271.             if not limX then
  272.                 term.setTextColor(colors.blue)
  273.                 write(rChar)
  274.             else
  275.                 if string.len(returnString) >= limX then
  276.                     term.setTextColor(colors.blue)
  277.                     term.setCursorPos(origX, origY)
  278.                     write(string.rep(rChar, limX))
  279.                 elseif string.len(returnString) < limX then
  280.                     term.setTextColor(colors.blue)
  281.                     write(rChar)
  282.                 end
  283.             end
  284.         end
  285.     elseif event == "key" and p1 == 14 then --backspace
  286.         returnString = string.sub(returnString, 1, (string.len(returnString))-1)
  287.         term.setCursorPos(xPos-1,yPos)
  288.         term.setTextColor(colors.blue)
  289.         write(" ")
  290.         term.setCursorPos(origX, origY)
  291.         if string.len(returnString) >= limX then
  292.             if not rChar then
  293.                 term.setTextColor(colors.blue)
  294.                 write(string.sub(returnString, (string.len(returnString)-limX)+1))
  295.             else
  296.                 term.setTextColor(colors.blue)
  297.                 write(string.rep(rChar,limX))
  298.             end
  299.         else
  300.             if not rChar then
  301.                 term.setTextColor(colors.blue)
  302.                 write(returnString)
  303.             else
  304.                 term.setTextColor(colors.blue)
  305.                 write(string.rep(rChar, string.len(returnString)))
  306.             end
  307.         end
  308.     elseif event == "key" and p1 == 28 then --enter
  309.         break
  310.     end
  311. end
  312. term.setCursorBlink(false)
  313. return returnString
  314. end
  315.  
  316. --login
  317. local function login()
  318. local x, y = term.getSize()
  319. term.clear()
  320. term.setCursorPos(1,1)
  321. term.setTextColor(colors.orange)
  322. write("."..string.rep("-", x - 3)..".")
  323. for i = 2, y - 1 do
  324.     term.setCursorPos(1,i)
  325.     write("|")
  326.     term.setCursorPos(x - 1, i)
  327.     write("|")
  328. end
  329. write("'"..string.rep("-", x - 3).."'")
  330. term.setTextColor(colors.white)
  331. term.setCursorPos(12,6)
  332. write(".-------------------------.")
  333. term.setCursorPos(12,7)
  334. write("|      ")
  335. term.setTextColor(colors.red)
  336. write("Please login")
  337. term.setTextColor(colors.white)
  338. write("       |")
  339. term.setCursorPos(12,8)
  340. write("|                         |")
  341. term.setCursorPos(12,9)
  342. write("| ")
  343. term.setTextColor(colors.red)
  344. write("User")
  345. term.setTextColor(colors.yellow)
  346. write(">")
  347. term.setTextColor(colors.white)
  348. write("                   |")
  349. term.setCursorPos(12,10)
  350. write("| ")
  351. term.setTextColor(colors.red)
  352. write("Pass")
  353. term.setTextColor(colors.yellow)
  354. write(">")
  355. term.setTextColor(colors.white)
  356. write("                   |")
  357. term.setCursorPos(12,11)
  358. write("|                         |")
  359. term.setCursorPos(12,12)
  360. write("'-------------------------'")
  361. term.setCursorPos(20,9)
  362. local inputUser = limitRead(17)
  363. term.setCursorPos(20,10)
  364. local inputPass = limitRead(17, "*")
  365. return inputUser, inputPass
  366. end
  367.  
  368. --gui
  369. local function frame()
  370.     term.setCursorPos(3,2)
  371.     term.setTextColor(colors.blue)
  372.     write("SmartPaste")
  373.     term.setCursorPos(x - 6,2)
  374.     term.setTextColor(colors.white)
  375.     write("_")
  376.     term.setTextColor(colors.yellow)
  377.     write("[]")
  378.     term.setTextColor(colors.red)
  379.     write("X ")
  380.     term.setCursorPos(1,1)
  381.     term.setTextColor(colors.orange)
  382.     write("."..string.rep("-", x - 3)..".")
  383.     for i = 2, y - 1 do
  384.         term.setCursorPos(1,i)
  385.         write("|")
  386.         term.setCursorPos(x - 1,i)
  387.         write("|")
  388.     end
  389.     term.setCursorPos(1,y)
  390.     write("+"..string.rep("-", x - 3).."+")
  391.     term.setCursorPos(1,3)
  392.     write("+"..string.rep("-", x - 3).."+")
  393. end
  394.  
  395. --file system
  396. local function fList(dir)
  397.     dir=shell.resolve(dir)
  398.     term.clear()
  399.     frame()
  400.     local _list = fs.list(dir)
  401.     table.sort(_list)
  402.     local selected=newmenu(_list,3,5,({term.getSize()})[2]-6)
  403.     local fReturn = dir..'/'..selected
  404.     if fs.isDir(shell.resolve(fReturn)) then
  405.         return fList(fReturn)
  406.     else
  407.         return fReturn,selected
  408.     end
  409. end
  410.  
  411. local function confirm(menu)
  412. local menu = menu or ""
  413. while true do
  414.     local events = {os.pullEvent()}
  415.     if events[1] == "key" and events[2] == 28 then
  416.         term.setBackgroundColor(colors.gray)
  417.         term.setCursorPos(23,11)
  418.         write(" OK ")
  419.         sleep(.25)
  420.         term.setBackgroundColor(colors.black)
  421.         return menu
  422.     elseif events[1] == "mouse_click" and events[2] == 1 and events[3] >= 23 and events[3] <= 27 and events[4] == 11 then
  423.         term.setBackgroundColor(colors.gray)
  424.         term.setCursorPos(23,11)
  425.         write(" OK ")
  426.         sleep(.25)
  427.         term.setBackgroundColor(colors.black)
  428.         return menu
  429.     elseif events[1] == "mouse_click" and events[2] == 1 and events[3] == 48 and events[4] == 2 then
  430.         return "Exit SmartPaste"
  431.     end
  432. end
  433. end
  434.  
  435. --messages
  436. local function failConnect()
  437.     term.clear()
  438.     frame()
  439.     term.setTextColor(colors.white)
  440.     term.setCursorPos(9,7)
  441.     write(".------------------------------.")
  442.     term.setCursorPos(9,8)
  443.     write("|")
  444.     term.setTextColor(colors.yellow)
  445.     write("      Unable to connect!      ")
  446.     term.setTextColor(colors.white)
  447.     write("|")
  448.     term.setCursorPos(9,9)
  449.     write("|")
  450.     term.setTextColor(colors.yellow)
  451.     write("    Please try again later    ")
  452.     term.setTextColor(colors.white)
  453.     write("|")
  454.     term.setCursorPos(9,10)
  455.     write("|                              |")
  456.     term.setCursorPos(9,11)
  457.     write("|            ")
  458.     term.setTextColor(colors.blue)
  459.     write("[ OK ]")
  460.     term.setTextColor(colors.white)
  461.     write("            |")
  462.     term.setCursorPos(9,12)
  463.     write("'------------------------------'")
  464.     confirm()
  465. end
  466.  
  467. -- application code
  468. local logged = false
  469. local userKey = ""
  470. while true do
  471.     term.clear()
  472.     frame()
  473.     local fMenu = mainMenu(logged)
  474.     if fMenu == "Login" then
  475.         local user,pass = login()
  476.        
  477.         local response = http.post(
  478.         "http://pastebin.com/api/api_login.php",
  479.         "api_dev_key="..key.."&"..
  480.         "api_user_name="..textutils.urlEncode(user).."&"..  
  481.         "api_user_password="..textutils.urlEncode(pass)  
  482.         )
  483.        
  484.         local badRequests = {
  485.         "Bad API request, invalid login",
  486.         "Bad API request, account not active"
  487.         }
  488.        
  489.         if response then
  490.             local sResponse = response.readAll()
  491.             response.close()
  492.             if sResponse == badRequests[1] then
  493.                 term.setCursorPos(19,11)
  494.                 term.setTextColor(colors.yellow)
  495.                 write("Invalid Login")
  496.                 sleep(2)
  497.                 logged = false
  498.             elseif sResponse == badRequests[2] then
  499.                 term.setCursorPos(17,11)
  500.                 term.setTextColor(colors.yellow)
  501.                 write("Account inactive")
  502.                 sleep(2)
  503.                 logged = false
  504.             else
  505.                 userKey = sResponse
  506.                 logged = true
  507.             end
  508.         else
  509.             failConnect()
  510.         end
  511.     elseif fMenu == "Logout" then
  512.         logged = false
  513.         userKey = ""
  514.     elseif fMenu == "Create post" then --create post
  515.         local postItem = fList("/")
  516.         if postItem ~= "Exit SmartPaste" then
  517.             local file = fs.open(postItem, "r")
  518.             local postText = file.readAll()
  519.             file.close()
  520.             while true do
  521.                 term.clear()
  522.                 frame()
  523.                 term.setTextColor(colors.white)
  524.                 term.setCursorPos(9,7)
  525.                 write(".------------------------------.")
  526.                 term.setCursorPos(9,8)
  527.                 write("|           ")
  528.                 term.setTextColor(colors.red)
  529.                 write("Save as:")
  530.                 term.setTextColor(colors.white)
  531.                 write("           |")
  532.                 term.setCursorPos(9,9)
  533.                 write("|   ")
  534.                 term.setTextColor(colors.yellow)
  535.                 write(">")
  536.                 term.setTextColor(colors.white)
  537.                 write("                          |")
  538.                 term.setCursorPos(9,10)
  539.                 write("'------------------------------'")
  540.                 term.setCursorPos(15,9)
  541.                 local postName = limitRead(22)
  542.                 if postName ~= "" then
  543.                    
  544.                 --add options for making private/unlisted/public
  545.                 --add options for expiry date
  546.                 --add options for format highlighting
  547.                
  548.                     local response = http.post(
  549.                         "http://pastebin.com/api/api_post.php",
  550.                         "api_option=paste&"..
  551.                         "api_dev_key="..key.."&"..
  552.                         "api_paste_format=lua&"..
  553.                         "api_user_key="..userKey.."&"..
  554.                         "api_paste_name="..textutils.urlEncode(postName).."&"..
  555.                         "api_paste_code="..textutils.urlEncode(postText)
  556.                         )
  557.                     if response then
  558.                         local sResponse = response.readAll()
  559.                         response.close()
  560.                         term.clear()
  561.                         frame()
  562.                         term.setTextColor(colors.white)
  563.                         term.setCursorPos(9,7)
  564.                         write(".------------------------------.")
  565.                         term.setCursorPos(9,8)
  566.                         write("|        ")
  567.                         term.setTextColor(colors.red)
  568.                         write("File saved as:")
  569.                         term.setTextColor(colors.white)
  570.                         write("        |")
  571.                         term.setCursorPos(9,9)
  572.                         write("|")
  573.                         term.setCursorPos(10,9)
  574.                         term.setTextColor(colors.yellow)
  575.                         write(" "..sResponse.." ")
  576.                         term.setTextColor(colors.white)
  577.                         write("|")
  578.                         term.setCursorPos(9,10)
  579.                         write("|                              |")
  580.                         term.setCursorPos(9,11)
  581.                         write("|            ")
  582.                         term.setTextColor(colors.blue)
  583.                         write("[ OK ]")
  584.                         term.setTextColor(colors.white)
  585.                         write("            |")
  586.                         term.setCursorPos(9,12)
  587.                         write("'------------------------------'")
  588.                         local fMenu = confirm(fMenu)
  589.                         break
  590.                     else
  591.                         failConnect()
  592.                         break
  593.                     end
  594.                 else
  595.                     term.clear()
  596.                     frame()
  597.                     term.setTextColor(colors.white)
  598.                     term.setCursorPos(9,7)
  599.                     write(".------------------------------.")
  600.                     term.setCursorPos(9,8)
  601.                     write("|       ")
  602.                     term.setTextColor(colors.red)
  603.                     write("Invalid filename")
  604.                     term.setTextColor(colors.white)
  605.                     write("       |")
  606.                     term.setCursorPos(9,9)
  607.                     write("|     ")
  608.                     term.setTextColor(colors.yellow)
  609.                     write("Please re-enter name")
  610.                     term.setTextColor(colors.white)
  611.                     write("     |")
  612.                     term.setCursorPos(9,10)
  613.                     write("|                              |")
  614.                     term.setCursorPos(9,11)
  615.                     write("|            ")
  616.                     term.setTextColor(colors.blue)
  617.                     write("[ OK ]")
  618.                     term.setTextColor(colors.white)
  619.                     write("            |")
  620.                     term.setCursorPos(9,12)
  621.                     write("'------------------------------'")
  622.                     local fMenu = confirm(fMenu)
  623.                 end
  624.             end
  625.         else
  626.             local fMenu = "Exit SmartPaste"
  627.         end
  628.     elseif fMenu == "Delete post" and logged == false then --delete if not logged in
  629.         term.clear()
  630.         frame()
  631.         term.setTextColor(colors.white)
  632.         term.setCursorPos(9,7)
  633.         write(".------------------------------.")
  634.         term.setCursorPos(9,8)
  635.         write("|    ")
  636.         term.setTextColor(colors.red)
  637.         write("You must be logged in")
  638.         term.setTextColor(colors.white)
  639.         write("     |")
  640.         term.setCursorPos(9,9)
  641.         write("|     ")
  642.         term.setTextColor(colors.red)
  643.         write("To use that feature")
  644.         term.setTextColor(colors.white)
  645.         write("      |")
  646.         term.setCursorPos(9,10)
  647.         write("|                              |")
  648.         term.setCursorPos(9,11)
  649.         write("|            ")
  650.         term.setTextColor(colors.blue)
  651.         write("[ OK ]")
  652.         term.setTextColor(colors.white)
  653.         write("            |")
  654.         term.setCursorPos(9,12)
  655.         write("'------------------------------'")
  656.         local fMenu = confirm(fMenu)
  657.     elseif fMenu == "Delete post" and logged == true then --delete if logged in
  658.         local response = http.post(
  659.             "http://pastebin.com/api/api_post.php",
  660.             "api_dev_key="..key.."&"..
  661.             "api_user_key="..userKey.."&"..
  662.             "api_option=list"
  663.             )
  664.         if response then
  665.             term.clear()
  666.             frame()
  667.             term.setTextColor(colors.blue)
  668.             term.setCursorPos(9,7)
  669.             write(".------------------------------.")
  670.             term.setCursorPos(9,8)
  671.             write("|    ")
  672.             term.setTextColor(colors.red)
  673.             write("Retreiving posts from")
  674.             term.setTextColor(colors.blue)
  675.             write("     |")
  676.             term.setCursorPos(9,9)
  677.             write("|         ")
  678.             term.setTextColor(colors.red)
  679.             write("Pastebin.com")
  680.             term.setTextColor(colors.blue)
  681.             write("         |")
  682.             term.setCursorPos(9,10)
  683.             write("'------------------------------'")
  684.             sleep(1)
  685.             local codeTable = {}
  686.             local titleTable = {}
  687.             local sResponse = response.readAll()
  688.             response.close()
  689.             for code in string.gmatch(sResponse, "<paste_key>(%w-)</paste_key>") do
  690.                 table.insert(codeTable, code)
  691.             end
  692.             for title in string.gmatch(sResponse, "<paste_title>(.-)</paste_title>") do
  693.                 table.insert(titleTable, title)
  694.             end
  695.             term.clear()
  696.             frame()
  697.             local delOpt = newmenu(titleTable, 3, 5, 13)
  698.             for i = 1, # codeTable do
  699.                 if delOpt == titleTable[i] then
  700.                     postCode = codeTable[i]
  701.                 end
  702.             end
  703.             local response = http.post(
  704.                 "http://pastebin.com/api/api_post.php",
  705.                 "api_dev_key="..key.."&"..
  706.                 "api_user_key="..userKey.."&"..
  707.                 "api_option=delete&"..
  708.                 "api_paste_key="..postCode
  709.                 )
  710.             if response then
  711.                 local sResponse = response.readAll()
  712.                 if sResponse == "Paste Removed" then
  713.                     term.clear()
  714.                     frame()
  715.                     term.setTextColor(colors.white)
  716.                     term.setCursorPos(9,7)
  717.                     write(".------------------------------.")
  718.                     term.setCursorPos(9,8)
  719.                     write("|  ")
  720.                     term.setTextColor(colors.red)
  721.                     write("Paste deleted successfully")
  722.                     term.setTextColor(colors.white)
  723.                     write("  |")
  724.                     term.setCursorPos(9,9)
  725.                     write("|            ")
  726.                     term.setTextColor(colors.blue)
  727.                     write("[ OK ]")
  728.                     term.setTextColor(colors.white)
  729.                     write("            |")
  730.                     term.setCursorPos(9,10)
  731.                     write("'------------------------------'")
  732.                     local fMenu = confirm(fMenu)
  733.                 end
  734.             else
  735.                 failConnect()
  736.             end
  737.         else
  738.             failConnect()
  739.         end
  740.     elseif fMenu == "Retrieve post" then --download paste
  741.         if logged == false then
  742.             term.clear()
  743.             frame()
  744.             term.setTextColor(colors.white)
  745.             term.setCursorPos(9,7)
  746.             write(".------------------------------.")
  747.             term.setCursorPos(9,8)
  748.             write("|        ")
  749.             term.setTextColor(colors.red)
  750.             write("Pastebin code:")
  751.             term.setTextColor(colors.white)
  752.             write("        |")
  753.             term.setCursorPos(9,9)
  754.             write("|   ")
  755.             term.setTextColor(colors.yellow)
  756.             write(">")
  757.             term.setTextColor(colors.white)
  758.             write("                          |")
  759.             term.setCursorPos(9,10)
  760.             write("'------------------------------'")
  761.             term.setCursorPos(15,9)
  762.             local postCode = limitRead(22)
  763.             if postCode ~= nil and string.len(postCode) == 8 then
  764.                 local response = http.get("http://pastebin.com/raw.php?i="..postCode)
  765.                 if response then
  766.                     local sResponse = response.readAll()
  767.                     response.close()
  768.                     term.clear()
  769.                     frame()
  770.                     term.setCursorPos(9,7)
  771.                     term.setTextColor(colors.white)
  772.                     write(".------------------------------.")
  773.                     term.setCursorPos(9,8)
  774.                     write("|        ")
  775.                     term.setTextColor(colors.red)
  776.                     write("New  filename:")
  777.                     term.setTextColor(colors.white)
  778.                     write("        |")
  779.                     term.setCursorPos(9,9)
  780.                     write("|   ")
  781.                     term.setTextColor(colors.yellow)
  782.                     write(">")
  783.                     term.setTextColor(colors.white)
  784.                     write("                          |")
  785.                     term.setCursorPos(9,10)
  786.                     write("'------------------------------'")
  787.                     term.setCursorPos(15,9)
  788.                     while true do
  789.                         local fileName = limitRead(22)
  790.                         if fileName ~= "" then
  791.                             local file = fs.open(fileName, "w")
  792.                             file.write(sResponse)          
  793.                             file.close()
  794.                             break
  795.                         else
  796.                             term.clear()
  797.                             frame()
  798.                             term.setCursorPos(9,7)
  799.                             term.setTextColor(colors.white)
  800.                             write(".------------------------------.")
  801.                             term.setCursorPos(9,8)
  802.                             write("|       ")
  803.                             term.setTextColor(colors.red)
  804.                             write("Invalid filename")
  805.                             term.setTextColor(colors.white)
  806.                             write("       |")
  807.                             term.setCursorPos(9,9)
  808.                             write("|     ")
  809.                             term.setTextColor(colors.red)
  810.                             write("Please re-enter name")
  811.                             term.setTextColor(colors.white)
  812.                             write("     |")
  813.                             term.setCursorPos(9,10)
  814.                             write("|            ")
  815.                             term.setTextColor(colors.blue)
  816.                             write("[ OK ]")
  817.                             term.setTextColor(colors.white)
  818.                             write("            |")
  819.                             term.setCursorPos(9,11)
  820.                             write("'------------------------------'")
  821.                             local fMenu = confirm(fMenu)
  822.                         end
  823.                     end
  824.                 else
  825.                     failConnect()
  826.                 end
  827.             else
  828.                 term.clear()
  829.                 frame()
  830.                 term.setCursorPos(9,7)
  831.                 term.setTextColor(colors.white)
  832.                 write(".------------------------------.")
  833.                 term.setCursorPos(9,8)
  834.                 write("|         ")
  835.                 term.setTextColor(colors.red)
  836.                 write("Invalid code")
  837.                 term.setTextColor(colors.white)
  838.                 write("         |")
  839.                 term.setCursorPos(9,9)
  840.                 write("|     ")
  841.                 term.setTextColor(colors.red)
  842.                 write("Please re-enter code")
  843.                 term.setTextColor(colors.white)
  844.                 write("     |")
  845.                 term.setCursorPos(9,10)
  846.                 write("|                              |")
  847.                 term.setCursorPos(9,11)
  848.                 write("|            ")
  849.                 term.setTextColor(colors.blue)
  850.                 write("[ OK ]")
  851.                 term.setTextColor(colors.white)
  852.                 write("            |")
  853.                 term.setCursorPos(9,12)
  854.                 write("'------------------------------'")
  855.                 local fMenu = confirm(fMenu)
  856.             end
  857.         elseif logged == true then
  858.             local response = http.post(
  859.                 "http://pastebin.com/api/api_post.php",
  860.                 "api_dev_key="..key.."&"..
  861.                 "api_user_key="..userKey.."&"..
  862.                 "api_option=list"
  863.                 )
  864.             if response then
  865.                 local sResponse = response.readAll()
  866.                 response.close()
  867.                 local codeTable = {}
  868.                 local titleTable = {}
  869.                 for code in string.gmatch(sResponse, "<paste_key>(%w-)</paste_key>") do
  870.                     table.insert(codeTable, code)
  871.                 end
  872.                 for title in string.gmatch(sResponse, "<paste_title>(.-)</paste_title>") do
  873.                     table.insert(titleTable, title)
  874.                 end
  875.                 term.clear()
  876.                 frame()
  877.                 local downOpt = newmenu(titleTable, 3, 5, 13)
  878.                 if downOpt ~= "Exit SmartPaste" then
  879.                     for i = 1, # codeTable do
  880.                         if downOpt == titleTable[i] then
  881.                             postCode = codeTable[i]
  882.                         end
  883.                     end
  884.                     local response = http.get("http://pastebin.com/raw.php?i="..postCode)
  885.                     if response then
  886.                         local sResponse = response.readAll()
  887.                         term.clear()
  888.                         frame()
  889.                         term.setCursorPos(9,7)
  890.                         term.setTextColor(colors.white)
  891.                         write(".------------------------------.")
  892.                         term.setCursorPos(9,8)
  893.                         write("|        ")
  894.                         term.setTextColor(colors.red)
  895.                         write("New  filename:")
  896.                         term.setTextColor(colors.white)
  897.                         write("        |")
  898.                         term.setCursorPos(9,9)
  899.                         write("|   ")
  900.                         term.setTextColor(colors.yellow)
  901.                         write(">")
  902.                         term.setTextColor(colors.white)
  903.                         write("                          |")
  904.                         term.setCursorPos(9,10)
  905.                         write("'------------------------------'")
  906.                         term.setCursorPos(15,9)
  907.                         while true do
  908.                             local fileName = limitRead(22)
  909.                             if fileName ~= "" then
  910.                                 local file = fs.open(fileName, "w")
  911.                                 file.write(sResponse)          
  912.                                 file.close()
  913.                                 break
  914.                             else
  915.                                 term.clear()
  916.                                 frame()
  917.                                 term.setCursorPos(9,7)
  918.                                 term.setTextColor(colors.white)
  919.                                 write(".------------------------------.")
  920.                                 term.setCursorPos(9,8)
  921.                                 write("|       ")
  922.                                 term.setTextColor(colors.red)
  923.                                 write("Invalid filename")
  924.                                 term.setTextColor(colors.white)
  925.                                 write("       |")
  926.                                 term.setCursorPos(9,9)
  927.                                 write("|     ")
  928.                                 term.setTextColor(colors.red)
  929.                                 write("Please re-enter name")
  930.                                 term.setTextColor(colors.white)
  931.                                 write("     |")
  932.                                 term.setCursorPos(9,10)
  933.                                 write("|            ")
  934.                                 term.setTextColor(colors.blue)
  935.                                 write("[ OK ]")
  936.                                 term.setTextColor(colors.white)
  937.                                 write("            |")
  938.                                 term.setCursorPos(9,11)
  939.                                 write("'------------------------------'")
  940.                                 local fMenu = confirm(fMenu)
  941.                             end
  942.                         end
  943.                     else
  944.                         fMenu = downOpt
  945.                     end
  946.                 else
  947.                     failConnect()
  948.                 end
  949.             else
  950.                 failConnect()
  951.             end
  952.         end
  953.     end
  954.     if fMenu == "Exit SmartPaste" then --exit program
  955.         sleep(.25)
  956.         term.setBackgroundColor(colors.black)
  957.         term.setTextColor(colors.yellow)
  958.         term.clear()
  959.         term.setCursorPos(1,1)
  960.         break
  961.     end
  962. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement