def check_existing_rule_matches(link, thread_queue): """ Checks Beaker to see if there are any Rules which are already known to match this Link. :param link: A dictionary describing the Link object. :param thread_queue: The Queue object to use for returning the result cross-thread. """ # If the Link was not known in the DB, just redirect to the # URL as specified in the JWT. if not link.get('redirect_url'): return thread_queue.put(constants.ACTION_DEFAULT) rules = link.get('rules') final_action = constants.ACTION_DEFAULT for rule_id in rules: try: rule = beaker_client.get_rule(rule_id) except Exception: logging.exception("Rule could not be retrieved.") continue action = rule.get('action', constants.ACTION_DEFAULT) if action > final_action: final_action = action thread_queue.put(final_action)
def check_current_rule_matches(link, message, thread_queue): """ Checks Beaker to see if there are any Rules which match this Message. :param link: A dictionary describing the Link object. :param message: A dictionary describing the Message object. :param thread_queue: The Queue object to use for returning the result cross-thread. """ message_from_address = message.get('message_from_address') envelope_from_address = message.get('envelope_from_address') rcpt_to_address = message.get('rcpt_to_address') domain_id = message.get('domain') domain_name = rcpt_to_address.split("@")[1] link_id = link.get('id') link_url = link.get('redirect_url') # Check the current Rules for the domain and ensure the URL # hasn't been listed in a Rule since it was first seen. link_rules_dict = beaker_client.check_rules( message_from_address=message_from_address, domain_id=domain_id, domain_name=domain_name, link_ids=[link_id], senders=[message_from_address, envelope_from_address], receivers=[rcpt_to_address], # TODO Probably need to have all the 'To's and 'Cc's in Beaker on the MSG object subject=None, body=None, urls=[link_url] ) final_action = constants.ACTION_DEFAULT # If we have matches, we need to notify Beaker and get the associated action to perform. for link_id, rule_ids in link_rules_dict.items(): rule_ids = rule_ids["rule_ids"] for rule_id in rule_ids: try: rule = beaker_client.get_rule(rule_id) except Exception: logging.exception("Rule could not be retrieved.") continue action = rule.get('action', constants.ACTION_DEFAULT) if action > final_action: final_action = action beaker_client.mark_link_with_rule(rule_id, link_id) thread_queue.put(final_action)