Skip to content

Commit

Permalink
Merge pull request #75 from tombell/refactor
Browse files Browse the repository at this point in the history
Another refactor
  • Loading branch information
technoweenie committed Oct 27, 2011
2 parents a4a6d50 + e0589a8 commit 574e322
Show file tree
Hide file tree
Showing 12 changed files with 66 additions and 53 deletions.
14 changes: 11 additions & 3 deletions src/creator.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class Creator
#
# path - The destination
copyDefaultScripts: (path) ->
Fs.readdirSync(@scriptsDir).forEach (file) =>
for file in Fs.readdirSync(@scriptsDir)
@copy "#{@scriptsDir}/#{file}", "#{path}/#{file}"

# Run the creator process
Expand All @@ -52,7 +52,15 @@ class Creator

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

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

@copy "#{@templateDir}/#{file}", "#{@path}/#{file}" for file in files

exports.Creator = Creator
8 changes: 4 additions & 4 deletions src/hubot/campfire.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class Campfire extends Robot
bot.Me (err, data) ->
bot.info = data.user
bot.name = bot.info.name
bot.rooms.forEach (room_id) ->
for room_id in bot.rooms
bot.Room(room_id).join (err, callback) ->
bot.Room(room_id).listen()

Expand All @@ -49,7 +49,7 @@ module.exports = Campfire

class CampfireStreaming extends EventEmitter
constructor: (options) ->
if options.token? && options.rooms? && options.account?
if options.token? and options.rooms? and options.account?
@token = options.token
@rooms = options.rooms.split(",")
@account = options.account
Expand Down Expand Up @@ -169,7 +169,7 @@ class CampfireStreaming extends EventEmitter
"headers": headers

if method is "POST"
if typeof(body) != "string"
if typeof(body) isnt "string"
body = JSON.stringify body

body = new Buffer(body)
Expand All @@ -189,7 +189,7 @@ class CampfireStreaming extends EventEmitter
try
callback null, JSON.parse(data)
catch err
callback null, data || { }
callback null, data or { }
response.on "error", (err) ->
callback err, { }

Expand Down
40 changes: 22 additions & 18 deletions src/hubot/hipchat.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,22 @@ Wobot = require("wobot").Bot
class HipChat extends Robot
send: (user, strings...) ->
console.log "Sending"
strings.forEach (str) =>
for str in strings
@bot.message user.reply_to, str

reply: (user, strings...) ->
console.log "Replying"
strings.forEach (str) =>
for str in strings
@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"
name: process.env.HUBOT_HIPCHAT_NAME or "#{self.name} Bot"
password: process.env.HUBOT_HIPCHAT_PASSWORD
rooms: process.env.HUBOT_HIPCHAT_ROOMS || "@All"
rooms: process.env.HUBOT_HIPCHAT_ROOMS or "@All"

console.log "Options:", @options
bot = new Wobot(jid: @options.jid, name: @options.name, password: @options.password)
Expand All @@ -30,44 +30,48 @@ class HipChat extends Robot

bot.onConnect =>
console.log "Connected to HipChat"
if @options.rooms == "@All"
@get "/v1/rooms/list", (err, response)->
if @options.rooms is "@All"
@get "/v1/rooms/list", (err, response) ->
if response
response.rooms.forEach (room)->
for room in response.rooms
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)->
for room_id in @options.rooms.split(',')
console.log "Joining #{room_id}"
bot.join room_id
@get "/v1/users/list", (err, response)->

@get "/v1/users/list", (err, response) ->
if response
response.users.forEach (user)->
for user in response.users
self.userForId user.user_id, user
else
console.log "Can't list rooms: #{err}"
bot.onError (message)->

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 }

bot.onMessage (channel, from, message) ->
author = name: from, reply_to: channel
hubot_msg = message.replace(mention, "#{self.name}: ")
self.receive new Robot.TextMessage(author, hubot_msg)
bot.onPrivateMessage (from, message)=>

bot.onPrivateMessage (from, message) =>
user = self.userForId(from.match(/_(\d+)@/)[1])
author = { name: user.name, reply_to: from }
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
Expand All @@ -77,7 +81,7 @@ class HipChat extends Robot

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

options =
"agent" : false
Expand Down Expand Up @@ -108,7 +112,7 @@ class HipChat extends Robot
try
callback null, JSON.parse(data)
catch err
callback null, data || { }
callback null, data or { }
response.on "error", (err) ->
callback err, { }

Expand Down
10 changes: 5 additions & 5 deletions src/hubot/irc.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,12 @@ class IrcBot extends Robot

if options.nickpass?
bot.addListener 'notice', (from, to, text) ->
if from == 'NickServ' and text.indexOf('registered') != -1
if from is 'NickServ' and text.indexOf('registered') isnt -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)
else if options.nickpass and from is 'NickServ' and text.indexOf('now identified') isnt -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 Down
3 changes: 2 additions & 1 deletion src/hubot/scripts/google-images.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ module.exports = (robot) ->
msg.send url

robot.respond /(?:mo?u)?sta(?:s|c)he?(?: me)? (.*)/i, (msg) ->
imagery = msg.match[1]
imagery = msg.match[1]

if imagery.match /^https?:\/\//i
msg.send "#{mustachify}#{imagery}"
else
Expand Down
22 changes: 11 additions & 11 deletions src/hubot/scripts/maps.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,19 @@
module.exports = (robot) ->

robot.respond /(?:(satellite|terrain|hybrid)[- ])?map me (.+)/i, (msg) ->
mapType = msg.match[1] || "roadmap"
mapType = msg.match[1] or "roadmap"
location = msg.match[2]
mapUrl = "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) +
"&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"
"&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
Expand Down
2 changes: 1 addition & 1 deletion src/hubot/scripts/roles.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ module.exports = (robot) ->
if newRole not in user.roles
msg.send "I know."
else
user.roles = (role for role in user.roles when role != newRole)
user.roles = (role for role in user.roles when role isnt newRole)
msg.send "Ok, #{name} is no longer #{newRole}."

else
Expand Down
2 changes: 1 addition & 1 deletion src/hubot/scripts/rules.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ otherRules = [
module.exports = (robot) ->
robot.respond /(the rules|the laws)/i, (msg) ->
text = msg.message.text
if text.match(/apple/i) || text.match(/dev/i)
if text.match(/apple/i) or text.match(/dev/i)
msg.send otherRules.join('\n')
else
msg.send rules.join('\n')
Expand Down
2 changes: 1 addition & 1 deletion src/hubot/scripts/youtube.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,5 @@ module.exports = (robot) ->
video = msg.random videos

video.link.forEach (link) ->
if link.rel == "alternate" && link.type == "text/html"
if link.rel is "alternate" and link.type is "text/html"
msg.send link.href
4 changes: 2 additions & 2 deletions src/hubot/twilio.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class Twilio extends Robot
console.log "successful sending #{body}"

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

respond: (regex, callback) ->
Expand All @@ -37,7 +37,7 @@ class Twilio extends Robot
response.writeHead 200, 'Content-Type': 'text/plain'
response.end()

server.listen (parseInt(process.env.PORT) || 8080), "0.0.0.0"
server.listen (parseInt(process.env.PORT) or 8080), "0.0.0.0"

handle: (body, from) ->
return if body.length is 0
Expand Down
6 changes: 3 additions & 3 deletions src/hubot/xmpp.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class XmppBot extends Robot
return

# ignore non-messages
return if !stanza.is 'message' || stanza.attrs.type not in ['groupchat', 'direct', 'chat']
return if !stanza.is 'message' or stanza.attrs.type not in ['groupchat', 'direct', 'chat']

# ignore our own messages
return if @options.username in stanza.attrs.from
Expand All @@ -66,7 +66,7 @@ class XmppBot extends Robot
@receive new Robot.TextMessage user, message

send: (user, strings...) ->
strings.forEach (str) =>
for str in strings
console.log "Sending to #{user.room}: #{str}"

to = if user.type in ['direct', 'chat'] then user.room + '/' + user.id else user.room
Expand All @@ -81,7 +81,7 @@ class XmppBot extends Robot
@client.send message

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

module.exports = XmppBot
Expand Down
6 changes: 3 additions & 3 deletions src/robot.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ class Robot
@listeners = []
@loadPaths = []
@enableSlash = false
if path then @load path

@load path if path

# Public: Adds a Listener that attempts to match incoming messages based on
# a Regex.
Expand Down Expand Up @@ -76,7 +77,7 @@ class Robot
#
# Returns nothing.
receive: (message) ->
@listeners.forEach (lst) ->
for lst in @listeners
try
lst.call message
catch ex
Expand Down Expand Up @@ -246,7 +247,6 @@ class Robot.EnterMessage extends Robot.Message
# 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.
Expand Down

0 comments on commit 574e322

Please sign in to comment.