changeset 11:f3d6d73a7184

Fixes
author Daniele Nicolodi <daniele@grinta.net>
date Mon, 11 Jan 2016 21:14:54 +0100
parents 636ea715af1e
children 4747393db602
files bnpparibas.py
diffstat 1 files changed, 29 insertions(+), 17 deletions(-) [+]
line wrap: on
line diff
--- a/bnpparibas.py	Mon Jan 11 19:45:30 2016 +0100
+++ b/bnpparibas.py	Mon Jan 11 21:14:54 2016 +0100
@@ -1,4 +1,5 @@
 import cgi
+import email
 import imp
 import itertools
 import json
@@ -46,6 +47,13 @@
 SEP = """-""" * len(HEADER)
 
 
+# GPG encrypted text is ascii and as such does not require encoding
+# but its decrypted form is utf-8 and therefore the charset header
+# must be set accordingly. define an appropriate charset object
+email.charset.add_charset('utf8 7bit', header_enc=email.charset.SHORTEST,
+                          body_enc=None, output_charset='utf-8')
+
+
 def loadconf(filename):
     module = imp.new_module('conf')
     module.__file__ = filename
@@ -339,22 +347,25 @@
             # not seen before
             unseen.append(t)
 
-    lines = []
-    lines.append(HEADER)
-    lines.append(SEP)
-    for t in unseen:
-        lines.append(str(t))
-    lines.append(SEP)
-    lines.append(FOOTER.format(balance=balance))
-    body = '\n'.join(lines)
+    if unseen:
+        lines = []
+        lines.append(HEADER)
+        lines.append(SEP)
+        for t in unseen:
+            lines.append(str(t))
+        lines.append(SEP)
+        lines.append(FOOTER.format(balance=balance))
 
-    message = MIMEText(encrypt(body, conf['MAILFROM'], conf['MAILTO']))
-    message['Subject'] = 'BNP Paribas Account update'
-    message['From'] = conf['MAILFROM']
-    message['To'] = conf['MAILTO']
-    message['Date'] = format_datetime(localtime())
+        text = '\n'.join(lines)
+        payload = encrypt(text, conf['MAILFROM'], conf['MAILTO'])
 
-    sendmail(message)
+        message = MIMEText(payload, _charset='utf8 7bit')
+        message['Subject'] = 'BNP Paribas Account update'
+        message['From'] = conf['MAILFROM']
+        message['To'] = conf['MAILTO']
+        message['Date'] = format_datetime(localtime())
+
+        sendmail(message)
 
     curs.executemany('''INSERT INTO transactions (id) VALUES (?)''', ((x.id, ) for x in unseen))
     db.commit()
@@ -368,14 +379,15 @@
     for m in data['messages']:
 
         curs = db.cursor()
-        curs.execute('''SELECT COUNT(*) FROM messages WHERE id = ?), 0)''', (m['id'], ))
+        curs.execute('''SELECT COUNT(*) FROM messages WHERE id = ?''', (m['id'], ))
         if curs.fetchone()[0]:
             # already handled
             continue
 
-        body = Message.fromjson(remote.message(m['id']))
+        text = Message.fromjson(remote.message(m['id']))
+        payload = encrypt(str(text), conf['MAILFROM'], conf['MAILTO'])
 
-        message = MIMEText(encrypt(str(body), conf['MAILFROM'], conf['MAILTO']))
+        message = MIMEText(payload, _charset='utf8 7bit')
         message['Subject'] = 'BNP Paribas Message'
         message['From'] = conf['MAILFROM']
         message['To'] = conf['MAILTO']