Advertisement
turtle5204

DeltaTV Server

May 30th, 2015
282
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.35 KB | None | 0 0
  1. local channel = "channel"
  2. local videoDir = "/tv"
  3.  
  4. --Base64 api by LBPHacker
  5. local lookup_V2C = {}
  6. for ixChar = 65, 90 do lookup_V2C[ixChar - 65] = string.char(ixChar) end
  7. for ixChar = 97, 122 do lookup_V2C[ixChar - 71] = string.char(ixChar) end
  8. for ixChar = 48, 57 do lookup_V2C[ixChar + 4] = string.char(ixChar) end
  9. lookup_V2C[62] = "+"
  10. lookup_V2C[63] = "/"
  11. local lookup_C2V = {}
  12. for key, value in pairs(lookup_V2C) do lookup_C2V[value] = key end
  13.  
  14. function encode(data)
  15.     local result = ""
  16.     for ix = 1, #data, 3 do
  17.         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)
  18.         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)]
  19.     end
  20.     local padding = (3 - data:len() % 3) % 3
  21.     return result:sub(1, result:len() - padding) .. string.rep("=", padding)
  22. end
  23.  
  24. function decode(data)
  25.     local result = ""
  26.     for ix = 1, #data, 4 do
  27.         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)
  28.         result = result .. string.char(bit.band(all24, 16711680) / 65536) .. string.char(bit.band(all24, 65280) / 256) .. string.char(bit.band(all24, 255))
  29.     end
  30.     return result:sub(1, result:len() - #data:match("^.-(=*)$"))
  31. end
  32.  
  33.  
  34.  
  35.  
  36. -- BNTP api below
  37.  
  38. local b = peripheral.find("bitnet_tower") or peripheral.find("bitnet_antenna")
  39.  
  40. local function ebn()
  41.  if not b then
  42.   error("No bitnet found!", 0)
  43.  end
  44. end
  45.  
  46. function sendMessage(message, useBase)
  47.  useBase = useBase or false
  48.  if type(useBase) ~= "boolean" then
  49.   useBase = false
  50.  end
  51.  
  52.  ebn()
  53.  if type(message) == "table" and useBase == true then
  54.   message = "t.bntp::"..encode(textutils.serialize(message))
  55.  elseif type(message) ~= "table" and useBase == true then
  56.   message = "bntp::"..encode(message)
  57.  end
  58.  
  59.  b.transmit(message)
  60. end
  61.  
  62.  
  63. function receive()
  64.  ebn()
  65.  local e,s,m,d = os.pullEvent("bitnet_message")
  66.  if m == nil then m = "nil" end
  67.  if m:sub(1,6) == "bntp::" then
  68.   m = decode(m:sub(7))
  69.  elseif m:sub(1,8) == "t.bntp::" then
  70.   m = textutils.unserialize( decode(m:sub(9)) )
  71.  end
  72.  return m,d
  73. end
  74.  
  75. -- BNTP api ends
  76.  
  77. ebn()
  78.  
  79.  
  80. local function fetch(program)
  81.     v = program
  82.     if fs.exists(videoDir.."/"..v) and not fs.isDir(videoDir.."/"..v) then
  83.         local file = fs.open(videoDir.."/"..v, "r")
  84.         local thing = textutils.unserialize(file.readAll())
  85.         file.close()
  86.         return thing
  87.     end
  88.     return nil
  89. end
  90.  
  91.  
  92. local function fetchList()
  93.     local ls = {}
  94.     local b = {}
  95.     for k,v in pairs(fs.list(videoDir)) do
  96.         if fs.exists(videoDir.."/"..v) and not fs.isDir(videoDir.."/"..v) then
  97.             local thing = fetch(v)
  98.             b = {thing[name], v}
  99.             table.insert(b)
  100.         end
  101.     end
  102.     return ls
  103. end
  104.  
  105.  
  106.  
  107. while true do
  108.  
  109.     local mes, d = receive()
  110.  
  111.     if type(mes) ~= "table" then mes = {} end
  112.  
  113.     if mes.sType == "dtv.ping" then
  114.         sendMessage({sType="dtv.pong", uid=mes.uid, channel=channel}, false)
  115.     elseif mes.sType == "dtv.list" then
  116.         sendMessage({sType="dtv.listPong", uid=mes.uid, channel=channel, data=fetchList()}, false)
  117.     elseif mes.sType == "dtv.dl" then
  118.         sendMessage({sType="dtv.listPong", uid=mes.uid, channel=channel, data=fetch(mes.program)}, false)
  119.     end
  120.  
  121. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement