Browse files

Merge pull request #129 from Mange/master

Correctly replace @mentions in Talker adapter
  • Loading branch information...
2 parents 21f7983 + fd0887f commit b63e5e37cd7de7e769312db089f0ebc8368d1dce @atmos atmos committed Oct 31, 2011
Showing with 27 additions and 18 deletions.
  1. +27 −18 src/hubot/talker.coffee
View
45 src/hubot/talker.coffee
@@ -22,13 +22,13 @@ class Talker extends Robot
console.log options
bot = new TalkerClient(options)
console.log bot
-
+
bot.connect ->
options.rooms.split(',').forEach (room) ->
console.log "Entering room: " + room
bot.write {"room": room, "token": options.token, "type": "connect"}
-
- setInterval ->
+
+ setInterval ->
bot.write {type: "ping"}
, 25000
@@ -38,21 +38,30 @@ class Talker extends Robot
bot.on "TextMessage", (message)->
console.log message
+
author = self.userForId(message.user.id, message.user)
- self.receive new Robot.TextMessage(author, message.content.replace(/^\s*@hubot\s+/, "Hubot: "))
-
+
+ # Replace "@mention" with "mention: ", case-insensitively
+ regexp = new RegExp("\\b@#{self.quoteRegex(self.name)}\\b", 'i')
+ content = message.content.replace(regexp, "#{self.name}: ")
+
+ self.receive new Robot.TextMessage(author, content)
+
bot.on "EnterMessage", (message) ->
console.log message
author = self.userForId(message.user.id, message.user)
self.receive new Robot.EnterMessage(author)
-
+
bot.on "LeaveMessage", (message) ->
console.log message
author = self.userForId(message.user.id, message.user)
self.receive new Robot.LeaveMessage(author)
-
+
@bot = bot
+ quoteRegex: (string) ->
+ string.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&")
+
module.exports = Talker
class TalkerClient extends EventEmitter
@@ -64,14 +73,14 @@ class TalkerClient extends EventEmitter
@port = 8500
connect: (callback) ->
- self = @
-
+ self = @
+
@socket = tls.connect @port, @domain, ->
# callback called only after successful socket socket
console.log "Connected to " + self.domain
self.socket.setEncoding @encoding
callback()
-
+
#callback
@socket.addListener 'data', (data) ->
for line in data.split '\n'
@@ -96,26 +105,26 @@ class TalkerClient extends EventEmitter
if message.type == "error"
self.disconnect message.message
-
+
@socket.addListener "eof", ->
console.log "eof"
@socket.addListener "timeout", ->
console.log "timeout"
@socket.addListener "end", ->
console.log "end"
-
+
write: (arguments) ->
self = @
-
+
if @socket.readyState != 'open'
return self.disconnect 'cannot send with readyState: ' + @socket.readyState
-
+
message = JSON.stringify(arguments)
console.log message
-
+
@socket.write message, @encoding
-
+
disconnect: (why) ->
- if @socket.readyState != 'closed'
+ if @socket.readyState != 'closed'
@socket.close
- console.log 'disconnected (reason: ' + why + ')'
+ console.log 'disconnected (reason: ' + why + ')'

0 comments on commit b63e5e3

Please sign in to comment.