# Path: mobsf/StaticAnalyzer/views/common/shared_func.py
# def url_n_email_extract(dat, relative_path):
#     """Extract URLs and Emails from Source Code."""
#     urls = []
#     emails = []
#     urllist = []
#     url_n_file = []
#     email_n_file = []
#     # URLs Extraction My Custom regex
#     pattern = re.compile(
#         (
#             r'((?:https?://|s?ftps?://|'
#             r'file://|javascript:|data:|www\d{0,3}[.])'
#             r'[\w().=/;,#:@?&~*+!$%\'{}-]+)'
#         ),
#         re.UNICODE)
#     urllist = re.findall(pattern, dat)
#     uflag = 0
#     for url in urllist:
#         if url not in urls:
#             urls.append(url)
#             uflag = 1
#     if uflag == 1:
#         url_n_file.append(
#             {'urls': urls, 'path': escape(relative_path)})
# 
#     # Email Extraction Regex
#     regex = re.compile(r'[\w.-]{1,20}@[\w-]{1,20}\.[\w]{2,10}')
#     eflag = 0
#     for email in regex.findall(dat.lower()):
#         if (email not in emails) and (not email.startswith('//')):
#             emails.append(email)
#             eflag = 1
#     if eflag == 1:
#         email_n_file.append(
#             {'emails': emails, 'path': escape(relative_path)})
#     return urllist, url_n_file, email_n_file


import io
import logging
from mobsf.MalwareAnalyzer.views.MalwareDomainCheck import (
    MalwareDomainCheck,
)
from mobsf.StaticAnalyzer.views.common.shared_func import (
    url_n_email_extract,
)

def extract_urls_n_email(src, all_files, strings):
    """IPA URL and Email Extraction."""
    try:
        logger.info('Starting IPA URL and Email Extraction')
        email_n_file = []
        url_n_file = []
        url_list = []
        domains = {}
        all_files.append({'data': strings, 'name': 'IPA Strings Dump'})
        for file in all_files:
            if isinstance(file, dict):
                relative_src_path = file['name']
                dat = '\n'.join(file['data'])
            # Skip CodeResources and contents under Frameworks
            elif 'CodeResources' in file or '/Frameworks/' in file:
                continue
            elif file.endswith(('.nib', '.ttf', '.svg', '.woff2',
                                '.png', '.dylib', '.mobileprovision',
                                'Assets.car')):
                continue
            else:
                dat = ''
                relative_src_path = file.replace(src, '')
                with io.open(file,
                             mode='r',
                             encoding='utf8',
                             errors='ignore') as flip:
                    dat = flip.read()
            # Extract URLs and Emails from Plists

# Path: mobsf/StaticAnalyzer/views/common/shared_func.py
# def url_n_email_extract(dat, relative_path):
#     """Extract URLs and Emails from Source Code."""
#     urls = []
#     emails = []
#     urllist = []
#     url_n_file = []
#     email_n_file = []
#     # URLs Extraction My Custom regex
#     pattern = re.compile(
#         (
#             r'((?:https?://|s?ftps?://|'
#             r'file://|javascript:|data:|www\d{0,3}[.])'
#             r'[\w().=/;,#:@?&~*+!$%\'{}-]+)'
#         ),
#         re.UNICODE)
#     urllist = re.findall(pattern, dat)
#     uflag = 0
#     for url in urllist:
#         if url not in urls:
#             urls.append(url)
#             uflag = 1
#     if uflag == 1:
#         url_n_file.append(
#             {'urls': urls, 'path': escape(relative_path)})
# 
#     # Email Extraction Regex
#     regex = re.compile(r'[\w.-]{1,20}@[\w-]{1,20}\.[\w]{2,10}')
#     eflag = 0
#     for email in regex.findall(dat.lower()):
#         if (email not in emails) and (not email.startswith('//')):
#             emails.append(email)
#             eflag = 1
#     if eflag == 1:
#         email_n_file.append(
#             {'emails': emails, 'path': escape(relative_path)})
#     return urllist, url_n_file, email_n_file


# Path: mobsf/StaticAnalyzer/views/ios/file_recon.py
import io
import logging
from mobsf.MalwareAnalyzer.views.MalwareDomainCheck import (
    MalwareDomainCheck,
)
from mobsf.StaticAnalyzer.views.common.shared_func import (
    url_n_email_extract,
)


def extract_urls_n_email(src, all_files, strings):
    """IPA URL and Email Extraction."""
    try:
        logger.info('Starting IPA URL and Email Extraction')
        email_n_file = []
        url_n_file = []
        url_list = []
        domains = {}
        all_files.append({'data': strings, 'name': 'IPA Strings Dump'})
        for file in all_files:
            if isinstance(file, dict):
                relative_src_path = file['name']
                dat = '\n'.join(file['data'])
            # Skip CodeResources and contents under Frameworks
            elif 'CodeResources' in file or '/Frameworks/' in file:
                continue
            elif file.endswith(('.nib', '.ttf', '.svg', '.woff2',
                                '.png', '.dylib', '.mobileprovision',
                                'Assets.car')):
                continue
            else:
                dat = ''
                relative_src_path = file.replace(src, '')
                with io.open(file,
                             mode='r',
                             encoding='utf8',
                             errors='ignore') as flip:
                    dat = flip.read()
            # Extract URLs and Emails from Plists

            urls, urls_nf, emails_nf = url_n_email_extract(