Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: allow parsing of mutt aliases using non-ASCII characters #517

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion configs/purebred.hs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import System.Environment (lookupEnv)
import System.Exit (die)
import System.IO.Unsafe

import Data.MIME (defaultCharsets)
import Purebred
import Purebred.Storage.AddressBook.MuttAliasFile

Expand Down Expand Up @@ -86,7 +87,7 @@ main = do

let addrsFile = fromMaybe cwd confdir <> "/aliases"
addressBook <-
initMuttAliasFileAddressBook addrsFile
initMuttAliasFileAddressBook defaultCharsets addrsFile
>>= either (die . show) pure

purebred
Expand Down
2 changes: 2 additions & 0 deletions src/Purebred.hs
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ module Purebred
, module Purebred.Plugin,
module Purebred.Plugin.TweakConfig,
module Purebred.Storage.Tags,
module Purebred.Storage.AddressBook.MuttAliasFile,
module Purebred.Types,
module Purebred.Types.Error,
module Purebred.UI.Actions,
Expand Down Expand Up @@ -187,6 +188,7 @@ import Purebred.Plugin.Internal
import Purebred.Plugin.TweakConfig
import Purebred.Storage.Server
import Purebred.Storage.Tags (TagOp(..))
import Purebred.Storage.AddressBook.MuttAliasFile
import Purebred.Types.Error

-- re-exports for configuration
Expand Down
18 changes: 9 additions & 9 deletions src/Purebred/Storage/AddressBook/MuttAliasFile.hs
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,16 @@ module Purebred.Storage.AddressBook.MuttAliasFile
, parseMuttAliasFile
) where

import Data.Attoparsec.ByteString (Parser, parseOnly, sepBy, string, takeTill)
import Data.Attoparsec.ByteString.Char8 (endOfLine, isSpace_w8, skipSpace, space)
import Data.Attoparsec.Text (Parser, parseOnly, sepBy, string, takeTill, space, skipSpace, endOfLine)
import Data.Char (isSpace)
import Data.Bifunctor (bimap)
import qualified Data.ByteString as B
import qualified Data.Text as T
import qualified Data.Text.Internal.Search as T
import Control.Lens (Lens', lens, toListOf, folded, filtered, view)

import Data.MIME (defaultCharsets)
import Data.IMF (address, Address)
import Data.MIME (Address)
import Data.IMF.Text (address)

import Purebred.Types.Error (Error(ParseError))
import Purebred.Types.AddressBook (AddressBook(..))
Expand All @@ -59,7 +59,7 @@ initMuttAliasFileAddressBook filePath = do
let mk addrs = AddressBook
(\substr -> pure $ filterMuttAliases substr addrs)
Nothing
pure $ bimap ParseError mk $ parseMuttAliasFile contents
pure $ bimap ParseError mk $ parseMuttAliasFile (decodeLenient contents)

filterMuttAliases :: T.Text -> [MuttAlias] -> [Address]
filterMuttAliases substr =
Expand All @@ -73,14 +73,14 @@ matchesSubstring :: T.Text -> T.Text -> Bool
matchesSubstring needle haystack = not $ null $ T.indices needle haystack

-- | Parser functions to parse a mutt alias file
parseMuttAliasFile :: B.ByteString -> Either String [MuttAlias]
parseMuttAliasFile :: T.Text -> Either String [MuttAlias]
parseMuttAliasFile = parseOnly (muttalias `sepBy` endOfLine)

muttalias :: Parser MuttAlias
muttalias = do
nick <- string "alias" *> space *> takeTill isSpace_w8
add <- skipSpace *> address defaultCharsets
pure $ MuttAlias (decodeLenient nick) add
nick <- string "alias" *> space *> takeTill isSpace
add <- skipSpace *> address
pure $ MuttAlias nick add

-- | Parser Datatypes
data MuttAlias = MuttAlias
Expand Down
6 changes: 3 additions & 3 deletions test/TestAddressBook.hs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ module TestAddressBook (
import Test.Tasty (TestTree, testGroup)
import Test.Tasty.HUnit ((@?=), testCase)

import Data.IMF (Address(..))
import Data.MIME (Address(..))

import Purebred.Storage.AddressBook.MuttAliasFile

Expand All @@ -50,11 +50,11 @@ testParseMuttAlias =
}
]
, testCase "with long names" $
parseMuttAliasFile "alias nick1 Mr Nick Name <[email protected]>\nalias nick2 Nick Test Name <[email protected]>"
parseMuttAliasFile "alias nick1 Mr Nück Name <[email protected]>\nalias nick2 Nick Test Name <[email protected]>"
@?= Right [
MuttAlias {
_muttAliasNick = "nick1"
, _muttAliasAddress = Single "Mr Nick Name <[email protected]>"
, _muttAliasAddress = Single "Mr Nück Name <[email protected]>"
}
, MuttAlias {
_muttAliasNick = "nick2"
Expand Down
Loading