# Path: src/petmail/database.py
# class DBError(Exception):
# class ObservableDatabase:
# def get_schema(version):
# def serialize(row):
#     def __init__(self, connection):
#     def subscribe(self, table, observer):
#     def unsubscribe(self, table, observer):
#     def execute(self, sql, values=None):
#     def insert(self, sql, values, table=None, tags={}):
#     def update(self, sql, values, table=None, id=None, tags={}):
#     def delete(self, sql, values, table, id, tags={}):
#     def commit(self):
# def get_db(dbfile, stderr=sys.stderr):
# def make_observable_db(dbfile, stderr=sys.stderr):
#     VERSION = 1
# 
# Path: src/petmail/util.py
# def equal(a, b):
#     # not vulnerable to timing attack
#     return hashlib.sha256(a).digest() == hashlib.sha256(b).digest()
# 
# def make_nonce():
#     return base64.b32encode(os.urandom(32)).strip("=").lower()
# 
# Path: src/petmail/errors.py
# class CommandError(Exception):
#     def __init__(self, msg):
#         self.msg = msg


import json, collections
from pkg_resources import resource_string, resource_filename
from twisted.application import service, strports
from twisted.web import server, static, resource, http
from .database import Notice
from .util import equal, make_nonce
from .errors import CommandError
    def __init__(self, db, access_token):
        resource.Resource.__init__(self)
        self.db = db
        self.access_token = access_token

    def render_GET(self, request):
        request.setHeader("content-type", "text/plain")
        if "opener-token" not in request.args:
            return "Please use 'petmail open' to get to the control panel\n"
        opener_token = request.args["opener-token"][0]
        c = self.db.execute("SELECT token FROM webapi_opener_tokens")
        tokens = [str(row[0]) for row in c.fetchall()]
        if opener_token not in tokens:
            return ("Sorry, that opener-token is expired or invalid,"
                    " please run 'petmail open' again\n")
        # good opener-token, single-use
        self.db.execute("DELETE FROM webapi_opener_tokens WHERE token=?",
                        (opener_token,))
        self.db.commit()

        request.setHeader("content-type", "text/html")
        return read_media("login.html") % self.access_token

class Control(resource.Resource):
    def __init__(self, access_token):
        resource.Resource.__init__(self)
        self.access_token = access_token

    def render_POST(self, request):
        token = request.args["token"][0]

# Path: src/petmail/database.py
# class DBError(Exception):
# class ObservableDatabase:
# def get_schema(version):
# def serialize(row):
#     def __init__(self, connection):
#     def subscribe(self, table, observer):
#     def unsubscribe(self, table, observer):
#     def execute(self, sql, values=None):
#     def insert(self, sql, values, table=None, tags={}):
#     def update(self, sql, values, table=None, id=None, tags={}):
#     def delete(self, sql, values, table, id, tags={}):
#     def commit(self):
# def get_db(dbfile, stderr=sys.stderr):
# def make_observable_db(dbfile, stderr=sys.stderr):
#     VERSION = 1
# 
# Path: src/petmail/util.py
# def equal(a, b):
#     # not vulnerable to timing attack
#     return hashlib.sha256(a).digest() == hashlib.sha256(b).digest()
# 
# def make_nonce():
#     return base64.b32encode(os.urandom(32)).strip("=").lower()
# 
# Path: src/petmail/errors.py
# class CommandError(Exception):
#     def __init__(self, msg):
#         self.msg = msg


# Path: src/petmail/web.py
import json, collections
from pkg_resources import resource_string, resource_filename
from twisted.application import service, strports
from twisted.web import server, static, resource, http
from .database import Notice
from .util import equal, make_nonce
from .errors import CommandError

    def __init__(self, db, access_token):
        resource.Resource.__init__(self)
        self.db = db
        self.access_token = access_token

    def render_GET(self, request):
        request.setHeader("content-type", "text/plain")
        if "opener-token" not in request.args:
            return "Please use 'petmail open' to get to the control panel\n"
        opener_token = request.args["opener-token"][0]
        c = self.db.execute("SELECT token FROM webapi_opener_tokens")
        tokens = [str(row[0]) for row in c.fetchall()]
        if opener_token not in tokens:
            return ("Sorry, that opener-token is expired or invalid,"
                    " please run 'petmail open' again\n")
        # good opener-token, single-use
        self.db.execute("DELETE FROM webapi_opener_tokens WHERE token=?",
                        (opener_token,))
        self.db.commit()

        request.setHeader("content-type", "text/html")
        return read_media("login.html") % self.access_token

class Control(resource.Resource):
    def __init__(self, access_token):
        resource.Resource.__init__(self)
        self.access_token = access_token

    def render_POST(self, request):
        token = request.args["token"][0]

        if not equal(token, self.access_token):