Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
prathamesh-sonpatki committed Jun 26, 2013
1 parent 50ceb57 commit 278c409
Show file tree
Hide file tree
Showing 21 changed files with 721 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
undefined
1 change: 1 addition & 0 deletions Procfile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
web: bin/hubot -a irc -n punerbot
7 changes: 7 additions & 0 deletions bin/hubot
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/bin/sh

npm install
export PATH="node_modules/.bin:node_modules/hubot/node_modules/.bin:$PATH"

exec node_modules/.bin/hubot "$@"

3 changes: 3 additions & 0 deletions bin/hubot.cmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
@echo off

npm install && node_modules\.bin\hubot.cmd %*
1 change: 1 addition & 0 deletions external-scripts.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[]
1 change: 1 addition & 0 deletions hubot-scripts.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
["redis-brain.coffee", "tweet.coffee", "shipit.coffee"]
36 changes: 36 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{
"name": "hosted-hubot",
"version": "2.5.5",

"author": "GitHub Inc.",

"keywords": [
"github",
"hubot",
"campfire",
"bot"
],

"description": "A simple helpful robot for your Company",

"licenses": [{
"type": "MIT",
"url": "http://github.com/github/hubot/raw/master/LICENSE"
}],

"repository" : {
"type": "git",
"url": "https://github.com/github/hubot.git"
},

"dependencies": {
"hubot-irc": ">= 0.0.1",
"hubot": "2.5.5",
"hubot-scripts": "2.4.6"
},

"engines": {
"node": ">= 0.8.x",
"npm": ">= 1.1.x"
}
}
107 changes: 107 additions & 0 deletions scripts/auth.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
# Description:
# Auth allows you to assign roles to users which can be used by other scripts
# to restrict access to Hubot commands
#
# Dependencies:
# None
#
# Configuration:
# HUBOT_AUTH_ADMIN - A comma separate list of user IDs
#
# Commands:
# hubot <user> has <role> role - Assigns a role to a user
# hubot <user> doesn't have <role> role - Removes a role from a user
# hubot what role does <user> have - Find out what roles are assigned to a specific user
# hubot who has admin role - Find out who's an admin and can assign roles
#
# Notes:
# * Call the method: robot.auth.hasRole(msg.envelope.user,'<role>')
# * returns bool true or false
#
# * the 'admin' role can only be assigned through the environment variable
# * roles are all transformed to lower case
#
# * The script assumes that user IDs will be unique on the service end as to
# correctly identify a user. Names were insecure as a user could impersonate
# a user
#
# Author:
# alexwilliamsca, tombell

module.exports = (robot) ->

unless process.env.HUBOT_AUTH_ADMIN?
robot.logger.warning 'The HUBOT_AUTH_ADMIN environment variable not set'

if process.env.HUBOT_AUTH_ADMIN?
admins = process.env.HUBOT_AUTH_ADMIN.split ','
else
admins = []

class Auth
hasRole: (user, roles) ->
user = robot.brain.userForId(user.id)
if user? and user.roles?
roles = [roles] if typeof roles is 'string'
for role in roles
return true if role in user.roles
return false

robot.auth = new Auth

