purebred-mua / purebred-email

A fast email parsing library implemented in Haskell
https://hackage.haskell.org/package/purebred-email
GNU Affero General Public License v3.0
23 stars 4 forks source link

Add encodeEncodedWords function for creating headers with non-ASCII #43

Closed singpolyma closed 4 years ago

singpolyma commented 4 years ago

Takes in the Text that you want in the header and returns either the same text as an ASCII ByteString or an appropriate encoded-word that will decode to the original.

frasertweedale commented 4 years ago

@singpolyma I have a general question. What is your use case? i.e. in what context are you turning Text into ByteString?

As part of fix for #50 I now have a function buildPhrase :: T.Text -> B.Builder. Note some productions from RFC 5322 and RFC 2047:

   phrase = 1*( encoded-word / word )
   word            =   atom / quoted-string
   atom            =   [CFWS] 1*atext [CFWS]

buildPhrase uses atom if possible, otherwise quoted-string if possible, otherwise encoded-word. Is it suitable for your use case?

singpolyma commented 4 years ago

My main usecase is encoding the Subject header, so I wouldn't want it to be wrapped in quotes.

frasertweedale commented 4 years ago

@singpolyma thanks for clarifying. I might introduce a specific optic to expose the subject header as 'Maybe Text', since it seems like a pretty common need :)

frasertweedale commented 4 years ago

@singpolyma I'm sorry it has been a while. I have made a few enhancements, including encoding only those words that require it rather than the whole input. Are you happy with what's in https://github.com/purebred-mua/purebred-email/tree/fix/50-mailbox-display-name-unicode ?

singpolyma commented 4 years ago

The comment:

 -- Currently turns the whole text into a single encoded word, if necessary.

is wrong now, but otherwise seems good (passes the tests in my application anyway, with appropriate changes).

frasertweedale commented 4 years ago

@singpolyma thanks for testing, and for picking up on the haddock needing fixing :+1:

frasertweedale commented 4 years ago

This was merged separately from the PR.