from bs4 import BeautifulSoup
import bleach
import re


def neteja_nou(html):
    html = sanitize(html)
    html = treure_signatura(html)
    html = treure_pgp(html)
    html = treure_imatges_trencades(html)
    html = compacta_br(html)
    html = treure_body(html)
    return html


def neteja_reply(html):
    html = sanitize(html)
    html = treure_reply(html)
    html = treure_signatura(html)
    html = treure_pgp(html)
    html = treure_imatges_trencades(html)
    html = compacta_br(html)
    html = treure_body(html)
    return html


def assegura_contingut(funcio_neteja):
    def funcio_assegura_contingut(html):
        html_net = funcio_neteja(html)
        soup = BeautifulSoup(html_net, "html.parser")
        # Truquillo pythonic per treure espais al text
        text_sense_espais = "".join(soup.text.strip())
        if len(text_sense_espais) > 0:
            return html_net
        else:
            return html

    return funcio_assegura_contingut


@assegura_contingut
def sanitize(html):
    # Mails del tipus <mail@fib.upc.edu> no son tags!
    html = re.sub(r"<(a-zA-Z0-9_\.+-]+@a-zA-Z0-9_\.+-]+)>", r"[\1]", html)
    html = re.sub(r"&lt;([a-zA-Z0-9_\.+-]+@[a-zA-Z0-9_\.+-]+)&gt;", r"[\1]",
                  html)
    net = bleach.clean(
        html,
        tags=[
            'a', 'abbr', 'acronym', 'b', 'blockquote', 'code', 'em', 'i',
            'li', 'ol', 'strong', 'ul', 'pre', 'table', 'tr', 'td', 'th',
            'tbody', 'thead', 'tfoot', 'div', 'br', 'hr', 'img', 'p'
        ],
        attributes={
            '*': ['class'],
            'img': ['src'],
            'a': ['href'],
            'td': ['colspan', 'rowspan'],
            'blockquote': ['type']
        },
        strip=True
    )
    # Aixo es perque els BR siguin autocontinguts i no interfereixin a l'arbre
    net = re.sub('<br\s*/?>', '<br/>', net, flags=re.I)
    net = re.sub('</br\s*>', '', net, flags=re.I)
    return "<body>%s</body>" % net


@assegura_contingut
def compacta_br(html):
    html = re.sub('<br\s*/?>(?:\s*<br\s*/?>)+', '<br />', html, flags=re.I)
    return html


@assegura_contingut
def treure_body(html):
    html = re.sub('</?body\s*>', '', html, flags=re.I)
    return html


def treure_reply(html):
    html = treure_blockquote(html)
    html = treure_reply_text(html)
    return html


@assegura_contingut
def treure_blockquote(html):
    soup = BeautifulSoup(html, "html.parser")

    # Thunderbird, webmail
    tags = soup.select('body > blockquote[type=cite]')
    if len(tags) == 1:
        tags[0].decompose()

    # gmail
    tags = soup.select('body > div.gmail_extra')
    if len(tags) == 1:
        tags[0].decompose()

    # Client mail android
    tags = soup.select('body > div.quote')
    if len(tags) == 1:
        tags[0].decompose()

    # Aixo es perillos
    # tags = soup.select('div.moz-cite-prefix')
    # if len(tags) == 1:
    #     tags[0].decompose()

    return unicode(soup)


@assegura_contingut
def treure_reply_text(text):
    blocs = 0
    anterior = False
    linies = text.split("<br/>\n")
    sensequotes = []
    for l in linies:
        if l.startswith("&gt;"):
            dintre = True
            if anterior != dintre:
                blocs += 1

        else:
            dintre = False
            sensequotes.append(l)

        anterior = dintre

    if blocs == 1:
        return "<br/>\n".join(sensequotes)
    else:
        return text


def treure_signatura(html):
    html = treure_signatura_text(html)
    html = treure_signatura_html(html)
    return html


@assegura_contingut
def treure_signatura_text(text):
    blocs = 0
    signatura = False
    cos = []
    linies = text.split("<br\s*/?>\n")
    for l in linies:
        if re.match("^--\s*$", l):
            signatura = True
            blocs += 1

        if not signatura:
            cos.append(l)

    if blocs == 1:
        return "<br>\n".join(cos)
    else:
        return text


@assegura_contingut
def treure_signatura_html(html):
    soup = BeautifulSoup(html, "html.parser")
    tags = soup.select('.moz-signature')
    if len(tags) >= 1:
        tags[len(tags) - 1].decompose()

    return unicode(soup)


@assegura_contingut
def treure_imatges_trencades(html):
    soup = BeautifulSoup(html, "html.parser")
    tags = soup.select('img[src^="cid:"]')
    for tag in tags:
        tag.decompose()

    return unicode(soup)


@assegura_contingut
def treure_pgp(text):
    pgp = False
    cos = []
    linies = text.split("\n")
    for l in linies:
        if re.match("^-----BEGIN PGP PUBLIC KEY BLOCK-----$", l):
            pgp = True

        if not pgp:
            cos.append(l)

        if re.match("^-----END PGP PUBLIC KEY BLOCK-----$", l):
            pgp = False

    return "\n".join(cos)

@@ -2312,32 +2312,254 @@
   return html%0A%0A%0A
+def _es_prefix_reply_valid(tag):%0A    # Comprovem que no contingui mes dels 2 tags que posa per defecte%0A    # (un %3Ca%3E i un %3Cbr%3E) o voldr%C3%A0 dir que t%C3%A9 contingut que no hem d'esborrar%0A    return len(tag.find_all(True) %3C= 2)%0A%0A%0A
 @assegura_contin
@@ -3016,30 +3016,46 @@
   # 
-Aixo es perillos
+Prefix de signatura de Thunderbird
 %0A   
- #
  tag
@@ -3071,16 +3071,23 @@
 select('
+body %3E 
 div.moz-
@@ -3103,18 +3103,16 @@
 ix')%0A   
- #
  if len(
@@ -3121,23 +3121,57 @@
 gs) == 1
+ and _es_prefix_reply_valid(tags%5B0%5D)
 :%0A   
- #
      tag

Refactor de la neteja del prefix de quote