Пример #1
0
def tell_rooms(msg, has, hasnt, notify_site="", report_data=None):
    global _rooms

    msg = msg.rstrip()
    target_rooms = set()

    for prop_has in has:
        if isinstance(prop_has, tuple):
            target_rooms.add(prop_has)

        if prop_has not in _room_roles:
            continue

        for room in _room_roles[prop_has]:
            if all(
                    map(
                        lambda prop: prop not in _room_roles or room not in
                        _room_roles[prop], hasnt)):
                if room not in _rooms:
                    site, roomid = room
                    deletion_watcher = room in _watcher_rooms

                    new_room = _clients[site].get_room(roomid)
                    new_room.join()

                    _rooms[room] = RoomData(new_room, -1, deletion_watcher)

                target_rooms.add(room)

    for room_id in target_rooms:
        room = _rooms[room_id]

        if notify_site:
            pings = datahandling.get_user_names_on_notification_list(
                room.room._client.host, room.room.id, notify_site,
                room.room._client)

            msg_pings = datahandling.append_pings(msg, pings)
        else:
            msg_pings = msg

        timestamp = time.time()

        if room.block_time < timestamp and _global_block < timestamp:
            if report_data and "delay" in _room_roles and room_id in _room_roles[
                    "delay"]:

                def callback(room=room, msg=msg_pings):
                    post = fetch_post_id_and_site_from_url(report_data[0])[0:2]

                    if not datahandling.is_false_positive(
                            post) and not datahandling.is_ignored_post(post):
                        _msg_queue.put((room, msg, report_data))

                task = Tasks.later(callback, after=300)

                GlobalVars.deletion_watcher.subscribe(report_data[0],
                                                      callback=task.cancel)
            else:
                _msg_queue.put((room, msg_pings, report_data))
Пример #2
0
def tell_rooms(msg, has, hasnt, notify_site="", report_data=None):
    global _rooms

    msg = msg.rstrip()
    target_rooms = set()

    # Go through the list of properties in "has" and add all rooms which have any of those properties
    # to the target_rooms set. _room_roles contains a list of rooms for each property.
    for prop_has in has:
        if isinstance(prop_has, tuple):
            # If the current prop_has is a tuple, then it's assumed to be a descriptor of a specific room.
            # The format is: (_client.host, room.id)
            target_rooms.add(prop_has)

        if prop_has not in _room_roles:
            # No rooms have this property.
            continue

        for room in _room_roles[prop_has]:
            if all(map(lambda prop: prop not in _room_roles or room not in _room_roles[prop], hasnt)):
                if room not in _rooms:
                    # If SD is not already in the room, then join the room.
                    site, roomid = room
                    deletion_watcher = room in _watcher_rooms

                    new_room = _clients[site].get_room(roomid)
                    new_room.join()

                    _rooms[room] = RoomData(new_room, -1, deletion_watcher)

                target_rooms.add(room)

    for room_id in target_rooms:
        room = _rooms[room_id]

        if notify_site:
            pings = datahandling.get_user_names_on_notification_list(room.room._client.host,
                                                                     room.room.id,
                                                                     notify_site,
                                                                     room.room._client)

            msg_pings = datahandling.append_pings(msg, pings)
        else:
            msg_pings = msg

        timestamp = time.time()

        if room.block_time < timestamp and _global_block < timestamp:
            if report_data and "delay" in _room_roles and room_id in _room_roles["delay"]:
                def callback(room=room, msg=msg_pings):
                    post = fetch_post_id_and_site_from_url(report_data[0])[0:2]

                    if not datahandling.is_false_positive(post) and not datahandling.is_ignored_post(post):
                        _msg_queue.put((room, msg, report_data))

                task = Tasks.later(callback, after=300)

                GlobalVars.deletion_watcher.subscribe(report_data[0], callback=task.cancel)
            else:
                _msg_queue.put((room, msg_pings, report_data))
Пример #3
0
def tell_rooms(msg, has, hasnt, notify_site="", report_data=None):
    global _rooms

    msg = msg.rstrip()
    target_rooms = set()

    for prop_has in has:
        if isinstance(prop_has, tuple):
            target_rooms.add(prop_has)

        if prop_has not in _room_roles:
            continue

        for room in _room_roles[prop_has]:
            if all(map(lambda prop: prop not in _room_roles or room not in _room_roles[prop], hasnt)):
                if room not in _rooms:
                    site, roomid = room
                    deletion_watcher = room in _watcher_rooms

                    new_room = _clients[site].get_room(roomid)
                    new_room.join()

                    _rooms[room] = RoomData(new_room, -1, deletion_watcher)

                target_rooms.add(room)

    for room_id in target_rooms:
        room = _rooms[room_id]

        if notify_site:
            pings = datahandling.get_user_names_on_notification_list(room.room._client.host,
                                                                     room.room.id,
                                                                     notify_site,
                                                                     room.room._client)

            msg_pings = datahandling.append_pings(msg, pings)
        else:
            msg_pings = msg

        timestamp = time.time()

        if room.block_time < timestamp and _global_block < timestamp:
            if report_data and "delay" in _room_roles and room_id in _room_roles["delay"]:
                def callback(room=room, msg=msg_pings):
                    post = fetch_post_id_and_site_from_url(report_data[0])[0:2]

                    if not datahandling.is_false_positive(post) and not datahandling.is_ignored_post(post):
                        _msg_queue.put((room, msg, report_data))

                task = Tasks.later(callback, after=300)

                GlobalVars.deletion_watcher.subscribe(report_data[0], callback=task.cancel)
            else:
                _msg_queue.put((room, msg_pings, report_data))
