Advertisement
An_random_user

CCFTP Client

Sep 16th, 2022 (edited)
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.54 KB | None | 0 0
  1. -- CCFTP Client
  2. local recv_port = 891
  3. local send_port = 890
  4. local modem = peripheral.find("modem")
  5. -- Characters
  6. local b = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
  7.  
  8. -- Encoding
  9. function b64encode(data)
  10.     return ((data:gsub('.', function(x)
  11.         local r,b='',x:byte()
  12.         for i=8,1,-1 do r=r..(b%2^i-b%2^(i-1)>0 and '1' or '0') end
  13.         return r;
  14.     end)..'0000'):gsub('%d%d%d?%d?%d?%d?', function(x)
  15.         if (#x < 6) then return '' end
  16.         local c=0
  17.         for i=1,6 do c=c+(x:sub(i,i)=='1' and 2^(6-i) or 0) end
  18.         return b:sub(c+1,c+1)
  19.     end)..({ '', '==', '=' })[#data%3+1])
  20. end
  21.  
  22. -- Decoding
  23. function b64decode(data)
  24.     data = string.gsub(data, '[^'..b..'=]', '')
  25.     return (data:gsub('.', function(x)
  26.         if (x == '=') then return '' end
  27.         local r,f='',(b:find(x)-1)
  28.         for i=6,1,-1 do r=r..(f%2^i-f%2^(i-1)>0 and '1' or '0') end
  29.         return r;
  30.     end):gsub('%d%d%d?%d?%d?%d?%d?%d?', function(x)
  31.         if (#x ~= 8) then return '' end
  32.         local c=0
  33.         for i=1,8 do c=c+(x:sub(i,i)=='1' and 2^(8-i) or 0) end
  34.         return string.char(c)
  35.     end))
  36. end
  37. modem.open(recv_port)
  38.  
  39. while true do
  40. term.setCursorPos(1,1)
  41. term.clear()
  42. term.write("filename: ")
  43. local fname = read()
  44. if fname == "file1" then
  45. modem.transmit(send_port,send_port,"file1")
  46.  local event, modemSide, senderChannel, replyChannel, message, senderDistance = os.pullEvent("modem_message")
  47.  local h = fs.open("file.dwnload", "w")
  48.  h.write(b64decode(message))
  49.  h.close()
  50. end end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement