Permalink
Browse files

alerts no go to gtalk if possible

  • Loading branch information...
1 parent 12538aa commit c18d9e1c6a5bbf602508d11b75c0931ec9465b0c @pathsny committed May 10, 2012
Showing with 45 additions and 21 deletions.
  1. +24 −15 commands.js
  2. +21 −6 gtalk.js
View
@@ -5,6 +5,7 @@ var fs = require('fs');
var parser = PEG.buildParser(fs.readFileSync('log_request.js',"ascii"));
var anidb = require('./anidb');
var email = require('./email');
+var gtalk = require('./gtalk').gtalk;
var Commands = exports.Commands = function(users, settings) {
if (!(this instanceof Commands)) return new Commands(users, settings);
@@ -312,28 +313,36 @@ var command_definitions = {
var nick = _(tokens).head();
var user = this.users.get(nick);
var email_address = this.users.getEmailAddress(nick);
+ var message = '<' + from + '> ' + _(tokens).tail().join(' ');
if (!nick || tokens.length <= 1) {
cb("alert <nick> message");
} else if (!user) {
cb("alert requires the nick of a valid user in the channel");
} else if (!email_address) {
- cb(nick + " has not configured an email address for alerts");
+ if (gtalk.tryAlert(nick, message)) cb('sent a gtalk alert to ' + nick);
+ else cb(nick + " has not configured any alert options");
} else {
- if (!this._email) {
- this._email = new email.Email({
- user: this.settings.gmail.user,
- pass: this.settings.gmail.password,
- clientName: 'Misaka Alerts'
+ gtalk.tryActiveAlert(nick, message, _(function(result){
+ if (result) {
+ cb('sent a gtalk alert to ' + nick);
+ return;
+ }
+ if (!this._email) {
+ this._email = new email.Email({
+ user: this.settings.gmail.user,
+ pass: this.settings.gmail.password,
+ clientName: 'Misaka Alerts'
+ });
+ }
+ this._email.send({
+ text: message,
+ to: nick + " <" + email_address + ">",
+ subject: "Alert Email from " + from
+ }, function(err){
+ if (err) cb("sorry an error occured");
+ else cb('sent an email alert to ' + nick);
});
- }
- this._email.send({
- text: '<' + from + '> ' + _(tokens).tail().join(' '),
- to: nick + " <" + email_address + ">",
- subject: "Alert Email from " + from
- }, function(err){
- if (err) cb("sorry an error occured");
- else cb('sent an alert to ' + nick);
- })
+ }).bind(this));
}
},
_help: "send an alert to a member of the group who has added their email address to me. alert <nick> message"
View
@@ -1,4 +1,3 @@
-var baseXmpp = require('node-xmpp');
var xmpp = require('simple-xmpp');
var _ = require('underscore');
require('./utils');
@@ -28,16 +27,28 @@ Gtalk.prototype._getId = function() {
return this._idNo;
};
-Gtalk.prototype.canAlert = function(jid, cb) {
- if (!this._configured) {
+Gtalk.prototype.tryActiveAlert = function(nick, message, cb) {
+ var gtalk_id = this._users.getGtalkId(nick);
+ if (!this._configured || !gtalk_id) {
cb(false);
return
}
- xmpp.probe(jid, function(state){
- console.log(state);
+ xmpp.probe(gtalk_id, function(state){
+ if (state !== 'online' && state !== 'dnd') cb(false)
+ else {
+ xmpp.send(gtalk_id, message);
+ cb(true);
+ }
})
};
+Gtalk.prototype.tryAlert = function(nick, message) {
+ var gtalk_id = this._users.getGtalkId(nick);
+ if (!gtalk_id || !this._configured) return false;
+ xmpp.send(gtalk_id, message);
+ return true;
+}
+
Gtalk.prototype._makeIq = function(attrs) {
var iqId = this._getId().toString();
var iq = new xmpp.Element('iq', _(attrs).extend({
@@ -102,12 +113,16 @@ Gtalk.prototype.login = function(options) {
self.relay(from, message);
});
+ xmpp.on('online', function(){
+ console.log('online on gtalk');
+ self._configured = true;
+ });
+
xmpp.on('error', function(e) {
console.error('gtalk error ' + e);
});
xmpp.on('stanza', function(s){
- console.log(s.toString());
if (s.is('iq') && self._iqHash[s.attrs['id']]) {
var fn = self._iqHash[s.attrs['id']];
delete self._iqHash[s.attrs['id']];

0 comments on commit c18d9e1

Please sign in to comment.