|
|
|
@ -18,12 +18,13 @@ define([
|
|
|
|
|
mapIdAck: 'MAP_ID_ACK'
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var clone = function (o) {
|
|
|
|
|
return JSON.parse(JSON.stringify(o));
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// TODO
|
|
|
|
|
// - mute a channel (hide notifications or don't open it?)
|
|
|
|
|
|
|
|
|
|
var ready = [];
|
|
|
|
|
var pending = {};
|
|
|
|
|
var pendingRequests = [];
|
|
|
|
|
|
|
|
|
|
var createData = Msg.createData = function (proxy, hash) {
|
|
|
|
|
return {
|
|
|
|
@ -36,6 +37,7 @@ define([
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// TODO make this async
|
|
|
|
|
var getFriend = function (proxy, pubkey) {
|
|
|
|
|
if (pubkey === proxy.curvePublic) {
|
|
|
|
|
var data = createData(proxy);
|
|
|
|
@ -45,6 +47,7 @@ define([
|
|
|
|
|
return proxy.friends ? proxy.friends[pubkey] : undefined;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// TODO make this async
|
|
|
|
|
var removeFromFriendList = function (proxy, realtime, curvePublic, cb) {
|
|
|
|
|
if (!proxy.friends) { return; }
|
|
|
|
|
var friends = proxy.friends;
|
|
|
|
@ -52,6 +55,7 @@ define([
|
|
|
|
|
Realtime.whenRealtimeSyncs(realtime, cb);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// TODO make this async
|
|
|
|
|
var getFriendList = Msg.getFriendList = function (proxy) {
|
|
|
|
|
if (!proxy.friends) { proxy.friends = {}; }
|
|
|
|
|
return proxy.friends;
|
|
|
|
@ -90,6 +94,7 @@ define([
|
|
|
|
|
throw new Error(err);
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
getMoreHistory = getMoreHistory; // FIXME
|
|
|
|
|
|
|
|
|
|
var getChannelMessagesSince = function (network, proxy, chan, data, keys) {
|
|
|
|
|
var cfg = {
|
|
|
|
@ -104,15 +109,181 @@ define([
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// 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);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Msg.messenger = function (common) {
|
|
|
|
|
var messenger = {};
|
|
|
|
|
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 = [];
|
|
|
|
|
|
|
|
|
|
var DEBUG = function (label) {
|
|
|
|
|
console.log('event:' + label);
|
|
|
|
|
};
|
|
|
|
|
DEBUG = DEBUG; // FIXME
|
|
|
|
|
|
|
|
|
|
var channels = messenger.channels = {};
|
|
|
|
|
|
|
|
|
|
var joining = {};
|
|
|
|
|
|
|
|
|
|
// declare common variables
|
|
|
|
|
var network = common.getNetwork();
|
|
|
|
|
var proxy = common.getProxy();
|
|
|
|
@ -120,6 +291,33 @@ define([
|
|
|
|
|
Msg.hk = network.historyKeeper;
|
|
|
|
|
var friends = getFriendList(proxy);
|
|
|
|
|
|
|
|
|
|
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();
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Id message allows us to map a netfluxId with a public curve key
|
|
|
|
|
var onIdMessage = function (msg, sender) {
|
|
|
|
|
var channel;
|
|
|
|
@ -161,9 +359,10 @@ define([
|
|
|
|
|
// 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; }
|
|
|
|
|
channel.mapId[sender] = parsed[1];
|
|
|
|
|
|
|
|
|
|
channel.updateStatus();
|
|
|
|
|
channel.mapId[sender] = parsed[1]; // HERE
|
|
|
|
|
messenger.handlers.join.forEach(function (f) {
|
|
|
|
|
f(parsed[1], channel.id);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (parsed[0] !== Types.mapId) { return; } // Don't send your key if it's already an ACK
|
|
|
|
|
// Answer with your own key
|
|
|
|
@ -178,7 +377,7 @@ define([
|
|
|
|
|
|
|
|
|
|
// TODO emit new message event or something
|
|
|
|
|
// extension point for other apps
|
|
|
|
|
console.log(msg);
|
|
|
|
|
//console.log(msg);
|
|
|
|
|
|
|
|
|
|
var sig = cryptMsg.slice(0, 64);
|
|
|
|
|
if (msgAlreadyKnown(channel, sig)) { return; }
|
|
|
|
@ -192,12 +391,22 @@ define([
|
|
|
|
|
channel: parsedMsg[1],
|
|
|
|
|
time: parsedMsg[2],
|
|
|
|
|
text: parsedMsg[3],
|
|
|
|
|
// this makes debugging a whole lot easier
|
|
|
|
|
curve: getCurveForChannel(channel.id),
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// TODO emit message event
|
|
|
|
|
channel.messages.push(res);
|
|
|
|
|
|
|
|
|
|
messenger.handlers.message.forEach(function (f) {
|
|
|
|
|
f(res);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
if (parsedMsg[0] === Types.update) {
|
|
|
|
|
// TODO emit update event
|
|
|
|
|
|
|
|
|
|
if (parsedMsg[1] === proxy.curvePublic) { return; }
|
|
|
|
|
var newdata = parsedMsg[3];
|
|
|
|
|
var data = getFriend(proxy, parsedMsg[1]);
|
|
|
|
@ -208,13 +417,13 @@ define([
|
|
|
|
|
data[k] = newdata[k];
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
channel.updateUI(types);
|
|
|
|
|
//channel.updateUI(types);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (parsedMsg[0] === Types.unfriend) {
|
|
|
|
|
removeFromFriendList(proxy, realtime, channel.friendEd, function () {
|
|
|
|
|
channel.wc.leave(Types.unfriend);
|
|
|
|
|
channel.removeUI();
|
|
|
|
|
//channel.removeUI();
|
|
|
|
|
});
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
@ -222,7 +431,9 @@ define([
|
|
|
|
|
|
|
|
|
|
/* Broadcast a display name, profile, or avatar change to all contacts
|
|
|
|
|
*/
|
|
|
|
|
var updateMyData = function (proxy) {
|
|
|
|
|
|
|
|
|
|
// TODO send event...
|
|
|
|
|
messenger.updateMyData = function () {
|
|
|
|
|
var friends = getFriendList(proxy);
|
|
|
|
|
var mySyncData = friends.me;
|
|
|
|
|
var myData = createData(proxy);
|
|
|
|
@ -245,16 +456,13 @@ define([
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var onChannelReady = function (proxy, chanId) {
|
|
|
|
|
if (ready.indexOf(chanId) !== -1) { return; }
|
|
|
|
|
ready.push(chanId);
|
|
|
|
|
channels[chanId].updateStatus(); // c'est quoi?
|
|
|
|
|
var friends = getFriendList(proxy);
|
|
|
|
|
if (ready.length === Object.keys(friends).length) {
|
|
|
|
|
// All channels are ready
|
|
|
|
|
updateMyData(proxy);
|
|
|
|
|
var onChannelReady = function (chanId) {
|
|
|
|
|
var cb = joining[chanId];
|
|
|
|
|
if (typeof(cb) !== 'function') {
|
|
|
|
|
return void console.log('channel ready without callback');
|
|
|
|
|
}
|
|
|
|
|
return ready.length;
|
|
|
|
|
delete joining[chanId];
|
|
|
|
|
return cb();
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var onDirectMessage = function (common, msg, sender) {
|
|
|
|
@ -266,13 +474,13 @@ define([
|
|
|
|
|
if (parsed.state && parsed.state === 1 && parsed.channel) {
|
|
|
|
|
if (channels[parsed.channel]) {
|
|
|
|
|
// parsed.channel is Ready
|
|
|
|
|
// TODO: call a function that shows that the channel is ready? (remove a spinner, ...)
|
|
|
|
|
// channel[parsed.channel].ready();
|
|
|
|
|
channels[parsed.channel].ready = true;
|
|
|
|
|
onChannelReady(proxy, parsed.channel);
|
|
|
|
|
onChannelReady(parsed.channel);
|
|
|
|
|
var updateTypes = channels[parsed.channel].updateOnReady;
|
|
|
|
|
if (updateTypes) {
|
|
|
|
|
channels[parsed.channel].updateUI(updateTypes);
|
|
|
|
|
|
|
|
|
|
//channels[parsed.channel].updateUI(updateTypes);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return;
|
|
|
|
@ -280,22 +488,19 @@ define([
|
|
|
|
|
var chan = parsed[3];
|
|
|
|
|
if (!chan || !channels[chan]) { return; }
|
|
|
|
|
pushMsg(channels[chan], parsed[4]);
|
|
|
|
|
channels[chan].refresh();
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var onMessage = function (common, msg, sender, chan) {
|
|
|
|
|
if (!channels[chan.id]) { return; }
|
|
|
|
|
|
|
|
|
|
var realtime = common.getRealtime();
|
|
|
|
|
var proxy = common.getProxy();
|
|
|
|
|
|
|
|
|
|
var isMessage = pushMsg(channels[chan.id], msg);
|
|
|
|
|
if (isMessage) {
|
|
|
|
|
// Don't notify for your own messages
|
|
|
|
|
if (channels[chan.id].wc.myID !== sender) {
|
|
|
|
|
channels[chan.id].notify();
|
|
|
|
|
// Don't notify for your own messages
|
|
|
|
|
//channels[chan.id].notify();
|
|
|
|
|
}
|
|
|
|
|
channels[chan.id].refresh();
|
|
|
|
|
//channels[chan.id].refresh();
|
|
|
|
|
// TODO emit message event
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
@ -304,160 +509,41 @@ define([
|
|
|
|
|
onDirectMessage(common, msg, sender);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Refresh the active channel
|
|
|
|
|
// TODO extract into UI method
|
|
|
|
|
var refresh = function (/*curvePublic*/) {
|
|
|
|
|
return;
|
|
|
|
|
/*
|
|
|
|
|
if (messenger.active !== curvePublic) { return; }
|
|
|
|
|
var data = friends[curvePublic];
|
|
|
|
|
if (!data) { return; }
|
|
|
|
|
var channel = channels[data.channel];
|
|
|
|
|
if (!channel) { return; }
|
|
|
|
|
|
|
|
|
|
var $chat = ui.getChannel(curvePublic);
|
|
|
|
|
|
|
|
|
|
if (!$chat) { return; }
|
|
|
|
|
|
|
|
|
|
// Add new messages
|
|
|
|
|
var messages = channel.messages;
|
|
|
|
|
var $messages = $chat.find('.messages');
|
|
|
|
|
var msg, name;
|
|
|
|
|
var last = typeof(channel.lastDisplayed) === 'number'? channel.lastDisplayed: -1;
|
|
|
|
|
for (var i = last + 1; i<messages.length; i++) {
|
|
|
|
|
msg = messages[i];
|
|
|
|
|
name = (msg.channel !== channel.lastSender)?
|
|
|
|
|
getFriend(proxy, msg.channel).displayName: undefined;
|
|
|
|
|
|
|
|
|
|
ui.createMessage(msg, name).appendTo($messages);
|
|
|
|
|
channel.lastSender = msg.channel;
|
|
|
|
|
}
|
|
|
|
|
$messages.scrollTop($messages[0].scrollHeight);
|
|
|
|
|
channel.lastDisplayed = i-1;
|
|
|
|
|
channel.unnotify();
|
|
|
|
|
|
|
|
|
|
// return void channel.notify();
|
|
|
|
|
if (messages.length > 10) {
|
|
|
|
|
var lastKnownMsg = messages[messages.length - 11];
|
|
|
|
|
channel.setLastMessageRead(lastKnownMsg.sig);
|
|
|
|
|
}*/
|
|
|
|
|
};
|
|
|
|
|
// Display a new channel
|
|
|
|
|
// TODO extract into UI method
|
|
|
|
|
//var display = function (curvePublic) {
|
|
|
|
|
//curvePublic = curvePublic;
|
|
|
|
|
/*
|
|
|
|
|
ui.hideInfo();
|
|
|
|
|
var $chat = ui.getChannel(curvePublic);
|
|
|
|
|
if (!$chat) {
|
|
|
|
|
$chat = ui.createChat(curvePublic);
|
|
|
|
|
ui.createChatBox(proxy, $chat, curvePublic);
|
|
|
|
|
}
|
|
|
|
|
// Show the correct div
|
|
|
|
|
ui.hideChat();
|
|
|
|
|
$chat.show();
|
|
|
|
|
|
|
|
|
|
// TODO set this attr per-messenger
|
|
|
|
|
messenger.setActive(curvePublic);
|
|
|
|
|
// TODO don't mark messages as read unless you have displayed them
|
|
|
|
|
|
|
|
|
|
refresh(curvePublic);*/
|
|
|
|
|
//};
|
|
|
|
|
|
|
|
|
|
// TODO take a callback
|
|
|
|
|
var remove = function (curvePublic) {
|
|
|
|
|
messenger.removeFriend = function (curvePublic, cb) {
|
|
|
|
|
// TODO throw if no callback
|
|
|
|
|
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 () {
|
|
|
|
|
removeFromFriendList(common, curvePublic, function () {
|
|
|
|
|
channel.wc.leave(Types.unfriend);
|
|
|
|
|
channel.removeUI();
|
|
|
|
|
delete friends[curvePublic];
|
|
|
|
|
Realtime.whenRealtimeSyncs(realtime, function () {
|
|
|
|
|
cb();
|
|
|
|
|
});
|
|
|
|
|
}, function (err) {
|
|
|
|
|
console.error(err);
|
|
|
|
|
cb(err);
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Open the channels
|
|
|
|
|
// TODO (curvePublic, cb) => (e) // READY
|
|
|
|
|
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] = {
|
|
|
|
|
id: data.channel,
|
|
|
|
|
sending: false,
|
|
|
|
|
friendEd: f,
|
|
|
|
|
keys: keys,
|
|
|
|
|
curve: data.curvePublic,
|
|
|
|
|
encryptor: encryptor,
|
|
|
|
|
messages: [],
|
|
|
|
|
unfriend: function () {
|
|
|
|
|
DEBUG('unfriend');
|
|
|
|
|
remove(data.curvePublic);
|
|
|
|
|
},
|
|
|
|
|
refresh: function () {
|
|
|
|
|
DEBUG('refresh');
|
|
|
|
|
refresh(data.curvePublic);
|
|
|
|
|
},
|
|
|
|
|
notify: function () {
|
|
|
|
|
//ui.notify(data.curvePublic);
|
|
|
|
|
DEBUG('notify');
|
|
|
|
|
common.notify(); // HERE
|
|
|
|
|
},
|
|
|
|
|
unnotify: function () {
|
|
|
|
|
DEBUG('unnotify');
|
|
|
|
|
//ui.unnotify(data.curvePublic);
|
|
|
|
|
},
|
|
|
|
|
removeUI: function () {
|
|
|
|
|
DEBUG('remove-ui');
|
|
|
|
|
//ui.remove(data.curvePublic);
|
|
|
|
|
},
|
|
|
|
|
updateUI: function (/*types*/) {
|
|
|
|
|
DEBUG('update-ui');
|
|
|
|
|
//ui.update(data.curvePublic, types);
|
|
|
|
|
},
|
|
|
|
|
updateStatus: function () {
|
|
|
|
|
DEBUG('update-status');
|
|
|
|
|
//ui.updateStatus(data.curvePublic,
|
|
|
|
|
//channel.getStatus(data.curvePublic));
|
|
|
|
|
},
|
|
|
|
|
setLastMessageRead: function (hash) {
|
|
|
|
|
DEBUG('updateLastKnownHash');
|
|
|
|
|
data.lastKnownHash = hash;
|
|
|
|
|
},
|
|
|
|
|
getLastMessageRead: function () {
|
|
|
|
|
return data.lastKnownHash;
|
|
|
|
|
},
|
|
|
|
|
isActive: function () {
|
|
|
|
|
return data.curvePublic === messenger.active;
|
|
|
|
|
},
|
|
|
|
|
getMessagesSinceDisconnect: function () {
|
|
|
|
|
getChannelMessagesSince(network, proxy, chan, data, keys);
|
|
|
|
|
},
|
|
|
|
|
wc: chan,
|
|
|
|
|
userList: [],
|
|
|
|
|
mapId: {},
|
|
|
|
|
getStatus: function (curvePublic) {
|
|
|
|
|
return channel.userList.some(function (nId) {
|
|
|
|
|
return channel.mapId[nId] === curvePublic;
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
getPreviousMessages: function () {
|
|
|
|
|
var history = channel.messages;
|
|
|
|
|
if (!history || !history.length) {
|
|
|
|
|
// TODO ask for default history?
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var oldestMessage = history[0];
|
|
|
|
|
if (!oldestMessage) {
|
|
|
|
|
return; // nothing to fetch
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var messageHash = oldestMessage[0];
|
|
|
|
|
getMoreHistory(network, chan, messageHash, 10);
|
|
|
|
|
},
|
|
|
|
|
send: function (payload, cb) {
|
|
|
|
|
if (!network.webChannels.some(function (wc) {
|
|
|
|
|
if (wc.id === channel.wc.id) { return true; }
|
|
|
|
@ -475,7 +561,7 @@ define([
|
|
|
|
|
}, function (err) {
|
|
|
|
|
cb(err);
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
chan.on('message', function (msg, sender) {
|
|
|
|
|
onMessage(common, msg, sender, chan);
|
|
|
|
@ -489,7 +575,6 @@ define([
|
|
|
|
|
var msgStr = JSON.stringify(msg);
|
|
|
|
|
var cryptMsg = channel.encryptor.encrypt(msgStr);
|
|
|
|
|
network.sendto(peer, cryptMsg);
|
|
|
|
|
channel.updateStatus();
|
|
|
|
|
};
|
|
|
|
|
chan.members.forEach(function (peer) {
|
|
|
|
|
if (peer === Msg.hk) { return; }
|
|
|
|
@ -498,12 +583,19 @@ define([
|
|
|
|
|
});
|
|
|
|
|
chan.on('join', onJoining);
|
|
|
|
|
chan.on('leave', function (peer) {
|
|
|
|
|
var curvePublic = channel.mapId[peer];
|
|
|
|
|
console.log(curvePublic);
|
|
|
|
|
|
|
|
|
|
var i = channel.userList.indexOf(peer);
|
|
|
|
|
while (i !== -1) {
|
|
|
|
|
channel.userList.splice(i, 1);
|
|
|
|
|
i = channel.userList.indexOf(peer);
|
|
|
|
|
}
|
|
|
|
|
channel.updateStatus();
|
|
|
|
|
// update status
|
|
|
|
|
if (!curvePublic) { return; }
|
|
|
|
|
messenger.handlers.leave.forEach(function (f) {
|
|
|
|
|
f(curvePublic, channel.id);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
getChannelMessagesSince(network, proxy, chan, data, keys);
|
|
|
|
@ -512,180 +604,100 @@ define([
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// FIXME don't do this implicitly.
|
|
|
|
|
// get messages when a channel is opened, and if it reconnects
|
|
|
|
|
/*
|
|
|
|
|
messenger.getLatestMessages = function () {
|
|
|
|
|
Object.keys(channels).forEach(function (id) {
|
|
|
|
|
if (id === 'me') { return; }
|
|
|
|
|
var friend = channels[id];
|
|
|
|
|
friend.getMessagesSinceDisconnect();
|
|
|
|
|
friend.refresh();
|
|
|
|
|
//friend.getMessagesSinceDisconnect();
|
|
|
|
|
//friend.refresh();
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
};*/
|
|
|
|
|
|
|
|
|
|
// FIXME this shouldn't be necessary
|
|
|
|
|
/*
|
|
|
|
|
messenger.cleanFriendChannels = function () {
|
|
|
|
|
Object.keys(channels).forEach(function (id) {
|
|
|
|
|
delete channels[id];
|
|
|
|
|
});
|
|
|
|
|
};*/
|
|
|
|
|
|
|
|
|
|
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';
|
|
|
|
|
}));
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var openFriendChannels = messenger.openFriendChannels = function () {
|
|
|
|
|
messenger.openFriendChannels = function () {
|
|
|
|
|
eachFriend(friends, openFriendChannel);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
//messenger.setEditable = ui.setEditable;
|
|
|
|
|
|
|
|
|
|
openFriendChannels();
|
|
|
|
|
messenger.openFriendChannel = function (curvePublic, cb) {
|
|
|
|
|
if (typeof(curvePublic) !== 'string') { return void cb('INVALID_ID'); }
|
|
|
|
|
if (typeof(cb) !== 'function') { throw new Error('expected callback'); }
|
|
|
|
|
|
|
|
|
|
// TODO split loop innards into ui methods
|
|
|
|
|
var checkNewFriends = function () {
|
|
|
|
|
eachFriend(friends, function (friend, id) {
|
|
|
|
|
if (!channels[id]) {
|
|
|
|
|
openFriendChannel(friend, id);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
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);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
common.onDisplayNameChanged(function () {
|
|
|
|
|
checkNewFriends();
|
|
|
|
|
updateMyData(proxy);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return messenger;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// 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"); }
|
|
|
|
|
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');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
friends[pubKey] = data;
|
|
|
|
|
var msg = [Types.message, proxy.curvePublic, +new Date(), payload];
|
|
|
|
|
var msgStr = JSON.stringify(msg);
|
|
|
|
|
var cryptMsg = channel.encryptor.encrypt(msgStr);
|
|
|
|
|
|
|
|
|
|
Realtime.whenRealtimeSyncs(common.getRealtime(), function () {
|
|
|
|
|
cb();
|
|
|
|
|
common.pinPads([data.channel]);
|
|
|
|
|
});
|
|
|
|
|
common.changeDisplayName(proxy[common.displayNameKey]);
|
|
|
|
|
};
|
|
|
|
|
channel.wc.bcast(cryptMsg).then(function () {
|
|
|
|
|
pushMsg(channel, cryptMsg);
|
|
|
|
|
cb();
|
|
|
|
|
}, function (err) {
|
|
|
|
|
cb(err);
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/* Used to accept friend requests within apps other than /contacts/ */
|
|
|
|
|
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); }
|
|
|
|
|
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);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// 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 emit friend-list-changed event
|
|
|
|
|
messenger.checkNewFriends = function () {
|
|
|
|
|
eachFriend(friends, function (friend, id) {
|
|
|
|
|
if (!channels[id]) {
|
|
|
|
|
openFriendChannel(friend, id);
|
|
|
|
|
}
|
|
|
|
|
// TODO: timeout ACK: warn the user
|
|
|
|
|
} catch (e) {
|
|
|
|
|
console.error("Cannot parse direct message", msg || message, "from", sender, e);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Msg.getPending = function () {
|
|
|
|
|
return pendingRequests;
|
|
|
|
|
};
|
|
|
|
|
messenger.getFriendInfo = function (curvePublic, cb) {
|
|
|
|
|
var friend = friends[curvePublic];
|
|
|
|
|
if (!friend) { return void cb('NO_SUCH_FRIEND'); }
|
|
|
|
|
cb(void 0, friend);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
return messenger;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return Msg;
|