Guest User

tv

a guest
Sep 25th, 2015
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.71 KB | None | 0 0
  1.  
  2. function lsh(value,shift)
  3.     return (value*(2^shift)) % 256
  4. end
  5. function rsh(value,shift)
  6.     return math.floor(value/2^shift) % 256
  7. end
  8. function bit(x,b)
  9.     return (x % 2^b - x % 2^(b-1) > 0)
  10. end
  11. function lor(x,y)
  12.     result = 0
  13.     for p=1,8 do result = result + (((bit(x,p) or bit(y,p)) == true) and 2^(p-1) or 0) end
  14.     return result
  15. end
  16.  
  17. -- Encoding
  18. local base64chars = {[0]='A',[1]='B',[2]='C',[3]='D',[4]='E',[5]='F',[6]='G',[7]='H',[8]='I',[9]='J',[10]='K',[11]='L',[12]='M',[13]='N',[14]='O',[15]='P',[16]='Q',[17]='R',[18]='S',[19]='T',[20]='U',[21]='V',[22]='W',[23]='X',[24]='Y',[25]='Z',[26]='a',[27]='b',[28]='c',[29]='d',[30]='e',[31]='f',[32]='g',[33]='h',[34]='i',[35]='j',[36]='k',[37]='l',[38]='m',[39]='n',[40]='o',[41]='p',[42]='q',[43]='r',[44]='s',[45]='t',[46]='u',[47]='v',[48]='w',[49]='x',[50]='y',[51]='z',[52]='0',[53]='1',[54]='2',[55]='3',[56]='4',[57]='5',[58]='6',[59]='7',[60]='8',[61]='9',[62]='-',[63]='_'}
  19. function encode(data)
  20.     local bytes = {}
  21.     local result = ""
  22.     for spos=0,string.len(data)-1,3 do
  23.         for byte=1,3 do bytes[byte] = string.byte(string.sub(data,(spos+byte))) or 0 end
  24.         result = string.format('%s%s%s%s%s',result,base64chars[rsh(bytes[1],2)],base64chars[lor(lsh((bytes[1] % 4),4), rsh(bytes[2],4))] or "=",((#data-spos) > 1) and base64chars[lor(lsh(bytes[2] % 16,2), rsh(bytes[3],6))] or "=",((#data-spos) > 2) and base64chars[(bytes[3] % 64)] or "=")
  25.     end
  26.     return result
  27. end
  28.  
  29. -- Decoding
  30. local base64bytes = {['A']=0,['B']=1,['C']=2,['D']=3,['E']=4,['F']=5,['G']=6,['H']=7,['I']=8,['J']=9,['K']=10,['L']=11,['M']=12,['N']=13,['O']=14,['P']=15,['Q']=16,['R']=17,['S']=18,['T']=19,['U']=20,['V']=21,['W']=22,['X']=23,['Y']=24,['Z']=25,['a']=26,['b']=27,['c']=28,['d']=29,['e']=30,['f']=31,['g']=32,['h']=33,['i']=34,['j']=35,['k']=36,['l']=37,['m']=38,['n']=39,['o']=40,['p']=41,['q']=42,['r']=43,['s']=44,['t']=45,['u']=46,['v']=47,['w']=48,['x']=49,['y']=50,['z']=51,['0']=52,['1']=53,['2']=54,['3']=55,['4']=56,['5']=57,['6']=58,['7']=59,['8']=60,['9']=61,['-']=62,['_']=63,['=']=nil}
  31. function decode(data)
  32.     local chars = {}
  33.     local result=""
  34.     for dpos=0,string.len(data)-1,4 do
  35.         for char=1,4 do chars[char] = base64bytes[(string.sub(data,(dpos+char),(dpos+char)) or "=")] end
  36.         result = string.format('%s%s%s%s',result,string.char(lor(lsh(chars[1],2), rsh(chars[2],4))),(chars[3] ~= nil) and string.char(lor(lsh(chars[2],4), rsh(chars[3],2))) or "",(chars[4] ~= nil) and string.char(lor(lsh(chars[3],6) % 192, (chars[4]))) or "")
  37.     end
  38.     return result
  39. end
  40.  
  41. done = 0
  42. done2 = 0
  43. print("TV v0.1 beta")
  44.  
  45. local args = {...}
  46. if #args < 1 then
  47.   print("Usage: tv <channel>")
  48.   return
  49. end
  50.  
  51.  
  52.  
  53. local channel = tonumber(args[1])
  54. oldID = os.getComputerID()
  55. function os.getComputerID()
  56.   return channel
  57. end
  58. if not channel or channel > 65535 or channel < 0 then
  59.   printError("Invalid channel")
  60.   return
  61. end
  62.  
  63. local modem = peripheral.find("modem")
  64. modem.open(5555)
  65. modem.transmit(5555,888,"request_subscrip_"..tostring(channel))
  66. sleep(0.5)
  67. repeat
  68.   e = {os.pullEvent()}
  69.   if e[1] == "modem_message" and e[3] == 5555 and e[5] == tostring(channel).."_FTA" then
  70.     done = 1
  71.   elseif e[1] == "modem_message" and e[3] == 5555 and e[5] == tostring(channel).."_PFP" then
  72.     print("PFP Channel")
  73.     if fs.exists("/disk") then
  74.       print("Directory 'disk' found.")
  75.       if disk.isPresent("bottom") then
  76.         print("Disk found.")
  77.         if fs.exists("/disk/.subscription_data") then
  78.           print("Subscription number found.")
  79.           f = fs.open("/disk/.subscription_data","r")
  80.           a = f.readAll()
  81.           f.close()
  82.           f = nil
  83.           print("Sending verification message...")
  84.           modem.transmit(5555,tonumber(channel),"verify_sub_"..a)
  85.           sleep(0.5)
  86.           repeat
  87.             e2 = {os.pullEvent()}
  88.             if e2[1] == "modem_message" and e2[3] == 5555 and e2[5] == "verify_yes_"..tostring(channel) then
  89.               done2 = 1
  90.               print("Verified.")
  91.             elseif e2[1] == "modem_message" and e2[3] == 5555 and e2[5] == "verify_no_"..tostring(channel) then
  92.               term.clear()
  93.               term.setCursorPos(1,1)
  94.               print("You need an FRB Subscription for this channel!")
  95.             end
  96.           until done2 == 1
  97.         end
  98.       end
  99.     end
  100.   end
  101. until done == 1
  102.  
  103. while true do
  104.   local ev = {os.pullEventRaw()}
  105.   if ev[1] == "terminate" then
  106.     term.clear()
  107.     term.setCursorPos(1, 1)
  108.     print("Connection closed.")
  109.     function os.getComputerID()
  110.       return oldID
  111.     end
  112.     return
  113.   elseif ev[1] == "modem_message" and ev[3] == channel and type(ev[5]) == "table" then
  114.     msg = decode(ev[5])
  115.     if term.current()[ msg[1] ] then
  116.     pcall(term.current()[ msg[1] ], unpack(msg, 2))
  117.     end
  118.   end
  119. end
Advertisement
Add Comment
Please, Sign In to add comment