aboutsummaryrefslogtreecommitdiff
path: root/src/Main.hs
blob: e5b33d4387e862611fc83cc01c222c652394a580 (plain) (blame)
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
{-
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 <https://www.gnu.org/licenses/>.

-}

{-# 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

main :: IO ()
main = do
  options <- execParser opts
  (feeds, mbMaildir, mbDbPath) <- getUserdata $ oConfigPath options
  let root = fromMaybe (oMaildir options) mbMaildir
  mapM_ (createDirectoryIfMissing True)
    [root </> "new", root </> "cur", root </> "tmp"]
  let dbPath = fromMaybe (oDbPath options) mbDbPath
  mapM_ (\feed ->
    catch (runStdoutLoggingT $ processFeed root dbPath feed)
    (\e -> runStderrLoggingT $ logErrorN $
           (T.pack $ show (e :: SomeException)) <> "\n"))
    feeds
  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")

-- 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

data Options = Options
  { oConfigPath :: FilePath
  , oDbPath :: FilePath
  , oMaildir :: FilePath
  }

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.")
  <*> strOption (long "db" <> short 'd' <> metavar "DB"
                  <> value "~/.f2mdb.json" <> showDefault
                  <> help "Path to db file storing timestamps of feeds.")
  <*> strOption (long "maildir" <> short 'm' <> metavar "MAILDIR"
                  <> value "~/mail/f2md" <> showDefault
                  <> help "Path to maildir root to deposit the maildir \
                          \message files.")