f215ab84cb7ef4f338756793c910548866e312e4
[dead/halcyon.git] / src / CommandLine.hs
1 -- | The CommandLine module handles parsing of the command-line
2 -- options. It should more or less be a black box, providing Main
3 -- with only the information it requires.
4 module CommandLine
5 ( get_cfg,
6 help_set,
7 help_text,
8 parse_errors,
9 parse_usernames
10 ) where
11
12 import Data.Maybe (fromJust, isJust, isNothing)
13 import System.Console.GetOpt
14 import System.Directory (doesFileExist)
15 import System.Environment (getArgs)
16
17 import Configuration (Cfg(..))
18
19 -- | A record containing values for all available options.
20 data Options = Options { opt_consumer_key :: Maybe String,
21 opt_consumer_secret :: Maybe String,
22 opt_access_token :: Maybe String,
23 opt_access_secret :: Maybe String,
24 opt_heartbeat :: Maybe Int,
25 opt_help :: Bool,
26 opt_ignore_replies :: Bool,
27 opt_ignore_retweets :: Bool,
28 opt_sendmail_path :: FilePath,
29 opt_from :: Maybe String,
30 opt_to :: Maybe String,
31 opt_verbose :: Bool }
32
33
34 -- | Constructs an instance of Options, with each of its members set
35 -- to default values.
36 default_options :: Options
37 default_options = Options { opt_access_token = Nothing,
38 opt_access_secret = Nothing,
39 opt_consumer_key = Nothing,
40 opt_consumer_secret = Nothing,
41 opt_heartbeat = Just 600,
42 opt_help = False,
43 opt_ignore_replies = False,
44 opt_ignore_retweets = False,
45 opt_sendmail_path = "/usr/sbin/sendmail",
46 opt_from = Nothing,
47 opt_to = Nothing,
48 opt_verbose = False }
49
50
51 -- | The options list that we construct associates a function with
52 -- each option. This function is responsible for updating an Options
53 -- record with the appropriate value.
54 --
55 -- For more information and an example of this idiom, see,
56 --
57 -- <http://www.haskell.org/haskellwiki/High-level_option_handling_with_GetOpt>
58 --
59 options :: [OptDescr (Options -> IO Options)]
60 options =
61 [ Option
62 [] ["consumer-key"]
63 (ReqArg set_consumer_key "consumer-key")
64 "Your Twitter API consumer key.",
65
66 Option
67 [] ["consumer-secret"]
68 (ReqArg set_consumer_secret "consumer-secret")
69 "Your Twitter API consumer secret.",
70
71 Option
72 [] ["access-token"]
73 (ReqArg set_access_token "access-token")
74 "Your Twitter API access token.",
75
76 Option
77 [] ["access-secret"]
78 (ReqArg set_access_secret "access-secret")
79 "Your Twitter API access secret.",
80
81 Option
82 ['h'] ["help"]
83 (NoArg set_help)
84 "Prints this help message.",
85
86 Option
87 ['n'] ["heartbeat"]
88 (ReqArg set_heartbeat "heartbeat")
89 "How many seconds to wait between polling.",
90
91 Option
92 ['t'] ["to"]
93 (ReqArg set_to "email_address")
94 "Send tweets TO email_address.",
95
96 Option
97 ['f'] ["from"]
98 (ReqArg set_from "email_address")
99 "Send tweets FROM email_address.",
100
101 Option
102 ['s'] ["sendmail_path"]
103 (ReqArg set_sendmail_path "sendmail_path")
104 "Use sendmail_path to send mail",
105
106 Option
107 ['i'] ["ignore-replies"]
108 (NoArg set_ignore_replies)
109 "Ignore replies.",
110
111 Option
112 ['I'] ["ignore-retweets"]
113 (NoArg set_ignore_retweets)
114 "Ignore retweets.",
115
116 Option
117 ['v'] ["verbose"]
118 (NoArg set_verbose)
119 "Be verbose about stuff."
120 ]
121
122
123 -- | Attempt to parse an 'Int' from a 'String'. This is just a 'Maybe'
124 -- wrapper around 'reads'.
125 parse_int :: String -> Maybe Int
126 parse_int s =
127 case (reads s) of
128 [(n,_)] -> Just n
129 _ -> Nothing
130
131 set_consumer_key :: String -> Options -> IO Options
132 set_consumer_key arg opts = do
133 return opts { opt_consumer_key = Just arg }
134
135 set_consumer_secret :: String -> Options -> IO Options
136 set_consumer_secret arg opts = do
137 return opts { opt_consumer_secret = Just arg }
138
139 set_access_token :: String -> Options -> IO Options
140 set_access_token arg opts = do
141 return opts { opt_access_token = Just arg }
142
143 set_access_secret :: String -> Options -> IO Options
144 set_access_secret arg opts = do
145 return opts { opt_access_secret = Just arg }
146
147 set_heartbeat :: String -> Options -> IO Options
148 set_heartbeat arg opts = do
149 let new_heartbeat = parse_int arg
150 return opts { opt_heartbeat = new_heartbeat }
151
152 set_help :: Options -> IO Options
153 set_help opts = do
154 return opts { opt_help = True }
155
156 set_ignore_retweets :: Options -> IO Options
157 set_ignore_retweets opts =
158 return opts { opt_ignore_retweets = True }
159
160 set_ignore_replies :: Options -> IO Options
161 set_ignore_replies opts =
162 return opts { opt_ignore_replies = True }
163
164 set_verbose :: Options -> IO Options
165 set_verbose opts =
166 return opts { opt_verbose = True }
167
168 set_sendmail_path :: String -> Options -> IO Options
169 set_sendmail_path arg opts = do
170 return opts { opt_sendmail_path = arg }
171
172 set_to :: String -> Options -> IO Options
173 set_to arg opts = do
174 return opts { opt_to = Just arg }
175
176 set_from :: String -> Options -> IO Options
177 set_from arg opts = do
178 return opts { opt_from = Just arg }
179
180
181 -- | The usage header.
182 usage :: String
183 usage = "Usage: twat --consumer-key=<key> --consumer-secret=<secret> --access-token=<key> --access-secret=<secret> [-n heartbeat] [-t to_address] [-f from_address] [-s path-to-sendmail] <username1> [username2, [username3]...]"
184
185
186 -- | Was the help option passed?
187 help_set :: IO Bool
188 help_set = do
189 opts <- parse_options
190 return (opt_help opts)
191
192 -- | The usage header, and all available flags (as generated by GetOpt)
193 help_text :: String
194 help_text = usageInfo usage options
195
196
197 -- | Return a list of options.
198 parse_options :: IO Options
199 parse_options = do
200 argv <- getArgs
201 let (actions, _, _) = getOpt Permute options argv
202
203 -- This will execute each of the functions contained in our options
204 -- list, one after another, on a default_options record. The end
205 -- result should be an Options instance with all of its members set
206 -- correctly.
207 opts <- foldl (>>=) (return default_options) actions
208
209 return opts
210
211
212 -- | A list of parse errors relating to the heartbeat.
213 heartbeat_errors :: IO [String]
214 heartbeat_errors = do
215 hb <- parse_heartbeat
216 if (isNothing hb)
217 then return ["\"heartbeat\" does not appear to be an integer."]
218 else return []
219
220 -- | Parse errors relating to the list of usernames.
221 username_errors :: IO [String]
222 username_errors = do
223 argv <- getArgs
224 let (_, usernames, _) = getOpt Permute options argv
225
226 if (null usernames)
227 then return ["no usernames provided."]
228 else return []
229
230
231 -- | Parse errors relating to the "To" address.
232 to_errors :: IO [String]
233 to_errors = do
234 toaddr <- parse_to_address
235 fromaddr <- parse_from_address
236 if (isNothing toaddr) && (isJust fromaddr)
237 then return ["\"from\" address specified without \"to\" address."]
238 else return []
239
240
241 -- | Errors for the sendmail path argument.
242 sendmail_path_errors :: IO [String]
243 sendmail_path_errors = do
244 sendmail <- parse_sendmail_path
245 exists <- doesFileExist sendmail
246 if (not exists)
247 then return ["sendmail path does not exist"]
248 else return []
249
250
251 -- | Parse errors relating to the "From" address.
252 from_errors :: IO [String]
253 from_errors = do
254 toaddr <- parse_to_address
255 fromaddr <- parse_from_address
256 if (isJust toaddr) && (isNothing fromaddr)
257 then return ["\"to\" address specified without \"from\" address."]
258 else return []
259
260
261 -- | Format an error message for printing.
262 format_error :: String -> String
263 format_error err = "ERROR: " ++ err ++ "\n"
264
265
266 -- | Return a list of all parse errors.
267 parse_errors :: IO [String]
268 parse_errors = do
269 argv <- getArgs
270 let (_, _, errors) = getOpt Permute options argv
271 errs_heartbeat <- heartbeat_errors
272 errs_username <- username_errors
273 errs_to <- to_errors
274 errs_from <- from_errors
275 errs_sendmail <- sendmail_path_errors
276 return $ map format_error (errors ++
277 errs_heartbeat ++
278 errs_username ++
279 errs_sendmail ++
280 errs_to ++
281 errs_from)
282
283 -- | What's the heartbeat?
284 parse_heartbeat :: IO (Maybe Int)
285 parse_heartbeat = do
286 opts <- parse_options
287 return (opt_heartbeat opts)
288
289 -- | What "To" address was given on the command line?
290 parse_to_address :: IO (Maybe String)
291 parse_to_address = do
292 opts <- parse_options
293 return (opt_to opts)
294
295 -- | What sendmail path was given on the command line?
296 parse_sendmail_path :: IO FilePath
297 parse_sendmail_path = do
298 opts <- parse_options
299 return (opt_sendmail_path opts)
300
301 -- | What "From" address was given on the command line?
302 parse_from_address :: IO (Maybe String)
303 parse_from_address = do
304 opts <- parse_options
305 return (opt_from opts)
306
307
308 -- | What usernames were passed on the command line?
309 parse_usernames :: IO [String]
310 parse_usernames = do
311 argv <- getArgs
312 let (_, usernames, _) = getOpt Permute options argv
313 return usernames
314
315
316
317 -- | Construct a Cfg object from the command line options assuming
318 -- there are no errors.
319 get_cfg :: IO Cfg
320 get_cfg = do
321 opts <- parse_options
322 return Cfg { consumer_key = fromJust $ opt_consumer_key opts,
323 consumer_secret = fromJust $ opt_consumer_secret opts,
324 access_token = fromJust $ opt_access_token opts,
325 access_secret = fromJust $ opt_access_secret opts,
326 heartbeat = fromJust $ opt_heartbeat opts,
327 ignore_replies = opt_ignore_replies opts,
328 ignore_retweets = opt_ignore_retweets opts,
329 sendmail_path = opt_sendmail_path opts,
330 from_address = opt_from opts,
331 to_address = opt_to opts,
332 verbose = opt_verbose opts }