예제 #1
0
def filter_messages(bot: DeltaBot, message: Message, replies: Replies) -> None:
    """Detect messages like +1 or -1 to increase/decrease score.
    """
    if not message.quote:
        return
    score = _parse(message.text)
    if not score:
        return
    sender = message.get_sender_contact().addr
    is_admin = bot.is_admin(sender)
    if score < 0 and not is_admin:
        return
    if not is_admin and db.get_score(sender) - score < 0:
        replies.add(text="❌ You can't give what you don't have...",
                    quote=message)
        return
    receiver = message.quote.get_sender_contact().addr
    if sender == receiver:
        return

    sender_score = _add_score(sender, -score)
    receiver_score = _add_score(receiver, score)
    if is_admin:
        text = '{0}: {1}{4}'
    else:
        text = '{0}: {1}{4}\n{2}: {3}{4}'
    text = text.format(
        bot.get_contact(receiver).name, receiver_score,
        bot.get_contact(sender).name, sender_score,
        _getdefault(bot, 'score_badge'))
    replies.add(text=text, quote=message)
예제 #2
0
def join(bot: DeltaBot, payload: str, message: Message,
         replies: Replies) -> None:
    """Join the given IRC channel."""
    sender = message.get_sender_contact()
    if not payload:
        replies.add(text="Wrong syntax")
        return
    if not bot.is_admin(sender.addr) and \
       not db.is_whitelisted(payload):
        replies.add(text="That channel isn't in the whitelist")
        return

    g = bot.get_chat(db.get_chat(payload))
    if g and sender in g.get_contacts():
        replies.add(text='You are already a member of this group', chat=g)
        return
    if g is None:
        chat = bot.create_group(payload, [sender])
        db.add_channel(payload, chat.id)
        irc_bridge.join_channel(payload)
        irc_bridge.preactor.join_channel(sender.addr, payload)
    else:
        _add_contact(g, sender)
        chat = bot.get_chat(sender)

    nick = db.get_nick(sender.addr)
    text = '** You joined {} as {}'.format(payload, nick)
    replies.add(text=text, chat=chat)
예제 #3
0
def group_topic(bot: DeltaBot, args: list, message: Message,
                replies: Replies) -> None:
    """Show or change group/channel topic.
    """
    if not message.chat.is_group():
        replies.add(text='❌ This is not a group')
        return

    if args:
        new_topic = ' '.join(args)
        max_size = int(_getdefault(bot, 'max_topic_size'))
        if len(new_topic) > max_size:
            new_topic = new_topic[:max_size] + '...'

        text = '** {} changed topic to:\n{}'

        ch = db.get_channel(message.chat.id)
        if ch and ch['admin'] == message.chat.id:
            name = _get_name(message.get_sender_contact())
            text = text.format(name, new_topic)
            db.set_channel_topic(ch['id'], new_topic)
            for chat in _get_cchats(bot, ch['id']):
                replies.add(text=text, chat=chat)
            replies.add(text=text)
            return
        if ch:
            replies.add(text='❌ Only channel operators can do that.')
            return

        addr = message.get_sender_contact().addr
        g = db.get_group(message.chat.id)
        if not g:
            _add_group(bot, message.chat.id, as_admin=bot.is_admin(addr))
            g = db.get_group(message.chat.id)
            assert g is not None
        db.upsert_group(g['id'], new_topic)
        replies.add(text=text.format(addr, new_topic))
        return

    g = db.get_channel(message.chat.id) or db.get_group(message.chat.id)
    if not g:
        addr = message.get_sender_contact().addr
        _add_group(bot, message.chat.id, as_admin=bot.is_admin(addr))
        g = db.get_group(message.chat.id)
        assert g is not None
    replies.add(text=g['topic'] or '-', quote=message)
예제 #4
0
def filter_messages(bot: DeltaBot, message: Message, replies: Replies) -> None:
    """Detect messages like +1 or -1 to increase/decrease score."""
    if message.quote:
        receiver_addr = message.quote.get_sender_contact().addr
        score = _parse(message.text)
    else:
        args = message.text.split(maxsplit=2)
        if len(args) == 2 and "@" in args[0]:
            receiver_addr = args[0]
            score = _parse(args[1])
        else:
            score = 0
    if not score:
        return
    sender_addr = message.get_sender_contact().addr
    is_admin = bot.is_admin(sender_addr)
    if (score < 0 and not is_admin) or sender_addr == receiver_addr:
        return

    with session_scope() as session:
        sender = session.query(User).filter_by(addr=sender_addr).first()
        if not sender:
            sender = User(addr=sender_addr)
            session.add(sender)
        if not is_admin and sender.score - score < 0:
            replies.add(text="❌ You can't give what you don't have...",
                        quote=message)
            return

        if not is_admin:
            sender.score -= score
        sender_score = sender.score

        receiver = session.query(User).filter_by(addr=receiver_addr).first()
        if not receiver:
            receiver = User(addr=receiver_addr)
            session.add(receiver)

        receiver.score += score
        receiver_score = receiver.score

    if is_admin:
        text = "{0}: {1}{4}"
    else:
        text = "{0}: {1}{4}\n{2}: {3}{4}"
    text = text.format(
        bot.get_contact(receiver_addr).name,
        receiver_score,
        bot.get_contact(sender_addr).name,
        sender_score,
        _getdefault(bot, "score_badge"),
    )
    replies.add(text=text, quote=message)
예제 #5
0
def group_join(bot: DeltaBot, args: list, message: Message,
               replies: Replies) -> None:
    """Join the given group/channel.
    """
    sender = message.get_sender_contact()
    is_admin = bot.is_admin(sender.addr)
    text = '{}\n\n{}\n\n⬅️ /group_remove_{}'
    arg = args[0] if args else ''
    if arg.startswith('g'):
        gid = int(arg[1:])
        gr = db.get_group(gid)
        if gr:
            g = bot.get_chat(gr['id'])
            contacts = g.get_contacts()
            if sender in contacts:
                replies.add(
                    text='❌ {}, you are already a member of this group'.format(
                        sender.addr),
                    chat=g)
            elif len(contacts) < int(_getdefault(
                    bot, 'max_group_size')) or is_admin:
                _add_contact(g, sender)
                replies.add(chat=bot.get_chat(sender),
                            text=text.format(g.get_name(), gr['topic'] or '-',
                                             arg))
            else:
                replies.add(text='❌ Group is full')
            return
    elif arg.startswith('c'):
        gid = int(arg[1:])
        ch = db.get_channel_by_id(gid)
        if ch:
            for g in _get_cchats(bot, ch['id'], include_admin=True):
                if sender in g.get_contacts():
                    replies.add(
                        text='❌ {}, you are already a member of this channel'.
                        format(sender.addr),
                        chat=g)
                    return
            g = bot.create_group(ch['name'], [sender])
            db.add_cchat(g.id, ch['id'])
            img = bot.get_chat(ch['id']).get_profile_image()
            if img:
                g.set_profile_image(img)
            replies.add(text=text.format(ch['name'], ch['topic'] or '-', arg),
                        chat=g)
            return

    replies.add(text='❌ Invalid ID')
예제 #6
0
def group_info(bot: DeltaBot, message: Message, replies: Replies) -> None:
    """Show the group/channel info.
    """
    if not message.chat.is_group():
        replies.add(text='❌ This is not a group')
        return

    text = '{0}\n👤 {1}\n{2}\n\n'
    text += '⬅️ /group_remove_{3}{4}\n➡️ /group_join_{3}{4}'

    ch = db.get_channel(message.chat.id)
    if ch:
        count = sum(
            map(lambda g: len(g.get_contacts()) - 1,
                _get_cchats(bot, ch['id'])))
        replies.add(text=text.format(ch['name'], count, ch['topic'] or '-',
                                     'c', ch['id']))
        return

    g = db.get_group(message.chat.id)
    if not g:
        addr = message.get_sender_contact().addr
        _add_group(bot, message.chat.id, as_admin=bot.is_admin(addr))
        g = db.get_group(message.chat.id)
        assert g is not None

    chat = bot.get_chat(g['id'])
    img = qrcode.make(chat.get_join_qr())
    buffer = io.BytesIO()
    img.save(buffer, format='jpeg')
    buffer.seek(0)
    count = len(bot.get_chat(g['id']).get_contacts())
    replies.add(text=text.format(chat.get_name(), count, g['topic'] or '-',
                                 'g', g['id']),
                filename='img.jpg',
                bytefile=buffer)
예제 #7
0
def deltabot_member_added(bot: DeltaBot, chat: Chat, contact: Contact,
                          actor: Contact) -> None:
    if contact == bot.self_contact and not db.get_channel(chat.id):
        _add_group(bot, chat.id, as_admin=bot.is_admin(actor.addr))