-
Notifications
You must be signed in to change notification settings - Fork 7
feat: add tiktok link sanitization #429
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
namemcguffin
wants to merge
19
commits into
dev
Choose a base branch
from
sanitizer
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 4 commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
e2dc5e6
feat: added tiktok link sanitation
namemcguffin adf345d
you assumed poorly friend
namemcguffin 92626bf
docs: added new `config.ini` options to `README.md`
namemcguffin 61a669d
added missing pillow dependency
namemcguffin 90a5332
Merge branch 'dev' into sanitizer
namemcguffin 22c5d86
change: removed opt out functionality due to newer tiktok tracking fe…
namemcguffin 9f9e74b
Merge branch 'dev' into sanitizer
namemcguffin d007050
fix: fix lockfile
namemcguffin 9aeb5fb
fix: removed remaining mentions of opt out functionality.
namemcguffin 83d49be
change: made link regexes stricter by enforcing at least one characte…
namemcguffin 169d92f
change: bot no longer DMs user, instead warning message is sent in ch…
namemcguffin 2d682e8
change: made suggested changes, replacement message now uses an embed.
namemcguffin edfddc2
change: removed emojis from footer.
namemcguffin 24b8ed4
you attain powerful fealty
namemcguffin d1fc4df
fix: added missing characters in regex
namemcguffin 0daca7a
fix: fixed regex matching on sanitized links.
namemcguffin a70a5db
yesterday another pope flew
namemcguffin 01c9b8e
refactor: made repeated code into a loop
namemcguffin 892f741
Merge branch 'dev' into sanitizer
namemcguffin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| # Copyright (C) idoneam (2016-2021) | ||
| # | ||
| # This file is part of Canary | ||
| # | ||
| # Canary 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. | ||
| # | ||
| # Canary 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 Canary. If not, see <https://www.gnu.org/licenses/>. | ||
| import discord | ||
|
namemcguffin marked this conversation as resolved.
|
||
| from discord import utils | ||
| from discord.ext import commands | ||
| import re | ||
| import requests | ||
|
|
||
| TIKTOK_SHORTLINK = re.compile(r"https?:\/\/vm\.tiktok\.com\/[A-Za-z0-9]*") | ||
| TIKTOK_MOBILE = re.compile( | ||
| r"(https?:\/\/m\.tiktok\.com\/v\/[0-9]*)\.html\?[A-Za-z0-9_&=%\.\?-]*") | ||
| TIKTOK_DESKTOP = re.compile( | ||
| r"(https?:\/\/www\.tiktok\.com\/@[A-Za-z0-9_\.]*\/video\/[0-9]*)\?[A-Za-z0-9_&=%\.\?-]*" | ||
| ) | ||
|
|
||
|
|
||
| def unroll_tiktok(link): | ||
| return requests.head(link, | ||
| headers={ | ||
| "User-Agent": "Mozilla/5.0 (X11)" | ||
| }, | ||
| allow_redirects=True).url | ||
|
|
||
|
|
||
| class Sanitizer(commands.Cog): | ||
| def __init__(self, bot): | ||
| self.bot = bot | ||
|
|
||
| @commands.Cog.listener("on_message") | ||
| async def tiktok_link_sanitizer(self, msg): | ||
| if (not isinstance(msg.author, discord.Member)) or utils.get( | ||
| msg.author.roles, | ||
| name=self.bot.config.sanitation["tt_optout"]): | ||
| return | ||
| replace: bool = False | ||
| msg_txt: str = str(msg.content) | ||
| for short in TIKTOK_SHORTLINK.finditer(msg_txt): | ||
| short_match = short.group() | ||
| msg_txt = msg_txt.replace(short_match, unroll_tiktok(short_match)) | ||
| replace = True | ||
| for mobile in TIKTOK_MOBILE.finditer(msg_txt): | ||
| full, clean = mobile.group(0, 1) | ||
| msg_txt = msg_txt.replace(full, unroll_tiktok(clean)) | ||
| replace = True | ||
| for desktop in TIKTOK_DESKTOP.finditer(msg_txt): | ||
| full, clean = desktop.group(0, 1) | ||
| msg_txt = msg_txt.replace(full, clean) | ||
| replace = True | ||
| if replace: | ||
| await msg.delete() | ||
| await msg.channel.send(f"from: `{msg.author}`\n>>> {msg_txt}") | ||
| if not utils.get(msg.author.roles, | ||
| name=self.bot.config.sanitation["tt_silent"]): | ||
| dm_channel = msg.author.dm_channel or await msg.author.create_dm( | ||
| ) | ||
| await dm_channel.send( | ||
| f"WARNING: a message you sent contained a tiktok link" | ||
| " that could potentially contain sensitive information." | ||
| "\nas such, it has been deleted and a sanitized version of" | ||
| " the message was resent.\nto opt out of this feature, feel" | ||
| f" free to request the `{self.bot.config.sanitation['tt_optout']}`" | ||
| "role.\nto still have this feature enabled but to no longer receive" | ||
| f" these message, feel free to request the `{self.bot.config.sanitation['tt_silent']}`" | ||
| f" role.\nhere is the message in question:\n>>> {msg.content}" | ||
| ) | ||
|
|
||
|
|
||
| def setup(bot): | ||
| bot.add_cog(Sanitizer(bot)) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.