Permalink
Browse files

Merge remote-tracking branch 'origin/new-adapters'

  • Loading branch information...
2 parents 637ffdd + 3cd3844 commit 63f16af47017087907c84558e877e42e2c96e4c9 @atmos atmos committed Nov 7, 2011
View
@@ -13,22 +13,20 @@ Creator = require '../src/creator'
OptParse = require 'optparse'
PortNumber = parseInt(process.env.PORT) || 8080
-Adapters = ["irc", "campfire", "hipchat", "twilio", "xmpp", "groupme", "talker", "twitter", "flowdock", "gtalk"]
-
Switches = [
- [ "-h", "--help", "Display the help information"],
- [ "-a", "--adapter ADAPTER", "The Adapter to use"],
- [ "-c", "--create PATH", "Create a deployable hubot"],
- [ "-s", "--enable-slash", "Enable replacing the robot's name with '/'"],
- [ "-n", "--name NAME", "The name of the robot in chat" ],
- [ "-v", "--version", "Displays the version of hubot installed"]
+ [ "-h", "--help", "Display the help information"],
+ [ "-a", "--adapter ADAPTER", "The Adapter to use"],
+ [ "-c", "--create PATH", "Create a deployable hubot"],
+ [ "-s", "--enable-slash", "Enable replacing the robot's name with '/'"],
+ [ "-n", "--name NAME", "The name of the robot in chat" ],
+ [ "-v", "--version", "Displays the version of hubot installed"]
]
Options =
path: "."
name: "Hubot"
create: false
- adapter: "stdio"
+ adapter: "shell"
Parser = new OptParse.OptionParser(Switches)
Parser.banner = "Usage hubot [options]"
@@ -75,31 +73,25 @@ else if Options.version
process.exit 0
else
- adapter = if Options.adapter in Adapters
- Options.adapter
- else
- 'shell'
+ adapterPath = Path.resolve "src", "adapters"
+ robot = Hubot.loadBot adapterPath, Options.adapter, Options.name
+ robot.enableSlash = Options.enableSlash
- scriptsPath = Path.resolve "./scripts"
+ scriptsPath = Path.resolve ".", "scripts"
console.log "Loading deploy-local scripts at #{scriptsPath}"
- robot = Hubot.loadBot adapter, scriptsPath, Options.name
- robot.enableSlash = Options.enableSlash
+ robot.load scriptsPath
- scriptsPath = Path.resolve "src", "hubot", "scripts"
+ scriptsPath = Path.resolve "src", "scripts"
console.log "Loading hubot core scripts for relative scripts at #{scriptsPath}"
robot.load scriptsPath
scriptsFile = Path.resolve "hubot-scripts.json"
Path.exists scriptsFile, (exists) =>
if exists
console.log "Loading hubot-scripts from #{scriptsFile}"
- Fs.readFile scriptsFile, (err, data) =>
- JSON.parse(data).forEach (plugin) =>
+ Fs.readFile scriptsFile, (err, data) ->
+ for plugin in JSON.parse(data)
robot.loadFile Path.resolve("node_modules", "hubot-scripts", "src", "scripts"), plugin
- else
- scriptsPath = Path.resolve "src", "scripts"
- console.log "Loading hubot-scripts plugins at #{scriptsPath}"
- robot.load scriptsPath
robot.run()
View
@@ -1,12 +1,12 @@
# This is the Hubot Loading Bay. NPM uses it as an entry point.
#
# Hubot = require 'hubot'
-# YourBot = Hubot.robot 'campfire', 'blah', 'yourbot'
+# YourBot = Hubot.robot 'campfire', 'yourbot'
# Loads a Hubot robot
-exports.loadBot = (adapterName, path, botName) ->
- robot = require "./src/hubot/#{adapterName}"
- new robot path, botName
+exports.loadBot = (adapterName, botName) ->
+ robot = require './src/robot'
+ new robot adapterName, botName
exports.robot = ->
require './src/robot'
View
@@ -18,11 +18,6 @@
"coffee-script": "1.1.2",
"optparse": "1.0.3",
"scoped-http-client": "0.9.4",
- "irc": "0.3.2",
- "node-xmpp": "0.2.11",
- "wobot": "0.3.0",
- "imap": "0.2.5",
- "flowdock": "0.2.2"
},
"main": "./index",
@@ -2,7 +2,7 @@ Robot = require "../robot"
HTTPS = require "https"
EventEmitter = require("events").EventEmitter
-class Campfire extends Robot
+class Campfire extends Robot.Adapter
send: (user, strings...) ->
if strings.length > 0
@bot.Room(user.room).speak strings.shift(), (err, data) =>
@@ -14,6 +14,7 @@ class Campfire extends Robot
run: ->
self = @
+
options =
token: process.env.HUBOT_CAMPFIRE_TOKEN
rooms: process.env.HUBOT_CAMPFIRE_ROOMS
@@ -25,8 +26,8 @@ class Campfire extends Robot
bot.User user, (err, userData) ->
if userData.user
author = self.userForId(userData.user.id, userData.user)
- self.brain.data.users[userData.user.id].name = userData.user.name
- self.brain.data.users[userData.user.id].email_address = userData.user.email_address
+ self.robot.brain.data.users[userData.user.id].name = userData.user.name
+ self.robot.brain.data.users[userData.user.id].email_address = userData.user.email_address
author.room = room
callback id, created, room, user, body, author
@@ -192,7 +193,6 @@ class CampfireStreaming extends EventEmitter
when 401 then throw new Error("Invalid access token provided, campfire refused the authentication")
else console.log "campfire error: #{response.statusCode}"
-
try
callback null, JSON.parse(data)
catch err
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
@@ -1,7 +1,7 @@
Robot = require "../robot"
Irc = require "irc"
-class IrcBot extends Robot
+class IrcBot extends Robot.Adapter
send: (user, strings...) ->
for str in strings
if user.room
@@ -28,7 +28,7 @@ class IrcBot extends Robot
self = @
options =
- nick: process.env.HUBOT_IRC_NICK or @name
+ nick: process.env.HUBOT_IRC_NICK or @robot.name
port: process.env.HUBOT_IRC_PORT
rooms: process.env.HUBOT_IRC_ROOMS.split(",")
server: process.env.HUBOT_IRC_SERVER
@@ -38,18 +38,14 @@ class IrcBot extends Robot
unflood: process.env.HUBOT_IRC_UNFLOOD or false
debug: process.env.HUBOT_IRC_DEBUG or false
-
- console.log options
-
- client_options = {
- password: options.password,
- debug: true,
- port: options.port,
- stripColors: true,
- secure: if options.port is "6697" then true else false,
- selfSigned: options.fakessl,
- floodProtection: options.unflood
- }
+ client_options =
+ password: options.password,
+ debug: options.debug,
+ port: options.port,
+ stripColors: true,
+ secure: if options.port is "6697" then true else false,
+ selfSigned: options.fakessl,
+ floodProtection: options.unflood
unless options.nickpass
client_options['channels'] = options.rooms
@@ -1,6 +1,6 @@
Robot = require '../robot'
-class Shell extends Robot
+class Shell extends Robot.Adapter
send: (user, strings...) ->
for str in strings
console.log str
@@ -12,7 +12,7 @@ class Shell extends Robot
run: ->
console.log "Hubot: the Shell."
- user = @userForId('1', {name: "Shell"})
+ user = @userForId('1', { name: "Shell" })
process.stdin.resume()
process.stdin.on 'data', (txt) =>
@@ -21,9 +21,9 @@ class Shell extends Robot
@receive new Robot.TextMessage user, line
setTimeout =>
- user = @userForId('1', {name: "Shell"})
- atmos = @userForId('2', {name: "atmos"})
- holman = @userForId('3', {name: "Zach Holman"})
+ user = @userForId('1', { name: "Shell" })
+ atmos = @userForId('2', { name: "atmos" })
+ holman = @userForId('3', { name: "Zach Holman" })
, 3000
module.exports = Shell
File renamed without changes.
@@ -2,7 +2,7 @@ Robot = require '../robot'
HTTP = require 'http'
QS = require 'querystring'
-class Twilio extends Robot
+class Twilio extends Robot.Adapter
constructor: ->
@sid = process.env.HUBOT_SMS_SID
@token = process.env.HUBOT_SMS_TOKEN
File renamed without changes.
File renamed without changes.
View
@@ -8,7 +8,7 @@ class Robot
# dispatch them to matching listeners.
#
# path - String directory full of Hubot scripts to load.
- constructor: (path, name = "Hubot") ->
+ constructor: (adapterPath, adapter, name = "Hubot") ->
@name = name
@brain = new Robot.Brain
@commands = []
@@ -17,7 +17,8 @@ class Robot
@loadPaths = []
@enableSlash = false
- @load path if path
+ Adapter = require "#{adapterPath}/#{adapter}"
+ @adapter = new Adapter @
# Public: Adds a Listener that attempts to match incoming messages based on
# a Regex.
@@ -128,6 +129,39 @@ class Robot
continue if !line.match('-')
@commands.push line[2..line.length]
+ # Public: Get an Array of User objects stored in the brain.
+ users: ->
+ @brain.data.users
+
+ # Public: Get a User object given a unique identifier
+ userForId: (id, options) ->
+ user = @brain.data.users[id]
+ unless user
+ user = new Robot.User id, options
+ @brain.data.users[id] = user
+ user
+
+ # Public: Get a User object given a name
+ userForName: (name) ->
+ result = null
+ lowerName = name.toLowerCase()
+ for k of (@brain.data.users or { })
+ if @brain.data.users[k]['name'].toLowerCase() is lowerName
+ result = @brain.data.users[k]
+ result
+
+ # Public: Run Hubot using the loaded adapter.
+ #
+ # Returns nothing.
+ run: ->
+ @adapter.run()
+
+class Robot.Adapter
+ # An adapter is a specific interface to a chat source for robots.
+ #
+ # robot - A Robot instance.
+ constructor: (@robot) ->
+
# Public: Raw method for sending data back to the chat source. Extend this.
#
# user - A Robot.User instance.
@@ -154,29 +188,28 @@ class Robot
# Public: Raw method for shutting the bot down.
# Extend this.
close: ->
- @brain.close()
+ @robot.brain.close()
- users: () ->
- @brain.data.users
+ # Public: Dispatch a received message to the robot.
+ #
+ # message - A TextMessage instance of the received message.
+ #
+ # Returns nothing.
+ receive: (message) ->
+ @robot.receive message
+
+ # Public: Get an Array of User objects stored in the brain.
+ users: ->
+ @robot.users
# Public: Get a User object given a unique identifier
- #
userForId: (id, options) ->
- user = @brain.data.users[id]
- unless user
- user = new Robot.User id, options
- @brain.data.users[id] = user
- user
+ @robot.userForId id, options
# Public: Get a User object given a name
- #
userForName: (name) ->
- result = null
- lowerName = name.toLowerCase()
- for k of (@brain.data.users or { })
- if @brain.data.users[k]['name'].toLowerCase() is lowerName
- result = @brain.data.users[k]
- result
+ @robot.userForName name
+
# Public: Creates a scoped http client with chainable methods for
# modifying the request. This doesn't actually make a request though.
@@ -229,14 +262,25 @@ class Robot.Brain extends EventEmitter
@resetSaveInterval 5
+ # Emits the 'save' event so that 'brain' scripts can handle persisting.
+ #
+ # Returns nothing.
save: ->
@emit 'save', @data
+ # Emits the 'close' event so that 'brain' scripts can handle closing.
+ #
+ # Returns nothing.
close: ->
clearInterval @saveInterval
@save()
@emit 'close'
+ # Reset the interval between save function calls.
+ #
+ # seconds - An Integer of seconds between saves.
+ #
+ # Returns nothing.
resetSaveInterval: (seconds) ->
clearInterval @saveInterval if @saveInterval
@saveInterval = setInterval =>
@@ -337,7 +381,7 @@ class Robot.Response
#
# Returns nothing.
send: (strings...) ->
- @robot.send @message.user, strings...
+ @robot.adapter.send @message.user, strings...
# Public: Posts a topic changing message
#
@@ -346,7 +390,7 @@ class Robot.Response
#
# Returns nothing.
topic: (strings...) ->
- @robot.topic @message.user, strings...
+ @robot.adapter.topic @message.user, strings...
# Public: Posts a message mentioning the current user.
#
@@ -355,7 +399,7 @@ class Robot.Response
#
# Returns nothing.
reply: (strings...) ->
- @robot.reply @message.user, strings...
+ @robot.adapter.reply @message.user, strings...
# Public: Picks a random item from the given items.
#
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
View
@@ -0,0 +1,21 @@
+Robot = require '../src/robot'
+
+class Danger extends Robot.Adapter
+ send: (user, strings...) ->
+ strings.forEach (str) =>
+ @sent.push str
+ @cb? strings...
+
+ reply: (user, strings...) ->
+ strings.forEach (str) =>
+ @send user, "#{@name}: #{str}"
+
+ run: ->
+ console.log "Hubot: The Danger Room."
+
+ receive: (text) ->
+ user = new Robot.User 1, 'helper'
+ super new Robot.TextMessage user, text
+
+module.exports = Danger
+
Oops, something went wrong.

0 comments on commit 63f16af

Please sign in to comment.