# By: Riasat Ullah
# This file handles functions that check if an incoming source is banned or not.

from dbqueries import db_banned_sources
from utils import errors, logging, times


def is_source_malicious(conn, ban_checks):
    '''
    Checks if an incoming source is malicious or not.
    :param conn: db connection
    :param ban_checks: (list of tuples) of ban types and values -> [('subdomain', 'gmail.com'), ...]
    :return:
    '''
    banned_sources = db_banned_sources.get_all_banned_sources(conn, times.get_current_timestamp())
    for ban_type, check_val in ban_checks:
        if ban_type in banned_sources and check_val in banned_sources[ban_type]:
            logging.error(errors.err_internal_malicious_source_detected + ' - ' + ban_type + ' | ' + check_val)
            return True
    return False
