Advertisement
savior909

cloud (not done yet)

Feb 3rd, 2019
245
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.15 KB | None | 0 0
  1. --[[
  2.      SaviOS logging pastebin system.
  3.                                      ]]--                        
  4.  
  5. w, h = term.getSize()
  6.  
  7. clear = term.clear
  8.  
  9. function term.clear()
  10.  for i=1, h-1 do
  11.   term.setCursorPos(1, i)
  12.   term.clearLine()
  13.  end
  14. end
  15.  
  16. function saveUserData(user, api_dev_key)
  17.  file = fs.open("disk/.cloudData", "a")
  18.   file.write(user.." "..api_dev_key)
  19.  file.close()
  20. end
  21.  
  22. function loadUserData(user)
  23.  file = fs.open("disk/.cloudData", "r")
  24.   sLine = file.readLine()
  25.   tLines = {}
  26.    while sLine do
  27.     table.insert(tLines, sLine)
  28.     sLine = file.readLine()
  29.    end
  30.    file.close()
  31.   for k,v in pairs(tLines) do
  32.    if string.find(v, user) ~= nil then
  33.     tWords = {}
  34.      for m in string.gmatch(v, "[^ \t]+") do
  35.       table.insert(tWords, m)
  36.       return tWords[2]
  37.      end
  38.    end
  39.   end
  40.  
  41. end
  42.  
  43. function setStatus(text)
  44.  term.setCursorPos(1, h)
  45.  term.clearLine()
  46.  term.write(text)
  47. end
  48.  
  49. function download(code, filename)
  50.  setStatus("Downloading from: pastebin.com/raw/"..code)
  51.  data = http.get("http://www.pastebin.com/raw"..textutils.urlEncode(code)).readAll()
  52.  
  53.  if data == nil then
  54.    data = "Error: Downloading wasn't succesfull!"
  55.  end
  56.  if fs.exists(filename) then
  57.   setStatus("Error: File exists!")
  58.   return false
  59.  else
  60.   file = fs.open(filename, "w")
  61.    file.write(data)
  62.   file.close()
  63.    if data == "Error: Downloading wasn't succesfull!" then
  64.      setStatus(data)
  65.     return false
  66.    else
  67.      setStatus("Successfully saved to "..filename)
  68.     return true
  69.    end
  70.  end
  71. end
  72.  
  73.  
  74. function login(user, password, dev_key)
  75.  api_user_key = http.post(
  76.         "http://www.pastebin.com/api/api_login.php",
  77.         "api_dev_key="..dev_key.."&"..
  78.         "api_user_name="..textutils.urlEncode(user).."&"..
  79.         "api_user_password="..textutils.urlEncode(password)
  80.         ).readAll()
  81.  return api_user_key
  82. end
  83.  
  84. function upload(title, textData, expiration)
  85.  response = http.post(
  86.       "http://www.pastebin.com/api/api_post.php",
  87.       "api_option=paste&"..
  88.       "api_user_key="..api_user_key.."&"..
  89.       "api_paste_name="..textutils.urlEncode(title).."&"..
  90.       "api_paste_format=lua&"..
  91.       "api_paste_expire_date="..expiration.."&"..
  92.       "api_paste_code="..textutils.urlEncode(textData).."&"..
  93.       "api_dev_key="..dev_key
  94.       ).readAll()
  95.  
  96.  ran = string.gmatch(response, "[^/]+$")
  97.  out = ran()
  98.  
  99.  return out
  100. end
  101.  
  102. function delete(paste_key)
  103.  setStatus("Removing paste: "..paste_key.."!")
  104.  progress = http.post(
  105.             "http://www.pastebin.com/api/api_post.php",
  106.             "api_option=delete&"..
  107.             "api_dev_key="..dev_key.."&"..
  108.             "api_user_key="..api_user_key.."&"..
  109.             "api_paste_key="..paste_key
  110.             ).readAll()
  111.  return progress
  112. end
  113.  
  114. function list(dev_key, user_key )
  115.  
  116.  files = http.get(
  117.          "https://pastebin.com/api/api_post.php",
  118.          "api_option=list&"..
  119.          "api_user_key="..user_key..
  120.          "api_dev_key="..dev_key).readAll()
  121.  
  122.  file = fs.open(".tmp", "w")
  123.   file.write(files)
  124.  file.close()
  125.  
  126.  
  127. end
  128.  
  129. function reloadMenu(n)
  130.  menu = {}
  131.  for i=1, #fullMenu do
  132.   table.insert(menu, fullMenu[n[i]])
  133.  end
  134. end
  135.  
  136. fullMenu = {"Login", "Register", "Logout", "Upload", "Download", "Edit"}
  137.  
  138. menu = {"Login", "Register"}
  139.  
  140. menuFunc = {
  141.  ["Edit"] = function()
  142.   term.clear()
  143.   setStatus("Filename you want to edit")
  144.     term.setCursorPos(2, 2)
  145.     term.write("Filename: ")
  146.     fName = read()
  147.    
  148.     shell.run("edit", fName)
  149.    
  150.  end,
  151.  ["Download"] = function()
  152.   term.clear()
  153.   setStatus("Filename, where'll be your code saved")
  154.     term.setCursorPos(2, 2)
  155.     term.write("Filename: ")
  156.     fName = read()
  157.   setStatus("A title of code, you want to download")
  158.     term.setCursorPos(2, 4)
  159.     term.write("Title: ")
  160.     title = read()
  161.  
  162.     code = loadUserData(title)
  163.  
  164.     success = download(code, fName)
  165.    
  166.  end,
  167.  ["Register"] = function()
  168.   term.clear()
  169.   setStatus("Your pastebin username")
  170.     term.setCursorPos(2, 2)  
  171.     term.write("User: ")
  172.     user = read()  
  173.   setStatus("You can find it on: www.pastebin.com/api")
  174.     term.setCursorPos(2, 4)
  175.     term.write("Type your 'api_dev_key': ")
  176.     api_dev_key = read()
  177.   setStatus("Password for your pastebin account")
  178.     term.setCursorPos(2, 6)
  179.     term.write("Password: ")      
  180.     pass = read("*")
  181.  
  182.    check = login(user, pass, api_dev_key)
  183.  
  184.    if check:sub(1, 7) ~= "Bad API" then
  185.    
  186.     setStatus("Data verified, your api_dev_key was saved")
  187.    else
  188.     setStatus("Check your data, something is wrong")
  189.    end
  190.  
  191.  end,
  192.  ["Upload"] = function()
  193.   term.clear()
  194.   setStatus("")
  195.   term.setCursorPos(2, 2)
  196.   term.write("Paste Title: ")
  197.    title = read()
  198.   term.setCursorPos(2, 4)
  199.   term.write("File to Upload: ")
  200.    filename = read()
  201.   term.write("Expiration: ")
  202.    expiration = read()
  203.  
  204.    file = fs.open(filename, "r")
  205.     data = file.readAll()
  206.    file.close()
  207.  
  208.   setStatus("Uploading..")
  209.  
  210.   code = upload(title, data, expiration)
  211.  
  212.   file = fs.open("disk/codes", "r")
  213.    sLine = file.readLine()
  214.    tLines = {}
  215.  
  216.   while sLine do
  217.    table.insert(tLines, sLine)
  218.    sLine = file.readLine()
  219.   end
  220.    file.close()
  221.  
  222.   file = fs.open("disk/codes", "w")
  223.    file.writeLine(title.." "..code)
  224.    for k,v in pairs(tLines) do
  225.     file.writeLine(v)
  226.    end
  227.   file.close()
  228.  
  229.   setStatus("Uploaded as: "..code.." Code was wrote to 'code file'!")    
  230.  
  231.  end,
  232.  
  233.  ["Logout"] = function()
  234.   n = {1}
  235.   api_user_key = nil
  236.   reloadMenu(n)
  237.  end,
  238.  
  239.  ["Login"] = function()
  240.   term.clear()
  241.   setStatus("")
  242.    term.setCursorPos(2, 2)
  243.    term.write("User: ")
  244.     user = read()
  245.    term.setCursorPos(2, 4)
  246.    term.write("Password: ")
  247.     pass = read("*")
  248.    
  249.    api_dev_key = loadUserData(user)
  250.    if not api_dev_key then
  251.     setStatus("This username is not registered, please register")
  252.    else
  253.    api_user_key = login(user, pass, api_dev_key)
  254.    
  255.     if api_user_key:sub(1, 7) == "Bad API" then
  256.      setStatus("Error: "..api_user_key:sub(17, #api_user_key))
  257.      term.clear()
  258.      n = {1}
  259.      reloadMenu(n)
  260.     else
  261.      setStatus("Welcome "..user.."!")
  262.      term.clear()
  263.      n = {3,4,5,6}
  264.      reloadMenu(n)
  265.     end    
  266.   end
  267. end,
  268.  
  269. }
  270.  
  271.  
  272.  
  273.  
  274.  
  275.  
  276.  
  277. term.clear = clear
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement