Skip to content

Commit

Permalink
Merge branch 'testssss' into redis-brain
Browse files Browse the repository at this point in the history
  • Loading branch information
technoweenie committed Oct 26, 2011
2 parents 6048ea7 + f2703cb commit d185baa
Show file tree
Hide file tree
Showing 19 changed files with 511 additions and 45 deletions.
20 changes: 20 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
v1.0.5
======

* Remove infinite loop script to keep things alive - atmos

v1.0.4
======

* HipChat adapter support - Assaf Arkin <assaf@labnotes.org>
* XMPP adapter support - Andy Fowler <andy@andyfowler.com>
* Twilio adapter fixups - Jesse Newland <jesse@jnewland.com>
* Fixup hubot-scripts.json template examples - Mike Skalnik <mike.skalnik@gmail.com>

v1.0.3
======

* Fix IRC adapter replies - Scott Moak <scott.moak@gmail.com>
* Ensure people are running node 0.4.x - Corey Donohoe <atmos@atmos.org>
* Doc fixups - Aitor García Rey <aitor@linkingpaths.com>
* Twilio adapter support - Tom Bell <tomb@tombell.org.uk>
30 changes: 28 additions & 2 deletions bin/hubot
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ Switches = [
[ "-a", "--adapter ADAPTER", "The Adapter to use"],
[ "-c", "--create PATH", "Create a deployable hubot"],
[ "-s", "--enable-slash", "Enable replacing the robot's name with '/'"],
[ "-n", "--name NAME", "The name of the robot in chat" ]
[ "-n", "--name NAME", "The name of the robot in chat" ],
[ "-v", "--version", "Displays the version of hubot installed"]
]

Options =
Expand Down Expand Up @@ -48,17 +49,42 @@ Parser.on "help", (opt, value) ->
console.log Parser.toString()
process.exit 0

Parser.on "version", (opt, value) ->
Options.version = true

Parser.parse process.ARGV

process.on 'SIGTERM', -> process.exit(0)

if Options.create
creator = new Creator.Creator(Options.path)
creator.run()

else if Options.version
package_path = __dirname + "/../package.json"

Fs.readFile package_path, (err,data) ->
if err
console.error "Could not open package file : %s", err
process.exit 1

content = JSON.parse(data.toString('ascii'))
console.log content['version']

process.exit 0

else
switch Options.adapter
when "irc"
Hubot = require("hubot/irc").IrcBot
when "campfire"
Hubot = require("hubot/campfire").Campfire
when "hipchat"
Hubot = require("hubot/hipchat").HipChat
when "twilio"
Hubot = require("hubot/twilio").Twilio
when "xmpp"
Hubot = require("hubot/xmpp").XmppBot
else
Hubot = require("hubot/shell").Shell

Expand All @@ -67,7 +93,7 @@ else
robot = new Hubot scriptsPath, Options.name
robot.enableSlash = Options.enableSlash

scriptsPath = Path.resolve "src", "hubot", "scripts"
scriptsPath = Path.resolve __dirname, "..", "src", "hubot", "scripts"
console.log "Loading hubot core scripts for relative scripts at #{scriptsPath}"
robot.load scriptsPath

Expand Down
8 changes: 5 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "hubot",
"version": "1.0.2",
"version": "1.0.6",
"author": "GitHub Inc.",
"keywords": "github hubot campfire bot",
"description": "A simple helpful Robot for your Company",
Expand All @@ -20,7 +20,9 @@
"scoped-http-client": "0.9.4",
"irc": "0.2",
"hiredis": "0.1.12",
"redis": "0.6.7"
"redis": "0.6.7",
"node-xmpp": ">=0.2.6",
"wobot": "0.3.0"
},

"directories": {
Expand All @@ -30,5 +32,5 @@
"bin": {
"hubot": "./bin/hubot"
},
"engine": "node >= 0.4.1"
"engine": "node >= 0.4.1 < 0.5.0"
}
2 changes: 1 addition & 1 deletion src/creator.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class Creator

@copyDefaultScripts("#{@path}/scripts")

["Procfile", "package.json", "README.md", ".gitignore", "bin/hubot"].forEach (file) =>
["Procfile", "package.json", "README.md", ".gitignore", "bin/hubot", "hubot-scripts.json"].forEach (file) =>
@copy "#{@templateDir}/#{file}", "#{@path}/#{file}"

exports.Creator = Creator
31 changes: 23 additions & 8 deletions src/hubot/campfire.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -45,11 +54,14 @@ exports.Campfire = Campfire

class CampfireStreaming extends EventEmitter
constructor: (options) ->
@token = options.token
@rooms = options.rooms.split(",")
@account = options.account
@domain = @account + ".campfirenow.com"
@authorization = "Basic " + new Buffer("#{@token}:x").toString("base64")
if options.token? && options.rooms? && options.account?
@token = options.token
@rooms = options.rooms.split(",")
@account = options.account
@domain = @account + ".campfirenow.com"
@authorization = "Basic " + new Buffer("#{@token}:x").toString("base64")
else
throw new Error("Not enough parameters provided. I need a token, rooms and account")

Rooms: (callback) ->
@get "/rooms", callback
Expand Down Expand Up @@ -174,7 +186,10 @@ class CampfireStreaming extends EventEmitter
data += chunk
response.on "end", ->
if response.statusCode >= 400
console.log "campfire error: #{response.statusCode}"
switch response.statusCode
when 401 then throw new Error("Invalid access token provided, campfire refused the authentication")
else console.log "campfire error: #{err}"


try
callback null, JSON.parse(data)
Expand Down
125 changes: 125 additions & 0 deletions src/hubot/hipchat.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
Robot = require "robot"
HTTPS = require "https"
Wobot = require("wobot").Bot

class HipChat extends Robot
send: (user, strings...) ->
console.log "Sending"
strings.forEach (str) =>
@bot.message user.reply_to, str

reply: (user, strings...) ->
console.log "Replying"
strings.forEach (str) =>
@send user, "@#{user.name} #{str}"

run: ->
self = @
@options =
token: process.env.HUBOT_HIPCHAT_TOKEN
jid: process.env.HUBOT_HIPCHAT_JID
name: process.env.HUBOT_HIPCHAT_NAME || "#{self.name} Bot"
password: process.env.HUBOT_HIPCHAT_PASSWORD
rooms: process.env.HUBOT_HIPCHAT_ROOMS || "@All"

console.log "Options:", @options
bot = new Wobot(jid: @options.jid, name: @options.name, password: @options.password)
mention = new RegExp("@#{@options.name.split(' ')[0]}\\b", "i")
console.log mention
console.log "Bot:", bot

bot.onConnect =>
console.log "Connected to HipChat"
if @options.rooms == "@All"
@get "/v1/rooms/list", (err, response)->
if response
response.rooms.forEach (room)->
console.log "Joining #{room.xmpp_jid}"
bot.join room.xmpp_jid
else
console.log "Can't list rooms: #{err}"
else
@options.rooms.split(',').forEach (room_id)->
console.log "Joining #{room_id}"
bot.join room_id
@get "/v1/users/list", (err, response)->
if response
response.users.forEach (user)->
self.userForId user.user_id, user
else
console.log "Can't list rooms: #{err}"
bot.onError (message)->
# If HipChat sends an error, we get the error message from XMPP.
# Otherwise, we get an Error object from the Node connection.
if message.message
console.log "Error talking to HipChat:", message.message
else
console.log "Received error from HipChat:", message
bot.onMessage (channel, from, message)->
author = { name: from, reply_to: channel }
hubot_msg = message.replace(mention, "#{self.name}: ")
self.receive new Robot.Message(author, hubot_msg)
bot.onPrivateMessage (from, message)=>
user = self.userForId(from.match(/_(\d+)@/)[1])
author = { name: user.name, reply_to: from }
self.receive new Robot.TextMessage(author, "#{self.name}: #{message}")
bot.connect()

@bot = bot


# Convenience HTTP Methods for posting on behalf of the token"d user
get: (path, callback) ->
@request "GET", path, null, callback

post: (path, body, callback) ->
@request "POST", path, body, callback

request: (method, path, body, callback) ->
console.log method, path, body
headers = { "Host": "api.hipchat.com" }

options =
"agent" : false
"host" : "api.hipchat.com"
"port" : 443
"path" : path
"method" : method
"headers": headers

if method == "POST"
body.auth_token = @options.token
body = JSON.stringify(body)
headers["Content-Type"] = "application/json"

body = new Buffer(body)
options.headers["Content-Length"] = body.length
else
options.path += "?auth_token=#{@options.token}"

request = HTTPS.request options, (response) ->
data = ""
response.on "data", (chunk) ->
data += chunk
response.on "end", ->
if response.statusCode >= 400
console.log "hipchat error: #{response.statusCode}"

try
callback null, JSON.parse(data)
catch err
callback null, data || { }
response.on "error", (err) ->
callback err, { }

if method == "POST"
request.end(body, 'binary')
else
request.end()

request.on "error", (err) ->
console.log err
console.log err.stack
callback err

exports.HipChat = HipChat
38 changes: 28 additions & 10 deletions src/hubot/irc.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,38 @@ class IrcBot extends Robot
run: ->
self = @
options =
nick: process.env.HUBOT_IRC_NICK
port: process.env.HUBOT_IRC_PORT
rooms: process.env.HUBOT_IRC_ROOMS.split(",")
server: process.env.HUBOT_IRC_SERVER
rooms: process.env.HUBOT_IRC_ROOMS.split(",")
nick: process.env.HUBOT_IRC_NICK
password: process.env.HUBOT_IRC_PASSWORD
nickpass: process.env.HUBOT_IRC_NICKSERV_PASSWORD

console.log options

bot = new Irc.Client options.server, options.nick, {
debug: true,
channels: options.rooms,
}
client_options = {
password: options.password,
debug: true,
port: options.port,
}

unless options.nickpass
client_options['channels'] = options.rooms

bot = new Irc.Client options.server, options.nick, client_options

next_id = 1
user_id = {}

if options.nickpass?
bot.addListener 'notice', (from, to, text) ->
if from == 'NickServ' and text.indexOf('registered') != -1
bot.say 'NickServ', "identify #{options.nickpass}"
else if options.nickpass and from == 'NickServ' and text.indexOf('now identified') != -1
for room in options.rooms
bot.join room, () ->
console.log('%s has joined %s', options.nick, room)

bot.addListener 'message', (from, toRoom, message) ->
console.log "From #{from} to #{toRoom}: #{message}"

Expand All @@ -36,11 +54,11 @@ class IrcBot extends Robot
user_id[from] = next_id
next_id = next_id + 1

user = new Robot.User user_id[from], from || "user", {
room: toRoom,
}
user = new Robot.User user_id[from], {
room: toRoom,
}

self.receive new Robot.Message(user, message)
self.receive new Robot.TextMessage(user, message)

bot.addListener 'error', (message) ->
console.error('ERROR: %s: %s', message.command, message.args.join(' '))
Expand Down
3 changes: 2 additions & 1 deletion src/hubot/scripts/help.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@
# help - Displays all of the help commands that Hubot knows about.

module.exports = (robot) ->
robot.hear /help$/i, (msg) ->
robot.respond /help$/i, (msg) ->
msg.send robot.helpCommands().join("\n")

12 changes: 10 additions & 2 deletions src/hubot/scripts/maps.coffee
Original file line number Diff line number Diff line change
@@ -1,16 +1,24 @@
# Interacts with the Google Maps API.
#
# map me <query> - Returns a map view of the area returned by `query`.

module.exports = (robot) ->
robot.hear /(?:(satellite|terrain|hybrid)[- ])?map me (.+)/i, (msg) ->

robot.respond /(?:(satellite|terrain|hybrid)[- ])?map me (.+)/i, (msg) ->
mapType = msg.match[1] || "roadmap"
location = msg.match[2]
url = "http://maps.google.com/maps/api/staticmap?markers=" +
mapUrl = "http://maps.google.com/maps/api/staticmap?markers=" +
escape(location) +
"&size=400x400&maptype=" +
mapType +
"&sensor=false" +
"&format=png" # So campfire knows it's an image
url = "http://maps.google.com/maps?q=" +
escape(location) +
"&hl=en&sll=37.0625,-95.677068&sspn=73.579623,100.371094&vpsrc=0&hnear=" +
escape(location) +
"&t=m&z=11"

msg.send mapUrl
msg.send url

Loading

0 comments on commit d185baa

Please sign in to comment.