import random import re from random import choice, choices from .config.const import * NOT_IN_CODE_OR_LINKS = '(?!([^<]*<\/(code|pre|a)>|[^`\n]*`|(.|\n)*```))' valid_username_regex = re.compile("^[\w-]{3,25}$", flags=re.A) valid_username_patron_regex = re.compile("^[\w-]{1,25}$", flags=re.A) mention_regex = re.compile('(?)!(everyone)' + NOT_IN_CODE_OR_LINKS, flags=re.A) valid_password_regex = re.compile("^.{8,100}$", flags=re.A) marseyaward_body_regex = re.compile(">[^<\s+]|[^>\s+]<", flags=re.A) marseyaward_title_regex = re.compile("( *]+>)+", flags=re.A) emoji_name_regex = re.compile("[a-z0-9]{1,30}", flags=re.A) tags_regex = re.compile("[a-z0-9: ]{1,200}", flags=re.A) hat_regex = re.compile("[\w\-() ,]{1,50}", flags=re.A) description_regex = re.compile("[^<>&\n\t]{1,300}", flags=re.A) badge_name_regex = re.compile(r"[^\/.]+", flags=re.A) hole_group_name_regex = re.compile("^[\w-]{3,25}$", flags=re.A) query_regex = re.compile("(\w+):(\S+)", flags=re.A) poll_regex = re.compile("(^|\n|>)\$\$([^\$\n]+)\$\$\s*?" + NOT_IN_CODE_OR_LINKS, flags=re.A) bet_regex = re.compile("(^|\n|>)##([^#\n]+)##\s*?" + NOT_IN_CODE_OR_LINKS, flags=re.A) choice_regex = re.compile("(^|\n|>)&&([^&\n]+)&&\s*?" + NOT_IN_CODE_OR_LINKS, flags=re.A) html_comment_regex = re.compile("", flags=re.A) title_regex = re.compile("[^\w ]", flags=re.A) controversial_regex = re.compile('https:\/\/old\.reddit\.com/r/\w{2,20}\/comments\/[\w\-.#&/=\?@%+]{5,250}', flags=re.A) spoiler_regex = re.compile('\|\|(.+?)\|\|' + NOT_IN_CODE_OR_LINKS, flags=re.A) sub_regex = re.compile('(?|")~{1,2}([^~]+)~{1,2}' + NOT_IN_CODE_OR_LINKS, flags=re.A) mute_regex = re.compile("\/mute @?([\w-]{1,30}) ([0-9]+)", flags=re.A|re.I) emoji_regex = re.compile(f"

\s*(:[!#@\w\-]{{1,72}}:\s*)+<\/p>", flags=re.A) emoji_regex2 = re.compile(f'(?|[^`]*`))', flags=re.A) snappy_url_regex = re.compile('(.+?)<\/a>', flags=re.A) email_regex = re.compile('[A-Za-z0-9._%+-]{1,64}@[A-Za-z0-9.-]{2,63}\.[A-Za-z]{2,63}', flags=re.A) slur_single_words = "|".join([slur.lower() for slur in SLURS.keys()]) slur_single_words_title = slur_single_words.title().replace('!\W','!\w') slur_single_words_upper = slur_single_words.upper().replace('!\W','!\w') profanity_single_words = "|".join([profanity.lower() for profanity in PROFANITIES.keys()]) profanity_single_words_title = profanity_single_words.title().replace('!\W','!\w') profanity_single_words_upper = profanity_single_words.upper().replace('!\W','!\w') slur_regex = re.compile(f"<[^>]*>|{slur_single_words}", flags=re.I|re.A) slur_regex_title = re.compile(f"<[^>]*>|{slur_single_words_title}", flags=re.A) slur_regex_upper = re.compile(f"<[^>]*>|{slur_single_words_upper}", flags=re.A) profanity_regex = re.compile(f"<[^>]*>|{profanity_single_words}", flags=re.I|re.A) profanity_regex_title = re.compile(f"<[^>]*>|{profanity_single_words_title}", flags=re.A) profanity_regex_upper = re.compile(f"<[^>]*>|{profanity_single_words_upper}", flags=re.A) torture_regex = re.compile('(^|\s)(i|me)($|\s)', flags=re.I|re.A) torture_regex2 = re.compile("(^|\s)(i'm)($|\s)", flags=re.I|re.A) torture_regex3 = re.compile("(^|\s)(my|mine)($|\s)", flags=re.I|re.A) #matches ". ", does not match "..." or a.b sentence_ending_regex = re.compile('(?+)', flags=re.I|re.A) #matches "the" or is, but only if it is not followed by "fucking". https://regex101.com/r/yxuYsQ/2 the_fucking_regex = re.compile('(?<=^|(?<=\s))((?:the|a)( (?:only))?|((that )?(?:is|are|was|were|will be|would be)( (?:your|her|his|their|no|a|not|to|too|so|this|the|our|what))?( (a|the))?)|is)(?=\s)(?! fucking)' + NOT_IN_CODE_OR_LINKS, flags=re.I|re.A) #matches a single question mark but only if it isn't preceded by ", bitch" bitch_question_mark_regex = re.compile('(?([^ >][^\n]*)", flags=re.A) allowed_domain_regex = re.compile("[a-z0-9\-.]+", flags=re.I|re.A) twitter_to_nitter_regex = re.compile('(^|>|")https:\/\/twitter.com\/(?!i\/)', flags=re.A) color_regex = re.compile("[a-f0-9]{6}", flags=re.A) # lazy match on the .*?, only match if there is trailing stuff # Specifically match Snappy's way of formatting, this might break some losers' comments. showmore_regex = re.compile(r"^(.*?

(?:)?)(\s*

.*)", flags=re.A|re.DOTALL) search_token_regex = re.compile('"([^"]*)"|(\S+)', flags=re.A) git_regex = re.compile("ref: (refs/.+)", flags=re.A) pronouns_regex = re.compile("([a-z]{1,7})\/[a-z]{1,7}(\/[a-z]{1,7})?", flags=re.A|re.I) html_title_regex = re.compile("(.{1,200})", flags=re.I) SLURS_FOR_REPLACING = {} for k, val in SLURS.items(): newkey = k.split('(?!')[0] if ')' in newkey: newkey = newkey.split(')')[1] SLURS_FOR_REPLACING[newkey] = val PROFANITIES_FOR_REPLACING = {} for k, val in PROFANITIES.items(): newkey = k.split('(?!')[0] if ')' in newkey: newkey = newkey.split(')')[1] PROFANITIES_FOR_REPLACING[newkey] = val def sub_matcher(match, upper=False, title=False, replace_with=SLURS_FOR_REPLACING): group_num = 0 match_str = match.group(group_num) if match_str.startswith('<'): return match_str else: repl = replace_with[match_str.lower()] if (not upper and not title) or "' in body or '' in body: return body if not logged_user or logged_user == 'chat' or logged_user.slurreplacer: body = replace_re(body, slur_regex, slur_regex_title, slur_regex_upper, sub_matcher_slurs, sub_matcher_slurs_title, sub_matcher_slurs_upper) if SITE_NAME == 'rDrama': if not logged_user or logged_user == 'chat' or logged_user.profanityreplacer: body = replace_re(body, profanity_regex, profanity_regex_title, profanity_regex_upper, sub_matcher_profanities, sub_matcher_profanities_title, sub_matcher_profanities_upper) return body commands = { "fortune": FORTUNE_REPLIES, "factcheck": FACTCHECK_REPLIES, "8ball": EIGHTBALL_REPLIES, "roll": range(1, 10000), "coinflip": COINFLIP_HEADS_OR_TAILS, } command_regex = re.compile("(\s|^)#(fortune|factcheck|8ball|roll|coinflip)", flags=re.A|re.I) def command_regex_matcher(match, upper=False): if match.group(2) == 'coinflip' and random.random() < 0.01: result = COINFLIP_EDGE else: result = str(choice(commands[match.group(2).lower()])) if match.group(2) == 'roll': color = tuple(choices(range(256), k=3)) result = f'Your roll: {result}' return match.group(1) + result reason_regex_post = re.compile('(/post/[0-9]+)', flags=re.A) reason_regex_comment = re.compile('(/comment/[0-9]+)', flags=re.A) numbered_list_regex = re.compile('((\s|^)[0-9]+)\. ', flags=re.A) image_link_regex = re.compile(f"https:\/\/(i\.)?{SITE}\/(chat_)?images\/[0-9]{{11,17}}r?\.webp", flags=re.A) video_link_regex = re.compile(f"https://(videos\.)?{SITE}\/(videos\/)?[0-9a-zA-Z._-]{{4,66}}\.({video_regex_extensions})", flags=re.A) asset_image_link_regex = re.compile(f"https:\/\/(i\.)?{SITE}\/assets\/images\/[\w\/]+.webp(\?x=\d+)?", flags=re.A) ###REDDIT #sanitizing reddit_mention_regex = re.compile('(?|")https:\/\/old.reddit.com\/(r|user)\/', flags=re.A) ###YOUTUBE #sanitize youtube_regex = re.compile('https:\/\/youtube\.com\/watch\?v=[\w-]{11}[\w&;=]*<\/a>' + NOT_IN_CODE_OR_LINKS, flags=re.I|re.A) #sanitize and song yt_id_regex = re.compile('[\w-]{11}', flags=re.A) #orgy bare_youtube_regex = re.compile('https:\/\/youtube\.com\/watch\?v=[\w-]{11}[\w&;=]*', flags=re.I|re.A)