From 3b9f6cd4e5173e974f2410397e7bae11e29f1b4d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?La=CC=81szlo=CC=81=20Ka=CC=81rolyi?= Date: Thu, 14 May 2015 22:51:48 +0200 Subject: [PATCH] Fixing input charset issues by autodetecting --- .gitignore | 3 +++ README.md | 7 ++++++- email-responder.py | 10 +++++++--- email-responder.sh | 9 +++++++++ requirements.txt | 1 + init-schema.sh => tools/init-schema.sh | 0 tools/install.sh | 10 ++++++++++ 7 files changed, 36 insertions(+), 4 deletions(-) mode change 100755 => 100644 email-responder.py create mode 100755 email-responder.sh create mode 100644 requirements.txt rename init-schema.sh => tools/init-schema.sh (100%) create mode 100755 tools/install.sh diff --git a/.gitignore b/.gitignore index ba74660..18505b2 100644 --- a/.gitignore +++ b/.gitignore @@ -55,3 +55,6 @@ docs/_build/ # PyBuilder target/ + +# Ignore the in here installed virtualenv +virtualenv/ diff --git a/README.md b/README.md index 38bf399..7f6794c 100644 --- a/README.md +++ b/README.md @@ -24,6 +24,9 @@ Used python3 modules: - `email` to construct the email. - `syslog` for logging the sending of emails into syslog +External modules: +- `chardet`, to autodetect the input charset (which can vary) + ## Requirements: - A mailserver. @@ -35,7 +38,9 @@ Used python3 modules: - Install `python3` (do I need to say this?) - Clone this repository into a random directory, reachable for dovecot2 -- Initialize the sqlite db with running `init-schema.sh` +- Change to that directory +- Install virtualenv and external modules by running `tools/install.sh` +- Initialize the sqlite db with running `tools/init-schema.sh` - Copy your replies (mentioned above) into the same repo directory - Edit your sieve configuration to pipe your given role into this tool. diff --git a/email-responder.py b/email-responder.py old mode 100755 new mode 100644 index 3152ec9..9c187c8 --- a/email-responder.py +++ b/email-responder.py @@ -2,12 +2,12 @@ import sys import os -import io import smtplib import sqlite3 import datetime import syslog import codecs +import chardet from email.mime.text import MIMEText from email.mime.multipart import MIMEMultipart @@ -19,8 +19,12 @@ from email.header import decode_header, make_header my_dir = os.path.dirname(os.path.realpath(__file__)) os.chdir(my_dir) -stdin_utf8 = io.TextIOWrapper(sys.stdin.buffer, encoding='utf-8') -original_headers = Parser().parsestr(stdin_utf8.read()) +input_bytes = sys.stdin.buffer.read() # This is bytes + +encoding_result = chardet.detect(input_bytes) +input_decoded = input_bytes.decode(encoding_result['encoding']) + +original_headers = Parser().parsestr(input_decoded) # with open('email1.txt') as fp: # original_headers = Parser().parsestr(fp.read()) diff --git a/email-responder.sh b/email-responder.sh new file mode 100755 index 0000000..c7d5127 --- /dev/null +++ b/email-responder.sh @@ -0,0 +1,9 @@ +#!/usr/bin/env bash + +PROJECT_DIR=$(cd $(dirname ${BASH_SOURCE[0]});pwd) + +cd $PROJECT_DIR + +source virtualenv/bin/activate + +exec python3 email-responder.py diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..7e01c11 --- /dev/null +++ b/requirements.txt @@ -0,0 +1 @@ +chardet==2.3.0 diff --git a/init-schema.sh b/tools/init-schema.sh similarity index 100% rename from init-schema.sh rename to tools/init-schema.sh diff --git a/tools/install.sh b/tools/install.sh new file mode 100755 index 0000000..43ad554 --- /dev/null +++ b/tools/install.sh @@ -0,0 +1,10 @@ +#!/usr/bin/env bash + +PROJECT_DIR=$(cd $(dirname ${BASH_SOURCE[0]});cd ..;pwd) + +cd $PROJECT_DIR + +pyvenv virtualenv +source virtualenv/bin/activate + +pip install -r requirements.txt