]> gitweb.michael.orlitzky.com - dead/halcyon.git/blob - src/Configuration.hs
c2136431577a574cd2ace86a67e93dc7d3747e2d
[dead/halcyon.git] / src / Configuration.hs
1 -- | This module defines the 'Cfg' type, which is just a wrapper
2 -- around all of the configuration options we accept on the command
3 -- line. We thread this throughout the rest of the program.
4 --
5
6 module Configuration (
7 Cfg(..),
8 merge_optional )
9 where
10
11 import System.Console.CmdArgs.Default ( Default(..) )
12
13 import qualified OptionalConfiguration as OC ( OptionalCfg(..) )
14 import Usernames ( Usernames(..) )
15
16
17 -- | The main configuration data type. It contains all options that
18 -- can be set in a config file or on the command line.
19 --
20 data Cfg =
21 Cfg { consumer_key :: String,
22 consumer_secret :: String,
23 access_token :: String,
24 access_secret :: String,
25 heartbeat :: Int,
26 ignore_replies :: Bool,
27 ignore_retweets :: Bool,
28 sendmail_path :: FilePath,
29 from_address :: Maybe String,
30 to_address :: Maybe String,
31 verbose :: Bool,
32 usernames :: Usernames }
33 deriving (Show)
34
35
36 instance Default Cfg where
37 -- | A 'Cfg' with all of its fields set to their default values.
38 --
39 def = Cfg { consumer_key = def,
40 consumer_secret = def,
41 access_token = def,
42 access_secret = def,
43 heartbeat = 600,
44 ignore_replies = def,
45 ignore_retweets = def,
46 sendmail_path = "/usr/sbin/sendmail",
47 from_address = def,
48 to_address = def,
49 verbose = def,
50 usernames = def }
51
52
53 -- | Merge a 'Cfg' with an 'OptionalCfg'. This is more or less the
54 -- Monoid instance for 'OptionalCfg', but since the two types are
55 -- different, we have to repeat ourselves.
56 --
57 merge_optional :: Cfg -> OC.OptionalCfg -> Cfg
58 merge_optional cfg opt_cfg =
59 Cfg
60 (merge (consumer_key cfg) (OC.consumer_key opt_cfg))
61 (merge (consumer_secret cfg) (OC.consumer_secret opt_cfg))
62 (merge (access_token cfg) (OC.access_token opt_cfg))
63 (merge (access_secret cfg) (OC.access_secret opt_cfg))
64 (merge (heartbeat cfg) (OC.heartbeat opt_cfg))
65 (merge (ignore_replies cfg) (OC.ignore_replies opt_cfg))
66 (merge (ignore_retweets cfg) (OC.ignore_retweets opt_cfg))
67 (merge (sendmail_path cfg) (OC.sendmail_path opt_cfg))
68 (merge' (from_address cfg) (OC.from_address opt_cfg))
69 (merge' (to_address cfg) (OC.to_address opt_cfg))
70 (merge (verbose cfg) (OC.verbose opt_cfg))
71 all_usernames
72 where
73 merge :: a -> Maybe a -> a
74 merge x Nothing = x
75 merge _ (Just y) = y
76
77 -- Used for the to/from address
78 merge' :: Maybe a -> Maybe a -> Maybe a
79 merge' Nothing Nothing = Nothing
80 merge' (Just x) Nothing = Just x
81 merge' Nothing (Just x) = Just x
82 merge' (Just _) (Just y) = Just y
83
84 -- If there are any optional usernames, use only those.
85 all_usernames = if (null (get_usernames (OC.usernames opt_cfg)))
86 then (usernames cfg)
87 else (OC.usernames opt_cfg)