Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local localShellThread = nil
- local mailDirectory = "/Mail"
- local screenWidth, screenHeight = term.getSize()
- function openModem()
- for index, side in pairs(rs.getSides()) do
- if peripheral.getType(side) == "modem" then
- rednet.open(side)
- return true
- end
- end
- return false
- end
- function createNewShell()
- return coroutine.create(function() shell.run("rom/programs/shell") end)
- end
- function getNumberOfTimesAppearedInMailDirectory(fileName)
- local timesAppeared = 0
- for index, file in ipairs(fs.list(mailDirectory)) do
- if file:find(fileName) then
- timesAppeared = timesAppeared + 1
- end
- end
- return timesAppeared
- end
- function handleMail(mail)
- local fileName = "From_" .. mail.sender .. '_'
- fileName = fileName .. getNumberOfTimesAppearedInMailDirectory(fileName)
- local file = fs.open(mailDirectory .. '/' .. fileName, 'w')
- file.write("From " .. mail.sender .. "\n\n" .. mail.message)
- file.close()
- end
- function listenForAndHandleMail()
- localShellThread = createNewShell()
- coroutine.resume(localShellThread)
- while true do
- local event = {os.pullEvent()}
- if event[1] == "rednet_message" then
- local mail = textutils.unserialize(event[3])
- if mail.sender and mail.message then
- handleMail(mail)
- local xPos, yPos = term.getCursorPos()
- term.setCursorPos(screenWidth - ("Mail from " .. mail.sender):len(), 1)
- term.write("Mail from " .. mail.sender)
- term.setCursorPos(xPos, yPos)
- event = {}
- end
- end
- coroutine.resume(localShellThread, unpack(event))
- end
- end
- if not openModem() then
- print("Modem not found.")
- return
- end
- if not fs.isDir(mailDirectory) then
- fs.makeDir(mailDirectory)
- end
- listenForAndHandleMail()
Advertisement
Add Comment
Please, Sign In to add comment