Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add mbox-from-files.hs

  • Loading branch information...
commit 002f69989fd5d14b44ae596ff271cf5f78ec12a1 1 parent 7c53969
Nicolas Pouillard authored
Showing with 70 additions and 0 deletions.
  1. +70 −0 mbox-from-files.hs
70 mbox-from-files.hs
View
@@ -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)
+
Please sign in to comment.
Something went wrong with that request. Please try again.