cryptpad/www/common/common-messenger.js

705 lines
26 KiB
JavaScript
Raw Normal View History

2017-08-16 08:04:50 +00:00
define([
'jquery',
'/bower_components/chainpad-crypto/crypto.js',
'/common/curve.js',
'/common/common-hash.js',
'/common/common-realtime.js'
// '/bower_components/marked/marked.min.js'
], function ($, Crypto, Curve, Hash, Realtime) {
var Msg = {
inputs: [],
};
var Types = {
message: 'MSG',
update: 'UPDATE',
unfriend: 'UNFRIEND',
mapId: 'MAP_ID',
mapIdAck: 'MAP_ID_ACK'
};
2017-08-22 13:55:00 +00:00
var clone = function (o) {
return JSON.parse(JSON.stringify(o));
};
2017-08-16 08:04:50 +00:00
// TODO
// - mute a channel (hide notifications or don't open it?)
var pending = {};
var createData = Msg.createData = function (proxy, hash) {
return {
channel: hash || Hash.createChannelId(),
displayName: proxy['cryptpad.username'],
profile: proxy.profile && proxy.profile.view,
edPublic: proxy.edPublic,
curvePublic: proxy.curvePublic,
avatar: proxy.profile && proxy.profile.avatar
};
};
2017-08-22 13:55:00 +00:00
// TODO make this async
2017-08-16 08:04:50 +00:00
var getFriend = function (proxy, pubkey) {
if (pubkey === proxy.curvePublic) {
var data = createData(proxy);
delete data.channel;
return data;
}
return proxy.friends ? proxy.friends[pubkey] : undefined;
};
2017-08-22 13:55:00 +00:00
// TODO make this async
2017-08-16 08:04:50 +00:00
var removeFromFriendList = function (proxy, realtime, curvePublic, cb) {
if (!proxy.friends) { return; }
var friends = proxy.friends;
delete friends[curvePublic];
Realtime.whenRealtimeSyncs(realtime, cb);
};
2017-08-22 13:55:00 +00:00
// TODO make this async
2017-08-16 08:04:50 +00:00
var getFriendList = Msg.getFriendList = function (proxy) {
if (!proxy.friends) { proxy.friends = {}; }
return proxy.friends;
};
var eachFriend = function (friends, cb) {
Object.keys(friends).forEach(function (id) {
if (id === 'me') { return; }
cb(friends[id], id, friends);
});
};
Msg.getFriendChannelsList = function (proxy) {
var list = [];
eachFriend(proxy, function (friend) {
list.push(friend.channel);
});
return list;
};
var msgAlreadyKnown = function (channel, sig) {
return channel.messages.some(function (message) {
return message[0] === sig;
});
};
var getMoreHistory = function (network, chan, hash, count) {
var msg = [ 'GET_HISTORY_RANGE', chan.id, {
from: hash,
count: count,
}
];
network.sendto(network.historyKeeper, JSON.stringify(msg)).then(function () {
}, function (err) {
throw new Error(err);
});
};
2017-08-22 13:55:00 +00:00
getMoreHistory = getMoreHistory; // FIXME
2017-08-16 08:04:50 +00:00
var getChannelMessagesSince = function (network, proxy, chan, data, keys) {
var cfg = {
validateKey: keys.validateKey,
owners: [proxy.edPublic, data.edPublic],
lastKnownHash: data.lastKnownHash
};
var msg = ['GET_HISTORY', chan.id, cfg];
network.sendto(network.historyKeeper, JSON.stringify(msg))
.then($.noop, function (err) {
throw new Error(err);
});
};
2017-08-22 13:55:00 +00:00
// Invitation
// FIXME there are too many functions with this name
var addToFriendList = Msg.addToFriendList = function (common, data, cb) {
var proxy = common.getProxy();
var friends = getFriendList(proxy);
var pubKey = data.curvePublic;
if (pubKey === proxy.curvePublic) { return void cb("E_MYKEY"); }
friends[pubKey] = data;
Realtime.whenRealtimeSyncs(common.getRealtime(), function () {
cb();
common.pinPads([data.channel]);
});
common.changeDisplayName(proxy[common.displayNameKey]);
};
var pendingRequests = [];
/* Used to accept friend requests within apps other than /contacts/ */
// TODO move this into MSG.messenger
// as _openGroupChannel_
Msg.addDirectMessageHandler = function (common) {
var network = common.getNetwork();
var proxy = common.getProxy();
if (!network) { return void console.error('Network not ready'); }
network.on('message', function (message, sender) {
var msg;
if (sender === network.historyKeeper) { return; }
try {
var parsed = common.parsePadUrl(window.location.href);
if (!parsed.hashData) { return; }
var chan = parsed.hashData.channel;
// Decrypt
var keyStr = parsed.hashData.key;
var cryptor = Crypto.createEditCryptor(keyStr);
var key = cryptor.cryptKey;
var decryptMsg;
try {
decryptMsg = Crypto.decrypt(message, key);
} catch (e) {
// If we can't decrypt, it means it is not a friend request message
}
if (!decryptMsg) { return; }
// Parse
msg = JSON.parse(decryptMsg);
if (msg[1] !== parsed.hashData.channel) { return; }
var msgData = msg[2];
var msgStr;
if (msg[0] === "FRIEND_REQ") {
msg = ["FRIEND_REQ_NOK", chan];
var todo = function (yes) {
if (yes) {
pending[sender] = msgData;
msg = ["FRIEND_REQ_OK", chan, createData(common, msgData.channel)];
}
msgStr = Crypto.encrypt(JSON.stringify(msg), key);
network.sendto(sender, msgStr);
};
var existing = getFriend(proxy, msgData.curvePublic);
if (existing) {
todo(true);
return;
}
var confirmMsg = common.Messages._getKey('contacts_request', [
common.fixHTML(msgData.displayName)
]);
common.confirm(confirmMsg, todo, null, true);
return;
}
if (msg[0] === "FRIEND_REQ_OK") {
var idx = pendingRequests.indexOf(sender);
if (idx !== -1) { pendingRequests.splice(idx, 1); }
// FIXME clarify this function's name
addToFriendList(common, msgData, function (err) {
if (err) {
return void common.log(common.Messages.contacts_addError);
}
common.log(common.Messages.contacts_added);
var msg = ["FRIEND_REQ_ACK", chan];
var msgStr = Crypto.encrypt(JSON.stringify(msg), key);
network.sendto(sender, msgStr);
});
return;
}
if (msg[0] === "FRIEND_REQ_NOK") {
var i = pendingRequests.indexOf(sender);
if (i !== -1) { pendingRequests.splice(i, 1); }
common.log(common.Messages.contacts_rejected);
common.changeDisplayName(proxy[common.displayNameKey]);
return;
}
if (msg[0] === "FRIEND_REQ_ACK") {
var data = pending[sender];
if (!data) { return; }
addToFriendList(common, data, function (err) {
if (err) {
return void common.log(common.Messages.contacts_addError);
}
common.log(common.Messages.contacts_added);
});
return;
}
// TODO: timeout ACK: warn the user
} catch (e) {
console.error("Cannot parse direct message", msg || message, "from", sender, e);
}
});
};
// TODO somehow fold this into openGroupChannel
Msg.inviteFromUserlist = function (common, netfluxId) {
var network = common.getNetwork();
var parsed = common.parsePadUrl(window.location.href);
if (!parsed.hashData) { return; }
// Message
var chan = parsed.hashData.channel;
var myData = createData(common);
var msg = ["FRIEND_REQ", chan, myData];
// Encryption
var keyStr = parsed.hashData.key;
var cryptor = Crypto.createEditCryptor(keyStr);
var key = cryptor.cryptKey;
var msgStr = Crypto.encrypt(JSON.stringify(msg), key);
// Send encrypted message
if (pendingRequests.indexOf(netfluxId) === -1) {
pendingRequests.push(netfluxId);
var proxy = common.getProxy();
// this redraws the userlist after a change has occurred
// TODO rename this function to reflect its purpose
common.changeDisplayName(proxy[common.displayNameKey]);
}
network.sendto(netfluxId, msgStr);
};
2017-08-16 08:04:50 +00:00
Msg.messenger = function (common) {
2017-08-22 13:55:00 +00:00
var messenger = {
handlers: {
message: [],
join: [],
leave: [],
update: [],
},
};
messenger.on = function (type, f) {
var stack = messenger.handlers[type];
if (!Array.isArray(stack)) {
return void console.error('unsupported message type');
}
if (typeof(f) !== 'function') {
return void console.error('expected function');
}
stack.push(f);
};
// TODO openGroupChannel
messenger.openGroupChannel = function (hash, cb) {
// sets up infrastructure for a one to one channel using curve cryptography
cb = cb;
};
//var ready = messenger.ready = [];
2017-08-16 08:04:50 +00:00
var DEBUG = function (label) {
console.log('event:' + label);
};
2017-08-22 13:55:00 +00:00
DEBUG = DEBUG; // FIXME
2017-08-16 08:04:50 +00:00
var channels = messenger.channels = {};
2017-08-22 13:55:00 +00:00
var joining = {};
2017-08-16 08:04:50 +00:00
// declare common variables
var network = common.getNetwork();
var proxy = common.getProxy();
var realtime = common.getRealtime();
Msg.hk = network.historyKeeper;
var friends = getFriendList(proxy);
2017-08-22 13:55:00 +00:00
var getChannel = function (curvePublic) {
var friend = friends[curvePublic];
if (!friend) { return; }
var chanId = friend.channel;
if (!chanId) { return; }
return channels[chanId];
};
var getCurveForChannel = function (id) {
var channel = channels[id];
if (!channel) { return; }
return channel.curve;
};
messenger.getChannelHead = function (curvePublic, cb) {
var friend = friends[curvePublic];
if (!friend) { return void cb('NO_SUCH_FRIEND'); }
cb(void 0, friend.lastKnownHash);
};
messenger.setChannelHead = function (curvePublic, hash, cb) {
var friend = friends[curvePublic];
if (!friend) { return void cb('NO_SUCH_FRIEND'); }
friend.lastKnownHash = hash;
cb();
};
2017-08-16 08:04:50 +00:00
// Id message allows us to map a netfluxId with a public curve key
var onIdMessage = function (msg, sender) {
var channel;
var isId = Object.keys(channels).some(function (chanId) {
if (channels[chanId].userList.indexOf(sender) !== -1) {
channel = channels[chanId];
return true;
}
});
if (!isId) { return; }
var decryptedMsg = channel.encryptor.decrypt(msg);
if (decryptedMsg === null) {
// console.error('unable to decrypt message');
// console.error('potentially meant for yourself');
// message failed to parse, meaning somebody sent it to you but
// encrypted it with the wrong key, or you're sending a message to
// yourself in a different tab.
return;
}
if (!decryptedMsg) {
console.error('decrypted message was falsey but not null');
return;
}
var parsed;
try {
parsed = JSON.parse(decryptedMsg);
} catch (e) {
console.error(decryptedMsg);
return;
}
if (parsed[0] !== Types.mapId && parsed[0] !== Types.mapIdAck) { return; }
// check that the responding peer's encrypted netflux id matches
// the sender field. This is to prevent replay attacks.
if (parsed[2] !== sender || !parsed[1]) { return; }
2017-08-22 13:55:00 +00:00
channel.mapId[sender] = parsed[1]; // HERE
messenger.handlers.join.forEach(function (f) {
f(parsed[1], channel.id);
});
2017-08-16 08:04:50 +00:00
if (parsed[0] !== Types.mapId) { return; } // Don't send your key if it's already an ACK
// Answer with your own key
var rMsg = [Types.mapIdAck, proxy.curvePublic, channel.wc.myID];
var rMsgStr = JSON.stringify(rMsg);
var cryptMsg = channel.encryptor.encrypt(rMsgStr);
network.sendto(sender, cryptMsg);
};
var pushMsg = function (channel, cryptMsg) {
var msg = channel.encryptor.decrypt(cryptMsg);
// TODO emit new message event or something
// extension point for other apps
2017-08-22 13:55:00 +00:00
//console.log(msg);
2017-08-16 08:04:50 +00:00
var sig = cryptMsg.slice(0, 64);
if (msgAlreadyKnown(channel, sig)) { return; }
var parsedMsg = JSON.parse(msg);
if (parsedMsg[0] === Types.message) {
// TODO validate messages here
var res = {
type: parsedMsg[0],
sig: sig,
channel: parsedMsg[1],
time: parsedMsg[2],
text: parsedMsg[3],
2017-08-22 13:55:00 +00:00
// this makes debugging a whole lot easier
curve: getCurveForChannel(channel.id),
2017-08-16 08:04:50 +00:00
};
2017-08-22 13:55:00 +00:00
// TODO emit message event
2017-08-16 08:04:50 +00:00
channel.messages.push(res);
2017-08-22 13:55:00 +00:00
messenger.handlers.message.forEach(function (f) {
f(res);
});
2017-08-16 08:04:50 +00:00
return true;
}
if (parsedMsg[0] === Types.update) {
2017-08-22 13:55:00 +00:00
// TODO emit update event
2017-08-16 08:04:50 +00:00
if (parsedMsg[1] === proxy.curvePublic) { return; }
var newdata = parsedMsg[3];
var data = getFriend(proxy, parsedMsg[1]);
var types = [];
Object.keys(newdata).forEach(function (k) {
if (data[k] !== newdata[k]) {
types.push(k);
data[k] = newdata[k];
}
});
2017-08-22 13:55:00 +00:00
//channel.updateUI(types);
2017-08-16 08:04:50 +00:00
return;
}
if (parsedMsg[0] === Types.unfriend) {
removeFromFriendList(proxy, realtime, channel.friendEd, function () {
channel.wc.leave(Types.unfriend);
2017-08-22 13:55:00 +00:00
//channel.removeUI();
2017-08-16 08:04:50 +00:00
});
return;
}
};
/* Broadcast a display name, profile, or avatar change to all contacts
*/
2017-08-22 13:55:00 +00:00
// TODO send event...
messenger.updateMyData = function () {
2017-08-16 08:04:50 +00:00
var friends = getFriendList(proxy);
var mySyncData = friends.me;
var myData = createData(proxy);
if (!mySyncData || mySyncData.displayName !== myData.displayName
|| mySyncData.profile !== myData.profile
|| mySyncData.avatar !== myData.avatar) {
delete myData.channel;
Object.keys(channels).forEach(function (chan) {
var channel = channels[chan];
var msg = [Types.update, myData.curvePublic, +new Date(), myData];
var msgStr = JSON.stringify(msg);
var cryptMsg = channel.encryptor.encrypt(msgStr);
channel.wc.bcast(cryptMsg).then(function () {
channel.refresh();
}, function (err) {
console.error(err);
});
});
friends.me = myData;
}
};
2017-08-22 13:55:00 +00:00
var onChannelReady = function (chanId) {
var cb = joining[chanId];
if (typeof(cb) !== 'function') {
return void console.log('channel ready without callback');
2017-08-16 08:04:50 +00:00
}
2017-08-22 13:55:00 +00:00
delete joining[chanId];
return cb();
2017-08-16 08:04:50 +00:00
};
var onDirectMessage = function (common, msg, sender) {
if (sender !== Msg.hk) { return void onIdMessage(msg, sender); }
var parsed = JSON.parse(msg);
if ((parsed.validateKey || parsed.owners) && parsed.channel) {
return;
}
if (parsed.state && parsed.state === 1 && parsed.channel) {
if (channels[parsed.channel]) {
// parsed.channel is Ready
// channel[parsed.channel].ready();
channels[parsed.channel].ready = true;
2017-08-22 13:55:00 +00:00
onChannelReady(parsed.channel);
2017-08-16 08:04:50 +00:00
var updateTypes = channels[parsed.channel].updateOnReady;
if (updateTypes) {
2017-08-22 13:55:00 +00:00
//channels[parsed.channel].updateUI(updateTypes);
2017-08-16 08:04:50 +00:00
}
}
return;
}
var chan = parsed[3];
if (!chan || !channels[chan]) { return; }
pushMsg(channels[chan], parsed[4]);
};
var onMessage = function (common, msg, sender, chan) {
if (!channels[chan.id]) { return; }
var isMessage = pushMsg(channels[chan.id], msg);
if (isMessage) {
if (channels[chan.id].wc.myID !== sender) {
2017-08-22 13:55:00 +00:00
// Don't notify for your own messages
//channels[chan.id].notify();
2017-08-16 08:04:50 +00:00
}
2017-08-22 13:55:00 +00:00
//channels[chan.id].refresh();
// TODO emit message event
2017-08-16 08:04:50 +00:00
}
};
// listen for messages...
network.on('message', function(msg, sender) {
onDirectMessage(common, msg, sender);
});
2017-08-22 13:55:00 +00:00
messenger.removeFriend = function (curvePublic, cb) {
// TODO throw if no callback
2017-08-16 08:04:50 +00:00
var data = getFriend(proxy, curvePublic);
var channel = channels[data.channel];
var msg = [Types.unfriend, proxy.curvePublic, +new Date()];
var msgStr = JSON.stringify(msg);
var cryptMsg = channel.encryptor.encrypt(msgStr);
channel.wc.bcast(cryptMsg).then(function () {
2017-08-22 13:55:00 +00:00
delete friends[curvePublic];
Realtime.whenRealtimeSyncs(realtime, function () {
cb();
2017-08-16 08:04:50 +00:00
});
}, function (err) {
console.error(err);
2017-08-22 13:55:00 +00:00
cb(err);
2017-08-16 08:04:50 +00:00
});
};
// Open the channels
2017-08-22 13:55:00 +00:00
// TODO (curvePublic, cb) => (e) // READY
2017-08-16 08:04:50 +00:00
var openFriendChannel = function (data, f) {
var keys = Curve.deriveKeys(data.curvePublic, proxy.curvePrivate);
var encryptor = Curve.createEncryptor(keys);
network.join(data.channel).then(function (chan) {
var channel = channels[data.channel] = {
2017-08-22 13:55:00 +00:00
id: data.channel,
2017-08-16 08:04:50 +00:00
sending: false,
friendEd: f,
keys: keys,
2017-08-22 13:55:00 +00:00
curve: data.curvePublic,
2017-08-16 08:04:50 +00:00
encryptor: encryptor,
messages: [],
wc: chan,
userList: [],
mapId: {},
send: function (payload, cb) {
if (!network.webChannels.some(function (wc) {
if (wc.id === channel.wc.id) { return true; }
})) {
return void cb('NO_SUCH_CHANNEL');
}
var msg = [Types.message, proxy.curvePublic, +new Date(), payload];
var msgStr = JSON.stringify(msg);
var cryptMsg = channel.encryptor.encrypt(msgStr);
channel.wc.bcast(cryptMsg).then(function () {
pushMsg(channel, cryptMsg);
cb();
}, function (err) {
cb(err);
});
2017-08-22 13:55:00 +00:00
}
2017-08-16 08:04:50 +00:00
};
chan.on('message', function (msg, sender) {
onMessage(common, msg, sender, chan);
});
var onJoining = function (peer) {
if (peer === Msg.hk) { return; }
if (channel.userList.indexOf(peer) !== -1) { return; }
channel.userList.push(peer);
var msg = [Types.mapId, proxy.curvePublic, chan.myID];
var msgStr = JSON.stringify(msg);
var cryptMsg = channel.encryptor.encrypt(msgStr);
network.sendto(peer, cryptMsg);
};
chan.members.forEach(function (peer) {
if (peer === Msg.hk) { return; }
if (channel.userList.indexOf(peer) !== -1) { return; }
channel.userList.push(peer);
});
chan.on('join', onJoining);
chan.on('leave', function (peer) {
2017-08-22 13:55:00 +00:00
var curvePublic = channel.mapId[peer];
console.log(curvePublic);
2017-08-16 08:04:50 +00:00
var i = channel.userList.indexOf(peer);
while (i !== -1) {
channel.userList.splice(i, 1);
i = channel.userList.indexOf(peer);
}
2017-08-22 13:55:00 +00:00
// update status
if (!curvePublic) { return; }
messenger.handlers.leave.forEach(function (f) {
f(curvePublic, channel.id);
});
2017-08-16 08:04:50 +00:00
});
getChannelMessagesSince(network, proxy, chan, data, keys);
}, function (err) {
console.error(err);
});
};
2017-08-22 13:55:00 +00:00
// FIXME don't do this implicitly.
// get messages when a channel is opened, and if it reconnects
/*
2017-08-16 08:04:50 +00:00
messenger.getLatestMessages = function () {
Object.keys(channels).forEach(function (id) {
if (id === 'me') { return; }
var friend = channels[id];
2017-08-22 13:55:00 +00:00
//friend.getMessagesSinceDisconnect();
//friend.refresh();
2017-08-16 08:04:50 +00:00
});
2017-08-22 13:55:00 +00:00
};*/
2017-08-16 08:04:50 +00:00
2017-08-22 13:55:00 +00:00
// FIXME this shouldn't be necessary
/*
2017-08-16 08:04:50 +00:00
messenger.cleanFriendChannels = function () {
Object.keys(channels).forEach(function (id) {
delete channels[id];
});
2017-08-22 13:55:00 +00:00
};*/
messenger.getFriendList = function (cb) {
var friends = proxy.friends;
if (!friends) { return void cb(void 0, []); }
cb(void 0, Object.keys(proxy.friends).filter(function (k) {
return k !== 'me';
}));
2017-08-16 08:04:50 +00:00
};
2017-08-22 13:55:00 +00:00
messenger.openFriendChannels = function () {
2017-08-16 08:04:50 +00:00
eachFriend(friends, openFriendChannel);
};
2017-08-22 13:55:00 +00:00
messenger.openFriendChannel = function (curvePublic, cb) {
if (typeof(curvePublic) !== 'string') { return void cb('INVALID_ID'); }
if (typeof(cb) !== 'function') { throw new Error('expected callback'); }
2017-08-16 08:04:50 +00:00
2017-08-22 13:55:00 +00:00
var friend = clone(friends[curvePublic]);
if (typeof(friend) !== 'object') {
return void cb('NO_FRIEND_DATA');
}
var channel = friend.channel;
if (!channel) { return void cb('E_NO_CHANNEL'); }
joining[channel] = cb;
openFriendChannel(friend, curvePublic);
2017-08-16 08:04:50 +00:00
};
2017-08-22 13:55:00 +00:00
messenger.sendMessage = function (curvePublic, payload, cb) {
var channel = getChannel(curvePublic);
if (!channel) { return void cb('NO_CHANNEL'); }
if (!network.webChannels.some(function (wc) {
if (wc.id === channel.wc.id) { return true; }
})) {
return void cb('NO_SUCH_CHANNEL');
}
2017-08-16 08:04:50 +00:00
2017-08-22 13:55:00 +00:00
var msg = [Types.message, proxy.curvePublic, +new Date(), payload];
var msgStr = JSON.stringify(msg);
var cryptMsg = channel.encryptor.encrypt(msgStr);
2017-08-16 08:04:50 +00:00
2017-08-22 13:55:00 +00:00
channel.wc.bcast(cryptMsg).then(function () {
pushMsg(channel, cryptMsg);
cb();
}, function (err) {
cb(err);
});
};
2017-08-16 08:04:50 +00:00
2017-08-22 13:55:00 +00:00
messenger.getStatus = function (curvePublic, cb) {
var channel = getChannel(curvePublic);
if (!channel) { return void cb('NO_SUCH_CHANNEL'); }
var online = channel.userList.some(function (nId) {
return channel.mapId[nId] === curvePublic;
});
cb(void 0, online);
};
2017-08-16 08:04:50 +00:00
2017-08-22 13:55:00 +00:00
// TODO emit friend-list-changed event
messenger.checkNewFriends = function () {
eachFriend(friends, function (friend, id) {
if (!channels[id]) {
openFriendChannel(friend, id);
2017-08-16 08:04:50 +00:00
}
2017-08-22 13:55:00 +00:00
});
};
2017-08-16 08:04:50 +00:00
2017-08-22 13:55:00 +00:00
messenger.getFriendInfo = function (curvePublic, cb) {
var friend = friends[curvePublic];
if (!friend) { return void cb('NO_SUCH_FRIEND'); }
cb(void 0, friend);
};
2017-08-16 08:04:50 +00:00
2017-08-22 13:55:00 +00:00
return messenger;
2017-08-16 08:04:50 +00:00
};
return Msg;
});