{- Copyright (C) 2022 Yuchen Pei. This file is part of f2md. f2md is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. f2md is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. You should have received a copy of the GNU Affero General Public License along with f2md. If not, see . -} {-# LANGUAGE OverloadedStrings #-} import qualified Data.Text as T import Options.Applicative import Control.Monad.IO.Class import Control.Monad.Logger import System.Directory import Control.Monad.Extra import Control.Exception import F2Md.Types import F2Md.Import import F2Md.Export import F2Md.Config import Data.Maybe import System.FilePath -- TODO: kill on double C-c main :: IO () main = do options <- execParser opts extConfs <- getUserdata $ oConfigPath options mapM_ processConfig extConfs where opts = info (progParser <**> helper) (fullDesc <> progDesc "f2md feeds to maildir" <> header "f2md - a utility to pull new items \ \from feeds and write them to maildir") processConfig :: ExtConf -> IO () processConfig (ExtConf dbPath maildir feeds) = do mapM_ (createDirectoryIfMissing True) [maildir "new", maildir "cur", maildir "tmp"] runStdoutLoggingT $ logInfoN $ "Updating " <> T.pack (show $ length feeds) <> " feeds for " <> T.pack maildir <> "..." mapM_ (\feed -> catch (runStdoutLoggingT $ processFeed maildir dbPath feed) (\e -> runStdoutLoggingT $ logErrorN $ T.pack (show (e :: SomeException)))) feeds data Options = Options { oConfigPath :: FilePath } -- TODO: update db on interruption with the latest date processFeed :: FilePath -> FilePath -> FeedUserdata -> LoggingT IO () processFeed root dbPath feed = do logInfoN $ "Fetching " <> fuUrl feed <> "..." msgs <- liftIO $ toMessagesFromUrl (fuUrl feed) (fuLastUpdated feed) logInfoN $ "Writing " <> T.pack (show $ length msgs) <> " messages..." liftIO $ mapM_ (writeMessage root) msgs liftIO $ updateLastUpdated dbPath feed msgs progParser :: Parser Options progParser = Options <$> strOption (long "config" <> short 'c' <> metavar "CONFIG" <> value "~/.f2md.json" <> showDefault <> help "Config file storing feeds, maildir \ \location and timestamp file location.")