forked from wikimedia-rights-watch/irc-bots
-
Notifications
You must be signed in to change notification settings - Fork 0
/
snitch.py
401 lines (351 loc) · 13.7 KB
/
snitch.py
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
#! /usr/bin/env python2.6
# Public domain; bjweeks, MZMcBride; 2011; Rschen7754, 2013
import collections
import re
import sqlite3
import sre_constants
import urlparse
from twisted.internet import protocol, reactor, task, defer
from twisted.python import log
from twisted.words.protocols import irc
import settings
'''
CREATE TABLE rules (
wiki text,
type text,
pattern text,
channel text,
ignore integer,
UNIQUE(wiki, type, pattern, channel, ignore)
);
CREATE TABLE channels(
name text,
UNIQUE(name)
);
'''
Rule = collections.namedtuple('Rule', 'wiki, type, pattern, channel, ignore')
COLOR_RE = re.compile(r'(?:\x02|\x03(?:\d{1,2}(?:,\d{1,2})?)?)')
CHANNEL_URLS = {'wikidata.wikipedia': 'www.wikidata',
'mediawiki.wikipedia': 'www.mediawiki',
'species.wikipedia': 'species.wikimedia',
'donate.wikimedia.org': 'donate.wikimedia',
'outreach.wikipedia': 'outreach.wikimedia',
'wikimania2013wiki': 'wikimania2013.wikimedia',
'wikimania2014wiki': 'wikimania2014.wikimedia',
'wikimediafoundation.org': 'wikimediafoundation',
}
def strip_formatting(message):
"""Strips colors and formatting from IRC messages"""
return COLOR_RE.sub('', message)
ACTION_RE = re.compile(r'\[\[(.+)\]\] (?P<log>.+) \* (?P<user>.+) \* (?P<summary>.+)')
DIFF_RE = re.compile(r'''
\[\[(?P<page>.*)\]\]\ # page title
(?P<patrolled>!|) # patrolled
(?P<new>N|) # new page
(?P<minor>M|) # minor edit
(?P<bot>B|)\ # bot edit
(?P<url>.*)\ # diff url
\*\ (?P<user>.*?)\ \*\ # user
\((?P<diff>(\+|-)\d*)\)\ # diff size
?(?P<summary>.*) # edit summary
''', re.VERBOSE)
class EternalClient(irc.IRCClient):
def __init__(self):
self.pingger = task.LoopingCall(self.pingServer)
def connectionMade(self):
irc.IRCClient.connectionMade(self)
self.pingger.start(60, now=False)
def connectionLost(self, reason):
irc.IRCClient.connectionLost(self, reason)
self.pingger.stop()
def pingServer(self):
self.sendLine('PING %s' % self.transport.connector.host)
log.msg('%s sent ping to %s'
% (self.nickname, self.transport.connector.host))
def irc_PONG(self, prefix, params):
log.msg('%s received ping from %s'
% (self.nickname, params[1]))
class Snatch(EternalClient):
realname = 'snerk'
nickname = '%s' % (settings.nickname)
def connectionMade(self):
EternalClient.connectionMade(self)
log.msg('Snatch connected')
self.factory.resetDelay()
self.factory.snatches.append(self)
self.channels = set()
self.cursor = self.factory.connection.cursor()
def connectionLost(self, reason):
EternalClient.connectionLost(self, reason)
log.msg('Snatch disconnected: %s' % reason.value)
self.factory.snatches.remove(self)
self.cursor.close()
def signedOn(self):
self.syncChannels()
def joined(self, channel):
log.msg('Snatch joined %s' % channel)
self.channels.add(channel)
def sleep(self, secs, channel):
d = self.join(channel)
return d
def left(self, channel):
log.msg('Snatch left %s' % channel)
self.channels.discard(channel)
def privmsg(self, user, channel, message):
wiki = channel[1:]
cleaned_message = strip_formatting(message)
edit_match = DIFF_RE.match(cleaned_message)
action_match = ACTION_RE.match(cleaned_message)
match = edit_match or action_match
if not match:
log.msg('%s was not matched' % repr(cleaned_message))
return
diff = match.groupdict()
self.cursor.execute(
'SELECT * FROM rules WHERE wiki=? ORDER BY ignore DESC', (wiki,))
rule_list = [Rule(*row) for row in self.cursor.fetchall()]
ignore = []
for rule in rule_list:
if rule.channel in ignore:
continue
pattern = re.compile(r'^%s$' % rule.pattern, re.I | re.U)
if rule.type == 'all':
pass
elif rule.type == 'summary':
if 'page' in diff:
if not pattern.search(diff['summary']):
continue
else:
if not pattern.search(diff['summary']):
continue
elif rule.type == 'user':
if not pattern.search(diff['user']):
continue
elif rule.type == 'page':
if 'page' in diff:
if not pattern.search(diff['page']):
continue
else:
if not pattern.search(diff['summary']):
continue
elif rule.type == 'log':
if 'log' in diff:
if not rule.pattern.lower() == diff['log']:
continue
else:
continue
if rule.ignore:
ignore.append(rule.channel)
else:
for snitch in self.factory.snitches:
snitch.tattle(rule, diff)
ignore.append(rule.channel)
def syncChannels(self):
log.msg('Syncing snatch\'s channels')
self.cursor.execute(
'SELECT wiki FROM rules')
channels = set('#%s' % row[0] for row in self.cursor.fetchall())
for channel in (channels - self.channels):
self.sleep(2, channel)
#self.join(channel)
[self.part(channel) for channel in (self.channels - channels)]
def quit(self):
irc.IRCClient.quit(self)
self.factory.stopTrying()
self.transport.loseConnection()
class Snitch(EternalClient):
realname = 'snitchbot'
nickname = '%s' % (settings.nickname)
password = ':%s %s' % (nickname, settings.nickserv_password)
lineRate = 1
def connectionMade(self):
EternalClient.connectionMade(self)
log.msg('Snitch connected')
self.factory.resetDelay()
self.channels = set()
self.factory.snitches.append(self)
self.cursor = self.factory.connection.cursor()
def connectionLost(self, reason):
EternalClient.connectionLost(self, reason)
log.msg('Snitch disconnected: %s' % reason.value)
self.factory.snitches.remove(self)
self.cursor.close()
def signedOn(self):
self.cursor.execute(
'SELECT name FROM channels')
for row in self.cursor.fetchall():
self.channels.add(row[0])
self.join(row[0])
def joined(self, channel):
log.msg('Snitch joined: %s' % channel)
self.channels.add(channel)
def left(self, channel):
log.msg('Snitch left %s' % channel)
self.channels.discard(channel)
def updateRules(self, channel, params, ignore=False, remove=False):
if len(params) < 2:
self.msg(channel,
'!(un)stalk wiki (page|user|summary|log|all) [pattern]')
return
wiki = params[0]
rule_type = params[1]
if rule_type not in ('summary', 'user', 'page', 'log', 'all'):
self.msg(channel,
'Type must be one of: all, user, summary, page, log.')
return
if rule_type == 'all':
pattern = ''
elif rule_type != 'all' and len(params) < 3:
self.msg(channel, 'That requires a pattern.')
return
else:
pattern = ' '.join(params[2:])
try:
re.compile(pattern)
except sre_constants.error:
self.msg(channel, 'Invalid pattern.')
return
self.cursor.execute(
'SELECT * FROM rules WHERE \
wiki=? AND type=? AND pattern=? AND channel=? AND ignore=?',
(wiki, rule_type, pattern, channel, ignore))
exists = self.cursor.fetchone()
if remove:
if exists:
self.cursor.execute(
'DELETE FROM rules \
WHERE wiki=? \
AND type=? \
AND pattern=? \
AND channel=? \
AND ignore=?',
(wiki, rule_type, pattern, channel, ignore))
self.msg(channel, 'Rule deleted.')
else:
self.msg(channel, 'No such rule.')
else:
if exists:
self.msg(channel, 'Rule already exists.')
else:
self.cursor.execute(
'INSERT OR REPLACE INTO rules VALUES (?,?,?,?,?)',
(wiki, rule_type, pattern, channel, ignore))
self.msg(channel, 'Rule added.')
for snatch in self.factory.snatches:
snatch.syncChannels()
def privmsg(self, sender, channel, message):
if not sender:
return # Twisted sucks
if channel == self.nickname:
pass
elif message.startswith('!'):
message = message[1:]
else:
return
user = sender.split('!', 1)[0]
hostmask = sender.split('@', 1)[1]
action = message.split(' ')[0]
params = message.split(' ')[1:]
if action == 'stalk':
self.updateRules(channel, params)
elif action == 'ignore':
self.updateRules(channel, params, ignore=True)
elif action == 'unstalk':
self.updateRules(channel, params, remove=True)
elif action == 'unignore':
self.updateRules(channel, params, ignore=True, remove=True)
elif action == 'list':
self.cursor.execute(
'SELECT * FROM rules WHERE channel=?', (channel,))
rules = [Rule(*row) for row in self.cursor.fetchall()]
[self.msg(user, '%s %s %s' % (r.wiki, r.type, r.pattern))
for r in rules]
elif action == 'listflood':
self.cursor.execute(
'SELECT * FROM rules WHERE channel=?', (channel,))
rules = [Rule(*row) for row in self.cursor.fetchall()]
[self.msg(channel, '%s %s %s' % (r.wiki, r.type, r.pattern))
for r in rules]
elif action == 'join':
if hostmask not in settings.authorized_users:
self.msg(channel, 'You are not authorized to do this.')
elif not params:
self.msg(channel, '!join (channel)')
else:
self.cursor.execute(
'INSERT OR IGNORE INTO channels VALUES (?)', (params[0],))
self.join(params[0])
elif action == 'part' or action == 'leave':
if hostmask not in settings.authorized_users:
self.msg(channel, 'You are not authorized to do this.')
else:
self.cursor.execute(
'DELETE FROM channels WHERE name=?', (channel,))
self.part(channel)
elif action == 'help':
self.msg(channel,
'!(stalk|ignore|unstalk|unignore|list|listflood|join|part|quit)')
elif action == 'quit':
if hostmask in settings.authorized_users:
log.msg('Quitting')
self.quit()
for snatch in self.factory.snatches:
snatch.quit()
else:
if hostmask in settings.authorized_users:
self.sendLine(message)
def quit(self):
irc.IRCClient.quit(self)
self.factory.stopTrying()
self.transport.loseConnection()
def tattle(self, rule, diff):
if rule.channel not in self.channels:
return
if 'page' in diff:
if not diff['summary']:
diff['summary'] = '[none]'
url = urlparse.urlparse(diff['url'])
fixed_netloc = CHANNEL_URLS.get(url.netloc.strip('.org'),
url.netloc.strip('.org'))+'.org'
fixed_url = diff['url'].replace(url.netloc, fixed_netloc)
final_url = fixed_url.replace('http://', 'https://')
self.msg(rule.channel,
' '.join(('\x0303%s\x0301' % diff['user'],
'edited \x0314[[\x0307%s\x0314]]\x0301:' % diff['page'],
'\x0310%s\x0301' %diff['summary'],
final_url)))
else:
base_url = CHANNEL_URLS.get(rule.wiki.strip('.org'),
rule.wiki.strip('.org'))
self.msg(rule.channel,
'%s %s; https://%s.org/wiki/Special:Log/%s'
% (diff['user'], diff['summary'], base_url, diff['log']))
class SnatchAndSnitch(protocol.ReconnectingClientFactory):
factories = 0
snatches = []
snitches = []
connection = None
@classmethod
def startFactory(cls):
if not cls.factories:
cls.connection = sqlite3.connect(settings.database)
cls.connection.text_factory = str
cls.factories += 1
@classmethod
def stopFactory(cls):
cls.factories -= 1
if not cls.factories:
cls.connection.commit()
cls.connection.close()
reactor.stop()
def main():
log.startLogging(open(settings.directory + 'snitch.log', 'w'))
snatch = SnatchAndSnitch()
snatch.protocol = Snatch
snitch = SnatchAndSnitch()
snitch.protocol = Snitch
reactor.connectTCP(settings.snatch_network, 6667, snatch)
reactor.connectTCP(settings.snitch_network, 6667, snitch)
reactor.run()
if __name__ == '__main__':
main()