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 (or threadDelay), in seconds.
21 -- |A wrapper around threadDelay which takes seconds instead of
22 -- microseconds as its argument.
23 thread_sleep :: Int -> IO ()
24 thread_sleep seconds = do
25 let microseconds = seconds * (10 ^ (6 :: Int))
26 threadDelay microseconds
29 -- |Given a 'Message' and a 'Status', update that message's body and
30 -- subject with the information contained in the status. Returns the
32 message_from_status :: Message -> Status -> Message
33 message_from_status message status =
34 message { subject = "Twat: " ++ (screen_name (user status)),
35 body = (pretty_print status) }
38 -- |This is the main recursive loop. It takes a username, a
39 -- latest_status_id, and optionally a 'Message' as arguments. The
40 -- latest_status_id is the last status (that we know of) to be posted
41 -- to username's Twitter account. If we find any newer statuses when
42 -- we check, they are printed and optionally emailed (if a 'Message'
43 -- was supplied). Then, the process repeats.
44 recurse :: String -> Integer -> (Maybe Message) -> IO ()
45 recurse username latest_status_id maybe_message = do
46 thread_sleep heartbeat
47 xmldata <- get_user_new_statuses username latest_status_id
49 -- Parsing an empty result can blow up. Just pretend there are
50 -- no new statuses in that case.
51 let new_statuses = case xmldata of
52 Just xml -> parse_statuses xml
55 case (length new_statuses) of
57 recurse username latest_status_id maybe_message
59 let new_latest_status_id = get_max_status_id new_statuses
60 mapM (putStrLn . pretty_print) new_statuses
64 recurse username new_latest_status_id maybe_message
67 let messages = map (message_from_status message) new_statuses
68 sendmail_results <- mapM sendmail messages
69 mapM print_sendmail_result sendmail_results
70 recurse username new_latest_status_id maybe_message
74 -- |Try continually to download username's timeline, and determine the
75 -- latest status id to be posted once we have done so.
76 get_latest_status_id :: String -> IO Integer
77 get_latest_status_id username = do
78 xmldata <- get_user_timeline username
80 let initial_statuses = case xmldata of
81 Just xml -> parse_statuses xml
84 case (length initial_statuses) of
86 -- If the HTTP part barfs, try again after a while.
87 putStrLn ("Couldn't retrieve " ++ username ++ "'s timeline. Retrying...")
88 thread_sleep heartbeat
89 get_latest_status_id username
90 _ -> return (get_max_status_id initial_statuses)
94 -- |This function wraps two steps. First, we need to find the latest
95 -- status id posted by username. Once we have that, we can begin the
96 -- recursive loop that checks for updates forever. The message
97 -- argument is optional and is passed to recurse in case the updates
99 run_twat :: Maybe Message -> String -> IO ()
100 run_twat message username = do
101 latest_status_id <- get_latest_status_id username
102 recurse username latest_status_id message
106 -- |The main function just parses the command-line arguments and then
107 -- forks off calls to 'run_twat' for each supplied username. After
108 -- forking, main loops forever.
111 errors <- parse_errors
113 -- If there were errors parsing the command-line options,
114 -- print them and exit.
115 when (not (null errors)) $ do
116 hPutStrLn stderr (concat errors)
118 exitWith (ExitFailure exit_args_parse_failed)
120 -- Next, check to see if the 'help' option was passed to the
121 -- program. If it was, display the help, and exit successfully.
122 help_opt_set <- help_set
123 when help_opt_set $ do
127 usernames <- parse_usernames
129 -- If we have both a "To" and "From" address, we'll create a
130 -- message object to be passed to all of our threads.
131 to_address <- to_email_address
132 from_address <- from_email_address
133 let message = case to_address of
139 Just (Message { headers = [],
145 -- Execute run_twat on each username in a new thread.
146 mapM (forkIO . (run_twat message)) usernames
149 -- This thread (the one executing main) doesn't do anything,
150 -- but when it terminates, so do all the threads we forked.
151 -- As a result, we need to keep this thread on life support.
152 thread_sleep heartbeat