Skip to content

Commit 55c95ea

Browse files
committed
Merging and fixing notice problems
1 parent 839d924 commit 55c95ea

3 files changed

Lines changed: 68 additions & 40 deletions

File tree

README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,19 @@ You will need to set some environment variables to use this adapter.
3939
% export HUBOT_IRC_NICK="hubot"
4040
% export HUBOT_IRC_ROOMS="#hubot,#hubot-irc"
4141
% export HUBOT_IRC_SERVER="irc.freenode.net"
42+
43+
### Advanced Options
44+
45+
The `hubot-irc` adapter has a number of configurable options based on different community contributions.
46+
47+
# Don't join other rooms or respond to PM's
48+
HUBOT_IRC_PRIVATE = true
49+
50+
# Send messages via notice instead of say
51+
HUBOT_IRC_SEND_NOTICE_MODE = true
52+
53+
# Issue an irc command once connected to the server.
54+
HUBOT_IRC_CONNECT_COMMAND = NICKSERV blah thing1
4255

4356
### Testing Local Changes
4457

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "hubot-irc",
3-
"version": "0.1.11",
3+
"version": "0.1.12",
44
"author": "Fernando Ortiz <fortiz2k@gmail.com>",
55
"description": "IRC adapter for Hubot 2.3",
66
"keywords": "hubot irc adapter",
@@ -18,7 +18,7 @@
1818
"url": "http://github.com/nandub/hubot-irc/issues"
1919
},
2020
"main": "./src/irc.coffee",
21-
"engine": "node > 0.6.0 < 0.8.0",
21+
"engine": "node > 0.6.0",
2222
"dependencies": {
2323
"irc": "0.3.6"
2424
},

src/irc.coffee

Lines changed: 53 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -6,54 +6,36 @@ Irc = require 'irc'
66

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

13-
# as of hubot 2.4.2, the first param to send() is an object with 'user'
14-
# and 'room' data inside. detect the old style here.
15-
if envelope.reply_to
16-
user = envelope
17-
else
18-
# expand envelope
19-
user = envelope.user
20-
room = envelope.room
21-
22-
if user
23-
# most common case - we're replying to a user in a room
24-
if user.room
25-
target = user.room
26-
# reply directly
27-
else if user.name
28-
target = user.name
29-
# replying to pm
30-
else if user.reply_to
31-
target = user.reply_to
32-
# allows user to be an id string
33-
else if user.search?(/@/) != -1
34-
target = user
35-
else if room
36-
# this will happen if someone uses robot.messageRoom(jid, ...)
37-
target = room
12+
target = @_getTargetFromEnvelope envelope
3813

3914
unless target
40-
console.log "ERROR: Not sure who to send to. envelope=", envelope
41-
return
15+
return console.log "ERROR: Not sure who to send to. envelope=", envelope
4216

43-
speak = if process.env.HUBOT_IRC_SEND_NOTICE_MODE? then "notice" else "say"
4417
for str in strings
45-
@bot[speak] target, str
18+
@bot.say target, str
4619

4720
notice: (envelope, strings...) ->
21+
target = @_getTargetFromEnvelope envelope
22+
23+
unless target
24+
return console.log "Notice: no target found", envelope
25+
26+
# Flatten out strings from send
27+
flattened = []
4828
for str in strings
29+
if Array.isArray str
30+
flattened = flattened.concat str
31+
else
32+
flattened.push str
33+
34+
for str in flattened
4935
if not str?
5036
continue
51-
if envelope.user.room
52-
console.log "notice #{envelope.user.room} #{str}"
53-
@bot.notice(envelope.user.room, str)
54-
else
55-
console.log "notice #{envelope.user.name} #{str}"
56-
@bot.notice(envelope.user.name, str)
37+
38+
@bot.notice target, str
5739

5840
reply: (envelope, strings...) ->
5941
for str in strings
@@ -227,5 +209,38 @@ class IrcBot extends Adapter
227209

228210
self.emit "connected"
229211

212+
_getTargetFromEnvelope: (envelope) ->
213+
user = null
214+
room = null
215+
target = null
216+
217+
# as of hubot 2.4.2, the first param to send() is an object with 'user'
218+
# and 'room' data inside. detect the old style here.
219+
if envelope.reply_to
220+
user = envelope
221+
else
222+
# expand envelope
223+
user = envelope.user
224+
room = envelope.room
225+
226+
if user
227+
# most common case - we're replying to a user in a room
228+
if user.room
229+
target = user.room
230+
# reply directly
231+
else if user.name
232+
target = user.name
233+
# replying to pm
234+
else if user.reply_to
235+
target = user.reply_to
236+
# allows user to be an id string
237+
else if user.search?(/@/) != -1
238+
target = user
239+
else if room
240+
# this will happen if someone uses robot.messageRoom(jid, ...)
241+
target = room
242+
243+
target
244+
230245
exports.use = (robot) ->
231246
new IrcBot robot

0 commit comments

Comments
 (0)