Advertisement
Tatantyler

Grand Unified Radio Apparatus Installer

Dec 8th, 2012
407
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.87 KB | None | 0 0
  1. -- grand unified installer
  2.  
  3. local function install(file, data)
  4.     local handle = fs.open(file, "w")
  5.     handle.write(data)
  6.     handle.close()
  7. end
  8.  
  9. local radioAPI = [[
  10. math.randomseed(os.time())
  11. local currentFrequency = 0
  12.  
  13. local chars = {
  14.     "a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"," ","_",
  15.     "#","@","!","*","^","1","2","3","4","5","6","7","8","9","0","+","-","~","`","%","$","{","}","\\","/",">","<",";",":",
  16. }
  17.  
  18.  
  19. function run()
  20.     while true do
  21.         local id, msg, distance = rednet.receive()
  22.         msg = textutils.unserialize(msg)
  23.         if type(msg) == "table" then
  24.             local freq = msg[1]
  25.             local data = msg[2]
  26.             local offset = math.abs(currentFrequency-freq)
  27.             --print("Got a message from "..id..", frequency "..freq.." data "..data.." freqOffset "..offset.." our freq is "..currentFrequency)
  28.             if offset < 10 then
  29.                 local doScramble = true
  30.                 if distance <= 100 then
  31.                     if offset == 1 then
  32.                         if math.random(1,10) < 5 then
  33.                             doScramble = false
  34.                         end
  35.                     elseif offset == 2 then
  36.                         if math.random(1,100) < 25 then
  37.                             doScramble = false
  38.                         end
  39.                     end
  40.                 end
  41.                 local scrambleAmt = 0
  42.                 if doScramble then
  43.                     scrambleAmt = math.ceil(#data * (offset/10))
  44.                 end
  45.                 if distance > 100 then
  46.                     scrambleAmt = scrambleAmt + math.ceil(#data * (distance-100 / 100))
  47.                 end
  48.                 if scrambleAmt < #data then
  49.                     --print("Scrambling "..scrambleAmt.." characters")
  50.                     local scrambledPositions = {}
  51.                     for i=1, #data do
  52.                         scrambledPositions[i] = string.sub(data,i, i)
  53.                     end
  54.                     if doScramble then
  55.                         for i=1, scrambleAmt do
  56.                             local scramblePos = math.random(1, #data)
  57.                             if scrambledPositions[scramblePos] == string.sub(data, scramblePos, scramblePos) then
  58.                                 if math.random(1,2) == 2 then
  59.                                     scrambledPositions[scramblePos] = string.upper(chars[math.random(1, #chars)])
  60.                                 else
  61.                                     scrambledPositions[scramblePos] = chars[math.random(1, #chars)]
  62.                                 end
  63.                             end
  64.                         end
  65.                     end
  66.                     local newData = ""
  67.                     for i=1, #scrambledPositions do
  68.                         newData = newData..scrambledPositions[i]
  69.                     end
  70.                     --print("Received radio transmission from "..id.." data: "..newData)
  71.                     os.queueEvent("radio_received", id, newData, distance, freq)
  72.                 end
  73.             end
  74.         end
  75.     end
  76. end
  77.  
  78. function receive(timeout)
  79.     local timer = -1
  80.     if timeout then
  81.         timer = os.startTimer(timeout)
  82.     end
  83.     while true do
  84.         local event, p1, p2, p3, p4 = os.pullEvent()
  85.         if event == "timer" and p1 == timer then
  86.             return
  87.         elseif event == "radio_received" then
  88.             return p1, p2, p3, p4
  89.         end
  90.     end
  91. end
  92.  
  93. function transmit(message)
  94.     rednet.broadcast(textutils.serialize({currentFrequency, message}))
  95. end
  96.  
  97. function setFrequency(newFreq)
  98.     currentFrequency = newFreq
  99. end
  100.  
  101. function getFrequency()
  102.     return currentFrequency
  103. end
  104. ]]
  105.  
  106. local startup = [[
  107.     if rednetRadio == nil then
  108.         -- do first time init
  109.         os.loadAPI("rednetRadio")
  110.         parallel.waitForAll(rednetRadio.run, function() shell.run("shell") end)
  111.     end
  112. ]]
  113.  
  114. local reciever = [[
  115. -- listener/decrypter
  116. local args = {...}
  117. local decryptedRecv = {}
  118. local recv = {}
  119.  
  120. if #args ~= 2 then
  121.     print("USAGE: "..fs.getName(shell.getRunningProgram()).." [key] [frequency]")
  122.     return
  123. end
  124.  
  125. local function printCentered(input)
  126.     local maxX = term.getSize()
  127.     print(string.rep(" ", math.floor((maxX - string.len(input))/2))..input)
  128. end
  129.  
  130. local function drawStream(key)
  131.     local maxX, maxY = term.getSize()
  132.     local itrFrom = 1
  133.     if #recv > maxY then
  134.         itrFrom = #recv-(maxY-1)
  135.     end
  136.     term.clear()
  137.     term.setCursorPos(1,1)
  138.     for i=itrFrom, #recv do
  139.         local decrypted = ""
  140.         for i2=1, #recv[i] do
  141.             decrypted = decrypted..string.char(bit.bxor(string.byte(string.sub(recv[i],i2,i2)), key))
  142.         end
  143.         decryptedRecv[i] = decrypted
  144.         if decrypted ~= "\n" then
  145.             printCentered(decrypted)
  146.         end
  147.     end
  148. end
  149.  
  150. rednetRadio.setFrequency(tonumber(args[2]))
  151.  
  152. while true do
  153.     local event, id, msg, distance, freq = os.pullEvent()
  154.     if event == "radio_received" then
  155.         drawStream(tonumber(args[1]))
  156.         if id then
  157.             table.insert(recv, msg)
  158.             local handle = fs.open(args[2], "a")
  159.             handle.write(decryptedRecv[#decryptedRecv])
  160.             handle.close()
  161.         end
  162.         local maxX, maxY = term.getSize()
  163.         term.setCursorPos(1,maxY)
  164.         if id then
  165.             term.setTextColor(colors.lime)
  166.             write("Currently recieving from "..id)
  167.         else
  168.             term.setTextColor(colors.red)
  169.             write("No stations active.")
  170.         end
  171.         term.setTextColor(colors.white)
  172.         term.setCursorPos(maxX-20, maxY)
  173.         write("Press SPACE to quit.")
  174.     elseif event == "key" and id == 57 then
  175.         term.clear()
  176.         term.setCursorPos(1,1)
  177.         return
  178.     end
  179. end
  180. ]]
  181.  
  182. local transmitter = [[
  183. os.loadAPI("rednetRadio")
  184. local args = {...}
  185. -- args[1]: file to transmit
  186. -- args[2]: Frequency
  187. -- args[3]: Transmit rate (wait this many seconds after each message)
  188. -- args[4]: Key
  189.  
  190. if #args ~= 4 then
  191.     print("USAGE: "..fs.getName(shell.getRunningProgram()).." [file] [frequency] [rate] [key]")
  192.     return
  193. end
  194.  
  195. rednetRadio.setFrequency(tonumber(args[2]))
  196.  
  197. local function transmitEncrypted(data)
  198.     local encrypted = ""
  199.     for i=1, #data do
  200.         encrypted = encrypted..string.char(bit.bxor(string.byte(string.sub(data,i,i)), tonumber(args[4])))
  201.     end
  202.     rednetRadio.transmit(encrypted)
  203. end
  204.  
  205. if fs.exists(args[1]) then
  206.     local handle = io.open(args[1], "r")
  207.     local lines = {}
  208.     for line in handle:lines() do
  209.         table.insert(lines, line)
  210.     end
  211.     handle:close()
  212.     local transNo = 1
  213.     while true do
  214.         transmitEncrypted("BEGIN TRANSMISSION #"..transNo)
  215.         transmitEncrypted("\n")
  216.         os.sleep(tonumber(args[3]))
  217.         for i,v in ipairs(lines) do
  218.             for block in string.gmatch(v, "[%S\n]+") do
  219.                 transmitEncrypted(block.." ")
  220.                 os.sleep(tonumber(args[3]))
  221.             end
  222.             transmitEncrypted("\n")
  223.         end
  224.         transmitEncrypted("END TRANSMISSION #"..transNo)
  225.         transmitEncrypted("\n")
  226.         os.sleep(5)
  227.         transNo = transNo+1
  228.     end
  229. else
  230.     print("USAGE: "..fs.getName(shell.getRunningProgram()).." [file] [frequency] [rate] [key]")
  231. end
  232. ]]
  233.  
  234. textutils.slowPrint("Radio Based Communications Suite Installer")
  235. textutils.slowPrint("(C) 2012 KillaVanilla Industries")
  236. textutils.slowPrint("Select an option:")
  237. textutils.slowPrint("1. Install Prerequisites")
  238. textutils.slowPrint("2. Install Transmitter Software")
  239. textutils.slowPrint("3. Install Reciever Software")
  240. textutils.slowWrite("selection> ")
  241. local selection = 0
  242. while true do
  243.     local select = tonumber(read())
  244.     if select then
  245.         if select > 0 and select < 4 then
  246.             selection = select
  247.             break
  248.         end
  249.     end
  250. end
  251. if select == 1 then
  252.     install("rednetRadio", radioAPI)
  253.     if fs.exists("startup") then
  254.         fs.move("startup", "startup2")
  255.     end
  256.     install("startup", startup)
  257. elseif select == 2 then
  258.     install("transmitter", transmitter)
  259. else
  260.     install("reciever", reciever)
  261. end
  262. textutils.slowPrint("Software installed.")
  263. textutils.slowPrint("This terminal will now reboot.")
  264. os.sleep(1.5)
  265. os.reboot()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement