+-- | This is the main recursive loop. It takes a the configuration, a
+-- username, a latest_status_id, and optionally a 'Message' as
+-- arguments. The latest_status_id is the last status (that we know
+-- of) to be posted to username's Twitter account. If we find any
+-- newer statuses when we check, they are printed and optionally
+-- emailed (if a 'Message' was supplied). Then, the process repeats.
+recurse :: Cfg -> String -> Integer -> (Maybe Message) -> IO ()
+recurse cfg username latest_status_id maybe_message = do
+ thread_sleep (heartbeat cfg)
+ xmldata <- get_user_new_statuses username latest_status_id
+
+ -- Parsing an empty result can blow up. Just pretend there are
+ -- no new statuses in that case.
+ let new_statuses = case xmldata of
+ Just xml -> parse_statuses xml
+ Nothing -> []
+
+ case (length new_statuses) of
+ 0 ->
+ do_recurse latest_status_id
+ _ -> do
+
+ mention_replies cfg new_statuses
+ mention_retweets cfg new_statuses
+
+ let good_statuses = filter_statuses cfg new_statuses
+
+ _ <- mapM (putStrLn . pretty_print) good_statuses
+
+ send_messages maybe_message good_statuses
+
+ let new_latest_status_id = get_max_status_id new_statuses
+ do_recurse new_latest_status_id
+
+ where
+ -- This lets us write all of these parameters once rather
+ -- than... more than once.
+ do_recurse :: Integer -> IO ()
+ do_recurse lsi = recurse cfg username lsi maybe_message
+
+
+-- | Try continually to download username's timeline, and determine the
+-- latest status id to be posted once we have done so.