2018-10-19 16:31:53 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
namespace :action_mailbox do
|
|
|
|
namespace :ingress do
|
|
|
|
desc "Pipe an inbound email from STDIN to the Postfix ingress at the given URL"
|
|
|
|
task :postfix do
|
|
|
|
require "active_support"
|
|
|
|
require "active_support/core_ext/object/blank"
|
|
|
|
require "http"
|
|
|
|
|
2018-11-08 07:30:37 -05:00
|
|
|
url, password = ENV.values_at("URL", "INGRESS_PASSWORD")
|
2018-10-19 16:31:53 -04:00
|
|
|
|
2018-11-08 07:30:37 -05:00
|
|
|
if url.blank? || password.blank?
|
|
|
|
abort "4.3.5 URL and INGRESS_PASSWORD are required"
|
2018-10-19 16:31:53 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
begin
|
2018-11-05 14:04:05 -05:00
|
|
|
response = HTTP.basic_auth(user: "actionmailbox", pass: password)
|
2018-10-19 16:31:53 -04:00
|
|
|
.timeout(connect: 1, write: 10, read: 10)
|
2018-11-05 14:18:03 -05:00
|
|
|
.post(url, headers: { "Content-Type" => "message/rfc822", "User-Agent" => ENV.fetch("USER_AGENT", "Postfix") }, body: STDIN)
|
2018-10-19 16:31:53 -04:00
|
|
|
|
2018-11-05 14:18:03 -05:00
|
|
|
case
|
|
|
|
when response.status.success?
|
2018-10-19 16:31:53 -04:00
|
|
|
puts "2.0.0 HTTP #{response.status}"
|
2018-11-05 14:18:03 -05:00
|
|
|
when response.status.unauthorized?
|
|
|
|
abort "4.7.0 HTTP #{response.status}"
|
|
|
|
when response.status.unsupported_media_type?
|
|
|
|
abort "5.6.1 HTTP #{response.status}"
|
2018-10-19 16:31:53 -04:00
|
|
|
else
|
2018-11-05 14:18:03 -05:00
|
|
|
abort "4.0.0 HTTP #{response.status}"
|
2018-10-19 16:31:53 -04:00
|
|
|
end
|
|
|
|
rescue HTTP::ConnectionError => error
|
2018-11-05 14:18:03 -05:00
|
|
|
abort "4.4.2 Error connecting to the Postfix ingress: #{error.message}"
|
2018-10-19 16:31:53 -04:00
|
|
|
rescue HTTP::TimeoutError
|
2018-11-05 14:18:03 -05:00
|
|
|
abort "4.4.7 Timed out piping to the Postfix ingress"
|
2018-10-19 16:31:53 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|