4 import Control.Concurrent (forkIO, threadDelay)
5 import Control.Monad (forever, when)
6 import Data.Maybe (fromJust)
7 import System.Exit (ExitCode(..), exitWith)
8 import System.IO (hPutStrLn, stderr)
18 -- |A wrapper around threadDelay which takes seconds instead of
19 -- microseconds as its argument.
20 thread_sleep :: Int -> IO ()
21 thread_sleep seconds = do
22 let microseconds = seconds * (10 ^ (6 :: Int))
23 threadDelay microseconds
26 -- |Given a 'Message', 'Status', and default date, update that
27 -- message's body and subject with the information contained in the
28 -- status. Adds a /Date: / header, and returns the updated message.
29 message_from_status :: Message -> String -> Status -> Message
30 message_from_status message default_date status =
31 message { subject = "Twat: " ++ (screen_name (user status)),
32 body = (pretty_print status),
33 headers = ((headers message) ++ ["Date: " ++ date])}
35 -- Use the Status' created_at date if it can be coerced into
37 date = case (created_at_to_rfc822 $ created_at status) of
38 Nothing -> default_date
41 -- |This is the main recursive loop. It takes a length of time to
42 -- delay (in seconds), a username, a latest_status_id, and optionally
43 -- a 'Message' as arguments. The latest_status_id is the last status
44 -- (that we know of) to be posted to username's Twitter account. If we
45 -- find any newer statuses when we check, they are printed and
46 -- optionally emailed (if a 'Message' was supplied). Then, the process
48 recurse :: Int -> String -> Integer -> (Maybe Message) -> IO ()
49 recurse delay username latest_status_id maybe_message = do
51 xmldata <- get_user_new_statuses username latest_status_id
53 -- Parsing an empty result can blow up. Just pretend there are
54 -- no new statuses in that case.
55 let new_statuses = case xmldata of
56 Just xml -> parse_statuses xml
59 case (length new_statuses) of
61 recurse delay username latest_status_id maybe_message
63 let new_latest_status_id = get_max_status_id new_statuses
64 _ <- mapM (putStrLn . pretty_print) new_statuses
68 recurse delay username new_latest_status_id maybe_message
71 default_date <- rfc822_now
72 let messages = map (message_from_status message (default_date)) new_statuses
73 sendmail_results <- mapM sendmail messages
74 _ <- mapM print_sendmail_result sendmail_results
75 recurse delay username new_latest_status_id maybe_message
79 -- |Try continually to download username's timeline, and determine the
80 -- latest status id to be posted once we have done so.
81 get_latest_status_id :: Int -> String -> IO Integer
82 get_latest_status_id delay username = do
83 xmldata <- get_user_timeline username
85 let initial_statuses = case xmldata of
86 Just xml -> parse_statuses xml
89 case (length initial_statuses) of
91 -- If the HTTP part barfs, try again after a while.
92 putStrLn ("Couldn't retrieve " ++ username ++ "'s timeline. Retrying...")
94 get_latest_status_id delay username
95 _ -> return (get_max_status_id initial_statuses)
99 -- |This function wraps two steps. First, we need to find the latest
100 -- status id posted by username. Once we have that, we can begin the
101 -- recursive loop that checks for updates forever. The message
102 -- argument is optional and is passed to recurse in case the updates
103 -- should be emailed.
104 run_twat :: Int -> Maybe Message -> String -> IO ()
105 run_twat delay message username = do
106 latest_status_id <- get_latest_status_id delay username
107 recurse delay username latest_status_id message
111 -- |The main function just parses the command-line arguments and then
112 -- forks off calls to 'run_twat' for each supplied username. After
113 -- forking, main loops forever.
116 errors <- parse_errors
118 -- If there were errors parsing the command-line options,
119 -- print them and exit.
120 when (not (null errors)) $ do
121 hPutStrLn stderr (concat errors)
123 exitWith (ExitFailure exit_args_parse_failed)
125 -- Next, check to see if the 'help' option was passed to the
126 -- program. If it was, display the help, and exit successfully.
127 help_opt_set <- help_set
128 when help_opt_set $ do
132 usernames <- parse_usernames
134 -- If we have both a "To" and "From" address, we'll create a
135 -- message object to be passed to all of our threads.
136 to_address <- to_email_address
137 from_address <- from_email_address
138 let message = case to_address of
144 Just (Message { headers = default_headers,
150 -- This should be safe since we checked for parse errors earlier.
151 delay <- fmap fromJust heartbeat
153 -- Execute run_twat on each username in a new thread.
154 _ <- mapM (forkIO . (run_twat delay message)) usernames
157 -- This thread (the one executing main) doesn't do anything,
158 -- but when it terminates, so do all the threads we forked.
159 -- As a result, we need to keep this thread on life support.
165 -- |A debugging tool that will parse, print, and email a single status
166 -- (given by its id).
167 twat_single_status :: Integer -> (Maybe Message) -> IO ()
168 twat_single_status the_status_id maybe_message = do
169 xmldata <- get_status the_status_id
171 -- Parsing an empty result can blow up. Just pretend there are
172 -- no new statuses in that case.
173 let statuses = case xmldata of
174 Just xml -> parse_status xml
177 case (length statuses) of
179 putStrLn "No statuses returned."
182 _ <- mapM (putStrLn . pretty_print) statuses
184 case maybe_message of
186 putStrLn "No message object given."
189 default_date <- rfc822_now
190 let messages = map (message_from_status message (default_date)) statuses
191 sendmail_results <- mapM sendmail messages
192 _ <- mapM print_sendmail_result sendmail_results