hopefully not breaking too much...

pull/4/head
Sascha Rommelfangen 2017-06-01 18:39:39 +02:00
parent 3de6a6a5aa
commit e1ade5b8d7
1 changed files with 39 additions and 30 deletions

View File

@ -54,37 +54,46 @@ debug = config.debug
stdin_used = False stdin_used = False
email_subject = config.email_subject_prefix email_subject = config.email_subject_prefix
try: #try:
if not sys.stdin.isatty(): #if not sys.stdin.isatty():
mailcontent = sys.stdin.buffer.read().decode("utf-8", "ignore") if len(sys.argv) == 1:
else: mailcontent = sys.stdin.buffer.read().decode("utf-8", "ignore")
mailcontent = sys.argv[1] else:
if len(sys.argv) >= 3: mailcontent = sys.argv[1]
mail_subject = sys.argv[2]
email_data = b''
syslog.syslog(mailcontent) syslog.syslog(mailcontent)
msg = email.message_from_string(mailcontent) if len(sys.argv) >= 3:
mail_subject = msg.get('Subject').encode("utf-8", "ignore") mail_subject = sys.argv[2]
for part in msg.walk(): email_data = b''
if part.get_content_charset() is None: msg = email.message_from_string(mailcontent)
# This could probably be detected if not mail_subject:
charset = 'utf-8' try:
else: mail_subject = msg.get('Subject').encode("utf-8", "ignore")
charset = part.get_content_charset() except:
if part.get_content_maintype() == 'multipart': pass
continue for part in msg.walk():
if part.get_content_maintype() == 'text': if part.get_content_charset() is None:
part.set_charset(charset) # This could probably be detected
email_data += part.get_payload(decode=True) charset = 'utf-8'
print("here") else:
email_subject += mail_subject charset = part.get_content_charset()
stdin_used = True if part.get_content_maintype() == 'multipart':
except Exception as e: continue
if debug: if part.get_content_maintype() == 'text':
syslog.syslog("FATAL ERROR: Not all required input received") part.set_charset(charset)
print(str(e)) try:
syslog.syslog(str(e)) syslog.syslog(str(part.get_payload(decode=True))
sys.exit(1) except Exception as e:
syslog.syslog(str(e))
email_data += part.get_payload(decode=True)
print("here")
email_subject += mail_subject
stdin_used = True
#except Exception as e:
# if debug:
# syslog.syslog("FATAL ERROR: Not all required input received")
# print(str(e))
# syslog.syslog(str(e))
# sys.exit(1)
#if debug: #if debug:
# syslog.syslog("Encoding of subject: {0}".format(ftfy.guess_bytes(email_subject)[1])) # syslog.syslog("Encoding of subject: {0}".format(ftfy.guess_bytes(email_subject)[1]))