Ejemplo n.º 1
0
def exec_kozi(r, w, params):
    if len(params) != 1:
        write_response(w, '{"error": "no kozi in params"}')
        return

    kozi_name = params[0]

    k = find_kozinak(kozi_name)

    if k is None:
        write_response(w, '{"error": "no such kozinak"}')
        return

    if k.owner != "open":

        cookies = parse_cookies(r)
        if "session" not in cookies:
            write_response(w, '{"error": "auth is required"}')
            return

        mng = get_session_manager()
        username = mng.get_session(cookies["session"])
        if username is None:
            write_response(w, '{"error": "auth is required"}')
            return

        u = find_user(username)

        if u is None:
            write_response(w, '{"error": "auth is required"}')
            return

        if not chk_owner(username, k.owner):
            write_response(w, '{"error": "no access"}')
            return

    result = ""

    while True:
        result += k.fortune
        if k.pipe is not None:
            newk = find_kozinak(k.pipe)
            if newk is not None:
                k = newk
            else:
                break
        else:
            break

    write_response(w, json.dumps({'result': result}))
Ejemplo n.º 2
0
def create_kozi(r, w, params):
    cookies = parse_cookies(r)
    if "session" not in cookies:
        write_response(w, '{"error": "auth is required"}')
        return

    mng = get_session_manager()
    username = mng.get_session(cookies["session"])
    if username is None:
        write_response(w, '{"error": "auth is required"}')
        return

    u = find_user(username)

    if u is None:
        write_response(w, '{"error": "auth is required"}')
        return

    d = get_json_or_error(r, w)

    if d is None:
        return

    if "name" not in d or "fortune" not in d or "checksum" not in d:
        write_response(w,
                       '{"error": "no name or fortune or checksum in body"}')
        return

    name = d["name"]

    if not validate(name):
        write_response(w, '{"error": "invalid name"}')
        return

    k = find_kozinak("%s_%s" % (username, name))

    if k is not None:
        write_response(w, '{"error": "kozinak already exists"}')
        return

    fortune = d["fortune"]
    checksum = d["checksum"]
    pipe = d["pipe"] if "pipe" in d else None

    if create_kozinak_chk(name, fortune, checksum, username, pipe):
        write_response(w, '{"result": "ok"}')
    else:
        write_response(w, '{"error": "incorrect checksum"}')
Ejemplo n.º 3
0
def open_kozi(r, w, params):
    cookies = parse_cookies(r)
    if "session" not in cookies:
        write_response(w, '{"error": "auth is required"}')
        return

    mng = get_session_manager()
    username = mng.get_session(cookies["session"])
    if username is None:
        write_response(w, '{"error": "auth is required"}')
        return

    u = find_user(username)

    if u is None:
        write_response(w, '{"error": "auth is required"}')
        return

    d = get_json_or_error(r, w)

    if d is None:
        return

    if "kozi" not in d:
        write_response(w, '{"error": "no kozi in body"}')
        return

    kozi_name = d["kozi"]

    k = find_kozinak(kozi_name)

    if k is None:
        write_response(w, '{"error": "no such kozinak"}')
        return

    if not chk_owner(username, k.owner):
        write_response(w, '{"error": "no access"}')
        return

    k.delete()
    k.set_owner("open")
    k.save()

    write_response(w, '{"result": "ok"}')
Ejemplo n.º 4
0
def get_kozi(r, w, params):
    if len(params) != 1:
        write_response(w, '{"error": "no kozi in params"}')
        return

    kozi_name = params[0]

    k = find_kozinak(kozi_name)

    if k is None:
        write_response(w, '{"error": "no such kozinak"}')
        return

    if k.owner != "open":

        cookies = parse_cookies(r)
        if "session" not in cookies:
            write_response(w, '{"error": "auth is required"}')
            return

        mng = get_session_manager()
        username = mng.get_session(cookies["session"])
        if username is None:
            write_response(w, '{"error": "auth is required"}')
            return

        u = find_user(username)

        if u is None:
            write_response(w, '{"error": "auth is required"}')
            return

        if not chk_owner(username, k.owner):
            write_response(w, '{"error": "no access"}')
            return

    write_response(w, json.dumps({'result': json.loads(k.to_json())}))
Ejemplo n.º 5
0
def copy_kozi(r, w, params):
    cookies = parse_cookies(r)
    if "session" not in cookies:
        write_response(w, '{"error": "auth is required"}')
        return

    mng = get_session_manager()
    username = mng.get_session(cookies["session"])
    if username is None:
        write_response(w, '{"error": "auth is required"}')
        return

    u = find_user(username)

    if u is None:
        write_response(w, '{"error": "auth is required"}')
        return

    d = get_json_or_error(r, w)

    if d is None:
        return

    if "url" not in d or "name" not in d:
        write_response(w, '{"error": "no name or url in body"}')
        return

    url = d["url"]
    name = d["name"]

    if not validate(name):
        write_response(w, '{"error": "invalid name"}')
        return

    if url.lower().startswith("gopher") or url.lower().startswith("file"):
        write_response(w, '{"error": "invalid url"}')
        return

    req = urlopen(url)

    data = req.read()

    try:
        j = json.loads(data)
    except:
        write_response(w, '{"error": "incorrect json %s"}' % data)
        return

    if "result" not in j:
        write_response(w, '{"error": "incorrect json %s"}' % data)
        return

    j = j["result"]

    if type(j) != type({}):
        write_response(w, '{"error": "incorrect json %s"}' % data)
        return

    if "name" not in j or "fortune" not in j or "owner" not in j or "pipe" not in j:
        write_response(
            w, '{"error": "no name or fortune or owner or pipe in body"}')
        return

    fortune = j["fortune"]
    pipe = j["pipe"]

    k = find_kozinak("%s_%s" % (username, name))

    if k is not None:
        write_response(w, '{"error": "kozinak already exists"}')
        return

    create_kozinak(name, fortune, username, pipe)
    write_response(w, '{"result": "ok"}')