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
|
2022-11-12 16:18:11 +01:00
|
|
|
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
|
|
|
|
|
2022-11-12 16:18:11 +01:00
|
|
|
reportErrorLocal :: MonadIO m => String -> m ()
|
2022-11-18 11:58:49 +01:00
|
|
|
reportErrorLocal = liftIO . hPutStr stderr
|
2022-11-08 20:16:34 +01:00
|
|
|
|
2022-11-12 16:18:11 +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)
|
2022-11-18 11:58:49 +01:00
|
|
|
(L.find ((isChoirThisWeek today) . date) table)
|
2022-11-12 16:18:11 +01:00
|
|
|
|
|
|
|
|
|
|
|
main' :: App ()
|
|
|
|
main' = do
|
|
|
|
args <- liftIO getArgs
|
2022-11-08 20:16:34 +01:00
|
|
|
if length args /= 1
|
|
|
|
then
|
2022-11-12 16:18:11 +01:00
|
|
|
fail "We need exactly one config path as option"
|
2022-11-08 20:16:34 +01:00
|
|
|
else do
|
2022-11-12 16:18:11 +01:00
|
|
|
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
|
2022-11-18 11:58:49 +01:00
|
|
|
mailText record = LT.concat ["Guten Morgen,\n\n"
|
2022-11-08 20:16:34 +01:00
|
|
|
, announcement record
|
|
|
|
,"\n\ndiesen Donnerstag\n\nDurchsingen: "
|
|
|
|
,song1 record
|
|
|
|
,", "
|
|
|
|
,song2 record
|
|
|
|
,"\nStimmproben: "
|
|
|
|
,voice1 record
|
|
|
|
,", "
|
|
|
|
,voice2 record
|
2023-03-09 20:46:10 +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]
|