Improve support of email importer if headers are missing

Fix #88
pull/86/merge
Raphaël Vinot 2017-01-07 10:25:38 -05:00
parent 3b56abd70e
commit b51806ac9f
1 changed files with 37 additions and 34 deletions

View File

@ -48,31 +48,38 @@ def handler(q=False):
"types": ['email-header']}) "types": ['email-header']})
# E-Mail MIME Boundry # E-Mail MIME Boundry
if message.get_boundary():
results.append({"values": message.get_boundary(), results.append({"values": message.get_boundary(),
"types": ['email-mime-boundary']}) "types": ['email-mime-boundary']})
# E-Mail Reply To # E-Mail Reply To
if message.get('In-Reply-To'):
results.append({"values": message.get('In-Reply-To').strip(), results.append({"values": message.get('In-Reply-To').strip(),
"types": ['email-reply-to']}) "types": ['email-reply-to']})
# X-Mailer # X-Mailer
if message.get('X-Mailer'):
results.append({"values": message.get('X-Mailer'), results.append({"values": message.get('X-Mailer'),
"types": ['email-x-mailer']}) "types": ['email-x-mailer']})
# Thread Index # Thread Index
if message.get('Thread-Index'):
results.append({"values": message.get('Thread-Index'), results.append({"values": message.get('Thread-Index'),
"types": ['email-thread-index']}) "types": ['email-thread-index']})
# Email Message ID # Email Message ID
if message.get('Message-ID'):
results.append({"values": message.get('Message-ID'), results.append({"values": message.get('Message-ID'),
"types": ['email-message-id']}) "types": ['email-message-id']})
# Subject # Subject
if message.get('Subject'):
results.append({"values": message.get('Subject'), results.append({"values": message.get('Subject'),
"types": ['email-subject']}) "types": ['email-subject']})
# Source # Source
from_addr = message.get('From') from_addr = message.get('From')
if from_addr:
results.append({"values": parseaddr(from_addr)[1], results.append({"values": parseaddr(from_addr)[1],
"types": ['email-src'], "types": ['email-src'],
"comment": "From: {0}".format(from_addr)}) "comment": "From: {0}".format(from_addr)})
@ -82,6 +89,7 @@ def handler(q=False):
# Return Path # Return Path
return_path = message.get('Return-Path') return_path = message.get('Return-Path')
if return_path:
# E-Mail Source # E-Mail Source
results.append({"values": parseaddr(return_path)[1], results.append({"values": parseaddr(return_path)[1],
"types": ['email-src'], "types": ['email-src'],
@ -96,7 +104,7 @@ def handler(q=False):
recipient_headers = ['To', 'Cc', 'Bcc'] recipient_headers = ['To', 'Cc', 'Bcc']
for hdr_val in recipient_headers: for hdr_val in recipient_headers:
try: if message.get(hdr_val):
addrs = message.get(hdr_val).split(',') addrs = message.get(hdr_val).split(',')
for addr in addrs: for addr in addrs:
# Parse and add destination header values # Parse and add destination header values
@ -110,15 +118,12 @@ def handler(q=False):
"comment": "{0}: {1}".format(hdr_val, "comment": "{0}: {1}".format(hdr_val,
addr)}) addr)})
except AttributeError:
continue
# Get E-Mail Targets # Get E-Mail Targets
# Get the addresses that received the email. # Get the addresses that received the email.
# As pulled from the Received header # As pulled from the Received header
received = message.get_all('Received') received = message.get_all('Received')
if received:
email_targets = set() email_targets = set()
try:
for rec in received: for rec in received:
try: try:
email_check = re.search("for\s(.*@.*);", rec).group(1) email_check = re.search("for\s(.*@.*);", rec).group(1)
@ -130,8 +135,6 @@ def handler(q=False):
results.append({"values": tar, results.append({"values": tar,
"types": ["target-email"], "types": ["target-email"],
"comment": "Extracted from email 'Received' header"}) "comment": "Extracted from email 'Received' header"})
except TypeError:
pass # If received header is missing we can't iterate over NoneType
# Check if we were given a configuration # Check if we were given a configuration
config = request.get("config", {}) config = request.get("config", {})