Advertisement
LegoStax

Rednet Snooper

Oct 1st, 2015
297
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.60 KB | None | 0 0
  1. -- Rednet Snooper
  2. local tArgs = {...}
  3. local RUNNING = true
  4. local w,h = term.getSize()
  5. local scrollpos = 1
  6. local data = {}
  7. local blink = true
  8. local minchan, maxchan = nil
  9. if not term.isColor() or not term.isColour() then
  10.     RUNNING = false
  11.     print("Get an advanced computer. It likely will not be this way in the future.")
  12. end
  13. if #tArgs < 2 then
  14.     print(fs.getName(shell.getRunningProgram()).." <minchan> <maxchan>")
  15.     RUNNING = false
  16. else
  17.     minchan = tonumber(tArgs[1])
  18.     maxchan = tonumber(tArgs[2])
  19.     if maxchan - minchan > 127 then
  20.         print("Only 128 channels can be open at once")
  21.         RUNNING = false
  22.     end
  23. end
  24. local m = nil
  25. local pospossible = 1
  26. local input = ""
  27. local function drawTopBar()
  28.     term.setBackgroundColor(colors.gray)
  29.     term.setCursorPos(1,1)
  30.     term.clearLine()
  31.     term.write(" Rednet Snooper "..minchan.."-"..maxchan)
  32.     term.setCursorPos(w-4,1)
  33.     term.write(" ? ")
  34.     term.setBackgroundColor(colors.red)
  35.     term.setCursorPos(w,1)
  36.     term.write("X")
  37. end
  38. local function drawBottomBar()
  39.     term.setBackgroundColor(colors.gray)
  40.     term.setCursorPos(1,h)
  41.     term.clearLine()
  42.     term.write("> ")
  43. end
  44. local function drawListen()
  45.     term.setBackgroundColor(colors.black)
  46.     for y = 2,h-1 do
  47.         term.setCursorPos(1,y)
  48.         term.clearLine()
  49.     end
  50.     term.setCursorPos(1,2)
  51.     for i = scrollpos,#data do
  52.         if i > #data then break end
  53.         term.write(data[i])
  54.         local x,y = term.getCursorPos()
  55.         y = y+1
  56.         if y > h-1 then break end
  57.         term.setCursorPos(1,y)
  58.     end
  59.     term.setBackgroundColor(colors.gray)
  60.     term.setCursorPos(3,h)
  61. end
  62. local function drawScreen()
  63.     term.clear()
  64.     drawTopBar()
  65.     drawBottomBar()
  66.     drawListen()
  67. end
  68. local function scanPeripherals()
  69.     local sides = {"top", "bottom", "left", "right", "front", "back"}
  70.     for i = 1,#sides do
  71.         if peripheral.isPresent(sides[i]) then
  72.             if peripheral.getType(sides[i]) == "modem" then
  73.                 m = peripheral.wrap(sides[i])
  74.                 m.closeAll()
  75.             end
  76.         end
  77.     end
  78.     if m == nil then
  79.         print("Please attach a modem")
  80.         RUNNING = false
  81.     end
  82. end
  83. local function openRange()
  84.     if RUNNING then
  85.         for i = minchan,maxchan do
  86.             if not m.isOpen(i) then m.open(i) end
  87.         end
  88.     end
  89. end
  90. local function processInput()
  91.     local chan = nil
  92.     local msg = nil
  93.     for i = 1,input:len() do
  94.         if string.sub(input,i,i) == " " then
  95.             chan = tonumber(string.sub(input,1,i-1))
  96.             msg = string.sub(input,i+1)
  97.             break
  98.         end
  99.     end
  100.     if chan then
  101.         local nMessageID = math.random( 1, 2147483647 ) -- transmit with rednet protocol
  102.         local tMessage = {
  103.             nMessageID = nMessageID,
  104.             nRecipient = chan,
  105.             message = msg,
  106.             sProtocol = nil,
  107.         }
  108.         local temp = nil
  109.         if not m.isOpen(chan) then
  110.             m.close(maxchan)
  111.             m.open(chan)
  112.             m.transmit(chan,maxchan,msg)
  113.             m.transmit(chan,maxchan,tMessage)
  114.             temp = "C"..chan.."/"..maxchan.." D:0: "
  115.         else
  116.             m.transmit(chan,chan,msg)
  117.             m.transmit(chan,chan,tMessage)
  118.             temp = "C"..chan.."/"..chan.." D:0: "
  119.         end
  120.         -- insert sent message to data table
  121.         if msg:len() + temp:len() > w then
  122.             table.insert(data, temp .. string.sub(msg,1,w - temp:len()))
  123.             for i = w-temp:len()+1,msg:len(),w do
  124.                 table.insert(data, string.sub(msg,i,i+w))
  125.             end
  126.         else
  127.             table.insert(data, temp .. msg)
  128.         end
  129.         pospossible = #data-(h-3)
  130.         if pospossible < 1 then pospossible = 1 end
  131.         scrollpos = pospossible
  132.         drawListen()
  133.         if not m.isOpen(maxchan) then
  134.             m.close(chan)
  135.             m.open(maxchan)
  136.         end
  137.     end
  138. end
  139. local function helpMenu()
  140.     term.setBackgroundColor(colors.lightGray)
  141.     term.setCursorPos(w-4,1)
  142.     term.write(" ? ")
  143.     term.setBackgroundColor(colors.black)
  144.     for y = 2,h-1 do
  145.         for x = 1,w do
  146.             term.setCursorPos(x,y)
  147.             term.write(" ")
  148.         end
  149.     end
  150.     term.setCursorPos(1,3)
  151.     print("Help Menu:")
  152.     print()
  153.     print("Message data:")
  154.     print("C<channel>/<replychannel> D:<distance>: ")
  155.     print()
  156.     print("To send a message:")
  157.     print("<channel> <message>")
  158.     print("and press enter")
  159.     while true do
  160.         local e = {os.pullEvent()}
  161.         if e[1] == "mouse_click" and e[2] == 1 and e[3] == w and e[4] == 1 then
  162.             RUNNING = false
  163.             break
  164.         elseif e[1] == "mouse_click" and e[2] == 1 and e[3] >= w-4 and e[3] <= w-2 and e[4] == 1 then
  165.             break
  166.         elseif e[1] == "modem_message" then -- Receiving messages
  167.             local temp = "C"..e[3].."/"..e[4].." D:"..e[6]..": "
  168.             if type(e[5]) == "table" then
  169.                 if e[5].message:len() + temp:len() > w then -- text wrapping
  170.                     table.insert(data, temp .. string.sub(e[5].message,1,w - temp:len()))
  171.                     for i = w-temp:len()+1,e[5].message:len(),w do
  172.                         table.insert(data, string.sub(e[5].message,i,i+w))
  173.                     end
  174.                 else
  175.                     table.insert(data, temp .. e[5].message)
  176.                 end
  177.             else
  178.                 if e[5]:len() + temp:len() > w then -- text wrapping
  179.                     table.insert(data, temp .. string.sub(e[5],1,w - temp:len()))
  180.                     for i = w-temp:len()+1,e[5]:len(),w do
  181.                         table.insert(data, string.sub(e[5],i,i+w))
  182.                     end
  183.                 else
  184.                     table.insert(data, temp .. e[5])
  185.                 end
  186.             end
  187.             pospossible = #data-(h-3)
  188.             if pospossible < 1 then pospossible = 1 end
  189.             scrollpos = pospossible
  190.         end
  191.     end
  192. end
  193. local function evtListen()
  194.     openRange()
  195.     drawScreen()
  196.     term.setBackgroundColor(colors.gray)
  197.     term.setCursorPos(3,h)
  198.     local xread = 3
  199.     local cursor = os.startTimer(0.3)
  200.     while RUNNING do
  201.         local e = {os.pullEvent()}
  202.         if e[1] == "modem_message" then -- Receiving messages
  203.             local temp = "C"..e[3].."/"..e[4].." D:"..e[6]..": "
  204.             if type(e[5]) == "table" then
  205.                 if e[5].message:len() + temp:len() > w then -- text wrapping
  206.                     table.insert(data, temp .. string.sub(e[5].message,1,w - temp:len()))
  207.                     for i = w-temp:len()+1,e[5].message:len(),w do
  208.                         table.insert(data, string.sub(e[5].message,i,i+w))
  209.                     end
  210.                 else
  211.                     table.insert(data, temp .. e[5].message)
  212.                 end
  213.             else
  214.                 if e[5]:len() + temp:len() > w then -- text wrapping
  215.                     table.insert(data, temp .. string.sub(e[5],1,w - temp:len()))
  216.                     for i = w-temp:len()+1,e[5]:len(),w do
  217.                         table.insert(data, string.sub(e[5],i,i+w))
  218.                     end
  219.                 else
  220.                     table.insert(data, temp .. e[5])
  221.                 end
  222.             end
  223.             pospossible = #data-(h-3)
  224.             if pospossible < 1 then pospossible = 1 end
  225.             scrollpos = pospossible
  226.             drawListen()
  227.         elseif e[1] == "mouse_scroll" then -- Mouse Input
  228.             if e[2] == 1 and scrollpos < pospossible then
  229.                 scrollpos = scrollpos+1
  230.                 drawListen()
  231.             elseif e[2] == -1 and scrollpos > 1 then
  232.                 scrollpos = scrollpos-1
  233.                 drawListen()
  234.             end
  235.         elseif e[1] == "mouse_click" and e[2] == 1 and e[3] == w and e[4] == 1 then
  236.             RUNNING = false
  237.         elseif e[1] == "mouse_click" and e[2] == 1 and e[3] >= w-4 and e[3] <= w-2 and e[4] == 1 then
  238.             helpMenu()
  239.             drawScreen()
  240.             cursor = os.startTimer(0.3)
  241.         elseif e[1] == "key" then -- custom read() function
  242.             if e[2] == keys.enter then
  243.                 processInput()
  244.                 input = ""
  245.                 xread = 3
  246.                 drawBottomBar()
  247.             elseif e[2] == keys.backspace and input ~= "" then
  248.                 input = string.sub(input,1,input:len()-1)
  249.                 xread = xread-1
  250.                 term.setBackgroundColor(colors.gray)
  251.                 term.setCursorPos(xread,h)
  252.                 term.write("  ")
  253.                 term.setCursorPos(xread,h)
  254.             end
  255.         elseif e[1] == "char" then
  256.             input = input .. e[2]
  257.             term.setBackgroundColor(colors.gray)
  258.             term.setCursorPos(xread,h)
  259.             term.write(e[2])
  260.             xread = xread+1
  261.         elseif e[1] == "timer" and e[2] == cursor then
  262.             if blink then term.setBackgroundColor(colors.lightGray)
  263.             else term.setBackgroundColor(colors.gray) end
  264.             term.setCursorPos(xread,h)
  265.             term.write(" ")
  266.             blink = not blink
  267.             cursor = os.startTimer(0.3)
  268.         end
  269.     end
  270. end
  271. scanPeripherals()
  272. if RUNNING then
  273.     parallel.waitForAll(evtListen, userInput)
  274.     term.setBackgroundColor(colors.black)
  275.     term.clear()
  276.     term.setCursorPos(1,1)
  277.     m.closeAll()
  278. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement