choirMail/app/Main.hs

78 lines
2.4 KiB
Haskell
Raw Normal View History

2022-11-08 20:16:34 +01:00
{-# LANGUAGE OverloadedStrings #-}
module Main where
import TableParser
import Requester
import Config
import Data.Time.Calendar
import Data.Time.Clock
import qualified Data.List as L
import System.IO
import qualified Data.Text as T
import qualified Data.Text.Lazy as LT
import System.Environment
import Data.Time.Format.ISO8601
import Sender
import Monad
2022-11-08 20:16:34 +01:00
isChoirThisWeek :: Day -> Day -> Bool
isChoirThisWeek today day = today <= day && diffDays day today <= 6
getToday :: IO Day
getToday = utctDay <$> getCurrentTime
reportErrorLocal :: MonadIO m => String -> m ()
reportErrorLocal = liftIO . hPutStr stderr
2022-11-08 20:16:34 +01:00
reportErrorMail :: MonadIO m => Config -> String -> m ()
reportErrorMail config error' = send (mailDomain config) (mailUsername config) (mailPassword config) (mailErrorTo config) (mailFrom config) "choirMail Error" (LT.pack error')
findChoirDay :: Day -> [MailRecord] -> Either String MailRecord
findChoirDay today table = maybe
(Left "Keine Probe :(")
(Right)
(L.find ((isChoirThisWeek today) . date) table)
main' :: App ()
main' = do
args <- liftIO getArgs
2022-11-08 20:16:34 +01:00
if length args /= 1
then
fail "We need exactly one config path as option"
2022-11-08 20:16:34 +01:00
else do
config <- parseConfigFile (head args)
-- we want to handle these while we have the config in scope
result <- liftIO $ runApp $ do
bs <- request
table <- except $ parseBString bs
today <- liftIO getToday
record <- except $ findChoirDay today table
send (mailDomain config) (mailUsername config) (mailPassword config) (mailTo config) (mailFrom config) (mailSubject record) (mailText record)
case result of
Right x -> return x
Left error' -> reportErrorMail config error'
main :: IO ()
main = do
result <- runApp main'
case result of
Right x -> return x
Left error' -> reportErrorLocal error'
2022-11-08 20:16:34 +01:00
mailText :: MailRecord -> LT.Text
mailText record = LT.concat ["Guten Morgen,\n\n"
2022-11-08 20:16:34 +01:00
, announcement record
,"\n\ndiesen Donnerstag\n\nDurchsingen: "
2023-12-10 19:35:58 +01:00
,songs record
2022-11-08 20:16:34 +01:00
,"\nStimmproben: "
2023-12-10 19:35:58 +01:00
,voices record
,"\nNoten mitbringen: "
, notes record
2023-03-09 23:26:02 +01:00
,"\n\nLG\nJo^^\n" ]
2022-11-08 20:16:34 +01:00
mailSubject :: MailRecord -> T.Text
mailSubject record = T.concat ["Donnerstag ", T.pack $ iso8601Show $ date record]