]> gitweb.michael.orlitzky.com - dead/halcyon.git/blob - src/Main.hs
Add a "Date: " header to outgoing messages.
[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.Time (getClockTime)
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 -- |The length of all calls to sleep (or threadDelay), in seconds.
19 heartbeat :: Int
20 heartbeat = 600
21
22 -- |A wrapper around threadDelay which takes seconds instead of
23 -- microseconds as its argument.
24 thread_sleep :: Int -> IO ()
25 thread_sleep seconds = do
26 let microseconds = seconds * (10 ^ (6 :: Int))
27 threadDelay microseconds
28
29
30 -- |Given a 'Message', 'Status', and date, update that message's body
31 -- and subject with the information contained in the status. Adds a
32 -- /Date: / header, and returns the updated message.
33 message_from_status :: Message -> String -> Status -> Message
34 message_from_status message date status =
35 message { subject = "Twat: " ++ (screen_name (user status)),
36 body = (pretty_print status),
37 headers = ((headers message) ++ ["Date: " ++ date])}
38
39
40 -- |This is the main recursive loop. It takes a username, a
41 -- latest_status_id, and optionally a 'Message' as arguments. The
42 -- latest_status_id is the last status (that we know of) to be posted
43 -- to username's Twitter account. If we find any newer statuses when
44 -- we check, they are printed and optionally emailed (if a 'Message'
45 -- was supplied). Then, the process repeats.
46 recurse :: String -> Integer -> (Maybe Message) -> IO ()
47 recurse username latest_status_id maybe_message = do
48 thread_sleep heartbeat
49 xmldata <- get_user_new_statuses username latest_status_id
50
51 -- Parsing an empty result can blow up. Just pretend there are
52 -- no new statuses in that case.
53 let new_statuses = case xmldata of
54 Just xml -> parse_statuses xml
55 Nothing -> []
56
57 case (length new_statuses) of
58 0 ->
59 recurse username latest_status_id maybe_message
60 _ -> do
61 let new_latest_status_id = get_max_status_id new_statuses
62 mapM (putStrLn . pretty_print) new_statuses
63
64 case maybe_message of
65 Nothing -> do
66 recurse username new_latest_status_id maybe_message
67 return ()
68 Just message -> do
69 date <- getClockTime
70 let messages = map (message_from_status message (show date)) new_statuses
71 sendmail_results <- mapM sendmail messages
72 mapM print_sendmail_result sendmail_results
73 recurse username new_latest_status_id maybe_message
74 return ()
75
76
77 -- |Try continually to download username's timeline, and determine the
78 -- latest status id to be posted once we have done so.
79 get_latest_status_id :: String -> IO Integer
80 get_latest_status_id username = do
81 xmldata <- get_user_timeline username
82
83 let initial_statuses = case xmldata of
84 Just xml -> parse_statuses xml
85 Nothing -> []
86
87 case (length initial_statuses) of
88 0 -> do
89 -- If the HTTP part barfs, try again after a while.
90 putStrLn ("Couldn't retrieve " ++ username ++ "'s timeline. Retrying...")
91 thread_sleep heartbeat
92 get_latest_status_id username
93 _ -> return (get_max_status_id initial_statuses)
94
95
96
97 -- |This function wraps two steps. First, we need to find the latest
98 -- status id posted by username. Once we have that, we can begin the
99 -- recursive loop that checks for updates forever. The message
100 -- argument is optional and is passed to recurse in case the updates
101 -- should be emailed.
102 run_twat :: Maybe Message -> String -> IO ()
103 run_twat message username = do
104 latest_status_id <- get_latest_status_id username
105 recurse username latest_status_id message
106 return ()
107
108
109 -- |The main function just parses the command-line arguments and then
110 -- forks off calls to 'run_twat' for each supplied username. After
111 -- forking, main loops forever.
112 main :: IO ()
113 main = do
114 errors <- parse_errors
115
116 -- If there were errors parsing the command-line options,
117 -- print them and exit.
118 when (not (null errors)) $ do
119 hPutStrLn stderr (concat errors)
120 putStrLn help_text
121 exitWith (ExitFailure exit_args_parse_failed)
122
123 -- Next, check to see if the 'help' option was passed to the
124 -- program. If it was, display the help, and exit successfully.
125 help_opt_set <- help_set
126 when help_opt_set $ do
127 putStrLn help_text
128 exitWith ExitSuccess
129
130 usernames <- parse_usernames
131
132 -- If we have both a "To" and "From" address, we'll create a
133 -- message object to be passed to all of our threads.
134 to_address <- to_email_address
135 from_address <- from_email_address
136 let message = case to_address of
137 Nothing -> Nothing
138 Just toaddr ->
139 case from_address of
140 Nothing -> Nothing
141 Just fromaddr ->
142 Just (Message { headers = [],
143 body = "",
144 subject = "",
145 to = toaddr,
146 from = fromaddr })
147
148 -- Execute run_twat on each username in a new thread.
149 mapM (forkIO . (run_twat message)) usernames
150
151 forever $ do
152 -- This thread (the one executing main) doesn't do anything,
153 -- but when it terminates, so do all the threads we forked.
154 -- As a result, we need to keep this thread on life support.
155 thread_sleep heartbeat
156
157 return ()