Skip to content
Browse files

Allow handling Campfire enter/leave messages.

  • Loading branch information...
1 parent f37292a commit 9968309b4b5fb52bf64e787ca4242caf41226f36 @eventualbuddha committed Oct 26, 2011
Showing with 63 additions and 7 deletions.
  1. +11 −2 src/hubot/campfire.coffee
  2. +52 −5 src/robot.coffee
View
13 src/hubot/campfire.coffee
@@ -24,12 +24,21 @@ class Campfire extends Robot
bot = new CampfireStreaming(options)
console.log bot
- bot.on "TextMessage", (id, created, room, user, body) ->
+ withAuthor = (callback) -> (id, created, room, user, body) ->
bot.User user, (err, userData) ->
if userData.user
author = self.userForId(userData.user.id, userData.user)
author.room = room
- self.receive new Robot.Message(author, body)
+ callback id, created, room, user, body, author
+
+ bot.on "TextMessage", withAuthor (id, created, room, user, body, author) ->
+ self.receive new Robot.TextMessage(author, body)
+
+ bot.on "EnterMessage", withAuthor (id, created, room, user, body, author) ->
+ self.receive new Robot.EnterMessage(author)
+
+ bot.on "LeaveMessage", withAuthor (id, created, room, user, body, author) ->
+ self.receive new Robot.LeaveMessage(author)
bot.Me (err, data) ->
console.log data
View
57 src/robot.coffee
@@ -26,7 +26,7 @@ class Robot
#
# Returns nothing.
hear: (regex, callback) ->
- @listeners.push new Listener(@, regex, callback)
+ @listeners.push new TextListener(@, regex, callback)
# Public: Adds a Listener that attempts to match incoming messages directed at the robot
# based on a Regex. All regexes treat patterns like they begin with a '^'
@@ -51,7 +51,23 @@ class Robot
newRegex = new RegExp("^#{@name}:?\\s*#{pattern}", modifiers)
console.log newRegex.toString()
- @listeners.push new Listener(@, newRegex, callback)
+ @listeners.push new TextListener(@, newRegex, callback)
+
+ # Public: Adds a Listener that triggers when anyone enters the room.
+ #
+ # callback - A Function that is called with a Response object.
+ #
+ # Returns nothing.
+ enter: (callback) ->
+ @listeners.push new Listener(@, ((msg) -> msg instanceof Robot.EnterMessage), callback)
+
+ # Public: Adds a Listener that triggers when anyone leaves the room.
+ #
+ # callback - A Function that is called with a Response object.
+ #
+ # Returns nothing.
+ leave: (callback) ->
+ @listeners.push new Listener(@, ((msg) -> msg instanceof Robot.LeaveMessage), callback)
# Public: Passes the given message to any interested Listeners.
#
@@ -209,8 +225,15 @@ class Robot.Message
# Represents an incoming message from the chat.
#
# user - A Robot.User instance that sent the message.
+ constructor: (@user) ->
+
+class Robot.TextMessage extends Robot.Message
+ # Represents an incoming message from the chat.
+ #
+ # user - A Robot.User instance that sent the message.
# text - The String message contents.
constructor: (@user, @text) ->
+ super @user
# Determines if the message matches the given regex.
#
@@ -220,15 +243,26 @@ class Robot.Message
match: (regex) ->
@text.match regex
+# Represents an incoming user entrance notification.
+#
+# user - A Robot.User instance for the user who entered.
+class Robot.EnterMessage extends Robot.Message
+
+# Represents an incoming user exit notification.
+#
+# user - A Robot.User instance for the user who left.
+class Robot.LeaveMessage extends Robot.Message
+
+
class Listener
# Listeners receive every message from the chat source and decide if they want
# to act on it.
#
# robot - The current Robot instance.
- # regex - The Regex that determines if this listener should trigger the
+ # matcher - The Function that determines if this listener should trigger the
# callback.
# callback - The Function that is triggered if the incoming message matches.
- constructor: (@robot, @regex, @callback) ->
+ 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
@@ -238,9 +272,22 @@ class Listener
#
# Returns nothing.
call: (message) ->
- if match = message.match @regex
+ if match = @matcher message
@callback new @robot.Response(@robot, message, match)
+class TextListener extends Listener
+ # TextListeners receive every message from the chat source and decide if they want
+ # to act on it.
+ #
+ # robot - The current Robot instance.
+ # regex - The Regex that determines if this listener should trigger the
+ # callback.
+ # callback - The Function that is triggered if the incoming message matches.
+ constructor: (@robot, @regex, @callback) ->
+ @matcher = (message) =>
+ if message instanceof Robot.TextMessage
+ message.match @regex
+
class Robot.Response
# Public: Responses are sent to matching listeners. Messages know about the
# content and user that made the original message, and how to reply back to

0 comments on commit 9968309

Please sign in to comment.
Something went wrong with that request. Please try again.