Beispiel #1
0
def register(socket, args):
    logging.debug('Processing registeration')
    if not db.get_user(args[1]):
        User(username=args[1], password=args[2]).save()
        send_cmd_success(socket, 102)
    else:
        send_error(socket, 201)
Beispiel #2
0
def logout(args):
    user = get_user(args[1])
    if user:
        s = socket_by_user(user)
        if s:
            dblogout(s, user)
        else:
            user.online = False
Beispiel #3
0
def user_friends(args):
    user = get_user(args[1])
    if user:
        print("Friend list of user '%s'" % user.username)
        for u in user.friends:
            print(u.username)
        print("%s total" % len(user.friends))
    else:
        print("User not found!")
Beispiel #4
0
def user_requests(args):
    user = get_user(args[1])
    if user:
        print("Friend requests for user '%s'" % user.username)
        for u in user.requests:
            print(u.username)
        print("%s total" % len(user.requests))
    else:
        print("User not found!")
Beispiel #5
0
def login(socket, args):
    logging.debug('Processing login')
    user = db.get_user(args[1])
    if user == 1:
        send_error(socket, 204)
    elif user and user.password == args[2]:
        auth.login(socket, user)
        send_cmd_success(socket, 100, user.username)
    else:
        send_error(socket, 200)
Beispiel #6
0
def friend_request_reject(socket, args):
    target = db.get_user(args[1])
    if target:
        if target in inputs[socket].profile.requests:
            inputs[socket].profile.requests.remove(target)
            send_cmd_success(socket, 142)
        else:
            send_error(socket, 223)
    else:
        send_error(socket, 220)
Beispiel #7
0
def login(socket, args):
    logging.debug('Processing login')
    user = db.get_user(args[1])
    if user and user.online is True:
        send_error(socket, 204)
    elif user and user.valid_password(args[2]):
        auth.login(socket, user)
        send_cmd_success(socket, 100, user.username)
        send_event(socket, 99, (''.join([' %s:%s' % (f.username, f.online)
                                         # [1:] so we dont send extra space
                                         for f in user.friends])[1:],
                                ''.join([r.username for r in user.requests])))
        notify_friends(inputs[socket].profile, 111, user.username)
    else:
        send_error(socket, 200)
Beispiel #8
0
def register(socket, args):
    logging.debug('Processing registeration')
    if not db.get_user(args[1]):
        try:
            User(username=args[1], password=args[2]).save()
        except ValueError as e:
            if e.args[0]['field'] == 'username':
                send_error(socket, 205)
            elif e.args[0]['field'] == 'password':
                send_error(socket, 206)
            else:
                send_error(socket, 207)
        else:
            send_cmd_success(socket, 102)
    else:
        send_error(socket, 201)
Beispiel #9
0
def friend_request_send(socket, args):
    target = db.get_user(args[1])
    if target:
        if target not in inputs[socket].profile.friends:
            if target not in inputs[socket].profile.requests and\
                    inputs[socket].profile not in target.requests:
                target.requests.append(inputs[socket].profile)
                send_cmd_success(socket, 140)
                # Lets notify the user that it has new friend request
                if target.online:
                    sock = socket_by_user(target)
                    if sock:
                        send_event(sock, 110, inputs[socket].profile.username)
            else:
                send_error(socket, 222)
        else:
            send_error(socket, 221)
    else:
        send_error(socket, 220)
Beispiel #10
0
def friend_request_accept(socket, args):
    target = db.get_user(args[1])
    if target:
        if target in inputs[socket].profile.requests:
            # Remove request
            inputs[socket].profile.requests.remove(target)
            # Add users to eachothers friends lists
            inputs[socket].profile.friends.append(target)
            target.friends.append(inputs[socket].profile)
            send_cmd_success(socket, 141)
            if target.online:
                s = socket_by_user(target)
                if s:
                    send_event(s, 111, inputs[socket].profile.username)
                    send_event(socket, 111, target.username)
        else:
            send_error(socket, 223)
    else:
        send_error(socket, 220)
Beispiel #11
0
def socket_by_username(username, socket=None):
    """
        socket_by_username(socket, username) -> socket or None

        returns socket user if it exists and is online or None. If returns None
        will send error message to client.
    """
    def err(socket, code):
        if socket:
            # FIXME: this is just stupid to import stuff here...
            from bruno.send_utils import send_error
            send_error(socket, code)
    user = db.get_user(username)
    if user:
        if user.online:
            s = socket_by_user(user)
            if s:
                return s
            else:
                err(socket, 301)
        else:
            err(socket, 301)
    else:
        err(socket, 300)
Beispiel #12
0
def user_stats(args):
    user = get_user(args[1])
    if user:
        print("username: {} online: {}".format(user.username, user.online))
    else:
        print("User not found!")