-
Notifications
You must be signed in to change notification settings - Fork 0
/
commands.py
207 lines (158 loc) · 9.12 KB
/
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
# matrix-admin-panel
# Copyright (C) 2022 Stefan9110
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
import requests
import json
import color
from main import prefix
import re
from time import time
import tui
def get_request(homeserver, token, path):
return requests.get((homeserver + path), headers={"Authorization": "Bearer " + token})
def post_request(homeserver, token, path, data):
return requests.post((homeserver + path), headers={"Authorization": "Bearer " + token}, data=data)
def delete_request(homeserver, token, path):
return requests.delete((homeserver + path), headers={"Authorization": "Bearer " + token})
def list_users(homeserver, token):
limit = input(color.YELLOW + "Enter user limit (default none): " + color.RESET)
req_path = "/_synapse/admin/v2/users?from=0&limit=" + str(limit) + "&guests=false"
if str(limit) == "":
req_path = "/_synapse/admin/v2/users?from=0&guests=false"
request = get_request(homeserver, token, req_path)
if request.status_code != 200:
print(prefix() + "An error as occurred (" + str(request.status_code) + ")")
return
print("\n" + prefix() + "Fetching user accounts...")
for user in json.loads(request.content.decode())["users"]:
print(color.YELLOW + "──────────────────────────────────────────────────────")
tui.print_user(user)
print(color.YELLOW + "──────────────────────────────────────────────────────")
def search_user(homeserver, token):
query = input(color.YELLOW + "Enter user search query: " + color.RESET)
request = get_request(homeserver, token, "/_synapse/admin/v2/users?from=0&guests=false")
if request.status_code != 200:
print(prefix() + "An error as occurred (" + str(request.status_code) + ")")
return
found = False
for user in json.loads(request.content.decode())["users"]:
if not re.search(query.lower(), str(user["name"]).lower()) \
and not re.search(query.lower(), str(user["displayname"]).lower()):
continue
found = True
print(color.YELLOW + "──────────────────────────────────────────────────────")
tui.print_user(user)
print(color.YELLOW + "──────────────────────────────────────────────────────")
if not found:
print(prefix() + "User not found...")
def list_registration_tokens(homeserver, token):
request = get_request(homeserver, token, "/_synapse/admin/v1/registration_tokens")
if request.status_code != 200:
print(prefix() + "An error as occurred (" + str(request.status_code) + ")")
return
found = False
print(prefix() + "Listing available registration tokens...")
for reg_token in json.loads(request.content.decode())["registration_tokens"]:
found = True
print(color.YELLOW + "──────────────────────────────────────────────────────")
tui.print_registration_token(reg_token)
if not found:
print(prefix() + "No registration tokens were found.")
else:
print(color.YELLOW + "──────────────────────────────────────────────────────")
def create_registration_token(homeserver, token):
print("\n" + prefix() + "Creating a registration token... Enter the details below:")
uses_allowed = input(color.YELLOW + "Uses allowed (default 1): " + color.RESET)
expire = input(color.YELLOW + "How many seconds should the token be available "
"(default " + color.RED + "permanent" + color.YELLOW + "): " + color.RESET)
length = input(color.YELLOW + "Token length (default 48): " + color.RESET)
data = {
"length": 48,
"expiry_time": None,
"uses_allowed": 1
}
if str(expire) != "" and str(expire).lower() != "no":
data["expiry_time"] = (int(time()) + int(expire)) * 1000
if str(length) != "":
_len = int(length)
# Cap length at 64 as synapse requires
if _len > 64:
_len = 64
data["length"] = _len
if str(uses_allowed) != "":
data["uses_allowed"] = int(uses_allowed)
request = post_request(homeserver, token, "/_synapse/admin/v1/registration_tokens/new", json.dumps(data))
if request.status_code != 200:
print(prefix() + "An error as occurred (" + str(request.status_code) + ")")
return
created_token = json.loads(request.content.decode())
print(prefix() + "A registration token was created: " +
color.GREEN + created_token["token"] + color.RESET)
print(color.YELLOW + "──────────────────────────────────────────────────────")
tui.print_registration_token(created_token)
print(color.YELLOW + "──────────────────────────────────────────────────────")
def delete_registration_token(homeserver, token):
print(prefix() + color.RED + "WARNING!" + color.RESET + " Deleting a registration token is a permanent action!")
to_delete = input(color.YELLOW + "Input registration token to delete: " + color.RESET)
if str(to_delete) == "":
return
request = delete_request(homeserver, token, "/_synapse/admin/v1/registration_tokens/" + to_delete)
if request.status_code == 200:
print(prefix() + "Successfully deleted token " + color.GREEN + to_delete + color.RESET)
else:
print(prefix() + "The token you were trying to delete does not exist")
# List all rooms that are created on the homeserver
def list_rooms(homeserver, token):
limit = input(color.YELLOW + "Enter room limit (default none): " + color.RESET)
if str(limit) == "" or not str(limit).isnumeric():
limit = 0
request = get_request(homeserver, token, path="/_synapse/admin/v1/rooms")
if request.status_code != 200:
print(prefix() + "An error as occurred (" + str(request.status_code) + ")")
return
print("\n" + prefix() + "Fetching rooms created on the " + color.GREEN + homeserver + color.RESET + " homeserver")
count = 0
for room in json.loads(request.content.decode())["rooms"]:
# Check whether the room id contains the homeserver link
if re.sub(r"http(s)?://", "", homeserver) not in room["room_id"]:
continue
# Print room data
print(color.YELLOW + "──────────────────────────────────────────────────────")
tui.print_room(room)
count += 1
if count >= int(limit) != 0:
break
print(color.YELLOW + "──────────────────────────────────────────────────────")
def search_room(homeserver, token):
to_search = input(color.YELLOW + "Enter room query: " + color.RESET)
request = get_request(homeserver, token, path="/_synapse/admin/v1/rooms")
if request.status_code != 200:
print(prefix() + "An error as occurred (" + str(request.status_code) + ")")
return
found = False
for room in json.loads(request.content.decode())["rooms"]:
# Check whether room is valid
if re.sub(r"http(s)?://", "", homeserver) not in room["room_id"] \
or room["name"] is None \
or to_search.lower() not in str(room["name"]).lower():
continue
found = True
# Print room data
print(color.YELLOW + "──────────────────────────────────────────────────────")
tui.print_room(room)
if not found:
print(prefix() + "No room was found...")
else:
print(color.YELLOW + "──────────────────────────────────────────────────────")