Skip to content

Commit

Permalink
Update with major code clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
Tom Bell committed Oct 10, 2011
1 parent 530d68b commit 83360d8
Show file tree
Hide file tree
Showing 11 changed files with 83 additions and 67 deletions.
30 changes: 13 additions & 17 deletions lib/bot.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/image.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
path = "/ajax/services/search/images?v=1.0&safe=off&rsz=8&q=" + (encodeURI(phrase));
client = http.createClient(80, host);
headers = {
Host: "ajax.googleapis.com"
'Host': "ajax.googleapis.com"
};
request = client.request("GET", path, headers);
request.on("response", function(response) {
Expand Down
8 changes: 7 additions & 1 deletion lib/time.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
response.on("data", function(chunk) {
return data += chunk;
});
return response.on("end", function() {
response.on("end", function() {
var body, time;
body = JSON.parse(data);
if (body.data.error) {
Expand All @@ -28,6 +28,12 @@
return callback(null, "The time in " + place + " is " + time);
}
});
return response.on("error", function(error) {
return callback(error);
});
});
request.on("error", function(error) {
return callback(error);
});
return request.end();
};
Expand Down
72 changes: 48 additions & 24 deletions src/bot.coffee
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
irc = require "irc"
sys = require "sys"

weather = require "./weather"
image = require "./image"
github = require "./github"
fortune = require "./fortune"
github = require "./github"
image = require "./image"
imdb = require "./imdb"
isup = require "./isitup"
seen = require "./seen"
time = require "./time"
imdb = require "./imdb"
weather = require "./weather"

irc_server = process.env.IRC_SERVER
irc_name = process.env.IRC_NAME
Expand All @@ -30,70 +30,77 @@ hear = (pattern, callback) ->
say = (channel, message) ->
client.say channel, message

success = (message) ->
console.log "\033[01;32m#{message}\033[0m"

error = (message) ->
console.log "\033[01;31m#{message}\033[0m"

listen = ->
opts =
autoRejoin: true
channels: irc_channels
realName: irc_name
userName: irc_name
opts = autoRejoin: true, channels: irc_channels, realName: irc_name, userName: irc_name

client = new irc.Client irc_server, irc_name, opts

client.addListener "message", (from, to, message) ->
dispatch { from: from, to: to, message: message }
dispatch from: from, to: to, message: message
success "#{to}:#{from} => #{message}"

client.addListener "error", (message) ->
error message

success = (message) ->
console.log "\033[01;32m#{message}\033[0m"

error = (message) ->
console.log "\033[01;31m#{message}\033[0m"

#
# Weather command - 'weather me :location'
#
hear /^weather me (.*)/i, (message) ->
seen.setSeenUser message.from, message.to
location = message.match[1]
weather.getWeather location, (err, weather) ->
if err or not weather
error "weather:error => #{err}"
say message.to, "Could not find weather for '#{location}'"
else
say message.to, "Current: #{weather.current}"
say message.to, "Today: #{weather.today}"
say message.to, "Tomorrow: #{weather.tomorrow}"

#
# Image command - 'image me :term'
#
hear /^image me (.*)$/i, (message) ->
seen.setSeenUser message.from, message.to
phrase = message.match[1]
image.getImage phrase, (err, image) ->
if err or not image
error "image:error => #{err}"
say message.to, "Could not find an image for '#{phrase}'"
else
say message.to, image

#
# Commit command - 'commit me :user/:project'
#
hear /^commit me (.*)\/(.*)/i, (message) ->
seen.setSeenUser message.from, message.to
user = message.match[1]
proj = message.match[2]
github.getLatestCommit user, proj, (err, commit) ->
if err or not commit
error "github:error => #{err}"
say message.to, "Could not get latest commit for '#{user}/#{proj}'"
else
say message.to, "#{commit.author} commited '#{commit.message}' to #{user}/#{proj} on #{commit.date}"

#
# Fortune command - 'fortune me'
#
hear /fortune me/i, (message) ->
seen.setSeenUser message.from, message.to
fortune.getFortune (err, fortune) ->
if err or not fortune
error "fortune:error => #{err}"
say message.to, "Could not get fortune"
say message.to, "#{message.from}: #{err}"
else
say message.to, fortune
say message.to, "#{message.from}: #{fortune}"

#
# Seen command - 'seen :nick'
#
hear /^seen ([\w^_-|\{\}\[\]`\\]+)$/i, (message) ->
seen.setSeenUser message.from, message.to
user = message.match[1]
Expand All @@ -103,15 +110,20 @@ hear /^seen ([\w^_-|\{\}\[\]`\\]+)$/i, (message) ->
else
say message.to, "#{message.from}: #{msg}"

#
# Roll command - 'roll me :sides'
#
hear /^roll me ?(\d*)/i, (message) ->
seen.setSeenUser message.from, message.to
sides = parseInt message.match[1] or 6

if sides is 0
say message.to, "#{message.from} I cannot make the warp core stabalizers divide by 0!"
say message.to, "#{message.from} I cannot make the warp core stabilizers divide by 0!"
else
say message.to, "#{message.from} rolls a #{sides} sided die and gets #{Math.floor(Math.random() * sides) + 1}"

#
# Website status command - 'is :domain up'
#
hear /^is (.*) up/, (message) ->
seen.setSeenUser message.from, message.to
url = message.match[1]
Expand All @@ -121,6 +133,9 @@ hear /^is (.*) up/, (message) ->
else
say message.to, "#{message.from}: #{msg}"

#
# Time command - 'what is the time in :city'
#
hear /^what is the time in (.*)/i, (message) ->
seen.setSeenUser message.from, message.to
place = message.match[1]
Expand All @@ -130,6 +145,9 @@ hear /^what is the time in (.*)/i, (message) ->
else
say message.to, "#{message.from}: #{msg}"

#
# Movie command - 'movie me :movie_or_tv_show'
#
hear /^movie me (.*)/i, (message) ->
seen.setSeenUser message.from, message.to
query = message.match[1]
Expand All @@ -139,10 +157,16 @@ hear /^movie me (.*)/i, (message) ->
else
say message.to, "#{message.from}: #{msg}"

#
# That's what she said command
#
hear /(it's|its|it was) (long|short|hard)/i, (message) ->
seen.setSeenUser message.from, message.to
say message.to, "That's what she said!"

#
# Pull requests command - 'what are the pulls on :user/:project'
#
hear /^what are the pulls on (.*)\/(.*)/i, (message) ->
seen.setSeenUser message.from, message.to
user = message.match[1]
Expand Down
4 changes: 1 addition & 3 deletions src/fortune.coffee
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
http = require "http"

exports.getFortune = getFortune = (callback) ->
opts =
host: "www.fortunefortoday.com"
path: "/getfortuneonly.php"
opts = host: "www.fortunefortoday.com", path: "/getfortuneonly.php"

request = http.request opts, (response) ->
data = ""
Expand Down
4 changes: 1 addition & 3 deletions src/github.coffee
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
https = require "https"

exports.getLatestCommit = getLatestCommit = (user, proj, callback) ->
opts =
host: "api.github.com"
path: "/repos/#{user}/#{proj}/commits?per_page=1"
opts = host: "api.github.com", path: "/repos/#{user}/#{proj}/commits?per_page=1"

request = https.request opts, (response) ->
data = ""
Expand Down
3 changes: 1 addition & 2 deletions src/image.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ exports.getImage = getImage = (phrase, callback) ->

client = http.createClient 80, host

headers =
Host: "ajax.googleapis.com"
headers = 'Host': "ajax.googleapis.com"

request = client.request "GET", path, headers

Expand Down
4 changes: 1 addition & 3 deletions src/imdb.coffee
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
http = require 'http'

exports.getMovie = getMovie = (query, callback) ->
opts =
host: process.env.IMDB_URL
path: "/by/title/#{encodeURI query}"
opts = host: process.env.IMDB_URL, path: "/by/title/#{encodeURI query}"

request = http.request opts, (response) ->
if response.statusCode is 200
Expand Down
5 changes: 1 addition & 4 deletions src/isitup.coffee
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
http = require "http"

exports.isItUp = isItUp = (url, callback) ->
opts =
host: "www.isup.me"
path: "/#{url}"
opts = host: "www.isup.me", path: "/#{url}"

request = http.request opts, (response) ->
data = ""
Expand All @@ -13,7 +11,6 @@ exports.isItUp = isItUp = (url, callback) ->

response.on "end", ->
body = ""


unknown = data.indexOf 'doesn\'t look like a site on the interwho.'
down = data.indexOf 'looks down from here.'
Expand Down
10 changes: 7 additions & 3 deletions src/time.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@ exports.getTime = getTime = (place, callback) ->
place = place.replace("?", "")
location = encodeURI place

opts =
host: "www.worldweatheronline.com"
path: "/feed/tz.ashx?q=#{location}&format=json&key=#{API_KEY}"
opts = host: "www.worldweatheronline.com", path: "/feed/tz.ashx?q=#{location}&format=json&key=#{API_KEY}"

request = http.request opts, (response) ->
data = ""
Expand All @@ -28,4 +26,10 @@ exports.getTime = getTime = (place, callback) ->
time = body.data.time_zone[0].localtime
callback null, "The time in #{place} is #{time}"

response.on "error", (error) ->
callback error

request.on "error", (error) ->
callback error

request.end()
Loading

0 comments on commit 83360d8

Please sign in to comment.