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/realtime-input.js

346 lines
12 KiB
JavaScript

/*
* Copyright 2014 XWiki SAS
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
window.Reflect = { has: function (x,y) { return (y in x); } };
define([
'/common/messages.js',
'/common/netflux-client.js',
'/common/crypto.js',
'/common/toolbar.js',
'/common/TextPatcher.js',
'/common/es6-promise.min.js',
'/common/chainpad.js',
'/bower_components/jquery/dist/jquery.min.js',
], function (Messages, Netflux, Crypto, Toolbar, TextPatcher) {
var $ = window.jQuery;
var ChainPad = window.ChainPad;
var PARANOIA = true;
var module = { exports: {} };
/**
* If an error is encountered but it is recoverable, do not immediately fail
* but if it keeps firing errors over and over, do fail.
*/
var MAX_RECOVERABLE_ERRORS = 15;
var debug = function (x) { console.log(x); },
warn = function (x) { console.error(x); },
verbose = function (x) { console.log(x); };
verbose = function () {}; // comment out to enable verbose logging
// ------------------ Trapping Keyboard Events ---------------------- //
var bindEvents = function (element, events, callback, unbind) {
for (var i = 0; i < events.length; i++) {
var e = events[i];
if (element.addEventListener) {
if (unbind) {
element.removeEventListener(e, callback, false);
} else {
element.addEventListener(e, callback, false);
}
} else {
if (unbind) {
element.detachEvent('on' + e, callback);
} else {
element.attachEvent('on' + e, callback);
}
}
}
};
var getParameterByName = function (name, url) {
if (!url) { url = window.location.href; }
name = name.replace(/[\[\]]/g, "\\$&");
var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"),
results = regex.exec(url);
if (!results) { return null; }
if (!results[2]) { return ''; }
return decodeURIComponent(results[2].replace(/\+/g, " "));
};
var start = module.exports.start =
function (config)
{
var websocketUrl = config.websocketURL;
var userName = config.userName;
var channel = config.channel;
var chanKey = config.cryptKey;
var cryptKey = Crypto.parseKey(chanKey).cryptKey;
var passwd = 'y';
// make sure configuration is defined
config = config || {};
var doc = config.doc || null;
var allMessages = [];
var initializing = true;
var recoverableErrorCount = 0;
var toReturn = {};
var messagesHistory = [];
var chainpadAdapter = {};
var realtime;
// define this in case it gets called before the rest of our stuff is ready.
var onEvent = toReturn.onEvent = function (newText) { };
var parseMessage = function (msg) {
var res ={};
// two or more? use a for
['pass','user','channelId','content'].forEach(function(attr){
var len=msg.slice(0,msg.indexOf(':')),
// taking an offset lets us slice out the prop
// and saves us one string copy
o=len.length+1,
prop=res[attr]=msg.slice(o,Number(len)+o);
// slice off the property and its descriptor
msg = msg.slice(prop.length+o);
});
// content is the only attribute that's not a string
res.content=JSON.parse(res.content);
return res;
};
var mkMessage = function (user, chan, content) {
content = JSON.stringify(content);
return user.length + ':' + user +
chan.length + ':' + chan +
content.length + ':' + content;
};
var whoami = new RegExp(userName.replace(/[\/\+]/g, function (c) {
return '\\' +c;
}));
var onMessage = function(peer, msg, wc, network) {
var hc = (wc && wc.history_keeper) ? wc.history_keeper : null;
if(wc && (msg === 0 || msg === '0')) {
// FIXME onReady is defined below, so JSHINT complains
/* jshint ignore:start */
onReady(wc, network);
/* jshint ignore:end */
return;
}
else if (peer === hc){
msg = JSON.parse(msg)[4];
}
var message = chainpadAdapter.msgIn(peer, msg);
verbose(message);
allMessages.push(message);
// if (!initializing) {
// if (toReturn.onLocal) {
// toReturn.onLocal();
// }
// }
realtime.message(message);
if (/\[5,/.test(message)) { verbose("pong"); }
if (!initializing) {
if (/\[2,/.test(message)) {
//verbose("Got a patch");
if (whoami.test(message)) {
//verbose("Received own message");
} else {
//verbose("Received remote message");
// obviously this is only going to get called if
if (config.onRemote) {
config.onRemote({
realtime: realtime
});
}
}
}
}
};
var userList = {
onChange : function() {},
users: []
};
var onJoining = function(peer) {
if(peer.length !== 32) { return; }
var list = userList.users;
var index = list.indexOf(peer);
if(index === -1) {
userList.users.push(peer);
}
userList.onChange();
};
var onLeaving = function(peer) {
var list = userList.users;
var index = list.indexOf(peer);
if(index !== -1) {
userList.users.splice(index, 1);
}
userList.onChange();
};
chainpadAdapter = {
msgIn : function(peerId, msg) {
var parsed = parseMessage(msg);
// Remove the password from the message
var passLen = msg.substring(0,msg.indexOf(':'));
var message = msg.substring(passLen.length+1 + Number(passLen));
try {
var decryptedMsg = Crypto.decrypt(message, cryptKey);
messagesHistory.push(decryptedMsg);
return decryptedMsg;
} catch (err) {
return message;
}
},
msgOut : function(msg, wc) {
var parsed = parseMessage(msg);
if(parsed.content[0] === 0) { // We're registering : send a REGISTER_ACK to Chainpad
onMessage('', '1:y'+mkMessage('', channel, [1,0]));
return;
}
if(parsed.content[0] === 4) { // PING message from Chainpad
parsed.content[0] = 5;
onMessage('', '1:y'+mkMessage(parsed.user, parsed.channelId, parsed.content));
// wc.sendPing();
return;
}
return Crypto.encrypt(msg, cryptKey);
}
};
var options = {
key: ''
};
var createRealtime = function(chan) {
return ChainPad.create(userName,
passwd,
channel,
config.initialState || {},
{
transformFunction: config.transformFunction
});
};
var onReady = function(wc, network) {
if(config.onInit) {
config.onInit({
myID: wc.myID,
realtime: realtime,
getLag: network.getLag,
userList: userList
});
}
// Trigger onJoining with our own Cryptpad username to tell the toolbar that we are synced
onJoining(wc.myID);
// we're fully synced
initializing = false;
// execute an onReady callback if one was supplied
if (config.onReady) {
config.onReady({
realtime: realtime
});
}
};
var onOpen = function(wc, network) {
channel = wc.id;
window.location.hash = channel + '|' + chanKey;
// Add the existing peers in the userList
wc.members.forEach(onJoining);
// Add the handlers to the WebChannel
wc.on('message', function (msg, sender) { //Channel msg
onMessage(sender, msg, wc, network);
});
wc.on('join', onJoining);
wc.on('leave', onLeaving);
if(config.setMyID) {
config.setMyID({
myID: wc.myID
});
}
// Open a Chainpad session
realtime = createRealtime();
// Sending a message...
realtime.onMessage(function(message) {
// Filter messages sent by Chainpad to make it compatible with Netflux
message = chainpadAdapter.msgOut(message, wc);
if(message) {
wc.bcast(message).then(function() {
// Send the message back to Chainpad once it is sent to the recipients.
onMessage(wc.myID, message);
}, function(err) {
// The message has not been sent, display the error.
console.error(err);
});
}
});
// Get the channel history
var hc;
wc.members.forEach(function (p) {
if (p.length === 16) { hc = p; }
});
wc.history_keeper = hc;
if (hc) { network.sendto(hc, JSON.stringify(['GET_HISTORY', wc.id])); }
toReturn.patchText = TextPatcher.create({
realtime: realtime
});
realtime.start();
};
var findChannelById = function(webChannels, channelId) {
var webChannel;
webChannels.forEach(function(chan) {
if(chan.id === channelId) { webChannel = chan; return;}
});
return webChannel;
};
// Connect to the WebSocket channel
Netflux.connect(websocketUrl).then(function(network) {
network.on('message', function (msg, sender) { // Direct message
var wchan = findChannelById(network.webChannels, channel);
if(wchan) {
onMessage(sender, msg, wchan, network);
}
});
network.join(channel || null).then(function(wc) {
onOpen(wc, network);
}, function(error) {
console.error(error);
});
}, function(error) {
warn(error);
});
return toReturn;
};
return module.exports;
});