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?
|
2018-11-14 20:47:40 -05:00
|
|
|
puts "4.3.5 URL and INGRESS_PASSWORD are required"
|
|
|
|
exit 1
|
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-14 14:16:03 -05:00
|
|
|
.post(url, body: STDIN.read,
|
|
|
|
headers: { "Content-Type" => "message/rfc822", "User-Agent" => ENV.fetch("USER_AGENT", "Postfix") })
|
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?
|
2018-11-14 20:47:40 -05:00
|
|
|
puts "4.7.0 HTTP #{response.status}"
|
|
|
|
exit 1
|
2018-11-05 14:18:03 -05:00
|
|
|
when response.status.unsupported_media_type?
|
2018-11-14 20:47:40 -05:00
|
|
|
puts "5.6.1 HTTP #{response.status}"
|
|
|
|
exit 1
|
2018-10-19 16:31:53 -04:00
|
|
|
else
|
2018-11-14 20:47:40 -05:00
|
|
|
puts "4.0.0 HTTP #{response.status}"
|
|
|
|
exit 1
|
2018-10-19 16:31:53 -04:00
|
|
|
end
|
|
|
|
rescue HTTP::ConnectionError => error
|
2018-11-14 20:47:40 -05:00
|
|
|
puts "4.4.2 Error connecting to the Postfix ingress: #{error.message}"
|
|
|
|
exit 1
|
2018-10-19 16:31:53 -04:00
|
|
|
rescue HTTP::TimeoutError
|
2018-11-14 20:47:40 -05:00
|
|
|
puts "4.4.7 Timed out piping to the Postfix ingress"
|
|
|
|
exit 1
|
2018-10-19 16:31:53 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|