You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
cryptpad/www/common/common-messaging.js

221 lines
8.1 KiB
JavaScript

8 years ago
define([
'/bower_components/chainpad-crypto/crypto.js',
7 years ago
'/common/common-hash.js',
'/common/common-util.js',
'/common/common-constants.js',
'/customize/messages.js',
7 years ago
'/common/common-realtime.js',
7 years ago
], function (Crypto, Hash, Util, Constants, Messages, Realtime) {
var Msg = {
inputs: [],
};
8 years ago
// TODO
// - mute a channel (hide notifications or don't open it?)
8 years ago
var pending = {};
var pendingRequests = [];
8 years ago
7 years ago
var createData = Msg.createData = function (proxy, hash) {
8 years ago
return {
7 years ago
channel: hash || Hash.createChannelId(),
displayName: proxy['cryptpad.username'],
profile: proxy.profile && proxy.profile.view,
8 years ago
edPublic: proxy.edPublic,
curvePublic: proxy.curvePublic,
avatar: proxy.profile && proxy.profile.avatar
8 years ago
};
};
7 years ago
var getFriend = function (proxy, pubkey) {
if (pubkey === proxy.curvePublic) {
7 years ago
var data = createData(proxy);
8 years ago
delete data.channel;
return data;
}
return proxy.friends ? proxy.friends[pubkey] : undefined;
};
7 years ago
var getFriendList = Msg.getFriendList = function (proxy) {
if (!proxy.friends) { proxy.friends = {}; }
return proxy.friends;
};
7 years ago
var eachFriend = function (friends, cb) {
Object.keys(friends).forEach(function (id) {
if (id === 'me') { return; }
cb(friends[id], id, friends);
});
};
7 years ago
Msg.getFriendChannelsList = function (proxy) {
var list = [];
eachFriend(proxy.friends, function (friend) {
7 years ago
list.push(friend.channel);
});
return list;
};
// TODO make this internal to the messenger
7 years ago
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();
});
};
8 years ago
// Invitation
// FIXME there are too many functions with this name
7 years ago
var addToFriendList = Msg.addToFriendList = function (cfg, data, cb) {
var proxy = cfg.proxy;
7 years ago
var friends = getFriendList(proxy);
var pubKey = data.curvePublic; // todo validata data
8 years ago
if (pubKey === proxy.curvePublic) { return void cb("E_MYKEY"); }
8 years ago
friends[pubKey] = data;
7 years ago
Realtime.whenRealtimeSyncs(cfg.realtime, function () {
cb();
7 years ago
cfg.pinPads([data.channel], function (res) {
if (res.error) { console.error(res.error); }
});
});
7 years ago
cfg.updateMetadata();
8 years ago
};
/* Used to accept friend requests within apps other than /contacts/ */
Msg.addDirectMessageHandler = function (cfg, href) {
7 years ago
var network = cfg.network;
var proxy = cfg.proxy;
8 years ago
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 = Hash.parsePadUrl(href);
var secret = Hash.getSecrets(parsed.type, parsed.hash);
8 years ago
if (!parsed.hashData) { return; }
var chan = secret.channel;
8 years ago
// Decrypt
var key = secret.keys ? secret.keys.cryptKey : Hash.decodeBase64(secret.key);
8 years ago
var decryptMsg;
try {
decryptMsg = Crypto.decrypt(message, key);
8 years ago
} catch (e) {
// If we can't decrypt, it means it is not a friend request message
}
8 years ago
if (!decryptMsg) { return; }
8 years ago
// Parse
msg = JSON.parse(decryptMsg);
if (msg[1] !== chan) { return; }
8 years ago
var msgData = msg[2];
var msgStr;
8 years ago
if (msg[0] === "FRIEND_REQ") {
msg = ["FRIEND_REQ_NOK", chan];
var todo = function (yes) {
8 years ago
if (yes) {
pending[sender] = msgData;
msg = ["FRIEND_REQ_OK", chan, createData(proxy, msgData.channel)];
8 years ago
}
msgStr = Crypto.encrypt(JSON.stringify(msg), key);
8 years ago
network.sendto(sender, msgStr);
};
7 years ago
var existing = getFriend(proxy, msgData.curvePublic);
if (existing) {
todo(true);
return;
}
var confirmMsg = Messages._getKey('contacts_request', [
Util.fixHTML(msgData.displayName)
]);
7 years ago
cfg.friendRequest(confirmMsg, todo);
8 years ago
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
7 years ago
addToFriendList(cfg, msgData, function (err) {
8 years ago
if (err) {
7 years ago
return void cfg.friendComplete({
logText: Messages.contacts_addError,
netfluxId: sender
});
8 years ago
}
7 years ago
cfg.friendComplete({
logText: Messages.contacts_added,
netfluxId: sender,
friend: msgData
});
8 years ago
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); }
7 years ago
cfg.friendComplete({
logText: Messages.contacts_rejected,
netfluxId: sender,
});
7 years ago
cfg.updateMetadata();
8 years ago
return;
}
if (msg[0] === "FRIEND_REQ_ACK") {
var data = pending[sender];
if (!data) { return; }
7 years ago
addToFriendList(cfg, data, function (err) {
8 years ago
if (err) {
7 years ago
return void cfg.friendComplete({
logText: Messages.contacts_addError,
netfluxId: sender
});
8 years ago
}
7 years ago
cfg.friendComplete({
logText: Messages.contacts_added,
netfluxId: sender,
friend: data
});
8 years ago
});
return;
}
// TODO: timeout ACK: warn the user
} catch (e) {
console.error("Cannot parse direct message", msg || message, "from", sender, e);
}
});
};
7 years ago
Msg.inviteFromUserlist = function (cfg, data, cb) {
var network = cfg.network;
var netfluxId = data.netfluxId;
var parsed = Hash.parsePadUrl(data.href);
var secret = Hash.getSecrets(parsed.type, parsed.hash);
8 years ago
if (!parsed.hashData) { return; }
8 years ago
// Message
var chan = secret.channel;
7 years ago
var myData = createData(cfg.proxy);
8 years ago
var msg = ["FRIEND_REQ", chan, myData];
// Encryption
var key = secret.keys ? secret.keys.cryptKey : Hash.decodeBase64(secret.key);
8 years ago
var msgStr = Crypto.encrypt(JSON.stringify(msg), key);
// Send encrypted message
if (pendingRequests.indexOf(netfluxId) === -1) {
pendingRequests.push(netfluxId);
7 years ago
cfg.updateMetadata(); // redraws the userlist in pad
}
8 years ago
network.sendto(netfluxId, msgStr);
7 years ago
cb();
8 years ago
};
return Msg;
});