3
0
mirror of https://github.com/reality/dbot.git synced 2024-12-24 03:33:07 +01:00
dbot/modules/dent/dent.js

86 lines
2.4 KiB
JavaScript
Raw Normal View History

2013-01-10 18:44:01 +01:00
var request = require('request');
2013-01-14 14:10:16 +01:00
_ = require('underscore')._;
2013-01-10 18:44:01 +01:00
var dent = function(dbot) {
this.dbot = dbot;
2013-03-11 20:30:00 +01:00
this.StatusRegex = {
identica: /\bhttps?:\/\/identi\.ca\/notice\/(\d+)\b/ig,
twitter: /\bhttps?:\/\/twitter\.com\/\w+\/status\/(\d+)\b/ig
};
this.StatusAPI = {
identica: "http://identi.ca/api/statuses/show.json",
twitter: "https://api.twitter.com/1/statuses/show.json"
};
this.api = {
2013-01-14 14:10:16 +01:00
'post': function(content) {
var username = dbot.config.dent.username,
password = dbot.config.dent.password,
info,
2013-01-30 23:13:08 +01:00
auth = "Basic " +
2013-01-10 18:44:01 +01:00
new Buffer(username + ":" + password).toString("base64");
2013-01-14 14:10:16 +01:00
2013-01-10 18:44:01 +01:00
request.post({
'url': 'http://identi.ca/api/statuses/update.json?status=' +
2013-01-30 23:13:08 +01:00
escape(content),
2013-01-10 18:44:01 +01:00
'headers': {
'Authorization': auth
}
},
function(error, response, body) {
2013-01-14 14:10:16 +01:00
console.log(body);
}.bind(this));
}
};
2013-03-11 20:30:00 +01:00
this.lookup = function(event, id, service) {
request({
url: this.StatusAPI[service],
qs: {"id": id},
json: true
}, function(error, response, body) {
if (!error && response.statusCode == 200) {
if (_.has(body, 'text')) {
event.reply(service + " [" + body.user.screen_name + '] ' + body.text);
}
}
});
};
this.commands = {
2013-01-14 14:10:16 +01:00
'~dent': function(event) {
this.api.post(event.input[1]);
2013-01-14 14:10:16 +01:00
event.reply('Dent posted (probably).');
2013-01-10 18:44:01 +01:00
}
};
this.commands['~dent'].regex = [/^~dent (.+)$/, 2];
this.onLoad = function() {
2013-01-21 21:50:23 +01:00
if(dbot.config.dent.dentQuotes === true && _.has(dbot.modules, 'quotes')) {
2013-01-21 20:07:25 +01:00
dbot.api.command.addHook('~qadd', function(key, text) {
if(text.indexOf('~~') == -1) {
this.api.post(key + ': ' + text);
}
2013-01-21 20:07:25 +01:00
}.bind(this));
}
}.bind(this);
2013-03-11 20:30:00 +01:00
this.listener = function(event) {
for (s in this.StatusRegex) {
if (this.StatusRegex.hasOwnProperty(s)) {
var matches = this.StatusRegex[s].exec(event.message);
if (matches != null) {
this.lookup(event, matches[1], s);
}
}
}
}.bind(this);
this.on = 'PRIVMSG';
2013-01-10 18:44:01 +01:00
};
exports.fetch = function(dbot) {
return new dent(dbot);
2013-01-10 18:44:01 +01:00
};