Skip to content

Commit

Permalink
mvp of feat completed
Browse files Browse the repository at this point in the history
  • Loading branch information
Eleni Ioakeim committed Jul 24, 2021
1 parent 1c02d05 commit 4bdb73b
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 32 deletions.
4 changes: 3 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,6 @@ OVISBOT_HTB_TEAM_ID=1000
OVISBOT_CTFTIME_TEAM_ID=999
OVISBOT_ADMIN_ROLE=moderator
OVISBOT_DB_URL=mongodb://localhost/ovisdb
OVISBOT_THIRD_PARTY_COGS_INSTALL_DIR=~/cogs
OVISBOT_THIRD_PARTY_COGS_INSTALL_DIR=~/cogs
OVISBOT_GITHUB_TOKEN=<github_token>
OVISBOT_GITHUB_REPO_API_PATH=<github_solves_repo>
2 changes: 2 additions & 0 deletions ovisbot/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,8 @@ class Config(AbstractConfig):
DB_URL = environ.get("OVISBOT_DB_URL", "mongodb://mongo/ovisdb")
COMMAND_PREFIX = environ.get("OVISBOT_COMMAND_PREFIX", "!")
DISCORD_BOT_TOKEN = environ.get("OVISBOT_DISCORD_TOKEN")
GITHUB_TOKEN = environ.get("OVISBOT_GITHUB_TOKEN")
GITHUB_SOLVES_REPO = environ.get("OVISBOT_GITHUB_REPO_API_PATH")

THIRD_PARTY_COGS_INSTALL_DIR = environ.get(
"OVISBOT_THIRD_PARTY_COGS_INSTALL_DIR", "/usr/local/share/ovisbot/cogs"
Expand Down
69 changes: 46 additions & 23 deletions ovisbot/extensions/ctf/ctf.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import requests
import dateutil.parser
import pytz
import random
import json

import ovisbot.locale as i18n

Expand Down Expand Up @@ -114,30 +116,11 @@ async def archive(self, ctx, ctf_name):
await ctfrole.delete()

for c in category.channels:
f = open(c.name + ".md","w")

pinnedMessages = await c.pins()

for pinned in pinnedMessages:
f.write(pinned.content + os.linesep)

attachs = pinned.attachments
for a in attachs:
base64string = base64.b64encode(await a.read())
# convert the image base 64 to a string
image_string = str(base64string)
# replace the newline characters
image_string = image_string.replace("\\n", "")
# replace the initial binary
image_string = image_string.replace("b'", "")
# replace the final question mark
image_string = image_string.replace("'", "")

f.write("![attachment](data:" + a.content_type + ";base64," + image_string + ")"+ os.linesep)

f.write("---" + os.linesep)

f.close()
challenge = next((ch for ch in ctf.challenges if ch.name == c.name), None)
if not not challenge and challenge.solved_at:
await harvestPins(c)
self.pushToGitHub(ctf_name,c.name)

await c.delete()

Expand Down Expand Up @@ -937,7 +920,47 @@ async def check_reminders(self):
)
except CTF.DoesNotExist:
continue

def pushToGitHub(self,ctf_name,challenge_name):

pinsDir = os.path.abspath(os.getcwd()) + '/pins/'
for filename in os.listdir(pinsDir):
url = self.bot.config_cls.GITHUB_SOLVES_REPO + ctf_name + "/" + challenge_name + "/" + filename
file = base64.b64encode(open(pinsDir + filename, "rb").read())

token = self.bot.config_cls.GITHUB_TOKEN
message = json.dumps({
"message":"add pins for challenge " + challenge_name + " of ctf " + ctf_name,
"branch": "main",
"content": file.decode("utf-8")
})

resp=requests.put(url, data = message, headers = {"Accept": "application/vnd.github.v3+json", "Authorization": "token "+token})
os.remove(pinsDir + filename)

# Gathers all pinned messages and files in the /pins folder
async def harvestPins(channel):
pinsDir = os.path.abspath(os.getcwd()) + '/pins'
completeName = os.path.join(pinsDir, channel.name + ".md")
f = open(completeName,"w")

pinnedMessages = await channel.pins()

# reversed as to write first pinned message, first.
# otherwise, the last pinned message is the first to be written.
for pinned in reversed(pinnedMessages):

f.write(pinned.content + os.linesep)

attachs = pinned.attachments
if(len(attachs) != 0):
for a in attachs:
await a.save(os.path.join(pinsDir, a.filename))
f.write("check file " + a.filename + " in this directory." + os.linesep)

f.write(os.linesep + "---" + os.linesep)

f.close()

def setup(bot):
bot.add_cog(Ctf(bot))
7 changes: 0 additions & 7 deletions pinctf-only.md

This file was deleted.

1 change: 0 additions & 1 deletion pinctf-pls.md

This file was deleted.

0 comments on commit 4bdb73b

Please sign in to comment.