forked from Charcoal-SE/SmokeDetector
-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.py
252 lines (198 loc) · 7.78 KB
/
helpers.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
# coding=utf-8
import os
import sys
import traceback
from datetime import datetime
import importlib
import threading
from termcolor import colored
import requests
import regex
from glob import glob
import sqlite3
def exit_mode(*args, code=0):
args = set(args)
if not (args & {'standby', 'no_standby'}):
from globalvars import GlobalVars
standby = 'standby' if GlobalVars.standby_mode else 'no_standby'
args.add(standby)
with open("exit.txt", "w") as f:
print("\n".join(args), file=f)
os._exit(code)
class ErrorLogs:
DB_FILE = "errorLogs.db"
# SQLite threading limitation !?!?!?
db = sqlite3.connect(DB_FILE)
if db.execute("SELECT name FROM sqlite_master WHERE type='table' AND name='error_logs'").fetchone() is None:
# Table 'error_logs' doesn't exist
db.execute("CREATE TABLE error_logs (time REAL PRIMARY KEY ASC, classname TEXT, message TEXT, traceback TEXT)")
db.commit()
db.close()
db_conns = {}
@classmethod
def get_db(cls):
thread_id = threading.get_ident()
if thread_id not in cls.db_conns:
cls.db_conns[thread_id] = sqlite3.connect(cls.DB_FILE)
return cls.db_conns[thread_id]
@classmethod
def add(cls, time, classname, message, traceback):
db = cls.get_db()
db.execute("INSERT INTO error_logs VALUES (?, ?, ?, ?)",
(time, classname, message, traceback))
db.commit()
@classmethod
def fetch_last(cls, n):
db = cls.get_db()
cursor = db.execute("SELECT * FROM error_logs ORDER BY time DESC LIMIT ?", (int(n),))
data = cursor.fetchall()
return data
@classmethod
def truncate(cls, n=100):
"""
Truncate the DB and keep only N latest exceptions
"""
db = cls.get_db()
cursor = db.execute(
"DELETE FROM error_logs WHERE time IN "
"(SELECT time FROM error_logs ORDER BY time DESC LIMIT -1 OFFSET ?)", (int(n),))
db.commit()
data = cursor.fetchall()
return data
class Helpers:
min_log_level = 0
def escape_format(s):
return s.replace("{", "{{").replace("}", "}}")
def expand_shorthand_link(s):
s = s.lower()
if s.endswith("so"):
s = s[:-2] + "stackoverflow.com"
elif s.endswith("se"):
s = s[:-2] + "stackexchange.com"
elif s.endswith("su"):
s = s[:-2] + "superuser.com"
elif s.endswith("sf"):
s = s[:-2] + "serverfault.com"
elif s.endswith("au"):
s = s[:-2] + "askubuntu.com"
return s
# noinspection PyMissingTypeHints
def log(log_level, *args, f=False):
levels = {
'debug': [0, 'grey'],
'info': [1, 'cyan'],
'warning': [2, 'yellow'],
'warn': [2, 'yellow'],
'error': [3, 'red']
}
level = levels[log_level][0]
if level < Helpers.min_log_level:
return
color = levels[log_level][1] if log_level in levels else 'white'
log_str = "{} {}".format(colored("[{}]".format(datetime.now().isoformat()[11:-3]),
color, attrs=['bold']),
" ".join([str(x) for x in args]))
print(log_str, file=sys.stderr)
if level == 3:
exc_tb = sys.exc_info()[2]
print("".join(traceback.format_tb(exc_tb)), file=sys.stderr)
if f: # Also to file
log_file(log_level, *args)
def log_file(log_level, *args):
levels = {
'debug': 0,
'info': 1,
'warning': 2,
'error': 3,
}
if levels[log_level] < Helpers.min_log_level:
return
log_str = "[{}] {}: {}".format(datetime.utcnow().strftime("%Y-%m-%d %H:%M:%S"), log_level.upper(),
" ".join([str(x) for x in args]))
with open("errorLogs.txt", "a", encoding="utf-8") as f:
print(log_str, file=f)
def log_exception(exctype, value, tb, f=False):
now = datetime.utcnow()
tr = ''.join(traceback.format_tb(tb))
exception_only = ''.join(traceback.format_exception_only(exctype, value)).strip()
logged_msg = "{exception}\n{now} UTC\n{row}\n\n".format(exception=exception_only, now=now, row=tr)
log('error', logged_msg, f=f)
ErrorLogs.add(now.timestamp(), exctype.__name__, str(value), tr)
def log_current_exception(f=False):
log_exception(*sys.exc_info(), f)
def files_changed(diff, file_set):
changed = set(diff.split())
return bool(len(changed & file_set))
core_files = {
"apigetpost.py", "blacklists.py", "bodyfetcher.py", "chatcommands.py", "chatcommunicate.py",
"chatexchange_extension.py", "datahandling.py", "deletionwatcher.py", "excepthook.py", "flovis.py",
"gitmanager.py", "globalvars.py", "helpers.py", "metasmoke.py", "nocrash.py", "parsing.py",
"spamhandling.py", "socketscience.py", "tasks.py", "ws.py",
"classes/feedback.py", "classes/_Git_Windows.py", "classes/__init__.py", "classes/_Post.py",
# Before these are made reloadable
"rooms.yml",
}
reloadable_modules = {
"findspam.py",
}
module_files = core_files | reloadable_modules
def only_blacklists_changed(diff):
return not files_changed(diff, module_files)
def only_modules_changed(diff):
return not files_changed(diff, core_files)
def reload_modules():
result = True
for s in reloadable_modules:
s = s.replace(".py", "") # Relying on our naming convention
try:
# Some reliable approach
importlib.reload(sys.modules[s])
except (KeyError, ImportError):
result = False
return result
# FAIR WARNING: Sending HEAD requests to resolve a shortened link is generally okay - there aren't
# as many exploits that work on just HEAD responses. If you specify sending a GET request, you
# acknowledge that this will fetch the full, potentially unsafe response from the shortener.
def unshorten_link(url, request_type='HEAD', explicitly_ignore_security_warning=False):
requesters = {
'GET': requests.get,
'HEAD': requests.head
}
if request_type not in requesters:
raise KeyError('Unavailable request_type {}'.format(request_type))
if request_type == 'GET' and not explicitly_ignore_security_warning:
raise SecurityError('Potentially unsafe request type GET not acknowledged')
requester = requesters[request_type]
response_code = 301
headers = {'User-Agent': 'SmokeDetector/git (+https://github.com/Charcoal-SE/SmokeDetector)'}
while response_code in {301, 302, 303, 307, 308}:
res = requester(url, headers=headers)
response_code = res.status_code
if 'Location' in res.headers:
url = res.headers['Location']
return url
pcre_comment = regex.compile(r"\(\?#(?<!(?:[^\\]|^)(?:\\\\)*\\\(\?#)[^)]*\)")
def blacklist_integrity_check():
bl_files = glob('bad_*.txt') + glob('blacklisted_*.txt') + glob('watched_*.txt')
seen = dict()
errors = []
for bl_file in bl_files:
with open(bl_file, 'r') as lines:
for lineno, line in enumerate(lines, 1):
if line.endswith('\r\n'):
errors.append('{0}:{1}:DOS line ending'.format(bl_file, lineno))
elif not line.endswith('\n'):
errors.append('{0}:{1}:No newline'.format(bl_file, lineno))
elif line == '\n':
errors.append('{0}:{1}:Empty line'.format(bl_file, lineno))
elif bl_file.startswith('watched_'):
line = line.split('\t')[2]
line = pcre_comment.sub("", line)
if line in seen:
errors.append('{0}:{1}:Duplicate entry {2} (also {3})'.format(
bl_file, lineno, line.rstrip('\n'), seen[line]))
else:
seen[line] = '{0}:{1}'.format(bl_file, lineno)
return errors
class SecurityError(Exception):
pass