From fa641a50c17d058c841c1da68a8302514181c8b5 Mon Sep 17 00:00:00 2001 From: Aevann1 Date: Tue, 1 Mar 2022 01:01:57 +0200 Subject: [PATCH] regex --- files/helpers/const.py | 24 +++++++++++++++++++----- files/helpers/sanitize.py | 34 +++++++++++++--------------------- files/routes/admin.py | 12 ++++++------ files/routes/front.py | 3 +-- 4 files changed, 39 insertions(+), 34 deletions(-) diff --git a/files/helpers/const.py b/files/helpers/const.py index 76122d12d2..dd0b284ed4 100644 --- a/files/helpers/const.py +++ b/files/helpers/const.py @@ -130,6 +130,7 @@ if SITE in {'rdrama.net','devrama.xyz'}: AUTOCHOICE_ID = 9167 BASEDBOT_ID = 0 + A_ID = 1230 MENTION_ID = 9376 KIPPY_ID = 7150 TAX_NOTIF_ID = 995 @@ -162,6 +163,7 @@ elif SITE == "pcmemes.net": AUTOCHOICE_ID = 2072 BASEDBOT_ID = 800 + A_ID = 0 MENTION_ID = 0 KIPPY_ID = 1592 PIZZASHILL_ID = 0 @@ -194,6 +196,7 @@ else: AUTOCHOICE_ID = 8 BASEDBOT_ID = 0 + A_ID = 0 MENTION_ID = 0 KIPPY_ID = 0 TAX_NOTIF_ID = 9 @@ -649,16 +652,18 @@ db.close() if SITE_NAME == 'PCM': valid_username_regex = re.compile("^[a-zA-Z0-9_\-А-я]{3,25}$", flags=re.A) - mention_regex = re.compile('(^|\s|\n|

)@(([a-zA-Z0-9_\-А-я]){3,25})', flags=re.A) + mention_regex = re.compile('(^|\s|

)@(([a-zA-Z0-9_\-А-я]){3,25})', flags=re.A) + mention_regex2 = re.compile('

@(([a-zA-Z0-9_\-А-я]){3,25})', flags=re.A) else: valid_username_regex = re.compile("^[a-zA-Z0-9_\-]{3,25}$", flags=re.A) - mention_regex = re.compile('(^|\s|\n|

)@(([a-zA-Z0-9_\-]){1,25})', flags=re.A) + mention_regex = re.compile('(^|\s|

)@(([a-zA-Z0-9_\-]){1,25})', flags=re.A) + mention_regex2 = re.compile('

@(([a-zA-Z0-9_\-]){1,25})', flags=re.A) valid_password_regex = re.compile("^.{8,100}$", flags=re.A) marsey_regex = re.compile("^(:[!#A-Za-z0-9]{1,30}?:\s*)+$", flags=re.A) -image_regex = re.compile("(^https:\/\/.*\.(png|jpg|jpeg|gif|webp|PNG|JPG|JPEG|GIF|WEBP|9999)($|\s|\n))", flags=re.M|re.A) +image_regex = re.compile("(^https:\/\/.*\.(png|jpg|jpeg|gif|webp|maxwidth=9999|fidelity=high)($|\s))", flags=re.I|re.M|re.A) valid_sub_regex = re.compile("^[a-zA-Z0-9_\-]{3,20}$", flags=re.A) @@ -676,10 +681,19 @@ based_regex = re.compile("based and (.{1,20}?)(-| )pilled", flags=re.I|re.A) controversial_regex = re.compile('(/comments/.*?)"', flags=re.A) +spoiler_regex = re.compile('\|\|(.*?)\|\|', flags=re.A) +video_regex = re.compile('

(https:\/\/.*?\.(mp4|webm|mov))<\/a><\/p>', flags=re.I|re.A) +unlinked_regex = re.compile('(^|\s|

)(https:\/\/[^ <>]*)', flags=re.A) +imgur_regex = re.compile('(https://i\.imgur\.com/([^_]*?))\.(jpg|png|jpeg|webp)(?!)', flags=re.A) +reddit_regex = re.compile('(^|\s|

)\/?((r|u)\/(\w|-){3,25})', flags=re.A) +sub_regex = re.compile('(^|\s|

)\/?(s\/(\w|-){3,25})', flags=re.A) +youtube_regex = re.compile('" target="_blank">(https://youtube\.com/watch\?v\=(.*?))(?!)', flags=re.A) +strikethrough_regex = re.compile('~~(.*?)~~', flags=re.A) + slur_regex = re.compile(rf"((?<=\s|>)|^)({single_words})((?=[\s<,.$]|s[\s<,.$]))", flags=re.I|re.A) slur_regex_upper = re.compile(rf"((?<=\s|>)|^)({single_words.upper()})((?=[\s<,.$]|S[\s<,.$]))", flags=re.A) -torture_regex = re.compile('(^|\s|\n)(i|me) ', flags=re.I|re.A) -torture_regex2 = re.compile("(^|\s|\n)i'm ", flags=re.I|re.A) +torture_regex = re.compile('(^|\s)(i|me) ', flags=re.I|re.A) +torture_regex2 = re.compile("(^|\s)i'm ", flags=re.I|re.A) def sub_matcher(match): return SLURS[match.group(0).lower()] diff --git a/files/helpers/sanitize.py b/files/helpers/sanitize.py index 8ab6bbee55..39540fea6c 100644 --- a/files/helpers/sanitize.py +++ b/files/helpers/sanitize.py @@ -116,14 +116,14 @@ def sanitize(sanitized, noimages=False, alert=False, comment=False, edit=False): sanitized = sanitized.replace("\ufeff", "").replace("𒐪","").replace("","").replace('‎','') if alert: - for i in re.finditer("

@((\w|-){1,25})", sanitized, flags=re.A): + for i in mention_regex2.finditer(sanitized): u = get_user(i.group(1), graceful=True) if u: sanitized = sanitized.replace(i.group(0), f'''

