Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Kyle Macey committed Mar 16, 2017
0 parents commit cb4bf2c
Show file tree
Hide file tree
Showing 6 changed files with 212 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
host.key
21 changes: 21 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2015 Ben Balter

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# SSH Hubot

An SSH based [Hubot](https://hubot.github.com) adapter to allow Hubot to be accessed from an SSH client.

## Running locally

Create a new hubot with [`yo-hubot`](https://hubot.github.com/docs/)

<!-- TODO: Finish writing setup instructions -->
30 changes: 30 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"name": "hubot-sshbot",
"version": "0.0.1",
"author": "Kyle Macey <[email protected]>",
"description": "A simple helpful robot for your Company",
"dependencies": {
"blessed": "~0.1.81",
"ssh2": "~0.5.1",
"parent-require": "^1.0.0"
},
"peerDependencies": {
"hubot": "~2.19"
},
"devDependencies": {
"coffee-script": ">=1.2.0"
},
"main": "src/sshbot.coffee",
"scripts": {
"start": "bin/hubot"
},
"repository": {
"type": "git",
"url": "git+https://github.com/kylemacey/hubot-sshbot.git"
},
"license": "MIT",
"bugs": {
"url": "https://github.com/kylemacey/hubot-sshbot/issues"
},
"homepage": "https://github.com/kylemacey/hubot-sshbot#readme"
}
109 changes: 109 additions & 0 deletions src/hubot-ssh-server.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
Ssh2 = require "ssh2"
Server = Ssh2.Server
Fs = require "fs"
util = require "util"
events = require "events"
EventEmitter = events.EventEmitter

Blessed = require "blessed"
utils = Ssh2.utils

noop = (v) ->

class HubotSshServer

constructor: (receiver=noop) ->
EventEmitter.call(@)
self = @
@server = new Server hostKeys: [Fs.readFileSync(process.env.HUBOT_SSH_HOST_KEY)], (client) ->
console.log "connected!"

client
.on "authentication", (ctx) ->
ctx.accept()

.on "ready", ->
client.once "session", (accept, reject) ->
session = this

accept()
.once "pty", (accept, reject, info) ->
session.rows = info.rows
session.cols = info.cols
session.term = info.term
accept && accept()
.once "shell", (accept, reject) ->
stream = accept()

stream.name = "Hubot"
stream.rows = session.rows || 24
stream.columns = session.cols || 80
stream.isTTY = true
stream.setRawMode = noop
stream.on 'error', noop

screen = new Blessed.screen
autoPadding: true
smartCSR: true
program: new Blessed.program
input: stream
output: stream
terminal: session.term || 'ansi'

screen.title = "Welcome to Hubot!"

output = stream.output = new Blessed.log
screen: screen
top: 0
left: 0
width: '100%'
bottom: 2
scrollOnInput: true
screen.append output

screen.append new Blessed.box
screen: screen
height: 1
bottom: 1
left: 0
width: '100%'
type: 'line'
ch: '='

input = new Blessed.textbox
screen: screen
bottom: 0
height: 1
width: '100%'
inputOnFocus: true
screen.append input

input.focus()

screen.render()

stream.output.add('Welcome to the SSH Hubot!\n' +
'Try running `hubot help`\n' +
'Type `/quit` or `/exit` to exit the chat.'
)

input.on "submit", (line) ->
input.clearValue()
screen.render()
if !input.focused
input.focus()
if line
if line == "/quit" || line == "/exit"
stream.end()
stream.output.add "User: #{line}"
self.emit "message", stream, line
receiver stream, line, (response) ->
if response
stream.output.add "Hubot: #{response}"

start: (port=3050) ->
@server.listen port, "0.0.0.0", ->
console.log "Listening on #{this.address().port}"

util.inherits(HubotSshServer, EventEmitter)
module.exports = HubotSshServer
41 changes: 41 additions & 0 deletions src/sshbot.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
fs = require 'fs'
express = require 'express'
path = require 'path'
SshServer = require './hubot-ssh-server'

try
{Robot,Adapter,TextMessage,User} = require 'hubot'
catch
prequire = require('parent-require')
{Robot,Adapter,TextMessage,User} = prequire 'hubot'

class Sshbot extends Adapter

send: (envelope, strings...) ->
envelope.user.output.add('\u001b[1m' + "Hubot: #{str}" + '\u001b[22m') for str in strings

emote: (envelope, strings...) ->
@send envelope, "* #{str}" for str in strings

reply: (envelope, strings...) ->
@send envelope, strings...

root: ->
@_root ||= path.resolve __dirname, "../"

run: ->
self = @
server = new SshServer
server.start(@port())

server.on "message", (stream, msg)->
self.receive new TextMessage(stream, msg, "message-#{Date.now()}")
console.log "Received #{msg}"

@emit 'connected'

port: ->
process.env.HUBOT_SSH_PORT || 3050

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

0 comments on commit cb4bf2c

Please sign in to comment.