Advertisement
shadowkat1010

Ticking time bomb

Mar 24th, 2014
428
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.05 KB | None | 0 0
  1. --Unnamed message forwarding
  2. --This is WTFPL, just change the name, OK?
  3. --Version 1.0 - this means it's the most broken yet
  4. --I commented out all they annoying text outputs. You people love me.
  5.  
  6. local component = require "component"
  7. local event = require "event"
  8. local text = require "text"
  9. local shell = require "shell"
  10.  
  11. local modem = component.modem
  12. tape = component.tape_drive
  13.  
  14. local modemPower = 100
  15. local nodeAddr = "derp" -- Change this. This is what other nodes will address you as
  16. local storedMessages = 0
  17.  
  18.  
  19.  
  20. local function tape_rezero()
  21.  tape.seek(0-tape.getSize())
  22. end
  23.  
  24. local function extractPath(path)
  25.  return path:sub(1,path:find("!")-1), path:sub(path:find("!")+1,path:len())
  26. end
  27.  
  28. local function idCheck(addr) -- I don't even use this... I should probably remove it
  29.  local n = 0
  30.  repeat
  31.   n = n + 1
  32.   --print(tConnections[n])
  33.  until tConnections[n] == addr or tConnections[n] == nil
  34.  if tConnections[n] == addr then
  35.   --print(n..":"..addr)
  36.   return true
  37.  end
  38. end
  39.  
  40. function recvMsg(name,user,message) --This is what happens when you get a message for a user on your system
  41.  
  42. end
  43.  
  44. local function sks_send(path,retPath,name,user,message)
  45.  local toAddr, contPath = extractPath(path)
  46.  modem.broadcast(42,toAddr,contPath,retPath,name,user,message)
  47. end
  48.  
  49. local function store(path,retPath,name,user,message)
  50.  tape.write(path:len())
  51.  tape.write(path)
  52.  --print(path)
  53.  tape.write(retPath:len())
  54.  tape.write(retPath)
  55.  --print(retPath)
  56.  tape.write(name:len())
  57.  tape.write(name)
  58.  --print(name)
  59.  tape.write(user:len())
  60.  tape.write(user)
  61.  --print(user)
  62.  tape.write(message:len())
  63.  tape.write(message)
  64.  --print(message)
  65.  storedMessages = storedMessages + 1
  66.  --print "Message stored! Will be sent soon." --This function really hurt to look at...
  67. end
  68.  
  69. function msgListener(_,_,from,port,_,toAddr,path,retPath,name,user,message)
  70.  print(from)
  71.  if port == 42 and path ~= nodeAddr and toAddr == nodeAddr then
  72.   store(path,retPath,name,user,message)
  73.  elseif port == 42 and path == nodeAddr and toAddr == nodeAddr then
  74.   recvMsg(name,user,message)
  75.  end
  76. end
  77.  
  78. local function sendTimer()
  79.  tape_rezero()
  80.  --print "Send timer activated"
  81.  if storedMessages > 0 then
  82.   repeat
  83.    local path = tape.read(tape.read())
  84.    local retPath = tape.read(tape.read())
  85.    local name = tape.read(tape.read())
  86.    local user = tape.read(tape.read())
  87.    local message = tape.read(tape.read())
  88.    sks_send(path,retPath,name,user,message)
  89.    storedMessages = storedMessages - 1
  90.   until storedMessages == 0
  91.   --print "Messages sent!"
  92.  else
  93.   --print "No messages sent!"
  94.  end
  95. end
  96.  
  97. local function setup()
  98.  modem.open(42)
  99.  modem.setStrength(100)
  100.  event.listen("modem_message",msgListener)
  101.  event.timer(30,sendTimer,math.huge) --Changing the 30 will change the delay between buffer cleaning, short time was for testing,
  102.  sendTimer() -- You'd get away with once every few days on a low traffic node, but on a big node, you'd have to do it every few
  103. end -- seconds or you'd lose messages. I doubt one will ever get to that point though. Prove me wrong!
  104.  
  105. setup()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement