Advertisement
jille_Jr

CC: Cannon mail [Server] - mail.lua

Feb 22nd, 2016
308
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.84 KB | None | 0 0
  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 = "drive_1"
  23. local printerPath = "printer_2"
  24.  
  25. --(( Variables ))--
  26.  
  27. err = {
  28.     missing_item = "No item inside the disk drive!",
  29.     invalid_item = "The received item is not valid!",
  30.     missing_data = "No data found on the received disk!",
  31.     invalid_data = "The data on the received disk is either corrupt or invalid!",
  32. }
  33.  
  34. local printer = peripheral.wrap(printerPath)
  35. or error("Unable to wrap printer!")
  36.  
  37. local drive = peripheral.wrap(drivePath)
  38. or error("Unable to wrap disk drive!")
  39.  
  40. --(( Functions ))--
  41.  
  42. -- get the message from the disk drive
  43. function readMail()
  44.     if not drive.isDiskPresent() then
  45.         return {err="missing_item"}
  46.     end
  47.    
  48.     if not drive.hasData() then
  49.         return {err="invalid_item"}
  50.     end
  51.    
  52.     -- Get file path
  53.     local path = drive.getMountPath()
  54.     path = fs.combine(path, filename)
  55.    
  56.     if not fs.exists(path) then
  57.         return {err="missing_data"}
  58.     end
  59.    
  60.     -- Read data
  61.     local file = fs.open(path,"r")
  62.     local rawData = file.readAll()
  63.    
  64.     if type(rawData) ~= "string" then
  65.         return {err="invalid_data"}
  66.     end
  67.    
  68.     -- Unserialize it
  69.     local data = textutils.unserialize(rawData)
  70.    
  71.     if type(data) ~= "table"
  72.     or type(data.sender) ~= "string"
  73.     or type(data.receivers) ~= "table"
  74.     or type(data.message) ~= "string"
  75.     then
  76.         return {err="invalid_data"}
  77.     end
  78.    
  79.     -- Prepare message
  80.     local mail = {
  81.         sender = data.sender,
  82.         receivers = {},
  83.         subject = data.subject,
  84.         message = data.message,
  85.     }
  86.    
  87.     -- validate the receivers
  88.     for _,receiver in pairs(data.receivers) do
  89.         if type(receiver) ~= "string" then
  90.             return {err="invalid_data"}
  91.         end
  92.  
  93.         table.insert(mail.receivers,receiver)
  94.     end
  95.    
  96.     if #mail.receivers == 0 then
  97.         return {err="invalid_data"}
  98.     end
  99.    
  100.     return mail
  101. end
  102.  
  103. -- write a letter
  104. function writeMail(mail)
  105.     for _,receiver in ipairs(mail.receivers) do
  106.  
  107.         local dir = inv.getDirection(receiver)
  108.         if type(dir) == "string" then
  109.  
  110.             local rows = prepareMessage(mail)
  111.             local pages = math.ceil(#rows/21) -- the pages are 21 high
  112.  
  113.             for page = 1,pages do
  114.                 if not printer.newPage() then
  115.                     error("Unable to start new page!")
  116.                 end
  117.  
  118.                 local title = '"'
  119.                 util.wordwrap(mail.subject, function(row) title=title..row end, 32, 0)
  120.                 title = title .. '" from '
  121.                 util.wordwrap(mail.sender, function(row) title=title..row end, 32, 0)
  122.  
  123.                 if pages > 1 then
  124.                     title = title .. " (page " .. page .. " of " .. pages .. ")"
  125.                 end
  126.  
  127.                 printer.setPageTitle(title)
  128.  
  129.                 local rows = prepareMessage(mail)
  130.                 for i,row in ipairs(rows) do
  131.                     if row == 666 then
  132.                         row = "[ Page " .. page .. " / " .. pages .. " ]--"
  133.                         row = string.rep("-",25-#row)..row
  134.  
  135.                         printer.setCursorPos(1,21)
  136.                     else
  137.                         printer.setCursorPos(1,i-(page-1)*21)
  138.                     end
  139.  
  140.                     printer.write(row)
  141.                 end
  142.  
  143.                 if not printer.endPage() then
  144.                     error("Unable to end page!")
  145.                 end
  146.  
  147.                 sleep(.5)
  148.                 inv.emptyDropper(dir)
  149.             end
  150.  
  151.         else
  152.             print("No mailbox for \"" .. receiver .. "\"!")
  153.         end
  154.     end
  155. end
  156.  
  157. -- prepare the mail for writing
  158. function prepareMessage(mail)
  159.     local w,h = 25,21 -- size of page
  160.     local rows = {}
  161.     local message = mail.message
  162.     local offset = 0
  163.  
  164.     repeat
  165.         util.wordwrap("From: "..mail.sender, rows, w, 0)
  166.         util.wordwrap("Subject: "..mail.subject, rows, w, 1)
  167.         table.insert(rows, string.rep("-",w))
  168.         message = util.wordwrap(message, rows, w, h - #rows - 2 + offset)
  169.         table.insert(rows,666)
  170.  
  171.         offset = #rows
  172.     until message == nil
  173.  
  174.     return rows
  175. end
  176.  
  177. function getDrive()
  178.     return drive
  179. end
  180.  
  181. function getPrinter()
  182.     return printer
  183. end
  184.  
  185. -- eof
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement