]> gitweb.michael.orlitzky.com - dead/halcyon.git/blob - src/Main.hs
5cd957bed71c80953d5df44c1af8cb05824d7633
[dead/halcyon.git] / src / Main.hs
1 module Main
2 where
3
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)
9
10 import CommandLine
11 import ExitCodes
12 import Mail
13 import Twitter.Http
14 import Twitter.Status
15 import Twitter.User
16
17
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
24
25
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])}
34 where
35 -- Use the Status' created_at date if it can be coerced into
36 -- RFC822 format.
37 date = case (created_at_to_rfc822 $ created_at status) of
38 Nothing -> default_date
39 Just c -> c
40
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
47 -- repeats.
48 recurse :: Int -> String -> Integer -> (Maybe Message) -> IO ()
49 recurse delay username latest_status_id maybe_message = do
50 thread_sleep delay
51 xmldata <- get_user_new_statuses username latest_status_id
52
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
57 Nothing -> []
58
59 case (length new_statuses) of
60 0 ->
61 recurse delay username latest_status_id maybe_message
62 _ -> do
63 let new_latest_status_id = get_max_status_id new_statuses
64 _ <- mapM (putStrLn . pretty_print) new_statuses
65
66 case maybe_message of
67 Nothing -> do
68 recurse delay username new_latest_status_id maybe_message
69 return ()
70 Just message -> do
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
76 return ()
77
78
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
84
85 let initial_statuses = case xmldata of
86 Just xml -> parse_statuses xml
87 Nothing -> []
88
89 case (length initial_statuses) of
90 0 -> do
91 -- If the HTTP part barfs, try again after a while.
92 putStrLn ("Couldn't retrieve " ++ username ++ "'s timeline. Retrying...")
93 thread_sleep delay
94 get_latest_status_id delay username
95 _ -> return (get_max_status_id initial_statuses)
96
97
98
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
108 return ()
109
110
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.
114 main :: IO ()
115 main = do
116 errors <- parse_errors
117
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)
122 putStrLn help_text
123 exitWith (ExitFailure exit_args_parse_failed)
124
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
129 putStrLn help_text
130 exitWith ExitSuccess
131
132 usernames <- parse_usernames
133
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
139 Nothing -> Nothing
140 Just toaddr ->
141 case from_address of
142 Nothing -> Nothing
143 Just fromaddr ->
144 Just (Message { headers = default_headers,
145 body = "",
146 subject = "",
147 to = toaddr,
148 from = fromaddr })
149
150 -- This should be safe since we checked for parse errors earlier.
151 delay <- fmap fromJust heartbeat
152
153 -- Execute run_twat on each username in a new thread.
154 _ <- mapM (forkIO . (run_twat delay message)) usernames
155
156 _ <- forever $ do
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.
160 thread_sleep delay
161
162 return ()
163
164
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
170
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
175 Nothing -> []
176
177 case (length statuses) of
178 0 -> do
179 putStrLn "No statuses returned."
180 return ()
181 _ -> do
182 _ <- mapM (putStrLn . pretty_print) statuses
183
184 case maybe_message of
185 Nothing -> do
186 putStrLn "No message object given."
187 return ()
188 Just message -> do
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
193 return ()