From 55c95eac859173e060e1cc0f67fda171e7db4855 Mon Sep 17 00:00:00 2001 From: Jacob Gable Date: Mon, 11 Mar 2013 15:02:00 -0500 Subject: [PATCH] Merging and fixing notice problems --- README.md | 13 ++++++++ package.json | 4 +-- src/irc.coffee | 91 +++++++++++++++++++++++++++++--------------------- 3 files changed, 68 insertions(+), 40 deletions(-) diff --git a/README.md b/README.md index 30c6bee..3352f5b 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/package.json b/package.json index 22ac976..abd1b26 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "hubot-irc", - "version": "0.1.11", + "version": "0.1.12", "author": "Fernando Ortiz ", "description": "IRC adapter for Hubot 2.3", "keywords": "hubot irc adapter", @@ -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" }, diff --git a/src/irc.coffee b/src/irc.coffee index 2d4b319..039b96f 100644 --- a/src/irc.coffee +++ b/src/irc.coffee @@ -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 @@ -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