cryptpad/www/common/common-messaging.js

219 lines
8.1 KiB
JavaScript
Raw Normal View History

2017-07-06 08:24:28 +00:00
define([
'/bower_components/chainpad-crypto/crypto.js',
2017-08-11 08:59:54 +00:00
'/common/common-hash.js',
'/common/common-util.js',
2017-11-21 15:46:19 +00:00
'/common/common-constants.js',
'/customize/messages.js',
2017-08-11 08:59:54 +00:00
'/common/common-realtime.js',
2017-11-30 14:01:17 +00:00
], function (Crypto, Hash, Util, Constants, Messages, Realtime) {
2017-08-03 12:12:07 +00:00
var Msg = {
inputs: [],
};
2017-07-07 16:53:21 +00:00
// TODO
// - mute a channel (hide notifications or don't open it?)
2017-07-06 08:24:28 +00:00
var pending = {};
var pendingRequests = [];
2017-07-06 08:24:28 +00:00
2017-08-11 08:59:54 +00:00
var createData = Msg.createData = function (proxy, hash) {
2017-07-07 16:53:21 +00:00
return {
2017-08-11 08:59:54 +00:00
channel: hash || Hash.createChannelId(),
displayName: proxy['cryptpad.username'],
2017-07-13 15:32:57 +00:00
profile: proxy.profile && proxy.profile.view,
2017-07-07 16:53:21 +00:00
edPublic: proxy.edPublic,
curvePublic: proxy.curvePublic,
2017-07-13 15:32:57 +00:00
avatar: proxy.profile && proxy.profile.avatar
2017-07-07 16:53:21 +00:00
};
};
2017-08-11 08:59:54 +00:00
var getFriend = function (proxy, pubkey) {
if (pubkey === proxy.curvePublic) {
2017-08-11 08:59:54 +00:00
var data = createData(proxy);
2017-07-07 16:53:21 +00:00
delete data.channel;
return data;
}
return proxy.friends ? proxy.friends[pubkey] : undefined;
};
2017-08-11 08:59:54 +00:00
var getFriendList = Msg.getFriendList = function (proxy) {
if (!proxy.friends) { proxy.friends = {}; }
return proxy.friends;
2017-07-06 16:00:03 +00:00
};
2017-08-11 08:59:54 +00:00
var eachFriend = function (friends, cb) {
Object.keys(friends).forEach(function (id) {
if (id === 'me') { return; }
cb(friends[id], id, friends);
});
};
2017-11-30 14:01:17 +00:00
Msg.getFriendChannelsList = function (proxy) {
2017-07-10 08:39:57 +00:00
var list = [];
2017-08-31 13:49:20 +00:00
eachFriend(proxy.friends, function (friend) {
2017-08-11 08:59:54 +00:00
list.push(friend.channel);
2017-07-10 08:39:57 +00:00
});
return list;
};
// TODO make this internal to the messenger
2017-11-30 14:01:17 +00:00
var channels = Msg.channels = {};
Msg.getLatestMessages = function () {
Object.keys(channels).forEach(function (id) {
if (id === 'me') { return; }
var friend = channels[id];
friend.getMessagesSinceDisconnect();
friend.refresh();
});
};
2017-07-07 16:53:21 +00:00
// Invitation
2017-08-09 12:20:37 +00:00
// FIXME there are too many functions with this name
2017-11-30 14:01:17 +00:00
var addToFriendList = Msg.addToFriendList = function (cfg, data, cb) {
var proxy = cfg.proxy;
2017-08-11 08:59:54 +00:00
var friends = getFriendList(proxy);
var pubKey = data.curvePublic; // todo validata data
2017-07-06 08:24:28 +00:00
if (pubKey === proxy.curvePublic) { return void cb("E_MYKEY"); }
2017-07-06 08:24:28 +00:00
friends[pubKey] = data;
2017-11-30 14:01:17 +00:00
Realtime.whenRealtimeSyncs(cfg.realtime, function () {
cb();
2017-11-30 14:01:17 +00:00
cfg.pinPads([data.channel], function (res) {
if (res.error) { console.error(res.error); }
});
2017-07-10 08:39:57 +00:00
});
2017-11-30 14:01:17 +00:00
cfg.updateMetadata();
2017-07-06 08:24:28 +00:00
};
2017-08-09 12:20:37 +00:00
/* Used to accept friend requests within apps other than /contacts/ */
2018-05-31 16:22:16 +00:00
Msg.addDirectMessageHandler = function (cfg, href) {
2017-11-30 14:01:17 +00:00
var network = cfg.network;
var proxy = cfg.proxy;
2017-07-06 08:24:28 +00:00
if (!network) { return void console.error('Network not ready'); }
network.on('message', function (message, sender) {
var msg;
if (sender === network.historyKeeper) { return; }
try {
2018-05-31 16:22:16 +00:00
var parsed = Hash.parsePadUrl(href);
var secret = Hash.getSecrets(parsed.type, parsed.hash);
2017-07-06 08:24:28 +00:00
if (!parsed.hashData) { return; }
2018-05-31 16:22:16 +00:00
var chan = secret.channel;
2017-07-06 08:24:28 +00:00
// Decrypt
2018-05-31 16:22:16 +00:00
var key = secret.keys ? secret.keys.cryptKey : Hash.decodeBase64(secret.key);
2017-07-18 13:52:46 +00:00
var decryptMsg;
2017-07-18 13:50:21 +00:00
try {
decryptMsg = Crypto.decrypt(message, key);
2017-07-18 13:52:46 +00:00
} catch (e) {
2017-07-18 13:50:21 +00:00
// If we can't decrypt, it means it is not a friend request message
}
2017-07-18 13:52:46 +00:00
if (!decryptMsg) { return; }
2017-07-06 08:24:28 +00:00
// Parse
msg = JSON.parse(decryptMsg);
2018-04-27 15:23:23 +00:00
if (msg[1] !== chan) { return; }
2017-07-06 08:24:28 +00:00
var msgData = msg[2];
var msgStr;
2017-07-06 08:24:28 +00:00
if (msg[0] === "FRIEND_REQ") {
msg = ["FRIEND_REQ_NOK", chan];
var todo = function (yes) {
2017-07-06 08:24:28 +00:00
if (yes) {
pending[sender] = msgData;
msg = ["FRIEND_REQ_OK", chan, createData(proxy, msgData.channel)];
2017-07-06 08:24:28 +00:00
}
msgStr = Crypto.encrypt(JSON.stringify(msg), key);
2017-07-06 08:24:28 +00:00
network.sendto(sender, msgStr);
};
2017-08-11 08:59:54 +00:00
var existing = getFriend(proxy, msgData.curvePublic);
if (existing) {
todo(true);
return;
}
var confirmMsg = Messages._getKey('contacts_request', [
Util.fixHTML(msgData.displayName)
2017-07-17 16:34:46 +00:00
]);
2017-11-30 14:01:17 +00:00
cfg.friendRequest(confirmMsg, todo);
2017-07-06 08:24:28 +00:00
return;
}
if (msg[0] === "FRIEND_REQ_OK") {
var idx = pendingRequests.indexOf(sender);
if (idx !== -1) { pendingRequests.splice(idx, 1); }
2017-08-09 12:20:37 +00:00
// FIXME clarify this function's name
2017-11-30 14:01:17 +00:00
addToFriendList(cfg, msgData, function (err) {
2017-07-06 08:24:28 +00:00
if (err) {
2017-11-30 14:01:17 +00:00
return void cfg.friendComplete({
logText: Messages.contacts_addError,
netfluxId: sender
});
2017-07-06 08:24:28 +00:00
}
2017-11-30 14:01:17 +00:00
cfg.friendComplete({
logText: Messages.contacts_added,
netfluxId: sender
});
2017-07-06 08:24:28 +00:00
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") {
2017-07-19 15:24:35 +00:00
var i = pendingRequests.indexOf(sender);
if (i !== -1) { pendingRequests.splice(i, 1); }
2017-11-30 14:01:17 +00:00
cfg.friendComplete({
logText: Messages.contacts_rejected,
netfluxId: sender
});
2017-11-30 14:01:17 +00:00
cfg.updateMetadata();
2017-07-06 08:24:28 +00:00
return;
}
if (msg[0] === "FRIEND_REQ_ACK") {
var data = pending[sender];
if (!data) { return; }
2017-11-30 14:01:17 +00:00
addToFriendList(cfg, data, function (err) {
2017-07-06 08:24:28 +00:00
if (err) {
2017-11-30 14:01:17 +00:00
return void cfg.friendComplete({
logText: Messages.contacts_addError,
netfluxId: sender
});
2017-07-06 08:24:28 +00:00
}
2017-11-30 14:01:17 +00:00
cfg.friendComplete({
logText: Messages.contacts_added,
netfluxId: sender
});
2017-07-06 08:24:28 +00:00
});
return;
}
// TODO: timeout ACK: warn the user
} catch (e) {
console.error("Cannot parse direct message", msg || message, "from", sender, e);
}
});
};
2017-11-30 14:01:17 +00:00
Msg.inviteFromUserlist = function (cfg, data, cb) {
var network = cfg.network;
var netfluxId = data.netfluxId;
var parsed = Hash.parsePadUrl(data.href);
2018-05-31 16:22:16 +00:00
var secret = Hash.getSecrets(parsed.type, parsed.hash);
2017-07-07 16:53:21 +00:00
if (!parsed.hashData) { return; }
2017-07-06 08:24:28 +00:00
// Message
2018-05-31 16:22:16 +00:00
var chan = secret.channel;
2017-11-30 14:01:17 +00:00
var myData = createData(cfg.proxy);
2017-07-06 08:24:28 +00:00
var msg = ["FRIEND_REQ", chan, myData];
// Encryption
2018-05-31 16:22:16 +00:00
var key = secret.keys ? secret.keys.cryptKey : Hash.decodeBase64(secret.key);
2017-07-06 08:24:28 +00:00
var msgStr = Crypto.encrypt(JSON.stringify(msg), key);
// Send encrypted message
if (pendingRequests.indexOf(netfluxId) === -1) {
pendingRequests.push(netfluxId);
2017-11-30 14:01:17 +00:00
cfg.updateMetadata(); // redraws the userlist in pad
}
2017-07-06 08:24:28 +00:00
network.sendto(netfluxId, msgStr);
2017-11-30 14:01:17 +00:00
cb();
2017-07-06 08:24:28 +00:00
};
return Msg;
});