jille_Jr

CC: Cannon mail [Client] - mail.lua

Feb 22nd, 2016
56
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -- mail.lua
  2.  
  3. --[[--
  4.     Data structure for mail files:
  5.     {
  6.         ["sender"] = (string),
  7.         ["receivers"] = (table) {
  8.             [1] = (string),
  9.             [2] = (string),
  10.             etc.
  11.         },
  12.         ["subject"] = (string),
  13.         ["message"] = (string),
  14.     }
  15.  
  16.     Printer page size: 25x21
  17. --]]--
  18.  
  19. --(( Settings ))--
  20.  
  21. local filename = "mail.dat"
  22. local drivePath = "back"
  23.  
  24. --(( Variables ))--
  25.  
  26. err = {
  27.     missing_item = "No item inside the disk drive!",
  28.     invalid_item = "The item in the disk drive is not a valid disk!",
  29. }
  30.  
  31. local drive = peripheral.wrap(drivePath)
  32. or error("Unable to wrap disk drive!")
  33.  
  34. --(( Functions ))--
  35.  
  36. -- write a letter
  37. function writeMail(mail)
  38.     if drive.hasData() then
  39.         local path = fs.combine(drive.getMountPath(),filename)
  40.  
  41.         local file = fs.open(path,"w")
  42.         file.write(textutils.serialize(mail))
  43.         file.close()
  44.  
  45.         rs.setOutput("top",true)
  46.         sleep(1)
  47.         rs.setOutput("top",false)
  48.     else
  49.         error("No disk available!")
  50.     end
  51. end
  52.  
  53. -- get a formatted title for the mail
  54. function getTitle(mail, page, pages)
  55.     local title = '"'
  56.     util.wordwrap(mail.subject, function(row) title=title..row end, 32, 0)
  57.     title = title .. '" from '
  58.     util.wordwrap(mail.sender, function(row) title=title..row end, 32, 0)
  59.  
  60.     if pages > 1 then
  61.         title = title .. " (page " .. page .. " of " .. pages .. ")"
  62.     end
  63.  
  64.     return title
  65. end
  66.  
  67. -- prepare the mail for writing
  68. function prepareMessage(data)
  69.     local w,h = 25,21 -- size of page
  70.     local rows = {}
  71.     local message = data.message
  72.     local offset = 0
  73.  
  74.     repeat
  75.         util.wordwrap("From: "..data.sender, rows, w, 0)
  76.         util.wordwrap("Subject: "..data.subject, rows, w, 1)
  77.         table.insert(rows, string.rep("-",w))
  78.         message = util.wordwrap(message, rows, w, h - #rows - 2 + offset)
  79.         for i=1,h-#rows-1+offset do table.insert(rows, "") end
  80.         table.insert(rows,666)
  81.  
  82.         offset = #rows
  83.     until message == nil
  84.  
  85.     -- Go through 2nd time
  86.     local pages = math.ceil(#rows/h)
  87.  
  88.     for i,row in ipairs(rows) do
  89.         if row == 666 then
  90.             local page = math.ceil(i/h)
  91.  
  92.             row = "[ Page " .. page .. " / " .. pages .. " ]--"
  93.             row = string.rep("-",25-#row)..row
  94.  
  95.             -- apply change
  96.             rows[i] = row
  97.         end
  98.     end
  99.  
  100.     return rows
  101. end
  102.  
  103. function splitPages(rows)
  104.     local h = 21 -- height of page
  105.     local pages = {}
  106.  
  107.     for i,sRow in ipairs(rows) do
  108.         local page = math.ceil(i/h)
  109.         local nRow = i-(page-1)*h
  110.  
  111.         pages[page] = pages[page] or {}
  112.         pages[page][nRow] = sRow
  113.     end
  114.  
  115.     return pages
  116. end
  117.  
  118. function getDrive()
  119.     return drive
  120. end
  121.  
  122. -- eof
RAW Paste Data