Skip to content

Commit

Permalink
Add support for private CTF channels
Browse files Browse the repository at this point in the history
Fix issue OpenToAllCTF#125.
Add util method for parsing args for options.
  • Loading branch information
l4cr0ss committed Jun 4, 2020
1 parent a5c56c1 commit 1370d7c
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 1 deletion.
25 changes: 25 additions & 0 deletions backend/DESIGN.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

#### abstract DAO describes the serializations supported by the backend

```python
class CtfDao:
pass

class ChallengeDao:
pass
```

#### concrete DAO implement serialization described by abstract DAO


#### application receives handle to backend at startup


#### ctf/challenge objects are initialized with instance of concrete dao

#### backend receives serialized DAO and persists it
┌───> PostgreSQL
+---------+ ├───> Redis
| Backend |>────────┼───> Pickle
+---------+ └───> ...

15 changes: 15 additions & 0 deletions backend/backend.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class Backend:
def __init__(self):
pass

def create(self):
pass

def read(self):
pass

def update(self):
pass

def destroy(self):
pass
2 changes: 2 additions & 0 deletions backend/pickle.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
class Pickle(Backend):
pass
2 changes: 2 additions & 0 deletions backend/redis.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
class Redis(Backend):
pass
7 changes: 6 additions & 1 deletion handlers/challenge_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:])

Expand All @@ -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']:
Expand Down
17 changes: 17 additions & 0 deletions util/util.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,30 @@
import json
import pickle
import re
from string import punctuation

from bottypes.invalid_command import InvalidCommand

#######
# 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):
"""
Expand Down

0 comments on commit 1370d7c

Please sign in to comment.