From 0f6cf9da4d4505c503aae2da9b3e5af4ce669b57 Mon Sep 17 00:00:00 2001 From: Frisk Date: Thu, 6 May 2021 11:25:16 +0200 Subject: [PATCH] Added abusefilter filters --- extensions/base/abusefilter.py | 73 +++++++++++++++++++++++++++++++++- extensions/base/mediawiki.py | 26 ++++++++++-- src/rc_formatters.py | 64 ++++------------------------- 3 files changed, 101 insertions(+), 62 deletions(-) diff --git a/extensions/base/abusefilter.py b/extensions/base/abusefilter.py index f02b2f3..7c82e3a 100644 --- a/extensions/base/abusefilter.py +++ b/extensions/base/abusefilter.py @@ -31,6 +31,8 @@ abusefilter_actions = {"edit": _("Edit"), "upload": _("Upload"), "move": _("Move logger = logging.getLogger("extensions.base") +# AbuseFilter - https://www.mediawiki.org/wiki/Special:MyLanguage/Extension:AbuseFilter +# Processing Abuselog LOG events, separate from RC logs def abuse_filter_format_user(change): author = change["user"] @@ -45,12 +47,79 @@ def abuse_filter_format_user(change): @formatter.embed(event="abuselog") -def embed_abuselog(ctx, change): +def embed_abuselog(ctx: Context, change: dict): action = "abuselog/{}".format(change["result"]) - embed = DiscordMessage("embed", action, settings["webhookURL"]) + embed = DiscordMessage(ctx.message_type, action, ctx.webhook_url) author = abuse_filter_format_user(change) embed["title"] = _("{user} triggered \"{abuse_filter}\"").format(user=author, abuse_filter=sanitize_to_markdown(change["filter"])) embed.add_field(_("Performed"), abusefilter_actions.get(change["action"], _("Unknown"))) embed.add_field(_("Action taken"), abusefilter_results.get(change["result"], _("Unknown"))) embed.add_field(_("Title"), sanitize_to_markdown(change.get("title", _("Unknown")))) return embed + + +@formatter.compact(event="abuselog") +def compact_abuselog(ctx: Context, change: dict): + action = "abuselog/{}".format(change["result"]) + author_url = clean_link(create_article_path("User:{user}".format(user=change["user"]))) + author = abuse_filter_format_user(change) + message = _("[{author}]({author_url}) triggered *{abuse_filter}*, performing the action \"{action}\" on *[{target}]({target_url})* - action taken: {result}.").format( + author=author, author_url=author_url, abuse_filter=change["filter"], + action=abusefilter_actions.get(change["action"], _("Unknown")), target=change.get("title", _("Unknown")), + target_url=clean_link(create_article_path(sanitize_to_url(change.get("title", _("Unknown"))))), + result=abusefilter_results.get(change["result"], _("Unknown"))) + return DiscordMessage(ctx.message_type, action, ctx.webhook_url, content=message) + +# abusefilter/modify - AbuseFilter filter modification + + +@formatter.embed(event="abuselog/modify") +def embed_abuselog_modify(ctx: Context, change: dict): + embed = DiscordMessage(ctx.message_type, ctx.event, ctx.webhook_url) + embed_helper(ctx, embed, change) + embed["url"] = create_article_path( + "Special:AbuseFilter/history/{number}/diff/prev/{historyid}".format(number=change["logparams"]['newId'], + historyid=change["logparams"]["historyId"])) + embed["title"] = _("Edited abuse filter number {number}").format(number=change["logparams"]['newId']) + return embed + + +@formatter.compact(event="abuselog/modify") +def compact_abuselog_modify(ctx: Context, change: dict): + author, author_url = compact_author(ctx, change) + link = clean_link(create_article_path( + "Special:AbuseFilter/history/{number}/diff/prev/{historyid}".format(number=change["logparams"]['newId'], + historyid=change["logparams"][ + "historyId"]))) + + content = _("[{author}]({author_url}) edited abuse filter [number {number}]({filter_url})").format(author=author, + author_url=author_url, + number=change[ + "logparams"][ + 'newId'], + filter_url=link) + return DiscordMessage(ctx.message_type, ctx.event, ctx.webhook_url, content=content) + +# abusefilter/create - AbuseFilter filter creation + + +@formatter.embed(event="abuselog/create") +def embed_abuselog_create(ctx: Context, change: dict): + embed = DiscordMessage(ctx.message_type, ctx.event, ctx.webhook_url) + embed_helper(ctx, embed, change) + embed["url"] = create_article_path("Special:AbuseFilter/{number}".format(number=change["logparams"]['newId'])) + embed["title"] = _("Created abuse filter number {number}").format(number=change["logparams"]['newId']) + return embed + +@formatter.compact(event="abuselog/create") +def compact_abuselog_create(ctx: Context, change: dict): + author, author_url = compact_author(ctx, change) + link = clean_link( + create_article_path("Special:AbuseFilter/{number}".format(number=change["logparams"]['newId']))) + content = _("[{author}]({author_url}) created abuse filter [number {number}]({filter_url})").format(author=author, + author_url=author_url, + number=change[ + "logparams"][ + 'newId'], + filter_url=link) + return DiscordMessage(ctx.message_type, ctx.event, ctx.webhook_url, content=content) diff --git a/extensions/base/mediawiki.py b/extensions/base/mediawiki.py index e65cbce..47d22bd 100644 --- a/extensions/base/mediawiki.py +++ b/extensions/base/mediawiki.py @@ -197,15 +197,35 @@ def embed_upload_upload(ctx, change) -> DiscordMessage: return embed +@formatter.compact(event="upload/revert", mode="compact") +def compact_upload_revert(ctx, change) -> DiscordMessage: + author, author_url = compact_author(ctx, change) + file_link = clean_link(create_article_path(sanitize_to_url(change["title"]))) + parsed_comment = "" if ctx.parsedcomment is None else " *(" + ctx.parsedcomment + ")*" + content = _("[{author}]({author_url}) reverted a version of [{file}]({file_link}){comment}").format( + author=author, author_url=author_url, file=sanitize_to_markdown(change["title"]), file_link=file_link, comment=parsed_comment) + return DiscordMessage(ctx.message_type, ctx.event, ctx.webhook_url, content=content) + + +@formatter.compact(event="upload/overwrite", mode="compact") +def compact_upload_overwrite(ctx, change) -> DiscordMessage: + author, author_url = compact_author(ctx, change) + file_link = clean_link(create_article_path(sanitize_to_url(change["title"]))) + parsed_comment = "" if ctx.parsedcomment is None else " *(" + ctx.parsedcomment + ")*" + content = _("[{author}]({author_url}) uploaded a new version of [{file}]({file_link}){comment}").format(author=author, author_url=author_url, file=sanitize_to_markdown(change["title"]), file_link=file_link, comment=parsed_comment) + return DiscordMessage(ctx.message_type, ctx.event, ctx.webhook_url, content=content) + + @formatter.compact(event="upload/upload", mode="compact") def compact_upload_upload(ctx, change) -> DiscordMessage: author, author_url = compact_author(ctx, change) file_link = clean_link(create_article_path(sanitize_to_url(change["title"]))) + parsed_comment = "" if ctx.parsedcomment is None else " *(" + ctx.parsedcomment + ")*" content = _("[{author}]({author_url}) uploaded [{file}]({file_link}){comment}").format(author=author, author_url=author_url, - file=change["title"], + file=sanitize_to_markdown(change["title"]), file_link=file_link, - comment="" if ctx.parsedcomment is None else " *(" + ctx.parsedcomment + ")*") + comment=parsed_comment) return DiscordMessage(ctx.message_type, ctx.event, ctx.webhook_url, content=content) @@ -227,7 +247,7 @@ def compact_delete_delete(ctx, change) -> DiscordMessage: page_link = clean_link(create_article_path(sanitize_to_url(change["title"]))) content = _("[{author}]({author_url}) deleted [{page}]({page_link}){comment}").format(author=author, author_url=author_url, - page=change["title"], + page=sanitize_to_markdown(change["title"]), page_link=page_link, comment=parsed_comment) return DiscordMessage(ctx.message_type, ctx.event, ctx.webhook_url, content=content) diff --git a/src/rc_formatters.py b/src/rc_formatters.py index 8c82e01..1fa3d9f 100644 --- a/src/rc_formatters.py +++ b/src/rc_formatters.py @@ -81,21 +81,6 @@ def format_user(change, recent_changes, action): return change["user"], author_url - - - -def compact_abuselog_formatter(change, recent_changes): - action = "abuselog/{}".format(change["result"]) - author_url = link_formatter(create_article_path("User:{user}".format(user=change["user"]))) - author = abuse_filter_format_user(change) - message = _("[{author}]({author_url}) triggered *{abuse_filter}*, performing the action \"{action}\" on *[{target}]({target_url})* - action taken: {result}.").format( - author=author, author_url=author_url, abuse_filter=change["filter"], - action=abusefilter_actions.get(change["action"], _("Unknown")), target=change.get("title", _("Unknown")), - target_url=link_formatter(create_article_path(change.get("title", _("Unknown")))), - result=abusefilter_results.get(change["result"], _("Unknown"))) - send_to_discord(DiscordMessage("compact", action, settings["webhookURL"], content=message), meta=DiscordMessageMetadata("POST")) - - def compact_formatter(action, change, parsed_comment, categories, recent_changes): request_metadata = DiscordMessageMetadata("POST", rev_id=change.get("revid", None), log_id=change.get("logid", None), page_id=change.get("pageid", None)) if action != "suppressed": @@ -107,43 +92,14 @@ def compact_formatter(action, change, parsed_comment, categories, recent_changes author = change["user"] parsed_comment = "" if parsed_comment is None else " *("+parsed_comment+")*" if action in ["edit", "new"]: - edit_link = link_formatter("{wiki}index.php?title={article}&curid={pageid}&diff={diff}&oldid={oldrev}".format( - wiki=WIKI_SCRIPT_PATH, pageid=change["pageid"], diff=change["revid"], oldrev=change["old_revid"], - article=change["title"])) - logger.debug(edit_link) - edit_size = change["newlen"] - change["oldlen"] - sign = "" - if edit_size > 0: - sign = "+" - bold = "" - if abs(edit_size) > 500: - bold = "**" - if change["title"].startswith("MediaWiki:Tag-"): - pass - if action == "edit": - content = _("[{author}]({author_url}) edited [{article}]({edit_link}){comment} {bold}({sign}{edit_size}){bold}").format(author=author, author_url=author_url, article=change["title"], edit_link=edit_link, comment=parsed_comment, edit_size=edit_size, sign=sign, bold=bold) - else: - content = _("[{author}]({author_url}) created [{article}]({edit_link}){comment} {bold}({sign}{edit_size}){bold}").format(author=author, author_url=author_url, article=change["title"], edit_link=edit_link, comment=parsed_comment, edit_size=edit_size, sign=sign, bold=bold) elif action =="upload/upload": - file_link = link_formatter(create_article_path(change["title"])) - content = _("[{author}]({author_url}) uploaded [{file}]({file_link}){comment}").format(author=author, - author_url=author_url, - file=change["title"], - file_link=file_link, - comment=parsed_comment) + elif action == "upload/revert": - file_link = link_formatter(create_article_path(change["title"])) - content = _("[{author}]({author_url}) reverted a version of [{file}]({file_link}){comment}").format( - author=author, author_url=author_url, file=change["title"], file_link=file_link, comment=parsed_comment) + elif action == "upload/overwrite": - file_link = link_formatter(create_article_path(change["title"])) - content = _("[{author}]({author_url}) uploaded a new version of [{file}]({file_link}){comment}").format(author=author, author_url=author_url, file=change["title"], file_link=file_link, comment=parsed_comment) + elif action == "delete/delete": - page_link = link_formatter(create_article_path(change["title"])) - content = _("[{author}]({author_url}) deleted [{page}]({page_link}){comment}").format(author=author, author_url=author_url, page=change["title"], page_link=page_link, - comment=parsed_comment) - if AUTO_SUPPRESSION_ENABLED: - delete_messages(dict(pageid=change.get("pageid"))) + elif action == "delete/delete_redir": elif action == "move/move": @@ -245,12 +201,8 @@ def compact_formatter(action, change, parsed_comment, categories, recent_changes elif action == "import/interwiki": elif action == "abusefilter/modify": - link = link_formatter(create_article_path("Special:AbuseFilter/history/{number}/diff/prev/{historyid}".format(number=change["logparams"]['newId'], historyid=change["logparams"]["historyId"]))) - content = _("[{author}]({author_url}) edited abuse filter [number {number}]({filter_url})").format(author=author, author_url=author_url, number=change["logparams"]['newId'], filter_url=link) elif action == "abusefilter/create": - link = link_formatter( - create_article_path("Special:AbuseFilter/{number}".format(number=change["logparams"]['newId']))) - content = _("[{author}]({author_url}) created abuse filter [number {number}]({filter_url})").format(author=author, author_url=author_url, number=change["logparams"]['newId'], filter_url=link) + elif action == "merge/merge": elif action == "newusers/autocreate": @@ -628,11 +580,9 @@ def embed_formatter(action, change, parsed_comment, categories, recent_changes): elif action == "import/interwiki": elif action == "abusefilter/modify": - link = create_article_path("Special:AbuseFilter/history/{number}/diff/prev/{historyid}".format(number=change["logparams"]['newId'], historyid=change["logparams"]["historyId"])) - embed["title"] = _("Edited abuse filter number {number}").format(number=change["logparams"]['newId']) + elif action == "abusefilter/create": - link = create_article_path("Special:AbuseFilter/{number}".format(number=change["logparams"]['newId'])) - embed["title"] = _("Created abuse filter number {number}").format(number=change["logparams"]['newId']) + elif action == "merge/merge": elif action == "newusers/autocreate":