Advertisement
shadowkat1010

netnode.d

Sep 26th, 2014
223
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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. local serial = require "serialization"
  11.  
  12. modem = component.modem
  13. local modemPower = 400
  14. local msgBuffer = {}
  15. nodeAddr = "derp3"
  16. local magicNumbers = {}
  17.  
  18. local function extractPath(path)
  19.  if path:find("!") ~= nil then
  20.   return path:sub(1,path:find("!")-1), path:sub(path:find("!")+1,path:len())
  21.  else
  22.   return path, path
  23.  end
  24. end
  25.  
  26. function recvMsg(name,user,message)
  27.  local f = io.open("/home/"..user.."/mail/"..name,"ab")
  28.  if f ~= nil then
  29.   f:write(message)
  30.   f:close()
  31.  end
  32. end
  33.  
  34. local function sks_send(path,retPath,name,user,message)
  35.  local toAddr, contPath = extractPath(path)
  36.  --print(toAddr,contPath)
  37.  modem.broadcast(42,toAddr,contPath,retPath,name,user,message,math.random(0,65535))
  38. end
  39.  
  40. local function store(path,retPath,name,user,message)
  41.  table.insert(msgBuffer,{path,retPath,name,user,message})
  42. end
  43.  
  44. function msgListener(_,_,from,port,_,toAddr,path,retPath,name,user,message,magNum)
  45.  local hBR = false
  46.  for i = 1, #magicNumbers do
  47.   if magicNumbers[i] == magNum then
  48.    hBR = true
  49.   end
  50.  end
  51.  if not hBR then
  52.   if port == 42 and path ~= nodeAddr and toAddr == nodeAddr then
  53.    store(path,retPath,name,user,message)
  54.    --print "routed"
  55.   elseif port == 42 and path == nodeAddr and toAddr == nodeAddr then
  56.    --print "Mail recieved!"
  57.    recvMsg(name,user,message)
  58.   end
  59.  end
  60. end
  61.  
  62. local function sendTimer()
  63.  --print "Send timer activated"
  64.  if #msgBuffer > 0 then
  65.   for i = 1, #msgBuffer do
  66.    sks_send(msgBuffer[i][1],msgBuffer[i][2],msgBuffer[i][3],msgBuffer[i][4],msgBuffer[i][5])
  67.   end
  68.   msgBuffer = {}
  69.   --print "Messages sent!"
  70.  else
  71.   --print "No messages sent!"
  72.  end
  73. end
  74.  
  75. local function loadConfig()
  76.  local f = io.open("/etc/net.cfg")
  77.  local configTab = serial.unserialize(f:read("*a"))
  78.  f:close()
  79.  nodeAddr = configTab.nodeName
  80.  modemPower = configTab.modemPower
  81. end
  82.  
  83. local function setup()
  84.  modem.open(42)
  85.  --loadConfig()
  86.  --pcall(modem.setStrength(modemPower))
  87.  print("netnode.d v1.0 running; nodename: " .. nodeAddr)
  88.  event.listen("modem_message",msgListener)
  89.  event.timer(1,sendTimer,math.huge)
  90.  sendTimer()
  91. end
  92.  
  93. setup()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement