@@ -6,54 +6,36 @@ Irc = require 'irc'
66
77class 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+
230245exports .use = (robot ) ->
231246 new IrcBot robot
0 commit comments