From bcf1e46d2cd260884d70e035f6c54565999bfd67 Mon Sep 17 00:00:00 2001 From: l4cr0ss Date: Thu, 4 Jun 2020 13:28:21 -0400 Subject: [PATCH] Add support for private CTF channels Fix issue #125 Add utility method for parsing options from the arguments passed to `!` commands. --- handlers/challenge_handler.py | 7 ++++++- util/util.py | 17 +++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/handlers/challenge_handler.py b/handlers/challenge_handler.py index f9c685d..e85609d 100644 --- a/handlers/challenge_handler.py +++ b/handlers/challenge_handler.py @@ -95,6 +95,10 @@ class AddCTFCommand(Command): @classmethod def execute(cls, slack_wrapper, args, timestamp, channel_id, user_id, user_is_admin): """Execute AddCTF command.""" + + # Pull options out into its own array + opts, args = parse_opts(args) + name = args[0].lower() long_name = " ".join(args[1:]) @@ -110,7 +114,8 @@ def execute(cls, slack_wrapper, args, timestamp, channel_id, user_id, user_is_ad raise InvalidCommand("Add CTF failed: Invalid characters for CTF name found.") # Create the channel - response = slack_wrapper.create_channel(name) + is_private = True if 'private' in opts else False + response = slack_wrapper.create_channel(name, is_private) # Validate that the channel was successfully created. if not response['ok']: diff --git a/util/util.py b/util/util.py index 26d2178..03a89e8 100644 --- a/util/util.py +++ b/util/util.py @@ -1,6 +1,7 @@ import json import pickle import re +from string import punctuation from bottypes.invalid_command import InvalidCommand @@ -8,6 +9,22 @@ # Helper functions ####### +def parse_opts(_args): + """ + Filter an array of args and extract those beginning with a hyphen into a + new array called opts, removing any/all leading punctuation in the process. + Return a 2-tuple like ([opts], [args]) + """ + opts = [] + args = [] + for arg in _args: + if arg.startswith('-'): + arg = arg.strip(punctuation) + opts.append(arg) + else: + args.append(arg) + return (opts,args) + def load_json(string): """