Skip to content

Commit 8be4372

Browse files
committed
Initial commit
0 parents  commit 8be4372

21 files changed

+886
-0
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
node_modules
2+
.DS_Store*

Procfile

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
web: bin/hubot -a campfire -n Hubot

README.md

+157
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
# Hubot
2+
3+
This is a version of GitHub's Campfire bot, hubot. He's pretty cool.
4+
5+
This version is designed to be deployed on [Heroku][heroku]. This README was generated for you by hubot to help get you started. Definitely update and improve to talk about your own instance, how to use and deploy, what functionality he has, etc!
6+
7+
[heroku]: http://www.heroku.com
8+
9+
### Testing Hubot Locally
10+
11+
You can test your hubot by running the following.
12+
13+
% bin/hubot
14+
15+
You'll see some start up output about where your scripts come from and a
16+
prompt.
17+
18+
[Sun, 04 Dec 2011 18:41:11 GMT] INFO Loading adapter shell
19+
[Sun, 04 Dec 2011 18:41:11 GMT] INFO Loading scripts from /home/tomb/Development/hubot/scripts
20+
[Sun, 04 Dec 2011 18:41:11 GMT] INFO Loading scripts from /home/tomb/Development/hubot/src/scripts
21+
Hubot>
22+
23+
Then you can interact with hubot by typing `hubot help`.
24+
25+
Hubot> hubot help
26+
27+
Hubot> animate me <query> - The same thing as `image me`, except adds a few
28+
convert me <expression> to <units> - Convert expression to given units.
29+
help - Displays all of the help commands that Hubot knows about.
30+
...
31+
32+
33+
### Scripting
34+
35+
Take a look at the scripts in the `./scripts` folder for examples.
36+
Delete any scripts you think are useless or boring. Add whatever functionality you
37+
want hubot to have. Read up on what you can do with hubot in the [Scripting Guide](https://github.com/github/hubot/blob/master/docs/scripting.md).
38+
39+
### Redis Persistence
40+
41+
If you are going to use the `redis-brain.coffee` script from `hubot-scripts`
42+
(strongly suggested), you will need to add the Redis to Go addon on Heroku which requires a verified
43+
account or you can create an account at [Redis to Go][redistogo] and manually
44+
set the `REDISTOGO_URL` variable.
45+
46+
% heroku config:set REDISTOGO_URL="..."
47+
48+
If you don't require any persistence feel free to remove the
49+
`redis-brain.coffee` from `hubot-scripts.json` and you don't need to worry
50+
about redis at all.
51+
52+
[redistogo]: https://redistogo.com/
53+
54+
## Adapters
55+
56+
Adapters are the interface to the service you want your hubot to run on. This
57+
can be something like Campfire or IRC. There are a number of third party
58+
adapters that the community have contributed. Check
59+
[Hubot Adapters][hubot-adapters] for the available ones.
60+
61+
If you would like to run a non-Campfire or shell adapter you will need to add
62+
the adapter package as a dependency to the `package.json` file in the
63+
`dependencies` section.
64+
65+
Once you've added the dependency and run `npm install` to install it you can
66+
then run hubot with the adapter.
67+
68+
% bin/hubot -a <adapter>
69+
70+
Where `<adapter>` is the name of your adapter without the `hubot-` prefix.
71+
72+
[hubot-adapters]: https://github.com/github/hubot/blob/master/docs/adapters.md
73+
74+
## hubot-scripts
75+
76+
There will inevitably be functionality that everyone will want. Instead
77+
of adding it to hubot itself, you can submit pull requests to
78+
[hubot-scripts][hubot-scripts].
79+
80+
To enable scripts from the hubot-scripts package, add the script name with
81+
extension as a double quoted string to the `hubot-scripts.json` file in this
82+
repo.
83+
84+
[hubot-scripts]: https://github.com/github/hubot-scripts
85+
86+
## external-scripts
87+
88+
Tired of waiting for your script to be merged into `hubot-scripts`? Want to
89+
maintain the repository and package yourself? Then this added functionality
90+
maybe for you!
91+
92+
Hubot is now able to load scripts from third-party `npm` packages! To enable
93+
this functionality you can follow the following steps.
94+
95+
1. Add the packages as dependencies into your `package.json`
96+
2. `npm install` to make sure those packages are installed
97+
98+
To enable third-party scripts that you've added you will need to add the package
99+
name as a double quoted string to the `external-scripts.json` file in this repo.
100+
101+
## Deployment
102+
103+
% heroku create --stack cedar
104+
% git push heroku master
105+
% heroku ps:scale app=1
106+
107+
If your Heroku account has been verified you can run the following to enable
108+
and add the Redis to Go addon to your app.
109+
110+
% heroku addons:add redistogo:nano
111+
112+
If you run into any problems, checkout Heroku's [docs][heroku-node-docs].
113+
114+
You'll need to edit the `Procfile` to set the name of your hubot.
115+
116+
More detailed documentation can be found on the
117+
[deploying hubot onto Heroku][deploy-heroku] wiki page.
118+
119+
### Deploying to UNIX or Windows
120+
121+
If you would like to deploy to either a UNIX operating system or Windows.
122+
Please check out the [deploying hubot onto UNIX][deploy-unix] and
123+
[deploying hubot onto Windows][deploy-windows] wiki pages.
124+
125+
[heroku-node-docs]: http://devcenter.heroku.com/articles/node-js
126+
[deploy-heroku]: https://github.com/github/hubot/blob/master/docs/deploying/heroku.md
127+
[deploy-unix]: https://github.com/github/hubot/blob/master/docs/deploying/unix.md
128+
[deploy-windows]: https://github.com/github/hubot/blob/master/docs/deploying/unix.md
129+
130+
## Campfire Variables
131+
132+
If you are using the Campfire adapter you will need to set some environment
133+
variables. Refer to the documentation for other adapters and the configuraiton
134+
of those, links to the adapters can be found on [Hubot Adapters][hubot-adapters].
135+
136+
Create a separate Campfire user for your bot and get their token from the web
137+
UI.
138+
139+
% heroku config:set HUBOT_CAMPFIRE_TOKEN="..."
140+
141+
Get the numeric IDs of the rooms you want the bot to join, comma delimited. If
142+
you want the bot to connect to `https://mysubdomain.campfirenow.com/room/42`
143+
and `https://mysubdomain.campfirenow.com/room/1024` then you'd add it like this:
144+
145+
% heroku config:set HUBOT_CAMPFIRE_ROOMS="42,1024"
146+
147+
Add the subdomain hubot should connect to. If you web URL looks like
148+
`http://mysubdomain.campfirenow.com` then you'd add it like this:
149+
150+
% heroku config:set HUBOT_CAMPFIRE_ACCOUNT="mysubdomain"
151+
152+
[hubot-adapters]: https://github.com/github/hubot/blob/master/docs/adapters.md
153+
154+
## Restart the bot
155+
156+
You may want to get comfortable with `heroku logs` and `heroku restart`
157+
if you're having issues.

bin/hubot

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#!/bin/sh
2+
3+
npm install
4+
export PATH="node_modules/.bin:node_modules/hubot/node_modules/.bin:$PATH"
5+
6+
exec node_modules/.bin/hubot "$@"
7+

bin/hubot.cmd

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
@echo off
2+
3+
npm install && node_modules\.bin\hubot.cmd %*

external-scripts.json

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[]

hubot-scripts.json

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
["redis-brain.coffee", "shipit.coffee"]

package.json

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
{
2+
"name": "hosted-hubot",
3+
"version": "2.7.1",
4+
"private": true,
5+
6+
"author": "GitHub Inc.",
7+
8+
"keywords": [
9+
"github",
10+
"hubot",
11+
"campfire",
12+
"bot"
13+
],
14+
15+
"description": "A simple helpful robot for your Company",
16+
17+
"licenses": [{
18+
"type": "MIT",
19+
"url": "https://github.com/github/hubot/raw/master/LICENSE"
20+
}],
21+
22+
"repository" : {
23+
"type": "git",
24+
"url": "https://github.com/github/hubot.git"
25+
},
26+
27+
"dependencies": {
28+
"hubot": ">= 2.6.0 < 3.0.0",
29+
"hubot-scripts": ">= 2.5.0 < 3.0.0"
30+
},
31+
32+
"engines": {
33+
"node": ">= 0.8.x",
34+
"npm": ">= 1.1.x"
35+
}
36+
}

scripts/auth.coffee

+120
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
# Description:
2+
# Auth allows you to assign roles to users which can be used by other scripts
3+
# to restrict access to Hubot commands
4+
#
5+
# Dependencies:
6+
# None
7+
#
8+
# Configuration:
9+
# HUBOT_AUTH_ADMIN - A comma separate list of user IDs
10+
#
11+
# Commands:
12+
# hubot <user> has <role> role - Assigns a role to a user
13+
# hubot <user> doesn't have <role> role - Removes a role from a user
14+
# hubot what role does <user> have - Find out what roles are assigned to a specific user
15+
# hubot who has admin role - Find out who's an admin and can assign roles
16+
#
17+
# Notes:
18+
# * Call the method: robot.auth.hasRole(msg.envelope.user,'<role>')
19+
# * returns bool true or false
20+
#
21+
# * the 'admin' role can only be assigned through the environment variable
22+
# * roles are all transformed to lower case
23+
#
24+
# * The script assumes that user IDs will be unique on the service end as to
25+
# correctly identify a user. Names were insecure as a user could impersonate
26+
# a user
27+
#
28+
# Author:
29+
# alexwilliamsca, tombell
30+
31+
module.exports = (robot) ->
32+
33+
unless process.env.HUBOT_AUTH_ADMIN?
34+
robot.logger.warning 'The HUBOT_AUTH_ADMIN environment variable not set'
35+
36+
if process.env.HUBOT_AUTH_ADMIN?
37+
admins = process.env.HUBOT_AUTH_ADMIN.split ','
38+
else
39+
admins = []
40+
41+
class Auth
42+
hasRole: (user, roles) ->
43+
user = robot.brain.userForId(user.id)
44+
if user? and user.roles?
45+
roles = [roles] if typeof roles is 'string'
46+
for role in roles
47+
return true if role in user.roles
48+
return false
49+
50+
usersWithRole: (role) ->
51+
users = []
52+
for own key, user of robot.brain.data.users
53+
if robot.auth.hasRole(msg.envelope.user, role)
54+
users.push(user)
55+
users
56+
57+
robot.auth = new Auth
58+
59+
robot.respond /@?(.+) (has) (["'\w: -_]+) (role)/i, (msg) ->
60+
name = msg.match[1].trim()
61+
newRole = msg.match[3].trim().toLowerCase()
62+
63+
unless name.toLowerCase() in ['', 'who', 'what', 'where', 'when', 'why']
64+
user = robot.brain.userForName(name)
65+
return msg.reply "#{name} does not exist" unless user?
66+
user.roles or= []
67+
68+
if newRole in user.roles
69+
msg.reply "#{name} already has the '#{newRole}' role."
70+
else
71+
if newRole is 'admin'
72+
msg.reply "Sorry, the 'admin' role can only be defined in the HUBOT_AUTH_ADMIN env variable."
73+
else
74+
myRoles = msg.message.user.roles or []
75+
if msg.message.user.id.toString() in admins
76+
user.roles.push(newRole)
77+
msg.reply "Ok, #{name} has the '#{newRole}' role."
78+
79+
robot.respond /@?(.+) (doesn't have|does not have) (["'\w: -_]+) (role)/i, (msg) ->
80+
name = msg.match[1].trim()
81+
newRole = msg.match[3].trim().toLowerCase()
82+
83+
unless name.toLowerCase() in ['', 'who', 'what', 'where', 'when', 'why']
84+
user = robot.brain.userForName(name)
85+
return msg.reply "#{name} does not exist" unless user?
86+
user.roles or= []
87+
88+
if newRole is 'admin'
89+
msg.reply "Sorry, the 'admin' role can only be removed from the HUBOT_AUTH_ADMIN env variable."
90+
else
91+
myRoles = msg.message.user.roles or []
92+
if msg.message.user.id.toString() in admins
93+
user.roles = (role for role in user.roles when role isnt newRole)
94+
msg.reply "Ok, #{name} doesn't have the '#{newRole}' role."
95+
96+
robot.respond /(what role does|what roles does) @?(.+) (have)\?*$/i, (msg) ->
97+
name = msg.match[2].trim()
98+
user = robot.brain.userForName(name)
99+
return msg.reply "#{name} does not exist" unless user?
100+
user.roles or= []
101+
displayRoles = user.roles
102+
103+
if user.id.toString() in admins
104+
displayRoles.push('admin')
105+
106+
if displayRoles.length == 0
107+
msg.reply "#{name} has no roles."
108+
else
109+
msg.reply "#{name} has the following roles: #{displayRoles.join(', ')}."
110+
111+
robot.respond /who has admin role\?*$/i, (msg) ->
112+
adminNames = []
113+
for admin in admins
114+
user = robot.brain.userForId(admin)
115+
adminNames.push user.name if user?
116+
117+
if adminNames.length > 0
118+
msg.reply "The following people have the 'admin' role: #{adminNames.join(', ')}"
119+
else
120+
msg.reply "There are no people that have the 'admin' role."

scripts/events.coffee

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Description:
2+
# Event system related utilities
3+
#
4+
# Commands:
5+
# hubot fake event <event> - Triggers the <event> event for debugging reasons
6+
#
7+
# Events:
8+
# debug - {user: <user object to send message to>}
9+
10+
util = require 'util'
11+
12+
module.exports = (robot) ->
13+
14+
robot.respond /FAKE EVENT (.*)/i, (msg) ->
15+
msg.send "fake event '#{msg.match[1]}' triggered"
16+
robot.emit msg.match[1], {user: msg.message.user}
17+
18+
robot.on 'debug', (event) ->
19+
robot.send event.user, util.inspect event

0 commit comments

Comments
 (0)