view script2.js @ 8:7b2ca4d5af6d

Add a resend-whole-state button and make it works.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Thu, 02 Feb 2012 15:45:25 +0100
parents 853dcbe8f06f
children d3b37bbb2b88
line wrap: on
line source

'use strict';

/** Copyright (c) 2012 Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
  *
  * Permission is hereby granted, free of charge, to any person obtaining a copy
  * of this software and associated documentation files (the "Software"), to
  * deal in the Software without restriction, including without limitation the
  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  * sell copies of the Software, and to permit persons to whom the Software is
  * furnished to do so, subject to the following conditions:
  *
  * The above copyright notice and this permission notice shall be included in
  * all copies or substantial portions of the Software.
  *
  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  * IN THE SOFTWARE.
  */

var conn = new Lightstring.Connection(SERVICE);
var roster = {};
var documents = {};

Lightstring.plugins['jingle'] = {
  namespaces: {
    jingle: 'urn:xmpp:jingle:1',
    jingle_transports_sxe: 'urn:xmpp:jingle:transports:sxe',
    jingle_apps_xhtml: 'urn:xmpp:jingle:apps:xhtml'
  }
};
Lightstring.ns['sxe'] = 'urn:xmpp:sxe:0';

conn.on('iq/' + Lightstring.ns['disco#info'] + ':query', function(stanza) {
  if (stanza.DOM.getAttributeNS(null, 'type') !== 'get')
    return;

  var query = stanza.DOM.firstChild;
  if (query.getAttributeNS(null, 'node')) {
    var response = "<iq to='" + stanza.DOM.getAttributeNS(null, 'from') + "'" +
                      " id='" + stanza.DOM.getAttributeNS(null, 'id') + "'" +
                      " type='error'/>"; //TODO: precise the error.
    conn.send(response);
    return;
  }

  var features = [Lightstring.ns['sxe'], Lightstring.ns['jingle_transports_sxe']]; //TODO: put that elsewhere.

  var response = "<iq to='" + stanza.DOM.getAttributeNS(null, 'from') + "'" +
                    " id='" + stanza.DOM.getAttributeNS(null, 'id') + "'" +
                    " type='result'>" +
                   "<query xmlns='" + Lightstring.ns['disco#info'] + "'>" +
                     "<identity category='client' type='browser'/>";
  features.forEach(function(f) {
    response += "<feature var='" + f + "'/>";
  });
  response += "</query>" +
            "</iq>";

  conn.send(response);
});

conn.on('presence', function(stanza) {
  var from = new Lightstring.JID(stanza.DOM.getAttributeNS(null, 'from'));
  if (!from.equals(conn.jid)) {
    var type = stanza.DOM.getAttributeNS(null, 'type');
    if (!type)
      conn.disco.info(from, undefined, function(stanza) {
        roster[from.full] = {identities: stanza.identities, features: stanza.features};
      }, function(stanza) {
        console.log(from + ' doesn’t handle disco#info.');
        roster[from.full] = {};
      });
    else if (type === 'unavailable')
      delete roster[from.full];
  }
});

var host = function(sid, name) {
  if (!sid)
    sid = String(Math.random());

  documents[sid] = new Document(conn.jid.full, name, conn.jid.full, 'document');

  for (var jid in roster) {
    var contact = roster[jid];
    if ((contact.features.indexOf(Lightstring.ns['sxe']) !== -1) && (contact.features.indexOf(Lightstring.ns['jingle_transports_sxe']) !== -1))
      initiate(jid, sid);
  }
}

var initiate = function(jid, sid) {
  var initiate = "<iq to='" + jid + "' type='set'>" +
                   "<jingle xmlns='" + Lightstring.ns.jingle.main + "'" +
                          " action='session-initiate'" +
                          " initiator='" + documents[sid].initiator + "'" +
                          " sid='" + sid + "'>" +
                     "<content creator='initiator' name='" + documents[sid].name + "'>" +
                       "<description xmlns='" + Lightstring.ns['jingle_apps_xhtml'] + "'/>" +
                       "<transport xmlns='" + Lightstring.ns['jingle_transports_sxe'] + "'>" +
                         "<host>" + documents[sid].host + "</host>" +
                       "</transport>" +
                     "</content>" +
                   "</jingle>" +
                 "</iq>";
  conn.send(initiate);
};

var accept = function(sid) {
  var doc = documents[sid];
  var accept = "<iq to='" + doc.initiator + "' type='set'>" +
                 "<jingle xmlns='" + Lightstring.ns.jingle.main + "'" +
                        " action='session-accept'" +
                        " initiator='" + doc.initiator + "'" +
                        " sid='" + sid + "'>" +
                   "<content creator='initiator' name='" + documents[sid].name + "'>" +
                     "<description xmlns='" + Lightstring.ns['jingle_apps_xhtml'] + "'/>" +
                     "<transport xmlns='" + Lightstring.ns['jingle_transports_sxe'] + "'>" +
                       "<host>" + doc.host + "</host>" +
                     "</transport>" +
                   "</content>" +
                 "</jingle>" +
               "</iq>";
  conn.send(accept, function(stanza) {
    if (stanza.DOM.getAttributeNS(null, 'type') === 'result')
      connect(sid);
    else
      terminate('TODO'); //XXX
  });
};

