Skip to content

Commit

Permalink
Merging and fixing notice problems
Browse files Browse the repository at this point in the history
  • Loading branch information
jgable committed Mar 11, 2013
1 parent 839d924 commit 55c95ea
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 40 deletions.
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,19 @@ You will need to set some environment variables to use this adapter.
% export HUBOT_IRC_NICK="hubot"
% export HUBOT_IRC_ROOMS="#hubot,#hubot-irc"
% export HUBOT_IRC_SERVER="irc.freenode.net"

### Advanced Options

The `hubot-irc` adapter has a number of configurable options based on different community contributions.

# Don't join other rooms or respond to PM's
HUBOT_IRC_PRIVATE = true

# Send messages via notice instead of say
HUBOT_IRC_SEND_NOTICE_MODE = true

# Issue an irc command once connected to the server.
HUBOT_IRC_CONNECT_COMMAND = NICKSERV blah thing1

### Testing Local Changes

Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "hubot-irc",
"version": "0.1.11",
"version": "0.1.12",
"author": "Fernando Ortiz <fortiz2k@gmail.com>",
"description": "IRC adapter for Hubot 2.3",
"keywords": "hubot irc adapter",
Expand All @@ -18,7 +18,7 @@
"url": "http://github.com/nandub/hubot-irc/issues"
},
"main": "./src/irc.coffee",
"engine": "node > 0.6.0 < 0.8.0",
"engine": "node > 0.6.0",
"dependencies": {
"irc": "0.3.6"
},
Expand Down
91 changes: 53 additions & 38 deletions src/irc.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -6,54 +6,36 @@ Irc = require 'irc'

class IrcBot extends Adapter
send: (envelope, strings...) ->
user = null
room = null
target = null
# Use @notice if SEND_NOTICE_MODE is set
return @notice envelope, strings if process.env.HUBOT_IRC_SEND_NOTICE_MODE?

# as of hubot 2.4.2, the first param to send() is an object with 'user'
# and 'room' data inside. detect the old style here.
if envelope.reply_to
user = envelope
else
# expand envelope
user = envelope.user
room = envelope.room

if user
# most common case - we're replying to a user in a room
if user.room
target = user.room
# reply directly
else if user.name
target = user.name
# replying to pm
else if user.reply_to
target = user.reply_to
# allows user to be an id string
else if user.search?(/@/) != -1
target = user
else if room
# this will happen if someone uses robot.messageRoom(jid, ...)
target = room
target = @_getTargetFromEnvelope envelope

unless target
console.log "ERROR: Not sure who to send to. envelope=", envelope
return
return console.log "ERROR: Not sure who to send to. envelope=", envelope

speak = if process.env.HUBOT_IRC_SEND_NOTICE_MODE? then "notice" else "say"
for str in strings
@bot[speak] target, str
@bot.say target, str

notice: (envelope, strings...) ->
target = @_getTargetFromEnvelope envelope

unless target
return console.log "Notice: no target found", envelope

# Flatten out strings from send
flattened = []
for str in strings
if Array.isArray str
flattened = flattened.concat str
else
flattened.push str

for str in flattened
if not str?
continue
if envelope.user.room
console.log "notice #{envelope.user.room} #{str}"
@bot.notice(envelope.user.room, str)
else
console.log "notice #{envelope.user.name} #{str}"
@bot.notice(envelope.user.name, str)

@bot.notice target, str

reply: (envelope, strings...) ->
for str in strings
Expand Down Expand Up @@ -227,5 +209,38 @@ class IrcBot extends Adapter

self.emit "connected"

_getTargetFromEnvelope: (envelope) ->
user = null
room = null
target = null

# as of hubot 2.4.2, the first param to send() is an object with 'user'
# and 'room' data inside. detect the old style here.
if envelope.reply_to
user = envelope
else
# expand envelope
user = envelope.user
room = envelope.room

if user
# most common case - we're replying to a user in a room
if user.room
target = user.room
# reply directly
else if user.name
target = user.name
# replying to pm
else if user.reply_to
target = user.reply_to
# allows user to be an id string
else if user.search?(/@/) != -1
target = user
else if room
# this will happen if someone uses robot.messageRoom(jid, ...)
target = room

target

exports.use = (robot) ->
new IrcBot robot

0 comments on commit 55c95ea

Please sign in to comment.