commodore 0.3.0

Slack Command API handler library
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
//! Call rank and take command of [Slack](https://slack.com/) with rust at your helm

//#[macro_use]
//extern crate error_chain;
#[macro_use]
extern crate log;
extern crate hyper;
extern crate hyper_native_tls;
extern crate url;
extern crate regex;
extern crate serde;
#[macro_use]
extern crate serde_derive;
extern crate serde_json;

use hyper::Client;
use hyper::net::HttpsConnector;
use hyper::header::ContentType;
use hyper::server::{Handler as HyperHandler, Request, Response as HyperResponse};
use hyper_native_tls::NativeTlsClient;
use regex::{Captures as RegexCaptures, Regex};
use std::collections::HashMap;
use std::io::Read;

mod response;
pub use response::{Attachment, Field, Response, ResponseBuilder, AttachmentBuilder};

const DEFAULT_RESPONSE: &'static [u8] = b"ok";

fn params<R: Read>(read: &mut R) -> HashMap<String, String> {
    let mut buffer = String::new();
    read.read_to_string(&mut buffer).unwrap();
    let mut params = HashMap::new();
    for (k, v) in url::form_urlencoded::parse(buffer.as_bytes()) {
        params.insert(k.into_owned(), v.into_owned());
    }
    params
}

/// Results for regex matchers that collect captures
pub type Captures<'a> = RegexCaptures<'a>;

/// Deferred response interface
pub trait Responder: Sync + Send {
    /// Calling respond should update
    /// the channel or reply to the user
    /// that issued the original command
    fn respond(&self, response: Response) -> ();
}

#[doc(hidden)]
pub struct DefaultResponder {
    response_url: String,
}

impl DefaultResponder {
    pub fn new<U>(response_url: U) -> DefaultResponder
    where
        U: Into<String>,
    {
        DefaultResponder { response_url: response_url.into() }
    }
}

impl Responder for DefaultResponder {
    fn respond(&self, response: Response) {
        let client = Client::with_connector(
            HttpsConnector::new(
                NativeTlsClient::new().unwrap()
            )
        );
        let _ = client
            .post(&self.response_url[..])
            .header(ContentType::json())
            .body(serde_json::to_string(&response).unwrap().as_bytes())
            .send();
    }
}

/// Command handling interface
/// Implementation for Fn
pub trait Handler: Sync + Send {
    /// handles Slack commands. Optional captures resulting
    /// from matching are provided along with an interface
    /// for deferred responses
    fn handle(
        &self,
        cmd: &Command,
        caps: &Option<Captures>,
        responder: Box<Responder>,
    ) -> Option<Response>;

    /// provides a mean explicit coersion for
    /// disambiguating cases where a fn named `handle` is
    /// already defined in another trait for which another
    /// impl exists for the same type
    fn as_handler(&self) -> &Handler;
}

impl<F> Handler for F
where
    F: Fn(&Command, &Option<Captures>, Box<Responder>) -> Option<Response>,
    F: Send + Sync,
{
    fn handle(
        &self,
        cmd: &Command,
        caps: &Option<Captures>,
        responder: Box<Responder>,
    ) -> Option<Response> {
        self(cmd, caps, responder)
    }

    fn as_handler(&self) -> &Handler {
        self
    }
}

#[doc(hidden)]
pub struct TokenValidator<H: Handler + 'static> {
    handler: H,
    token: String,
}

impl<H: Handler + 'static> Handler for TokenValidator<H> {
    fn handle(
        &self,
        cmd: &Command,
        caps: &Option<Captures>,
        responder: Box<Responder>,
    ) -> Option<Response> {
        if cmd.token == self.token {
            self.handler.handle(cmd, caps, responder)
        } else {
            error!(
                "cmd token ${:?} did not match handler token ${:?}",
                cmd.token,
                self.token
            );
            None
        }
    }

    fn as_handler(&self) -> &Handler {
        self
    }
}

/// Command matching interface
pub trait Matcher: Send + Sync {
    /// returns of tuple of optional captures and an indicator for
    /// whether or not the provided command is matched
    fn matches<'a>(&self, cmd: &'a Command) -> (Option<Captures<'a>>, bool);
}