Пример #4
0
def tell_rooms(msg, has, hasnt, notify_site="", report_data=None):
    global _rooms

    msg = msg.rstrip()
    target_rooms = set()

    for prop_has in has:
        if prop_has not in _room_roles:
            continue

        for room in _room_roles[prop_has]:
            if all(
                    map(
                        lambda prop: prop not in _room_roles or room not in
                        _room_roles[prop], hasnt)):
                if room not in _rooms:
                    site, roomid = room
                    deletion_watcher = room in _watcher_rooms

                    new_room = _clients[site].get_room(roomid)
                    new_room.join()

                    _rooms[room] = RoomData(new_room, -1, deletion_watcher)

                target_rooms.add(room)

    for room_id in target_rooms:
        room = _rooms[room_id]

        if notify_site:
            pings = datahandling.get_user_names_on_notification_list(
                room.room._client.host, room.room.id, notify_site,
                room.room._client)

            msg_pings = datahandling.append_pings(msg, pings)
        else:
            msg_pings = msg

        timestamp = time.time()

        if room.block_time < timestamp and _global_block < timestamp:
            if report_data and "delay" in _room_roles and room_id in _room_roles[
                    "delay"]:
                threading.Thread(
                    name="delayed post",
                    target=DeletionWatcher.post_message_if_not_deleted,
                    args=(msg_pings, room, report_data)).start()
            else:
                _msg_queue.put((room, msg_pings, report_data))
def tell_rooms(msg, has, hasnt, notify_site="", report_data=None):
    global _rooms

    msg = msg.rstrip()
    target_rooms = set()

    for prop_has in has:
        if prop_has not in _room_roles:
            continue

        for room in _room_roles[prop_has]:
            if all(map(lambda prop: prop not in _room_roles or room not in _room_roles[prop], hasnt)):
                if room not in _rooms:
                    site, roomid = room
                    deletion_watcher = room in _watcher_rooms

                    new_room = _clients[site].get_room(roomid)
                    new_room.join()

                    _rooms[room] = RoomData(new_room, -1, deletion_watcher)

                target_rooms.add(room)

    for room_id in target_rooms:
        room = _rooms[room_id]

        if notify_site:
            pings = datahandling.get_user_names_on_notification_list(room.room._client.host,
                                                                     room.room.id,
                                                                     notify_site,
                                                                     room.room._client)

            msg_pings = datahandling.append_pings(msg, pings)
        else:
            msg_pings = msg

        timestamp = time.time()

        if room.block_time < timestamp and _global_block < timestamp:
            if report_data and "delay" in _room_roles and room_id in _room_roles["delay"]:
                threading.Thread(name="delayed post",
                                 target=DeletionWatcher.post_message_if_not_deleted,
                                 args=(msg_pings, room, report_data)).start()
            else:
                _msg_queue.put((room, msg_pings, report_data))
def test_append_pings():
    assert append_pings("foo", ["user1", "some user"]) == "foo (@user1 @someuser)"
    assert append_pings("foo", [u"Doorknob 冰"]) == u"foo (@Doorknob冰)"
Пример #7
0
def test_append_pings():
    assert append_pings("foo",
                        ["user1", "some user"]) == "foo (@user1 @someuser)"
    assert append_pings("foo", [u"Doorknob 冰"]) == u"foo (@Doorknob冰)"
