1 {-# LANGUAGE BangPatterns #-}
2 {-# LANGUAGE DoAndIfThenElse #-}
7 import Control.Concurrent ( threadDelay )
8 import Control.Exception.Base ( bracket )
9 import Control.Monad ( when )
10 import Data.List ( isPrefixOf )
11 import Data.Maybe ( isNothing )
12 import Data.Monoid ( (<>) )
16 import System.Console.CmdArgs ( def )
17 import System.Directory ( doesFileExist )
18 import System.Exit ( ExitCode(..), exitWith )
19 import System.FilePath ( (</>) )
21 BufferMode (NoBuffering),
30 import System.IO.Error ( catchIOError )
31 import System.Timeout ( timeout )
33 import CommandLine ( get_args )
34 import Configuration ( Configuration(..), merge_optional )
46 import qualified OptionalConfiguration as OC (
47 OptionalConfiguration(..),
55 import TSN.FeedHosts ( FeedHosts(..) )
56 import TSN.Xml ( parse_xmlfid )
57 import Unix ( full_daemonize )
59 -- | Display and log debug information. WARNING! This does not
60 -- automatically append a newline. The output is displayed/logged
61 -- as-is, for, you know, debug purposes.
62 report_debug :: String -> IO ()
68 -- | Display and log an error condition. This will prefix the error
69 -- with "ERROR: " when displaying (but not logging) it so that it
72 report_error :: String -> IO ()
74 display_error $ "ERROR: " ++ s
78 -- | Display and log an informational (status) message.
80 report_info :: String -> IO ()
86 -- | Display and log a warning. This will prefix the warning with
87 -- "WARNING: " when displaying (but not logging) it so that it
90 report_warning :: String -> IO ()
92 display_warning $ "WARNING: " ++ s
96 -- | Receive a single line of text from a Handle, and send it to the
99 recv_line :: Handle -> IO String
102 report_debug (line ++ "\n")
106 -- | Takes a Configuration, and an XML document (as a String). The XML
107 -- document is written to the output directory, as specified by the
110 -- This can fail, but we don't purposefully throw any exceptions. If
111 -- something goes wrong, we would rather log it and keep going.
113 save_document :: Configuration -> String -> IO ()
114 save_document cfg doc =
116 Left err -> report_error err
118 already_exists <- doesFileExist path
119 when already_exists $ do
120 let msg = "File " ++ path ++ " already exists, overwriting."
123 report_info $ "Wrote file: " ++ path ++ "."
125 -- All the fmaps are because we're working inside a Maybe.
126 xmlfid = fmap show (parse_xmlfid doc)
127 filename = fmap (++ ".xml") xmlfid
128 either_path = fmap ((output_directory cfg) </>) filename
131 -- | Loop forever, writing the buffer to file whenever a </message>
132 -- tag is seen. This is the low-level "loop forever" function that
133 -- we stay in as long as we are connected to one feed.
135 -- The documentation at
136 -- <http://www.sportsnetworkdata.com/feeds/xml-levels.asp> states
137 -- that \<message\> will always be the root element of the XML
138 -- documents, and \</message\> will be the final line transmitted
139 -- for a given document. We therefore rely on this to simplify
142 loop :: Configuration -> Handle -> [String] -> IO ()
143 loop !cfg !h !buffer = do
145 let new_buffer = line : buffer
147 -- Use isPrefixOf to avoid line-ending issues. Hopefully they won't
148 -- send invalid junk (on the same line) after closing the root
150 if "</message>" `isPrefixOf` line
152 -- The buffer is in reverse (newest first) order, though, so we
153 -- have to reverse it first. We then concatenate all of its lines
154 -- into one big string.
155 let document = concat $ reverse new_buffer
156 save_document cfg document
157 loop cfg h [] -- Empty the buffer before looping again.
159 -- Append line to the head of the buffer and loop.
160 loop cfg h new_buffer
163 -- | Once we're connected to a feed, we need to log in. There's no
164 -- protocol for this (the docs don't mention one), but we have
165 -- (apparently) successfully guessed it.
167 -- The first thing TSN sends once we've connected is the string
168 -- "Username: ", containing 10 ASCII characters. We then send a
169 -- username, followed by a newline. If TSN likes the username, the
170 -- second they'll send is the string "Password: ", also containing
171 -- 10 ASCII characters, to which we reply in kind.
173 -- Assuming the above will always hold, it is implemented as follows:
175 -- 1. Receive 10 chars
177 -- 2. Send username if we got the username prompt
179 -- 3. Receive 10 chars
181 -- 4. Send password if we got the password prompt
183 -- If TSN likes the password as well, they send the string "The
184 -- Sports Network" before finally beginning to stream the feed.
186 log_in :: Configuration -> Handle -> IO ()
188 prompt1 <- recv_prompt h
190 if prompt1 /= username_prompt then
191 report_error "Didn't receive username prompt."
193 send_cred h (username cfg)
194 prompt2 <- recv_prompt h
196 if prompt2 /= password_prompt then
197 report_error "Didn't receive password prompt."
199 send_cred h (password cfg)
200 _ <- recv_line h -- "The Sports Network"
203 username_prompt = "Username: "
204 password_prompt = "Password: "
206 send_cred :: Handle -> String -> IO ()
208 -- The carriage return is super important!
209 let line = s ++ "\r\n"
211 display_sent line -- Don't log the username/password!
213 recv_chars :: Int -> Handle -> IO String
215 s <- sequence [ hGetChar h' | _ <- [1..n] ]
219 recv_prompt :: Handle -> IO String
220 recv_prompt = recv_chars 10
223 -- | Connect to @host@ and attempt to parse the feed. As long as we
224 -- stay connected and nothing bad happens, the program will remain in
225 -- this function. If anything goes wrong, then the current invocation
226 -- of connect_and_parse will return, and get called again later
227 -- (probably with a different @host@).
231 -- 1. Connect to the host on the XML port
235 -- 3. Go into the eternal read/save loop.
237 connect_and_parse :: Configuration -> String -> IO ()
238 connect_and_parse cfg host = do
239 report_info $ "Connecting to " ++ host ++ "."
240 bracket acquire_handle release_handle action
244 five_seconds = 5000000
246 acquire_handle = connectTo host (PortNumber 4500)
247 release_handle = hClose
249 -- No buffering anywhere.
250 hSetBuffering h NoBuffering
252 -- The feed is often unresponsive after we send out username. It
253 -- happens in a telnet session, too (albeit less frequently?),
254 -- so there might be a bug on their end.
256 -- If we dump the packets with tcpdump, it looks like their
257 -- software is getting confused: they send us some XML in
258 -- the middle of the log-in procedure.
260 -- On the other hand, the documentation at
261 -- <http://www.sportsnetworkdata.com/feeds/xml-levels.asp>
262 -- states that you can only make one connection per username to
263 -- a given host. So maybe they're simply rejecting the username
264 -- in an unfriendly fashion. In any case, the easiest fix is to
265 -- disconnect and try again.
267 login_worked <- timeout five_seconds $ log_in cfg h
269 Nothing -> report_info $ "Login timed out (5 seconds). "
270 ++ "Waiting 5 seconds to reconnect."
271 Just _ -> loop cfg h []
274 -- | A wrapper around threadDelay which takes seconds instead of
275 -- microseconds as its argument.
277 thread_sleep :: Int -> IO ()
278 thread_sleep seconds = do
279 let microseconds = seconds * (10 ^ (6 :: Int))
280 threadDelay microseconds
283 -- | The entry point of the program.
290 -- Merge the config file options with the command-line ones,
291 -- prefering the command-line ones.
292 let opt_config = rc_cfg <> cmd_cfg
294 -- Update a default config with any options that have been set in
295 -- either the config file or on the command-line. We initialize
296 -- logging before the missing parameter checks below so that we can
298 let cfg = (def :: Configuration) `merge_optional` opt_config
299 init_logging (log_file cfg) (log_level cfg) (syslog cfg)
301 -- Check the optional config for missing required options. This is
302 -- necessary because if the user specifies an empty list of
303 -- hostnames in e.g. the config file, we want to bail rather than
304 -- fall back on the default list (which was merged from a default
305 -- Configuration above).
306 when (null $ get_feed_hosts (OC.feed_hosts opt_config)) $ do
307 report_error "No feed hosts supplied."
308 exitWith (ExitFailure exit_no_feed_hosts)
310 when (isNothing (OC.password opt_config)) $ do
311 report_error "No password supplied."
312 exitWith (ExitFailure exit_no_password)
314 when (isNothing (OC.username opt_config)) $ do
315 report_error "No username supplied."
316 exitWith (ExitFailure exit_no_username)
318 when (daemonize cfg) $ do
319 -- Old PID files can be left around after an unclean shutdown. We
320 -- only care if we're running as a daemon.
321 pidfile_exists <- doesFileExist (pidfile cfg)
322 when pidfile_exists $ do
323 report_error $ "PID file " ++ (pidfile cfg) ++ " already exists. "
324 ++ "Refusing to start."
325 exitWith (ExitFailure exit_pidfile_exists)
327 -- This may be superstition (and I believe stderr is unbuffered),
328 -- but it can't hurt.
329 hSetBuffering stderr NoBuffering
330 hSetBuffering stdout NoBuffering
332 -- The rest of the program is kicked off by the following line which
333 -- begins connecting to our feed hosts, starting with the first one,
334 -- and proceeds in a round-robin fashion.
335 let run_program = round_robin cfg 0
337 -- If we were asked to daemonize, do that; otherwise just run the thing.
339 then full_daemonize cfg run_program
343 -- | This is the top-level "loop forever" function. If an
344 -- exception is thrown, it will propagate up to this point, where
345 -- it will be logged and ignored in style.
347 -- Afterwards, we recurse (call ourself) again to loop more forevers.
349 round_robin :: Configuration -> Int -> IO ()
350 round_robin cfg feed_host_idx = do
351 let hosts = get_feed_hosts $ feed_hosts cfg
352 let host = hosts !! feed_host_idx
353 catchIOError (connect_and_parse cfg host) (report_error . show)
354 thread_sleep 5 -- Wait 5s before attempting to reconnect.
355 round_robin cfg $ (feed_host_idx + 1) `mod` (length hosts)