]> gitweb.michael.orlitzky.com - dead/htsn.git/blob - src/Main.hs
ece44a8b9fb53a6e8d27a9e0eef828205185d255
[dead/htsn.git] / src / Main.hs
1 {-# LANGUAGE BangPatterns #-}
2 {-# LANGUAGE DoAndIfThenElse #-}
3
4 module Main
5 where
6
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 ( (<>) )
13 import Network (
14 connectTo,
15 PortID (PortNumber) )
16 import System.Console.CmdArgs ( def )
17 import System.Directory ( doesFileExist )
18 import System.Exit ( ExitCode(..), exitWith )
19 import System.FilePath ( (</>) )
20 import System.IO (
21 BufferMode (NoBuffering),
22 Handle,
23 hClose,
24 hGetChar,
25 hGetLine,
26 hPutStr,
27 hSetBuffering,
28 stderr,
29 stdout )
30 import System.IO.Error ( catchIOError )
31 import System.Timeout ( timeout )
32
33 import CommandLine ( get_args )
34 import Configuration ( Configuration(..), merge_optional )
35 import ExitCodes (
36 exit_no_feed_hosts,
37 exit_no_password,
38 exit_no_username,
39 exit_pidfile_exists )
40 import FeedHosts ( FeedHosts(..) )
41 import Logging ( init_logging )
42 import qualified OptionalConfiguration as OC (
43 OptionalConfiguration(..),
44 from_rc )
45 import Report (
46 report_debug,
47 report_info,
48 report_warning,
49 report_error )
50 import Terminal ( display_sent )
51 import Xml ( parse_xmlfid )
52 import Unix ( full_daemonize )
53
54
55 -- | Receive a single line of text from a Handle, and send it to the
56 -- debug log.
57 --
58 recv_line :: Handle -> IO String
59 recv_line h = do
60 line <- hGetLine h
61 report_debug (line ++ "\n")
62 return line
63
64
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
67 -- Configuration.
68 --
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.
71 --
72 save_document :: Configuration -> String -> IO ()
73 save_document cfg doc =
74 case either_path of
75 Left err -> report_error err
76 Right path -> do
77 already_exists <- doesFileExist path
78 when already_exists $ do
79 let msg = "File " ++ path ++ " already exists, overwriting."
80 report_warning msg
81 writeFile path doc
82 report_info $ "Wrote file: " ++ path ++ "."
83 where
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
88
89
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.
93 --
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
99 -- processing.
100 --
101 loop :: Configuration -> Handle -> [String] -> IO ()
102 loop !cfg !h !buffer = do
103 line <- recv_line h
104 let new_buffer = line : buffer
105
106 -- Use isPrefixOf to avoid line-ending issues. Hopefully they won't
107 -- send invalid junk (on the same line) after closing the root
108 -- element.
109 if "</message>" `isPrefixOf` line
110 then do
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.
117 else
118 -- Append line to the head of the buffer and loop.
119 loop cfg h new_buffer
120
121
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.
125 --
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.
131 --
132 -- Assuming the above will always hold, it is implemented as follows:
133 --
134 -- 1. Receive 10 chars
135 --
136 -- 2. Send username if we got the username prompt
137 --
138 -- 3. Receive 10 chars
139 --
140 -- 4. Send password if we got the password prompt
141 --
142 -- If TSN likes the password as well, they send the string "The
143 -- Sports Network" before finally beginning to stream the feed.
144 --
145 log_in :: Configuration -> Handle -> IO ()
146 log_in cfg h = do
147 prompt1 <- recv_prompt h
148
149 if prompt1 /= username_prompt then
150 report_error "Didn't receive username prompt."
151 else do
152 send_cred h (username cfg)
153 prompt2 <- recv_prompt h
154
155 if prompt2 /= password_prompt then
156 report_error "Didn't receive password prompt."
157 else do
158 send_cred h (password cfg)
159 _ <- recv_line h -- "The Sports Network"
160 report_info $ "Logged in as " ++ (username cfg) ++ "."
161 return ()
162 where
163 username_prompt = "Username: "
164 password_prompt = "Password: "
165
166 send_cred :: Handle -> String -> IO ()
167 send_cred h' s = do
168 -- The carriage return is super important!
169 let line = s ++ "\r\n"
170 hPutStr h' line
171 display_sent line -- Don't log the username/password!
172
173 recv_chars :: Int -> Handle -> IO String
174 recv_chars n h' = do
175 s <- sequence [ hGetChar h' | _ <- [1..n] ]
176 report_debug s
177 return s
178
179 recv_prompt :: Handle -> IO String
180 recv_prompt = recv_chars 10
181
182
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@).
188 --
189 -- Steps:
190 --
191 -- 1. Connect to the host on the XML port
192 --
193 -- 2. Log in
194 --
195 -- 3. Go into the eternal read/save loop.
196 --
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
201 return ()
202 where
203 five_seconds :: Int
204 five_seconds = 5000000
205
206 acquire_handle = connectTo host (PortNumber 4500)
207 release_handle = hClose
208 action h = do
209 -- No buffering anywhere.
210 hSetBuffering h NoBuffering
211
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.
215 --
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.
219 --
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.
226 --
227 login_worked <- timeout five_seconds $ log_in cfg h
228 case login_worked of
229 Nothing -> report_info $ "Login timed out (5 seconds). "
230 ++ "Waiting 5 seconds to reconnect."
231 Just _ -> loop cfg h []
232
233
234 -- | A wrapper around threadDelay which takes seconds instead of
235 -- microseconds as its argument.
236 --
237 thread_sleep :: Int -> IO ()
238 thread_sleep seconds = do
239 let microseconds = seconds * (10 ^ (6 :: Int))
240 threadDelay microseconds
241
242
243 -- | The entry point of the program.
244 --
245 main :: IO ()
246 main = do
247 rc_cfg <- OC.from_rc
248 cmd_cfg <- get_args
249
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
253
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
257 -- log the errors.
258 let cfg = (def :: Configuration) `merge_optional` opt_config
259 init_logging (log_file cfg) (log_level cfg) (syslog cfg)
260
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)
269
270 when (isNothing (OC.password opt_config)) $ do
271 report_error "No password supplied."
272 exitWith (ExitFailure exit_no_password)
273
274 when (isNothing (OC.username opt_config)) $ do
275 report_error "No username supplied."
276 exitWith (ExitFailure exit_no_username)
277
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)
286
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
291
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
296
297 -- If we were asked to daemonize, do that; otherwise just run the thing.
298 if (daemonize cfg)
299 then full_daemonize cfg run_program
300 else run_program
301
302 where
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.
306 --
307 -- Afterwards, we recurse (call ourself) again to loop more forevers.
308 --
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)