diff options
author | Gregor Kleen <aethoago@141.li> | 2017-03-08 14:39:38 +0100 |
---|---|---|
committer | Gregor Kleen <aethoago@141.li> | 2017-03-08 14:39:38 +0100 |
commit | b78644e2fe649207d81a89d0af36988aed110104 (patch) | |
tree | 3bf68d64f91a56858f78763eda1b3f69ee20ff17 /src/Simple.hs | |
parent | c6f1f8c512e8dbca953cb43c11a08c6fcf54f6d3 (diff) | |
download | postdelay-b78644e2fe649207d81a89d0af36988aed110104.tar postdelay-b78644e2fe649207d81a89d0af36988aed110104.tar.gz postdelay-b78644e2fe649207d81a89d0af36988aed110104.tar.bz2 postdelay-b78644e2fe649207d81a89d0af36988aed110104.tar.xz postdelay-b78644e2fe649207d81a89d0af36988aed110104.zip |
Framework for spooling
Diffstat (limited to 'src/Simple.hs')
-rw-r--r-- | src/Simple.hs | 77 |
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 | |||
1 | module Main (main) where | 3 | module Main (main) where |
2 | 4 | ||
3 | import Postdelay.Scan | 5 | import Postdelay.Scan |
6 | import Postdelay.Queue | ||
4 | 7 | ||
5 | import Control.Monad.IO.Class | 8 | import Data.Function (on) |
6 | import Control.Monad.Trans.Except | 9 | import Data.List |
10 | import Data.CaseInsensitive (CI) | ||
11 | import qualified Data.CaseInsensitive as CI | ||
7 | 12 | ||
8 | 13 | import Control.Monad.IO.Class (MonadIO(..)) | |
9 | main :: MonadIO m => m () | 14 | import Control.Monad.Catch (MonadCatch) |
15 | import Control.Monad.Reader | ||
16 | |||
17 | import Data.ByteString.Lazy as Lazy (ByteString) | ||
18 | import qualified Data.ByteString.Lazy as Lazy.BS | ||
19 | |||
20 | import Data.ByteString.Lazy.Char8 as Lazy.Char8 (ByteString) | ||
21 | import qualified Data.ByteString.Lazy.Char8 as Lazy.Char8.BS | ||
22 | |||
23 | import Data.Conduit | ||
24 | |||
25 | import System.Environment (getProgName) | ||
26 | |||
27 | import qualified Options.Applicative as Opt (Parser) | ||
28 | import Options.Applicative hiding (Parser) | ||
29 | |||
30 | |||
31 | opts :: Opt.Parser (String, [String], StoreConfig) | ||
32 | opts = 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 | |||
44 | main :: (MonadIO m, MonadCatch m) => m () | ||
10 | main = do | 45 | main = 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 | |
20 | sendNow :: MonadIO m => String -> m () | 55 | delay <- scan mailStr |
21 | sendNow = undefined | 56 | let |
22 | 57 | (headers, body) = break (== "") $ lines mailStr | |
23 | sendLater :: MonadIO m => String -> Delay -> m () | 58 | mailStr' = unlines $ filter (not . (isPrefixOf `on` CI.foldCase) "X-Delay:") headers ++ body |
24 | sendLater = 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 | ||