aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--logs2
-rw-r--r--src/Logger.hs22
-rw-r--r--src/Main.hs37
-rw-r--r--src/SendMail.hs13
4 files changed, 60 insertions, 14 deletions
diff --git a/logs b/logs
new file mode 100644
index 0000000..3269e69
--- /dev/null
+++ b/logs
@@ -0,0 +1,2 @@
+[21/11/2015] - Sending mail to joris.guyonvarch@gmail.com with subject “Hey, there is 1 birthday today!” and body “Alain Noe is now 19 years old.”
+[21/11/2015, 18:55] - Sending mail to joris.guyonvarch@gmail.com with subject “Hey, there is 1 birthday today!” and body “Alain Noe is now 19 years old.”
diff --git a/src/Logger.hs b/src/Logger.hs
new file mode 100644
index 0000000..4be55be
--- /dev/null
+++ b/src/Logger.hs
@@ -0,0 +1,22 @@
+{-# LANGUAGE OverloadedStrings #-}
+
+module Logger
+ ( info
+ ) where
+
+import Data.Text (Text)
+import qualified Data.Text as T
+import qualified Data.Text.IO as T (appendFile)
+import Data.Time.LocalTime (getZonedTime)
+import Data.Time.Format (formatTime, defaultTimeLocale)
+
+info :: Text -> IO ()
+info message = do
+ time <- T.pack <$> formatTime defaultTimeLocale "[%d/%m/%Y, %H:%M]" <$> getZonedTime
+ T.appendFile "logs" $
+ T.concat
+ [ time
+ , " - "
+ , message
+ , "\n"
+ ]
diff --git a/src/Main.hs b/src/Main.hs
index d72bd95..0aa2910 100644
--- a/src/Main.hs
+++ b/src/Main.hs
@@ -6,6 +6,7 @@ module Main
import System.IO (stderr)
+import Data.Text (Text)
import qualified Data.Text as T
import qualified Data.Text.IO as T
@@ -28,24 +29,32 @@ main = do
eitherConfig <- getConfig configPath
case (eitherBirthdates, eitherConfig) of
(Left err, _) ->
- T.hPutStr stderr $
- T.concat
- [ "Error while parsing file "
- , T.pack birthdatePath
- , ":\n"
- , err
- ]
+ birthdateError err
(_, Left err) ->
- T.hPutStr stderr $
- T.concat
- [ "Error while parsing config file "
- , T.pack birthdatePath
- , ":\n"
- , err
- ]
+ configError err
(Right birthdates, Right config) ->
sendNotificationForBirthdayToday birthdates config
+birthdateError :: Text -> IO ()
+birthdateError err =
+ T.hPutStr stderr $
+ T.concat
+ [ "Error while parsing file "
+ , T.pack birthdatePath
+ , ":\n"
+ , err
+ ]
+
+configError :: Text -> IO ()
+configError err =
+ T.hPutStr stderr $
+ T.concat
+ [ "Error while parsing config file "
+ , T.pack configPath
+ , ":\n"
+ , err
+ ]
+
sendNotificationForBirthdayToday :: [Birthdate] -> Config -> IO ()
sendNotificationForBirthdayToday birthdates config = do
currentDate <- getCurrentDate
diff --git a/src/SendMail.hs b/src/SendMail.hs
index 23b1b80..0e1f91d 100644
--- a/src/SendMail.hs
+++ b/src/SendMail.hs
@@ -5,12 +5,25 @@ module SendMail
) where
import Data.Text (Text)
+import qualified Data.Text as T
import Data.Text.Lazy (fromStrict)
import Network.Mail.Mime
+import Logger
+
sendMail :: Text -> Text -> Text -> Text -> IO ()
sendMail to from subject body = do
+ Logger.info $
+ T.concat
+ [ "Sending mail to "
+ , to
+ , " with subject “"
+ , subject
+ , "” and body “"
+ , body
+ , "”"
+ ]
renderSendMail (simpleMail' (address to) (address from) subject (fromStrict body))
address :: Text -> Address