]> gitweb.michael.orlitzky.com - dead/htsn.git/blobdiff - src/OptionalConfiguration.hs
Move the Report, Logging, and Terminal modules into their own namespace.
[dead/htsn.git] / src / OptionalConfiguration.hs
index 0913864e0bb064cfd61884e25940a1e326eda771..8790ca075943fe94a0f54e9d291a5c5cfc81b8c7 100644 (file)
@@ -28,13 +28,14 @@ import Data.Data ( Data )
 import Data.Maybe ( fromMaybe )
 import Data.Monoid ( Monoid(..) )
 import Data.Typeable ( Typeable )
+import Paths_htsn ( getSysconfDir )
 import System.Directory ( getHomeDirectory )
 import System.FilePath ( (</>) )
 import System.IO.Error ( catchIOError )
 import System.Log ( Priority(..) )
 
-import Logging ( log_error )
-import TSN.FeedHosts ( FeedHosts(..) )
+import FeedHosts ( FeedHosts(..) )
+import Network.Services.TSN.Report ( report_error )
 
 
 -- Derive standalone instances of Data and Typeable for Priority. This
@@ -50,11 +51,15 @@ deriving instance Typeable Priority
 --
 data OptionalConfiguration =
   OptionalConfiguration {
+    daemonize        :: Maybe Bool,
     feed_hosts       :: FeedHosts,
     log_file         :: Maybe FilePath,
     log_level        :: Maybe Priority,
-    password         :: Maybe String,
     output_directory :: Maybe FilePath,
+    password         :: Maybe String,
+    pidfile          :: Maybe FilePath,
+    run_as_group     :: Maybe String,
+    run_as_user      :: Maybe String,
     syslog           :: Maybe Bool,
     username         :: Maybe String }
     deriving (Show, Data, Typeable)
@@ -81,6 +86,7 @@ merge_maybes (Just _) (Just y) = Just y
 instance Monoid OptionalConfiguration where
   -- | An empty OptionalConfiguration.
   mempty = OptionalConfiguration
+             Nothing
              (FeedHosts [])
              Nothing
              Nothing
@@ -88,16 +94,23 @@ instance Monoid OptionalConfiguration where
              Nothing
              Nothing
              Nothing
+             Nothing
+             Nothing
+             Nothing
 
 
   -- | Combine @cfg1@ and @cfg2@, giving precedence to @cfg2@.
   cfg1 `mappend` cfg2 =
     OptionalConfiguration
+      (merge_maybes (daemonize cfg1) (daemonize cfg2))
       all_feed_hosts
       (merge_maybes (log_file cfg1) (log_file cfg2))
       (merge_maybes (log_level cfg1) (log_level cfg2))
-      (merge_maybes (password cfg1) (password cfg2))
       (merge_maybes (output_directory cfg1) (output_directory cfg2))
+      (merge_maybes (password cfg1) (password cfg2))
+      (merge_maybes (pidfile cfg1) (pidfile cfg2))
+      (merge_maybes (run_as_group cfg1) (run_as_group cfg2))
+      (merge_maybes (run_as_user cfg1) (run_as_user cfg2))
       (merge_maybes (syslog cfg1) (syslog cfg2))
       (merge_maybes (username cfg1) (username cfg2))
     where
@@ -119,40 +132,48 @@ instance DCT.Configured Priority where
   convert _ = Nothing
 
 
--- | Obtain an OptionalConfiguration from the file ".htsnrc" in the
---   user's home directory.
+-- | Obtain an OptionalConfiguration from htsnrc in either the global
+--   configuration directory or the user's home directory. The one in
+--   $HOME is prefixed by a dot so that it is hidden.
 --
 --   We make an attempt at cross-platform compatibility; we will try
---   to find the correct directory even on Windows. But if the call
---   to getHomeDirectory fails for whatever reason, we fall back to
---   using the environment variable $HOME.
+--   to find the correct directory even on Windows. But if the calls
+--   to getHomeDirectory/getSysconfDir fail for whatever reason, we
+--   fall back to using the Unix-specific /etc and $HOME.
 --
 from_rc :: IO OptionalConfiguration
 from_rc = do
-  -- After some thought, the "global" /etc/htsnrc configuration file
-  -- was left out. Since each config file needs a password, and this
-  -- should be run by a dedicated user anyway, the global file does
-  -- not serve much purpose. It could also be a security risk (visible
-  -- password) if the admin screws up.
+  etc  <- catchIOError getSysconfDir (\e -> do
+                                        report_error (show e)
+                                        return "/etc")
   home <- catchIOError getHomeDirectory (\e -> do
-                                           log_error (show e)
+                                           report_error (show e)
                                            return "$(HOME)")
+  let global_config_path = etc </> "htsnrc"
   let user_config_path = home </> ".htsnrc"
-  cfg <- DC.load [ DC.Optional user_config_path ]
+  cfg <- DC.load [ DC.Optional global_config_path,
+                   DC.Optional user_config_path ]
+  cfg_daemonize <- DC.lookup cfg "daemonize"
+  cfg_feed_hosts <- DC.lookup cfg "feed_hosts"
   cfg_log_file <- DC.lookup cfg "log_file"
   cfg_log_level <- DC.lookup cfg "log_level"
-  cfg_password <- DC.lookup cfg "password"
   cfg_output_directory <- DC.lookup cfg "output_directory"
+  cfg_password <- DC.lookup cfg "password"
+  cfg_pidfile <- DC.lookup cfg "pidfile"
+  cfg_run_as_group <- DC.lookup cfg "run_as_group"
+  cfg_run_as_user <- DC.lookup cfg "run_as_user"
   cfg_syslog <- DC.lookup cfg "syslog"
   cfg_username <- DC.lookup cfg "username"
-  cfg_feed_hosts <- DC.lookup cfg "feed_hosts"
 
   return $ OptionalConfiguration
+             cfg_daemonize
              (fromMaybe (FeedHosts []) cfg_feed_hosts)
              cfg_log_file
              cfg_log_level
-             cfg_password
              cfg_output_directory
+             cfg_password
+             cfg_pidfile
+             cfg_run_as_group
+             cfg_run_as_user
              cfg_syslog
              cfg_username
-