-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmbox-from-files.hs
69 lines (58 loc) · 2.13 KB
/
mbox-from-files.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
{-# LANGUAGE TemplateHaskell, TypeOperators #-}
--------------------------------------------------------------------
-- |
-- Executable : mbox-from-files
-- Copyright : (c) Nicolas Pouillard 2010, 2011
-- License : BSD3
--
-- Maintainer: Nicolas Pouillard <[email protected]>
-- Stability : provisional
-- Portability:
--
--------------------------------------------------------------------
import Prelude hiding (mod)
import Control.Monad (join)
import Control.Lens ((^.), makeLenses, set)
import Codec.Mbox (Mbox(..),MboxMessage(..),showMbox)
import System.Environment (getArgs)
import System.Console.GetOpt
import qualified Data.ByteString.Lazy.Char8 as B
data Settings = Settings { _help :: Bool }
$(makeLenses ''Settings)
type Flag = Settings -> Settings
msgFromFile :: FilePath -> IO (MboxMessage B.ByteString)
msgFromFile fp =
do contents <- B.readFile fp
return $ MboxMessage { _mboxMsgSender = B.pack "XXX"
, _mboxMsgTime = B.pack "Thu Jan 01 01:00:00 +0100 1970"
, _mboxMsgBody = contents
, _mboxMsgFile = fp
, _mboxMsgOffset = undefined }
mboxFromFiles :: Settings -> [FilePath] -> IO ()
mboxFromFiles _ =
join . fmap (B.putStr . showMbox . Mbox)
. mapM msgFromFile
defaultSettings :: Settings
defaultSettings = Settings { _help = False }
usage :: String -> a
usage msg = error $ unlines ls
where header = "Usage: mbox-from-files [OPTION] <mail-files>*"
ls = msg : usageInfo header options :
["If no files are given as argument, a list of"
,"file names is read from the standard input."]
options :: [OptDescr Flag]
options =
[ Option "?" ["help"] (NoArg (set help True)) "Show this help message"
]
main :: IO ()
main = do
args <- getArgs
let (flags, nonopts, errs) = getOpt Permute options args
let opts = foldr ($) defaultSettings flags
if opts^.help
then usage ""
else
case (nonopts, errs) of
([], []) -> mboxFromFiles opts . lines =<< getContents
(files, []) -> mboxFromFiles opts files
(_, _) -> usage (concat errs)