]> gitweb.michael.orlitzky.com - dead/htsn.git/blob - src/Main.hs
Add more code documentation.
[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 Logging (
41 init_logging,
42 log_debug,
43 log_error,
44 log_info,
45 log_warning )
46 import qualified OptionalConfiguration as OC (
47 OptionalConfiguration(..),
48 from_rc )
49 import Terminal (
50 display_debug,
51 display_error,
52 display_info,
53 display_sent,
54 display_warning )
55 import TSN.FeedHosts ( FeedHosts(..) )
56 import TSN.Xml ( parse_xmlfid )
57 import Unix ( full_daemonize )
58
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 ()
63 report_debug s = do
64 display_debug s
65 log_debug s
66
67
68 -- | Display and log an error condition. This will prefix the error
69 -- with "ERROR: " when displaying (but not logging) it so that it
70 -- stands out.
71 --
72 report_error :: String -> IO ()
73 report_error s = do
74 display_error $ "ERROR: " ++ s
75 log_error s
76
77
78 -- | Display and log an informational (status) message.
79 --
80 report_info :: String -> IO ()
81 report_info s = do
82 display_info s
83 log_info s
84
85
86 -- | Display and log a warning. This will prefix the warning with
87 -- "WARNING: " when displaying (but not logging) it so that it
88 -- stands out.
89 --
90 report_warning :: String -> IO ()
91 report_warning s = do
92 display_warning $ "WARNING: " ++ s
93 log_warning s
94
95
96 -- | Receive a single line of text from a Handle, and send it to the
97 -- debug log.
98 --
99 recv_line :: Handle -> IO String
100 recv_line h = do
101 line <- hGetLine h
102 report_debug (line ++ "\n")
103 return line
104
105
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
108 -- Configuration.
109 --
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.
112 --
113 save_document :: Configuration -> String -> IO ()
114 save_document cfg doc =
115 case either_path of
116 Left err -> report_error err
117 Right path -> do
118 already_exists <- doesFileExist path
119 when already_exists $ do
120 let msg = "File " ++ path ++ " already exists, overwriting."
121 report_warning msg
122 writeFile path doc
123 report_info $ "Wrote file: " ++ path ++ "."
124 where
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
129
130
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.
134 --
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
140 -- processing.
141 --
142 loop :: Configuration -> Handle -> [String] -> IO ()
143 loop !cfg !h !buffer = do
144 line <- recv_line h
145 let new_buffer = line : buffer
146
147 -- Use isPrefixOf to avoid line-ending issues. Hopefully they won't
148 -- send invalid junk (on the same line) after closing the root
149 -- element.
150 if "</message>" `isPrefixOf` line
151 then do
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.
158 else
159 -- Append line to the head of the buffer and loop.
160 loop cfg h new_buffer
161
162
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.
166 --
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.
172 --
173 -- Assuming the above will always hold, it is implemented as follows:
174 --
175 -- 1. Receive 10 chars
176 --
177 -- 2. Send username if we got the username prompt
178 --
179 -- 3. Receive 10 chars
180 --
181 -- 4. Send password if we got the password prompt
182 --
183 -- If TSN likes the password as well, they send the string "The
184 -- Sports Network" before finally beginning to stream the feed.
185 --
186 log_in :: Configuration -> Handle -> IO ()
187 log_in cfg h = do
188 prompt1 <- recv_prompt h
189
190 if prompt1 /= username_prompt then
191 report_error "Didn't receive username prompt."
192 else do
193 send_cred h (username cfg)
194 prompt2 <- recv_prompt h
195
196 if prompt2 /= password_prompt then
197 report_error "Didn't receive password prompt."
198 else do
199 send_cred h (password cfg)
200 _ <- recv_line h -- "The Sports Network"
201 return ()
202 where
203 username_prompt = "Username: "
204 password_prompt = "Password: "
205
206 send_cred :: Handle -> String -> IO ()
207 send_cred h' s = do
208 -- The carriage return is super important!
209 let line = s ++ "\r\n"
210 hPutStr h' line
211 display_sent line -- Don't log the username/password!
212
213 recv_chars :: Int -> Handle -> IO String
214 recv_chars n h' = do
215 s <- sequence [ hGetChar h' | _ <- [1..n] ]
216 report_debug s
217 return s
218
219 recv_prompt :: Handle -> IO String
220 recv_prompt = recv_chars 10
221
222
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@).
228 --
229 -- Steps:
230 --
231 -- 1. Connect to the host on the XML port
232 --
233 -- 2. Log in
234 --
235 -- 3. Go into the eternal read/save loop.
236 --
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
241 return ()
242 where
243 five_seconds :: Int
244 five_seconds = 5000000
245
246 acquire_handle = connectTo host (PortNumber 4500)
247 release_handle = hClose
248 action h = do
249 -- No buffering anywhere.
250 hSetBuffering h NoBuffering
251
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.
255 --
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.
259 --
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.
266 --
267 login_worked <- timeout five_seconds $ log_in cfg h
268 case login_worked of
269 Nothing -> report_info $ "Login timed out (5 seconds). "
270 ++ "Waiting 5 seconds to reconnect."
271 Just _ -> loop cfg h []
272
273
274 -- | A wrapper around threadDelay which takes seconds instead of
275 -- microseconds as its argument.
276 --
277 thread_sleep :: Int -> IO ()
278 thread_sleep seconds = do
279 let microseconds = seconds * (10 ^ (6 :: Int))
280 threadDelay microseconds
281
282
283 -- | The entry point of the program.
284 --
285 main :: IO ()
286 main = do
287 rc_cfg <- OC.from_rc
288 cmd_cfg <- get_args
289
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
293
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
297 -- log the errors.
298 let cfg = (def :: Configuration) `merge_optional` opt_config
299 init_logging (log_file cfg) (log_level cfg) (syslog cfg)
300
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)
309
310 when (isNothing (OC.password opt_config)) $ do
311 report_error "No password supplied."
312 exitWith (ExitFailure exit_no_password)
313
314 when (isNothing (OC.username opt_config)) $ do
315 report_error "No username supplied."
316 exitWith (ExitFailure exit_no_username)
317
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)
326
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
331
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
336
337 -- If we were asked to daemonize, do that; otherwise just run the thing.
338 if (daemonize cfg)
339 then full_daemonize cfg run_program
340 else run_program
341
342 where
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.
346 --
347 -- Afterwards, we recurse (call ourself) again to loop more forevers.
348 --
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)