my_summary = program_name ++ "-" ++ (showVersion version)
+-- | A description of the "daemonize" option.
daemonize_help :: String
daemonize_help =
"Run as a daemon, in the background."
log_file_help =
"Log to the given file."
+-- | A description of the "log_level" option.
log_level_help :: String
log_level_help =
"How verbose should the logs be? One of INFO, WARNING, ERROR."
-- | A description of the "output_directory" option.
output_directory_help :: String
output_directory_help =
- "Directory in which to output the XML files; must be writable"
+ "Directory in which to output the XML files; must be writable."
-- | A description of the "password" option.
password_help :: String
password_help =
- "Password to use when connecting to the feed"
+ "Password to use when connecting to the feed."
+-- | A description of the "pidfile" option.
pidfile_help :: String
pidfile_help =
"Location to create PID file (daemon only)."
+-- | A description of the "run_as_group" option.
run_as_group_help :: String
run_as_group_help =
"System group to run as (daemon only)."
+-- | A description of the "run_as_user" option.
run_as_user_help :: String
run_as_user_help =
"System user to run under (daemon only)."
-- | A description of the "username" option.
username_help :: String
username_help =
- "Username to use when connecting to the feed"
+ "Username to use when connecting to the feed."
-- | A data structure representing the possible command-line
-- options. The CmdArgs library is doing heavy magic beneath the
merge_maybes )
import TSN.FeedHosts (FeedHosts(..))
+-- | The main configuration data type. This will be passed to most of
+-- the important functions once it has been created.
data Configuration =
Configuration {
daemonize :: Bool,
--- |All exit codes that the program can return (excepting
--- ExitSuccess). There's only one, since the program will try and fail
--- forever upon errors.
+-- | All exit codes that the program can return (excepting
+-- ExitSuccess).
module ExitCodes (
exit_no_feed_hosts,
exit_no_password,
warningM )
+-- | Log a message at the DEBUG level.
log_debug :: String -> IO ()
log_debug = debugM rootLoggerName
+-- | Log a message at the ERROR level.
log_error :: String -> IO ()
log_error = errorM rootLoggerName
+-- | Log a message at the INFO level.
log_info :: String -> IO ()
log_info = infoM rootLoggerName
+-- | Log a message at the WARNING level.
log_warning :: String -> IO ()
log_warning = warningM rootLoggerName
--- | Why don't we take a Configuration as an argument? Because it
+-- | Set up the logging. All logs are handled by the global "root"
+-- logger provided by HSLogger. We remove all of its handlers so
+-- that it does nothing; then we conditionally add back two handlers
+-- -- one for syslog, and one for a normal file -- dependent upon
+-- the 'syslog' and 'log_file' configuration items.
+--
+-- Why don't we take a Configuration as an argument? Because it
-- would create circular imports!
init_logging :: Maybe FilePath -> Priority -> Bool -> IO ()
init_logging log_file log_level syslog = do
-- | Display and log an informational (status) message.
+--
report_info :: String -> IO ()
report_info s = do
display_info s
loop cfg h new_buffer
+-- | Once we're connected to a feed, we need to log in. There's no
+-- protocol for this (the docs don't mention one), but we have
+-- (apparently) successfully guessed it.
+--
+-- The first thing TSN sends once we've connected is the string
+-- "Username: ", containing 10 ASCII characters. We then send a
+-- username, followed by a newline. If TSN likes the username, the
+-- second they'll send is the string "Password: ", also containing
+-- 10 ASCII characters, to which we reply in kind.
+--
+-- Assuming the above will always hold, it is implemented as follows:
+--
+-- 1. Receive 10 chars
+--
+-- 2. Send username if we got the username prompt
+--
+-- 3. Receive 10 chars
+--
+-- 4. Send password if we got the password prompt
+--
+-- If TSN likes the password as well, they send the string "The
+-- Sports Network" before finally beginning to stream the feed.
+--
log_in :: Configuration -> Handle -> IO ()
log_in cfg h = do
prompt1 <- recv_prompt h
if prompt1 /= username_prompt then
report_error "Didn't receive username prompt."
else do
- send_line h (username cfg)
+ send_cred h (username cfg)
prompt2 <- recv_prompt h
if prompt2 /= password_prompt then
report_error "Didn't receive password prompt."
else do
- send_line h (password cfg)
+ send_cred h (password cfg)
_ <- recv_line h -- "The Sports Network"
return ()
where
username_prompt = "Username: "
password_prompt = "Password: "
- send_line :: Handle -> String -> IO ()
- send_line h' s = do
+ send_cred :: Handle -> String -> IO ()
+ send_cred h' s = do
+ -- The carriage return is super important!
let line = s ++ "\r\n"
hPutStr h' line
- -- Don't log the username/password!
- display_sent line
+ display_sent line -- Don't log the username/password!
recv_chars :: Int -> Handle -> IO String
recv_chars n h' = do
recv_prompt = recv_chars 10
-connect_and_loop :: Configuration -> String -> IO ()
-connect_and_loop cfg host = do
+-- | Connect to @host@ and attempt to parse the feed. As long as we
+-- stay connected and nothing bad happens, the program will remain in
+-- this function. If anything goes wrong, then the current invocation
+-- of connect_and_parse will return, and get called again later
+-- (probably with a different @host@).
+--
+-- Steps:
+--
+-- 1. Connect to the host on the XML port
+--
+-- 2. Log in
+--
+-- 3. Go into the eternal read/save loop.
+--
+connect_and_parse :: Configuration -> String -> IO ()
+connect_and_parse cfg host = do
report_info $ "Connecting to " ++ host ++ "."
bracket acquire_handle release_handle action
return ()
--
login_worked <- timeout five_seconds $ log_in cfg h
case login_worked of
- Nothing -> report_info "Login timed out (5s)."
+ Nothing -> report_info $ "Login timed out (5 seconds). "
+ ++ "Waiting 5 seconds to reconnect."
Just _ -> loop cfg h []
-- | The entry point of the program.
+--
main :: IO ()
main = do
rc_cfg <- OC.from_rc
exitWith (ExitFailure exit_no_username)
when (daemonize cfg) $ do
+ -- Old PID files can be left around after an unclean shutdown. We
+ -- only care if we're running as a daemon.
pidfile_exists <- doesFileExist (pidfile cfg)
when pidfile_exists $ do
report_error $ "PID file " ++ (pidfile cfg) ++ " already exists. "
round_robin cfg feed_host_idx = do
let hosts = get_feed_hosts $ feed_hosts cfg
let host = hosts !! feed_host_idx
- catchIOError (connect_and_loop cfg host) (report_error . show)
+ catchIOError (connect_and_parse cfg host) (report_error . show)
thread_sleep 5 -- Wait 5s before attempting to reconnect.
round_robin cfg $ (feed_host_idx + 1) `mod` (length hosts)
import Data.Typeable (Typeable)
+-- | A (wrapper around a) list of hostnames that supply the XML feed.
+--
newtype FeedHosts =
FeedHosts { get_feed_hosts :: [String] }
deriving (Data, Show, Typeable)
+
instance Default FeedHosts where
-- | The default list of feed hosts. These were found by checking
-- PTR records in the neighborhood of the IP address in use. There
with_sgr h [SetColor Foreground Vivid color]
+-- | Write the given String to a handle in color. The funnyCaps are
+-- for synergy with putstrLn and friends.
+--
hPutStrColor :: Handle -> Color -> String -> IO ()
hPutStrColor h c = with_color h c . hPutStr h
+
+-- | Write the given line to a handle in color. The funnyCaps are for
+-- synergy with putstrLn and friends.
+--
hPutStrColorLn :: Handle -> Color -> String -> IO ()
hPutStrColorLn h c s = hPutStrColor h c (s ++ "\n")
--- | Don't automatically append a newline.
+
+-- | Display text sent to the feed on the console. Don't automatically
+-- append a newline.
+--
display_sent :: String -> IO ()
display_sent = hPutStrColor stdout Green
+
+-- | Display debug text on the console. Don't automatically append a
+-- newline in case the raw text is needed for, uh, debugging.
+--
display_debug :: String -> IO ()
display_debug = putStr
+
+-- | Display an informational message on the console.
+--
display_info :: String -> IO ()
display_info = hPutStrColorLn stdout Cyan
+
+-- | Display a warning on the console. Uses stderr instead of stdout.
+--
display_warning :: String -> IO ()
display_warning = hPutStrColorLn stderr Yellow
+
+-- | Display an error on the console. Uses stderr instead of stdout.
+--
display_error :: String -> IO ()
display_error = hPutStrColorLn stderr Red
+-- | Non-portable code for daemonizing on unix.
+--
module Unix
where
run_as_user ))
import Logging ( log_info )
+-- | Retrieve the uid associated with the given system user name. We
+-- take a Maybe String as an argument so the user name can be passed
+-- in directly from the config.
+--
get_user_id :: Maybe String -> IO UserID
get_user_id Nothing = getRealUserID
get_user_id (Just s) = fmap userID (getUserEntryForName s)
+
+-- | Retrieve the gid associated with the given system group name. We
+-- take a Maybe String as an argument so the group name can be
+-- passed in directly from the config.
+--
get_group_id :: Maybe String -> IO GroupID
get_group_id Nothing = getRealGroupID
get_group_id (Just s) = fmap groupID (getGroupEntryForName s)
+
+-- | This function will be called in response to a SIGTERM; i.e. when
+-- someone tries to kill our process. We simply delete the PID file
+-- and signal our parent thread to quit (successfully).
graceful_shutdown :: Configuration -> ThreadId -> IO ()
graceful_shutdown cfg main_thread_id = do
log_info "SIGTERM received, removing PID file and shutting down."
removeLink (pidfile cfg)
throwTo main_thread_id ExitSuccess
+
+-- | Write a PID file, install a SIGTERM handler, drop privileges, and
+-- finally do the daemonization dance.
+--
full_daemonize :: Configuration -> IO () -> IO ()
-full_daemonize cfg program = do
+full_daemonize cfg program =
-- This is the 'daemonize' from System.Posix.Daemonize.
daemonize program'
where