# Path: csirtg_mail/constants.py
# PYVERSION = 2
# 
# Path: csirtg_mail/utils.py
# def parse_headers(message):
#     keys = message.keys()
#     values = message.values()
#     headers = {}
# 
#     for k, v in zip(keys, values):
#         headers.setdefault(k.lower(), []).append(v)
# 
#     return headers
# 
# Path: csirtg_mail/urls.py
# def extract_urls(content, html=False, defanged_urls=False, sanitize_urls=False):
#     urls = set()
# 
#     from .constants import PYVERSION
# 
#     if PYVERSION == 3:
#         content = str(content)
# 
#     if content:
#         if html:
#             urls = _extract_urls_html(content, defanged_urls=defanged_urls)
#         else:
#             urls = _extract_urls_text(content, defanged_urls=defanged_urls)
# 
#         # check do we want to strip parameters from URLs, i.e. remove '?foo=bar'
#         if sanitize_urls:
#             sanitized_urls = set()
#             for url in urls:
#                 if '?' in url: # if this URL has parameters
#                     param_start = url.index('?')
#                     s_url = url[:param_start]
#                     sanitized_urls.add(s_url) # Add the sanitized URL to the sanitized set
#             urls = sanitized_urls # replace the set we are returning with the sanitized set
#     else:
#         return urls
# 
#     return urls
# 
# Path: csirtg_mail/urls.py
# def extract_email_addresses(content, html=False):
#     email_addresses = set()
# 
#     if content:
#         if html:
#             email_addresses = _extract_email_addresses_html(content)
#         else:
#             email_addresses = _extract_email_addresses_text(content)
#     else:
#         return email_addresses
# 
#     return email_addresses
# 
# Path: csirtg_mail/btc.py
# def extract_btcs(content, html=False):
#     btcs = set()
# 
#     from .constants import PYVERSION
# 
#     if PYVERSION == 3:
#         content = str(content)
# 
#     if content:
#         btcs = _extract_btcs_text(content)
#     else:
#         return btcs
# 
#     return btcs


from .constants import PYVERSION
from .utils import parse_headers
from pyzmail.parse import message_from_string as pyzmail_message_from_string
from pyzmail.parse import get_mail_parts as pyzmail_get_mail_parts
from pyzmail.parse import decode_mail_header as pyzmail_decode_mail_header
from pyzmail.parse import decode_text as pyzmail_decode_text
from csirtg_mail.urls import extract_urls as _extract_urls
from csirtg_mail.urls import extract_email_addresses as _extract_email_addresses
from csirtg_mail.btc import extract_btcs as _extract_btcs
import pyzmail
import base64
import sys
import chardet
    #         mailpart.type, \
    #         mailpart.charset, \
    #         mailpart.part.get('Content-Description'), \
    #         len(mailpart.get_payload()))
    #
    #     if mailpart.type.startswith('text/'):
    #         # display first line of the text
    #         payload, used_charset = pyzmail.decode_text(mailpart.get_payload(), mailpart.charset, None)
    #         print '        >', payload.split('\\n')[0]

    # is fwd

    # inline
    # attachment is .eml
    # is html
    # is txt
    # attachment is [.docx?|.zip|.html?|.xlsx?]
    # multiple .eml attachments
    # is html
    # is txt

    urls = set()
    email_addresses = set()
    attachments = []
    if message.html_part:
        body = message.html_part.get_payload()
        try:
            body = body.decode('utf-8')
        except Exception:
            body = body.decode('latin-1')

# Path: csirtg_mail/constants.py
# PYVERSION = 2
# 
# Path: csirtg_mail/utils.py
# def parse_headers(message):
#     keys = message.keys()
#     values = message.values()
#     headers = {}
# 
#     for k, v in zip(keys, values):
#         headers.setdefault(k.lower(), []).append(v)
# 
#     return headers
# 
# Path: csirtg_mail/urls.py
# def extract_urls(content, html=False, defanged_urls=False, sanitize_urls=False):
#     urls = set()
# 
#     from .constants import PYVERSION
# 
#     if PYVERSION == 3:
#         content = str(content)
# 
#     if content:
#         if html:
#             urls = _extract_urls_html(content, defanged_urls=defanged_urls)
#         else:
#             urls = _extract_urls_text(content, defanged_urls=defanged_urls)
# 
#         # check do we want to strip parameters from URLs, i.e. remove '?foo=bar'
#         if sanitize_urls:
#             sanitized_urls = set()
#             for url in urls:
#                 if '?' in url: # if this URL has parameters
#                     param_start = url.index('?')
#                     s_url = url[:param_start]
#                     sanitized_urls.add(s_url) # Add the sanitized URL to the sanitized set
#             urls = sanitized_urls # replace the set we are returning with the sanitized set
#     else:
#         return urls
# 
#     return urls
# 
# Path: csirtg_mail/urls.py
# def extract_email_addresses(content, html=False):
#     email_addresses = set()
# 
#     if content:
#         if html:
#             email_addresses = _extract_email_addresses_html(content)
#         else:
#             email_addresses = _extract_email_addresses_text(content)
#     else:
#         return email_addresses
# 
#     return email_addresses
# 
# Path: csirtg_mail/btc.py
# def extract_btcs(content, html=False):
#     btcs = set()
# 
#     from .constants import PYVERSION
# 
#     if PYVERSION == 3:
#         content = str(content)
# 
#     if content:
#         btcs = _extract_btcs_text(content)
#     else:
#         return btcs
# 
#     return btcs


# Path: csirtg_mail/parse.py
from .constants import PYVERSION
from .utils import parse_headers
from pyzmail.parse import message_from_string as pyzmail_message_from_string
from pyzmail.parse import get_mail_parts as pyzmail_get_mail_parts
from pyzmail.parse import decode_mail_header as pyzmail_decode_mail_header
from pyzmail.parse import decode_text as pyzmail_decode_text
from csirtg_mail.urls import extract_urls as _extract_urls
from csirtg_mail.urls import extract_email_addresses as _extract_email_addresses
from csirtg_mail.btc import extract_btcs as _extract_btcs
import pyzmail
import base64
import sys
import chardet

    #         mailpart.type, \
    #         mailpart.charset, \
    #         mailpart.part.get('Content-Description'), \
    #         len(mailpart.get_payload()))
    #
    #     if mailpart.type.startswith('text/'):
    #         # display first line of the text
    #         payload, used_charset = pyzmail.decode_text(mailpart.get_payload(), mailpart.charset, None)
    #         print '        >', payload.split('\\n')[0]

    # is fwd

    # inline
    # attachment is .eml
    # is html
    # is txt
    # attachment is [.docx?|.zip|.html?|.xlsx?]
    # multiple .eml attachments
    # is html
    # is txt

    urls = set()
    email_addresses = set()
    attachments = []
    if message.html_part:
        body = message.html_part.get_payload()
        try:
            body = body.decode('utf-8')
        except Exception:
            body = body.decode('latin-1')

        urls = _extract_urls(body, html=True)