Skip to content

Commit

Permalink
Add mbox-from-files.hs
Browse files Browse the repository at this point in the history
  • Loading branch information
np committed Feb 24, 2010
1 parent 7c53969 commit 002f699
Showing 1 changed file with 70 additions and 0 deletions.
70 changes: 70 additions & 0 deletions mbox-from-files.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
{-# LANGUAGE TemplateHaskell, TypeOperators #-}
--------------------------------------------------------------------
-- |
-- Executable : mbox-from-files
-- Copyright : (c) Nicolas Pouillard 2010
-- License : BSD3
--
-- Maintainer: Nicolas Pouillard <nicolas.pouillard@gmail.com>
-- Stability : provisional
-- Portability:
--
--------------------------------------------------------------------

import Prelude hiding (mod)
import Control.Monad (join)
import Codec.Mbox (Mbox(..),MboxMessage(..),showMbox)
import System.Environment (getArgs)
import System.Console.GetOpt
import Data.Record.Label
import qualified Data.ByteString.Lazy.Char8 as B

data Settings = Settings { _help :: Bool }

$(mkLabels [''Settings])
help :: Settings :-> Bool

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.empty
, _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 get help opts
then usage ""
else
case (nonopts, errs) of
([], []) -> mboxFromFiles opts . lines =<< getContents
(files, []) -> mboxFromFiles opts files
(_, _) -> usage (concat errs)

0 comments on commit 002f699

Please sign in to comment.