Skip to content

Commit

Permalink
parse multiline headers from editor input
Browse files Browse the repository at this point in the history
The input still contains utf-8 encoded stuff tped in by the user (in headers and body), that needs
to be recoded to mime format. Hence we do it ourselves.
This adds handling for multiline header values and fixes issue #31
  • Loading branch information
pazz committed Aug 26, 2011
1 parent 160515b commit f067867
Showing 1 changed file with 19 additions and 6 deletions.
25 changes: 19 additions & 6 deletions alot/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
Copyright (C) 2011 Patrick Totzke <patricktotzke@gmail.com>
"""
import os
import re
import code
import glob
import logging
Expand Down Expand Up @@ -751,17 +752,29 @@ def apply(self, ui):
self.mail = ui.current_buffer.get_email()

def openEnvelopeFromTmpfile():
# This parses the input from the tempfile.
# we do this ourselves here because we want to be able to
# just type utf-8 encoded stuff into the tempfile and let alot worry
# about encodings.

# get input
f = open(tf.name)
enc = settings.config.get('general', 'editor_writes_encoding')
editor_input = f.read().decode(enc)

#split editor out
headertext, bodytext = editor_input.split('\n\n', 1)

# go through multiline, utf-8 encoded headers
key = value = None
for line in headertext.splitlines():
key, value = line.strip().split(':', 1)
value = value.strip()
del self.mail[key] # ensure there is only one
if re.match('\w+:', line): #new k/v pair
if key and value: # save old one from stack
del self.mail[key] # ensure unique values in mails
self.mail[key] = encode_header(key, value) # save
key, value = line.strip().split(':', 1) # parse new pair
elif key and value: # append new line without key prefix to value
value += line
if key and value: # save last one if present
del self.mail[key]
self.mail[key] = encode_header(key, value)

if self.mail.is_multipart():
Expand All @@ -785,7 +798,7 @@ def openEnvelopeFromTmpfile():
for key in edit_headers:
value = u''
if key in self.mail:
value = decode_header(self.mail[key])
value = decode_header(self.mail.get(key, ''))
headertext += '%s: %s\n' % (key, value)

if self.mail.is_multipart():
Expand Down

0 comments on commit f067867

Please sign in to comment.