impl<F> Matcher for F
where
    F: Fn(&Command) -> (Option<Captures>, bool),
    F: Send + Sync,
{
    fn matches<'a>(&self, cmd: &'a Command) -> (Option<Captures<'a>>, bool) {
        self(cmd)
    }
}

/// A direct command matcher
pub struct MatchCommand(pub String);

impl Matcher for MatchCommand {
    fn matches<'a>(&self, cmd: &'a Command) -> (Option<Captures<'a>>, bool) {
        (None, cmd.command == self.0)
    }
}

/// A matcher that assumes any text starting with
/// the provided string is a subcommand. i.e. /cmd help
pub struct MatchSubCommand(pub String);

impl Matcher for MatchSubCommand {
    fn matches<'a>(&self, cmd: &'a Command) -> (Option<Captures<'a>>, bool) {
        if cmd.text.starts_with(&self.0) {
            (None, true)
        } else {
            debug!("regex {:?} did not match cmd {:?}", self.0, cmd.command);
            (None, false)
        }
    }
}

/// A regex pattern matcher for command text.
/// Regex captures will be provided to the matched Handler
pub struct MatchText(pub Regex);

impl Matcher for MatchText {
    fn matches<'a>(&self, cmd: &'a Command) -> (Option<Captures<'a>>, bool) {
        if self.0.is_match(cmd.text.as_ref()) {
            (self.0.captures(cmd.text.as_ref()), true)
        } else {
            debug!("regex {:?} did not match cmd {:?}", self.0, cmd.command);
            (None, false)
        }
    }
}

#[doc(hidden)]
pub struct Route {
    handler: Box<Handler>,
    matcher: Box<Matcher>,
}

/// A command de-multiplexor
#[derive(Default)]
pub struct Mux {
    routes: Vec<Box<Route>>,
}

impl Mux {
    pub fn new() -> Mux {
        Mux { ..Default::default() }
    }

    /// Install routing for a Slack command, secret token, and target Handler
    pub fn command<C, T, H>(&mut self, cmd: C, token: T, handler: H)
    where
        C: Into<String>,
        T: Into<String>,
        H: Handler + 'static,
    {
        self.matching(
            MatchCommand(cmd.into()),
            TokenValidator {
                handler: handler,
                token: token.into(),
            },
        )
    }

    /// Install routing for a Slack command matcher and target Handler
    pub fn matching<M, H>(&mut self, matcher: M, handler: H)
    where
        M: Matcher + 'static,
        H: Handler + 'static,
    {
        let route = Route {
            handler: Box::new(handler),
            matcher: Box::new(matcher),
        };
        self.route(route)
    }

    /// Install a command routing
    pub fn route(&mut self, route: Route) {
        self.routes.push(Box::new(route));
    }

    /// Attempts to return the first match result for a target Handler
    pub fn handler<'a>(&self, cmd: &'a Command) -> Option<(Option<Captures<'a>>, &Box<Handler>)> {
        for r in self.routes.iter() {
            if let (captures, true) = r.matcher.matches(cmd) {
                return Some((captures, &r.handler));
            }
        }
        None
    }
}

impl Handler for Mux {
    fn handle(
        &self,
        cmd: &Command,
        _: &Option<Captures>,
        responder: Box<Responder>,
    ) -> Option<Response> {
        if let &Some((ref captures, handler)) = &self.handler(&cmd) {
            debug!("cmd matched. attempting to handle cmd {:#?}", cmd);
            handler.handle(&cmd, &captures, responder)
        } else {
            debug!("no matching handlers for {:#?}", cmd);
            None
        }
    }

    fn as_handler(&self) -> &Handler {
        self
    }
}

/// A struct representation of a Slack Command
/// and the context from which it was triggered
#[derive(Default, Debug, PartialEq)]
pub struct Command {
    pub token: String,
    pub team_id: String,
    pub team_domain: String,
    pub channel_id: String,
    pub channel_name: String,
    pub user_id: String,
    pub user_name: String,
    pub command: String,
    pub text: String,
    pub response_url: String,
}

impl Command {
    pub fn from_params(params: HashMap<String, String>) -> Option<Command> {
        if let (Some(token),
                Some(team_id),
                Some(team_domain),
                Some(channel_id),
                Some(channel_name),
                Some(user_id),
                Some(user_name),
                Some(command),
                Some(text),
                Some(response_url)) =
            (params.get("token"),
             params.get("team_id"),
             params.get("team_domain"),
             params.get("channel_id"),
             params.get("channel_name"),
             params.get("user_id"),
             params.get("user_name"),
             params.get("command"),
             params.get("text"),
             params.get("response_url")) {
            Some(
                Command {
                    token: token.clone(),
                    team_id: team_id.clone(),
                    team_domain: team_domain.clone(),
                    channel_id: channel_id.clone(),
                    channel_name: channel_name.clone(),
                    user_id: user_id.clone(),
                    user_name: user_name.clone(),
                    command: command.clone(),
                    text: text.clone(),
                    response_url: response_url.clone(),
                },
            )
        } else {
            None
        }
    }
}

impl HyperHandler for Mux {
    // https://api.slack.com/slash-commands
    fn handle(&self, req: Request, mut res: HyperResponse) {
        let (_, _, _, _, _, mut body) = req.deconstruct();

        // parse params
        let params = params(&mut body);
        // parse cmd
        if let Some(cmd) = Command::from_params(params) {
            debug!("rec cmd {:?}", cmd);
            let write = |bytes: &[u8], content_type: ContentType| {
                res.headers_mut().set(content_type);
                let _ = res.send(bytes);
            };
            let responder = DefaultResponder::new(cmd.response_url.clone());
            if let Some(resp) = self.as_handler()
                   .handle(&cmd, &None, Box::new(responder)) {
                match serde_json::to_string(&resp) {
                    Ok(payload) => write(payload.as_bytes(), ContentType::json()),
                    _ => write(DEFAULT_RESPONSE, ContentType::plaintext()),
                };
            } else {
                write(DEFAULT_RESPONSE, ContentType::plaintext())
            };
        } else {
            error!("rec invalid cmd");
            let _ = res.send(DEFAULT_RESPONSE);
        }
        ()
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use super::regex::Regex;
    use std::collections::HashMap;

    #[test]
    fn matches_commands() {
        let cmd = Command {
            command: "/test".to_owned(),
            ..Default::default()
        };
        let (_, matched) = MatchCommand("/test".to_owned()).matches(&cmd);
        assert!(matched, "cmd did not match")
    }

    #[test]
    fn matches_text() {
        let cmd = Command {
            text: "/test hello world".to_owned(),
            ..Default::default()
        };
        let (captures, matched) = MatchText(Regex::new(r"(?P<greeting>\S+?) (?P<name>\S+?)$").unwrap(),)
            .matches(&cmd);
        assert!(matched, "cmd did not match");
        match captures {
            Some(caps) => {
                assert_eq!(caps.name("greeting").map(|m| m.as_str()), Some("hello"));
                assert_eq!(caps.name("name").map(|m| m.as_str()), Some("world"));
            }
            _ => assert!(false, "expected captures"),
        }
    }

    #[test]
    fn extracts_commands() {
        let mut params = HashMap::new();
        params.insert("token".to_owned(), "test_token".to_owned());
        params.insert("team_id".to_owned(), "test_team".to_owned());
        params.insert("team_domain".to_owned(), "test_team_domain".to_owned());
        params.insert("channel_id".to_owned(), "test_channel_id".to_owned());
        params.insert("channel_name".to_owned(), "test_channel_name".to_owned());
        params.insert("user_id".to_owned(), "test_user_id".to_owned());
        params.insert("user_name".to_owned(), "test_user_name".to_owned());
        params.insert("command".to_owned(), "test_command".to_owned());
        params.insert("text".to_owned(), "test_text".to_owned());
        params.insert("response_url".to_owned(), "test_response_url".to_owned());
        match Command::from_params(params) {
            Some(cmd) => {
                assert_eq!(
                    cmd,
                    Command {
                        token: "test_token".to_owned(),
                        team_id: "test_team".to_owned(),
                        team_domain: "test_team_domain".to_owned(),
                        channel_id: "test_channel_id".to_owned(),
                        channel_name: "test_channel_name".to_owned(),
                        user_id: "test_user_id".to_owned(),
                        user_name: "test_user_name".to_owned(),
                        command: "test_command".to_owned(),
                        text: "test_text".to_owned(),
                        response_url: "test_response_url".to_owned(),
                    }
                )
            }
            _ => assert!(false, "failed to extract command"),
        }
    }
}