]> gitweb.michael.orlitzky.com - dead/halcyon.git/blobdiff - src/Configuration.hs
Add configuration options for daemonize, pidfile, run_as_group and run_as_user.
[dead/halcyon.git] / src / Configuration.hs
index c2136431577a574cd2ace86a67e93dc7d3747e2d..4a38d0664cd4d8c4c22fd2b853cb3d1d38ed821b 100644 (file)
@@ -8,6 +8,7 @@ module Configuration (
   merge_optional )
 where
 
+import Data.Monoid ( Monoid(..) )
 import System.Console.CmdArgs.Default ( Default(..) )
 
 import qualified OptionalConfiguration as OC ( OptionalCfg(..) )
@@ -18,36 +19,44 @@ import Usernames ( Usernames(..) )
 --   can be set in a config file or on the command line.
 --
 data Cfg =
-  Cfg { consumer_key :: String,
-        consumer_secret :: String,
+  Cfg { access_secret :: String,
         access_token :: String,
-        access_secret :: String,
+        consumer_key :: String,
+        consumer_secret :: String,
+        daemonize :: Bool,
+        from_address :: Maybe String,
         heartbeat :: Int,
         ignore_replies :: Bool,
         ignore_retweets :: Bool,
+        pidfile :: FilePath,
+        run_as_group     :: Maybe String,
+        run_as_user      :: Maybe String,
         sendmail_path :: FilePath,
-        from_address :: Maybe String,
         to_address :: Maybe String,
-        verbose :: Bool,
-        usernames :: Usernames }
+        usernames :: Usernames,
+        verbose :: Bool }
     deriving (Show)
 
 
 instance Default Cfg where
   -- | A 'Cfg' with all of its fields set to their default values.
   --
-  def = Cfg { consumer_key = def,
-              consumer_secret = def,
+  def = Cfg { access_secret = def,
               access_token = def,
-              access_secret = def,
+              consumer_key = def,
+              consumer_secret = def,
+              daemonize = def,
+              from_address = def,
               heartbeat = 600,
               ignore_replies = def,
               ignore_retweets = def,
+              pidfile = "/run/halcyon/halcyon.pid",
+              run_as_group = def,
+              run_as_user = def,
               sendmail_path = "/usr/sbin/sendmail",
-              from_address = def,
               to_address = def,
-              verbose = def,
-              usernames = def }
+              usernames = def,
+              verbose = def }
 
 
 -- | Merge a 'Cfg' with an 'OptionalCfg'. This is more or less the
@@ -57,31 +66,30 @@ instance Default Cfg where
 merge_optional :: Cfg -> OC.OptionalCfg -> Cfg
 merge_optional cfg opt_cfg =
   Cfg
+    (merge (access_secret cfg) (OC.access_secret opt_cfg))
+    (merge (access_token cfg) (OC.access_token opt_cfg))
     (merge (consumer_key cfg) (OC.consumer_key opt_cfg))
     (merge (consumer_secret cfg) (OC.consumer_secret opt_cfg))
-    (merge (access_token cfg) (OC.access_token opt_cfg))
-    (merge (access_secret cfg) (OC.access_secret opt_cfg))
+    (merge (daemonize cfg) (OC.daemonize opt_cfg))
+    (merge_maybes (from_address cfg) (OC.from_address opt_cfg))
     (merge (heartbeat cfg) (OC.heartbeat opt_cfg))
     (merge (ignore_replies cfg) (OC.ignore_replies opt_cfg))
     (merge (ignore_retweets cfg) (OC.ignore_retweets opt_cfg))
+    (merge (pidfile cfg) (OC.pidfile opt_cfg))
+    (merge_maybes (run_as_group cfg) (OC.run_as_group opt_cfg))
+    (merge_maybes (run_as_user cfg) (OC.run_as_user opt_cfg))
     (merge (sendmail_path cfg) (OC.sendmail_path opt_cfg))
-    (merge' (from_address cfg) (OC.from_address opt_cfg))
-    (merge' (to_address cfg) (OC.to_address opt_cfg))
+    (merge_maybes (to_address cfg) (OC.to_address opt_cfg))
+    ((usernames cfg) `mappend` (OC.usernames opt_cfg))
     (merge (verbose cfg) (OC.verbose opt_cfg))
-    all_usernames
   where
     merge :: a -> Maybe a -> a
     merge x Nothing  = x
     merge _ (Just y) = y
 
-    -- Used for the to/from address
-    merge' :: Maybe a -> Maybe a -> Maybe a
-    merge' Nothing Nothing = Nothing
-    merge' (Just x) Nothing  = Just x
-    merge' Nothing (Just x)  = Just x
-    merge' (Just _) (Just y) = Just y
-
-    -- If there are any optional usernames, use only those.
-    all_usernames = if (null (get_usernames (OC.usernames opt_cfg)))
-                    then (usernames cfg)
-                    else (OC.usernames opt_cfg)
+    -- Used for the truly optional fields
+    merge_maybes :: Maybe a -> Maybe a -> Maybe a
+    merge_maybes Nothing Nothing = Nothing
+    merge_maybes (Just x) Nothing  = Just x
+    merge_maybes Nothing (Just x)  = Just x
+    merge_maybes (Just _) (Just y) = Just y