Skip to content

Commit ea14410

Browse files
committed
add --set-perms to pysogs, let --add-room also use them
1 parent 2ddaf9b commit ea14410

File tree

1 file changed

+138
-9
lines changed

1 file changed

+138
-9
lines changed

sogs/__main__.py

Lines changed: 138 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,12 @@
1919
# Add a global moderator visible as a moderator of all rooms:
2020
python3 -msogs --add-moderators 050123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef --rooms=+ --visible
2121
22+
# Set default read/write True and upload False on all rooms
23+
python3 -msogs --set-perms --add-perms rw --remove-perms u --rooms='*'
24+
25+
# Remove overrides for user 0501234... on all rooms
26+
python3 -msogs --set-perms --clear-perms rwua --rooms='*' --users 050123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef
27+
2228
# List room info:
2329
python3 -msogs -L
2430
@@ -51,6 +57,27 @@
5157
metavar='SESSIONID',
5258
help="Delete the the given Session ID(s) as moderator and admins of the room given by --rooms",
5359
)
60+
actions.add_argument('--set-perms', help="Sets permissions for the room given by --rooms")
61+
ap.add_argument(
62+
'--users',
63+
help="Users to set permissions for with --set-perms; if omitted, set global perms",
64+
nargs='+',
65+
metavar='SESSIONID',
66+
)
67+
ap.add_argument(
68+
"--add-perms",
69+
help="With --add-room or --set-perms, set these permissions to true; --add-perms=[r][w][u][a]",
70+
)
71+
ap.add_argument(
72+
"--remove-perms",
73+
help="With --add-room or --set-perms, set these permissions to false; "
74+
"--remove-perms=[r][w][u][a]",
75+
)
76+
ap.add_argument(
77+
"--clear-perms",
78+
help="With --add-room or --set-perms, clear room or user overrides on these permissions; "
79+
"--clear-perms=[r][w][u][a]",
80+
)
5481
ap.add_argument(
5582
'--admin',
5683
action='store_true',
@@ -60,10 +87,11 @@
6087
'--rooms',
6188
nargs='+',
6289
metavar='TOKEN',
63-
help="Room(s) to use when adding/removing moderators/admins. If a single room name of '+' is "
64-
"given then the user will be added/removed as a global admin/moderator. If a single room name "
90+
help="Room(s) to use when adding/removing moderators/admins or when setting permissions. "
91+
"If a single room name of '+' is given then the user will be added/removed as a global "
92+
"admin/moderator. '+' is not valid for setting permissions. If a single room name "
6593
"of '*' is given then the user is added/removed as an admin/moderator from each of the "
66-
"server's current rooms.",
94+
"server's current rooms or the requested permissions are set on each of the rooms.",
6795
)
6896
vis_group = ap.add_mutually_exclusive_group()
6997
vis_group.add_argument(
@@ -201,6 +229,52 @@ def print_room(room: Room):
201229
print()
202230

203231

232+
def room_token_valid(room):
233+
if not re.fullmatch(r'[\w-]{1,64}', room):
234+
print(
235+
"Error: room tokens may only contain a-z, A-Z, 0-9, _, and - characters",
236+
file=sys.stderr,
237+
)
238+
sys.exit(1)
239+
240+
241+
def perm_flag_to_word(char):
242+
if char == 'r':
243+
return "read"
244+
if char == 'w':
245+
return "write"
246+
if char == 'u':
247+
return "upload"
248+
if char == 'a':
249+
return "accessible"
250+
251+
print(f"Error: invalid permission flag '{char}'")
252+
sys.exit(1)
253+
254+
255+
perms = {}
256+
257+
258+
def parse_and_set_perm_flags(flags, perm_setting):
259+
for char in flags:
260+
perm_type = perm_flag_to_word(char)
261+
if perm_type in perms:
262+
print(
263+
f"Error: permission flag '{char}' in more than one permission set "
264+
"(add/remove/clear)"
265+
)
266+
sys.exit(1)
267+
perms[perm_type] = perm_setting
268+
269+
270+
if args.add_room or args.set_perms:
271+
if args.add_perms:
272+
parse_and_set_perm_flags(args.add_perms, True)
273+
if args.remove_perms:
274+
parse_and_set_perm_flags(args.remove_perms, False)
275+
if args.clear_perms:
276+
parse_and_set_perm_flags(args.clear_perms, None)
277+
204278
if args.initialize:
205279
print("Database schema created.")
206280

@@ -211,17 +285,21 @@ def print_room(room: Room):
211285
print("No database upgrades required.")
212286

213287
elif args.add_room:
214-
if not re.fullmatch(r'[\w-]{1,64}', args.add_room):
215-
print(
216-
"Error: room tokens may only contain a-z, A-Z, 0-9, _, and - characters",
217-
file=sys.stderr,
218-
)
219-
sys.exit(1)
288+
room_token_valid(args.add_room)
220289

221290
try:
222291
room = Room.create(
223292
token=args.add_room, name=args.name or args.add_room, description=args.description
224293
)
294+
if "read" in perms:
295+
room.default_read = perms["read"]
296+
if "write" in perms:
297+
room.default_write = perms["write"]
298+
if "accessible" in perms:
299+
room.default_accessible = perms["accessible"]
300+
if "upload" in perms:
301+
room.default_upload = perms["upload"]
302+
225303
except AlreadyExists:
226304
print(f"Error: room '{args.add_room}' already exists!", file=sys.stderr)
227305
sys.exit(1)
@@ -363,6 +441,57 @@ def print_room(room: Room):
363441
f"Removed {u2.session_id} as moderator/admin of {room.name} ({room.token})"
364442
)
365443

444+
elif args.set_perms:
445+
if not args.rooms:
446+
print("Error: --rooms is required when using --set-perms", file=sys.stderr)
447+
sys.exit(1)
448+
449+
if args.rooms == ['+']:
450+
print("Error: --rooms cannot be '+' (i.e. global) with --set-perms", file=sys.stderr)
451+
sys.exit(1)
452+
453+
users = []
454+
if args.users:
455+
for sid in args.users:
456+
u = User(session_id=sid, try_blinding=True)
457+
u2 = None
458+
if u.is_blinded and sid.startswith('05'):
459+
try:
460+
u2 = User(session_id=sid, try_blinding=False, autovivify=False)
461+
except NoSuchUser:
462+
pass
463+
users.append([u, u2])
464+
465+
rooms = []
466+
if args.rooms == ['*']:
467+
rooms = get_rooms()
468+
else:
469+
try:
470+
rooms = [Room(token=r) for r in args.rooms]
471+
except NoSuchRoom as nsr:
472+
print(f"No such room: '{nsr.token}'", file=sys.stderr)
473+
474+
if not len(rooms):
475+
print("Error: no valid rooms specified for call to --set-perms")
476+
sys.exit(1)
477+
478+
# users not specified means set room defaults
479+
if not len(users):
480+
for room in rooms:
481+
if "read" in perms:
482+
room.default_read = perms["read"]
483+
if "write" in perms:
484+
room.default_write = perms["write"]
485+
if "accessible" in perms:
486+
room.default_accessible = perms["accessible"]
487+
if "upload" in perms:
488+
room.default_upload = perms["upload"]
489+
else:
490+
sysadmin = SystemUser()
491+
for room in rooms:
492+
for user in users:
493+
room.set_permissions(user, mod=sysadmin, **perms)
494+
366495
elif args.list_rooms:
367496
rooms = get_rooms()
368497
if rooms:

0 commit comments

Comments
 (0)