Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --Unnamed message forwarding
- --This is WTFPL, just change the name, OK?
- --Version 1.0 - this means it's the most broken yet
- --I commented out all they annoying text outputs. You people love me.
- local component = require "component"
- local event = require "event"
- local text = require "text"
- local shell = require "shell"
- local serial = require "serialization"
- modem = component.modem
- local modemPower = 400
- local msgBuffer = {}
- nodeAddr = "derp3"
- local magicNumbers = {}
- local function extractPath(path)
- if path:find("!") ~= nil then
- return path:sub(1,path:find("!")-1), path:sub(path:find("!")+1,path:len())
- else
- return path, path
- end
- end
- function recvMsg(name,user,message)
- local f = io.open("/home/"..user.."/mail/"..name,"ab")
- if f ~= nil then
- f:write(message)
- f:close()
- end
- end
- local function sks_send(path,retPath,name,user,message)
- local toAddr, contPath = extractPath(path)
- --print(toAddr,contPath)
- modem.broadcast(42,toAddr,contPath,retPath,name,user,message,math.random(0,65535))
- end
- local function store(path,retPath,name,user,message)
- table.insert(msgBuffer,{path,retPath,name,user,message})
- end
- function msgListener(_,_,from,port,_,toAddr,path,retPath,name,user,message,magNum)
- local hBR = false
- for i = 1, #magicNumbers do
- if magicNumbers[i] == magNum then
- hBR = true
- end
- end
- if not hBR then
- if port == 42 and path ~= nodeAddr and toAddr == nodeAddr then
- store(path,retPath,name,user,message)
- --print "routed"
- elseif port == 42 and path == nodeAddr and toAddr == nodeAddr then
- --print "Mail recieved!"
- recvMsg(name,user,message)
- end
- end
- end
- local function sendTimer()
- --print "Send timer activated"
- if #msgBuffer > 0 then
- for i = 1, #msgBuffer do
- sks_send(msgBuffer[i][1],msgBuffer[i][2],msgBuffer[i][3],msgBuffer[i][4],msgBuffer[i][5])
- end
- msgBuffer = {}
- --print "Messages sent!"
- else
- --print "No messages sent!"
- end
- end
- local function loadConfig()
- local f = io.open("/etc/net.cfg")
- local configTab = serial.unserialize(f:read("*a"))
- f:close()
- nodeAddr = configTab.nodeName
- modemPower = configTab.modemPower
- end
- local function setup()
- modem.open(42)
- --loadConfig()
- --pcall(modem.setStrength(modemPower))
- print("netnode.d v1.0 running; nodename: " .. nodeAddr)
- event.listen("modem_message",msgListener)
- event.timer(1,sendTimer,math.huge)
- sendTimer()
- end
- setup()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement