Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local channel = "channel"
- local videoDir = "/tv"
- --Base64 api by LBPHacker
- local lookup_V2C = {}
- for ixChar = 65, 90 do lookup_V2C[ixChar - 65] = string.char(ixChar) end
- for ixChar = 97, 122 do lookup_V2C[ixChar - 71] = string.char(ixChar) end
- for ixChar = 48, 57 do lookup_V2C[ixChar + 4] = string.char(ixChar) end
- lookup_V2C[62] = "+"
- lookup_V2C[63] = "/"
- local lookup_C2V = {}
- for key, value in pairs(lookup_V2C) do lookup_C2V[value] = key end
- function encode(data)
- local result = ""
- for ix = 1, #data, 3 do
- local all24 = data:sub(ix, ix):byte() * 65536 + (data:sub(ix + 1, ix + 1):byte() or 0) * 256 + (data:sub(ix + 2, ix + 2):byte() or 0)
- result = result .. lookup_V2C[bit.band(all24, 16515072) / 262144] .. lookup_V2C[bit.band(all24, 258048) / 4096] .. lookup_V2C[bit.band(all24, 4032) / 64] .. lookup_V2C[bit.band(all24, 63)]
- end
- local padding = (3 - data:len() % 3) % 3
- return result:sub(1, result:len() - padding) .. string.rep("=", padding)
- end
- function decode(data)
- local result = ""
- for ix = 1, #data, 4 do
- local all24 = lookup_C2V[data:sub(ix, ix)] * 262144 + (lookup_C2V[data:sub(ix + 1, ix + 1)] or 0) * 4096 + (lookup_C2V[data:sub(ix + 2, ix + 2)] or 0) * 64 + (lookup_C2V[data:sub(ix + 3, ix + 3)] or 0)
- result = result .. string.char(bit.band(all24, 16711680) / 65536) .. string.char(bit.band(all24, 65280) / 256) .. string.char(bit.band(all24, 255))
- end
- return result:sub(1, result:len() - #data:match("^.-(=*)$"))
- end
- -- BNTP api below
- local b = peripheral.find("bitnet_tower") or peripheral.find("bitnet_antenna")
- local function ebn()
- if not b then
- error("No bitnet found!", 0)
- end
- end
- function sendMessage(message, useBase)
- useBase = useBase or false
- if type(useBase) ~= "boolean" then
- useBase = false
- end
- ebn()
- if type(message) == "table" and useBase == true then
- message = "t.bntp::"..encode(textutils.serialize(message))
- elseif type(message) ~= "table" and useBase == true then
- message = "bntp::"..encode(message)
- end
- b.transmit(message)
- end
- function receive()
- ebn()
- local e,s,m,d = os.pullEvent("bitnet_message")
- if m == nil then m = "nil" end
- if m:sub(1,6) == "bntp::" then
- m = decode(m:sub(7))
- elseif m:sub(1,8) == "t.bntp::" then
- m = textutils.unserialize( decode(m:sub(9)) )
- end
- return m,d
- end
- -- BNTP api ends
- ebn()
- local function fetch(program)
- v = program
- if fs.exists(videoDir.."/"..v) and not fs.isDir(videoDir.."/"..v) then
- local file = fs.open(videoDir.."/"..v, "r")
- local thing = textutils.unserialize(file.readAll())
- file.close()
- return thing
- end
- return nil
- end
- local function fetchList()
- local ls = {}
- local b = {}
- for k,v in pairs(fs.list(videoDir)) do
- if fs.exists(videoDir.."/"..v) and not fs.isDir(videoDir.."/"..v) then
- local thing = fetch(v)
- b = {thing[name], v}
- table.insert(b)
- end
- end
- return ls
- end
- while true do
- local mes, d = receive()
- if type(mes) ~= "table" then mes = {} end
- if mes.sType == "dtv.ping" then
- sendMessage({sType="dtv.pong", uid=mes.uid, channel=channel}, false)
- elseif mes.sType == "dtv.list" then
- sendMessage({sType="dtv.listPong", uid=mes.uid, channel=channel, data=fetchList()}, false)
- elseif mes.sType == "dtv.dl" then
- sendMessage({sType="dtv.listPong", uid=mes.uid, channel=channel, data=fetch(mes.program)}, false)
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement