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