2009-09-02 10:15:54 -06:00
|
|
|
#! /usr/bin/env python3
|
|
|
|
|
|
|
|
import fcntl
|
2009-09-25 13:27:37 -06:00
|
|
|
import time
|
|
|
|
import os
|
2009-09-02 14:25:31 -06:00
|
|
|
from urllib.parse import quote, unquote
|
2009-10-05 13:33:20 -06:00
|
|
|
from . import config
|
2009-09-02 10:15:54 -06:00
|
|
|
|
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
|
2009-09-02 10:15:54 -06:00
|
|
|
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
|
2009-09-02 10:15:54 -06:00
|
|
|
|
|
|
|
teams = {}
|
|
|
|
try:
|
2009-09-25 13:27:37 -06:00
|
|
|
f = open(passwdfn)
|
2009-09-02 10:15:54 -06:00
|
|
|
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)
|
2009-09-02 10:15:54 -06:00
|
|
|
except IOError:
|
|
|
|
pass
|
2009-09-25 13:27:37 -06:00
|
|
|
built = time.time()
|
2009-09-02 10:15:54 -06:00
|
|
|
|
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:
|
2009-09-02 10:15:54 -06:00
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
|
|
|
|
def exists(team):
|
2009-09-02 14:25:31 -06:00
|
|
|
validate(team)
|
2009-09-02 10:15:54 -06:00
|
|
|
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')
|
2009-09-02 10:15:54 -06:00
|
|
|
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]
|
|
|
|
|
|
|
|
|
|
|
|
|