4 import Control.Concurrent (forkIO, threadDelay)
5 import Control.Monad (forever, when)
6 import System.Exit (ExitCode(..), exitWith)
7 import System.IO (hPutStrLn, stderr)
17 -- The length of all calls to sleep, in seconds.
21 thread_sleep :: Int -> IO ()
22 thread_sleep microseconds = do
23 let seconds = microseconds * (10 ^ (6 :: Int))
27 message_from_status :: Message -> Status -> Message
28 message_from_status message status =
29 message { subject = "Twat: " ++ (screen_name (user status)),
30 body = (pretty_print status) }
32 recurse :: String -> Integer -> (Maybe Message) -> IO ()
33 recurse username latest_status_id maybe_message = do
34 thread_sleep heartbeat
35 xmldata <- get_user_new_statuses username latest_status_id
37 -- Parsing an empty result can blow up. Just pretend there are
38 -- no new statuses in that case.
39 let new_statuses = case xmldata of
40 Just xml -> parse_statuses xml
43 case (length new_statuses) of
45 recurse username latest_status_id maybe_message
47 let new_latest_status_id = get_max_status_id new_statuses
48 mapM (putStrLn . pretty_print) new_statuses
52 recurse username new_latest_status_id maybe_message
55 let messages = map (message_from_status message) new_statuses
56 sendmail_results <- mapM sendmail messages
57 mapM print_sendmail_result sendmail_results
58 recurse username new_latest_status_id maybe_message
62 get_latest_status_id :: String -> IO Integer
63 get_latest_status_id username = do
64 xmldata <- get_user_timeline username
66 let initial_statuses = case xmldata of
67 Just xml -> parse_statuses xml
70 case (length initial_statuses) of
72 -- If the HTTP part barfs, try again after a while.
73 putStrLn ("Couldn't retrieve " ++ username ++ "'s timeline. Retrying...")
74 thread_sleep heartbeat
75 get_latest_status_id username
76 _ -> return (get_max_status_id initial_statuses)
80 run_twat :: Maybe Message -> String -> IO ()
81 run_twat message username = do
82 latest_status_id <- get_latest_status_id username
83 recurse username latest_status_id message
89 errors <- parse_errors
91 -- If there were errors parsing the command-line options,
92 -- print them and exit.
93 when (not (null errors)) $ do
94 hPutStrLn stderr (concat errors)
96 exitWith (ExitFailure exit_args_parse_failed)
98 -- Next, check to see if the 'help' option was passed to the
99 -- program. If it was, display the help, and exit successfully.
100 help_opt_set <- help_set
101 when help_opt_set $ do
105 usernames <- parse_usernames
107 -- If we have both a "To" and "From" address, we'll create a
108 -- message object to be passed to all of our threads.
109 to_address <- to_email_address
110 from_address <- from_email_address
111 let message = case to_address of
117 Just (Message { headers = [],
123 -- Execute run_twat on each username in a new thread.
124 mapM (forkIO . (run_twat message)) usernames
127 -- This thread (the one executing main) doesn't do anything,
128 -- but when it terminates, so do all the threads we forked.
129 -- As a result, we need to keep this thread on life support.
130 thread_sleep heartbeat