#!/usr/bin/ruby # frozen_string_literal: true # This expects postfix to be piping an email into stdin require "dhall" require "mail" require "nokogiri" require "securerandom" require "sentry-ruby" require "time" require_relative "../lib/blather_notify" require_relative "../lib/interac_email" def error_entry(title, text, id) Nokogiri::XML::Builder.new { |xml| xml.entry(xmlns: "http://www.w3.org/2005/Atom") do xml.updated DateTime.now.iso8601 xml.id id xml.title title xml.content text.to_s, type: "text" xml.author { xml.name "interac_email" } xml.generator "interac_email", version: "1.0" end }.doc.root end raise "Need a Dhall config" unless ARGV[0] # I shift here because ARGF will work with either stdin or a file in argv if # there are any CONFIG = Dhall::Coder .new(safe: Dhall::Coder::JSON_LIKE + [Symbol, Proc]) .load(ARGV.shift, transform_keys: :to_sym) Sentry.init do |config| config.background_worker_threads = 0 end pubsub = BlatherNotify::PubSub::Address.new(**CONFIG[:pubsub]) m = Mail.new(ARGF.read) id_builder = ->(id) { "#{pubsub.to_uri};item=#{id}" } EM.run do BlatherNotify.start( CONFIG[:jid], CONFIG[:password], default_pubsub_addr: pubsub ).then { InteracEmail.for(m, id_builder: id_builder).process }.catch_only(InteracEmail::Error) { |e| uuid = SecureRandom.uuid BlatherNotify.publish "#{uuid}": error_entry("💥 Exception #{e.class}", e, id_builder.call(uuid)) }.catch { |e| if e.is_a?(Exception) Sentry.capture_exception(e) else Sentry.capture_message(e.to_s) end puts e }.then { BlatherNotify.shutdown } end