Пример #8
0
def handle_spam(title, body, poster, site, post_url, poster_url, post_id, reasons, is_answer, why="", owner_rep=None, post_score=None, up_vote_count=None, down_vote_count=None, question_id=None):
    post_url = parsing.to_protocol_relative(parsing.url_to_shortlink(post_url))
    poster_url = parsing.to_protocol_relative(parsing.user_url_to_shortlink(poster_url))
    reason = ", ".join(reasons)
    reason = reason[:1].upper() + reason[1:]  # reason is capitalised, unlike the entries of reasons list
    datahandling.append_to_latest_questions(site, post_id, title if not is_answer else "")
    if len(reasons) == 1 and ("all-caps title" in reasons or
                              "repeating characters in title" in reasons or
                              "repeating characters in body" in reasons or
                              "repeating characters in answer" in reasons or
                              "repeating words in title" in reasons or
                              "repeating words in body" in reasons or
                              "repeating words in answer" in reasons):
        datahandling.add_auto_ignored_post((post_id, site, datetime.now()))
    if why is not None and why != "":
        datahandling.add_why(site, post_id, why)
    if is_answer and question_id is not None:
        datahandling.add_post_site_id_link((post_id, site, "answer"), question_id)
    try:
        title = parsing.escape_special_chars_in_title(title)
        sanitized_title = regex.sub('https?://', '', title)

        prefix = u"[ [SmokeDetector](//git.io/vgx7b) ]"
        if GlobalVars.metasmoke_key:
            prefix_ms = u"[ [SmokeDetector](//git.io/vgx7b) | [MS](//m.erwaysoftware.com/posts/by-url?url=" + post_url + ") ]"
        else:
            prefix_ms = prefix

        if not poster.strip():
            s = u" {}: [{}]({}) by a deleted user on `{}`".format(reason, sanitized_title.strip(), post_url, site)
            username = ""
            user_link = ""
        else:
            s = u" {}: [{}]({}) by [{}]({}) on `{}`" .format(reason, sanitized_title.strip(), post_url, poster.strip(), poster_url, site)
            username = poster.strip()
            user_link = poster_url

        t_metasmoke = Thread(target=metasmoke.Metasmoke.send_stats_on_post,
                             args=(title, post_url, reason.split(", "), body, username, user_link, why, owner_rep, post_score, up_vote_count, down_vote_count))
        t_metasmoke.start()

        print GlobalVars.parser.unescape(s).encode('ascii', errors='replace')
        if time.time() >= GlobalVars.blockedTime["all"]:
            datahandling.append_to_latest_questions(site, post_id, title)
            if reason not in GlobalVars.experimental_reasons:
                if time.time() >= GlobalVars.blockedTime[GlobalVars.charcoal_room_id]:
                    chq_pings = datahandling.get_user_names_on_notification_list("stackexchange.com", GlobalVars.charcoal_room_id, site, GlobalVars.wrap)
                    chq_msg = prefix + s
                    chq_msg_pings = prefix + datahandling.append_pings(s, chq_pings)
                    chq_msg_pings_ms = prefix_ms + datahandling.append_pings(s, chq_pings)
                    msg_to_send = chq_msg_pings_ms if len(chq_msg_pings_ms) <= 500 else chq_msg_pings if len(chq_msg_pings) <= 500 else chq_msg[0:500]
                    GlobalVars.charcoal_hq.send_message(msg_to_send)
                if not should_reasons_prevent_tavern_posting(reasons) and site not in GlobalVars.non_tavern_sites and time.time() >= GlobalVars.blockedTime[GlobalVars.meta_tavern_room_id]:
                    tavern_pings = datahandling.get_user_names_on_notification_list("meta.stackexchange.com", GlobalVars.meta_tavern_room_id, site, GlobalVars.wrapm)
                    tavern_msg = prefix + s
                    tavern_msg_pings = prefix + datahandling.append_pings(s, tavern_pings)
                    tavern_msg_pings_ms = prefix_ms + datahandling.append_pings(s, tavern_pings)
                    msg_to_send = tavern_msg_pings_ms if len(tavern_msg_pings_ms) <= 500 else tavern_msg_pings if len(tavern_msg_pings) <= 500 else tavern_msg[0:500]
                    t_check_websocket = Thread(target=deletionwatcher.DeletionWatcher.post_message_if_not_deleted, args=((post_id, site, "answer" if is_answer else "question"), post_url, msg_to_send, GlobalVars.tavern_on_the_meta))
                    t_check_websocket.daemon = True
                    t_check_websocket.start()
                if site == "stackoverflow.com" and reason not in GlobalVars.non_socvr_reasons and time.time() >= GlobalVars.blockedTime[GlobalVars.socvr_room_id]:
                    socvr_pings = datahandling.get_user_names_on_notification_list("stackoverflow.com", GlobalVars.socvr_room_id, site, GlobalVars.wrapso)
                    socvr_msg = prefix + s
                    socvr_msg_pings = prefix + datahandling.append_pings(s, socvr_pings)
                    socvr_msg_pings_ms = prefix_ms + datahandling.append_pings(s, socvr_pings)
                    msg_to_send = socvr_msg_pings_ms if len(socvr_msg_pings_ms) <= 500 else socvr_msg_pings if len(socvr_msg_pings) <= 500 else socvr_msg[0:500]
                    GlobalVars.socvr.send_message(msg_to_send)

            for specialroom in GlobalVars.specialrooms:
                sites = specialroom["sites"]
                if site in sites and reason not in specialroom["unwantedReasons"]:
                    room = specialroom["room"]
                    if room.id not in GlobalVars.blockedTime or time.time() >= GlobalVars.blockedTime[room.id]:
                        room_site = room._client.host
                        room_id = int(room.id)
                        room_pings = datahandling.get_user_names_on_notification_list(room_site, room_id, site, room._client)
                        room_msg = prefix + s
                        room_msg_pings = prefix + datahandling.append_pings(s, room_pings)
                        room_msg_pings_ms = prefix_ms + datahandling.append_pings(s, room_pings)
                        msg_to_send = room_msg_pings_ms if len(room_msg_pings_ms) <= 500 else room_msg_pings if len(room_msg_pings) <= 500 else room_msg[0:500]
                        specialroom["room"].send_message(msg_to_send)
    except:
        exc_type, exc_obj, exc_tb = sys.exc_info()
        excepthook.uncaught_exception(exc_type, exc_obj, exc_tb)
