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 friend_request_list(socket, args):
    if len(inputs[socket].profile.requests) > 0:
        output = ''.join([' %s' % r.username
                          for r in inputs[socket].profile.requests])[1:]
        send_cmd_success(socket, 145, output)
    else:
        send_error(socket, 224)
Beispiel #3
0
def hangup(socket, args):
    target = socket_by_username(args[1], socket)
    if target:
        call = call_between(socket, target)
        if call:
            call.hangup()
            send_cmd_success(socket, 112)
        else:
            send_error(socket, 403)
Beispiel #4
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 #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 call(socket, args):
    target = socket_by_username(args[1], socket)
    if target:
        if not call_between(socket, target):
            if inputs[target].udp_addr:
                Call(caller=socket, target=target)
                send_cmd_success(socket, 110)
            else:
                send_error(socket, 401)
        else:
            send_error(socket, 402)
Beispiel #7
0
def udp_init(socket, args):
    if inputs[socket].udp_addr:
        send_error(socket, 104)
    elif socket in list(udp_inits.values()):
        send_error(socket, 105)
    else:
        key = ''.join(random.choice(string.ascii_lowercase) for x in range(10))
        # Rest of this happens in brunod.py
        udp_inits.update({key: socket})
        send_cmd_success(socket, 120)
        send_event(socket, 101, (key))
Beispiel #8
0
def answer(socket, args):
    def pending(socket, target):
        call = call_between(socket, target)
        if call and not call.answered:
            return call
        return False
    target = socket_by_username(args[1], socket)
    if target:
        # call = inputs[socket].call_pending(target)
        call = pending(socket, target)
        if call:
            send_cmd_success(socket, 111)
            call.answer()
        else:
            send_error(socket, 400)
Beispiel #9
0
def call(socket, args):
    target = socket_by_username(args[1], socket)
    if target and not inputs[socket].call:
        if not inputs[target].call:
            if inputs[target].udp_addr:
                Call(caller=socket, target=target)
            else:
                # Target is not ready for udp
                send_error(socket, 401)
        else:
            # TODO: target is already having a call
            pass
    else:
        # TODO: You already are having a call
        pass
Beispiel #10
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 #11
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 #12
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 #13
0
def data_processor(socket):
    """
        data_processors(socket) -> None

        Converts cbuff to JSON. If fails, closes client.
    """
    logging.debug('Starting to process data from client')
    try:
        data_json = json.loads(inputs[socket].cbuff)
    except ValueError as err:
        logging.debug('ValueError: Failed to convert cbuff to JSON (%s)', err)
        data_json = None
    if data_json:
        try:
            validate_json_data(data_json)
        except KeyError as err:
            logging.debug('KerError: Received invalid '
                          'formated JSON data (%s).', err)
            return
        data = data_json
        if data['type'] == 'msg':
            send_msg(socket, data)
        elif data['type'] == 'cmd':
            cmd = data['args'].split()[0]
            if len(cmd) != 0:
                if cmd in commands:
                    if commands[cmd]['args'] == str:
                        commands[cmd]['func'](socket, data['args'])
                    else:
                        commands[cmd]['func'](socket, data['args'].split())
                else:
                    send_error(socket, 101)
            else:
                close_client(socket)
    else:
        close_client(socket)
Beispiel #14
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 #15
0
 def wrapper(socket, args):
     if self.no_args == len(args):
         func(socket, args)
     else:
         send_error(socket, 102,
                    'Invalid arguments ' + self.msg if self.msg else '')
Beispiel #16
0
 def __call__(self, socket, *args, **kwargs):
     if inputs[socket].udp_addr:
         self.func(socket, *args, **kwargs)
     else:
         send_error(socket, 103)
Beispiel #17
0
 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)
Beispiel #18
0
 def __call__(self, socket, *args, **kwargs):
     if inputs[socket].profile:
         self.func(socket, *args, **kwargs)
     else:
         send_error(socket, 203)