zgo-notifier/app/Main.hs

69 lines
2 KiB
Haskell
Raw Permalink Normal View History

2023-01-24 16:29:31 +00:00
{-# LANGUAGE OverloadedStrings #-}
module Main where
import Config
2023-01-24 20:01:08 +00:00
import qualified Data.Text.Lazy as L
import qualified Data.Text.Lazy.IO as TIO
2023-01-24 16:29:31 +00:00
import Data.Time.Clock
import Database.MongoDB
import qualified Network.Mail.Mime as M
import Network.Mail.SMTP
2023-01-24 20:01:08 +00:00
import qualified Network.Socket as NS
2023-01-24 16:29:31 +00:00
import Owner
import System.Exit
main :: IO ()
main = do
now <- getCurrentTime
loadedConfig <- loadZGoConfig "zgo.cfg"
pipe <- connect $ host (c_dbHost loadedConfig)
let db = c_dbName loadedConfig
x <-
access
pipe
master
db
(auth (c_dbUser loadedConfig) (c_dbPassword loadedConfig))
if x
then do
putStrLn "Connected to MongoDB!"
users <- access pipe master db $ findExpiringOwners now
putStr "Expiring users: "
print $ length users
mapM_ (emailOwner loadedConfig) users
2023-01-24 20:01:08 +00:00
else die "MongoDB connection failed! :("
2023-01-24 16:29:31 +00:00
where
emailOwner :: Config -> Document -> IO ()
emailOwner config doc = do
let o = (cast' . Doc) doc
case o of
Nothing -> putStrLn "Couldn't parse owner record."
Just o1 -> do
2023-01-24 20:01:08 +00:00
msgtmp <- TIO.readFile "email.html"
txttmp <- TIO.readFile "email.txt"
let msg =
L.replace
"PLACEHOLDER_EXPIRATION"
(L.pack $ show (oexpiration o1)) $
L.replace "PLACEHOLDER_NAME" (L.fromStrict $ ofirst o1) msgtmp
let txt =
L.replace
"PLACEHOLDER_EXPIRATION"
(L.pack $ show (oexpiration o1)) $
L.replace "PLACEHOLDER_NAME" (L.fromStrict $ ofirst o1) txttmp
2023-01-24 16:29:31 +00:00
mail <-
M.simpleMail
(Address (Just $ oname o1) (oemail o1))
(Address (Just "ZGo Support") "contact@zgo.cash")
"ZGo session about to expire"
2023-01-24 20:01:08 +00:00
txt
msg
2023-01-24 16:29:31 +00:00
[]
sendMailWithLogin'
2023-01-24 20:01:08 +00:00
(c_smtpHost config)
(fromInteger (c_smtpPort config) :: NS.PortNumber)
(c_smtpUser config)
(c_smtpPwd config)
2023-01-24 16:29:31 +00:00
mail