robot.respond /@?(.+) (has) (["'\w: -_]+) (role)/i, (msg) ->
name = msg.match[1].trim()
newRole = msg.match[3].trim().toLowerCase()

unless name.toLowerCase() in ['', 'who', 'what', 'where', 'when', 'why']
user = robot.brain.userForName(name)
return msg.reply "#{name} does not exist" unless user?
user.roles or= []

if newRole in user.roles
msg.reply "#{name} already has the '#{newRole}' role."
else
if newRole is 'admin'
msg.reply "Sorry, the 'admin' role can only be defined in the HUBOT_AUTH_ADMIN env variable."
else
myRoles = msg.message.user.roles or []
if msg.message.user.id.toString() in admins
user.roles.push(newRole)
msg.reply "Ok, #{name} has the '#{newRole}' role."

robot.respond /@?(.+) (doesn't have|does not have) (["'\w: -_]+) (role)/i, (msg) ->
name = msg.match[1].trim()
newRole = msg.match[3].trim().toLowerCase()

unless name.toLowerCase() in ['', 'who', 'what', 'where', 'when', 'why']
user = robot.brain.userForName(name)
return msg.reply "#{name} does not exist" unless user?
user.roles or= []

if newRole is 'admin'
msg.reply "Sorry, the 'admin' role can only be removed from the HUBOT_AUTH_ADMIN env variable."
else
myRoles = msg.message.user.roles or []
if msg.message.user.id.toString() in admins
user.roles = (role for role in user.roles when role isnt newRole)
msg.reply "Ok, #{name} doesn't have the '#{newRole}' role."

robot.respond /(what role does|what roles does) @?(.+) (have)\?*$/i, (msg) ->
name = msg.match[2].trim()
user = robot.brain.userForName(name)
return msg.reply "#{name} does not exist" unless user?
user.roles or= []

if user.id.toString() in admins
isAdmin = ' and is also an admin'
else
isAdmin = ''
msg.reply "#{name} has the following roles: #{user.roles.join(', ')}#{isAdmin}."

robot.respond /who has admin role\?*$/i, (msg) ->
adminNames = []
for admin in admins
user = robot.brain.userForId(admin)
adminNames.push user.name if user?

msg.reply "The following people have the 'admin' role: #{adminNames.join(', ')}"
19 changes: 19 additions & 0 deletions scripts/events.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Description:
# Event system related utilities
#
# Commands:
# hubot fake event <event> - Triggers the <event> event for debugging reasons
#
# Events:
# debug - {user: <user object to send message to>}

util = require 'util'

module.exports = (robot) ->

robot.respond /FAKE EVENT (.*)/i, (msg) ->
msg.send "fake event '#{msg.match[1]}' triggered"
robot.emit msg.match[1], {user: msg.message.user}

robot.on 'debug', (event) ->
robot.send event.user, util.inspect event
44 changes: 44 additions & 0 deletions scripts/google-images.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# Description:
# A way to interact with the Google Images API.
#
# Commands:
# hubot image me <query> - The Original. Queries Google Images for <query> and returns a random top result.
# hubot animate me <query> - The same thing as `image me`, except adds a few parameters to try to return an animated GIF instead.
# hubot mustache me <url> - Adds a mustache to the specified URL.
# hubot mustache me <query> - Searches Google Images for the specified query and mustaches it.

module.exports = (robot) ->
robot.respond /(image|img)( me)? (.*)/i, (msg) ->
imageMe msg, msg.match[3], (url) ->
msg.send url

robot.respond /animate( me)? (.*)/i, (msg) ->
imageMe msg, msg.match[2], true, (url) ->
msg.send url

robot.respond /(?:mo?u)?sta(?:s|c)he?(?: me)? (.*)/i, (msg) ->
type = Math.floor(Math.random() * 3)
mustachify = "http://mustachify.me/#{type}?src="
imagery = msg.match[1]

if imagery.match /^https?:\/\//i
msg.send "#{mustachify}#{imagery}"
else
imageMe msg, imagery, false, true, (url) ->
msg.send "#{mustachify}#{url}"

imageMe = (msg, query, animated, faces, cb) ->
cb = animated if typeof animated == 'function'
cb = faces if typeof faces == 'function'
q = v: '1.0', rsz: '8', q: query, safe: 'active'
q.imgtype = 'animated' if typeof animated is 'boolean' and animated is true
q.imgtype = 'face' if typeof faces is 'boolean' and faces is true
msg.http('http://ajax.googleapis.com/ajax/services/search/images')
.query(q)
.get() (err, res, body) ->
images = JSON.parse(body)
images = images.responseData?.results
if images?.length > 0
image = msg.random images
cb "#{image.unescapedUrl}#.png"

80 changes: 80 additions & 0 deletions scripts/help.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
# Description:
# Generates help commands for Hubot.
#
# Commands:
# hubot help - Displays all of the help commands that Hubot knows about.
# hubot help <query> - Displays all help commands that match <query>.
#
# URLS:
# /hubot/help
#
# Notes:
# These commands are grabbed from comment blocks at the top of each file.

helpContents = (name, commands) ->

"""
<html>
<head>
<title>#{name} Help</title>
<style type="text/css">
body {
background: #d3d6d9;
color: #636c75;
text-shadow: 0 1px 1px rgba(255, 255, 255, .5);
font-family: Helvetica, Arial, sans-serif;
}
h1 {
margin: 8px 0;
padding: 0;
}
.commands {
font-size: 13px;
}
p {
border-bottom: 1px solid #eee;
margin: 6px 0 0 0;
padding-bottom: 5px;
}
p:last-child {
border: 0;
}
</style>
</head>
<body>
<h1>#{name} Help</h1>
<div class="commands">
#{commands}
</div>
</body>
</html>
"""

module.exports = (robot) ->
robot.respond /help\s*(.*)?$/i, (msg) ->
cmds = robot.helpCommands()

if msg.match[1]
cmds = cmds.filter (cmd) ->
cmd.match new RegExp(msg.match[1], 'i')

if cmds.length == 0
msg.send "No available commands match #{msg.match[1]}"
return
emit = cmds.join "\n"

unless robot.name.toLowerCase() is 'hubot'
emit = emit.replace /hubot/ig, robot.name

msg.send emit

robot.router.get "/#{robot.name}/help", (req, res) ->
cmds = robot.helpCommands().map (cmd) ->
cmd.replace(/&/g,'&amp;').replace(/</g,'&lt;').replace(/>/g,'&gt;')

emit = "<p>#{cmds.join '</p><p>'}</p>"

emit = emit.replace /hubot/ig, "<b>#{robot.name}</b>"

res.setHeader 'content-type', 'text/html'
res.end helpContents robot.name, emit
42 changes: 42 additions & 0 deletions scripts/httpd.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# Description:
# A simple interaction with the built in HTTP Daemon
#
# Dependencies:
# None
#
# Configuration:
# None
#
# Commands:
# None
#
# URLS:
# /hubot/version
# /hubot/ping
# /hubot/time
# /hubot/info
# /hubot/ip

spawn = require('child_process').spawn

module.exports = (robot) ->

robot.router.get "/hubot/version", (req, res) ->
res.end robot.version

robot.router.post "/hubot/ping", (req, res) ->
res.end "PONG"

robot.router.get "/hubot/time", (req, res) ->
res.end "Server time is: #{new Date()}"

robot.router.get "/hubot/info", (req, res) ->
child = spawn('/bin/sh', ['-c', "echo I\\'m $LOGNAME@$(hostname):$(pwd) \\($(git rev-parse HEAD)\\)"])

child.stdout.on 'data', (data) ->
res.end "#{data.toString().trim()} running node #{process.version} [pid: #{process.pid}]"
child.stdin.end()

robot.router.get "/hubot/ip", (req, res) ->
robot.http('http://ifconfig.me/ip').get() (err, r, body) ->
res.end body
26 changes: 26 additions & 0 deletions scripts/maps.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Description:
# Interacts with the Google Maps API.
#
# Commands:
# hubot map me <query> - Returns a map view of the area returned by `query`.

module.exports = (robot) ->

robot.respond /(?:(satellite|terrain|hybrid)[- ])?map me (.+)/i, (msg) ->
mapType = msg.match[1] or "roadmap"
location = msg.match[2]
mapUrl = "http://maps.google.com/maps/api/staticmap?markers=" +
escape(location) +
"&size=400x400&maptype=" +
mapType +
"&sensor=false" +
"&format=png" # So campfire knows it's an image
url = "http://maps.google.com/maps?q=" +
escape(location) +
"&hl=en&sll=37.0625,-95.677068&sspn=73.579623,100.371094&vpsrc=0&hnear=" +
escape(location) +
"&t=m&z=11"

msg.send mapUrl
msg.send url

Loading

0 comments on commit 278c409

Please sign in to comment.