Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- grand unified installer
- local function install(file, data)
- local handle = fs.open(file, "w")
- handle.write(data)
- handle.close()
- end
- local radioAPI = [[
- math.randomseed(os.time())
- local currentFrequency = 0
- local chars = {
- "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"," ","_",
- "#","@","!","*","^","1","2","3","4","5","6","7","8","9","0","+","-","~","`","%","$","{","}","\\","/",">","<",";",":",
- }
- function run()
- while true do
- local id, msg, distance = rednet.receive()
- msg = textutils.unserialize(msg)
- if type(msg) == "table" then
- local freq = msg[1]
- local data = msg[2]
- local offset = math.abs(currentFrequency-freq)
- --print("Got a message from "..id..", frequency "..freq.." data "..data.." freqOffset "..offset.." our freq is "..currentFrequency)
- if offset < 10 then
- local doScramble = true
- if distance <= 100 then
- if offset == 1 then
- if math.random(1,10) < 5 then
- doScramble = false
- end
- elseif offset == 2 then
- if math.random(1,100) < 25 then
- doScramble = false
- end
- end
- end
- local scrambleAmt = 0
- if doScramble then
- scrambleAmt = math.ceil(#data * (offset/10))
- end
- if distance > 100 then
- scrambleAmt = scrambleAmt + math.ceil(#data * (distance-100 / 100))
- end
- if scrambleAmt < #data then
- --print("Scrambling "..scrambleAmt.." characters")
- local scrambledPositions = {}
- for i=1, #data do
- scrambledPositions[i] = string.sub(data,i, i)
- end
- if doScramble then
- for i=1, scrambleAmt do
- local scramblePos = math.random(1, #data)
- if scrambledPositions[scramblePos] == string.sub(data, scramblePos, scramblePos) then
- if math.random(1,2) == 2 then
- scrambledPositions[scramblePos] = string.upper(chars[math.random(1, #chars)])
- else
- scrambledPositions[scramblePos] = chars[math.random(1, #chars)]
- end
- end
- end
- end
- local newData = ""
- for i=1, #scrambledPositions do
- newData = newData..scrambledPositions[i]
- end
- --print("Received radio transmission from "..id.." data: "..newData)
- os.queueEvent("radio_received", id, newData, distance, freq)
- end
- end
- end
- end
- end
- function receive(timeout)
- local timer = -1
- if timeout then
- timer = os.startTimer(timeout)
- end
- while true do
- local event, p1, p2, p3, p4 = os.pullEvent()
- if event == "timer" and p1 == timer then
- return
- elseif event == "radio_received" then
- return p1, p2, p3, p4
- end
- end
- end
- function transmit(message)
- rednet.broadcast(textutils.serialize({currentFrequency, message}))
- end
- function setFrequency(newFreq)
- currentFrequency = newFreq
- end
- function getFrequency()
- return currentFrequency
- end
- ]]
- local startup = [[
- if rednetRadio == nil then
- -- do first time init
- os.loadAPI("rednetRadio")
- parallel.waitForAll(rednetRadio.run, function() shell.run("shell") end)
- end
- ]]
- local reciever = [[
- -- listener/decrypter
- local args = {...}
- local decryptedRecv = {}
- local recv = {}
- if #args ~= 2 then
- print("USAGE: "..fs.getName(shell.getRunningProgram()).." [key] [frequency]")
- return
- end
- local function printCentered(input)
- local maxX = term.getSize()
- print(string.rep(" ", math.floor((maxX - string.len(input))/2))..input)
- end
- local function drawStream(key)
- local maxX, maxY = term.getSize()
- local itrFrom = 1
- if #recv > maxY then
- itrFrom = #recv-(maxY-1)
- end
- term.clear()
- term.setCursorPos(1,1)
- for i=itrFrom, #recv do
- local decrypted = ""
- for i2=1, #recv[i] do
- decrypted = decrypted..string.char(bit.bxor(string.byte(string.sub(recv[i],i2,i2)), key))
- end
- decryptedRecv[i] = decrypted
- if decrypted ~= "\n" then
- printCentered(decrypted)
- end
- end
- end
- rednetRadio.setFrequency(tonumber(args[2]))
- while true do
- local event, id, msg, distance, freq = os.pullEvent()
- if event == "radio_received" then
- drawStream(tonumber(args[1]))
- if id then
- table.insert(recv, msg)
- local handle = fs.open(args[2], "a")
- handle.write(decryptedRecv[#decryptedRecv])
- handle.close()
- end
- local maxX, maxY = term.getSize()
- term.setCursorPos(1,maxY)
- if id then
- term.setTextColor(colors.lime)
- write("Currently recieving from "..id)
- else
- term.setTextColor(colors.red)
- write("No stations active.")
- end
- term.setTextColor(colors.white)
- term.setCursorPos(maxX-20, maxY)
- write("Press SPACE to quit.")
- elseif event == "key" and id == 57 then
- term.clear()
- term.setCursorPos(1,1)
- return
- end
- end
- ]]
- local transmitter = [[
- os.loadAPI("rednetRadio")
- local args = {...}
- -- args[1]: file to transmit
- -- args[2]: Frequency
- -- args[3]: Transmit rate (wait this many seconds after each message)
- -- args[4]: Key
- if #args ~= 4 then
- print("USAGE: "..fs.getName(shell.getRunningProgram()).." [file] [frequency] [rate] [key]")
- return
- end
- rednetRadio.setFrequency(tonumber(args[2]))
- local function transmitEncrypted(data)
- local encrypted = ""
- for i=1, #data do
- encrypted = encrypted..string.char(bit.bxor(string.byte(string.sub(data,i,i)), tonumber(args[4])))
- end
- rednetRadio.transmit(encrypted)
- end
- if fs.exists(args[1]) then
- local handle = io.open(args[1], "r")
- local lines = {}
- for line in handle:lines() do
- table.insert(lines, line)
- end
- handle:close()
- local transNo = 1
- while true do
- transmitEncrypted("BEGIN TRANSMISSION #"..transNo)
- transmitEncrypted("\n")
- os.sleep(tonumber(args[3]))
- for i,v in ipairs(lines) do
- for block in string.gmatch(v, "[%S\n]+") do
- transmitEncrypted(block.." ")
- os.sleep(tonumber(args[3]))
- end
- transmitEncrypted("\n")
- end
- transmitEncrypted("END TRANSMISSION #"..transNo)
- transmitEncrypted("\n")
- os.sleep(5)
- transNo = transNo+1
- end
- else
- print("USAGE: "..fs.getName(shell.getRunningProgram()).." [file] [frequency] [rate] [key]")
- end
- ]]
- textutils.slowPrint("Radio Based Communications Suite Installer")
- textutils.slowPrint("(C) 2012 KillaVanilla Industries")
- textutils.slowPrint("Select an option:")
- textutils.slowPrint("1. Install Prerequisites")
- textutils.slowPrint("2. Install Transmitter Software")
- textutils.slowPrint("3. Install Reciever Software")
- textutils.slowWrite("selection> ")
- local selection = 0
- while true do
- local select = tonumber(read())
- if select then
- if select > 0 and select < 4 then
- selection = select
- break
- end
- end
- end
- if select == 1 then
- install("rednetRadio", radioAPI)
- if fs.exists("startup") then
- fs.move("startup", "startup2")
- end
- install("startup", startup)
- elseif select == 2 then
- install("transmitter", transmitter)
- else
- install("reciever", reciever)
- end
- textutils.slowPrint("Software installed.")
- textutils.slowPrint("This terminal will now reboot.")
- os.sleep(1.5)
- os.reboot()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement