diff --git a/app/lib/received_header.rb b/app/lib/received_header.rb new file mode 100644 index 0000000..7d578f4 --- /dev/null +++ b/app/lib/received_header.rb @@ -0,0 +1,30 @@ +# frozen_string_literal: true + +class ReceivedHeader + + OUR_HOSTNAMES = { + smtp: Postal.config.dns.smtp_server_hostname, + http: Postal.config.web.host + }.freeze + + class << self + + def generate(server, helo, ip_address, method) + our_hostname = OUR_HOSTNAMES[method] + if our_hostname.nil? + raise Error, "`method` is invalid (must be one of #{OUR_HOSTNAMES.join(', ')})" + end + + header = "by #{our_hostname} with #{method.to_s.upcase}; #{Time.now.utc.rfc2822}" + + if server.nil? || server.privacy_mode == false + hostname = DNSResolver.local.ip_to_hostname(ip_address) + header = "from #{helo} (#{hostname} [#{ip_address}]) #{header}" + end + + header + end + + end + +end diff --git a/app/lib/smtp_server/client.rb b/app/lib/smtp_server/client.rb index 3040f7f..c89aea2 100644 --- a/app/lib/smtp_server/client.rb +++ b/app/lib/smtp_server/client.rb @@ -366,8 +366,8 @@ module SMTPServer @headers = {} @receiving_headers = true - received_header = Postal::ReceivedHeader.generate(@credential&.server, @helo_name, @ip_address, :smtp) - .force_encoding("BINARY") + received_header = ReceivedHeader.generate(@credential&.server, @helo_name, @ip_address, :smtp) + .force_encoding("BINARY") @data << "Received: #{received_header}\r\n" @headers["received"] = [received_header] diff --git a/app/models/incoming_message_prototype.rb b/app/models/incoming_message_prototype.rb index 334df62..ee3c0ec 100644 --- a/app/models/incoming_message_prototype.rb +++ b/app/models/incoming_message_prototype.rb @@ -95,7 +95,7 @@ class IncomingMessagePrototype content: attachment[:data] } end - mail.header["Received"] = Postal::ReceivedHeader.generate(@server, @source_type, @ip, :http) + mail.header["Received"] = ReceivedHeader.generate(@server, @source_type, @ip, :http) mail.to_s end end diff --git a/app/models/outgoing_message_prototype.rb b/app/models/outgoing_message_prototype.rb index 7362f30..f36522a 100644 --- a/app/models/outgoing_message_prototype.rb +++ b/app/models/outgoing_message_prototype.rb @@ -177,7 +177,7 @@ class OutgoingMessagePrototype content: attachment[:data] } end - mail.header["Received"] = Postal::ReceivedHeader.generate(@server, @source_type, @ip, :http) + mail.header["Received"] = ReceivedHeader.generate(@server, @source_type, @ip, :http) mail.message_id = "<#{@message_id}>" mail.to_s end diff --git a/lib/postal/received_header.rb b/lib/postal/received_header.rb deleted file mode 100644 index b019d69..0000000 --- a/lib/postal/received_header.rb +++ /dev/null @@ -1,32 +0,0 @@ -# frozen_string_literal: true - -module Postal - class ReceivedHeader - - OUR_HOSTNAMES = { - smtp: Postal.config.dns.smtp_server_hostname, - http: Postal.config.web.host - }.freeze - - class << self - - def generate(server, helo, ip_address, method) - our_hostname = OUR_HOSTNAMES[method] - if our_hostname.nil? - raise Error, "`method` is invalid (must be one of #{OUR_HOSTNAMES.join(', ')})" - end - - header = "by #{our_hostname} with #{method.to_s.upcase}; #{Time.now.utc.rfc2822}" - - if server.nil? || server.privacy_mode == false - hostname = DNSResolver.local.ip_to_hostname(ip_address) - header = "from #{helo} (#{hostname} [#{ip_address}]) #{header}" - end - - header - end - - end - - end -end diff --git a/spec/lib/postal/received_header_spec.rb b/spec/lib/received_header_spec.rb similarity index 98% rename from spec/lib/postal/received_header_spec.rb rename to spec/lib/received_header_spec.rb index 0a9c6bb..199f027 100644 --- a/spec/lib/postal/received_header_spec.rb +++ b/spec/lib/received_header_spec.rb @@ -2,7 +2,7 @@ require "rails_helper" -describe Postal::ReceivedHeader do +describe ReceivedHeader do before do allow(DNSResolver.local).to receive(:ip_to_hostname).and_return("hostname.com") end