1 {-# LANGUAGE DoAndIfThenElse #-}
11 import Data.ConfigFile
12 import Data.Maybe (isJust)
13 import Data.Monoid (Monoid(..))
14 import Network.HTTP.Conduit (CookieJar, createCookieJar, destroyCookieJar)
15 import System.Directory (
17 getAppUserDataDirectory
19 import System.Exit (ExitCode(..), exitWith)
20 import System.FilePath.Posix (joinPath)
21 import System.IO (hPutStrLn, stderr)
23 import qualified CommandLine as Cmd (
30 -- | Contains all of our configurable options.
33 cookie_jar :: CookieJar,
36 password :: Maybe String,
37 username :: Maybe String }
39 -- An empty CookieJar. See cj_append for rationale.
41 cj_empty = createCookieJar []
44 -- Defined for convenience; I would really like to use mappend but GHC
45 -- bitches about the orphan instance.
46 cj_append :: CookieJar -> CookieJar -> CookieJar
48 createCookieJar (cookies1 ++ cookies2)
50 -- Decompose the cookie jars into lists.
51 cookies1 = destroyCookieJar cj1
52 cookies2 = destroyCookieJar cj2
54 instance Monoid Cfg where
55 mempty = Cfg { article = mempty,
56 cookie_jar = cj_empty,
63 let article' = (if null article1 then article2 else article1)
64 cookie_jar' = cookie_jar1 `cj_append` cookie_jar2
65 full_stories' = full_stories1 || full_stories2
66 output' = (if null output1 then output2 else output1)
67 password' = password1 `mappend` password2
68 username' = username1 `mappend` username2
70 Cfg { article = article',
71 cookie_jar = cookie_jar',
72 full_stories = full_stories',
75 username = username' }
79 cookie_jar1 = cookie_jar c1
80 cookie_jar2 = cookie_jar c2
81 full_stories1 = full_stories c1
82 full_stories2 = full_stories c2
85 password1 = password c1
86 password2 = password c2
87 username1 = username c1
88 username2 = username c2
91 use_account :: Cfg -> Bool
93 (isJust $ username cfg) && (isJust $ password cfg)
95 format_cpe :: CPError -> String
96 format_cpe (ParseError desc, _) = "Parse error: " ++ desc
97 format_cpe (SectionAlreadyExists desc, _) = "Section already exists: " ++ desc
98 format_cpe (NoSection desc, _) = "Missing section: " ++ desc
99 format_cpe (NoOption desc, _) = "Missing required option: " ++ desc
100 format_cpe (OtherProblem desc, _) = "Error: " ++ desc
101 format_cpe (InterpolationError desc, _) = "Interpolation error: " ++ desc
104 -- | The config parsing functions return either the result or a
105 -- 'CPError'. If there was an error, we represent it as 'Nothing'
106 -- (at least if we're forcing the value into a 'Maybe'
107 -- wrapper. Otherwise, we return 'Just' the result.
108 either_to_maybe :: Either CPError a -> Maybe a
109 either_to_maybe (Left _) = Nothing
110 either_to_maybe (Right x) = Just x
113 config_filename :: String
114 config_filename = Cmd.program_name ++ ".conf"
116 config_path :: IO FilePath
118 cfg_dir <- getAppUserDataDirectory Cmd.program_name
119 let cfg_file = joinPath [cfg_dir, config_filename]
122 parse_config :: IO (Either String Cfg)
124 cfg_file <- config_path
125 it_exists <- doesFileExist cfg_file
126 if not it_exists then do
127 return $ Right mempty
129 parse_result <- readfile emptyCP cfg_file
133 Left err -> Left (format_cpe err)
135 let cp_full_stories = get cp "DEFAULT" "full_stories"
136 cp_password = get cp "DEFAULT" "password"
137 cp_username = get cp "DEFAULT" "username"
139 cfg_password = either_to_maybe cp_password
140 cfg_full_stories = case cp_full_stories of
141 Left _ -> False -- default
143 cfg_username = either_to_maybe cp_username
145 Right $ mempty { full_stories = cfg_full_stories,
146 password = cfg_password,
147 username = cfg_username }
153 cmd_article <- Cmd.apply_args
154 let arg_cfg = mempty { article = Cmd.article cmd_article,
155 full_stories = Cmd.full_stories cmd_article,
156 output = Cmd.output cmd_article }
158 either_file_cfg <- parse_config
159 case either_file_cfg of
162 exitWith $ ExitFailure exit_config_parse_failed
164 -- The left operand takes precedence when both are non-empty!
165 return $ arg_cfg `mappend` file_cfg