Skip to content
Browse files

Add ear dropping script

  • Loading branch information...
1 parent e5e62a5 commit 6b1e1ec54d6f4c8b233696371603b8a4a457d44e garylin committed Aug 9, 2012
Showing with 70 additions and 0 deletions.
  1. +70 −0 src/scripts/eardropping.coffee
View
70 src/scripts/eardropping.coffee
@@ -0,0 +1,70 @@
+# Description:
+# Add programmable interface to hubot. Allow to run a hubot command
+# whenever something came up in the conversation.
+#
+# Dependencies:
+# None
+#
+# Configuration:
+# None
+#
+# Commands:
+# hubot when you hear <pattern> do <something hubot does> - Setup a ear dropping event
+# hubot stop ear dropping - Stop all ear dropping
+# hubot stop ear dropping on <pattern> - Remove a particular ear dropping event
+# hubot show ear dropping - Show what hubot is ear dropping on
+#
+# Author:
+# garylin
+
+TextMessage = require('hubot').TextMessage
+
+class EarDropping
+ constructor: (@robot) ->
+ @cache = []
+ @robot.brain.on 'loaded', =>
+ if @robot.brain.data.eardropping
+ @cache = @robot.brain.data.eardropping
+ add: (pattern, action) ->
+ task = {key: pattern, task: action}
+ @cache.push task
+ @robot.brain.data.eardropping = @cache
+ all: -> @cache
+ deleteByPattern: (pattern) ->
+ @cache = @cache.filter (n) -> n.key != pattern
+ @robot.brain.data.eardropping = @cache
+ deleteAll: () ->
+ @cache = []
+ @robot.brain.data.eardropping = @cache
+
+module.exports = (robot) ->
+ earDropping = new EarDropping robot
+
+ robot.respond /when you hear (.+?) do (.+?)$/i, (msg) ->
+ key = msg.match[1]
+ task = msg.match[2]
+ earDropping.add(key, task)
+ msg.send "I am now ear dropping for #{key}. Hehe."
+
+ robot.respond /stop ear *dropping$/i, (msg) ->
+ earDropping.deleteAll()
+ msg.send 'Okay, fine. :( I am keep my ears shut.'
+
+ robot.respond /stop ear *dropping (for|on) (.+?)$/i, (msg) ->
+ pattern = msg.match[2]
+ earDropping.deleteByPattern(pattern)
+ msg.send "Okay, I will ignore #{pattern}"
+
+ robot.respond /show ear *dropping/i, (msg) ->
+ response = "\n"
+ for task in earDropping.all()
+ response += "#{task.key} -> #{task.task}\n"
+ msg.send response
+
+ robot.hear /(.+)/i, (msg) ->
+ robotHeard = msg.match[1]
+
+ for task in earDropping.all()
+ if new RegExp(task.key, "i").test(robotHeard)
+ if (robot.name != msg.message.user.name && !(new RegExp("^#{robot.name}", "i").test(robotHeard)))
+ robot.receive new TextMessage(msg.message.user, "#{robot.name}: #{task.task}")

0 comments on commit 6b1e1ec

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