-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.py
367 lines (313 loc) · 11.8 KB
/
app.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
# -*- coding: utf-8 -*-
from twisted.spread import pb
from twisted.internet import endpoints, task, defer
from os import environ
import functools
import treq
import lxml.html
import re
import Levenshtein
from urllib import parse as urlparse
import json
import sqlite3
import codecs
import simple_eval
import ipaddress
from twisted.logger import textFileLogObserver, globalLogPublisher, Logger
log = Logger()
class AppException(Exception):
pass
BLOCKLIST = [
ipaddress.IPv4Network('127.0.0.0/8'),
ipaddress.IPv4Network('192.168.0.0/16'),
ipaddress.IPv4Network('10.0.0.0/8'),
ipaddress.IPv4Network('172.16.0.0/12'),
ipaddress.IPv6Network('::1'),
ipaddress.IPv6Network('fe80::/10'),
]
config = {}
def acceptable_netloc(hostname):
try:
address = ipaddress.ip_address(hostname)
except ValueError:
if hostname == "localhost":
return False
else:
return True
else:
for network in BLOCKLIST:
if address in network:
return False
else:
return True
class UrlHandler(object):
TIMEOUT = 30
def __init__(self, max_body, parser_class,
accepted_mimes=("text/html",),
headers={"Accept-Language": "en-US",
"User-Agent": ("nanobot title fetching, contacts to"
"http://github.com/nanonyme/nanobot")
}):
self.max_body = max_body
self.bytes = 0
self.parser_class = parser_class
self.parser = None
self.accepted_mimes = accepted_mimes
self.headers = headers
def feed(self, data):
if self.bytes < self.max_body:
if len(data) > self.max_body - self.bytes:
data = data[:self.max_body - self.bytes]
data_len = len(data)
self.bytes += data_len
self.parser.feed(data)
else:
self.connection.cancel()
async def handle_response(self, response):
if response.code != 200:
raise AppException(f"Response code {response.code}")
try:
headers = response.headers.getRawHeaders("Content-Type")
except KeyError:
raise AppException("No Content-Type")
if not headers:
raise AppException("Empty Content-Type")
else:
header = headers[0]
log.info(f"Header line {header}")
mime, _, encoding = header.partition(";")
if encoding:
_, _, encoding = encoding.strip().partition("=")
try:
codecs.lookup(encoding)
except LookupError:
encoding = None
if mime not in self.accepted_mimes:
raise AppException(f"Mime {mime} not supported")
if encoding:
log.info(f"Using encoding {encoding} to handle response")
self.parser = self.parser_class()
await response.collect(self.feed)
return self.parser.close()
async def get_url(self, url):
return await treq.get(url, timeout=self.TIMEOUT, headers=self.headers)
async def get_title(self, url):
response = await self.get_url(url)
root = await self.handle_response(response)
title = root.xpath("//title")[0].text
if not title:
return ""
else:
return " ".join(title.split())
def difference_check(a, s):
if len(a) < 14 or len(s) < 14:
if len(a) != len(s):
return True
else:
return a != s
else:
return Levenshtein.distance(a, s) >= 7
def dynsearch(l, s):
a, b = l[0], l[1:]
if not b:
return difference_check(a, s)
else:
if not dynsearch(b, s):
return False
else:
return difference_check("".join(b), s)
def prepare_url(url):
path = urlparse.unquote(urlparse.urlparse(url).path).replace("-", "")
path = path.replace(" ", "").replace("+", "").replace("_", "").lower()
path = path.rstrip("0123456789")
return path.split("/")
def prepare_title(title):
title = title.replace("+", "").replace(" ", "").replace("_", "").lower()
return re.split("[-–]", title)[0]
class MessageHandler(object):
_URL_HANDLER_CLASS = UrlHandler
def __init__(self, reactor, hits, misses, callback, max_len):
self._reactor = reactor
self._hits = hits
self._misses = misses
self._max_len = max_len
self._callback = callback
async def success(self, title, url):
log.info(f"Got title {title}")
if dynsearch(prepare_url(url), prepare_title(title)):
log.info("Will try to send title as a message")
await self._callback("title: %s" % title)
await task.deferLater(self._reactor, 2, defer.succeed,
None)
def fail(self, url):
self._misses.update(url, "miss")
log.failure(f"Adding {url} to temporary block list")
async def find_links(self, message):
for m in re.finditer("(https?://[^ ]+)", message):
url = m.group(0)
if not acceptable_netloc(urlparse.urlparse(url).netloc):
continue
if self._misses.fetch(url):
log.info((f"Skipped title check for URL {url} because of "
"previous failures"))
continue
title = self._hits.fetch(url)
if title is None:
log.info(f"Cache miss for URL {url}")
handler = self._URL_HANDLER_CLASS(
max_body=2 * 1024 ** 2, parser_class=lxml.html.HTMLParser)
try:
title = await handler.get_title(url)
except Exception:
self.fail(url)
else:
if len(title) > self._max_len:
title = title[:self._max_len]
if title:
self._hits.update(url, title)
await self.success(title, url)
else:
log.info(f"Cache hit for URL {url}")
await self.success(title, url)
class UrlCache(object):
def __init__(self, reactor, expiration=60):
self._reactor = reactor
self._expiration = expiration
self._db = {}
self._reaper = task.LoopingCall(self._reap)
self._reaper.clock = reactor
def fetch(self, key):
try:
value = self._db[key]["value"]
except KeyError:
value = None
return value
def update(self, key, value):
self._db[key] = {"value": value,
"timestamp": self._reactor.seconds()}
def _valid(self):
for key, value in self._db.items():
if self._reactor.seconds() - value["timestamp"] < self._expiration:
yield key, value
def enable(self):
if not self._reaper.running:
self._reaper.start(self._expiration, False)
def disable(self):
if self._reaper.running:
self._reaper.stop()
def _reap(self):
self._db = dict(self._valid())
class API(pb.Referenceable):
STALENESS_LIMIT = 24*60*60
def __init__(self, reactor):
self.reactor = reactor
self.good_urls = UrlCache(self.reactor, expiration=3600)
self.good_urls.enable()
self.bad_urls = UrlCache(self.reactor, expiration=60)
self.bad_urls.enable()
def _staleness_check(self, timestamp):
if self.reactor.seconds() - timestamp > self.STALENESS_LIMIT:
log.info("Message stale, ignoring")
return True
else:
return False
def remote_handlePublicMessage(self, protocol, user, channel, message,
max_line_length, timestamp):
if self._staleness_check(timestamp):
return
try:
callback = functools.partial(
protocol.callRemote, "msg", channel)
roles = resolveRoles(user)
if "ignored" in roles:
return
if message.startswith("!"):
return handleCommand(protocol, user, roles, channel, message[1:],
max_line_length, callback)
else:
handler = MessageHandler(self.reactor, self.good_urls,
self.bad_urls, callback,
max_line_length)
return defer.ensureDeferred(handler.find_links(message))
except Exception:
log.failure("FIXME, runaway exception")
def remote_handlePrivateMessage(self, protocol, user, channel, message,
max_line_length, timestamp):
if self._staleness_check(timestamp):
return
channel, _, _ = user.partition("!")
return self.remote_handlePublicMessage(protocol, user, channel,
message,
max_line_length,
timestamp)
user_query = ("select roles.name from roles where roles.oid in "
"(select userroles.oid from (users natural join usermask)"
"natural join userroles where usermask.mask=?);")
def resolveRoles(user):
with sqlite3.connect(config["core"]["db"]) as conn:
cur = conn.cursor()
res = cur.execute(user_query, (user,))
return [role[0] for role in res.fetchmany()]
def handleCommand(protocol, user, roles, channel, message, max_line_length,
callback):
command, _, suffix = message.partition(" ")
if command == "reincarnate":
if "superadmin" in roles:
log.info("Restarting app")
reactor.stop()
else:
log.info("User {user} tried to do code reload", user=user)
elif command == "eval":
truth, expr = suffix.split(":")
truth = [s.strip() for s in truth.split(",")]
try:
ret = simple_eval.eval_bool(expr, truth)
except simple_eval.EvalError as e:
callback(str(e))
else:
callback("Result: %s" % ret)
elif command == "join":
channel, _, password = suffix.partition(" ")
if not password:
password = None
if "superadmin" in roles:
if password:
log.info(f"Joining {channel} ({password})")
else:
log.info(f"Joining {channel}")
return protocol.callRemote("join", channel, password)
elif command == "leave":
channel, _, reason = suffix.partition(" ")
if not reason:
reason = None
if "superadmin" in roles:
if reason:
log.info("Leaving {channel} ({reason})",
channel=channel, reason=reason)
else:
log.info(f"Leaving {channel}")
return protocol.callRemote("leave", channel, reason)
else:
log.info(f"Unrecognized command {command}")
def log_and_exit(ret, reactor):
log.failure("Critical failure, terminating application")
reactor.stop()
def register(root, reactor):
log.info("Registering app for bot")
return root.callRemote("register", API(reactor))
if __name__ == "__main__":
from twisted.internet import reactor
with open(environ["CONFIG"]) as f:
config.update(json.load(f))
f = open(config["core"]["log_file"], "a")
globalLogPublisher.addObserver(textFileLogObserver(f))
endpoint = endpoints.StandardIOEndpoint(reactor)
factory = pb.PBClientFactory()
d = endpoint.listen(factory)
@d.addCallback
def initialize(_):
d = factory.getRootObject()
d.addCallback(register, reactor)
d.addErrback(log_and_exit, reactor)
return
reactor.run()