2021-05-06 05:38:38 +00:00
# This file is part of Recent changes Goat compatible Discord webhook (RcGcDw).
#
# RcGcDw is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# RcGcDw is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with RcGcDw. If not, see <http://www.gnu.org/licenses/>.
import ipaddress
import logging
from src . discord . message import DiscordMessage
from src . api import formatter
from src . api . context import Context
from src . api . util import embed_helper , sanitize_to_url , parse_mediawiki_changes , clean_link , compact_author , \
create_article_path , sanitize_to_markdown
2022-09-27 09:04:48 +00:00
# Order results from most drastic first to less drastic last
abuselog_results = [ " degroup " , " blockautopromote " , " rangeblock " , " block " , " disallow " , " throttle " , " warn " , " tag " , " " ]
abusefilter_results = lambda string , _ , default : { " degroup " : _ ( " **Removed from privileged groups** " ) , " blockautopromote " : _ ( " Removed autoconfirmed group " ) , " rangeblock " : _ ( " **IP range blocked** " ) , " block " : _ ( " **Blocked user** " ) , " disallow " : _ ( " Disallowed the action " ) , " throttle " : _ ( " Throttled actions " ) , " warn " : _ ( " Warning issued " ) , " tag " : _ ( " Tagged the edit " ) , " " : _ ( " None " ) } . get ( string , default )
abusefilter_actions = lambda string , _ , default : { " edit " : _ ( " Edit " ) , " upload " : _ ( " Upload " ) , " move " : _ ( " Move " ) , " stashupload " : _ ( " Stash upload " ) , " delete " : _ ( " Deletion " ) , " createaccount " : _ ( " Account creation " ) , " autocreateaccount " : _ ( " Auto account creation " ) } . get ( string , default )
2021-05-06 05:38:38 +00:00
logger = logging . getLogger ( " extensions.base " )
2021-05-06 09:25:16 +00:00
# AbuseFilter - https://www.mediawiki.org/wiki/Special:MyLanguage/Extension:AbuseFilter
# Processing Abuselog LOG events, separate from RC logs
2021-05-06 05:38:38 +00:00
2022-06-28 12:01:56 +00:00
2022-09-27 09:04:48 +00:00
def abuselog_action ( results ) :
action = " unknown "
for result in abuselog_results :
if result in results :
action = " abuselog/ {} " . format ( result )
break
return action
2022-06-28 12:01:56 +00:00
def abuse_filter_format_user ( change , settings ) :
2021-05-06 05:38:38 +00:00
author = change [ " user " ]
if settings . get ( " hide_ips " , False ) :
try :
ipaddress . ip_address ( change [ " user " ] )
except ValueError :
pass
else :
author = _ ( " Unregistered user " )
return author
@formatter.embed ( event = " abuselog " )
2021-05-06 09:25:16 +00:00
def embed_abuselog ( ctx : Context , change : dict ) :
2022-09-27 09:04:48 +00:00
results = change [ " result " ] . split ( " , " )
action = abuselog_action ( results )
2021-05-06 09:25:16 +00:00
embed = DiscordMessage ( ctx . message_type , action , ctx . webhook_url )
2022-06-28 12:01:56 +00:00
author = abuse_filter_format_user ( change , ctx . settings )
2022-07-11 11:51:03 +00:00
embed [ " title " ] = ctx . _ ( " {user} triggered \" {abuse_filter} \" " ) . format ( user = author , abuse_filter = sanitize_to_markdown ( change [ " filter " ] ) )
2022-09-27 09:04:48 +00:00
embed . add_field ( ctx . _ ( " Performed " ) , abusefilter_actions ( change [ " action " ] , ctx . _ , change [ " action " ] ) )
2022-07-11 11:51:03 +00:00
embed . add_field ( ctx . _ ( " Title " ) , sanitize_to_markdown ( change . get ( " title " , ctx . _ ( " Unknown " ) ) ) )
2022-09-27 09:04:48 +00:00
embed . add_field ( ctx . _ ( " Action taken " ) , ctx . _ ( " , " ) . join ( [ abusefilter_results ( result , ctx . _ , result ) for result in results ] ) )
2021-05-06 05:38:38 +00:00
return embed
2021-05-06 09:25:16 +00:00
@formatter.compact ( event = " abuselog " )
def compact_abuselog ( ctx : Context , change : dict ) :
2022-09-27 09:04:48 +00:00
results = change [ " result " ] . split ( " , " )
action = abuselog_action ( results )
2021-05-06 09:25:16 +00:00
author_url = clean_link ( create_article_path ( " User: {user} " . format ( user = change [ " user " ] ) ) )
2022-06-28 12:01:56 +00:00
author = abuse_filter_format_user ( change , ctx . settings )
2022-07-11 11:51:03 +00:00
message = ctx . _ ( " [ {author} ]( {author_url} ) triggered * {abuse_filter} *, performing the action \" {action} \" on *[ {target} ]( {target_url} )* - action taken: {result} . " ) . format (
2021-05-16 14:52:27 +00:00
author = author , author_url = author_url , abuse_filter = sanitize_to_markdown ( change [ " filter " ] ) ,
2022-09-27 09:04:48 +00:00
action = abusefilter_actions ( change [ " action " ] , ctx . _ , change [ " action " ] ) , target = change . get ( " title " , ctx . _ ( " Unknown " ) ) ,
2022-07-11 11:51:03 +00:00
target_url = clean_link ( create_article_path ( sanitize_to_url ( change . get ( " title " , ctx . _ ( " Unknown " ) ) ) ) ) ,
2022-09-27 09:04:48 +00:00
result = ctx . _ ( " , " ) . join ( [ abusefilter_results ( result , ctx . _ , result ) for result in results ] )
2021-05-06 09:25:16 +00:00
return DiscordMessage ( ctx . message_type , action , ctx . webhook_url , content = message )
# abusefilter/modify - AbuseFilter filter modification
2021-06-20 00:43:43 +00:00
@formatter.embed ( event = " abusefilter/modify " )
2021-05-06 09:25:16 +00:00
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 " ] ) )
2022-07-11 11:51:03 +00:00
embed [ " title " ] = ctx . _ ( " Edited abuse filter number {number} " ) . format ( number = change [ " logparams " ] [ ' newId ' ] )
2021-05-06 09:25:16 +00:00
return embed
2021-06-20 00:43:43 +00:00
@formatter.compact ( event = " abusefilter/modify " )
2021-05-06 09:25:16 +00:00
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 " ] ) ) )
2022-07-11 11:51:03 +00:00
content = ctx . _ ( " [ {author} ]( {author_url} ) edited abuse filter [number {number} ]( {filter_url} ) " ) . format ( author = author ,
2021-05-06 09:25:16 +00:00
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
2021-06-20 00:43:43 +00:00
@formatter.embed ( event = " abusefilter/create " )
2021-05-06 09:25:16 +00:00
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 ' ] ) )
2022-07-11 11:51:03 +00:00
embed [ " title " ] = ctx . _ ( " Created abuse filter number {number} " ) . format ( number = change [ " logparams " ] [ ' newId ' ] )
2021-05-06 09:25:16 +00:00
return embed
2022-06-28 12:01:56 +00:00
2021-06-20 00:43:43 +00:00
@formatter.compact ( event = " abusefilter/create " )
2021-05-06 09:25:16 +00:00
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 ' ] ) ) )
2022-07-11 11:51:03 +00:00
content = ctx . _ ( " [ {author} ]( {author_url} ) created abuse filter [number {number} ]( {filter_url} ) " ) . format ( author = author ,
2021-05-06 09:25:16 +00:00
author_url = author_url ,
number = change [
" logparams " ] [
' newId ' ] ,
filter_url = link )
return DiscordMessage ( ctx . message_type , ctx . event , ctx . webhook_url , content = content )