forked from hubotio/hubot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlistener.coffee
48 lines (42 loc) · 1.46 KB
/
listener.coffee
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
{inspect} = require 'util'
{TextMessage} = require './message'
class Listener
# Listeners receive every message from the chat source and decide if they
# want to act on it.
#
# robot - A Robot instance.
# matcher - A Function that determines if this listener should trigger the
# callback.
# callback - A Function that is triggered if the incoming message matches.
constructor: (@robot, @matcher, @callback) ->
# Public: Determines if the listener likes the content of the message. If
# so, a Response built from the given Message is passed to the Listener
# callback.
#
# message - A Message instance.
#
# Returns a boolean of whether the matcher matched.
call: (message) ->
if match = @matcher message
@robot.logger.debug \
"Message '#{message}' matched regex /#{inspect @regex}/" if @regex
@callback new @robot.Response(@robot, message, match)
true
else
false
class TextListener extends Listener
# TextListeners receive every message from the chat source and decide if they
# want to act on it.
#
# robot - A Robot instance.
# regex - A Regex that determines if this listener should trigger the
# callback.
# callback - A Function that is triggered if the incoming message matches.
constructor: (@robot, @regex, @callback) ->
@matcher = (message) =>
if message instanceof TextMessage
message.match @regex
module.exports = {
Listener
TextListener
}