Пример #9
0
def handle_spam(title,
                body,
                poster,
                site,
                post_url,
                poster_url,
                post_id,
                reasons,
                is_answer,
                why="",
                owner_rep=None,
                post_score=None,
                up_vote_count=None,
                down_vote_count=None,
                question_id=None):
    post_url = parsing.to_protocol_relative(parsing.url_to_shortlink(post_url))
    poster_url = parsing.to_protocol_relative(
        parsing.user_url_to_shortlink(poster_url))
    reason = ", ".join(reasons)
    reason = reason[:1].upper() + reason[
        1:]  # reason is capitalised, unlike the entries of reasons list
    datahandling.append_to_latest_questions(site, post_id,
                                            title if not is_answer else "")
    if len(reasons) == 1 and ("all-caps title" in reasons
                              or "repeating characters in title" in reasons
                              or "repeating characters in body" in reasons
                              or "repeating characters in answer" in reasons
                              or "repeating words in title" in reasons
                              or "repeating words in body" in reasons
                              or "repeating words in answer" in reasons):
        datahandling.add_auto_ignored_post((post_id, site, datetime.now()))
    if why is not None and why != "":
        datahandling.add_why(site, post_id, why)
    if is_answer and question_id is not None:
        datahandling.add_post_site_id_link((post_id, site, "answer"),
                                           question_id)
    try:
        title = parsing.escape_special_chars_in_title(title)
        sanitized_title = regex.sub('(https?://|\n)', '', title)

        prefix = u"[ [SmokeDetector](//git.io/vgx7b) ]"
        if GlobalVars.metasmoke_key:
            prefix_ms = u"[ [SmokeDetector](//git.io/vgx7b) | [MS](//m.erwaysoftware.com/posts/by-url?url=" + post_url + ") ]"
        else:
            prefix_ms = prefix

        if not poster.strip():
            s = u" {}: [{}]({}) by a deleted user on `{}`".format(
                reason, sanitized_title.strip(), post_url, site)
            username = ""
            user_link = ""
        else:
            s = u" {}: [{}]({}) by [{}]({}) on `{}`".format(
                reason, sanitized_title.strip(), post_url, poster.strip(),
                poster_url, site)
            username = poster.strip()
            user_link = poster_url

        t_metasmoke = Thread(target=metasmoke.Metasmoke.send_stats_on_post,
                             args=(title, post_url, reason.split(", "), body,
                                   username, user_link, why, owner_rep,
                                   post_score, up_vote_count, down_vote_count))
        t_metasmoke.start()

        print GlobalVars.parser.unescape(s).encode('ascii', errors='replace')
        if time.time() >= GlobalVars.blockedTime["all"]:
            datahandling.append_to_latest_questions(site, post_id, title)
            if reason not in GlobalVars.experimental_reasons:
                if time.time() >= GlobalVars.blockedTime[
                        GlobalVars.charcoal_room_id]:
                    chq_pings = datahandling.get_user_names_on_notification_list(
                        "stackexchange.com", GlobalVars.charcoal_room_id, site,
                        GlobalVars.wrap)
                    chq_msg = prefix + s
                    chq_msg_pings = prefix + datahandling.append_pings(
                        s, chq_pings)
                    chq_msg_pings_ms = prefix_ms + datahandling.append_pings(
                        s, chq_pings)
                    msg_to_send = chq_msg_pings_ms if len(
                        chq_msg_pings_ms) <= 500 else chq_msg_pings if len(
                            chq_msg_pings) <= 500 else chq_msg[0:500]
                    GlobalVars.charcoal_hq.send_message(msg_to_send)
                if not should_reasons_prevent_tavern_posting(
                        reasons
                ) and site not in GlobalVars.non_tavern_sites and time.time(
                ) >= GlobalVars.blockedTime[GlobalVars.meta_tavern_room_id]:
                    tavern_pings = datahandling.get_user_names_on_notification_list(
                        "meta.stackexchange.com",
                        GlobalVars.meta_tavern_room_id, site, GlobalVars.wrapm)
                    tavern_msg = prefix + s
                    tavern_msg_pings = prefix + datahandling.append_pings(
                        s, tavern_pings)
                    tavern_msg_pings_ms = prefix_ms + datahandling.append_pings(
                        s, tavern_pings)
                    msg_to_send = tavern_msg_pings_ms if len(
                        tavern_msg_pings_ms
                    ) <= 500 else tavern_msg_pings if len(
                        tavern_msg_pings) <= 500 else tavern_msg[0:500]
                    t_check_websocket = Thread(
                        target=deletionwatcher.DeletionWatcher.
                        post_message_if_not_deleted,
                        args=((post_id, site,
                               "answer" if is_answer else "question"),
                              post_url, msg_to_send,
                              GlobalVars.tavern_on_the_meta))
                    t_check_websocket.daemon = True
                    t_check_websocket.start()
                if site == "stackoverflow.com" and reason not in GlobalVars.non_socvr_reasons and time.time(
                ) >= GlobalVars.blockedTime[GlobalVars.socvr_room_id]:
                    socvr_pings = datahandling.get_user_names_on_notification_list(
                        "stackoverflow.com", GlobalVars.socvr_room_id, site,
                        GlobalVars.wrapso)
                    socvr_msg = prefix + s
                    socvr_msg_pings = prefix + datahandling.append_pings(
                        s, socvr_pings)
                    socvr_msg_pings_ms = prefix_ms + datahandling.append_pings(
                        s, socvr_pings)
                    msg_to_send = socvr_msg_pings_ms if len(
                        socvr_msg_pings_ms) <= 500 else socvr_msg_pings if len(
                            socvr_msg_pings) <= 500 else socvr_msg[0:500]
                    GlobalVars.socvr.send_message(msg_to_send)

            for specialroom in GlobalVars.specialrooms:
                sites = specialroom["sites"]
                if site in sites and reason not in specialroom[
                        "unwantedReasons"]:
                    room = specialroom["room"]
                    if room.id not in GlobalVars.blockedTime or time.time(
                    ) >= GlobalVars.blockedTime[room.id]:
                        room_site = room._client.host
                        room_id = int(room.id)
                        room_pings = datahandling.get_user_names_on_notification_list(
                            room_site, room_id, site, room._client)
                        room_msg = prefix + s
                        room_msg_pings = prefix + datahandling.append_pings(
                            s, room_pings)
                        room_msg_pings_ms = prefix_ms + datahandling.append_pings(
                            s, room_pings)
                        msg_to_send = room_msg_pings_ms if len(
                            room_msg_pings_ms
                        ) <= 500 else room_msg_pings if len(
                            room_msg_pings) <= 500 else room_msg[0:500]
                        specialroom["room"].send_message(msg_to_send)
    except:
        exc_type, exc_obj, exc_tb = sys.exc_info()
        excepthook.uncaught_exception(exc_type, exc_obj, exc_tb)
