From d3dd0e129658e3617f9e6e4fa0910cb15c42520d Mon Sep 17 00:00:00 2001 From: Joris Guyonvarch Date: Tue, 14 Apr 2015 00:10:21 +0200 Subject: Send mail to notify for new ads --- src/AdListener.hs | 28 ++++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) (limited to 'src/AdListener.hs') diff --git a/src/AdListener.hs b/src/AdListener.hs index 21775cc..da3051d 100644 --- a/src/AdListener.hs +++ b/src/AdListener.hs @@ -22,6 +22,8 @@ import View.Ad (renderAds) import Page import Parser.Detail +import Mail (sendMail) + import Config (Config) import qualified Config as C @@ -35,7 +37,9 @@ listenToNewAds config = do listenError config [] error Right resumes -> let newURLs = map url resumes - in listenToNewAdsWithViewedURLs config newURLs + in do + putStrLn "Listening for new ads…" + listenToNewAdsWithViewedURLs config newURLs listenToNewAdsWithViewedURLs :: Config -> [URL] -> IO () listenToNewAdsWithViewedURLs config viewedURLs = do @@ -59,12 +63,32 @@ listenToNewAdsWithResumes config viewedURLs resumes = time <- getCurrentFormattedTime if not (null newAds) then - T.putStrLn (newAdsMessage time newAds) + let message = newAdsMessage time newAds + in do + T.putStrLn message + trySendMail config message else return () waitOneMinute listenToNewAdsWithViewedURLs config (viewedURLs ++ newURLs) +trySendMail :: Config -> Text -> IO () +trySendMail config message = + case C.mailTo config of + Just mailTo -> + do + eitherMailSuccess <- sendMail mailTo message + case eitherMailSuccess of + Right () -> + return "Mail sent." + Left error -> + T.putStrLn . T.concat $ + [ "Error sending mail: " + , error + ] + Nothing -> + return () + newAdsMessage :: Text -> [Ad] -> Text newAdsMessage time newAds = let newAdsMessage = -- cgit v1.2.3