-
Notifications
You must be signed in to change notification settings - Fork 6
/
abusefilter_irc.rb
250 lines (197 loc) · 8.66 KB
/
abusefilter_irc.rb
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
$LOAD_PATH << '..'
require 'musikbot'
require 'cinch'
require 'em-eventsource'
###
# Schema for the database:
#
# CREATE TABLE subscriptions (
# id INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
# project VARCHAR(30) NOT NULL,
# user VARCHAR(255) NOT NULL,
# filter_id INT NOT NULL
# ) ENGINE=InnoDB DEFAULT CHARSET=utf8 ROW_FORMAT=DYNAMIC;
# CREATE UNIQUE INDEX proj_user_sub ON subscriptions (project, user, filter_id);
###
module AbuseFilterIRC
include Cinch::Plugin
CHANNELS = {
'commons.wikimedia.org' => '#wikimedia-commons-abuse-log',
'en.wikipedia.org' => '#wikipedia-en-abuse-log-all',
'meta.wikimedia.org' => '#wikimedia-meta-abuse-log',
'wikimania.wikimedia.org' => '#wikimania-abuse-log',
'en.wikisource.org' => '#wikisource-en-abuse-log'
}
UNSUBSCRIBE_MSG = 'To unsubscribe, use `!unsubscribe [lang.project.org] [filter ID]` or `!unsubscribe [lang.project.org] all`'
def self.run
$mb = MusikBot::Session.new(inspect)
$client = $mb.repl_client(credentials: :abusefilter_irc, log: false)
$subscriptions = nil
bot = Cinch::Bot.new do
configure do |c|
c.server = 'irc.libera.chat'
c.port = 6667
c.channels = CHANNELS.values
c.nick = $mb.app_config[:irc][:nick]
c.password = $mb.app_config[:irc][:password]
c.user = $mb.app_config[:irc][:user]
end
helpers do
def authed(user)
unless user.authed?
user.send "Your IRC nick must be identified to subscribe to filters. See https://libera.chat/guides/registration"
return false
end
true
end
def load_subscriptions
$subscriptions = {}
$client.query('SELECT * FROM subscriptions').to_a.each do |row|
$subscriptions[row['project']] ||= {}
$subscriptions[row['project']][row['user']] ||= []
$subscriptions[row['project']][row['user']] << row['filter_id']
end
end
def validate_project(user, project)
if !CHANNELS.keys.include?(project)
user.send "'#{project}' is an invalid project or is currently unsupported."
user.send "Supported projects include: " + CHANNELS.keys.join(', ')
user.send "Contact musikanimal about requesting new projects."
return false
end
true
end
end
on :connect do
load_subscriptions
EM.run do
source = EventMachine::EventSource.new('https://stream.wikimedia.org/v2/stream/recentchange')
source.on "message" do |message|
data = JSON.parse(message)
if data['log_action'] == 'hit' && (CHANNELS.keys.include?(data['server_name']) || data['log_params']['filter'].to_s.include?('global'))
irc_wiki = data['log_params']['filter'].to_s.include?('global') ? 'meta.wikimedia.org' : data['server_name']
msg = "User:#{data['user'].score} tripped *filter-#{data['log_params']['filter']}* on [[#{data['title']}]]: " \
"https://#{data['server_name']}/wiki/Special:AbuseLog/#{data['log_params']['log']}"
Channel(CHANNELS[irc_wiki]).send(msg)
$subscriptions[irc_wiki] ||= {}
$subscriptions[irc_wiki].each do |user, filter_ids|
if filter_ids.include?(data['log_params']['filter'].to_s.scan(/\d+/)[0].to_i)
if user =~ /^#.*/
Channel(user).join
Channel(user).send(msg)
else
User(user).send(msg)
end
end
end
end
end
source.error do |error|
puts "EventStreams error: #{error}"
end
source.open do
puts "EventStreams connection opened"
end
source.start
end
end
on :message, "!ping" do |m|
m.reply 'Pong.'
end
# Channel subscribe
on :message, /^!subscribe (\w+\.\w+\.org) (\d+|all) #([#\w\-]+)$/ do |m, project, filter_id, channel|
return unless authed(m.user)
return unless validate_project(m.user, project)
channel = '#' + channel
Channel(channel).join
m.user.send "Please wait..."
# FIXME: Huge hack... need to somehow know when the bot actually joined.
sleep 2
unless Channel(channel).opped?(m.user)
Channel(channel).part
return m.user.send "You must be opped in #{channel} to subscribe filters to it."
end
statement = $client.prepare('INSERT IGNORE INTO subscriptions VALUES(NULL, ?, ?, ?)')
statement.execute(project, channel, filter_id.to_i)
load_subscriptions
m.user.send "#{channel} has been subscribed to filter #{filter_id} on #{project}"
Channel(channel).send("This channel has been subscribed to filter #{filter_id} on #{project}")
Channel(channel).send("To unsubscribe, ops can run `!unsubscribe [lang.project.org] [filter ID] [channel name]` or `!unsubscribe [lang.project.org] [channel name] all`")
end
# User subscribe
on :message, /^!subscribe (\w+\.\w+\.org) (\d+)$/ do |m, project, filter_id|
return unless authed(m.user)
return unless validate_project(m.user, project)
statement = $client.prepare('INSERT IGNORE INTO subscriptions VALUES(NULL, ?, ?, ?)')
statement.execute(project, m.user.nick, filter_id.to_i)
load_subscriptions
m.user.send "You have subscribed to filter #{filter_id} on #{project}"
m.user.send UNSUBSCRIBE_MSG
end
# Channel unsubscribe
on :message, /^!unsubscribe (\w+\.\w+\.org) (\d+|all) #([#\w\-]+)$/ do |m, project, filter_id, channel|
return unless authed(m.user)
return unless validate_project(m.user, project)
channel = '#' + channel
Channel(channel).join
m.user.send "Please wait..."
# FIXME: Huge hack... need to somehow know when the bot actually joined.
sleep 2
unless Channel(channel).opped?(m.user)
return m.user.send "You must be opped in #{channel} to manage subscriptions."
end
if 'all' == filter_id
statement = $client.prepare('DELETE FROM subscriptions WHERE project = ? AND user = ?')
statement.execute(project, channel)
m.user.send "#{channel} has unsubscribed to all filters on #{project}"
else
statement = $client.prepare('DELETE FROM subscriptions WHERE project = ? AND user = ? AND filter_id = ?')
statement.execute(project, channel, filter_id.to_i)
m.user.send "#{channel} has unsubscribed to filter #{filter_id} on #{project}"
end
load_subscriptions
Channel(channel).send("This channel has been unsubscribed to filter #{filter_id} on #{project}")
end
# User unsubscribe
on :message, /^!unsubscribe (\w+\.\w+\.org) (\d+|all)$/ do |m, project, filter_id|
return unless authed(m.user)
return unless validate_project(m.user, project)
if 'all' == filter_id
statement = $client.prepare('DELETE FROM subscriptions WHERE project = ? AND user = ?')
statement.execute(project, m.user.nick)
m.user.send "You have unsubscribed to all filters on #{project}"
else
statement = $client.prepare('DELETE FROM subscriptions WHERE project = ? AND user = ? AND filter_id = ?')
statement.execute(project, m.user.nick, filter_id.to_i)
m.user.send "You have unsubscribed to filter #{filter_id}"
end
load_subscriptions
end
on :message, /^!subscriptions( \w+\.\w+\.org)?/ do |m, project|
return unless authed(m.user)
if project.present?
project = project.strip
return unless validate_project(m.user, project)
statement = $client.prepare('SELECT filter_id FROM subscriptions WHERE project = ? AND user = ?')
subscriptions = statement.execute(project, m.user.nick).to_a.collect { |row| row['filter_id'] }.join(', ')
else
statement = $client.prepare('SELECT project, filter_id FROM subscriptions WHERE user = ?')
data = {}
statement.execute(m.user.nick).to_a.each do |row|
data[row['project']] ||= []
data[row['project']] << row['filter_id']
end
subscriptions = ''
data.each do |proj, filter_ids|
subscriptions += proj + ' = ' + filter_ids.join(', ') + '; '
end
subscriptions.chomp!('; ')
end
m.user.send "You are subscribed to the following filters: #{subscriptions}"
m.user.send UNSUBSCRIBE_MSG
end
end
bot.start
end
end
AbuseFilterIRC.run