]> gitweb.michael.orlitzky.com - dead/halcyon.git/blob - src/CommandLine.hs
db3b12da2039c6de2d046bf04e22782849b32649
[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 =
133 return opts { opt_consumer_key = Just arg }
134
135 set_consumer_secret :: String -> Options -> IO Options
136 set_consumer_secret arg opts =
137 return opts { opt_consumer_secret = Just arg }
138
139 set_access_token :: String -> Options -> IO Options
140 set_access_token arg opts =
141 return opts { opt_access_token = Just arg }
142
143 set_access_secret :: String -> Options -> IO Options
144 set_access_secret arg opts =
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 =
170 return opts { opt_sendmail_path = arg }
171
172 set_to :: String -> Options -> IO Options
173 set_to arg opts =
174 return opts { opt_to = Just arg }
175
176 set_from :: String -> Options -> IO Options
177 set_from arg opts =
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 foldl (>>=) (return default_options) actions
208
209
210 -- | A list of parse errors relating to the heartbeat.
211 heartbeat_errors :: IO [String]
212 heartbeat_errors = do
213 hb <- parse_heartbeat
214 if (isNothing hb)
215 then return ["\"heartbeat\" does not appear to be an integer."]
216 else return []
217
218 -- | Parse errors relating to the list of usernames.
219 username_errors :: IO [String]
220 username_errors = do
221 argv <- getArgs
222 let (_, usernames, _) = getOpt Permute options argv
223
224 if (null usernames)
225 then return ["no usernames provided."]
226 else return []
227
228
229 -- | Parse errors relating to the "To" address.
230 to_errors :: IO [String]
231 to_errors = do
232 toaddr <- parse_to_address
233 fromaddr <- parse_from_address
234 if (isNothing toaddr) && (isJust fromaddr)
235 then return ["\"from\" address specified without \"to\" address."]
236 else return []
237
238
239 -- | Errors for the sendmail path argument.
240 sendmail_path_errors :: IO [String]
241 sendmail_path_errors = do
242 sendmail <- parse_sendmail_path
243 exists <- doesFileExist sendmail
244 if (not exists)
245 then return ["sendmail path does not exist"]
246 else return []
247
248
249 -- | Parse errors relating to the "From" address.
250 from_errors :: IO [String]
251 from_errors = do
252 toaddr <- parse_to_address
253 fromaddr <- parse_from_address
254 if (isJust toaddr) && (isNothing fromaddr)
255 then return ["\"to\" address specified without \"from\" address."]
256 else return []
257
258
259 -- | Format an error message for printing.
260 format_error :: String -> String
261 format_error err = "ERROR: " ++ err ++ "\n"
262
263
264 -- | Return a list of all parse errors.
265 parse_errors :: IO [String]
266 parse_errors = do
267 argv <- getArgs
268 let (_, _, errors) = getOpt Permute options argv
269 errs_heartbeat <- heartbeat_errors
270 errs_username <- username_errors
271 errs_to <- to_errors
272 errs_from <- from_errors
273 errs_sendmail <- sendmail_path_errors
274 return $ map format_error (errors ++
275 errs_heartbeat ++
276 errs_username ++
277 errs_sendmail ++
278 errs_to ++
279 errs_from)
280
281 -- | What's the heartbeat?
282 parse_heartbeat :: IO (Maybe Int)
283 parse_heartbeat = do
284 opts <- parse_options
285 return (opt_heartbeat opts)
286
287 -- | What "To" address was given on the command line?
288 parse_to_address :: IO (Maybe String)
289 parse_to_address = do
290 opts <- parse_options
291 return (opt_to opts)
292
293 -- | What sendmail path was given on the command line?
294 parse_sendmail_path :: IO FilePath
295 parse_sendmail_path = do
296 opts <- parse_options
297 return (opt_sendmail_path opts)
298
299 -- | What "From" address was given on the command line?
300 parse_from_address :: IO (Maybe String)
301 parse_from_address = do
302 opts <- parse_options
303 return (opt_from opts)
304
305
306 -- | What usernames were passed on the command line?
307 parse_usernames :: IO [String]
308 parse_usernames = do
309 argv <- getArgs
310 let (_, usernames, _) = getOpt Permute options argv
311 return usernames
312
313
314
315 -- | Construct a Cfg object from the command line options assuming
316 -- there are no errors.
317 get_cfg :: IO Cfg
318 get_cfg = do
319 opts <- parse_options
320 return Cfg { consumer_key = fromJust $ opt_consumer_key opts,
321 consumer_secret = fromJust $ opt_consumer_secret opts,
322 access_token = fromJust $ opt_access_token opts,
323 access_secret = fromJust $ opt_access_secret opts,
324 heartbeat = fromJust $ opt_heartbeat opts,
325 ignore_replies = opt_ignore_replies opts,
326 ignore_retweets = opt_ignore_retweets opts,
327 sendmail_path = opt_sendmail_path opts,
328 from_address = opt_from opts,
329 to_address = opt_to opts,
330 verbose = opt_verbose opts }