Move SMTP server from messages plugin into core
This commit is contained in:
parent
2264351cf5
commit
7d594bf391
9 changed files with 117 additions and 92 deletions
45
lib/foodsoft_mail_receiver.rb
Normal file
45
lib/foodsoft_mail_receiver.rb
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
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)
|
||||
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.foodcoops.include? m[:foodcoop]
|
||||
FoodsoftConfig.select_multifoodcoop m[:foodcoop]
|
||||
|
||||
@@registered_classes.each do |klass|
|
||||
klass_m = klass.regexp.match(m[:address])
|
||||
return klass.new.received klass_m, data if klass_m
|
||||
end
|
||||
|
||||
raise "invalid format for recipient"
|
||||
end
|
||||
|
||||
def start
|
||||
super
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def on_message_data_event(ctx)
|
||||
puts ctx[:envelope][:to]
|
||||
ctx[:envelope][:to].each do |to|
|
||||
begin
|
||||
m = /<(?<recipient>[^<>]+)>/.match(to)
|
||||
raise "invalid format for RCPT TO" if m.nil?
|
||||
FoodsoftMailReceiver.received(m[:recipient], ctx[:message][:data])
|
||||
rescue => error
|
||||
Rails.logger.warn "Can't deliver mail to #{to}: #{error.message}"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
|
@ -43,6 +43,21 @@ namespace :foodsoft do
|
|||
rake_say "created until #{created_until}"
|
||||
end
|
||||
end
|
||||
|
||||
desc "Parse incoming email on stdin (options: RECIPIENT=foodcoop.handling)"
|
||||
task :parse_reply_email => :environment do
|
||||
FoodsoftMailReceiver.received ENV['RECIPIENT'], STDIN.read
|
||||
end
|
||||
|
||||
desc "Start STMP server for incoming email (options: SMTP_SERVER_PORT=2525, SMTP_SERVER_HOST=0.0.0.0)"
|
||||
task :reply_email_smtp_server => :environment do
|
||||
port = ENV['SMTP_SERVER_PORT'].present? ? ENV['SMTP_SERVER_PORT'].to_i : 2525
|
||||
host = ENV['SMTP_SERVER_HOST']
|
||||
rake_say "Started SMTP server for incoming email on port #{port}."
|
||||
server = FoodsoftMailReceiver.new port, host
|
||||
server.start
|
||||
server.join
|
||||
end
|
||||
end
|
||||
|
||||
# Helper
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue