예제 #1
0
def relay_inner(ev, *, caption_text=None, signed=False, tripcode=False):
    is_media = is_forward(ev) or ev.content_type in MEDIA_FILTER_TYPES
    msid = core.prepare_user_message(UserContainer(ev.from_user),
                                     calc_spam_score(ev),
                                     is_media=is_media,
                                     signed=signed,
                                     tripcode=tripcode)
    if msid is None or isinstance(msid, rp.Reply):
        return send_answer(ev, msid)  # don't relay message, instead reply

    user = db.getUser(id=ev.from_user.id)

    # apply text formatting to text or caption (if media)
    ev_tosend = ev
    force_caption = None
    if is_forward(ev):
        pass  # leave message alone
    elif ev.content_type == "text" or ev.caption is not None or caption_text is not None:
        fmt = FormattedMessageBuilder(caption_text, ev.caption, ev.text)
        formatter_replace_links(ev, fmt)
        formatter_network_links(fmt)
        if signed:
            formatter_signed_message(user, fmt)
        elif tripcode:
            formatter_tripcoded_message(user, fmt)
        fmt = fmt.build()
        # either replace whole message or just the caption
        if ev.content_type == "text":
            ev_tosend = fmt or ev_tosend
        else:
            force_caption = fmt

    # find out which message is being replied to
    reply_msid = None
    if ev.reply_to_message is not None:
        reply_msid = ch.lookupMapping(ev.from_user.id,
                                      data=ev.reply_to_message.message_id)
        if reply_msid is None:
            logging.warning("Message replied to not found in cache")

    # relay message to all other users
    logging.debug("relay(): msid=%d reply_msid=%r", msid, reply_msid)
    for user2 in db.iterateUsers():
        if not user2.isJoined():
            continue
        if user2 == user and not user.debugEnabled:
            ch.saveMapping(user2.id, msid, ev.message_id)
            continue

        send_to_single(ev_tosend,
                       msid,
                       user2,
                       reply_msid=reply_msid,
                       force_caption=force_caption)
예제 #2
0
def relay(ev):
    # handle commands and karma giving
    if ev.content_type == "text" and ev.text.startswith("/"):
        pos = ev.text.find(" ") if " " in ev.text else len(ev.text)
        c = ev.text[1:pos].lower()
        if c in registered_commands.keys():
            registered_commands[c](ev)
        return
    elif ev.content_type == "text" and ev.text.strip() == "+1":
        return cmd_plusone(ev)
    elif ev.content_type == "text" and ev.text.strip() == "Based":
        return cmd_plusone(ev)

    # filter disallowed media types
    if not allow_documents and ev.content_type == "document" and ev.document.mime_type not in (
            "image/gif", "video/mp4"):
        return

    msid = core.prepare_user_message(UserContainer(ev.from_user),
                                     calc_spam_score(ev))
    if isinstance(
            msid,
            rp.Reply):  # don't relay message, instead reply with something
        return send_answer(ev, msid)

    user = db.getUser(id=ev.from_user.id)

    # find out which message is being replied to
    reply_msid = None
    if ev.reply_to_message is not None:
        reply_msid = ch.lookupMapping(ev.from_user.id,
                                      data=ev.reply_to_message.message_id)
        if reply_msid is None:
            logging.warning("Message replied to not found in cache")

    # relay message to all other users
    logging.debug("relay(): msid=%d reply_msid=%r", msid, reply_msid)
    for user2 in db.iterateUsers():
        if not user2.isJoined():
            continue
        if user2 == user and not user.debugEnabled:
            ch.saveMapping(user2.id, msid, ev.message_id)
            continue

        send_to_single(ev, msid, user2, reply_msid)