Пример #10
0
def handle_spam(post, reasons, why):
    post_url = parsing.to_protocol_relative(
        parsing.url_to_shortlink(post.post_url))
    poster_url = parsing.to_protocol_relative(
        parsing.user_url_to_shortlink(post.user_url))
    reason = ", ".join(reasons[:5])
    if len(reasons) > 5:
        reason += ", +{} more".format(len(reasons) - 5)
    reason = reason[:1].upper() + reason[
        1:]  # reason is capitalised, unlike the entries of reasons list
    shortened_site = post.post_site.replace(
        "stackexchange.com", "SE")  # site.stackexchange.com -> site.SE
    datahandling.append_to_latest_questions(
        post.post_site, post.post_id, post.title if not post.is_answer else "")
    if len(reasons) == 1 and ("all-caps title" in reasons
                              or "repeating characters in title" in reasons
                              or "repeating characters in body" in reasons
                              or "repeating characters in answer" in reasons
                              or "repeating words in title" in reasons
                              or "repeating words in body" in reasons
                              or "repeating words in answer" in reasons):
        datahandling.add_auto_ignored_post(
            (post.post_id, post.post_site, datetime.now()))
    if why is not None and why != "":
        datahandling.add_why(post.post_site, post.post_id, why)
    if post.is_answer and post.post_id is not None and post.post_id is not "":
        datahandling.add_post_site_id_link(
            (post.post_id, post.post_site, "answer"), post.parent.post_id)
    try:
        post._title = parsing.escape_special_chars_in_title(post.title)
        if post.is_answer:
            # If the post is an answer type post, the 'title' is going to be blank, so when posting the
            # message contents we need to set the post title to the *parent* title, so the message in the
            # chat is properly constructed with parent title instead. This will make things 'print'
            # in a proper way in chat messages.
            sanitized_title = regex.sub('(https?://|\n)', '',
                                        post.parent.title)
        else:
            sanitized_title = regex.sub('(https?://|\n)', '', post.title)

        sanitized_title = regex.sub(r'([\]*`])', r'\\$1',
                                    sanitized_title).replace('\n', u'\u23CE')

        prefix = u"[ [SmokeDetector](//goo.gl/eLDYqh) ]"
        if GlobalVars.metasmoke_key:
            prefix_ms = u"[ [SmokeDetector](//goo.gl/eLDYqh) | [MS](//m.erwaysoftware.com/posts/by-url?url=" + \
                        post_url + ") ]"
        else:
            prefix_ms = prefix

        if not post.user_name.strip() or (not poster_url
                                          or poster_url.strip() == ""):
            s = u" {}: [{}]({}) by a deleted user on `{}`".format(
                reason, sanitized_title.strip(), post_url, shortened_site)
            username = ""
        else:
            s = u" {}: [{}]({}) by [{}]({}) on `{}`".format(
                reason, sanitized_title.strip(), post_url,
                post.user_name.strip(), poster_url, shortened_site)
            username = post.user_name.strip()

        t_metasmoke = Thread(name="metasmoke send post",
                             target=metasmoke.Metasmoke.send_stats_on_post,
                             args=(post.title_ignore_type, post_url, reasons,
                                   post.body, username, post.user_link, why,
                                   post.owner_rep, post.post_score,
                                   post.up_vote_count, post.down_vote_count))
        t_metasmoke.start()

        log('debug',
            GlobalVars.parser.unescape(s).encode('ascii', errors='replace'))
        if time.time() >= GlobalVars.blockedTime["all"]:
            datahandling.append_to_latest_questions(post.post_site,
                                                    post.post_id, post.title)
            if set(reasons).intersection(
                    GlobalVars.experimental_reasons) != set(reasons):
                if time.time() >= GlobalVars.blockedTime[
                        GlobalVars.charcoal_room_id]:
                    chq_pings = datahandling.get_user_names_on_notification_list(
                        "stackexchange.com", GlobalVars.charcoal_room_id,
                        post.post_site, GlobalVars.wrap)
                    chq_msg = prefix + s
                    chq_msg_pings = prefix + datahandling.append_pings(
                        s, chq_pings)
                    chq_msg_pings_ms = prefix_ms + datahandling.append_pings(
                        s, chq_pings)
                    msg_to_send = chq_msg_pings_ms if len(chq_msg_pings_ms) <= 500 else chq_msg_pings \
                        if len(chq_msg_pings) <= 500 else chq_msg[0:500]
                    try:
                        GlobalVars.charcoal_hq.send_message(msg_to_send)
                    except AttributeError:  # In our Test Suite
                        pass
                if not should_reasons_prevent_tavern_posting(reasons) \
                        and post.post_site not in GlobalVars.non_tavern_sites \
                        and time.time() >= GlobalVars.blockedTime[GlobalVars.meta_tavern_room_id]:
                    tavern_pings = datahandling.get_user_names_on_notification_list(
                        "meta.stackexchange.com",
                        GlobalVars.meta_tavern_room_id, post.post_site,
                        GlobalVars.wrapm)
                    tavern_msg = prefix + s
                    tavern_msg_pings = prefix + datahandling.append_pings(
                        s, tavern_pings)
                    tavern_msg_pings_ms = prefix_ms + datahandling.append_pings(
                        s, tavern_pings)
                    msg_to_send = tavern_msg_pings_ms if len(tavern_msg_pings_ms) <= 500 else tavern_msg_pings \
                        if len(tavern_msg_pings) <= 500 else tavern_msg[0:500]
                    t_check_websocket = Thread(
                        name="deletionwatcher post message if not deleted",
                        target=deletionwatcher.DeletionWatcher.
                        post_message_if_not_deleted,
                        args=((post.post_id, post.post_site,
                               "answer" if post.is_answer else "question"),
                              post_url, msg_to_send,
                              GlobalVars.tavern_on_the_meta))
                    t_check_websocket.daemon = True
                    t_check_websocket.start()
                if post.post_site == "stackoverflow.com" and reason not in GlobalVars.non_socvr_reasons \
                        and time.time() >= GlobalVars.blockedTime[GlobalVars.socvr_room_id]:
                    socvr_pings = datahandling.get_user_names_on_notification_list(
                        "stackoverflow.com", GlobalVars.socvr_room_id,
                        post.post_site, GlobalVars.wrapso)
                    socvr_msg = prefix + s
                    socvr_msg_pings = prefix + datahandling.append_pings(
                        s, socvr_pings)
                    socvr_msg_pings_ms = prefix_ms + datahandling.append_pings(
                        s, socvr_pings)
                    msg_to_send = socvr_msg_pings_ms if len(socvr_msg_pings_ms) <= 500 else socvr_msg_pings \
                        if len(socvr_msg_pings) <= 500 else socvr_msg[0:500]
                    try:
                        GlobalVars.socvr.send_message(msg_to_send)
                    except AttributeError:  # In test Suite
                        pass

            for specialroom in GlobalVars.specialrooms:
                sites = specialroom["sites"]
                if post.post_site in sites and reason not in specialroom[
                        "unwantedReasons"]:
                    room = specialroom["room"]
                    if room.id not in GlobalVars.blockedTime or time.time(
                    ) >= GlobalVars.blockedTime[room.id]:
                        room_site = room._client.host
                        room_id = int(room.id)
                        room_pings = datahandling.get_user_names_on_notification_list(
                            room_site, room_id, post.post_site, room._client)
                        room_msg = prefix + s
                        room_msg_pings = prefix + datahandling.append_pings(
                            s, room_pings)
                        room_msg_pings_ms = prefix_ms + datahandling.append_pings(
                            s, room_pings)
                        msg_to_send = room_msg_pings_ms if len(room_msg_pings_ms) <= 500 else room_msg_pings \
                            if len(room_msg_pings) <= 500 else room_msg[0:500]
                        specialroom["room"].send_message(msg_to_send)
    except:
        exc_type, exc_obj, exc_tb = sys.exc_info()
        excepthook.uncaught_exception(exc_type, exc_obj, exc_tb)
