]> gitweb.michael.orlitzky.com - dead/halcyon.git/blob - src/Main.hs
Bump tagsoup dependency.
[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.Aeson (decode)
7 import Data.List ((\\))
8 import Data.Maybe (fromMaybe, isNothing)
9 import Data.Monoid ((<>))
10 import Data.Time.LocalTime (TimeZone, getCurrentTimeZone)
11 import System.Exit (ExitCode(..), exitWith)
12 import System.IO (hPutStrLn, stderr)
13
14 import CommandLine
15 import Configuration (Cfg(..), default_config, merge_optional)
16 import ExitCodes (exit_no_usernames)
17 import qualified OptionalConfiguration as OC
18 import Mail (
19 Message(..),
20 default_headers,
21 print_sendmail_result,
22 rfc822_now,
23 sendmail)
24 import Twitter.Http
25 import Twitter.Status
26 import Twitter.User
27 import Usernames (Usernames(..))
28
29 -- | A wrapper around threadDelay which takes seconds instead of
30 -- microseconds as its argument.
31 thread_sleep :: Int -> IO ()
32 thread_sleep seconds = do
33 let microseconds = seconds * (10 ^ (6 :: Int))
34 threadDelay microseconds
35
36
37 -- | Given a 'Message', 'Status', and date, update that message's body
38 -- and subject with the information contained in the status. Adds a
39 -- /Date: / header, and returns the updated message.
40 message_from_status :: Maybe TimeZone -> Message -> String -> Status -> Message
41 message_from_status mtz message default_date status =
42 message { subject = "Halcyon: " ++ (screen_name (user status)),
43 body = (pretty_print mtz status),
44 headers = ((headers message) ++ ["Date: " ++ date])}
45 where
46 date =
47 case created_at status of
48 Nothing -> default_date
49 Just c -> utc_time_to_rfc822 mtz c
50
51 -- | If the given Message is not Nothing, send a copy of it for every
52 -- Status in the list.
53 send_messages :: Cfg -> Maybe TimeZone -> Maybe Message -> [Status] -> IO ()
54 send_messages cfg mtz maybe_message statuses =
55 case maybe_message of
56 Nothing -> return ()
57 Just message -> do
58 default_date <- rfc822_now
59 let mfs = message_from_status mtz message default_date
60 let messages = map mfs statuses
61 sendmail_results <- mapM sendmail' messages
62 _ <- mapM print_sendmail_result sendmail_results
63 return ()
64 where
65 sendmail' = sendmail (sendmail_path cfg)
66
67 -- | Display the number of skipped replies if ignore_replies is true
68 -- and verbose is enabled.
69 mention_replies :: Cfg -> [Status] -> IO ()
70 mention_replies cfg ss = do
71 let replies = filter reply ss
72 when ((ignore_replies cfg) && (verbose cfg)) $ do
73 let countstr = show $ length replies
74 putStrLn $ "Ignoring " ++ countstr ++ " replies."
75
76
77 -- | Display the number of skipped retweets if ignore_retweets is true
78 -- and verbose is enabled.
79 mention_retweets :: Cfg -> [Status] -> IO ()
80 mention_retweets cfg ss = do
81 let retweets = filter retweeted ss
82 when ((ignore_retweets cfg) && (verbose cfg)) $ do
83 let countstr = show $ length retweets
84 putStrLn $ "Ignoring " ++ countstr ++ " retweets."
85
86
87
88 -- | Filter out replies/retweets based on the configuration.
89 filter_statuses :: Cfg -> [Status] -> [Status]
90 filter_statuses cfg ss =
91 good_statuses
92 where
93 replies = filter reply ss
94 retweets = filter retweeted ss
95
96 good_statuses' = if (ignore_replies cfg)
97 then ss \\ replies
98 else ss
99
100 good_statuses = if (ignore_retweets cfg)
101 then good_statuses' \\ retweets
102 else good_statuses'
103
104
105
106 -- | This is the main recursive loop. It takes a the configuration, a
107 -- username, a latest_status_id, and optionally a 'Message' as
108 -- arguments. The latest_status_id is the last status (that we know
109 -- of) to be posted to username's Twitter account. If we find any
110 -- newer statuses when we check, they are printed and optionally
111 -- emailed (if a 'Message' was supplied). Then, the process repeats.
112 recurse :: Cfg -> String -> Integer -> (Maybe Message) -> IO ()
113 recurse cfg username latest_status_id maybe_message = do
114 thread_sleep (heartbeat cfg)
115 timeline <- get_user_new_statuses cfg username latest_status_id
116
117 let decoded_timeline = decode timeline :: Maybe Timeline
118
119 when (isNothing decoded_timeline) $
120 hPutStrLn stderr $
121 "Couldn't retrieve "
122 ++ username
123 ++ "'s timeline. Skipping..."
124
125 let new_statuses = fromMaybe [] decoded_timeline
126
127 case new_statuses of
128 [] -> do_recurse latest_status_id
129 _ -> do
130 mention_replies cfg new_statuses
131 mention_retweets cfg new_statuses
132
133 let good_statuses = filter_statuses cfg new_statuses
134
135 tz <- getCurrentTimeZone
136 let mtz = Just tz
137 mapM_ (putStr . (pretty_print mtz)) good_statuses
138
139 send_messages cfg mtz maybe_message good_statuses
140
141 let new_latest_status_id = get_max_status_id new_statuses
142 do_recurse new_latest_status_id
143
144 where
145 -- This lets us write all of these parameters once rather
146 -- than... more than once.
147 do_recurse :: Integer -> IO ()
148 do_recurse lsi = recurse cfg username lsi maybe_message
149
150
151 -- | Try continually to download username's timeline, and determine the
152 -- latest status id to be posted once we have done so.
153 get_latest_status_id :: Cfg -> String -> IO Integer
154 get_latest_status_id cfg username = do
155 let delay = heartbeat cfg
156 timeline <- get_user_timeline cfg username
157
158 let decoded_timeline = decode timeline :: Maybe Timeline
159
160 when (isNothing decoded_timeline) $
161 hPutStrLn stderr $
162 "Couldn't retrieve "
163 ++ username
164 ++ "'s timeline. Skipping..."
165
166 let initial_timeline = fromMaybe [] decoded_timeline
167
168 case initial_timeline of
169 [] -> do
170 -- If the HTTP part barfs, try again after a while.
171 thread_sleep delay
172 get_latest_status_id cfg username
173 _ ->
174 return (get_max_status_id initial_timeline)
175
176
177
178 -- | This function wraps two steps. First, we need to find the latest
179 -- status id posted by username. Once we have that, we can begin the
180 -- recursive loop that checks for updates forever. The message
181 -- argument is optional and is passed to recurse in case the updates
182 -- should be emailed.
183 run :: Cfg -> Maybe Message -> String -> IO ()
184 run cfg msg username = do
185 latest_status_id <- get_latest_status_id cfg username
186 recurse cfg username latest_status_id msg
187 return ()
188
189
190
191 -- | Take advantage of the Maybe monad to only return a message when
192 -- we have both a "to" and "from" address.
193 construct_message :: Cfg -> Maybe Message
194 construct_message cfg = do
195 to_addr <- to_address cfg
196 from_addr <- from_address cfg
197 return $ make_msg to_addr from_addr
198 where
199 make_msg t f = Message { headers = default_headers,
200 body = "",
201 subject = "",
202 to = t,
203 from = f }
204
205 -- |The main function just parses the command-line arguments and then
206 -- forks off calls to 'run' for each supplied username. After
207 -- forking, main loops forever.
208 main :: IO ()
209 main = do
210 -- And a Cfg object.
211 rc_cfg <- OC.from_rc
212 cmd_cfg <- get_args
213
214 -- Merge the config file options with the command-line ones,
215 -- prefering the command-line ones.
216 let opt_config = rc_cfg <> cmd_cfg
217
218 -- Finally, update a default config with any options that have been
219 -- set in either the config file or on the command-line.
220 let cfg = merge_optional default_config opt_config
221
222 when (null $ get_usernames (usernames cfg)) $ do
223 hPutStrLn stderr "ERROR: no usernames supplied."
224 _ <- show_help
225 exitWith (ExitFailure exit_no_usernames)
226
227 -- If we have both a "To" and "From" address, we'll create a
228 -- message object to be passed to all of our threads.
229 let message = construct_message cfg
230
231 -- Execute run on each username in a new thread.
232 let run_curried = run cfg message
233 _ <- mapM (forkIO . run_curried) (get_usernames (usernames cfg))
234
235 _ <- forever $
236 -- This thread (the one executing main) doesn't do anything,
237 -- but when it terminates, so do all the threads we forked.
238 -- As a result, we need to keep this thread on life support.
239 thread_sleep (heartbeat cfg)
240
241 return ()