forked from tripleee/sloshy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sloshy.py
executable file
·637 lines (541 loc) · 22 KB
/
sloshy.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
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
#!/usr/bin/env python3
"""
Sloshy the Thawman - main class
"""
from datetime import datetime, timedelta
from os import environ
import random
from time import sleep
import platform
import logging
import yaml
from chatexchange.client import Client as ChExClient, ChatActionError
from requests.exceptions import RequestException
from scrape_chat import Transcript, TranscriptFrozenDeletedException
DEFAULT_MAX_AGE = {'days': 12}
AGGRESSIVE_MAX_MSG_THRESHOLD = 15
DEFAULT_AGGRESSIVE_MAX_AGE = {'days': 5}
class SchemaError(Exception):
pass
class LocalClientRequestQueue:
"Quick hack to support logout() method"
def empty(self):
return True
class LocalClient:
"""
Simple mock ChatExchange.client with no actual networking functionality
"""
def __init__(self, host='stackexchange.com', email=None, password=None):
self.host = host
self.email = email
self.password = password
self.room = None
self._request_queue = LocalClientRequestQueue()
def login(self, email=None, password=None):
logging.info('local - not logging in to %s', self.host)
def get_room(self, id: int):
logging.info('local - not fetching %s/rooms/%s', self.host, id)
self.room = id
return self
def join(self):
assert self.room is not None
logging.info('local - not joining %s/rooms/%i', self.host, self.room)
def send_message(self, message: str):
assert self.room is not None
logging.info(
'local - %s/rooms/%i: not sending message', self.host, self.room)
if message:
print(message)
else:
print('(Not printing any message, just checking ability to join)')
def logout(self):
logging.info('local - %s/rooms/%i: logging out', self.host, self.room)
class Chatclients:
"""
Singleton to keep track of chat servers, with one client for each.
"""
def __init__(self, local=False):
"""
Create a dispatcher for the servers we know about.
With local=True, create a LocalClient instead of a real
chatexchange one.
"""
self.servers = dict()
self.email = None
self.password = None
self.local = local
def authnz(self, email: str, password: str):
self.email = email
self.password = password
def get_client_for_server(self, server: str) -> ChExClient:
assert self.email is not None
assert self.password is not None
if server not in self.servers:
assert server.startswith('chat.')
site = server[5:]
if self.local:
client = LocalClient(site)
else:
client = ChExClient(site)
client.login(self.email, self.password)
self.servers[server] = client
return self.servers[server]
def logout(self):
"""
Log out from all the chat clients.
"""
for server, client in self.servers.items():
logging.info("Logging out from %s", server)
client.logout()
queue = client._request_queue
while not queue.empty():
logging.info(
"Waiting for %s queue to drain (%i items in queue)",
server, queue.qsize())
sleep(15)
self.servers = dict()
class Room:
"""
Chat room with server, id, and a name for human-readable messages.
Also, include Sloshy's chat id for this server, and a handle to a
Chatclients object which instantiates clients as necessary.
"""
def __init__(
self,
server: str,
id: int,
name: str,
sloshy_id: int,
clients: Chatclients
):
self.server = server
self.id = id
self.name = name
self.log_id = 'https://%s/rooms/%i' % (server, id)
self.escaped_name = name.replace('[', '\[').replace(']', '\]')
self.sloshy_id = sloshy_id
self.clients = clients
self.homeroom = False
self._chatroom = None
def set_as_home_room(self):
self.homeroom = True
def is_home_room(self) -> bool:
return self.homeroom
def transcript_url(self) -> str:
return 'https://%s/transcript/%i' % (self.server, self.id)
def send_message(self, message: str) -> None:
if self._chatroom is None:
chat = self.clients.get_client_for_server(self.server)
room = chat.get_room(self.id)
room.join()
self._chatroom = room
else:
room = self._chatroom
if message:
room.send_message(message)
def get_sloshy_id(self) -> int:
return self.sloshy_id
class Sloshy:
def __init__(self, conffile=None, local=False):
"""
Create a Sloshy instance by parsing the supplied YAML file.
With local=True, don't connect to chat rooms.
"""
self.conffile = conffile
self.local = local
self.rooms = []
self.homeroom = None
self.chatclients = None
self.email = ''
self.password = ''
if conffile:
self.load_conf(conffile)
if 'SLOSHY_EMAIL' in environ:
self.email = environ.get('SLOSHY_EMAIL')
if 'SLOSHY_PASSWORD' in environ:
self.password = environ.get('SLOSHY_PASSWORD')
self.chatclients.authnz(self.email, self.password)
def load_conf(self, conffile=None):
"""
Load YAML config from self.conffile, or the given file if specified
"""
if conffile is None:
assert self.conffile is not None
conffile = self.conffile
with open(conffile, 'r') as filehandle:
config = yaml.safe_load(filehandle)
warning = None
if 'schema' not in config:
warning = 'Config file does not contain key "schema". ' \
' Run with --migrate?'
if config['schema'] != 20211215:
warning = 'Config file uses old schema %i; run with --migrate?' % (
config['schema'])
if warning is not None:
logging.error(warning)
raise SchemaError(warning)
assert 'servers' in config
self.config = config
self.rooms = []
self.homeroom = None
if not self.local and 'local' in config:
self.local = bool(config['local'])
clients = Chatclients(local=self.local)
self.chatclients = clients
for server in self.config['servers']:
if server in self.chatclients.servers:
logging.warning(
'Skipping duplicate server %s in %s', server, conffile)
continue
assert 'sloshy_id' in self.config['servers'][server]
sloshy_id = self.config['servers'][server]['sloshy_id']
assert 'rooms' in self.config['servers'][server]
seen = set()
for room in self.config['servers'][server]['rooms']:
assert 'id' in room
assert 'name' in room
if room['id'] in seen:
logging.warning(
'Skipping duplicate room %s:%s (%s) in %s',
server, room['id'], room['name'], conffile)
continue
seen.add(room['id'])
roomobj = Room(
server, room['id'], room['name'], sloshy_id, clients)
self.rooms.append(roomobj)
if 'role' in room and room['role'] == 'home':
assert self.homeroom is None
self.homeroom = roomobj
roomobj.set_as_home_room()
assert self.homeroom is not None
if 'auth' in config:
self.email = config['auth']['email']
self.password = config['auth']['password']
# Gripe a bit, we don't want users to embed authnz in the file
logging.warning('Chat username and password read from config')
def migrate(self):
"""
Rewrite older-format configuration file using new YAML schema.
The meat is in the migrated_config method which reads the old
config and returns the new format.
"""
updated_config = self.migrated_config()
with open(self.conffile, 'w', encoding='utf-8') as newconf:
yaml.dump(updated_config, newconf)
def migrated_config(self):
"""
No-op in the base class. Subclasses should inherit and provide
a migration path to the new base class.
"""
warning = 'Already at the newest version of the schema %s' % (
self.config['schema'])
logging.error(warning)
raise SchemaError(warning)
# return self.config
def nodename(self):
"""
Produce a string which identifies where Sloshy is running,
for the startup message in the monitoring room.
If the configuration file contains a node name, use that;
otherwise, return platform.node()
"""
if 'nodename' not in self.config:
self.config['nodename'] = platform.node()
return self.config['nodename']
def generate_chat_message(self):
"""
Output a random witty, witty unfreeze message.
Starting with PR#23 (December 2021), include a tag to make these
easier to search for.
"""
return '[tag:unfreeze] ' + random.choice((
'thaw',
'sprinkling antifreeze',
'!freeze',
'♫ the heat never bothered me anyway🎶',
'Kilr^WSloshy the Thawman was here!'))
def send_chat_message(self, room: Room, message: str):
"""
Send chat message. If not self.local, sleep after sending.
"""
assert self.chatclients is not None
room.send_message(message)
if not self.local:
sleep(3)
def startup_notice(self, room: Room, message: str):
"""
Send a startup message to the specified room.
The startup message has a link to the Github repo and
includes the output from self.nodename().
"""
self.send_chat_message(
room, '[Sloshy](%s) startup: %s on %s' % (
'https://github.com/tripleee/sloshy',
message,
self.nodename()))
def notice(self, room: Room):
"""
Drop a thawing notice in room
"""
self.send_chat_message(room, self.generate_chat_message())
def log_notice(self, message: str, log_emit=logging.info):
"""
Emit a log message to the home room, and as a warning
"""
log_emit(message)
self.send_chat_message(self.homeroom, message)
log_warn = lambda self, x: self.log_notice(x, log_emit=logging.warning)
log_error = lambda self, x: self.log_notice(x, log_emit=logging.error)
def test_rooms(self, announce=None):
"""
Test write access in all the rooms in the configuration.
If announce is missing / None, simply try to enter each room
in turn, but don't post any message.
Otherwise, check if Sloshy has already has posted a message to
each room in turn; if we have, regard it as tested. If not,
attempt to write the announcement message to the room in question.
"""
fetcher = Transcript()
counter = {'server': set(), 'id': set(), 'fail': set()}
self.startup_notice(self.homeroom, announce or "room test")
for room in self.rooms:
sloshy_id = room.get_sloshy_id()
if announce is None:
logging.info('Joining %s', room.log_id)
try:
room.send_message("") # just join, don't send anything
except KeyError as exception:
# the immediate problem from a closed room is
# ...
# File "./sloshy.py", line 156, in send_message
# room.join()
# File ".../chatexchange/rooms.py", line 50, in join
# return self._client._join_room(self.id)
# File ".../chatexchange/client.py", line 340, in _join_room
# self._br.join_room(room_id)
# File ".../chatexchange/browser.py", line 265, in join_room
# eventtime = response.json()['time']
# KeyError: 'time'
self.log_error(
'** Error: could not join %s' % room.log_id)
self.log_error(repr(exception))
counter['fail'].add(room.log_id)
counter['server'].add(room.server)
counter['id'].add(room.log_id)
continue
found = None
for phrase in (
'tagged/unfreeze',
# Fall back to static search for keywords in legacy notices
'thaw', 'antifreeze', 'freeze', 'heat', 'thawman'):
try:
found = fetcher.search(
room.server, room.id, sloshy_id, phrase)
except RequestException as exception:
self.log_error(
'** Error: could not fetch transcript for %s'
% room.log_id)
self.log_error(repr(exception))
counter['fail'].add(room.log_id)
break
if found:
logging.info('Found: %s', found)
break
# Sleeps established experimentally
# The site starts emitting 409 errors if we go too fast
# but it's not really clear what it regards as too fast
logging.info('Sleeping between searches ...')
sleep(3)
logging.info('Sleeping between searches ...')
sleep(3)
if not found:
try:
self.send_chat_message(room, '[tag:unfreeze] %s' % announce)
except KeyError as exception:
self.log_error(
'** Error: could not announce presence in %s'
% room.log_id)
self.log_error(repr(exception))
counter['fail'].add(room.log_id)
continue
self.log_notice(
'announced presence in %s' % room.log_id)
if announce is None:
self.log_notice(
'scanned %i rooms on %i servers' % (
len(counter['id']), len(counter['server'])))
if len(counter['fail']) > 0:
raise ValueError('failed to process rooms %s' % counter['fail'])
def scan_rooms(self, startup_message=None):
"""
Main entry point for scanning: Visit room transcripts,
check if they are in danger of being frozen; if so, join
and post a message.
The startup_message is included in the notification in the
monitoring room when Sloshy starts up.
If it is empty, missing, or None, it defaults to "manual run".
"""
if not startup_message:
startup_message = "manual run"
now = datetime.now()
failures = set()
# Default freeze schedule is 14 days; thaw a little before that,
# just to be on the safe side.
if 'threshold' in self.config:
if isinstance(self.config['threshold'], str):
maxage = timedelta(*tuple(
int(x.strip())
for x in self.config['threshold'].split(",")))
else:
maxage = timedelta(self.config['threshold'])
else:
maxage = timedelta(**DEFAULT_MAX_AGE)
max_aggressive_age = timedelta(**DEFAULT_AGGRESSIVE_MAX_AGE)
fetcher = Transcript()
homeroom = self.homeroom
self.startup_notice(homeroom, startup_message)
for room in self.rooms:
if room.is_home_room() and 'scan_homeroom' not in self.config:
continue
try:
room_latest = fetcher.latest(room.id, room.server)
except (TranscriptFrozenDeletedException, RequestException
) as exception:
self.log_error(
'** Error: could not fetch transcript for %s'
% room.log_id)
self.log_error(repr(exception))
failures.add(room.log_id)
continue
if room_latest:
when = room_latest['when']
age = now-when
# Trim microseconds
age = age - timedelta(microseconds=age.microseconds)
msg = '[%s](%s): latest activity %s (%s hours ago)' % (
room.escaped_name, room_latest['url'], when, age)
else:
msg = '[%s](%s): no non-feed, non-admin activity ever' % (
room.escaped_name, room.transcript_url())
age = maxage + timedelta(days=1)
self.log_notice(msg)
quiet = False
if age <= maxage and age > max_aggressive_age:
activity = fetcher.usercount(
room.id, room.server, userlimit=2,
messagelimit=AGGRESSIVE_MAX_MSG_THRESHOLD)
quiet = not activity
if age > maxage or (quiet and age > max_aggressive_age):
try:
self.notice(room)
self.log_notice(
'%s: Age threshold exceeded; sending a thawing notice'
% room.escaped_name)
except ChatActionError as err:
self.log_error(
'%s: Age threshold exceeded, but failed to thaw: %s'
% (room.log_id, err))
failures.add(room.id)
if failures:
raise ValueError('Failed to process %i rooms: %s' % (
len(failures), failures))
def perform_scan(self, startup_message=None):
"""
Entry point for a single scan: Perform room scan, then logout().
If startup_message is given, provide this as the identifying message
for Sloshy.
"""
try:
self.scan_rooms(startup_message)
except Exception as exception:
raise
finally:
self.chatclients.logout()
class SloshyLegacyConfig20211215(Sloshy):
"""
Child class with the original configuration file processing
and a migration method for updating to the new format.
"""
def load_conf(self, conffile=None):
"""
Load YAML config from self.conffile, or the given file if specified
"""
if conffile is None:
assert self.conffile is not None
conffile = self.conffile
with open(conffile, 'r') as filehandle:
config = yaml.safe_load(filehandle)
assert 'rooms' in config
self.config = config
self.rooms = []
self.homeroom = None
if not self.local and 'local' in config:
self.local = bool(config['local'])
clients = Chatclients(local=self.local)
self.chatclients = clients
for item in self.config['rooms']:
for server, rooms in item.items():
if server in self.chatclients.servers:
logging.warning(
'Duplicate server %s in %s', server, conffile)
seen = set()
for idx in range(len(item[server])):
room = item[server][idx]
assert 'id' in room
assert 'name' in room
if room['id'] in seen:
logging.warning(
'Skipping duplicate room %s:%s (%s) in %s',
server, room['id'], room['name'], conffile)
continue
seen.add(room['id'])
roomobj = Room(server, room['id'], room['name'], clients)
self.rooms.append(roomobj)
if 'role' in room and room['role'] == 'home':
assert self.homeroom is None
self.homeroom = roomobj
roomobj.set_as_home_room()
assert self.homeroom is not None
if 'auth' in config:
self.email = config['auth']['email']
self.password = config['auth']['password']
# Gripe a bit, we don't want users to embed authnz in the file
logging.warning('Chat username and password read from config')
def migrated_config(self):
# Stupidly hard-code information which should be in the config
sloshy_id = {
'chat.stackoverflow.com': 16115299,
'chat.stackexchange.com': 514718,
'chat.meta.stackexchange.com': 1018361
}
servers = []
for serverdict in self.config['rooms']:
servers.extend(serverdict.keys())
for server in servers:
if server in sloshy_id:
continue
logging.warning(
'Server %s not known to migration code - abort', server)
assert all(x in sloshy_id for x in servers)
serverconfigs = dict()
for serverdict in self.config['rooms']:
for server in serverdict.keys():
serverconfigs[server] = {
'sloshy_id': sloshy_id[server],
'rooms': serverdict[server]
}
return {'schema': 20211215, 'servers': serverconfigs}
def main():
from sys import argv
logging.basicConfig(level=logging.INFO)
if len(argv) > 1 and argv[1] == '--migrate':
SloshyLegacyConfig20211215(
argv[2] if len(argv) > 2 else "sloshy.yaml").migrate()
exit(0)
me = Sloshy(argv[1] if len(argv) > 1 else "test.yaml")
if len(argv) > 2 and argv[2] in ('--announce', '--test-rooms'):
me.test_rooms(argv[3] if len(argv) > 3 else None)
else:
me.perform_scan(argv[2] if len(argv) > 2 else None)
if __name__ == '__main__':
main()