-- mail.lua
--[[--
Data structure for mail files:
{
["sender"] = (string),
["receivers"] = (table) {
[1] = (string),
[2] = (string),
etc.
},
["subject"] = (string),
["message"] = (string),
}
Printer page size: 25x21
--]]--
--(( Settings ))--
local filename = "mail.dat"
local drivePath = "back"
--(( Variables ))--
err = {
missing_item = "No item inside the disk drive!",
invalid_item = "The item in the disk drive is not a valid disk!",
}
local drive = peripheral.wrap(drivePath)
or error("Unable to wrap disk drive!")
--(( Functions ))--
-- write a letter
function writeMail(mail)
if drive.hasData() then
local path = fs.combine(drive.getMountPath(),filename)
local file = fs.open(path,"w")
file.write(textutils.serialize(mail))
file.close()
rs.setOutput("top",true)
sleep(1)
rs.setOutput("top",false)
else
error("No disk available!")
end
end
-- get a formatted title for the mail
function getTitle(mail, page, pages)
local title = '"'
util.wordwrap(mail.subject, function(row) title=title..row end, 32, 0)
title = title .. '" from '
util.wordwrap(mail.sender, function(row) title=title..row end, 32, 0)
if pages > 1 then
title = title .. " (page " .. page .. " of " .. pages .. ")"
end
return title
end
-- prepare the mail for writing
function prepareMessage(data)
local w,h = 25,21 -- size of page
local rows = {}
local message = data.message
local offset = 0
repeat
util.wordwrap("From: "..data.sender, rows, w, 0)
util.wordwrap("Subject: "..data.subject, rows, w, 1)
table.insert(rows, string.rep("-",w))
message = util.wordwrap(message, rows, w, h - #rows - 2 + offset)
for i=1,h-#rows-1+offset do table.insert(rows, "") end
table.insert(rows,666)
offset = #rows
until message == nil
-- Go through 2nd time
local pages = math.ceil(#rows/h)
for i,row in ipairs(rows) do
if row == 666 then
local page = math.ceil(i/h)
row = "[ Page " .. page .. " / " .. pages .. " ]--"
row = string.rep("-",25-#row)..row
-- apply change
rows[i] = row
end
end
return rows
end
function splitPages(rows)
local h = 21 -- height of page
local pages = {}
for i,sRow in ipairs(rows) do
local page = math.ceil(i/h)
local nRow = i-(page-1)*h
pages[page] = pages[page] or {}
pages[page][nRow] = sRow
end
return pages
end
function getDrive()
return drive
end
-- eof
Comments