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 )
40 import FeedHosts ( FeedHosts(..) )
41 import Logging ( init_logging )
42 import qualified OptionalConfiguration as OC (
43 OptionalConfiguration(..),
50 import Terminal ( display_sent )
51 import Xml ( parse_xmlfid )
52 import Unix ( full_daemonize )
55 -- | Receive a single line of text from a Handle, and send it to the
58 recv_line :: Handle -> IO String
61 report_debug (line ++ "\n")
65 -- | Takes a Configuration, and an XML document (as a String). The XML
66 -- document is written to the output directory, as specified by the
69 -- This can fail, but we don't purposefully throw any exceptions. If
70 -- something goes wrong, we would rather log it and keep going.
72 save_document :: Configuration -> String -> IO ()
73 save_document cfg doc =
75 Left err -> report_error err
77 already_exists <- doesFileExist path
78 when already_exists $ do
79 let msg = "File " ++ path ++ " already exists, overwriting."
82 report_info $ "Wrote file: " ++ path ++ "."
84 -- All the fmaps are because we're working inside a Maybe.
85 xmlfid = fmap show (parse_xmlfid doc)
86 filename = fmap (++ ".xml") xmlfid
87 either_path = fmap ((output_directory cfg) </>) filename
90 -- | Loop forever, writing the buffer to file whenever a </message>
91 -- tag is seen. This is the low-level "loop forever" function that
92 -- we stay in as long as we are connected to one feed.
94 -- The documentation at
95 -- <http://www.sportsnetworkdata.com/feeds/xml-levels.asp> states
96 -- that \<message\> will always be the root element of the XML
97 -- documents, and \</message\> will be the final line transmitted
98 -- for a given document. We therefore rely on this to simplify
101 loop :: Configuration -> Handle -> [String] -> IO ()
102 loop !cfg !h !buffer = do
104 let new_buffer = line : buffer
106 -- Use isPrefixOf to avoid line-ending issues. Hopefully they won't
107 -- send invalid junk (on the same line) after closing the root
109 if "</message>" `isPrefixOf` line
111 -- The buffer is in reverse (newest first) order, though, so we
112 -- have to reverse it first. We then concatenate all of its lines
113 -- into one big string.
114 let document = concat $ reverse new_buffer
115 save_document cfg document
116 loop cfg h [] -- Empty the buffer before looping again.
118 -- Append line to the head of the buffer and loop.
119 loop cfg h new_buffer
122 -- | Once we're connected to a feed, we need to log in. There's no
123 -- protocol for this (the docs don't mention one), but we have
124 -- (apparently) successfully guessed it.
126 -- The first thing TSN sends once we've connected is the string
127 -- "Username: ", containing 10 ASCII characters. We then send a
128 -- username, followed by a newline. If TSN likes the username, the
129 -- second they'll send is the string "Password: ", also containing
130 -- 10 ASCII characters, to which we reply in kind.
132 -- Assuming the above will always hold, it is implemented as follows:
134 -- 1. Receive 10 chars
136 -- 2. Send username if we got the username prompt
138 -- 3. Receive 10 chars
140 -- 4. Send password if we got the password prompt
142 -- If TSN likes the password as well, they send the string "The
143 -- Sports Network" before finally beginning to stream the feed.
145 log_in :: Configuration -> Handle -> IO ()
147 prompt1 <- recv_prompt h
149 if prompt1 /= username_prompt then
150 report_error "Didn't receive username prompt."
152 send_cred h (username cfg)
153 prompt2 <- recv_prompt h
155 if prompt2 /= password_prompt then
156 report_error "Didn't receive password prompt."
158 send_cred h (password cfg)
159 _ <- recv_line h -- "The Sports Network"
160 report_info $ "Logged in as " ++ (username cfg) ++ "."
163 username_prompt = "Username: "
164 password_prompt = "Password: "
166 send_cred :: Handle -> String -> IO ()
168 -- The carriage return is super important!
169 let line = s ++ "\r\n"
171 display_sent line -- Don't log the username/password!
173 recv_chars :: Int -> Handle -> IO String
175 s <- sequence [ hGetChar h' | _ <- [1..n] ]
179 recv_prompt :: Handle -> IO String
180 recv_prompt = recv_chars 10
183 -- | Connect to @host@ and attempt to parse the feed. As long as we
184 -- stay connected and nothing bad happens, the program will remain in
185 -- this function. If anything goes wrong, then the current invocation
186 -- of connect_and_parse will return, and get called again later
187 -- (probably with a different @host@).
191 -- 1. Connect to the host on the XML port
195 -- 3. Go into the eternal read/save loop.
197 connect_and_parse :: Configuration -> String -> IO ()
198 connect_and_parse cfg host = do
199 report_info $ "Connecting to " ++ host ++ "."
200 bracket acquire_handle release_handle action
204 five_seconds = 5000000
206 acquire_handle = connectTo host (PortNumber 4500)
207 release_handle = hClose
209 -- No buffering anywhere.
210 hSetBuffering h NoBuffering
212 -- The feed is often unresponsive after we send out username. It
213 -- happens in a telnet session, too (albeit less frequently?),
214 -- so there might be a bug on their end.
216 -- If we dump the packets with tcpdump, it looks like their
217 -- software is getting confused: they send us some XML in
218 -- the middle of the log-in procedure.
220 -- On the other hand, the documentation at
221 -- <http://www.sportsnetworkdata.com/feeds/xml-levels.asp>
222 -- states that you can only make one connection per username to
223 -- a given host. So maybe they're simply rejecting the username
224 -- in an unfriendly fashion. In any case, the easiest fix is to
225 -- disconnect and try again.
227 login_worked <- timeout five_seconds $ log_in cfg h
229 Nothing -> report_info $ "Login timed out (5 seconds). "
230 ++ "Waiting 5 seconds to reconnect."
231 Just _ -> loop cfg h []
234 -- | A wrapper around threadDelay which takes seconds instead of
235 -- microseconds as its argument.
237 thread_sleep :: Int -> IO ()
238 thread_sleep seconds = do
239 let microseconds = seconds * (10 ^ (6 :: Int))
240 threadDelay microseconds
243 -- | The entry point of the program.
250 -- Merge the config file options with the command-line ones,
251 -- prefering the command-line ones.
252 let opt_config = rc_cfg <> cmd_cfg
254 -- Update a default config with any options that have been set in
255 -- either the config file or on the command-line. We initialize
256 -- logging before the missing parameter checks below so that we can
258 let cfg = (def :: Configuration) `merge_optional` opt_config
259 init_logging (log_file cfg) (log_level cfg) (syslog cfg)
261 -- Check the optional config for missing required options. This is
262 -- necessary because if the user specifies an empty list of
263 -- hostnames in e.g. the config file, we want to bail rather than
264 -- fall back on the default list (which was merged from a default
265 -- Configuration above).
266 when (null $ get_feed_hosts (OC.feed_hosts opt_config)) $ do
267 report_error "No feed hosts supplied."
268 exitWith (ExitFailure exit_no_feed_hosts)
270 when (isNothing (OC.password opt_config)) $ do
271 report_error "No password supplied."
272 exitWith (ExitFailure exit_no_password)
274 when (isNothing (OC.username opt_config)) $ do
275 report_error "No username supplied."
276 exitWith (ExitFailure exit_no_username)
278 when (daemonize cfg) $ do
279 -- Old PID files can be left around after an unclean shutdown. We
280 -- only care if we're running as a daemon.
281 pidfile_exists <- doesFileExist (pidfile cfg)
282 when pidfile_exists $ do
283 report_error $ "PID file " ++ (pidfile cfg) ++ " already exists. "
284 ++ "Refusing to start."
285 exitWith (ExitFailure exit_pidfile_exists)
287 -- This may be superstition (and I believe stderr is unbuffered),
288 -- but it can't hurt.
289 hSetBuffering stderr NoBuffering
290 hSetBuffering stdout NoBuffering
292 -- The rest of the program is kicked off by the following line which
293 -- begins connecting to our feed hosts, starting with the first one,
294 -- and proceeds in a round-robin fashion.
295 let run_program = round_robin cfg 0
297 -- If we were asked to daemonize, do that; otherwise just run the thing.
299 then full_daemonize cfg run_program
303 -- | This is the top-level "loop forever" function. If an
304 -- exception is thrown, it will propagate up to this point, where
305 -- it will be logged and ignored in style.
307 -- Afterwards, we recurse (call ourself) again to loop more forevers.
309 round_robin :: Configuration -> Int -> IO ()
310 round_robin cfg feed_host_idx = do
311 let hosts = get_feed_hosts $ feed_hosts cfg
312 let host = hosts !! feed_host_idx
313 catchIOError (connect_and_parse cfg host) (report_error . show)
314 thread_sleep 5 -- Wait 5s before attempting to reconnect.
315 round_robin cfg $ (feed_host_idx + 1) `mod` (length hosts)