@{u.username}''', 1) else: - sanitized = re.sub('(^|\s|\n|

)\/?((r|u)\/(\w|-){3,25})', r'\1/\2', sanitized, flags=re.A) + sanitized = reddit_regex.sub(r'\1/\2', sanitized) - sanitized = re.sub('(^|\s|\n|

)\/?(s\/(\w|-){3,25})', r'\1/\2', sanitized, flags=re.A) + sanitized = sub_regex.sub(r'\1/\2', sanitized) for i in mention_regex.finditer(sanitized): u = get_user(i.group(2), graceful=True) @@ -135,8 +135,7 @@ def sanitize(sanitized, noimages=False, alert=False, comment=False, edit=False): sanitized = sanitized.replace(i.group(0), f'''{i.group(1)}@{u.username}''', 1) - for i in re.finditer('https://i\.imgur\.com/(([^_]*?)\.(jpg|png|jpeg))(?!)', sanitized, flags=re.A): - sanitized = sanitized.replace(i.group(1), i.group(2) + "_d.webp?maxwidth=9999&fidelity=high") + sanitized = imgur_regex.sub(r'\1_d.webp?maxwidth=9999&fidelity=high', sanitized) if noimages: sanitized = bleach.Cleaner(tags=no_images, @@ -181,14 +180,9 @@ def sanitize(sanitized, noimages=False, alert=False, comment=False, edit=False): tag["target"] = "_blank" tag["rel"] = "nofollow noopener noreferrer" - if re.fullmatch("https?://\S+", str(tag.string), flags=re.A): - try: tag.string = tag["href"] - except: tag.string = "" - - sanitized = str(soup) - sanitized = re.sub('\|\|(.*?)\|\|', r'\1', sanitized) + sanitized = spoiler_regex.sub(r'\1', sanitized) if comment: marseys_used = set() @@ -265,7 +259,7 @@ def sanitize(sanitized, noimages=False, alert=False, comment=False, edit=False): if "https://youtube.com/watch?v=" in sanitized: sanitized = sanitized.replace("?t=", "&t=") - for i in re.finditer('" target="_blank">(https://youtube\.com/watch\?v\=(.*?))(?!)', sanitized, flags=re.A): + for i in youtube_regex.finditer(sanitized): url = i.group(1) yt_id = i.group(2).split('&')[0].split('%')[0] replacing = f'{url}' @@ -280,18 +274,16 @@ def sanitize(sanitized, noimages=False, alert=False, comment=False, edit=False): sanitized = sanitized.replace(replacing, htmlsource) - if not noimages: - for i in re.finditer('>(https://.*?\.(mp4|webm|mov|MP4|WEBM|MOV))

', sanitized, flags=re.A): - sanitized = sanitized.replace(f'

{i.group(1)}

', f'

') - for i in re.finditer('

(https:.*?\.(mp4|webm|mov|MP4|WEBM|MOV))

', sanitized, flags=re.A): - sanitized = sanitized.replace(i.group(0), f'

') - for rd in ["://reddit.com", "://new.reddit.com", "://www.reddit.com", "://redd.it", "://libredd.it"]: sanitized = sanitized.replace(rd, "://old.reddit.com") sanitized = sanitized.replace("old.reddit.com/gallery", "reddit.com/gallery") - sanitized = re.sub(' (https:\/\/[^ <>]*)', r' \1', sanitized, flags=re.A) - sanitized = re.sub('

(https:\/\/[^ <>]*)', r'

\1

', sanitized, flags=re.A) + + + sanitized = unlinked_regex.sub(r'\1\2', sanitized) + + if not noimages: + sanitized = video_regex.sub(r'

', sanitized) if comment: for marsey in g.db.query(Marsey).filter(Marsey.name.in_(marseys_used)).all(): @@ -348,7 +340,7 @@ def filter_emojis_only(title, edit=False, graceful=False): if path.isfile(f'files/assets/images/emojis/{emoji}.webp'): title = re.sub(f'(?', title, flags=re.I|re.A) - title = re.sub('~~(.*?)~~', r'\1', title, flags=re.A) + title = strikethrough_regex.sub(r'\1', title) signal.alarm(0) diff --git a/files/routes/admin.py b/files/routes/admin.py index 656666ec7c..e6709f936f 100644 --- a/files/routes/admin.py +++ b/files/routes/admin.py @@ -408,7 +408,7 @@ def monthly(v): emails = [x['email'] for x in requests.get(f'https://api.gumroad.com/v2/products/{GUMROAD_ID}/subscribers', data=data, timeout=5).json()["subscribers"]] for u in g.db.query(User).filter(User.patron > 0, User.patron_utc == 0).all(): - if u.email and u.email.lower() in emails: + if u.patron > 4 or u.email and u.email.lower() in emails: if u.patron == 1: procoins = 2500 elif u.patron == 2: procoins = 5000 elif u.patron == 3: procoins = 10000 @@ -419,11 +419,6 @@ def monthly(v): u.procoins += procoins g.db.add(u) send_repeatable_notification(u.id, f"@{v.username} has given you {procoins} Marseybux for the month of {month}! You can use them to buy awards in the [shop](/shop).") - elif u.patron == 5: - procoins = 50000 - u.procoins += procoins - g.db.add(u) - send_repeatable_notification(u.id, f"@{v.username} has given you {procoins} Marseybux for the month of {month}! You can use them to buy awards in the [shop](/shop).") elif u.patron == 1 and u.admin_level > 0: procoins = 2500 u.procoins += procoins @@ -436,6 +431,11 @@ def monthly(v): u.procoins += 50000 g.db.add(u) + if request.host == 'rdrama.net': + u = g.db.query(User).filter_by(id=A_ID).one() + u.procoins += 25000 + g.db.add(u) + ma = ModAction( kind="monthly", user_id=v.id diff --git a/files/routes/front.py b/files/routes/front.py index 9091d2328e..41c20eac75 100644 --- a/files/routes/front.py +++ b/files/routes/front.py @@ -164,7 +164,6 @@ def front_all(v, sub=None): ccmode=request.values.get('ccmode', "false").lower() defaultsubs = 'Include subs' - if v: subs=session.get('sub_toggle', defaultsubs) else: subs=defaultsubs @@ -270,7 +269,7 @@ def front_all(v, sub=None): @cache.memoize(timeout=86400) -def frontlist(v=None, sort="hot", page=1, t="all", ids_only=True, ccmode="false", subs='Exclude subs', filter_words='', gt=0, lt=0, sub=None, site=None): +def frontlist(v=None, sort="hot", page=1, t="all", ids_only=True, ccmode="false", subs='Include subs', filter_words='', gt=0, lt=0, sub=None, site=None): posts = g.db.query(Submission)