aboutsummaryrefslogtreecommitdiff
path: root/src/Main.hs
blob: 29e9698f0396aea4225d1efe67ccf789b4be7378 (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
{-# 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 (filterBirthday)
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) -> do
      currentDate <- getCurrentDate
      let birthdays = filterBirthday currentDate birthdates
      if not (null birthdays)
        then
          sendMail
            (mailTo config)
            (mailFrom config)
            (mailSubject birthdays)
            (mailBody currentDate birthdays)
        else
          return ()