示例#1
0
def message(request, socket, context, message):
    """
    Event handler for a room receiving a message. First validates a
    joining user's name and sends them the list of users.
    """

    if message['action'] == "start":
        user = get_author_by_message(message)
        msg = make_message(u'%s entered the room' % user, 'activity', 'global')
        socket.send_and_broadcast_channel(msg)
        return

    user = get_author_by_message(message)
    room = get_room_or_none(message['room'])
    if user.user.has_perm("chat.super_chat_user"):
        if message['msg'][0] == '/' and message['room'] == "global":
            text = message['msg'].split(" ")
            if len(text) > 1:
                try:
                    sender = Player.objects.get(nickname=text[1])
                    sender = sender.user.get_profile().get_extension(ChatUser)
                except:
                    return False

                if text[0] == "/kick":
                    add_message(text[1], user, room, sender, "special", "kick")
                elif text[0] == "/unban":
                    sender.can_access_chat = True
                    sender.save()
                elif text[0] == "/ban":
                    sender.can_access_chat = False
                    sender.save()
                else:
                    return False
                message['text'] = strip_tags(text[1])
                message['user'] = unicode(user.nickname)
                message['time'] = str(datetime.now().strftime('%H:%M'))
                message['command'] = "kick"
                message['mess_type'] = "special"
                message['dest_user'] = unicode(sender.nickname)
                socket.send_and_broadcast_channel(message)
                return False

    if message['action'] == "message":
        add_message(message['msg'], user, room, user, "normal", "normal")
        message['text'] = strip_tags(message['msg'])
        message['user'] = unicode(user.nickname)
        message['time'] = str(datetime.now().strftime('%H:%M'))
        message['command'] = "normal"
        message['mess_type'] = "normal"
        message['dest_user'] = unicode(user.nickname)
        socket.send_and_broadcast_channel(message)
示例#2
0
文件: events.py 项目: AndreiRO/wouso
def message(request, socket, context, message):
    """
    Event handler for a room receiving a message. First validates a
    joining user's name and sends them the list of users.
    """

    if message['action'] == "start":
        user = get_author_by_message(message)
        msg = make_message(u'%s entered the room' % user, 'activity', 'global')
        socket.send_and_broadcast_channel(msg)
        return

    user = get_author_by_message(message)
    room = get_room_or_none(message['room'])
    if user.user.has_perm("chat.super_chat_user"):
        if message['msg'][0] == '/' and message['room'] == "global":
            text = message['msg'].split(" ")
            if len(text) > 1:
                try:
                    sender = Player.objects.get(nickname=text[1])
                    sender = sender.user.get_profile().get_extension(ChatUser)
                except:
                    return False

                if text[0] == "/kick":
                    add_message(text[1], user, room, sender, "special", "kick")
                elif text[0] == "/unban":
                    sender.can_access_chat = True
                    sender.save()
                elif text[0] == "/ban":
                    sender.can_access_chat = False
                    sender.save()
                else:
                    return False
                message['text'] = strip_tags(text[1])
                message['user'] = unicode(user.nickname)
                message['time'] = str(datetime.now().strftime('%H:%M'))
                message['command'] = "kick"
                message['mess_type'] = "special"
                message['dest_user'] = unicode(sender.nickname)
                socket.send_and_broadcast_channel(message)
                return False

    if message['action'] == "message":
        add_message(message['msg'], user, room, user, "normal", "normal")
        message['text'] = strip_tags(message['msg'])
        message['user'] = unicode(user.nickname)
        message['time'] = str(datetime.now().strftime('%H:%M'))
        message['command'] = "normal"
        message['mess_type'] = "normal"
        message['dest_user'] = unicode(user.nickname)
        socket.send_and_broadcast_channel(message)
示例#3
0
def sendmessage(request):
    """ Default endpoint (/chat/m/)
    """
    user = get_author(request)
    data = request.REQUEST

    if data['opcode'] == 'message':
        room = roomexist(data['room'])
        if user.user.has_perm('chat.super_chat_user'):
            if data['msg'][0] == '/' and data['room'] == 'global':
                text = data['msg'].split(" ")
                if len(text) > 1:
                    try:
                        sender = Player.objects.get(nickname=text[1])
                        sender = sender.user.get_profile().get_extension(ChatUser)
                    except:
                        return json_response(serve_message(user))

                    if text[0] == '/kick':
                        add_message(text[1], user, room, sender, "special", "kick")
                    if text[0] == '/unban':
                        sender.canAccessChat = True
                        sender.save()
                    if text[0] == '/ban':
                        sender.canAccessChat = False
                        sender.save()
                    return json_response(serve_message(user))


        try:
            assert room is not None
            # content, author, room, user_to, messType, command
            add_message(data['msg'], user, room, user, "normal", "normal")
        except (ValueError, AssertionError):
            return HttpResponseBadRequest()
    elif data['opcode'] == 'keepAlive':
        chat_global = roomexist('global')
        if user.has_modifier('block-communication'):
            return json_response(special_message(user, None, "block-communication"))
        elif user.has_modifier('block-global-chat-page') or not user.canAccessChat:
            return json_response(special_message(user, None, "kick"))

        if user not in chat_global.participants.all():
            chat_global.participants.add(user)
            user.lastMessageTS = datetime.now()
            user.save()
    elif data['opcode'] == 'getRoom':
        try:
            user_to = Player.objects.get(id=data['to'])
            user_to = user_to.get_extension(ChatUser)
        except ChatUser.DoesNotExist:
            return HttpResponseBadRequest()
        if user.has_modifier('block-communication'):
            return HttpResponseBadRequest()
        if user_to.has_modifier('block-communication'):
            return HttpResponseBadRequest()
        rooms = ChatRoom.objects.exclude(name='global').filter(participants=user).filter(participants=user_to)
        rooms = [r for r in rooms if r.participants.count() <= 2]
        if len(rooms) > 1:
            return HttpResponseBadRequest()
        if rooms:
            room = rooms[0]
        else:
            name = '%d-%d' % ((user.id, user_to.id) if user.id < user_to.id else (user_to.id, user.id))
            room = ChatRoom.create(name)
        room.participants.add(user)
        room.participants.add(user_to.id)
        return json_response(room.to_dict())
    return json_response(serve_message(user))