Пример #11
0
def test_append_pings():
    assert append_pings("foo", ["user1", "some user"]) == "foo (@user1 @someuser)"
Пример #12
0
def handle_spam(post, reasons, why):
    post_url = parsing.to_protocol_relative(parsing.url_to_shortlink(post.post_url))
    poster_url = parsing.to_protocol_relative(parsing.user_url_to_shortlink(post.user_url))
    reason = ", ".join(reasons[:5])
    if len(reasons) > 5:
        reason += ", +{} more".format(len(reasons) - 5)
    reason = reason[:1].upper() + reason[1:]  # reason is capitalised, unlike the entries of reasons list
    shortened_site = post.post_site.replace("stackexchange.com", "SE")  # site.stackexchange.com -> site.SE
    datahandling.append_to_latest_questions(post.post_site, post.post_id, post.title if not post.is_answer else "")
    if len(reasons) == 1 and ("all-caps title" in reasons or
                              "repeating characters in title" in reasons or
                              "repeating characters in body" in reasons or
                              "repeating characters in answer" in reasons or
                              "repeating words in title" in reasons or
                              "repeating words in body" in reasons or
                              "repeating words in answer" in reasons):
        datahandling.add_auto_ignored_post((post.post_id, post.post_site, datetime.now()))
    if why is not None and why != "":
        datahandling.add_why(post.post_site, post.post_id, why)
    if post.is_answer and post.post_id is not None and post.post_id is not "":
        datahandling.add_post_site_id_link((post.post_id, post.post_site, "answer"), post.parent.post_id)
    try:
        post._title = parsing.escape_special_chars_in_title(post.title)
        if post.is_answer:
            # If the post is an answer type post, the 'title' is going to be blank, so when posting the
            # message contents we need to set the post title to the *parent* title, so the message in the
            # chat is properly constructed with parent title instead. This will make things 'print'
            # in a proper way in chat messages.
            sanitized_title = regex.sub('(https?://|\n)', '', post.parent.title)
        else:
            sanitized_title = regex.sub('(https?://|\n)', '', post.title)

        sanitized_title = regex.sub(r'([\]*`])', r'\\$1', sanitized_title).replace('\n', u'\u23CE')

        prefix = u"[ [SmokeDetector](//goo.gl/eLDYqh) ]"
        if GlobalVars.metasmoke_key:
            prefix_ms = u"[ [SmokeDetector](//goo.gl/eLDYqh) | [MS](//m.erwaysoftware.com/posts/by-url?url=" + \
                        post_url + ") ]"
        else:
            prefix_ms = prefix

        if not post.user_name.strip() or (not poster_url or poster_url.strip() == ""):
            s = u" {}: [{}]({}) by a deleted user on `{}`".format(reason, sanitized_title.strip(), post_url,
                                                                  shortened_site)
            username = ""
        else:
            s = u" {}: [{}]({}) by [{}]({}) on `{}`".format(reason, sanitized_title.strip(), post_url,
                                                            post.user_name.strip(), poster_url, shortened_site)
            username = post.user_name.strip()

        t_metasmoke = Thread(name="metasmoke send post",
                             target=metasmoke.Metasmoke.send_stats_on_post,
                             args=(post.title_ignore_type, post_url, reasons, post.body, username,
                                   post.user_link, why, post.owner_rep, post.post_score,
                                   post.up_vote_count, post.down_vote_count))
        t_metasmoke.start()

        log('debug', GlobalVars.parser.unescape(s).encode('ascii', errors='replace'))
        if time.time() >= GlobalVars.blockedTime["all"]:
            datahandling.append_to_latest_questions(post.post_site, post.post_id, post.title)
            if set(reasons).intersection(GlobalVars.experimental_reasons) != set(reasons):
                if time.time() >= GlobalVars.blockedTime[GlobalVars.charcoal_room_id]:
                    chq_pings = datahandling.get_user_names_on_notification_list("stackexchange.com",
                                                                                 GlobalVars.charcoal_room_id,
                                                                                 post.post_site,
                                                                                 GlobalVars.wrap)
                    chq_msg = prefix + s
                    chq_msg_pings = prefix + datahandling.append_pings(s, chq_pings)
                    chq_msg_pings_ms = prefix_ms + datahandling.append_pings(s, chq_pings)
                    msg_to_send = chq_msg_pings_ms if len(chq_msg_pings_ms) <= 500 else chq_msg_pings \
                        if len(chq_msg_pings) <= 500 else chq_msg[0:500]
                    try:
                        GlobalVars.charcoal_hq.send_message(msg_to_send)
                    except AttributeError:  # In our Test Suite
                        pass
                if not should_reasons_prevent_tavern_posting(reasons) \
                        and post.post_site not in GlobalVars.non_tavern_sites \
                        and time.time() >= GlobalVars.blockedTime[GlobalVars.meta_tavern_room_id]:
                    tavern_pings = datahandling.get_user_names_on_notification_list("meta.stackexchange.com",
                                                                                    GlobalVars.meta_tavern_room_id,
                                                                                    post.post_site, GlobalVars.wrapm)
                    tavern_msg = prefix + s
                    tavern_msg_pings = prefix + datahandling.append_pings(s, tavern_pings)
                    tavern_msg_pings_ms = prefix_ms + datahandling.append_pings(s, tavern_pings)
                    msg_to_send = tavern_msg_pings_ms if len(tavern_msg_pings_ms) <= 500 else tavern_msg_pings \
                        if len(tavern_msg_pings) <= 500 else tavern_msg[0:500]
                    t_check_websocket = Thread(name="deletionwatcher post message if not deleted",
                                               target=deletionwatcher.DeletionWatcher.post_message_if_not_deleted,
                                               args=((post.post_id, post.post_site,
                                                      "answer" if post.is_answer else "question"),
                                                     post_url, msg_to_send, GlobalVars.tavern_on_the_meta))
                    t_check_websocket.daemon = True
                    t_check_websocket.start()
                if post.post_site == "stackoverflow.com" and reason not in GlobalVars.non_socvr_reasons \
                        and time.time() >= GlobalVars.blockedTime[GlobalVars.socvr_room_id]:
                    socvr_pings = datahandling.get_user_names_on_notification_list("stackoverflow.com",
                                                                                   GlobalVars.socvr_room_id,
                                                                                   post.post_site,
                                                                                   GlobalVars.wrapso)
                    socvr_msg = prefix + s
                    socvr_msg_pings = prefix + datahandling.append_pings(s, socvr_pings)
                    socvr_msg_pings_ms = prefix_ms + datahandling.append_pings(s, socvr_pings)
                    msg_to_send = socvr_msg_pings_ms if len(socvr_msg_pings_ms) <= 500 else socvr_msg_pings \
                        if len(socvr_msg_pings) <= 500 else socvr_msg[0:500]
                    try:
                        GlobalVars.socvr.send_message(msg_to_send)
                    except AttributeError:  # In test Suite
                        pass

            for specialroom in GlobalVars.specialrooms:
                sites = specialroom["sites"]
                if post.post_site in sites and reason not in specialroom["unwantedReasons"]:
                    room = specialroom["room"]
                    if room.id not in GlobalVars.blockedTime or time.time() >= GlobalVars.blockedTime[room.id]:
                        room_site = room._client.host
                        room_id = int(room.id)
                        room_pings = datahandling.get_user_names_on_notification_list(room_site, room_id,
                                                                                      post.post_site, room._client)
                        room_msg = prefix + s
                        room_msg_pings = prefix + datahandling.append_pings(s, room_pings)
                        room_msg_pings_ms = prefix_ms + datahandling.append_pings(s, room_pings)
                        msg_to_send = room_msg_pings_ms if len(room_msg_pings_ms) <= 500 else room_msg_pings \
                            if len(room_msg_pings) <= 500 else room_msg[0:500]
                        specialroom["room"].send_message(msg_to_send)
    except:
        exc_type, exc_obj, exc_tb = sys.exc_info()
        excepthook.uncaught_exception(exc_type, exc_obj, exc_tb)