Skip to content

Commit

Permalink
Add command to check for website status
Browse files Browse the repository at this point in the history
  • Loading branch information
Tom Bell committed Sep 22, 2011
1 parent 13d2098 commit 4bde98f
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 2 deletions.
15 changes: 14 additions & 1 deletion lib/bot.js

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

46 changes: 46 additions & 0 deletions lib/isitup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
(function() {
var http, isItUp;
http = require("http");
exports.isItUp = isItUp = function(url, callback) {
var opts, request;
opts = {
host: "www.isup.me",
path: "/" + url
};
request = http.request(opts, function(response) {
var data;
data = "";
response.on("data", function(chunk) {
return data += chunk;
});
response.on("end", function() {
var body, down, unknown, up;
body = "";
unknown = data.indexOf('doesn\'t look like a site on the interwho.');
down = data.indexOf('looks down from here.');
up = data.indexOf('is up.');
if (unknown !== -1) {
body = "" + url + " doesn't look like a site on the interwho.";
} else if (down !== -1) {
body = "" + url + " looks down from here.";
} else if (up !== -1) {
body = "" + url + " is up.";
} else {
body = null;
}
if (!body) {
return callback("Could not find the status for " + url);
} else {
return callback(null, body);
}
});
return response.on("error", function(error) {
return callback(error);
});
});
request.on("error", function(error) {
return callback(error);
});
return request.end();
};
}).call(this);
1 change: 0 additions & 1 deletion lib/seen.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
if (!reply.channel && !reply.time) {
return callback("not seen " + user + " yet, sorry");
} else {
console.log(reply);
return callback(null, "I last saw " + user + " speak in " + reply.channel + " " + (relative(reply.time, new Date)));
}
});
Expand Down
10 changes: 10 additions & 0 deletions src/bot.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ weather = require "./weather"
image = require "./image"
github = require "./github"
fortune = require "./fortune"
isup = require "./isitup"
seen = require "./seen"

irc_server = process.env.IRC_SERVER
Expand Down Expand Up @@ -109,6 +110,15 @@ hear /^roll me ?(\d*)/i, (message) ->
else
say message.to, "#{message.from} rolls a #{sides} sided die and gets #{Math.floor(Math.random() * sides) + 1}"

hear /^is (.*) up/, (message) ->
seen.setSeenUser message.from, message.to
url = message.match[1]
isup.isItUp url, (err, msg) ->
if err or not msg
say message.to, "#{message.from}: #{err}"
else
say message.to, "#{message.from}: #{msg}"

hear /.*/, (message) ->
seen.setSeenUser message.from, message.to

Expand Down
42 changes: 42 additions & 0 deletions src/isitup.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
http = require "http"

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

request = http.request opts, (response) ->
data = ""

response.on "data", (chunk) ->
data += chunk

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


unknown = data.indexOf 'doesn\'t look like a site on the interwho.'
down = data.indexOf 'looks down from here.'
up = data.indexOf 'is up.'

if unknown isnt -1
body = "#{url} doesn't look like a site on the interwho."
else if down isnt -1
body = "#{url} looks down from here."
else if up isnt -1
body = "#{url} is up."
else
body = null

if not body
callback "Could not find the status for #{url}"
else
callback null, body

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

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

request.end()

0 comments on commit 4bde98f

Please sign in to comment.