moth/ctf/teams.py

72 lines
1.5 KiB
Python
Raw Normal View History

#! /usr/bin/env python3
import fcntl
2009-09-25 13:27:37 -06:00
import time
import os
2009-10-06 11:50:21 -06:00
# python 2 compatibility
try:
from urllib.parse import quote, unquote
except:
from urllib import quote, unquote
2009-10-05 13:33:20 -06:00
from . import config
2009-10-01 12:17:03 -06:00
house = config.get('global', 'house_team')
passwdfn = config.get('global', 'passwd')
team_colors = config.get('global', 'team_colors')
2009-09-25 13:27:37 -06:00
2009-09-29 15:36:25 -06:00
teams = {}
2009-09-25 13:27:37 -06:00
built = 0
def build_teams():
2009-09-25 13:27:37 -06:00
global teams, built
2009-09-29 15:36:25 -06:00
if not os.path.exists(passwdfn):
return
if os.path.getmtime(passwdfn) <= built:
2009-09-25 13:27:37 -06:00
return
teams = {}
try:
2009-09-25 13:27:37 -06:00
f = open(passwdfn)
for line in f:
2009-09-02 14:25:31 -06:00
line = line.strip()
team, passwd = [unquote(v) for v in line.strip().split('\t')]
2009-10-01 12:17:03 -06:00
color = team_colors.pop(0)
team_colors.append(color)
teams[team] = (passwd, color)
except IOError:
pass
2009-09-25 13:27:37 -06:00
built = time.time()
2009-09-02 14:25:31 -06:00
def validate(team):
2009-09-25 13:27:37 -06:00
build_teams()
2009-09-02 14:25:31 -06:00
def chkpasswd(team, passwd):
validate(team)
2009-10-01 12:17:03 -06:00
if teams.get(team, [None, None])[0] == passwd:
return True
else:
return False
def exists(team):
2009-09-02 14:25:31 -06:00
validate(team)
if team == house:
return True
return team in teams
def add(team, passwd):
2009-09-29 15:36:25 -06:00
f = open(passwdfn, 'a')
fcntl.lockf(f, fcntl.LOCK_EX)
f.seek(0, 2)
2009-09-02 14:25:31 -06:00
f.write('%s\t%s\n' % (quote(team), quote(passwd)))
2009-10-01 12:17:03 -06:00
def color(team):
t = teams.get(team)
if not t:
validate(team)
t = teams.get(team)
if not t:
return '888888'
return t[1]