forked from Charcoal-SE/SmokeDetector
-
Notifications
You must be signed in to change notification settings - Fork 0
/
globalvars.py
206 lines (166 loc) · 8.06 KB
/
globalvars.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
# -*- coding: utf-8 -*-
import sys
import os
from collections import namedtuple
from datetime import datetime
from html.parser import HTMLParser
from html import unescape
from hashlib import md5
from configparser import NoOptionError, RawConfigParser
import threading
# noinspection PyCompatibility
import regex
import subprocess as sp
import platform
from helpers import log
CommitInfo = namedtuple('CommitInfo', ['id', 'id_full', 'author', 'message'])
def git_commit_info():
try:
data = sp.check_output(['git', 'rev-list', '-1', '--pretty=%H%n%an%n%s', 'HEAD'],
stderr=sp.STDOUT).decode('utf-8')
except sp.CalledProcessError as e:
raise OSError("Git error:\n" + e.output) from e
_, full_id, author, message = data.strip().split("\n")
return CommitInfo(id=full_id[:7], id_full=full_id, author=author, message=message)
def git_ref():
git_cp = sp.run(['git', 'symbolic-ref', '--short', '-q', 'HEAD'], stdout=sp.PIPE)
return git_cp.stdout.decode("utf-8").strip() # not on branch = empty output
# We don't need strip_escape_chars() anymore, see commit message of 1931d30804a675df07887ce0466e558167feae57
# noinspection PyClassHasNoInit,PyDeprecation,PyUnresolvedReferences
class GlobalVars:
on_windows = 'windows' in platform.platform().lower()
false_positives = []
whitelisted_users = set()
blacklisted_users = dict()
blacklisted_usernames = []
blacklisted_websites = []
blacklisted_numbers = []
watched_numbers = []
blacklisted_numbers_normalized = None
watched_numbers_normalized = None
bad_keywords = []
watched_keywords = {}
ignored_posts = []
auto_ignored_posts = []
startup_utc_date = datetime.utcnow()
startup_utc = startup_utc_date.strftime("%H:%M:%S")
latest_questions = []
api_backoff_time = 0
deletion_watcher = None
metasmoke_last_ping_time = datetime.now()
not_privileged_warning = \
"You are not a privileged user. Please see " \
"[the privileges wiki page](https://charcoal-se.org/smokey/Privileges) for " \
"information on what privileges are and what is expected of privileged users."
experimental_reasons = { # Don't widely report these
"potentially bad keyword in answer",
"potentially bad keyword in body",
"potentially bad keyword in title",
"potentially bad keyword in username",
"potentially bad NS for domain in title",
"potentially bad NS for domain in body",
"potentially bad NS for domain in answer",
"potentially bad ASN for hostname in title",
"potentially bad ASN for hostname in body",
"potentially bad ASN for hostname in answer",
"potentially bad IP for hostname in title",
"potentially bad IP for hostname in body",
"potentially bad IP for hostname in answer",
"potentially problematic NS configuration in title",
"potentially problematic NS configuration in body",
"potentially problematic NS configuration in answer",
"toxic body detected",
"toxic answer detected",
}
parser = HTMLParser()
parser.unescape = unescape
code_privileged_users = None
# these are loaded in GlobalVars.reload()
commit = None
commit_with_author = None
on_branch = None
s = ""
s_reverted = ""
s_norestart = ""
s_norestart2 = ""
apiquota = -1
bodyfetcher = None
cookies = {}
se_sites = []
why_data = []
notifications = []
listen_to_these_if_edited = []
multiple_reporters = []
api_calls_per_site = {}
reason_weights = {}
metasmoke_ids = {}
standby_message = ""
standby_mode = False
api_request_lock = threading.Lock()
num_posts_scanned = 0
post_scan_time = 0
posts_scan_stats_lock = threading.Lock()
config_parser = RawConfigParser()
if os.path.isfile('config') and "pytest" not in sys.modules:
config_parser.read('config')
log('debug', "Configuration loaded from \"config\"")
else:
config_parser.read('config.ci')
if "pytest" in sys.modules and os.path.isfile('config'): # Another config found while running in pytest
log('debug', "Running in pytest, force load config from \"config.ci\"")
else:
log('debug', "Configuration loaded from \"config.ci\"")
config = config_parser["Config"] # It's a collections.OrderedDict now
# environ_or_none replaced by os.environ.get (essentially dict.get)
bot_name = os.environ.get("SMOKEDETECTOR_NAME", "SmokeDetector")
bot_repo_slug = os.environ.get("SMOKEDETECTOR_REPO", "Charcoal-SE/SmokeDetector")
bot_repository = "//github.com/{}".format(bot_repo_slug)
chatmessage_prefix = "[{}]({})".format(bot_name, bot_repository)
site_id_dict = {}
post_site_id_to_question = {}
location = config.get("location", "Continuous Integration")
metasmoke_ws = None
metasmoke_down = False
metasmoke_failures = 0 # Consecutive count, not cumulative
chatexchange_u = config.get("ChatExchangeU")
chatexchange_p = config.get("ChatExchangeP")
metasmoke_host = config.get("metasmoke_host")
metasmoke_key = config.get("metasmoke_key")
metasmoke_ws_host = config.get("metasmoke_ws_host")
github_username = config.get("github_username")
github_password = config.get("github_password")
perspective_key = config.get("perspective_key")
flovis_host = config.get("flovis_host")
flovis = None
# Miscellaneous
log_time_format = config.get("log_time_format", "%H:%M:%S")
valid_content = """This is a totally valid post that should never be caught. Any blacklist or watchlist item that triggers on this item should be avoided. java.io.BbbCccDddException: nothing wrong found. class Safe { perfect valid code(int float &#%$*v a b c =+ /* - 0 1 2 3 456789.EFGQ} English 中文Français Español Português Italiano Deustch ~@#%*-_/'()?!:;" vvv kkk www sss ttt mmm absolute std::adjacent_find (power).each do |s| bbb end ert zal l gsopsq kdowhs@ xjwk* %_sooqmzb xjwpqpxnf. Please don't blacklist disk-partition.com, it's a valid domain (though it also gets spammed rather frequently).""" # noqa: E501
@staticmethod
def reload():
GlobalVars.commit = commit = git_commit_info()
GlobalVars.commit_with_author = "`{}` ({}: {})".format(
commit.id, commit.author, commit.message)
GlobalVars.on_branch = git_ref()
GlobalVars.s = "[ {} ] SmokeDetector started at [rev {}]({}/commit/{}) (running on {}, Python {})".format(
GlobalVars.chatmessage_prefix, GlobalVars.commit_with_author, GlobalVars.bot_repository,
GlobalVars.commit.id, GlobalVars.location, platform.python_version())
GlobalVars.s_reverted = \
"[ {} ] SmokeDetector started in [reverted mode](" \
"https://charcoal-se.org/smokey/SmokeDetector-Statuses#reverted-mode) " \
"at [rev {}]({}/commit/{}) (running on {})".format(
GlobalVars.chatmessage_prefix, GlobalVars.commit_with_author, GlobalVars.bot_repository,
GlobalVars.commit.id, GlobalVars.location)
GlobalVars.s_norestart = "[ {} ] Blacklists reloaded at [rev {}]({}/commit/{}) (running on {})".format(
GlobalVars.chatmessage_prefix, GlobalVars.commit_with_author, GlobalVars.bot_repository,
GlobalVars.commit.id, GlobalVars.location)
GlobalVars.s_norestart2 = "[ {} ] FindSpam module reloaded at [rev {}]({}/commit/{}) (running on {})".format(
GlobalVars.chatmessage_prefix, GlobalVars.commit_with_author, GlobalVars.bot_repository,
GlobalVars.commit.id, GlobalVars.location)
GlobalVars.standby_message = \
"[ {} ] SmokeDetector started in [standby mode](" \
"https://charcoal-se.org/smokey/SmokeDetector-Statuses#standby-mode) " \
"at [rev {}]({}/commit/{}) (running on {})".format(
GlobalVars.chatmessage_prefix, GlobalVars.commit_with_author, GlobalVars.bot_repository,
GlobalVars.commit.id, GlobalVars.location)
log('debug', "GlobalVars loaded")
GlobalVars.reload()