|
require 'bundler/inline' |
|
gemfile do |
|
source "https://rubygems.org" |
|
gem 'mail' |
|
gem 'mail_room' |
|
gem 'pry' |
|
end |
|
|
|
require 'open3' |
|
|
|
module MailRoom |
|
module Delivery |
|
class PrintAttachments |
|
Options = Class.new do |
|
def initialize(config) |
|
@config = config.delivery_options |
|
@logger = config.logger |
|
super() |
|
end |
|
|
|
attr_reader :config |
|
attr_reader :logger |
|
|
|
def lp_command |
|
@config[:lp_command] || "lp" |
|
end |
|
|
|
def whitelisted?(mail) |
|
return true if @config[:whitelist_from].nil? || @config[:whitelist_from] == "" |
|
|
|
|
|
regex = Regexp.new(@config[:whitelist_from]) |
|
mail.return_path[regex] |
|
end |
|
end |
|
|
|
# build a new delivery, do nothing |
|
def initialize(config) |
|
@config = config |
|
end |
|
|
|
# accept the delivery, do nothing |
|
def deliver(*args) |
|
mail = Mail.new(args.first) |
|
if @config.whitelisted?(mail) |
|
print!(mail) |
|
else |
|
@config.logger.warn "unpermitted return-path #{mail.return_path} #{mail.message_id}" |
|
end |
|
true |
|
end |
|
|
|
def print!(mail) |
|
attachments = mail.attachments.select { |i| i.content_type['application/pdf'] } |
|
if attachments.length == 0 |
|
@config.logger.warn "No pdf attachments found in mail #{mail.message_id}" |
|
return |
|
end |
|
attachments.each do |pdf| |
|
print_attachment(pdf) |
|
end |
|
end |
|
|
|
def print_attachment(pdf) |
|
save_name = pdf.filename.gsub(/[^\w\.]+/, '-') |
|
tf = Tempfile.new(['mail_room_print_attachments', save_name]) |
|
tf.binmode |
|
tf.write pdf.body.decoded |
|
tf.rewind |
|
|
|
command = "#{@config.lp_command} #{Shellwords.escape(tf.path)}" |
|
|
|
stdout, stderr, status = Open3.capture3(command) |
|
if status.success? |
|
@config.logger.info("Printing #{save_name}: #{command}") |
|
else |
|
@config.logger.error("Printing failed: #{stderr}") |
|
end |
|
end |
|
end |
|
end |
|
end |
|
|
|
class MailRoom::Mailbox |
|
def delivery_klass |
|
MailRoom::Delivery::PrintAttachments |
|
end |
|
end |
|
|
|
mailboxes = [ |
|
mb = MailRoom::Mailbox.new(YAML.load_file('config.yml')) |
|
] |
|
logger = Logger.new(STDOUT) |
|
logger.formatter = proc do |severity, datetime, progname, msg| |
|
"#{severity} #{datetime.strftime("%Y-%m-%dT%H:%M:%S")}: #{msg}\n" |
|
end |
|
mailboxes.first.instance_variable_set("@logger", logger) |
|
$coordinator = MailRoom::Coordinator.new(mailboxes) |
|
|
|
def start |
|
Signal.trap(:INT) do |
|
$coordinator.running = false |
|
end |
|
|
|
Signal.trap(:TERM) do |
|
exit |
|
end |
|
|
|
puts "Starting MailRoom Server" |
|
$coordinator.run |
|
rescue Exception => e # not just Errors, but includes lower-level Exceptions |
|
p e |
|
puts e.backtrace |
|
exit |
|
end |
|
|
|
start |