2020-06-25 12:17:10 +02:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'concurrent'
|
2023-05-24 11:55:40 +02:00
|
|
|
require_relative 'base'
|
2020-06-25 12:17:10 +02:00
|
|
|
|
2023-05-23 16:08:26 +02:00
|
|
|
module Mastodon::CLI
|
2023-05-24 11:55:40 +02:00
|
|
|
class EmailDomainBlocks < Base
|
2020-06-26 21:28:40 +02:00
|
|
|
desc 'list', 'List blocked e-mail domains'
|
2020-06-25 12:17:10 +02:00
|
|
|
def list
|
2023-12-04 10:44:54 +01:00
|
|
|
EmailDomainBlock.where(parent_id: nil).find_each do |entry|
|
2020-06-25 12:17:10 +02:00
|
|
|
say(entry.domain.to_s, :white)
|
2020-06-26 21:28:40 +02:00
|
|
|
|
2023-12-04 10:44:54 +01:00
|
|
|
EmailDomainBlock.where(parent_id: entry.id).find_each do |child|
|
2020-06-25 12:17:10 +02:00
|
|
|
say(" #{child.domain}", :cyan)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
option :with_dns_records, type: :boolean
|
2020-06-26 21:28:40 +02:00
|
|
|
desc 'add DOMAIN...', 'Block e-mail domain(s)'
|
2020-06-25 12:17:10 +02:00
|
|
|
long_desc <<-LONG_DESC
|
2020-06-26 21:28:40 +02:00
|
|
|
Blocking an e-mail domain prevents users from signing up
|
|
|
|
with e-mail addresses from that domain. You can provide one or
|
|
|
|
multiple domains to the command.
|
|
|
|
|
|
|
|
When the --with-dns-records option is given, an attempt to resolve the
|
2022-03-21 19:10:09 +01:00
|
|
|
given domains' MX records will be made and the results will also be blocked.
|
|
|
|
This can be helpful if you are blocking an e-mail server that has many
|
|
|
|
different domains pointing to it as it allows you to essentially block
|
2020-06-26 21:28:40 +02:00
|
|
|
it at the root.
|
2020-06-25 12:17:10 +02:00
|
|
|
LONG_DESC
|
|
|
|
def add(*domains)
|
2024-01-26 09:53:44 +01:00
|
|
|
fail_with_message 'No domain(s) given' if domains.empty?
|
2020-06-25 12:17:10 +02:00
|
|
|
|
|
|
|
skipped = 0
|
|
|
|
processed = 0
|
|
|
|
|
|
|
|
domains.each do |domain|
|
2023-05-30 16:09:57 +02:00
|
|
|
if EmailDomainBlock.exists?(domain: domain)
|
2020-06-25 12:17:10 +02:00
|
|
|
say("#{domain} is already blocked.", :yellow)
|
|
|
|
skipped += 1
|
|
|
|
next
|
|
|
|
end
|
|
|
|
|
2022-03-21 19:10:09 +01:00
|
|
|
other_domains = []
|
|
|
|
if options[:with_dns_records]
|
|
|
|
Resolv::DNS.open do |dns|
|
|
|
|
dns.timeouts = 5
|
|
|
|
other_domains = dns.getresources(@email_domain_block.domain, Resolv::DNS::Resource::IN::MX).to_a
|
2020-06-25 12:17:10 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-03-21 19:10:09 +01:00
|
|
|
email_domain_block = EmailDomainBlock.new(domain: domain, other_domains: other_domains)
|
|
|
|
email_domain_block.save!
|
|
|
|
processed += 1
|
|
|
|
|
|
|
|
(email_domain_block.other_domains || []).uniq.each do |hostname|
|
2020-06-25 12:17:10 +02:00
|
|
|
another_email_domain_block = EmailDomainBlock.new(domain: hostname, parent: email_domain_block)
|
2020-06-26 21:28:40 +02:00
|
|
|
|
2023-05-30 16:09:57 +02:00
|
|
|
if EmailDomainBlock.exists?(domain: hostname)
|
2020-06-25 12:17:10 +02:00
|
|
|
say("#{hostname} is already blocked.", :yellow)
|
|
|
|
skipped += 1
|
|
|
|
next
|
|
|
|
end
|
2020-06-26 21:28:40 +02:00
|
|
|
|
2020-06-25 12:17:10 +02:00
|
|
|
another_email_domain_block.save!
|
|
|
|
processed += 1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
say("Added #{processed}, skipped #{skipped}", color(processed, 0))
|
|
|
|
end
|
|
|
|
|
2020-06-26 21:28:40 +02:00
|
|
|
desc 'remove DOMAIN...', 'Remove e-mail domain blocks'
|
2020-06-25 12:17:10 +02:00
|
|
|
def remove(*domains)
|
2024-01-26 09:53:44 +01:00
|
|
|
fail_with_message 'No domain(s) given' if domains.empty?
|
2020-06-25 12:17:10 +02:00
|
|
|
|
|
|
|
skipped = 0
|
|
|
|
processed = 0
|
|
|
|
failed = 0
|
|
|
|
|
|
|
|
domains.each do |domain|
|
|
|
|
entry = EmailDomainBlock.find_by(domain: domain)
|
2020-06-26 21:28:40 +02:00
|
|
|
|
2020-06-25 12:17:10 +02:00
|
|
|
if entry.nil?
|
|
|
|
say("#{domain} is not yet blocked.", :yellow)
|
|
|
|
skipped += 1
|
|
|
|
next
|
|
|
|
end
|
|
|
|
|
|
|
|
children_count = EmailDomainBlock.where(parent_id: entry.id).count
|
|
|
|
result = entry.destroy
|
2020-06-26 21:28:40 +02:00
|
|
|
|
2020-06-25 12:17:10 +02:00
|
|
|
if result
|
2021-01-31 21:26:09 +01:00
|
|
|
processed += children_count + 1
|
2020-06-25 12:17:10 +02:00
|
|
|
else
|
2020-06-26 21:28:40 +02:00
|
|
|
say("#{domain} could not be unblocked.", :red)
|
2020-06-25 12:17:10 +02:00
|
|
|
failed += 1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
say("Removed #{processed}, skipped #{skipped}, failed #{failed}", color(processed, failed))
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def color(processed, failed)
|
|
|
|
if !processed.zero? && failed.zero?
|
|
|
|
:green
|
|
|
|
elsif failed.zero?
|
|
|
|
:yellow
|
|
|
|
else
|
|
|
|
:red
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|