1
0
mirror of https://github.com/moparisthebest/kaiwa synced 2024-11-16 14:25:22 -05:00
kaiwa/clientapp/helpers/xmppEventHandlers.js

292 lines
8.2 KiB
JavaScript
Raw Normal View History

2013-08-29 23:38:28 -04:00
/*global XMPP, me, app, client*/
"use strict";
2013-08-20 13:45:06 -04:00
var crypto = XMPP.crypto;
var _ = require('underscore');
var async = require('async');
2013-09-03 18:25:14 -04:00
var log = require('andlog');
var uuid = require('node-uuid');
2013-08-29 23:38:28 -04:00
var Contact = require('../models/contact');
var Resource = require('../models/resource');
var Message = require('../models/message');
2013-08-20 13:45:06 -04:00
var discoCapsQueue = async.queue(function (pres, cb) {
var jid = pres.from;
var caps = pres.caps;
log.debug('Checking storage for ' + caps.ver);
var contact = me.getContact(jid);
var resource = null;
if (contact) {
resource = contact.resources.get(jid);
}
app.storage.disco.get(caps.ver, function (err, existing) {
log.debug(err, existing);
if (existing) {
log.debug('Already found info for ' + caps.ver);
if (resource) resource.discoInfo = existing;
return cb();
}
log.debug('getting info for ' + caps.ver + ' from ' + jid);
client.getDiscoInfo(jid, caps.node + '#' + caps.ver, function (err, result) {
log.debug(caps.ver, err, result);
if (err) {
log.debug('Couldnt get info for ' + caps.ver);
return cb();
}
if (client.verifyVerString(result.discoInfo, caps.hash, caps.ver)) {
log.debug('Saving info for ' + caps.ver);
var data = result.discoInfo.toJSON();
app.storage.disco.add(caps.ver, data, function () {
if (resource) resource.discoInfo = existing;
cb();
});
} else {
log.debug('Couldnt verify info for ' + caps.ver + ' from ' + jid);
cb();
}
});
});
});
2013-08-20 13:45:06 -04:00
module.exports = function (client, app) {
client.on('*', function (name, data) {
2013-09-03 18:25:14 -04:00
log.debug(name, data);
2013-08-20 13:45:06 -04:00
});
2013-09-05 19:53:23 -04:00
client.on('credentials:update', function (creds) {
client.config.credentials = creds;
if (creds.clientKey && creds.serverKey) {
delete creds.password;
delete creds.saltedPassword;
} else if (creds.saltedPassword) {
delete creds.password;
}
localStorage.config = JSON.stringify({
jid: client.config.jid,
server: client.config.server,
wsURL: client.config.wsURL,
credentials: creds
});
});
client.on('disconnected', function () {
me.connected = false;
if (!app.hasConnected) {
window.location = '/login';
}
2013-09-05 19:53:23 -04:00
});
client.on('auth:failed', function () {
log.debug('auth failed');
2013-09-05 19:53:23 -04:00
window.location = '/login';
});
2013-09-12 01:02:54 -04:00
client.on('stream:management:resumed', function () {
me.connected = true;
});
2013-08-20 13:45:06 -04:00
client.on('session:started', function (jid) {
me.jid = jid;
me.connected = true;
2013-09-05 19:53:23 -04:00
2013-08-20 13:45:06 -04:00
client.getRoster(function (err, resp) {
resp = resp.toJSON();
2013-08-29 23:38:28 -04:00
2013-09-10 03:57:01 -04:00
app.storage.rosterver.set(me.jid.bare, resp.roster.ver);
2013-08-29 23:38:28 -04:00
2013-08-20 13:45:06 -04:00
_.each(resp.roster.items, function (item) {
2013-08-29 23:38:28 -04:00
me.setContact(item, true);
2013-08-20 13:45:06 -04:00
});
var caps = client.updateCaps();
app.storage.disco.add(caps.ver, caps.discoInfo, function () {
client.sendPresence({
caps: client.disco.caps
});
client.enableCarbons();
2013-08-20 13:45:06 -04:00
});
});
});
client.on('roster:update', function (iq) {
2013-08-29 23:38:28 -04:00
iq = iq.toJSON();
var items = iq.roster.items;
2013-09-10 03:57:01 -04:00
app.storage.rosterver.set(me.jid.bare, iq.roster.ver);
2013-08-29 23:38:28 -04:00
2013-08-20 13:45:06 -04:00
_.each(items, function (item) {
var contact = me.getContact(item.jid);
if (item.subscription === 'remove') {
if (contact) {
2013-08-29 23:38:28 -04:00
me.removeContact(contact);
2013-08-20 13:45:06 -04:00
}
return;
}
2013-09-10 03:57:01 -04:00
me.setContact(item, true);
2013-08-20 13:45:06 -04:00
});
});
client.on('available', function (pres) {
pres = pres.toJSON();
var contact = me.getContact(pres.from);
if (contact) {
delete pres.id;
pres.show = pres.show || '';
pres.status = pres.status || '';
pres.priority = pres.priority || 0;
var resource = contact.resources.get(pres.from);
if (resource) {
2013-09-10 03:57:01 -04:00
pres.from = pres.from.full;
2013-08-20 13:45:06 -04:00
resource.set(pres);
} else {
resource = new Resource(pres);
resource.id = pres.from.full;
2013-08-20 13:45:06 -04:00
contact.resources.add(resource);
2013-09-13 02:50:27 -04:00
if (!pres.caps) {
resource.fetchDisco();
}
resource.fetchTimezone();
2013-08-20 13:45:06 -04:00
}
}
});
client.on('unavailable', function (pres) {
pres = pres.toJSON();
var contact = me.getContact(pres.from);
if (contact) {
2013-09-10 03:57:01 -04:00
var resource = contact.resources.get(pres.from.full);
2013-08-20 13:45:06 -04:00
if (resource) {
contact.resources.remove(resource);
}
if (contact.resources.length === 0) {
contact.offlineStatus = pres.status;
}
}
});
client.on('avatar', function (info) {
var contact = me.getContact(info.jid);
if (contact) {
2013-08-29 23:38:28 -04:00
var id = '';
var type = 'image/png';
2013-08-20 13:45:06 -04:00
if (info.avatars.length > 0) {
2013-08-29 23:38:28 -04:00
id = info.avatars[0].id;
type = info.avatars[0].type || 'image/png';
2013-08-20 13:45:06 -04:00
}
2013-08-29 23:38:28 -04:00
contact.setAvatar(id, type);
2013-08-20 13:45:06 -04:00
}
});
client.on('chatState', function (info) {
var contact = me.getContact(info.from);
if (contact) {
contact.chatState = info.chatState;
if (info.chatState === 'gone') {
contact.lockedResource = undefined;
}
} else if (me.isMe(info.from)) {
if (info.chatState === 'active' || info.chatState === 'composing') {
contact = me.getContact(info.to);
if (contact) {
contact.unreadCount = 0;
}
}
2013-08-20 13:45:06 -04:00
}
});
client.on('chat', function (msg) {
msg = msg.toJSON();
var contact = me.getContact(msg.from, msg.to);
if (contact && !msg.replace) {
if (!msg.id) {
msg.id = uuid.v4();
2013-09-12 15:03:58 -04:00
}
var message = new Message(msg);
2013-08-29 23:38:28 -04:00
2013-09-12 15:03:58 -04:00
if (msg.archived) {
msg.archived.forEach(function (archived) {
if (me.isMe(archived.by)) {
message.archivedId = archived.id;
2013-09-12 15:03:58 -04:00
}
});
}
2013-08-29 23:38:28 -04:00
contact.addMessage(message, true);
2013-08-20 13:45:06 -04:00
if (!contact.lockedResource) {
2013-09-13 16:55:46 -04:00
contact.lockedResource = msg.from.full;
} else if (msg.from.full !== contact.lockedResource) {
2013-08-20 13:45:06 -04:00
contact.lockedResource = undefined;
}
}
});
client.on('replace', function (msg) {
msg = msg.toJSON();
var contact = me.getContact(msg.from, msg.to);
if (!contact) return;
var id = msg.replace;
var original = contact.messages.get(id);
if (!original) return;
original.correct(msg);
});
client.on('carbon:received', function (carbon) {
if (!me.isMe(carbon.from)) return;
var msg = carbon.carbonReceived.forwarded.message;
var delay = carbon.carbonReceived.forwarded.delay;
if (!delay.stamp) {
delay.stamp = Date.now();
}
if (!msg._extensions.delay) {
msg.delay = delay;
}
client.emit('message', msg);
});
client.on('carbon:sent', function (carbon) {
if (!me.isMe(carbon.from)) return;
var msg = carbon.carbonSent.forwarded.message;
var delay = carbon.carbonSent.forwarded.delay;
if (!delay.stamp) {
delay.stamp = Date.now();
}
if (!msg._extensions.delay) {
msg.delay = delay;
}
client.emit('message', msg);
});
client.on('disco:caps', function (pres) {
2013-09-11 17:58:39 -04:00
if (pres.caps.hash) {
log.debug('Caps from ' + pres.from + ' ver: ' + pres.caps.ver);
discoCapsQueue.push(pres);
}
});
2013-08-20 13:45:06 -04:00
};