# Path: cardinal/decorators.py
# def regex(expression):
#     if (not isinstance(expression, str) and
#             not isinstance(expression, _RETYPE)):
#         raise TypeError("Regular expression must be a string or regex type")
# 
#     def wrap(f):
#         f.regex = expression
#         return f
# 
#     return wrap


import re
import html
import logging
import unicodedata
from datetime import datetime
from urllib import request
from twisted.internet import defer
from twisted.internet.threads import deferToThread
from cardinal.decorators import regex
# Some notes about this regex - it will attempt to capture URLs prefixed by a
# space, a control character (e.g. for formatting), or the beginning of the
# string.
URL_REGEX = re.compile(r"(?:^|\s|[\x00-\x1f\x7f-\x9f])((?:https?://)?(?:[a-z0-9.\-]+[.][a-z]{2,4}/?)(?:[^\s()<>]*|\((?:[^\s()<>]+|(?:\([^\s()<>]+\)))*\))+(?:\((?:[^\s()<>]+|(?:\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:\'\".,<>?]))",  # noqa: E501
                       flags=re.IGNORECASE | re.DOTALL)
TITLE_REGEX = re.compile(r'<title(\s+.*?)?>(.*?)</title>',
                         flags=re.IGNORECASE | re.DOTALL)


def get_urls(message):
    urls = re.findall(URL_REGEX, message)
    # strip any control characters that remain on the right side of the string
    # we don't need to worry about the left side, since the regex won't capture
    # any strings that don't begin "http"
    for i in range(len(urls)):
        url = urls[i]

        idx_r = len(url)
        for j in range(len(url)):
            if unicodedata.category(url[len(url) - 1 - j])[0] == "C":
                idx_r -= 1
            else:
                break

        urls[i] = url[0:idx_r]

    return urls


class URLsPlugin:

# Path: cardinal/decorators.py
# def regex(expression):
#     if (not isinstance(expression, str) and
#             not isinstance(expression, _RETYPE)):
#         raise TypeError("Regular expression must be a string or regex type")
# 
#     def wrap(f):
#         f.regex = expression
#         return f
# 
#     return wrap


# Path: plugins/urls/plugin.py
import re
import html
import logging
import unicodedata
from datetime import datetime
from urllib import request
from twisted.internet import defer
from twisted.internet.threads import deferToThread
from cardinal.decorators import regex

# Some notes about this regex - it will attempt to capture URLs prefixed by a
# space, a control character (e.g. for formatting), or the beginning of the
# string.
URL_REGEX = re.compile(r"(?:^|\s|[\x00-\x1f\x7f-\x9f])((?:https?://)?(?:[a-z0-9.\-]+[.][a-z]{2,4}/?)(?:[^\s()<>]*|\((?:[^\s()<>]+|(?:\([^\s()<>]+\)))*\))+(?:\((?:[^\s()<>]+|(?:\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:\'\".,<>?]))",  # noqa: E501
                       flags=re.IGNORECASE | re.DOTALL)
TITLE_REGEX = re.compile(r'<title(\s+.*?)?>(.*?)</title>',
                         flags=re.IGNORECASE | re.DOTALL)


def get_urls(message):
    urls = re.findall(URL_REGEX, message)
    # strip any control characters that remain on the right side of the string
    # we don't need to worry about the left side, since the regex won't capture
    # any strings that don't begin "http"
    for i in range(len(urls)):
        url = urls[i]

        idx_r = len(url)
        for j in range(len(url)):
            if unicodedata.category(url[len(url) - 1 - j])[0] == "C":
                idx_r -= 1
            else:
                break

        urls[i] = url[0:idx_r]

    return urls


class URLsPlugin:

    TIMEOUT = 5