Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- "plisk" by LDDestroier
- -- thanks for the AES, SquidDev
- -- pastebin get G9fsXzGt plisk
- local tArg = {...}
- local port, id = 257, os.getComputerID()
- local absolutePath = false
- assert(type(id)=="number", "why is your ID a "..type(id).."...?")
- assert(id>=0, "IDs can't be below zero, plebian!")
- assert(id<=65535, "IDs can't be above 65535, fool!")
- local modem = {}
- local waitForModem = function(time)
- local tID, evt, rID = os.startTimer(time or 1)
- while true do
- evt, rID = os.pullEventRaw()
- if evt == "timer" then
- if rID == tID then
- return false
- end
- elseif evt == "terminate" then
- return false
- elseif evt == "peripheral" then
- if peripheral.getType(rID) == "modem" then
- return true
- end
- end
- end
- end
- local getModem = function(timeout) --returns a working modem, prioritizes use of wireless modem
- if modem.open then return modem end
- local modems = {peripheral.find("modem")}
- if #modems == 0 then
- if waitForModem(timeout or 3) then
- modems = {peripheral.find("modem")}
- else
- error("No modem found...")
- end
- end
- for a = 1, #modems do
- if modems[a].isWireless() then
- modems[a].open(port)
- modems[a].open(id)
- return modems[a]
- end
- end
- modems[1].open(port)
- modems[1].open(id)
- return modems[1]
- end
- modem = getModem()
- local readFile = function(path)
- if fs.exists(path) and (not fs.isDir(path)) then
- local file = fs.open(path,"r")
- return file.readAll(), file.close()
- end
- end
- local writeFile = function(path,contents)
- local file = fs.open(path,"w")
- return file.write(contents), file.close()
- end
- local initAES = function()
- local apipath = "aes"
- if (not aes) and (not fs.exists(apipath)) then
- print("AES API not found! Downloading...")
- local prog = http.get("https://gist.githubusercontent.com/SquidDev/86925e07cbabd70773e53d781bd8b2fe/raw/ccea5f652cc33a979de02d6e0fe193db0c5bdfb1/aeslua.min.lua")
- if not prog then error("FAIL!") end
- writeFile(apipath, prog.readAll())
- end
- if not aes then
- local res = os.loadAPI(apipath)
- if not res then error("Couldn't load AES API!") end
- end
- end
- initAES()
- plisk = {
- send = function(rID, key, content)
- local output = {
- id = id,
- rID = rID,
- content = content
- }
- output = aes.encrypt(key, textutils.serialize(output))
- modem = getModem()
- modem.transmit(port,id,output)
- return true, output
- end,
- receive = function(rID, key)
- assert(type(key) == "string", "key must be string!")
- local evt, msg
- while true do
- evt = {os.pullEventRaw()}
- if evt[1] == "modem_message" then
- if type(evt[5]) == "string" then
- msg = textutils.unserialize(aes.decrypt(key,evt[5]))
- if type(msg) == "table" then
- if (not rID) or (msg.id == rID) then
- if (not msg.id) or (msg.id == id) then
- break
- end
- end
- end
- end
- if msg.content then
- return msg.id, msg.content
- end
- elseif evt[1] == "terminate" then
- return false, evt[1]
- end
- end
- end
- }
- local function listAll(_path, _files, noredundant)
- local path = _path or ""
- local files = _files or {}
- if #path > 1 then table.insert(files, path) end
- for _, file in ipairs(fs.list(path)) do
- local path = fs.combine(path, file)
- if (file ~= thisProgram) then
- local guud = true
- if guud then
- if fs.isDir(path) then
- listAll(path, files, noredundant)
- else
- table.insert(files, path)
- end
- end
- end
- end
- if noredundant then
- for a = 1, #files do
- if fs.isDir(tostring(files[a])) then
- if #fs.list(tostring(files[a])) ~= 0 then
- table.remove(files,a)
- end
- end
- end
- end
- return files
- end
- if shell then
- local command = tArg[1]
- local filename = tArg[2] and fs.combine(absolutePath and "" or shell.dir(), tArg[2])
- local key = tArg[3] or "scissors61"
- local rID = tonumber(tArg[4])
- if rID then
- if (rID < 0) or (rID > 65535) then
- error("recipient ID must be betwixt 0-65535")
- end
- if rID ~= math.floor(rID) then
- error("recipient ID can't be a decimal, fool")
- end
- end
- local displayHelp = function()
- local name = fs.getName(shell.getRunningProgram() or "plisk")
- print(name.." get <path> [key] [ID filter]")
- print(name.." send <file> [key] [ID filter]")
- end
- local goodPrint = function(text)
- local ct = term.getTextColor()
- if term.isColor() then
- term.setTextColor(colors.lime)
- else
- term.setTextColor(colors.lightGray)
- end
- local res = {print(text)}
- term.setTextColor(ct)
- return table.unpack(res)
- end
- if not filename then
- return displayHelp()
- end
- if command == "send" then
- if not fs.exists(filename) then
- error("no such file exists")
- else
- local content = {}
- if fs.isDir(filename) then
- local fList = listAll(filename)
- for k,v in pairs(fList) do
- if v:find("/") then
- content[v:sub(v:find("/")+1)] = readFile(v)
- end
- end
- else
- content = readFile(filename)
- end
- write("Transmitting...")
- local result = plisk.send(rID, key, content)
- if result then
- goodPrint("gucci!")
- else
- printError("failed!?")
- end
- end
- elseif command == "get" then
- if fs.isReadOnly(filename) then
- error("file path is read-only.")
- else
- if fs.exists(filename) then
- if fs.isDir(filename) then
- print("Overwrite folder? (Y/N)")
- else
- print("Overwrite? (Y/N)")
- end
- local evt
- repeat
- evt = {os.pullEventRaw()}
- until ((evt[1] == "char") and string.find("yn",evt[2])) or evt[1] == "terminate"
- if (evt[2] == "n") or (evt[1] == "terminate") then
- error("Oh, uh, alright.")
- end
- end
- write("Waiting for file") if rID then write(" from "..tostring(rID)) end write("...")
- local recipID, content = plisk.receive(rID, key)
- if content then
- if fs.exists(filename) then fs.delete(filename) end
- if type(content) == "string" then
- writeFile(filename, content)
- elseif type(content) == "table" then
- for name,cont in pairs(content) do
- writeFile(fs.combine(filename, name), cont)
- end
- else
- printError("failed...")
- end
- if recipID then
- if rID then
- goodPrint("gucci!")
- else
- goodPrint("gucci from "..tostring(recipID).."!")
- end
- else
- printError("failed...")
- end
- else
- printError("failed...")
- end
- end
- end
- end
Add Comment
Please, Sign In to add comment