-
Notifications
You must be signed in to change notification settings - Fork 0
/
rdlist_commands.py
314 lines (262 loc) · 14.6 KB
/
rdlist_commands.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
import os
import subprocess
import json
import time
import asyncio
import user_commands
import room_commands
import report_commands
import hardcoded_variables
def testing_mode_warning():
print("\nWARNING! Testing mode is enabled, this will:\n\n- Reduce the amount of data collected in user reports.\n- Slow down rdlist blocking/purging.\n- Prevent the deactivation of accounts.\n- Send incident reports to yourself instead of other homeserver admins.\n")
def sync_rdlist():
rdlist_dir = os.path.expanduser(hardcoded_variables.rdlist_dir)
os.makedirs(rdlist_dir, exist_ok=True)
# Check if the rdlist repo has already been cloned
if os.path.isdir(os.path.join(rdlist_dir, ".git")):
print("\nrdlist repo already cloned...")
# Update git remote references and get status
subprocess.run(["git", "-C", rdlist_dir, "remote", "update"], check=True)
status = subprocess.run(["git", "-C", rdlist_dir, "status", "-uno"], stdout=subprocess.PIPE, check=True)
# If "Your branch is up to date" is not in the status, then there are changes to pull
if "Your branch is up to date" not in status.stdout.decode():
print("Pulling latest changes from rdlist repo...")
subprocess.run(["git", "-C", rdlist_dir, "pull"], check=True)
else:
print("rdlist repo is up-to-date, no need to pull changes.")
else:
print("Cloning rdlist repo...")
subprocess.run(["git", "clone", "ssh://[email protected]:1488/loj/rdlist.git", rdlist_dir], check=True)
# A function to return the rdlist tags associated with a room
def get_rdlist_tags(preset_internal_ID):
if preset_internal_ID == '':
internal_ID = input("\nEnter the internal id of the room you wish to query (Example: !OLkDvaYjpNrvmwnwdj:matrix.org): ")
elif preset_internal_ID != '':
internal_ID = preset_internal_ID
# Git clone the rdlist repo to specified directory
sync_rdlist()
# Expand the user in the path and load the summaries JSON file
summaries_dir = os.path.expanduser(hardcoded_variables.rdlist_dir)
summaries_path = os.path.join(summaries_dir, "dist", "summaries.json")
with open(summaries_path, 'r') as file:
data = json.load(file)
# Find the room with the given id and return its tags
for item in data:
if 'room' in item and 'room_id' in item['room'] and item['room']['room_id'] == internal_ID:
if 'report_info' in item and 'tags' in item['report_info']:
return item['report_info']['tags']
return None
def get_key_rdlist_info(rdlist_tags):
# Expand the user in the path and load the summaries JSON file
summaries_dir = os.path.expanduser(hardcoded_variables.rdlist_dir)
summaries_path = os.path.join(summaries_dir, "dist", "summaries.json")
with open(summaries_path, 'r') as file:
data = json.load(file)
# Create an empty dictionary to store all the room_ids for each user
all_local_users = dict()
all_remote_users = dict()
# Create a set to store all room_ids
all_room_ids = set()
# Create a dictionary to store the tags for each room
room_tags = dict()
print("\nCalculating local and remote users in rdlist rooms... (This may take a while, please wait.)")
# Iterate over the provided rdlist_tags
for tag in rdlist_tags:
# Filter the data to keep only the entries where the tag appears in the "tags" list
filtered_data = [item for item in data if 'report_info' in item and 'tags' in item['report_info'] and tag in item['report_info']['tags']]
# Store the tags for each room
for item in filtered_data:
if 'room' in item and 'room_id' in item['room']:
room_id = item['room']['room_id']
all_room_ids.add(room_id) # add the room_id to the set
if room_id not in room_tags:
room_tags[room_id] = set()
room_tags[room_id].update(item['report_info']['tags'])
# Extract the room_ids
room_ids = [item['room']['room_id'] for item in filtered_data if 'room' in item and 'room_id' in item['room']]
# Examine these room_ids for local and remote users
for room_id in room_ids:
joined_members = room_commands.get_room_members(room_id)
# For each user, add the room_id and tags to the dictionary
for user_id in joined_members:
if user_id.endswith(hardcoded_variables.base_url):
if user_id not in all_local_users:
all_local_users[user_id] = dict()
all_local_users[user_id][room_id] = list(room_tags.get(room_id, []))
else:
if user_id not in all_remote_users:
all_remote_users[user_id] = dict()
all_remote_users[user_id][room_id] = list(room_tags.get(room_id, []))
all_room_ids = list(all_room_ids) # convert the set to a list
return all_room_ids, all_local_users, all_remote_users
def collect_user_reports_on_rdlist_accounts(all_local_users=None, skip_input=False):
# Print warning if testing mode is enabled
if hardcoded_variables.testing_mode == True:
testing_mode_warning()
# If all_local_users is None, then we need to generate it
if all_local_users == None:
# Git clone the rdlist repo to specified directory
sync_rdlist()
all_room_ids, all_local_users, all_remote_users = get_key_rdlist_info(hardcoded_variables.rdlist_recommended_tags)
# If there's at least 1 local user detected, ask the admin if they want to generate a user report for every user found in rdlist rooms
if len(all_local_users) > 0:
print(f"\nWARNING! The following local users are current members of rooms tagged in rdlist: {list(all_local_users.keys())}")
if skip_input == False:
generate_user_report_confirmation = input("\nDo you want to generate a user report file for each of these users? y/n? ")
if generate_user_report_confirmation.lower() in ['y', 'yes', 'Y', 'Yes'] or skip_input == True:
for user_id in all_local_users:
# Generate report_dict for each user
report_content = report_commands.generate_rdlist_report_summary(all_local_users[user_id], user_id)
report_commands.generate_user_report(user_id, report_content)
elif generate_user_report_confirmation.lower() in ['n', 'no', 'N', 'No']:
print("\nSkipping user report generation...")
elif len(all_local_users) == 0:
print(f"\nNo local users were found in rdlist rooms.")
def send_incident_reports_on_rdlist_accounts(all_remote_users=None, skip_input=False):
# Print warning if testing mode is enabled
if hardcoded_variables.testing_mode == True:
testing_mode_warning()
# If all_remote_users is None, then we need to generate it
if all_remote_users == None:
# Git clone the rdlist repo to specified directory
sync_rdlist()
all_room_ids, all_local_users, all_remote_users = get_key_rdlist_info(hardcoded_variables.rdlist_recommended_tags)
# If there's at least 1 remote user detected, ask the admin if they want to generate a user report for every user found in rdlist rooms
if len(all_remote_users) > 0:
print(f"\nWARNING! The following remote users are current members of rooms tagged in rdlist: {list(all_remote_users.keys())}")
if skip_input == False:
send_incident_report_confirmation = input("\nDo you want to send out incident reports for these users to every homeserver admin involved? y/n? ")
if send_incident_report_confirmation.lower() in ['y', 'yes', 'Y', 'Yes'] or skip_input == True:
loop = asyncio.get_event_loop()
loop.run_until_complete(report_commands.send_incident_reports(all_remote_users))
elif send_incident_report_confirmation.lower() in ['n', 'no', 'N', 'No']:
print("\nSkipping incident report generation...")
elif len(all_remote_users) == 0:
print(f"\nNo remote users were found in rdlist rooms.")
def block_all_rooms_with_rdlist_tags(rdlist_use_recommended,preset_user_ID,preset_new_room_name,preset_message):
# Git clone the rdlist repo to specified directory
sync_rdlist()
if rdlist_use_recommended == True:
# Use the hardcoded recommended tags
rdlist_tags = hardcoded_variables.rdlist_recommended_tags
print(f"\nUsing recommended rdlist tags. Rooms matching the following tags will be purged and/or blocked:\n{hardcoded_variables.rdlist_recommended_tags}")
elif rdlist_use_recommended == False:
# Expand the user in the path and read the file into a string
rdlist_dir = os.path.expanduser(hardcoded_variables.rdlist_dir)
rdlist_path = os.path.join(rdlist_dir, "lib", "docs", "tags.md")
with open(rdlist_path, 'r') as file:
data = file.readlines()
# Print rdlist/lib/docs/tags.md README file for the user
print("\nPrinting details about the current tags in rdlist:\n")
for line in data:
print(line, end='') # Print the contents of the file
# Take input from the user and convert it to a list
print("\nPlease enter a space seperated list of tags you wish to block:\n")
rdlist_tags = input().split()
print('')
all_room_ids, all_local_users, all_remote_users = get_key_rdlist_info(rdlist_tags)
#print(f"\nDEBUG all_local_users: {all_local_users}")
#print(f"DEBUG all_remote_users: {all_remote_users}")
#print(f"DEBUG all_room_ids: {all_room_ids}")
# If there's at least 1 local user detected, ask the admin if they want to generate a user report for every user found in rdlist rooms
collect_user_reports_on_rdlist_accounts(all_local_users, False)
# If there's at least 1 remote user detected, ask the admin if they want to generate a incident report for every user homeserver involved in rdlist rooms
send_incident_reports_on_rdlist_accounts(all_remote_users, False)
# Ask the user if they wish to block and purge all these rooms, then collect shutdown parameters
if preset_user_ID == '':
user_ID = input("\nPlease enter the local username that will create a 'muted violation room' for your users (Example: michael): ")
elif preset_user_ID != '':
user_ID = preset_user_ID
if preset_new_room_name == '':
new_room_name = input("\nPlease enter the room name of the muted violation room your users will be sent to: ")
elif preset_new_room_name != '':
new_room_name = preset_new_room_name
if preset_message == '':
message = input("\nPlease enter the shutdown message that will be displayed to users: ")
elif preset_message != '':
message = preset_message
# Ask the user if they wish to block and purge all these rooms
shutdown_confirmation = input("\nNumber of rdlist rooms being shutdown: " + str(len(all_room_ids)) + "\n\nAre you sure you want to block/shutdown these rooms? y/n? ")
total_list_kicked_users = []
num_rooms_blocked = 0
num_rooms_purged = 0
if shutdown_confirmation.lower() in ['y', 'yes', 'Y', 'Yes']:
# Ask the user if they wish to shadow ban all local users in these rooms
shadow_ban_confirmation = input("\nDo you want to also shadow ban all your local users in these rooms before performing these shutdowns? (This is recommended as it prevents them from alerting others about these mass shutdown.) y/n? ")
# Perform shadow bans if admin confirms
if shadow_ban_confirmation in ['y', 'yes', 'Y', 'Yes']:
for user in all_local_users:
print(f"\nShadow banning user: {user}")
user_commands.shadow_ban_account(user)
for room_id in all_room_ids:
blocked_status = room_commands.get_block_status(room_id)
#print(f"\nroom_details_dict: {room_details_dict}")
#print(f"\nblock_status: {blocked_status}")
# If room is already blocked, skip it
if blocked_status == False:
# Examine if unblocked room is known, if not block it
room_details_dict = room_commands.get_room_details(room_id)
if "Room not found" in room_details_dict.get('error', ''):
print(f"\n\nBlocking unknown room: {room_id}")
room_commands.set_block_status(room_id, True)
num_rooms_blocked += 1
# If unblocked room is known, perform a shutdown of the room
else:
print(f"\n\nShutting down known room: {room_id}")
list_kicked_users = room_commands.shutdown_room(room_id, user_ID, new_room_name, message, True, True)
num_rooms_purged += 1
total_list_kicked_users.extend(list_kicked_users)
if hardcoded_variables.testing_mode == True:
time.sleep(5)
elif blocked_status == True:
print(f"\n\nSkipping already blocked room: {room_id}")
if hardcoded_variables.testing_mode == True:
time.sleep(5)
elif shutdown_confirmation.lower() in ['n', 'no', 'N', 'No']:
print("\nSkipping blocking/shutdown of rooms...\n")
return 0, 0, []
else:
print("\nInvalid input, skipping these files...\n")
return 0, 0, []
# Deduplicate the list of all kicked users
total_list_kicked_users = list(set(total_list_kicked_users))
# Return the list of all kicked users
return num_rooms_blocked, num_rooms_purged, total_list_kicked_users
def block_recommended_rdlist_tags():
# Print warning if testing mode is enabled
if hardcoded_variables.testing_mode == True:
testing_mode_warning()
# Check if user account already exists
account_query = user_commands.query_account(hardcoded_variables.rdlist_bot_username)
# If user is not found, create it
if 'User not found' in account_query:
# Create user account
user_commands.create_account(hardcoded_variables.rdlist_bot_username, hardcoded_variables.rdlist_bot_password)
else:
print(f"@{hardcoded_variables.rdlist_bot_username}:{hardcoded_variables.base_url} account already exists. Resetting account password.")
user_commands.reset_password(hardcoded_variables.rdlist_bot_username, hardcoded_variables.rdlist_bot_password)
# Define default valies for shutdown_room()
preset_new_room_name = 'POLICY VIOLATION'
preset_message = 'THIS ROOM VIOLATES SERVER POLICIES'
# Block all rooms with recommended tag set
num_rooms_blocked, num_rooms_purged, total_list_kicked_users = block_all_rooms_with_rdlist_tags(True, hardcoded_variables.rdlist_bot_username, preset_new_room_name, preset_message)
# Print user login details if any rooms were shutdown
if total_list_kicked_users != []:
print("\n\nRoom shutdowns completed!\n\nUser login details for your moderator account:\n")
print("Username: " + hardcoded_variables.rdlist_bot_username)
print("Password: " + hardcoded_variables.rdlist_bot_password)
# Print statistics for the admin
print(f"\nPrinting rdlist statistics:")
print(f"\nNumber of rooms blocked: {num_rooms_blocked}")
print(f"Number of rooms purged: {num_rooms_purged}")
print(f"Number of local users located in rdlist rooms and kicked: {len(total_list_kicked_users)}")
if total_list_kicked_users != []:
print(f"\nThe following users were current members of rooms tagged in rdlist: {total_list_kicked_users}")
# Ask admin if they want to deactivate all the accounts that were kicked from rdlist rooms
deactivate_confirmation = input("\nDo you want to also deactivate all these accounts that were kicked from rdlist rooms? y/n? ")
if deactivate_confirmation.lower() in ['y', 'yes', 'Y', 'Yes']:
for user_id in total_list_kicked_users:
user_commands.deactivate_account(user_id)
print(f"\nThese accounts have been deactivated.")
elif deactivate_confirmation.lower() in ['n', 'no', 'N', 'No']:
print("\nSkipping account deactivations...\n")