62 lines
2.0 KiB
Ruby
62 lines
2.0 KiB
Ruby
class Distributor
|
|
|
|
class Attributes < Hash
|
|
def self.parse(subject:, body:)
|
|
new = Attributes.new
|
|
subject_added_attribute = false
|
|
subject.to_s.split(",").each { new.add_attribute!(_1); subject_added_attribute = true }
|
|
body.to_s.split("\r\n").each { new.add_attribute!(_1) } if body.include?("=") && subject_added_attribute == false
|
|
new
|
|
end
|
|
|
|
def add_attribute!(key_eq_value)
|
|
k, v = key_eq_value.split("=", 2)
|
|
self[k.strip] = v.strip if k && v
|
|
end
|
|
end
|
|
|
|
def initialize
|
|
@smtp_client = Protocols::Smtp.new
|
|
@imap_client = Protocols::Imap.new
|
|
@imap_client.clean if ENV["HARD_RESET_NOT_SEEN_MESSAGE"] == "true"
|
|
@handlers = {
|
|
:default => Actions::Distribute.new(distributor: self),
|
|
"subscribe" => Actions::Subscribe.new(distributor: self),
|
|
"unsubscribe" => Actions::Unsubscribe.new(distributor: self),
|
|
"help" => Actions::Help.new(distributor: self),
|
|
"set-permissions" => Actions::SetPermissions.new(distributor: self),
|
|
"list-users" => Actions::ListUsers.new(distributor: self),
|
|
}
|
|
end
|
|
|
|
def handle_one(mail)
|
|
$logger.info "incoming email from #{mail.from} | #{mail.subject}"
|
|
list = Mailinglist.search_mail(mail)
|
|
if list
|
|
# TODO: create list from mail ?
|
|
subject, subject_attributes = mail.subject.split(",", 2)
|
|
attributes = Attributes.parse(subject: subject_attributes, body: mail.body)
|
|
handler = @handlers[subject] || @handlers[:default]
|
|
$logger.info "#{handler.class}#handle on #{list.email} for #{mail.from}"
|
|
handler.handle(list: list, to: mail, attributes: attributes)
|
|
end
|
|
mail.seen!(imap_client: @imap_client)
|
|
end
|
|
|
|
def distribute(*ary, **opt)
|
|
@smtp_client.distribute(*ary, **opt)
|
|
end
|
|
|
|
def start(cpu_sleep: 1)
|
|
puts "fetching new mail to distribute every #{cpu_sleep} second..."
|
|
loop do
|
|
mail = @imap_client.fetch
|
|
handle_one(mail) if mail
|
|
|
|
sleep cpu_sleep
|
|
end
|
|
end
|
|
|
|
require_relative "distributor/action"
|
|
end
|