var terminate = function(sid, reason) {
  if (!(sid in documents))
    return console.log('BIG WARNING!!!');

  var terminate = "<iq to='" + documents[sid].initiator + "' type='set'>" +
                    "<jingle xmlns='" + Lightstring.ns.jingle.main + "'" +
                           " action='session-terminate'" +
                           " host='" + documents[sid].host + "'" +
                           " initiator='" + documents[sid].initiator + "'" +
                           " sid='" + sid + "'>" +
                      "<reason>" +
                        "<" + reason + "/>" +
                      "</reason>" +
                    "</jingle>" +
                  "</iq>";
  delete documents[sid];
  conn.send(terminate);
};

conn.on('iq/' + Lightstring.ns.jingle + ':jingle', function(stanza) {
  conn.send("<iq to='" + stanza.DOM.getAttributeNS(null, 'from') + "'" +
               " id='" + stanza.DOM.getAttributeNS(null, 'id') + "'" +
               " type='result'/>");

  var jingle = stanza.DOM.firstChild;
  var action = jingle.getAttributeNS(null, 'action');
  var initiator = jingle.getAttributeNS(null, 'initiator');
  var sid = jingle.getAttributeNS(null, 'sid');

  var payload = jingle.firstChild; //TODO: what if they are multiple?

  //TODO: verify that the payload is really what we want.
  //TODO: use the reason even with content as payload.
  if (action === 'session-initiate') {
    var content = payload;
    var creator = content.getAttributeNS(null, 'creator');
    var name = content.getAttributeNS(null, 'name');

    if (documents[sid])
      return terminate(sid, 'alternative-session'); //TODO: The XEP: “and wishes to use that [previous] session instead”

    var description = content.getElementsByTagName('description')[0]; //TODO: supporte multiple applications.
    if (description.namespaceURI !== Lightstring.ns['jingle_apps_xhtml'])
      return terminate(sid, 'unsupported-applications');

    var transport = content.getElementsByTagName('transport')[0]; //TODO: supporte multiple transports.
    if (transport.namespaceURI !== Lightstring.ns['jingle_transports_sxe'])
      return terminate(sid, 'unsupported-transports');

    var host = transport.textContent; //TODO: verify the presence of the host element.

    documents[sid] = new Document(initiator, name, host, 'document');

    if (confirm('Do you accept?'))
      accept(sid);
    else
      terminate(sid, 'decline');

  } else if (action === 'session-accept') {
    var content = payload;
    var creator = content.getAttributeNS(null, 'creator');
    var name = content.getAttributeNS(null, 'name');

    alert('Accepted! \\o/');

    var description = content.getElementsByTagName('description')[0]; //TODO: supporte multiple applications.
    if (description.namespaceURI !== Lightstring.ns['jingle_apps_xhtml'])
      return terminate(sid, 'unsupported-applications');

    var transport = content.getElementsByTagName('transport')[0]; //TODO: supporte multiple transports.
    if (transport.namespaceURI !== Lightstring.ns['jingle_transports_sxe'])
      return terminate(sid, 'unsupported-transports');

    var host = transport.textContent; //TODO: verify the presence of the host element.

    var doc = documents[sid];
    if (doc.initiator !== initiator ||
        doc.name !== name ||
        doc.host !== host)
      return terminate(sid, 'unknown-error'); //XXX

  } else if (action === 'session-terminate') {
    var reason = payload;

    //TODO: verify they are what the should be.
    var element = reason.firstChild;
    var text = element.nextSibling;

    alert('Terminated, reason: ' + element.localName + (text? ' (' + text.textContent + ')': ''));

    delete documents[sid];
  }
});

var connect = function(sid) {
  var doc = documents[sid];
  var host = doc.host;
  var identities = roster[host].identities;
  var type = 'chat';
  identities.forEach(function(identity) {
    if (identity.category === 'conference')
      type = 'groupchat';
  });
  var message = "<message to='" + host + "'" +
                        " type='" + type + "'>" +
                  "<sxe xmlns='" + Lightstring.ns['sxe'] + "'" +
                      " id='" + Lightstring.newId('sxe') + "'" +
                      " session='" + sid + "'>" +
                    "<connect/>" +
                  "</sxe>" +
                "</message>";
  conn.send(message);
  doc.participants[conn.jid.full] = type;
};

