viehlieb
fb8ccfea4a
mv lib to app/lib due to upgrade removing concerns from autoload path resolve zeitwerk issues make foodsoft run for dev on rails 7 and ruby 2.7 fix mail file permission bug fix database_config fix articles controller test ActiveModell::Error bump Gemfile.lock
58 lines
1.4 KiB
Ruby
58 lines
1.4 KiB
Ruby
require 'mail'
|
|
require 'midi-smtp-server'
|
|
|
|
class FoodsoftMailReceiver < MidiSmtpServer::Smtpd
|
|
@@registered_classes = Set.new
|
|
|
|
def self.register(klass)
|
|
@@registered_classes.add klass
|
|
end
|
|
|
|
def self.received(recipient, data)
|
|
find_handler(recipient).call(data)
|
|
end
|
|
|
|
def start
|
|
super
|
|
@handlers = []
|
|
end
|
|
|
|
private
|
|
|
|
def on_rcpt_to_event(_ctx, rcpt_to)
|
|
recipient = rcpt_to.gsub(/^\s*<\s*(.*)\s*>\s*$/, '\1')
|
|
@handlers << self.class.find_handler(recipient)
|
|
rcpt_to
|
|
rescue => error
|
|
logger.info("Can not accept mail for '#{rcpt_to}': #{error}")
|
|
raise MidiSmtpServer::Smtpd550Exception
|
|
end
|
|
|
|
def on_message_data_event(ctx)
|
|
@handlers.each do |handler|
|
|
handler.call(ctx[:message][:data])
|
|
end
|
|
rescue => error
|
|
ExceptionNotifier.notify_exception(error, data: ctx)
|
|
raise error
|
|
ensure
|
|
@handlers.clear
|
|
end
|
|
|
|
def self.find_handler(recipient)
|
|
m = /(?<foodcoop>[^@.]+)\.(?<address>[^@]+)(@(?<hostname>[^@]+))?/.match recipient
|
|
raise "recipient is missing or has an invalid format" if m.nil?
|
|
raise "Foodcoop '#{m[:foodcoop]}' could not be found" unless FoodsoftConfig.allowed_foodcoop? m[:foodcoop]
|
|
|
|
FoodsoftConfig.select_multifoodcoop m[:foodcoop]
|
|
|
|
@@registered_classes.each do |klass|
|
|
if match = klass.regexp.match(m[:address])
|
|
handler = klass.new match
|
|
return ->(data) { handler.received(data) }
|
|
end
|
|
end
|
|
|
|
raise "invalid format for recipient"
|
|
end
|
|
end
|