summaryrefslogtreecommitdiff
path: root/src/Simple.hs
diff options
context:
space:
mode:
Diffstat (limited to 'src/Simple.hs')
-rw-r--r--src/Simple.hs77
1 files changed, 59 insertions, 18 deletions
diff --git a/src/Simple.hs b/src/Simple.hs
index f461766..68524a8 100644
--- a/src/Simple.hs
+++ b/src/Simple.hs
@@ -1,24 +1,65 @@
1{-# LANGUAGE RecordWildCards, ApplicativeDo, FlexibleContexts #-}
2
1module Main (main) where 3module Main (main) where
2 4
3import Postdelay.Scan 5import Postdelay.Scan
6import Postdelay.Queue
4 7
5import Control.Monad.IO.Class 8import Data.Function (on)
6import Control.Monad.Trans.Except 9import Data.List
10import Data.CaseInsensitive (CI)
11import qualified Data.CaseInsensitive as CI
7 12
8 13import Control.Monad.IO.Class (MonadIO(..))
9main :: MonadIO m => m () 14import Control.Monad.Catch (MonadCatch)
15import Control.Monad.Reader
16
17import Data.ByteString.Lazy as Lazy (ByteString)
18import qualified Data.ByteString.Lazy as Lazy.BS
19
20import Data.ByteString.Lazy.Char8 as Lazy.Char8 (ByteString)
21import qualified Data.ByteString.Lazy.Char8 as Lazy.Char8.BS
22
23import Data.Conduit
24
25import System.Environment (getProgName)
26
27import qualified Options.Applicative as Opt (Parser)
28import Options.Applicative hiding (Parser)
29
30
31opts :: Opt.Parser (String, [String], StoreConfig)
32opts = do
33 sConfig <- storeConfig
34 sender <- strOption $ mconcat
35 [ long "sender"
36 , short 'f'
37 , metavar "SENDER"
38 ]
39 recipients <- some . strArgument $ mconcat
40 [ metavar "RECIPIENTS"
41 ]
42 pure (sender, recipients, sConfig)
43
44main :: (MonadIO m, MonadCatch m) => m ()
10main = do 45main = do
11 mailStr <- liftIO getContents 46 pName <- liftIO getProgName
12 delay <- runExceptT $ scan mailStr 47 (sender, recipients, sConfig) <- liftIO . execParser . info (helper <*> opts) $ mconcat
13 case delay of 48 [ fullDesc
14 Left err -> do 49 , progDesc "Queue an email for later delivery"
15 liftIO . putStrLn $ show err 50 , header pName
16 sendNow mailStr 51 ]
17 Right Nothing -> sendNow mailStr 52
18 Right (Just d) -> sendLater mailStr d 53 flip runReaderT sConfig $ do
19 54 mailStr <- liftIO getContents
20sendNow :: MonadIO m => String -> m () 55 delay <- scan mailStr
21sendNow = undefined 56 let
22 57 (headers, body) = break (== "") $ lines mailStr
23sendLater :: MonadIO m => String -> Delay -> m () 58 mailStr' = unlines $ filter (not . (isPrefixOf `on` CI.foldCase) "X-Delay:") headers ++ body
24sendLater = undefined 59
60 sendNow = sendmail sender recipients $ Lazy.Char8.BS.pack mailStr'
61 sendLater sendDelay = pushStore . yield $ StoredMail
62 { message = Lazy.Char8.BS.pack mailStr'
63 , ..
64 }
65 maybe sendNow sendLater delay