]> gitweb.michael.orlitzky.com - dead/halcyon.git/blob - src/Configuration.hs
095f5395f6bf7b6d0e4994ece60f453df1e5b614
[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 default_config,
9 merge_optional
10 )
11 where
12
13 import qualified OptionalConfiguration as OC
14 import Usernames
15
16 data Cfg =
17 Cfg { consumer_key :: String,
18 consumer_secret :: String,
19 access_token :: String,
20 access_secret :: String,
21 heartbeat :: Int,
22 ignore_replies :: Bool,
23 ignore_retweets :: Bool,
24 sendmail_path :: String,
25 from_address :: Maybe String,
26 to_address :: Maybe String,
27 verbose :: Bool,
28 usernames :: Usernames }
29 deriving (Show)
30
31
32 default_config :: Cfg
33 default_config =
34 Cfg { consumer_key = "",
35 consumer_secret = "",
36 access_token = "",
37 access_secret = "",
38 heartbeat = 600,
39 ignore_replies = False,
40 ignore_retweets = False,
41 sendmail_path = "/usr/sbin/sendmail",
42 from_address = Nothing,
43 to_address = Nothing,
44 verbose = False,
45 usernames = Usernames [] }
46
47 merge_optional :: Cfg -> OC.OptionalCfg -> Cfg
48 merge_optional cfg opt_cfg =
49 Cfg
50 (merge (consumer_key cfg) (OC.consumer_key opt_cfg))
51 (merge (consumer_secret cfg) (OC.consumer_secret opt_cfg))
52 (merge (access_token cfg) (OC.access_token opt_cfg))
53 (merge (access_secret cfg) (OC.access_secret opt_cfg))
54 (merge (heartbeat cfg) (OC.heartbeat opt_cfg))
55 (merge (ignore_replies cfg) (OC.ignore_replies opt_cfg))
56 (merge (ignore_retweets cfg) (OC.ignore_retweets opt_cfg))
57 (merge (sendmail_path cfg) (OC.sendmail_path opt_cfg))
58 (merge' (from_address cfg) (OC.from_address opt_cfg))
59 (merge' (to_address cfg) (OC.to_address opt_cfg))
60 (merge (verbose cfg) (OC.verbose opt_cfg))
61 all_usernames
62 where
63 merge :: a -> Maybe a -> a
64 merge x Nothing = x
65 merge _ (Just y) = y
66
67 -- Used for the to/from address
68 merge' :: Maybe a -> Maybe a -> Maybe a
69 merge' Nothing Nothing = Nothing
70 merge' (Just x) Nothing = Just x
71 merge' Nothing (Just x) = Just x
72 merge' (Just _) (Just y) = Just y
73
74 -- If there are any optional usernames, use only those.
75 all_usernames = if (null (get_usernames (OC.usernames opt_cfg)))
76 then (usernames cfg)
77 else (OC.usernames opt_cfg)