Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move SMTP server from messages plugin into core
- Loading branch information
Showing
9 changed files
with
117 additions
and
92 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
plugins/messages/app/mail_receivers/messages_mail_receiver.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
class MessagesMailReceiver | ||
|
||
def self.regexp | ||
/(?<message_id>\d+)\.(?<user_id>\d+)\.(?<hash>\w+)/ | ||
end | ||
|
||
def received(match, data) | ||
original_message = Message.find_by_id(match[:message_id]) | ||
user = User.find_by_id(match[:user_id]) | ||
|
||
raise "Message could not be found" if original_message.nil? | ||
raise "User could not be found" if user.nil? | ||
|
||
hash = original_message.mail_hash_for_user user | ||
raise "Hash does not match expectations" unless hash.casecmp(match[:hash]) == 0 | ||
|
||
mail = Mail.new data | ||
|
||
mail_part = nil | ||
if mail.multipart? | ||
for part in mail.parts | ||
mail_part = part if MIME::Type.simplified(part.content_type) == "text/plain" | ||
end | ||
else | ||
mail_part = mail | ||
end | ||
|
||
body = mail_part.body.decoded | ||
unless mail_part.content_type_parameters.nil? | ||
body = body.force_encoding mail_part.content_type_parameters[:charset] | ||
end | ||
|
||
message = user.send_messages.new body: body, | ||
group: original_message.group, | ||
private: original_message.private, | ||
received_email: received_email, | ||
subject: mail.subject.gsub("[#{FoodsoftConfig[:name]}] ", "") | ||
if original_message.reply_to | ||
message.reply_to_message = original_message.reply_to_message | ||
else | ||
message.reply_to_message = original_message | ||
end | ||
message.add_recipients original_message.recipients | ||
message.add_recipients [original_message.sender] | ||
|
||
message.save! | ||
Resque.enqueue(MessageNotifier, FoodsoftConfig.scope, "message_deliver", message.id) | ||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
ActiveSupport.on_load(:after_initialize) do | ||
FoodsoftMailReceiver.register MessagesMailReceiver | ||
end |
This file was deleted.
Oops, something went wrong.