aboutsummaryrefslogtreecommitdiff
path: root/src/Main.hs
blob: d72bd95a1a52a05a3cf9fb984732f7ab0b586faa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
{-# LANGUAGE OverloadedStrings #-}

module Main
  ( main
  ) where

import System.IO (stderr)

import qualified Data.Text as T
import qualified Data.Text.IO as T

import Date (getCurrentDate)
import Birthdate (Birthdate, filterBirthdayAt)
import BirthdateParser (parseBirthdates)
import Mail (mailSubject, mailBody)
import SendMail (sendMail)
import Config

birthdatePath :: FilePath
birthdatePath = "birthdates.csv"

configPath :: FilePath
configPath = "config.txt"

main :: IO ()
main = do
  eitherBirthdates <- parseBirthdates <$> T.readFile birthdatePath
  eitherConfig <- getConfig configPath
  case (eitherBirthdates, eitherConfig) of
    (Left err, _) ->
      T.hPutStr stderr $
        T.concat
          [ "Error while parsing file "
          , T.pack birthdatePath
          , ":\n"
          , err
          ]
    (_, Left err) ->
      T.hPutStr stderr $
        T.concat
          [ "Error while parsing config file "
          , T.pack birthdatePath
          , ":\n"
          , err
          ]
    (Right birthdates, Right config) ->
      sendNotificationForBirthdayToday birthdates config

sendNotificationForBirthdayToday :: [Birthdate] -> Config -> IO ()
sendNotificationForBirthdayToday birthdates config = do
  currentDate <- getCurrentDate
  let birthdays = filterBirthdayAt currentDate birthdates
  if not (null birthdays)
    then
      sendMail
        (mailTo config)
        (mailFrom config)
        (mailSubject birthdays)
        (mailBody currentDate birthdays)
    else
      return ()