var offerState = function(to, type, sid) {
  var message = "<message to='" + to + "'" +
                        " type='" + type + "'>" +
                  "<sxe xmlns='" + Lightstring.ns['sxe'] + "'" +
                      " id='" + Lightstring.newId('sxe') + "'" +
                      " session='" + sid + "'>" +
                    "<state-offer>" +
                      "<description xmlns='" + Lightstring.ns['jingle_apps_xhtml'] + "'/>" +
                    "</state-offer>" +
                  "</sxe>" +
                "</message>";
  conn.send(message);
};

var sendState = function(sid) {
  var doc = documents[sid];
  var initialState = doc.createState([]).map(function(element) {
    return Lightstring.DOM2XML(element);
  }).join('');
  if (conn.jid.full in doc.participants) { // participant
    var type = 'chat'; //XXX
    var message = "<message to='" + doc.host + "'" +
                          " type='" + type + "'>" +
                    "<sxe xmlns='" + Lightstring.ns['sxe'] + "'" +
                        " id='" + Lightstring.newId('sxe') + "'" +
                        " session='" + sid + "'>" +
                      "<state>" +
                        "<document-begin prolog='" + doc.prolog + "'/>" +
                         initialState +
                        "<document-end last-sender='' last-id=''/>" +
                      "</state>" +
                    "</sxe>" +
                  "</message>";
    conn.send(message);
  } else { // host
    for (var jid in doc.participants) {
      var type = doc.participants[jid];
      var message = "<message to='" + jid + "'" +
                            " type='" + type + "'>" +
                      "<sxe xmlns='" + Lightstring.ns['sxe'] + "'" +
                          " id='" + Lightstring.newId('sxe') + "'" +
                          " session='" + sid + "'>" +
                        "<state>" +
                          "<document-begin prolog='" + doc.prolog + "'/>" +
                           initialState +
                          "<document-end last-sender='' last-id=''/>" +
                        "</state>" +
                      "</sxe>" +
                    "</message>";
      conn.send(message);
    }
  }
};

conn.on('message/' + Lightstring.ns['sxe'] + ':sxe', function(stanza) {
  var from = stanza.DOM.getAttributeNS(null, 'from');
  var type = 'chat'; //TODO: always?
  var sxe = stanza.DOM.firstChild; //TODO: there can be multiple payloads.
  var id = sxe.getAttributeNS(null, 'id');
  var sid = sxe.getAttributeNS(null, 'session');

  var doc = documents[sid];
  if (!doc) {
    conn.send("<message to='" + from + "' type='error'/>"); //XXX
    return;
  }

  var payload = sxe.firstChild; //TODO: really?
  switch (payload.localName) {
    case 'connect':
      offerState(from, type, sid);
      doc.participants[from] = type;
      break;
    case 'state-offer':
      var description = payload.firstChild;
      if (description.namespaceURI !== Lightstring.ns['jingle_apps_xhtml'])
        return terminate(sid, 'unsupported-applications');

      var accept = /*false;
      if (from === doc.host || from === doc.initiator)
        accept =*/ true;

      //TODO: refuse if proposed multiple times.

      var message = "<message to='" + from + "'" +
                            " type='" + type + "'>" +
                      "<sxe xmlns='" + Lightstring.ns['sxe'] + "'" +
                          " id='" + Lightstring.newId('sxe') + "'" +
                          " session='" + sid + "'>" +
                        "<" + (accept? 'accept': 'refuse') + "-state/>" +
                      "</sxe>" +
                    "</message>";
      conn.send(message);
      break;
    case 'accept-state':
      sendState(sid);
      break;
    case 'refuse-state':
      terminate(sid, 'decline');
      break;
    case 'state':
      var elements = payload.children;
      doc.empty();
      doc.processState(from, elements);
      break;
    default:
      var elements = sxe.children;
      doc.processState(from, elements);
  }
});

(function() {
  var events = ['connected', 'conn-error', 'connecting', 'disconnected', 'disconnecting'];
  var state = document.getElementById('state');
  events.forEach(function(e) {
    conn.on(e, function() {
      state.innerHTML = e;
      return false;
    });
  });
})();

conn.on('error', function(a) {
  alert(a);
});

document.getElementById('host').addEventListener('click', function() {
  host(Lightstring.newId('sess'), 'My First XHTML Document');
}, false);

document.getElementById('resend-state').addEventListener('click', function() {
  for (var sid in documents)
    sendState(sid);
}, false);

conn.on('output', function(stanza) {
  console.log('out:', stanza.DOM || stanza.XML);
});
conn.on('input', function(stanza) {
  console.log('in:', stanza.DOM || stanza.XML);
});

conn.on('connected', function() {
  conn.presence.send(conn);
  return true;
});

conn.load('DIGEST-MD5');
conn.load('presence');
conn.load('disco');
conn.load('jingle');
conn.disco.addFeatures(Lightstring.ns['jingle_transports_sxe'], Lightstring.ns['sxe']);
conn.connect(JID + '/' + prompt('Resource?'), PASSWORD);