Skip to content

Commit

Permalink
Fix help text around tootctl email_domain_blocks (mastodon#14147)
Browse files Browse the repository at this point in the history
  • Loading branch information
Gargron authored and Mage committed Jan 14, 2022
1 parent 5f75643 commit 7629691
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 14 deletions.
2 changes: 1 addition & 1 deletion lib/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def self.exit_on_failure?
desc 'upgrade SUBCOMMAND ...ARGS', 'Various version upgrade utilities'
subcommand 'upgrade', Mastodon::UpgradeCLI

desc 'email-domain-blocks SUBCOMMAND ...ARGS', 'Manage E-mail domain blocks'
desc 'email_domain_blocks SUBCOMMAND ...ARGS', 'Manage e-mail domain blocks'
subcommand 'email_domain_blocks', Mastodon::EmailDomainBlocksCLI

option :dry_run, type: :boolean
Expand Down
31 changes: 18 additions & 13 deletions lib/mastodon/email_domain_blocks_cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,27 +13,29 @@ def self.exit_on_failure?
true
end

desc 'list', 'list E-mail domain blocks'
long_desc <<-LONG_DESC
list up all E-mail domain blocks.
LONG_DESC
desc 'list', 'List blocked e-mail domains'
def list
EmailDomainBlock.where(parent_id: nil).order(id: 'DESC').find_each do |entry|
say(entry.domain.to_s, :white)

EmailDomainBlock.where(parent_id: entry.id).order(id: 'DESC').find_each do |child|
say(" #{child.domain}", :cyan)
end
end
end

option :with_dns_records, type: :boolean
desc 'add [DOMAIN...]', 'add E-mail domain blocks'
desc 'add DOMAIN...', 'Block e-mail domain(s)'
long_desc <<-LONG_DESC
add E-mail domain blocks from a given DOMAIN.
When the --with-dns-records option is given, An attempt to resolve the
given domain's DNS records will be made and the results will also be
blacklisted.
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
given domains' DNS records will be made and the results (A, AAAA and MX) 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
it at the root.
LONG_DESC
def add(*domains)
if domains.empty?
Expand Down Expand Up @@ -72,11 +74,13 @@ def add(*domains)

(hostnames + ips).uniq.each do |hostname|
another_email_domain_block = EmailDomainBlock.new(domain: hostname, parent: email_domain_block)

if EmailDomainBlock.where(domain: hostname).exists?
say("#{hostname} is already blocked.", :yellow)
skipped += 1
next
end

another_email_domain_block.save!
processed += 1
end
Expand All @@ -85,7 +89,7 @@ def add(*domains)
say("Added #{processed}, skipped #{skipped}", color(processed, 0))
end

desc 'remove [DOMAIN...]', 'remove E-mail domain blocks'
desc 'remove DOMAIN...', 'Remove e-mail domain blocks'
def remove(*domains)
if domains.empty?
say('No domain(s) given', :red)
Expand All @@ -98,19 +102,20 @@ def remove(*domains)

domains.each do |domain|
entry = EmailDomainBlock.find_by(domain: domain)

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

if result
processed += 1 + children_count
else
say("#{domain} was not unblocked. 'destroy' returns false.", :red)
say("#{domain} could not be unblocked.", :red)
failed += 1
end
end
Expand Down

0 comments on commit 7629691

Please sign in to comment.