Skip to content
Browse files

Initial commit

  • Loading branch information...
0 parents commit b9c506d4a4c0aa160293f3e2414b545beac236ea @unixcharles committed Nov 25, 2011
Showing with 179 additions and 0 deletions.
  1. +18 −0 LICENSE
  2. +11 −0 README
  3. +23 −0 package.json
  4. +127 −0 src/talker.coffee
18 LICENSE
@@ -0,0 +1,18 @@
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
11 README
@@ -0,0 +1,11 @@
+# Hubot-talker
+
+[Talker](http://talkerapp.com) adapter for [Hubot](http://github.com/github/hubot).
+
+# Pull request?
+
+Yes.
+
+# Credit
+
+Originally contributed to hubot by [Victor Castell](https://github.com/victorcoder).
23 package.json
@@ -0,0 +1,23 @@
+{
+ "name": "hubot-talker",
+ "version": "1.0.0",
+ "author": "Charles Barbier <unixcharles@gmail.com>",
+ "keywords": "github hubot talker adapter",
+ "description": "Talker adapter for hubot",
+ "licenses": [{
+ "type": "MIT",
+ "url": "http://github.com/unixcharles/hubot-talker/raw/master/LICENSE"
+ }],
+ "repository" : {
+ "type" : "git",
+ "url" : "http://github.com/unixcharles/hubot-talker.git"
+ },
+ "dependencies": {
+ "hubot": ">= 2.0.0"
+ },
+ "devDependencies": {
+ "coffee-script": "1.1.3"
+ },
+ "main": "./src/talker",
+ "engine": "node > 0.6.0 < 0.7.0"
+}
127 src/talker.coffee
@@ -0,0 +1,127 @@
+Robot = require('hubot').robot()
+Adapter = require('hubot').adapter()
+
+HTTPS = require 'https'
+EventEmitter = require('events').EventEmitter
+net = require('net')
+tls = require('tls')
+
+class Talker extends Adapter
+ send: (user, strings...) ->
+ strings.forEach (str) =>
+ @bot.write user.room, {"type": "message", "content": str}
+
+ reply: (user, strings...) ->
+ strings.forEach (str) =>
+ @send user, "@#{user.name} #{str}"
+
+ run: ->
+ self = @
+ token = process.env.HUBOT_TALKER_TOKEN
+ rooms = process.env.HUBOT_TALKER_ROOMS.split(',')
+
+ bot = new TalkerClient()
+ console.log bot
+
+ ping = (room)->
+ setInterval ->
+ bot.write room, {type: "ping"}
+ , 25000
+
+ bot.on "Ready", (room)->
+ message = {"room": room, "token": token, "type": "connect"}
+ bot.write room, message
+ ping room
+
+ bot.on "Users", (message)->
+ for user in message.users
+ self.userForId(user.id, user)
+
+ bot.on "TextMessage", (room, message)->
+ unless self.name == message.user.name
+ # Replace "@mention" with "mention: ", case-insensitively
+ regexp = new RegExp "^@#{self.name}", 'i'
+ content = message.content.replace(regexp, "#{self.name}:")
+
+ self.receive new Robot.TextMessage self.userForMessage(room, message), content
+
+ bot.on "EnterMessage", (room, message) ->
+ unless self.name == message.user.name
+ self.receive new Robot.EnterMessage self.userForMessage(room, message)
+
+ bot.on "LeaveMessage", (room, message) ->
+ unless self.name == message.user.name
+ self.receive new Robot.LeaveMessage self.userForMessage(room, message)
+
+ for room in rooms
+ bot.sockets[room] = bot.createSocket(room)
+
+ @bot = bot
+
+ userForMessage: (room, message)->
+ author = @userForId(message.user.id, message.user)
+ author.room = room
+ author
+
+exports.use = (robot) ->
+ new Talker robot
+
+class TalkerClient extends EventEmitter
+ constructor: ->
+ @domain = 'talkerapp.com'
+ @encoding = 'utf8'
+ @port = 8500
+ @sockets = {}
+
+ createSocket: (room) ->
+ self = @
+
+ socket = tls.connect @port, @domain, ->
+ console.log("Connected to room #{room}.")
+ self.emit "Ready", room
+
+ #callback
+ socket.on 'data', (data) ->
+ for line in data.split '\n'
+ message = if line is '' then null else JSON.parse(line)
+
+ if message
+ console.log "From room #{room}: #{line}"
+ if message.type == "users"
+ self.emit "Users", message
+ if message.type == "message"
+ self.emit "TextMessage", room, message
+ if message.type == "join"
+ self.emit "EnterMessage", room, message
+ if message.type == "leave"
+ self.emit "LeaveMessage", room, message
+ if message.type == "error"
+ self.disconnect room, message.message
+
+ socket.addListener "eof", ->
+ console.log "eof"
+ socket.addListener "timeout", ->
+ console.log "timeout"
+ socket.addListener "end", ->
+ console.log "end"
+
+ socket.setEncoding @encoding
+
+ socket
+
+ write: (room, arguments) ->
+ self = @
+ @sockets[room]
+
+ if @sockets[room].readyState != 'open'
+ return @disconnect 'cannot send with readyState: ' + @sockets[room].readyState
+
+ message = JSON.stringify(arguments)
+ console.log "To room #{room}: #{message}"
+
+ @sockets[room].write message, @encoding
+
+ disconnect: (room, why) ->
+ if @sockets[room] != 'closed'
+ @sockets[room]
+ console.log 'disconnected (reason: ' + why + ')'

0 comments on commit b9c506d

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