/
silence
executable file
·125 lines (94 loc) · 3.32 KB
/
silence
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#!/usr/bin/env ruby
require 'rubygems'
require 'bundler'
require 'net/http'
require 'optparse'
require 'yaml'
CONF_PATH = "#{ENV['HOME']}/.silence"
PARSE_LINK = 'https://fediverse.network/mastodon?age=created&build=gab'
def get_fresh_links
# download a fresh copy and check against a cached version
document = Nokogiri::HTML(Net::HTTP.get(URI.parse(PARSE_LINK)))
document.css('table.table-condensed td a').collect {|link| link.content}
end
config = File.exists?(CONF_PATH) ? YAML.load_file(CONF_PATH) : {}
options = {}
OptionParser.new do |opts|
opts.banner = 'Usage: ./silence [options]'
opts.on('-a', '--admin',
'Run in admin mode -- for use by Mastodon Server owners') do
options[:admin] = true
end
opts.on('-t', '--token TOKEN',
'Mastodon access TOKEN') do |t|
options[:token] = t
end
opts.on('-i', '--instance INSTANCE',
'Specify the users Mastodon INSTANCE') do |i|
options[:instance] = i
end
opts.on('-v', '--verbose',
'Prints out domains as they get blocked') do
options[:verbose] = true
end
opts.on('-p', '--path PATH',
'PATH to Mastodon root folder') do |p|
options[:path] = p
end
opts.on('-h', '--help', 'prints this help') do
puts opts
exit
end
end.parse!
config.merge! options
# if a new link has been added to the site we need to block it
if options[:admin]
## ADMIN MODE
APP_PATH = options[:path] || config[:path] || '..'
# load up our rails application environment
require_relative "#{APP_PATH}/config/environment"
links = get_fresh_links
if links.join != (config[:cache] || '')
# create a domain block for each link
links.each do |url|
unless DomainBlock.blocked? url
DomainBlock.new(domain: url,
severity: 'suspend',
reject_media: true,
reject_reports: true).save!
puts "blocking #{url}" if options[:verbose]
end
end
# write out our new cache
config[:cache] = links.join
File.write(CONF_PATH, config.to_yaml)
end
else
## USER MODE
require 'bundler/setup'
require 'nokogiri'
require 'moostodon'
links = get_fresh_links
if links.join != (config[:cache] || '')
info = { instance: "https://#{options[:instance].nil? ? '' :
options[:instance].gsub(/https?:\/\//,'')}".strip,
token: options[:token] }
info[:instance], info[:token] = config[:instance].strip, config[:token].strip if File.exists?(CONF_PATH)
# create our mastodon client
raise 'no user information found. see help' if ((info[:instance].nil? or info[:instance].empty?) or
(info[:token].nil? or info[:token].empty?))
client = Mastodon::REST::Client.new(base_url: info[:instance],
bearer_token: info[:token])
blocks = Array(client.domain_blocks)
# user-level block each domain
links.select {|link| !blocks.include?(link)}.each do |url|
puts "blocking #{url}" if options[:verbose]
client.block_domain(url)
end
# save our info for next time
info.merge! config
info[:cache] = links.join
File.write(CONF_PATH, info.to_yaml)
end
end
puts "done!" if options[:verbose]