Fixing input charset issues by autodetecting

This commit is contained in:
László Károlyi 2015-05-14 22:51:48 +02:00
parent 4723f43223
commit 3b9f6cd4e5
7 changed files with 36 additions and 4 deletions

3
.gitignore vendored
View file

@ -55,3 +55,6 @@ docs/_build/
# PyBuilder # PyBuilder
target/ target/
# Ignore the in here installed virtualenv
virtualenv/

View file

@ -24,6 +24,9 @@ Used python3 modules:
- `email` to construct the email. - `email` to construct the email.
- `syslog` for logging the sending of emails into syslog - `syslog` for logging the sending of emails into syslog
External modules:
- `chardet`, to autodetect the input charset (which can vary)
## Requirements: ## Requirements:
- A mailserver. - A mailserver.
@ -35,7 +38,9 @@ Used python3 modules:
- Install `python3` (do I need to say this?) - Install `python3` (do I need to say this?)
- Clone this repository into a random directory, reachable for dovecot2 - 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 - Copy your replies (mentioned above) into the same repo directory
- Edit your sieve configuration to pipe your given role into this tool. - Edit your sieve configuration to pipe your given role into this tool.

10
email-responder.py Executable file → Normal file
View file

@ -2,12 +2,12 @@
import sys import sys
import os import os
import io
import smtplib import smtplib
import sqlite3 import sqlite3
import datetime import datetime
import syslog import syslog
import codecs import codecs
import chardet
from email.mime.text import MIMEText from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart 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__)) my_dir = os.path.dirname(os.path.realpath(__file__))
os.chdir(my_dir) os.chdir(my_dir)
stdin_utf8 = io.TextIOWrapper(sys.stdin.buffer, encoding='utf-8') input_bytes = sys.stdin.buffer.read() # This is bytes
original_headers = Parser().parsestr(stdin_utf8.read())
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: # with open('email1.txt') as fp:
# original_headers = Parser().parsestr(fp.read()) # original_headers = Parser().parsestr(fp.read())

9
email-responder.sh Executable file
View file

@ -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

1
requirements.txt Normal file
View file

@ -0,0 +1 @@
chardet==2.3.0

10
tools/install.sh Executable file
View file

@ -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