]> gitweb.michael.orlitzky.com - dead/halcyon.git/blob - src/Main.hs
Add the test suite.
[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 System.Exit (ExitCode(..), exitWith)
7 import System.IO (hPutStrLn, stderr)
8
9 import CommandLine
10 import ExitCodes
11 import Mail
12 import Twitter.Http
13 import Twitter.Status
14 import Twitter.User
15
16
17 -- |The length of all calls to sleep (or threadDelay), in seconds.
18 heartbeat :: Int
19 heartbeat = 600
20
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
27
28
29 -- |Given a 'Message' and a 'Status', update that message's body and
30 -- subject with the information contained in the status. Returns the
31 -- updated message.
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) }
36
37
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
48
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
53 Nothing -> []
54
55 case (length new_statuses) of
56 0 ->
57 recurse username latest_status_id maybe_message
58 _ -> do
59 let new_latest_status_id = get_max_status_id new_statuses
60 mapM (putStrLn . pretty_print) new_statuses
61
62 case maybe_message of
63 Nothing -> do
64 recurse username new_latest_status_id maybe_message
65 return ()
66 Just message -> do
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
71 return ()
72
73
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
79
80 let initial_statuses = case xmldata of
81 Just xml -> parse_statuses xml
82 Nothing -> []
83
84 case (length initial_statuses) of
85 0 -> do
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)
91
92
93
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
98 -- should be emailed.
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
103 return ()
104
105
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.
109 main :: IO ()
110 main = do
111 errors <- parse_errors
112
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)
117 putStrLn help_text
118 exitWith (ExitFailure exit_args_parse_failed)
119
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
124 putStrLn help_text
125 exitWith ExitSuccess
126
127 usernames <- parse_usernames
128
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
134 Nothing -> Nothing
135 Just toaddr ->
136 case from_address of
137 Nothing -> Nothing
138 Just fromaddr ->
139 Just (Message { headers = [],
140 body = "",
141 subject = "",
142 to = toaddr,
143 from = fromaddr })
144
145 -- Execute run_twat on each username in a new thread.
146 mapM (forkIO . (run_twat message)) usernames
147
148 forever $ do
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
153
154 return ()