Browse Source

Merge branch 'converse-omemo'

JC Brand 7 years ago
parent
commit
11cc41d3a3
57 changed files with 14244 additions and 2026 deletions
  1. 4 3
      .eslintrc.json
  2. 1 0
      .gitignore
  3. 3295 0
      3rdparty/bytebuffer.js
  4. 1207 0
      3rdparty/long.js
  5. 5222 0
      3rdparty/protobuf.js
  6. 396 47
      css/converse.css
  7. 1 0
      dev.html
  8. 591 332
      dist/converse.js
  9. 1 1
      locale/af/LC_MESSAGES/converse.po
  10. 21 0
      mockup/chatbox.html
  11. 385 857
      package-lock.json
  12. 3 0
      package.json
  13. 2 0
      sass/_chatbox.scss
  14. 0 9
      sass/_controlbox.scss
  15. 4 0
      sass/_core.scss
  16. 10 4
      sass/_forms.scss
  17. 56 0
      sass/_modal.scss
  18. 0 7
      sass/_profile.scss
  19. 2 0
      sass/_variables.scss
  20. 3 1
      sass/converse.scss
  21. 47 45
      spec/bookmarks.js
  22. 161 122
      spec/chatbox.js
  23. 36 39
      spec/chatroom.js
  24. 16 11
      spec/controlbox.js
  25. 1 1
      spec/converse.js
  26. 23 7
      spec/mam.js
  27. 292 252
      spec/messages.js
  28. 19 15
      spec/minchats.js
  29. 971 0
      spec/omemo.js
  30. 5 5
      spec/presence.js
  31. 13 7
      spec/roomslist.js
  32. 0 11
      spec/roster.js
  33. 3 2
      spec/user-details-modal.js
  34. 4 2
      src/converse-bookmarks.js
  35. 53 38
      src/converse-chatboxes.js
  36. 19 16
      src/converse-chatview.js
  37. 1 0
      src/converse-core.js
  38. 0 1
      src/converse-disco.js
  39. 13 5
      src/converse-mam.js
  40. 2 1
      src/converse-message-view.js
  41. 1 5
      src/converse-minimize.js
  42. 10 0
      src/converse-modal.js
  43. 3 1
      src/converse-muc.js
  44. 1062 0
      src/converse-omemo.js
  45. 1 1
      src/converse-otr.js
  46. 14 5
      src/converse-profile.js
  47. 1 1
      src/converse.js
  48. 3 1
      src/templates/message.html
  49. 107 44
      src/templates/profile_modal.html
  50. 1 0
      src/templates/toolbar_omemo.html
  51. 44 13
      src/templates/user_details_modal.html
  52. 49 8
      src/utils/core.js
  53. 1 1
      src/utils/form.js
  54. 61 1
      tests/mock.js
  55. 0 102
      tests/runner-transpiled.js
  56. 2 1
      tests/runner.js
  57. 1 1
      tests/utils.js

+ 4 - 3
.eslintrc.json

@@ -9,12 +9,13 @@
     "plugins": ["lodash"],
     "plugins": ["lodash"],
     "extends": ["eslint:recommended", "plugin:lodash/canonical"],
     "extends": ["eslint:recommended", "plugin:lodash/canonical"],
     "globals": {
     "globals": {
+        "Uint8Array": true,
         "Promise": true,
         "Promise": true,
         "converse": true,
         "converse": true,
-        "window": true,
-        "sinon": true,
         "define": true,
         "define": true,
-        "require": true
+        "require": true,
+        "sinon": true,
+        "window": true
     },
     },
     "rules": {
     "rules": {
         "lodash/prefer-lodash-method": [2, {
         "lodash/prefer-lodash-method": [2, {

+ 1 - 0
.gitignore

@@ -12,6 +12,7 @@
 .idea
 .idea
 .su?
 .su?
 builds/*
 builds/*
+3rdparty/libsignal-protocol-javascript/
 *.map
 *.map
 dist/converse-no-dependencies-es2015.js
 dist/converse-no-dependencies-es2015.js
 
 

+ 3295 - 0
3rdparty/bytebuffer.js

@@ -0,0 +1,3295 @@
+/*
+ Copyright 2013-2014 Daniel Wirtz <dcode@dcode.io>
+
+ Licensed under the Apache License, Version 2.0 (the "License");
+ you may not use this file except in compliance with the License.
+ You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+ */
+
+/**
+ * @license ByteBuffer.js (c) 2013-2014 Daniel Wirtz <dcode@dcode.io>
+ * This version of ByteBuffer.js uses an ArrayBuffer as its backing buffer which is accessed through a DataView and is
+ * compatible with modern browsers.
+ * Released under the Apache License, Version 2.0
+ * see: https://github.com/dcodeIO/ByteBuffer.js for details
+ */ //
+(function(global) {
+    "use strict";
+
+    /**
+     * @param {function(new: Long, number, number, boolean=)=} Long
+     * @returns {function(new: ByteBuffer, number=, boolean=, boolean=)}}
+     * @inner
+     */
+    function loadByteBuffer(Long) {
+
+        /**
+         * Constructs a new ByteBuffer.
+         * @class The swiss army knife for binary data in JavaScript.
+         * @exports ByteBuffer
+         * @constructor
+         * @param {number=} capacity Initial capacity. Defaults to {@link ByteBuffer.DEFAULT_CAPACITY}.
+         * @param {boolean=} littleEndian Whether to use little or big endian byte order. Defaults to
+         *  {@link ByteBuffer.DEFAULT_ENDIAN}.
+         * @param {boolean=} noAssert Whether to skip assertions of offsets and values. Defaults to
+         *  {@link ByteBuffer.DEFAULT_NOASSERT}.
+         * @expose
+         */
+        var ByteBuffer = function(capacity, littleEndian, noAssert) {
+            if (typeof capacity     === 'undefined') capacity     = ByteBuffer.DEFAULT_CAPACITY;
+            if (typeof littleEndian === 'undefined') littleEndian = ByteBuffer.DEFAULT_ENDIAN;
+            if (typeof noAssert     === 'undefined') noAssert     = ByteBuffer.DEFAULT_NOASSERT;
+            if (!noAssert) {
+                capacity = capacity | 0;
+                if (capacity < 0)
+                    throw RangeError("Illegal capacity");
+                littleEndian = !!littleEndian;
+                noAssert = !!noAssert;
+            }
+
+            /**
+             * Backing buffer.
+             * @type {!ArrayBuffer}
+             * @expose
+             */
+            this.buffer = capacity === 0 ? EMPTY_BUFFER : new ArrayBuffer(capacity);
+
+            /**
+             * Data view to manipulate the backing buffer. Becomes `null` if the backing buffer has a capacity of `0`.
+             * @type {?DataView}
+             * @expose
+             */
+            this.view = capacity === 0 ? null : new DataView(this.buffer);
+
+            /**
+             * Absolute read/write offset.
+             * @type {number}
+             * @expose
+             * @see ByteBuffer#flip
+             * @see ByteBuffer#clear
+             */
+            this.offset = 0;
+
+            /**
+             * Marked offset.
+             * @type {number}
+             * @expose
+             * @see ByteBuffer#mark
+             * @see ByteBuffer#reset
+             */
+            this.markedOffset = -1;
+
+            /**
+             * Absolute limit of the contained data. Set to the backing buffer's capacity upon allocation.
+             * @type {number}
+             * @expose
+             * @see ByteBuffer#flip
+             * @see ByteBuffer#clear
+             */
+            this.limit = capacity;
+
+            /**
+             * Whether to use little endian byte order, defaults to `false` for big endian.
+             * @type {boolean}
+             * @expose
+             */
+            this.littleEndian = typeof littleEndian !== 'undefined' ? !!littleEndian : false;
+
+            /**
+             * Whether to skip assertions of offsets and values, defaults to `false`.
+             * @type {boolean}
+             * @expose
+             */
+            this.noAssert = !!noAssert;
+        };
+
+        /**
+         * ByteBuffer version.
+         * @type {string}
+         * @const
+         * @expose
+         */
+        ByteBuffer.VERSION = "3.5.5";
+
+        /**
+         * Little endian constant that can be used instead of its boolean value. Evaluates to `true`.
+         * @type {boolean}
+         * @const
+         * @expose
+         */
+        ByteBuffer.LITTLE_ENDIAN = true;
+
+        /**
+         * Big endian constant that can be used instead of its boolean value. Evaluates to `false`.
+         * @type {boolean}
+         * @const
+         * @expose
+         */
+        ByteBuffer.BIG_ENDIAN = false;
+
+        /**
+         * Default initial capacity of `16`.
+         * @type {number}
+         * @expose
+         */
+        ByteBuffer.DEFAULT_CAPACITY = 16;
+
+        /**
+         * Default endianess of `false` for big endian.
+         * @type {boolean}
+         * @expose
+         */
+        ByteBuffer.DEFAULT_ENDIAN = ByteBuffer.BIG_ENDIAN;
+
+        /**
+         * Default no assertions flag of `false`.
+         * @type {boolean}
+         * @expose
+         */
+        ByteBuffer.DEFAULT_NOASSERT = false;
+
+        /**
+         * A `Long` class for representing a 64-bit two's-complement integer value. May be `null` if Long.js has not been loaded
+         *  and int64 support is not available.
+         * @type {?Long}
+         * @const
+         * @see https://github.com/dcodeIO/Long.js
+         * @expose
+         */
+        ByteBuffer.Long = Long || null;
+
+        /**
+         * @alias ByteBuffer.prototype
+         * @inner
+         */
+        var ByteBufferPrototype = ByteBuffer.prototype;
+
+        // helpers
+
+        /**
+         * @type {!ArrayBuffer}
+         * @inner
+         */
+        var EMPTY_BUFFER = new ArrayBuffer(0);
+
+        /**
+         * String.fromCharCode reference for compile-time renaming.
+         * @type {function(...number):string}
+         * @inner
+         */
+        var stringFromCharCode = String.fromCharCode;
+
+        /**
+         * Creates a source function for a string.
+         * @param {string} s String to read from
+         * @returns {function():number|null} Source function returning the next char code respectively `null` if there are
+         *  no more characters left.
+         * @throws {TypeError} If the argument is invalid
+         * @inner
+         */
+        function stringSource(s) {
+            var i=0; return function() {
+                return i < s.length ? s.charCodeAt(i++) : null;
+            };
+        }
+
+        /**
+         * Creates a destination function for a string.
+         * @returns {function(number=):undefined|string} Destination function successively called with the next char code.
+         *  Returns the final string when called without arguments.
+         * @inner
+         */
+        function stringDestination() {
+            var cs = [], ps = []; return function() {
+                if (arguments.length === 0)
+                    return ps.join('')+stringFromCharCode.apply(String, cs);
+                if (cs.length + arguments.length > 1024)
+                    ps.push(stringFromCharCode.apply(String, cs)),
+                        cs.length = 0;
+                Array.prototype.push.apply(cs, arguments);
+            };
+        }
+
+        /**
+         * Allocates a new ByteBuffer backed by a buffer of the specified capacity.
+         * @param {number=} capacity Initial capacity. Defaults to {@link ByteBuffer.DEFAULT_CAPACITY}.
+         * @param {boolean=} littleEndian Whether to use little or big endian byte order. Defaults to
+         *  {@link ByteBuffer.DEFAULT_ENDIAN}.
+         * @param {boolean=} noAssert Whether to skip assertions of offsets and values. Defaults to
+         *  {@link ByteBuffer.DEFAULT_NOASSERT}.
+         * @returns {!ByteBuffer}
+         * @expose
+         */
+        ByteBuffer.allocate = function(capacity, littleEndian, noAssert) {
+            return new ByteBuffer(capacity, littleEndian, noAssert);
+        };
+
+        /**
+         * Concatenates multiple ByteBuffers into one.
+         * @param {!Array.<!ByteBuffer|!ArrayBuffer|!Uint8Array|string>} buffers Buffers to concatenate
+         * @param {(string|boolean)=} encoding String encoding if `buffers` contains a string ("base64", "hex", "binary",
+         *  defaults to "utf8")
+         * @param {boolean=} littleEndian Whether to use little or big endian byte order for the resulting ByteBuffer. Defaults
+         *  to {@link ByteBuffer.DEFAULT_ENDIAN}.
+         * @param {boolean=} noAssert Whether to skip assertions of offsets and values for the resulting ByteBuffer. Defaults to
+         *  {@link ByteBuffer.DEFAULT_NOASSERT}.
+         * @returns {!ByteBuffer} Concatenated ByteBuffer
+         * @expose
+         */
+        ByteBuffer.concat = function(buffers, encoding, littleEndian, noAssert) {
+            if (typeof encoding === 'boolean' || typeof encoding !== 'string') {
+                noAssert = littleEndian;
+                littleEndian = encoding;
+                encoding = undefined;
+            }
+            var capacity = 0;
+            for (var i=0, k=buffers.length, length; i<k; ++i) {
+                if (!ByteBuffer.isByteBuffer(buffers[i]))
+                    buffers[i] = ByteBuffer.wrap(buffers[i], encoding);
+                length = buffers[i].limit - buffers[i].offset;
+                if (length > 0) capacity += length;
+            }
+            if (capacity === 0)
+                return new ByteBuffer(0, littleEndian, noAssert);
+            var bb = new ByteBuffer(capacity, littleEndian, noAssert),
+                bi;
+            var view = new Uint8Array(bb.buffer);
+            i=0; while (i<k) {
+                bi = buffers[i++];
+                length = bi.limit - bi.offset;
+                if (length <= 0) continue;
+                view.set(new Uint8Array(bi.buffer).subarray(bi.offset, bi.limit), bb.offset);
+                bb.offset += length;
+            }
+            bb.limit = bb.offset;
+            bb.offset = 0;
+            return bb;
+        };
+
+        /**
+         * Tests if the specified type is a ByteBuffer.
+         * @param {*} bb ByteBuffer to test
+         * @returns {boolean} `true` if it is a ByteBuffer, otherwise `false`
+         * @expose
+         */
+        ByteBuffer.isByteBuffer = function(bb) {
+            return (bb && bb instanceof ByteBuffer) === true;
+        };
+        /**
+         * Gets the backing buffer type.
+         * @returns {Function} `Buffer` for NB builds, `ArrayBuffer` for AB builds (classes)
+         * @expose
+         */
+        ByteBuffer.type = function() {
+            return ArrayBuffer;
+        };
+
+        /**
+         * Wraps a buffer or a string. Sets the allocated ByteBuffer's {@link ByteBuffer#offset} to `0` and its
+         *  {@link ByteBuffer#limit} to the length of the wrapped data.
+         * @param {!ByteBuffer|!ArrayBuffer|!Uint8Array|string|!Array.<number>} buffer Anything that can be wrapped
+         * @param {(string|boolean)=} encoding String encoding if `buffer` is a string ("base64", "hex", "binary", defaults to
+         *  "utf8")
+         * @param {boolean=} littleEndian Whether to use little or big endian byte order. Defaults to
+         *  {@link ByteBuffer.DEFAULT_ENDIAN}.
+         * @param {boolean=} noAssert Whether to skip assertions of offsets and values. Defaults to
+         *  {@link ByteBuffer.DEFAULT_NOASSERT}.
+         * @returns {!ByteBuffer} A ByteBuffer wrapping `buffer`
+         * @expose
+         */
+        ByteBuffer.wrap = function(buffer, encoding, littleEndian, noAssert) {
+            if (typeof encoding !== 'string') {
+                noAssert = littleEndian;
+                littleEndian = encoding;
+                encoding = undefined;
+            }
+            if (typeof buffer === 'string') {
+                if (typeof encoding === 'undefined')
+                    encoding = "utf8";
+                switch (encoding) {
+                    case "base64":
+                        return ByteBuffer.fromBase64(buffer, littleEndian);
+                    case "hex":
+                        return ByteBuffer.fromHex(buffer, littleEndian);
+                    case "binary":
+                        return ByteBuffer.fromBinary(buffer, littleEndian);
+                    case "utf8":
+                        return ByteBuffer.fromUTF8(buffer, littleEndian);
+                    case "debug":
+                        return ByteBuffer.fromDebug(buffer, littleEndian);
+                    default:
+                        throw Error("Unsupported encoding: "+encoding);
+                }
+            }
+            if (buffer === null || typeof buffer !== 'object')
+                throw TypeError("Illegal buffer");
+            var bb;
+            if (ByteBuffer.isByteBuffer(buffer)) {
+                bb = ByteBufferPrototype.clone.call(buffer);
+                bb.markedOffset = -1;
+                return bb;
+            }
+            if (buffer instanceof Uint8Array) { // Extract ArrayBuffer from Uint8Array
+                bb = new ByteBuffer(0, littleEndian, noAssert);
+                if (buffer.length > 0) { // Avoid references to more than one EMPTY_BUFFER
+                    bb.buffer = buffer.buffer;
+                    bb.offset = buffer.byteOffset;
+                    bb.limit = buffer.byteOffset + buffer.length;
+                    bb.view = buffer.length > 0 ? new DataView(buffer.buffer) : null;
+                }
+            } else if (buffer instanceof ArrayBuffer) { // Reuse ArrayBuffer
+                bb = new ByteBuffer(0, littleEndian, noAssert);
+                if (buffer.byteLength > 0) {
+                    bb.buffer = buffer;
+                    bb.offset = 0;
+                    bb.limit = buffer.byteLength;
+                    bb.view = buffer.byteLength > 0 ? new DataView(buffer) : null;
+                }
+            } else if (Object.prototype.toString.call(buffer) === "[object Array]") { // Create from octets
+                bb = new ByteBuffer(buffer.length, littleEndian, noAssert);
+                bb.limit = buffer.length;
+                for (i=0; i<buffer.length; ++i)
+                    bb.view.setUint8(i, buffer[i]);
+            } else
+                throw TypeError("Illegal buffer"); // Otherwise fail
+            return bb;
+        };
+
+        // types/ints/int8
+
+        /**
+         * Writes an 8bit signed integer.
+         * @param {number} value Value to write
+         * @param {number=} offset Offset to write to. Will use and advance {@link ByteBuffer#offset} by `1` if omitted.
+         * @returns {!ByteBuffer} this
+         * @expose
+         */
+        ByteBufferPrototype.writeInt8 = function(value, offset) {
+            var relative = typeof offset === 'undefined';
+            if (relative) offset = this.offset;
+            if (!this.noAssert) {
+                if (typeof value !== 'number' || value % 1 !== 0)
+                    throw TypeError("Illegal value: "+value+" (not an integer)");
+                value |= 0;
+                if (typeof offset !== 'number' || offset % 1 !== 0)
+                    throw TypeError("Illegal offset: "+offset+" (not an integer)");
+                offset >>>= 0;
+                if (offset < 0 || offset + 0 > this.buffer.byteLength)
+                    throw RangeError("Illegal offset: 0 <= "+offset+" (+"+0+") <= "+this.buffer.byteLength);
+            }
+            offset += 1;
+            var capacity0 = this.buffer.byteLength;
+            if (offset > capacity0)
+                this.resize((capacity0 *= 2) > offset ? capacity0 : offset);
+            offset -= 1;
+            this.view.setInt8(offset, value);
+            if (relative) this.offset += 1;
+            return this;
+        };
+
+        /**
+         * Writes an 8bit signed integer. This is an alias of {@link ByteBuffer#writeInt8}.
+         * @function
+         * @param {number} value Value to write
+         * @param {number=} offset Offset to write to. Will use and advance {@link ByteBuffer#offset} by `1` if omitted.
+         * @returns {!ByteBuffer} this
+         * @expose
+         */
+        ByteBufferPrototype.writeByte = ByteBufferPrototype.writeInt8;
+
+        /**
+         * Reads an 8bit signed integer.
+         * @param {number=} offset Offset to read from. Will use and advance {@link ByteBuffer#offset} by `1` if omitted.
+         * @returns {number} Value read
+         * @expose
+         */
+        ByteBufferPrototype.readInt8 = function(offset) {
+            var relative = typeof offset === 'undefined';
+            if (relative) offset = this.offset;
+            if (!this.noAssert) {
+                if (typeof offset !== 'number' || offset % 1 !== 0)
+                    throw TypeError("Illegal offset: "+offset+" (not an integer)");
+                offset >>>= 0;
+                if (offset < 0 || offset + 1 > this.buffer.byteLength)
+                    throw RangeError("Illegal offset: 0 <= "+offset+" (+"+1+") <= "+this.buffer.byteLength);
+            }
+            var value = this.view.getInt8(offset);
+            if (relative) this.offset += 1;
+            return value;
+        };
+
+        /**
+         * Reads an 8bit signed integer. This is an alias of {@link ByteBuffer#readInt8}.
+         * @function
+         * @param {number=} offset Offset to read from. Will use and advance {@link ByteBuffer#offset} by `1` if omitted.
+         * @returns {number} Value read
+         * @expose
+         */
+        ByteBufferPrototype.readByte = ByteBufferPrototype.readInt8;
+
+        /**
+         * Writes an 8bit unsigned integer.
+         * @param {number} value Value to write
+         * @param {number=} offset Offset to write to. Will use and advance {@link ByteBuffer#offset} by `1` if omitted.
+         * @returns {!ByteBuffer} this
+         * @expose
+         */
+        ByteBufferPrototype.writeUint8 = function(value, offset) {
+            var relative = typeof offset === 'undefined';
+            if (relative) offset = this.offset;
+            if (!this.noAssert) {
+                if (typeof value !== 'number' || value % 1 !== 0)
+                    throw TypeError("Illegal value: "+value+" (not an integer)");
+                value >>>= 0;
+                if (typeof offset !== 'number' || offset % 1 !== 0)
+                    throw TypeError("Illegal offset: "+offset+" (not an integer)");
+                offset >>>= 0;
+                if (offset < 0 || offset + 0 > this.buffer.byteLength)
+                    throw RangeError("Illegal offset: 0 <= "+offset+" (+"+0+") <= "+this.buffer.byteLength);
+            }
+            offset += 1;
+            var capacity1 = this.buffer.byteLength;
+            if (offset > capacity1)
+                this.resize((capacity1 *= 2) > offset ? capacity1 : offset);
+            offset -= 1;
+            this.view.setUint8(offset, value);
+            if (relative) this.offset += 1;
+            return this;
+        };
+
+        /**
+         * Reads an 8bit unsigned integer.
+         * @param {number=} offset Offset to read from. Will use and advance {@link ByteBuffer#offset} by `1` if omitted.
+         * @returns {number} Value read
+         * @expose
+         */
+        ByteBufferPrototype.readUint8 = function(offset) {
+            var relative = typeof offset === 'undefined';
+            if (relative) offset = this.offset;
+            if (!this.noAssert) {
+                if (typeof offset !== 'number' || offset % 1 !== 0)
+                    throw TypeError("Illegal offset: "+offset+" (not an integer)");
+                offset >>>= 0;
+                if (offset < 0 || offset + 1 > this.buffer.byteLength)
+                    throw RangeError("Illegal offset: 0 <= "+offset+" (+"+1+") <= "+this.buffer.byteLength);
+            }
+            var value = this.view.getUint8(offset);
+            if (relative) this.offset += 1;
+            return value;
+        };
+
+        // types/ints/int16
+
+        /**
+         * Writes a 16bit signed integer.
+         * @param {number} value Value to write
+         * @param {number=} offset Offset to write to. Will use and advance {@link ByteBuffer#offset} by `2` if omitted.
+         * @throws {TypeError} If `offset` or `value` is not a valid number
+         * @throws {RangeError} If `offset` is out of bounds
+         * @expose
+         */
+        ByteBufferPrototype.writeInt16 = function(value, offset) {
+            var relative = typeof offset === 'undefined';
+            if (relative) offset = this.offset;
+            if (!this.noAssert) {
+                if (typeof value !== 'number' || value % 1 !== 0)
+                    throw TypeError("Illegal value: "+value+" (not an integer)");
+                value |= 0;
+                if (typeof offset !== 'number' || offset % 1 !== 0)
+                    throw TypeError("Illegal offset: "+offset+" (not an integer)");
+                offset >>>= 0;
+                if (offset < 0 || offset + 0 > this.buffer.byteLength)
+                    throw RangeError("Illegal offset: 0 <= "+offset+" (+"+0+") <= "+this.buffer.byteLength);
+            }
+            offset += 2;
+            var capacity2 = this.buffer.byteLength;
+            if (offset > capacity2)
+                this.resize((capacity2 *= 2) > offset ? capacity2 : offset);
+            offset -= 2;
+            this.view.setInt16(offset, value, this.littleEndian);
+            if (relative) this.offset += 2;
+            return this;
+        };
+
+        /**
+         * Writes a 16bit signed integer. This is an alias of {@link ByteBuffer#writeInt16}.
+         * @function
+         * @param {number} value Value to write
+         * @param {number=} offset Offset to write to. Will use and advance {@link ByteBuffer#offset} by `2` if omitted.
+         * @throws {TypeError} If `offset` or `value` is not a valid number
+         * @throws {RangeError} If `offset` is out of bounds
+         * @expose
+         */
+        ByteBufferPrototype.writeShort = ByteBufferPrototype.writeInt16;
+
+        /**
+         * Reads a 16bit signed integer.
+         * @param {number=} offset Offset to read from. Will use and advance {@link ByteBuffer#offset} by `2` if omitted.
+         * @returns {number} Value read
+         * @throws {TypeError} If `offset` is not a valid number
+         * @throws {RangeError} If `offset` is out of bounds
+         * @expose
+         */
+        ByteBufferPrototype.readInt16 = function(offset) {
+            var relative = typeof offset === 'undefined';
+            if (relative) offset = this.offset;
+            if (!this.noAssert) {
+                if (typeof offset !== 'number' || offset % 1 !== 0)
+                    throw TypeError("Illegal offset: "+offset+" (not an integer)");
+                offset >>>= 0;
+                if (offset < 0 || offset + 2 > this.buffer.byteLength)
+                    throw RangeError("Illegal offset: 0 <= "+offset+" (+"+2+") <= "+this.buffer.byteLength);
+            }
+            var value = this.view.getInt16(offset, this.littleEndian);
+            if (relative) this.offset += 2;
+            return value;
+        };
+
+        /**
+         * Reads a 16bit signed integer. This is an alias of {@link ByteBuffer#readInt16}.
+         * @function
+         * @param {number=} offset Offset to read from. Will use and advance {@link ByteBuffer#offset} by `2` if omitted.
+         * @returns {number} Value read
+         * @throws {TypeError} If `offset` is not a valid number
+         * @throws {RangeError} If `offset` is out of bounds
+         * @expose
+         */
+        ByteBufferPrototype.readShort = ByteBufferPrototype.readInt16;
+
+        /**
+         * Writes a 16bit unsigned integer.
+         * @param {number} value Value to write
+         * @param {number=} offset Offset to write to. Will use and advance {@link ByteBuffer#offset} by `2` if omitted.
+         * @throws {TypeError} If `offset` or `value` is not a valid number
+         * @throws {RangeError} If `offset` is out of bounds
+         * @expose
+         */
+        ByteBufferPrototype.writeUint16 = function(value, offset) {
+            var relative = typeof offset === 'undefined';
+            if (relative) offset = this.offset;
+            if (!this.noAssert) {
+                if (typeof value !== 'number' || value % 1 !== 0)
+                    throw TypeError("Illegal value: "+value+" (not an integer)");
+                value >>>= 0;
+                if (typeof offset !== 'number' || offset % 1 !== 0)
+                    throw TypeError("Illegal offset: "+offset+" (not an integer)");
+                offset >>>= 0;
+                if (offset < 0 || offset + 0 > this.buffer.byteLength)
+                    throw RangeError("Illegal offset: 0 <= "+offset+" (+"+0+") <= "+this.buffer.byteLength);
+            }
+            offset += 2;
+            var capacity3 = this.buffer.byteLength;
+            if (offset > capacity3)
+                this.resize((capacity3 *= 2) > offset ? capacity3 : offset);
+            offset -= 2;
+            this.view.setUint16(offset, value, this.littleEndian);
+            if (relative) this.offset += 2;
+            return this;
+        };
+
+        /**
+         * Reads a 16bit unsigned integer.
+         * @param {number=} offset Offset to read from. Will use and advance {@link ByteBuffer#offset} by `2` if omitted.
+         * @returns {number} Value read
+         * @throws {TypeError} If `offset` is not a valid number
+         * @throws {RangeError} If `offset` is out of bounds
+         * @expose
+         */
+        ByteBufferPrototype.readUint16 = function(offset) {
+            var relative = typeof offset === 'undefined';
+            if (relative) offset = this.offset;
+            if (!this.noAssert) {
+                if (typeof offset !== 'number' || offset % 1 !== 0)
+                    throw TypeError("Illegal offset: "+offset+" (not an integer)");
+                offset >>>= 0;
+                if (offset < 0 || offset + 2 > this.buffer.byteLength)
+                    throw RangeError("Illegal offset: 0 <= "+offset+" (+"+2+") <= "+this.buffer.byteLength);
+            }
+            var value = this.view.getUint16(offset, this.littleEndian);
+            if (relative) this.offset += 2;
+            return value;
+        };
+
+        // types/ints/int32
+
+        /**
+         * Writes a 32bit signed integer.
+         * @param {number} value Value to write
+         * @param {number=} offset Offset to write to. Will use and increase {@link ByteBuffer#offset} by `4` if omitted.
+         * @expose
+         */
+        ByteBufferPrototype.writeInt32 = function(value, offset) {
+            var relative = typeof offset === 'undefined';
+            if (relative) offset = this.offset;
+            if (!this.noAssert) {
+                if (typeof value !== 'number' || value % 1 !== 0)
+                    throw TypeError("Illegal value: "+value+" (not an integer)");
+                value |= 0;
+                if (typeof offset !== 'number' || offset % 1 !== 0)
+                    throw TypeError("Illegal offset: "+offset+" (not an integer)");
+                offset >>>= 0;
+                if (offset < 0 || offset + 0 > this.buffer.byteLength)
+                    throw RangeError("Illegal offset: 0 <= "+offset+" (+"+0+") <= "+this.buffer.byteLength);
+            }
+            offset += 4;
+            var capacity4 = this.buffer.byteLength;
+            if (offset > capacity4)
+                this.resize((capacity4 *= 2) > offset ? capacity4 : offset);
+            offset -= 4;
+            this.view.setInt32(offset, value, this.littleEndian);
+            if (relative) this.offset += 4;
+            return this;
+        };
+
+        /**
+         * Writes a 32bit signed integer. This is an alias of {@link ByteBuffer#writeInt32}.
+         * @param {number} value Value to write
+         * @param {number=} offset Offset to write to. Will use and increase {@link ByteBuffer#offset} by `4` if omitted.
+         * @expose
+         */
+        ByteBufferPrototype.writeInt = ByteBufferPrototype.writeInt32;
+
+        /**
+         * Reads a 32bit signed integer.
+         * @param {number=} offset Offset to read from. Will use and increase {@link ByteBuffer#offset} by `4` if omitted.
+         * @returns {number} Value read
+         * @expose
+         */
+        ByteBufferPrototype.readInt32 = function(offset) {
+            var relative = typeof offset === 'undefined';
+            if (relative) offset = this.offset;
+            if (!this.noAssert) {
+                if (typeof offset !== 'number' || offset % 1 !== 0)
+                    throw TypeError("Illegal offset: "+offset+" (not an integer)");
+                offset >>>= 0;
+                if (offset < 0 || offset + 4 > this.buffer.byteLength)
+                    throw RangeError("Illegal offset: 0 <= "+offset+" (+"+4+") <= "+this.buffer.byteLength);
+            }
+            var value = this.view.getInt32(offset, this.littleEndian);
+            if (relative) this.offset += 4;
+            return value;
+        };
+
+        /**
+         * Reads a 32bit signed integer. This is an alias of {@link ByteBuffer#readInt32}.
+         * @param {number=} offset Offset to read from. Will use and advance {@link ByteBuffer#offset} by `4` if omitted.
+         * @returns {number} Value read
+         * @expose
+         */
+        ByteBufferPrototype.readInt = ByteBufferPrototype.readInt32;
+
+        /**
+         * Writes a 32bit unsigned integer.
+         * @param {number} value Value to write
+         * @param {number=} offset Offset to write to. Will use and increase {@link ByteBuffer#offset} by `4` if omitted.
+         * @expose
+         */
+        ByteBufferPrototype.writeUint32 = function(value, offset) {
+            var relative = typeof offset === 'undefined';
+            if (relative) offset = this.offset;
+            if (!this.noAssert) {
+                if (typeof value !== 'number' || value % 1 !== 0)
+                    throw TypeError("Illegal value: "+value+" (not an integer)");
+                value >>>= 0;
+                if (typeof offset !== 'number' || offset % 1 !== 0)
+                    throw TypeError("Illegal offset: "+offset+" (not an integer)");
+                offset >>>= 0;
+                if (offset < 0 || offset + 0 > this.buffer.byteLength)
+                    throw RangeError("Illegal offset: 0 <= "+offset+" (+"+0+") <= "+this.buffer.byteLength);
+            }
+            offset += 4;
+            var capacity5 = this.buffer.byteLength;
+            if (offset > capacity5)
+                this.resize((capacity5 *= 2) > offset ? capacity5 : offset);
+            offset -= 4;
+            this.view.setUint32(offset, value, this.littleEndian);
+            if (relative) this.offset += 4;
+            return this;
+        };
+
+        /**
+         * Reads a 32bit unsigned integer.
+         * @param {number=} offset Offset to read from. Will use and increase {@link ByteBuffer#offset} by `4` if omitted.
+         * @returns {number} Value read
+         * @expose
+         */
+        ByteBufferPrototype.readUint32 = function(offset) {
+            var relative = typeof offset === 'undefined';
+            if (relative) offset = this.offset;
+            if (!this.noAssert) {
+                if (typeof offset !== 'number' || offset % 1 !== 0)
+                    throw TypeError("Illegal offset: "+offset+" (not an integer)");
+                offset >>>= 0;
+                if (offset < 0 || offset + 4 > this.buffer.byteLength)
+                    throw RangeError("Illegal offset: 0 <= "+offset+" (+"+4+") <= "+this.buffer.byteLength);
+            }
+            var value = this.view.getUint32(offset, this.littleEndian);
+            if (relative) this.offset += 4;
+            return value;
+        };
+
+        // types/ints/int64
+
+        if (Long) {
+
+            /**
+             * Writes a 64bit signed integer.
+             * @param {number|!Long} value Value to write
+             * @param {number=} offset Offset to write to. Will use and increase {@link ByteBuffer#offset} by `8` if omitted.
+             * @returns {!ByteBuffer} this
+             * @expose
+             */
+            ByteBufferPrototype.writeInt64 = function(value, offset) {
+                var relative = typeof offset === 'undefined';
+                if (relative) offset = this.offset;
+                if (!this.noAssert) {
+                    if (typeof value === 'number')
+                        value = Long.fromNumber(value);
+                    else if (typeof value === 'string')
+                        value = Long.fromString(value);
+                    else if (!(value && value instanceof Long))
+                        throw TypeError("Illegal value: "+value+" (not an integer or Long)");
+                    if (typeof offset !== 'number' || offset % 1 !== 0)
+                        throw TypeError("Illegal offset: "+offset+" (not an integer)");
+                    offset >>>= 0;
+                    if (offset < 0 || offset + 0 > this.buffer.byteLength)
+                        throw RangeError("Illegal offset: 0 <= "+offset+" (+"+0+") <= "+this.buffer.byteLength);
+                }
+                if (typeof value === 'number')
+                    value = Long.fromNumber(value);
+                else if (typeof value === 'string')
+                    value = Long.fromString(value);
+                offset += 8;
+                var capacity6 = this.buffer.byteLength;
+                if (offset > capacity6)
+                    this.resize((capacity6 *= 2) > offset ? capacity6 : offset);
+                offset -= 8;
+                if (this.littleEndian) {
+                    this.view.setInt32(offset  , value.low , true);
+                    this.view.setInt32(offset+4, value.high, true);
+                } else {
+                    this.view.setInt32(offset  , value.high, false);
+                    this.view.setInt32(offset+4, value.low , false);
+                }
+                if (relative) this.offset += 8;
+                return this;
+            };
+
+            /**
+             * Writes a 64bit signed integer. This is an alias of {@link ByteBuffer#writeInt64}.
+             * @param {number|!Long} value Value to write
+             * @param {number=} offset Offset to write to. Will use and increase {@link ByteBuffer#offset} by `8` if omitted.
+             * @returns {!ByteBuffer} this
+             * @expose
+             */
+            ByteBufferPrototype.writeLong = ByteBufferPrototype.writeInt64;
+
+            /**
+             * Reads a 64bit signed integer.
+             * @param {number=} offset Offset to read from. Will use and increase {@link ByteBuffer#offset} by `8` if omitted.
+             * @returns {!Long}
+             * @expose
+             */
+            ByteBufferPrototype.readInt64 = function(offset) {
+                var relative = typeof offset === 'undefined';
+                if (relative) offset = this.offset;
+                if (!this.noAssert) {
+                    if (typeof offset !== 'number' || offset % 1 !== 0)
+                        throw TypeError("Illegal offset: "+offset+" (not an integer)");
+                    offset >>>= 0;
+                    if (offset < 0 || offset + 8 > this.buffer.byteLength)
+                        throw RangeError("Illegal offset: 0 <= "+offset+" (+"+8+") <= "+this.buffer.byteLength);
+                }
+                var value = this.littleEndian
+                    ? new Long(this.view.getInt32(offset  , true ), this.view.getInt32(offset+4, true ), false)
+                    : new Long(this.view.getInt32(offset+4, false), this.view.getInt32(offset  , false), false);
+                if (relative) this.offset += 8;
+                return value;
+            };
+
+            /**
+             * Reads a 64bit signed integer. This is an alias of {@link ByteBuffer#readInt64}.
+             * @param {number=} offset Offset to read from. Will use and increase {@link ByteBuffer#offset} by `8` if omitted.
+             * @returns {!Long}
+             * @expose
+             */
+            ByteBufferPrototype.readLong = ByteBufferPrototype.readInt64;
+
+            /**
+             * Writes a 64bit unsigned integer.
+             * @param {number|!Long} value Value to write
+             * @param {number=} offset Offset to write to. Will use and increase {@link ByteBuffer#offset} by `8` if omitted.
+             * @returns {!ByteBuffer} this
+             * @expose
+             */
+            ByteBufferPrototype.writeUint64 = function(value, offset) {
+                var relative = typeof offset === 'undefined';
+                if (relative) offset = this.offset;
+                if (!this.noAssert) {
+                    if (typeof value === 'number')
+                        value = Long.fromNumber(value);
+                    else if (typeof value === 'string')
+                        value = Long.fromString(value);
+                    else if (!(value && value instanceof Long))
+                        throw TypeError("Illegal value: "+value+" (not an integer or Long)");
+                    if (typeof offset !== 'number' || offset % 1 !== 0)
+                        throw TypeError("Illegal offset: "+offset+" (not an integer)");
+                    offset >>>= 0;
+                    if (offset < 0 || offset + 0 > this.buffer.byteLength)
+                        throw RangeError("Illegal offset: 0 <= "+offset+" (+"+0+") <= "+this.buffer.byteLength);
+                }
+                if (typeof value === 'number')
+                    value = Long.fromNumber(value);
+                else if (typeof value === 'string')
+                    value = Long.fromString(value);
+                offset += 8;
+                var capacity7 = this.buffer.byteLength;
+                if (offset > capacity7)
+                    this.resize((capacity7 *= 2) > offset ? capacity7 : offset);
+                offset -= 8;
+                if (this.littleEndian) {
+                    this.view.setInt32(offset  , value.low , true);
+                    this.view.setInt32(offset+4, value.high, true);
+                } else {
+                    this.view.setInt32(offset  , value.high, false);
+                    this.view.setInt32(offset+4, value.low , false);
+                }
+                if (relative) this.offset += 8;
+                return this;
+            };
+
+            /**
+             * Reads a 64bit unsigned integer.
+             * @param {number=} offset Offset to read from. Will use and increase {@link ByteBuffer#offset} by `8` if omitted.
+             * @returns {!Long}
+             * @expose
+             */
+            ByteBufferPrototype.readUint64 = function(offset) {
+                var relative = typeof offset === 'undefined';
+                if (relative) offset = this.offset;
+                if (!this.noAssert) {
+                    if (typeof offset !== 'number' || offset % 1 !== 0)
+                        throw TypeError("Illegal offset: "+offset+" (not an integer)");
+                    offset >>>= 0;
+                    if (offset < 0 || offset + 8 > this.buffer.byteLength)
+                        throw RangeError("Illegal offset: 0 <= "+offset+" (+"+8+") <= "+this.buffer.byteLength);
+                }
+                var value = this.littleEndian
+                    ? new Long(this.view.getInt32(offset  , true ), this.view.getInt32(offset+4, true ), true)
+                    : new Long(this.view.getInt32(offset+4, false), this.view.getInt32(offset  , false), true);
+                if (relative) this.offset += 8;
+                return value;
+            };
+
+        } // Long
+
+
+        // types/floats/float32
+
+        /**
+         * Writes a 32bit float.
+         * @param {number} value Value to write
+         * @param {number=} offset Offset to write to. Will use and increase {@link ByteBuffer#offset} by `4` if omitted.
+         * @returns {!ByteBuffer} this
+         * @expose
+         */
+        ByteBufferPrototype.writeFloat32 = function(value, offset) {
+            var relative = typeof offset === 'undefined';
+            if (relative) offset = this.offset;
+            if (!this.noAssert) {
+                if (typeof value !== 'number')
+                    throw TypeError("Illegal value: "+value+" (not a number)");
+                if (typeof offset !== 'number' || offset % 1 !== 0)
+                    throw TypeError("Illegal offset: "+offset+" (not an integer)");
+                offset >>>= 0;
+                if (offset < 0 || offset + 0 > this.buffer.byteLength)
+                    throw RangeError("Illegal offset: 0 <= "+offset+" (+"+0+") <= "+this.buffer.byteLength);
+            }
+            offset += 4;
+            var capacity8 = this.buffer.byteLength;
+            if (offset > capacity8)
+                this.resize((capacity8 *= 2) > offset ? capacity8 : offset);
+            offset -= 4;
+            this.view.setFloat32(offset, value, this.littleEndian);
+            if (relative) this.offset += 4;
+            return this;
+        };
+
+        /**
+         * Writes a 32bit float. This is an alias of {@link ByteBuffer#writeFloat32}.
+         * @function
+         * @param {number} value Value to write
+         * @param {number=} offset Offset to write to. Will use and increase {@link ByteBuffer#offset} by `4` if omitted.
+         * @returns {!ByteBuffer} this
+         * @expose
+         */
+        ByteBufferPrototype.writeFloat = ByteBufferPrototype.writeFloat32;
+
+        /**
+         * Reads a 32bit float.
+         * @param {number=} offset Offset to read from. Will use and increase {@link ByteBuffer#offset} by `4` if omitted.
+         * @returns {number}
+         * @expose
+         */
+        ByteBufferPrototype.readFloat32 = function(offset) {
+            var relative = typeof offset === 'undefined';
+            if (relative) offset = this.offset;
+            if (!this.noAssert) {
+                if (typeof offset !== 'number' || offset % 1 !== 0)
+                    throw TypeError("Illegal offset: "+offset+" (not an integer)");
+                offset >>>= 0;
+                if (offset < 0 || offset + 4 > this.buffer.byteLength)
+                    throw RangeError("Illegal offset: 0 <= "+offset+" (+"+4+") <= "+this.buffer.byteLength);
+            }
+            var value = this.view.getFloat32(offset, this.littleEndian);
+            if (relative) this.offset += 4;
+            return value;
+        };
+
+        /**
+         * Reads a 32bit float. This is an alias of {@link ByteBuffer#readFloat32}.
+         * @function
+         * @param {number=} offset Offset to read from. Will use and increase {@link ByteBuffer#offset} by `4` if omitted.
+         * @returns {number}
+         * @expose
+         */
+        ByteBufferPrototype.readFloat = ByteBufferPrototype.readFloat32;
+
+        // types/floats/float64
+
+        /**
+         * Writes a 64bit float.
+         * @param {number} value Value to write
+         * @param {number=} offset Offset to write to. Will use and increase {@link ByteBuffer#offset} by `8` if omitted.
+         * @returns {!ByteBuffer} this
+         * @expose
+         */
+        ByteBufferPrototype.writeFloat64 = function(value, offset) {
+            var relative = typeof offset === 'undefined';
+            if (relative) offset = this.offset;
+            if (!this.noAssert) {
+                if (typeof value !== 'number')
+                    throw TypeError("Illegal value: "+value+" (not a number)");
+                if (typeof offset !== 'number' || offset % 1 !== 0)
+                    throw TypeError("Illegal offset: "+offset+" (not an integer)");
+                offset >>>= 0;
+                if (offset < 0 || offset + 0 > this.buffer.byteLength)
+                    throw RangeError("Illegal offset: 0 <= "+offset+" (+"+0+") <= "+this.buffer.byteLength);
+            }
+            offset += 8;
+            var capacity9 = this.buffer.byteLength;
+            if (offset > capacity9)
+                this.resize((capacity9 *= 2) > offset ? capacity9 : offset);
+            offset -= 8;
+            this.view.setFloat64(offset, value, this.littleEndian);
+            if (relative) this.offset += 8;
+            return this;
+        };
+
+        /**
+         * Writes a 64bit float. This is an alias of {@link ByteBuffer#writeFloat64}.
+         * @function
+         * @param {number} value Value to write
+         * @param {number=} offset Offset to write to. Will use and increase {@link ByteBuffer#offset} by `8` if omitted.
+         * @returns {!ByteBuffer} this
+         * @expose
+         */
+        ByteBufferPrototype.writeDouble = ByteBufferPrototype.writeFloat64;
+
+        /**
+         * Reads a 64bit float.
+         * @param {number=} offset Offset to read from. Will use and increase {@link ByteBuffer#offset} by `8` if omitted.
+         * @returns {number}
+         * @expose
+         */
+        ByteBufferPrototype.readFloat64 = function(offset) {
+            var relative = typeof offset === 'undefined';
+            if (relative) offset = this.offset;
+            if (!this.noAssert) {
+                if (typeof offset !== 'number' || offset % 1 !== 0)
+                    throw TypeError("Illegal offset: "+offset+" (not an integer)");
+                offset >>>= 0;
+                if (offset < 0 || offset + 8 > this.buffer.byteLength)
+                    throw RangeError("Illegal offset: 0 <= "+offset+" (+"+8+") <= "+this.buffer.byteLength);
+            }
+            var value = this.view.getFloat64(offset, this.littleEndian);
+            if (relative) this.offset += 8;
+            return value;
+        };
+
+        /**
+         * Reads a 64bit float. This is an alias of {@link ByteBuffer#readFloat64}.
+         * @function
+         * @param {number=} offset Offset to read from. Will use and increase {@link ByteBuffer#offset} by `8` if omitted.
+         * @returns {number}
+         * @expose
+         */
+        ByteBufferPrototype.readDouble = ByteBufferPrototype.readFloat64;
+
+
+        // types/varints/varint32
+
+        /**
+         * Maximum number of bytes required to store a 32bit base 128 variable-length integer.
+         * @type {number}
+         * @const
+         * @expose
+         */
+        ByteBuffer.MAX_VARINT32_BYTES = 5;
+
+        /**
+         * Calculates the actual number of bytes required to store a 32bit base 128 variable-length integer.
+         * @param {number} value Value to encode
+         * @returns {number} Number of bytes required. Capped to {@link ByteBuffer.MAX_VARINT32_BYTES}
+         * @expose
+         */
+        ByteBuffer.calculateVarint32 = function(value) {
+            // ref: src/google/protobuf/io/coded_stream.cc
+            value = value >>> 0;
+                 if (value < 1 << 7 ) return 1;
+            else if (value < 1 << 14) return 2;
+            else if (value < 1 << 21) return 3;
+            else if (value < 1 << 28) return 4;
+            else                      return 5;
+        };
+
+        /**
+         * Zigzag encodes a signed 32bit integer so that it can be effectively used with varint encoding.
+         * @param {number} n Signed 32bit integer
+         * @returns {number} Unsigned zigzag encoded 32bit integer
+         * @expose
+         */
+        ByteBuffer.zigZagEncode32 = function(n) {
+            return (((n |= 0) << 1) ^ (n >> 31)) >>> 0; // ref: src/google/protobuf/wire_format_lite.h
+        };
+
+        /**
+         * Decodes a zigzag encoded signed 32bit integer.
+         * @param {number} n Unsigned zigzag encoded 32bit integer
+         * @returns {number} Signed 32bit integer
+         * @expose
+         */
+        ByteBuffer.zigZagDecode32 = function(n) {
+            return ((n >>> 1) ^ -(n & 1)) | 0; // // ref: src/google/protobuf/wire_format_lite.h
+        };
+
+        /**
+         * Writes a 32bit base 128 variable-length integer.
+         * @param {number} value Value to write
+         * @param {number=} offset Offset to write to. Will use and increase {@link ByteBuffer#offset} by the number of bytes
+         *  written if omitted.
+         * @returns {!ByteBuffer|number} this if `offset` is omitted, else the actual number of bytes written
+         * @expose
+         */
+        ByteBufferPrototype.writeVarint32 = function(value, offset) {
+            var relative = typeof offset === 'undefined';
+            if (relative) offset = this.offset;
+            if (!this.noAssert) {
+                if (typeof value !== 'number' || value % 1 !== 0)
+                    throw TypeError("Illegal value: "+value+" (not an integer)");
+                value |= 0;
+                if (typeof offset !== 'number' || offset % 1 !== 0)
+                    throw TypeError("Illegal offset: "+offset+" (not an integer)");
+                offset >>>= 0;
+                if (offset < 0 || offset + 0 > this.buffer.byteLength)
+                    throw RangeError("Illegal offset: 0 <= "+offset+" (+"+0+") <= "+this.buffer.byteLength);
+            }
+            var size = ByteBuffer.calculateVarint32(value),
+                b;
+            offset += size;
+            var capacity10 = this.buffer.byteLength;
+            if (offset > capacity10)
+                this.resize((capacity10 *= 2) > offset ? capacity10 : offset);
+            offset -= size;
+            // ref: http://code.google.com/searchframe#WTeibokF6gE/trunk/src/google/protobuf/io/coded_stream.cc
+            this.view.setUint8(offset, b = value | 0x80);
+            value >>>= 0;
+            if (value >= 1 << 7) {
+                b = (value >> 7) | 0x80;
+                this.view.setUint8(offset+1, b);
+                if (value >= 1 << 14) {
+                    b = (value >> 14) | 0x80;
+                    this.view.setUint8(offset+2, b);
+                    if (value >= 1 << 21) {
+                        b = (value >> 21) | 0x80;
+                        this.view.setUint8(offset+3, b);
+                        if (value >= 1 << 28) {
+                            this.view.setUint8(offset+4, (value >> 28) & 0x0F);
+                            size = 5;
+                        } else {
+                            this.view.setUint8(offset+3, b & 0x7F);
+                            size = 4;
+                        }
+                    } else {
+                        this.view.setUint8(offset+2, b & 0x7F);
+                        size = 3;
+                    }
+                } else {
+                    this.view.setUint8(offset+1, b & 0x7F);
+                    size = 2;
+                }
+            } else {
+                this.view.setUint8(offset, b & 0x7F);
+                size = 1;
+            }
+            if (relative) {
+                this.offset += size;
+                return this;
+            }
+            return size;
+        };
+
+        /**
+         * Writes a zig-zag encoded 32bit base 128 variable-length integer.
+         * @param {number} value Value to write
+         * @param {number=} offset Offset to write to. Will use and increase {@link ByteBuffer#offset} by the number of bytes
+         *  written if omitted.
+         * @returns {!ByteBuffer|number} this if `offset` is omitted, else the actual number of bytes written
+         * @expose
+         */
+        ByteBufferPrototype.writeVarint32ZigZag = function(value, offset) {
+            return this.writeVarint32(ByteBuffer.zigZagEncode32(value), offset);
+        };
+
+        /**
+         * Reads a 32bit base 128 variable-length integer.
+         * @param {number=} offset Offset to read from. Will use and increase {@link ByteBuffer#offset} by the number of bytes
+         *  written if omitted.
+         * @returns {number|!{value: number, length: number}} The value read if offset is omitted, else the value read
+         *  and the actual number of bytes read.
+         * @throws {Error} If it's not a valid varint. Has a property `truncated = true` if there is not enough data available
+         *  to fully decode the varint.
+         * @expose
+         */
+        ByteBufferPrototype.readVarint32 = function(offset) {
+            var relative = typeof offset === 'undefined';
+            if (relative) offset = this.offset;
+            if (!this.noAssert) {
+                if (typeof offset !== 'number' || offset % 1 !== 0)
+                    throw TypeError("Illegal offset: "+offset+" (not an integer)");
+                offset >>>= 0;
+                if (offset < 0 || offset + 1 > this.buffer.byteLength)
+                    throw RangeError("Illegal offset: 0 <= "+offset+" (+"+1+") <= "+this.buffer.byteLength);
+            }
+            // ref: src/google/protobuf/io/coded_stream.cc
+            var size = 0,
+                value = 0 >>> 0,
+                temp,
+                ioffset;
+            do {
+                ioffset = offset+size;
+                if (!this.noAssert && ioffset > this.limit) {
+                    var err = Error("Truncated");
+                    err['truncated'] = true;
+                    throw err;
+                }
+                temp = this.view.getUint8(ioffset);
+                if (size < 5)
+                    value |= ((temp&0x7F)<<(7*size)) >>> 0;
+                ++size;
+            } while ((temp & 0x80) === 0x80);
+            value = value | 0; // Make sure to discard the higher order bits
+            if (relative) {
+                this.offset += size;
+                return value;
+            }
+            return {
+                "value": value,
+                "length": size
+            };
+        };
+
+        /**
+         * Reads a zig-zag encoded 32bit base 128 variable-length integer.
+         * @param {number=} offset Offset to read from. Will use and increase {@link ByteBuffer#offset} by the number of bytes
+         *  written if omitted.
+         * @returns {number|!{value: number, length: number}} The value read if offset is omitted, else the value read
+         *  and the actual number of bytes read.
+         * @throws {Error} If it's not a valid varint
+         * @expose
+         */
+        ByteBufferPrototype.readVarint32ZigZag = function(offset) {
+            var val = this.readVarint32(offset);
+            if (typeof val === 'object')
+                val["value"] = ByteBuffer.zigZagDecode32(val["value"]);
+            else
+                val = ByteBuffer.zigZagDecode32(val);
+            return val;
+        };
+
+        // types/varints/varint64
+
+        if (Long) {
+
+            /**
+             * Maximum number of bytes required to store a 64bit base 128 variable-length integer.
+             * @type {number}
+             * @const
+             * @expose
+             */
+            ByteBuffer.MAX_VARINT64_BYTES = 10;
+
+            /**
+             * Calculates the actual number of bytes required to store a 64bit base 128 variable-length integer.
+             * @param {number|!Long} value Value to encode
+             * @returns {number} Number of bytes required. Capped to {@link ByteBuffer.MAX_VARINT64_BYTES}
+             * @expose
+             */
+            ByteBuffer.calculateVarint64 = function(value) {
+                if (typeof value === 'number')
+                    value = Long.fromNumber(value);
+                else if (typeof value === 'string')
+                    value = Long.fromString(value);
+                // ref: src/google/protobuf/io/coded_stream.cc
+                var part0 = value.toInt() >>> 0,
+                    part1 = value.shiftRightUnsigned(28).toInt() >>> 0,
+                    part2 = value.shiftRightUnsigned(56).toInt() >>> 0;
+                if (part2 == 0) {
+                    if (part1 == 0) {
+                        if (part0 < 1 << 14)
+                            return part0 < 1 << 7 ? 1 : 2;
+                        else
+                            return part0 < 1 << 21 ? 3 : 4;
+                    } else {
+                        if (part1 < 1 << 14)
+                            return part1 < 1 << 7 ? 5 : 6;
+                        else
+                            return part1 < 1 << 21 ? 7 : 8;
+                    }
+                } else
+                    return part2 < 1 << 7 ? 9 : 10;
+            };
+
+            /**
+             * Zigzag encodes a signed 64bit integer so that it can be effectively used with varint encoding.
+             * @param {number|!Long} value Signed long
+             * @returns {!Long} Unsigned zigzag encoded long
+             * @expose
+             */
+            ByteBuffer.zigZagEncode64 = function(value) {
+                if (typeof value === 'number')
+                    value = Long.fromNumber(value, false);
+                else if (typeof value === 'string')
+                    value = Long.fromString(value, false);
+                else if (value.unsigned !== false) value = value.toSigned();
+                // ref: src/google/protobuf/wire_format_lite.h
+                return value.shiftLeft(1).xor(value.shiftRight(63)).toUnsigned();
+            };
+
+            /**
+             * Decodes a zigzag encoded signed 64bit integer.
+             * @param {!Long|number} value Unsigned zigzag encoded long or JavaScript number
+             * @returns {!Long} Signed long
+             * @expose
+             */
+            ByteBuffer.zigZagDecode64 = function(value) {
+                if (typeof value === 'number')
+                    value = Long.fromNumber(value, false);
+                else if (typeof value === 'string')
+                    value = Long.fromString(value, false);
+                else if (value.unsigned !== false) value = value.toSigned();
+                // ref: src/google/protobuf/wire_format_lite.h
+                return value.shiftRightUnsigned(1).xor(value.and(Long.ONE).toSigned().negate()).toSigned();
+            };
+
+            /**
+             * Writes a 64bit base 128 variable-length integer.
+             * @param {number|Long} value Value to write
+             * @param {number=} offset Offset to write to. Will use and increase {@link ByteBuffer#offset} by the number of bytes
+             *  written if omitted.
+             * @returns {!ByteBuffer|number} `this` if offset is omitted, else the actual number of bytes written.
+             * @expose
+             */
+            ByteBufferPrototype.writeVarint64 = function(value, offset) {
+                var relative = typeof offset === 'undefined';
+                if (relative) offset = this.offset;
+                if (!this.noAssert) {
+                    if (typeof value === 'number')
+                        value = Long.fromNumber(value);
+                    else if (typeof value === 'string')
+                        value = Long.fromString(value);
+                    else if (!(value && value instanceof Long))
+                        throw TypeError("Illegal value: "+value+" (not an integer or Long)");
+                    if (typeof offset !== 'number' || offset % 1 !== 0)
+                        throw TypeError("Illegal offset: "+offset+" (not an integer)");
+                    offset >>>= 0;
+                    if (offset < 0 || offset + 0 > this.buffer.byteLength)
+                        throw RangeError("Illegal offset: 0 <= "+offset+" (+"+0+") <= "+this.buffer.byteLength);
+                }
+                if (typeof value === 'number')
+                    value = Long.fromNumber(value, false);
+                else if (typeof value === 'string')
+                    value = Long.fromString(value, false);
+                else if (value.unsigned !== false) value = value.toSigned();
+                var size = ByteBuffer.calculateVarint64(value),
+                    part0 = value.toInt() >>> 0,
+                    part1 = value.shiftRightUnsigned(28).toInt() >>> 0,
+                    part2 = value.shiftRightUnsigned(56).toInt() >>> 0;
+                offset += size;
+                var capacity11 = this.buffer.byteLength;
+                if (offset > capacity11)
+                    this.resize((capacity11 *= 2) > offset ? capacity11 : offset);
+                offset -= size;
+                switch (size) {
+                    case 10: this.view.setUint8(offset+9, (part2 >>>  7) & 0x01);
+                    case 9 : this.view.setUint8(offset+8, size !== 9 ? (part2       ) | 0x80 : (part2       ) & 0x7F);
+                    case 8 : this.view.setUint8(offset+7, size !== 8 ? (part1 >>> 21) | 0x80 : (part1 >>> 21) & 0x7F);
+                    case 7 : this.view.setUint8(offset+6, size !== 7 ? (part1 >>> 14) | 0x80 : (part1 >>> 14) & 0x7F);
+                    case 6 : this.view.setUint8(offset+5, size !== 6 ? (part1 >>>  7) | 0x80 : (part1 >>>  7) & 0x7F);
+                    case 5 : this.view.setUint8(offset+4, size !== 5 ? (part1       ) | 0x80 : (part1       ) & 0x7F);
+                    case 4 : this.view.setUint8(offset+3, size !== 4 ? (part0 >>> 21) | 0x80 : (part0 >>> 21) & 0x7F);
+                    case 3 : this.view.setUint8(offset+2, size !== 3 ? (part0 >>> 14) | 0x80 : (part0 >>> 14) & 0x7F);
+                    case 2 : this.view.setUint8(offset+1, size !== 2 ? (part0 >>>  7) | 0x80 : (part0 >>>  7) & 0x7F);
+                    case 1 : this.view.setUint8(offset  , size !== 1 ? (part0       ) | 0x80 : (part0       ) & 0x7F);
+                }
+                if (relative) {
+                    this.offset += size;
+                    return this;
+                } else {
+                    return size;
+                }
+            };
+
+            /**
+             * Writes a zig-zag encoded 64bit base 128 variable-length integer.
+             * @param {number|Long} value Value to write
+             * @param {number=} offset Offset to write to. Will use and increase {@link ByteBuffer#offset} by the number of bytes
+             *  written if omitted.
+             * @returns {!ByteBuffer|number} `this` if offset is omitted, else the actual number of bytes written.
+             * @expose
+             */
+            ByteBufferPrototype.writeVarint64ZigZag = function(value, offset) {
+                return this.writeVarint64(ByteBuffer.zigZagEncode64(value), offset);
+            };
+
+            /**
+             * Reads a 64bit base 128 variable-length integer. Requires Long.js.
+             * @param {number=} offset Offset to read from. Will use and increase {@link ByteBuffer#offset} by the number of bytes
+             *  read if omitted.
+             * @returns {!Long|!{value: Long, length: number}} The value read if offset is omitted, else the value read and
+             *  the actual number of bytes read.
+             * @throws {Error} If it's not a valid varint
+             * @expose
+             */
+            ByteBufferPrototype.readVarint64 = function(offset) {
+                var relative = typeof offset === 'undefined';
+                if (relative) offset = this.offset;
+                if (!this.noAssert) {
+                    if (typeof offset !== 'number' || offset % 1 !== 0)
+                        throw TypeError("Illegal offset: "+offset+" (not an integer)");
+                    offset >>>= 0;
+                    if (offset < 0 || offset + 1 > this.buffer.byteLength)
+                        throw RangeError("Illegal offset: 0 <= "+offset+" (+"+1+") <= "+this.buffer.byteLength);
+                }
+                // ref: src/google/protobuf/io/coded_stream.cc
+                var start = offset,
+                    part0 = 0,
+                    part1 = 0,
+                    part2 = 0,
+                    b  = 0;
+                b = this.view.getUint8(offset++); part0  = (b & 0x7F)      ; if (b & 0x80) {
+                b = this.view.getUint8(offset++); part0 |= (b & 0x7F) <<  7; if (b & 0x80) {
+                b = this.view.getUint8(offset++); part0 |= (b & 0x7F) << 14; if (b & 0x80) {
+                b = this.view.getUint8(offset++); part0 |= (b & 0x7F) << 21; if (b & 0x80) {
+                b = this.view.getUint8(offset++); part1  = (b & 0x7F)      ; if (b & 0x80) {
+                b = this.view.getUint8(offset++); part1 |= (b & 0x7F) <<  7; if (b & 0x80) {
+                b = this.view.getUint8(offset++); part1 |= (b & 0x7F) << 14; if (b & 0x80) {
+                b = this.view.getUint8(offset++); part1 |= (b & 0x7F) << 21; if (b & 0x80) {
+                b = this.view.getUint8(offset++); part2  = (b & 0x7F)      ; if (b & 0x80) {
+                b = this.view.getUint8(offset++); part2 |= (b & 0x7F) <<  7; if (b & 0x80) {
+                throw Error("Buffer overrun"); }}}}}}}}}}
+                var value = Long.fromBits(part0 | (part1 << 28), (part1 >>> 4) | (part2) << 24, false);
+                if (relative) {
+                    this.offset = offset;
+                    return value;
+                } else {
+                    return {
+                        'value': value,
+                        'length': offset-start
+                    };
+                }
+            };
+
+            /**
+             * Reads a zig-zag encoded 64bit base 128 variable-length integer. Requires Long.js.
+             * @param {number=} offset Offset to read from. Will use and increase {@link ByteBuffer#offset} by the number of bytes
+             *  read if omitted.
+             * @returns {!Long|!{value: Long, length: number}} The value read if offset is omitted, else the value read and
+             *  the actual number of bytes read.
+             * @throws {Error} If it's not a valid varint
+             * @expose
+             */
+            ByteBufferPrototype.readVarint64ZigZag = function(offset) {
+                var val = this.readVarint64(offset);
+                if (val && val['value'] instanceof Long)
+                    val["value"] = ByteBuffer.zigZagDecode64(val["value"]);
+                else
+                    val = ByteBuffer.zigZagDecode64(val);
+                return val;
+            };
+
+        } // Long
+
+
+        // types/strings/cstring
+
+        /**
+         * Writes a NULL-terminated UTF8 encoded string. For this to work the specified string must not contain any NULL
+         *  characters itself.
+         * @param {string} str String to write
+         * @param {number=} offset Offset to write to. Will use and increase {@link ByteBuffer#offset} by the number of bytes
+         *  contained in `str` + 1 if omitted.
+         * @returns {!ByteBuffer|number} this if offset is omitted, else the actual number of bytes written
+         * @expose
+         */
+        ByteBufferPrototype.writeCString = function(str, offset) {
+            var relative = typeof offset === 'undefined';
+            if (relative) offset = this.offset;
+            var i,
+                k = str.length;
+            if (!this.noAssert) {
+                if (typeof str !== 'string')
+                    throw TypeError("Illegal str: Not a string");
+                for (i=0; i<k; ++i) {
+                    if (str.charCodeAt(i) === 0)
+                        throw RangeError("Illegal str: Contains NULL-characters");
+                }
+                if (typeof offset !== 'number' || offset % 1 !== 0)
+                    throw TypeError("Illegal offset: "+offset+" (not an integer)");
+                offset >>>= 0;
+                if (offset < 0 || offset + 0 > this.buffer.byteLength)
+                    throw RangeError("Illegal offset: 0 <= "+offset+" (+"+0+") <= "+this.buffer.byteLength);
+            }
+            // UTF8 strings do not contain zero bytes in between except for the zero character, so:
+            k = utfx.calculateUTF16asUTF8(stringSource(str))[1];
+            offset += k+1;
+            var capacity12 = this.buffer.byteLength;
+            if (offset > capacity12)
+                this.resize((capacity12 *= 2) > offset ? capacity12 : offset);
+            offset -= k+1;
+            utfx.encodeUTF16toUTF8(stringSource(str), function(b) {
+                this.view.setUint8(offset++, b);
+            }.bind(this));
+            this.view.setUint8(offset++, 0);
+            if (relative) {
+                this.offset = offset;
+                return this;
+            }
+            return k;
+        };
+
+        /**
+         * Reads a NULL-terminated UTF8 encoded string. For this to work the string read must not contain any NULL characters
+         *  itself.
+         * @param {number=} offset Offset to read from. Will use and increase {@link ByteBuffer#offset} by the number of bytes
+         *  read if omitted.
+         * @returns {string|!{string: string, length: number}} The string read if offset is omitted, else the string
+         *  read and the actual number of bytes read.
+         * @expose
+         */
+        ByteBufferPrototype.readCString = function(offset) {
+            var relative = typeof offset === 'undefined';
+            if (relative) offset = this.offset;
+            if (!this.noAssert) {
+                if (typeof offset !== 'number' || offset % 1 !== 0)
+                    throw TypeError("Illegal offset: "+offset+" (not an integer)");
+                offset >>>= 0;
+                if (offset < 0 || offset + 1 > this.buffer.byteLength)
+                    throw RangeError("Illegal offset: 0 <= "+offset+" (+"+1+") <= "+this.buffer.byteLength);
+            }
+            var start = offset,
+                temp;
+            // UTF8 strings do not contain zero bytes in between except for the zero character itself, so:
+            var sd, b = -1;
+            utfx.decodeUTF8toUTF16(function() {
+                if (b === 0) return null;
+                if (offset >= this.limit)
+                    throw RangeError("Illegal range: Truncated data, "+offset+" < "+this.limit);
+                return (b = this.view.getUint8(offset++)) === 0 ? null : b;
+            }.bind(this), sd = stringDestination(), true);
+            if (relative) {
+                this.offset = offset;
+                return sd();
+            } else {
+                return {
+                    "string": sd(),
+                    "length": offset - start
+                };
+            }
+        };
+
+        // types/strings/istring
+
+        /**
+         * Writes a length as uint32 prefixed UTF8 encoded string.
+         * @param {string} str String to write
+         * @param {number=} offset Offset to write to. Will use and increase {@link ByteBuffer#offset} by the number of bytes
+         *  written if omitted.
+         * @returns {!ByteBuffer|number} `this` if `offset` is omitted, else the actual number of bytes written
+         * @expose
+         * @see ByteBuffer#writeVarint32
+         */
+        ByteBufferPrototype.writeIString = function(str, offset) {
+            var relative = typeof offset === 'undefined';
+            if (relative) offset = this.offset;
+            if (!this.noAssert) {
+                if (typeof str !== 'string')
+                    throw TypeError("Illegal str: Not a string");
+                if (typeof offset !== 'number' || offset % 1 !== 0)
+                    throw TypeError("Illegal offset: "+offset+" (not an integer)");
+                offset >>>= 0;
+                if (offset < 0 || offset + 0 > this.buffer.byteLength)
+                    throw RangeError("Illegal offset: 0 <= "+offset+" (+"+0+") <= "+this.buffer.byteLength);
+            }
+            var start = offset,
+                k;
+            k = utfx.calculateUTF16asUTF8(stringSource(str), this.noAssert)[1];
+            offset += 4+k;
+            var capacity13 = this.buffer.byteLength;
+            if (offset > capacity13)
+                this.resize((capacity13 *= 2) > offset ? capacity13 : offset);
+            offset -= 4+k;
+            this.view.setUint32(offset, k, this.littleEndian);
+            offset += 4;
+            utfx.encodeUTF16toUTF8(stringSource(str), function(b) {
+                this.view.setUint8(offset++, b);
+            }.bind(this));
+            if (offset !== start + 4 + k)
+                throw RangeError("Illegal range: Truncated data, "+offset+" == "+(offset+4+k));
+            if (relative) {
+                this.offset = offset;
+                return this;
+            }
+            return offset - start;
+        };
+
+        /**
+         * Reads a length as uint32 prefixed UTF8 encoded string.
+         * @param {number=} offset Offset to read from. Will use and increase {@link ByteBuffer#offset} by the number of bytes
+         *  read if omitted.
+         * @returns {string|!{string: string, length: number}} The string read if offset is omitted, else the string
+         *  read and the actual number of bytes read.
+         * @expose
+         * @see ByteBuffer#readVarint32
+         */
+        ByteBufferPrototype.readIString = function(offset) {
+            var relative = typeof offset === 'undefined';
+            if (relative) offset = this.offset;
+            if (!this.noAssert) {
+                if (typeof offset !== 'number' || offset % 1 !== 0)
+                    throw TypeError("Illegal offset: "+offset+" (not an integer)");
+                offset >>>= 0;
+                if (offset < 0 || offset + 4 > this.buffer.byteLength)
+                    throw RangeError("Illegal offset: 0 <= "+offset+" (+"+4+") <= "+this.buffer.byteLength);
+            }
+            var temp = 0,
+                start = offset,
+                str;
+            temp = this.view.getUint32(offset, this.littleEndian);
+            offset += 4;
+            var k = offset + temp,
+                sd;
+            utfx.decodeUTF8toUTF16(function() {
+                return offset < k ? this.view.getUint8(offset++) : null;
+            }.bind(this), sd = stringDestination(), this.noAssert);
+            str = sd();
+            if (relative) {
+                this.offset = offset;
+                return str;
+            } else {
+                return {
+                    'string': str,
+                    'length': offset - start
+                };
+            }
+        };
+
+        // types/strings/utf8string
+
+        /**
+         * Metrics representing number of UTF8 characters. Evaluates to `c`.
+         * @type {string}
+         * @const
+         * @expose
+         */
+        ByteBuffer.METRICS_CHARS = 'c';
+
+        /**
+         * Metrics representing number of bytes. Evaluates to `b`.
+         * @type {string}
+         * @const
+         * @expose
+         */
+        ByteBuffer.METRICS_BYTES = 'b';
+
+        /**
+         * Writes an UTF8 encoded string.
+         * @param {string} str String to write
+         * @param {number=} offset Offset to write to. Will use and increase {@link ByteBuffer#offset} if omitted.
+         * @returns {!ByteBuffer|number} this if offset is omitted, else the actual number of bytes written.
+         * @expose
+         */
+        ByteBufferPrototype.writeUTF8String = function(str, offset) {
+            var relative = typeof offset === 'undefined';
+            if (relative) offset = this.offset;
+            if (!this.noAssert) {
+                if (typeof offset !== 'number' || offset % 1 !== 0)
+                    throw TypeError("Illegal offset: "+offset+" (not an integer)");
+                offset >>>= 0;
+                if (offset < 0 || offset + 0 > this.buffer.byteLength)
+                    throw RangeError("Illegal offset: 0 <= "+offset+" (+"+0+") <= "+this.buffer.byteLength);
+            }
+            var k;
+            var start = offset;
+            k = utfx.calculateUTF16asUTF8(stringSource(str))[1];
+            offset += k;
+            var capacity14 = this.buffer.byteLength;
+            if (offset > capacity14)
+                this.resize((capacity14 *= 2) > offset ? capacity14 : offset);
+            offset -= k;
+            utfx.encodeUTF16toUTF8(stringSource(str), function(b) {
+                this.view.setUint8(offset++, b);
+            }.bind(this));
+            if (relative) {
+                this.offset = offset;
+                return this;
+            }
+            return offset - start;
+        };
+
+        /**
+         * Writes an UTF8 encoded string. This is an alias of {@link ByteBuffer#writeUTF8String}.
+         * @function
+         * @param {string} str String to write
+         * @param {number=} offset Offset to write to. Will use and increase {@link ByteBuffer#offset} if omitted.
+         * @returns {!ByteBuffer|number} this if offset is omitted, else the actual number of bytes written.
+         * @expose
+         */
+        ByteBufferPrototype.writeString = ByteBufferPrototype.writeUTF8String;
+
+        /**
+         * Calculates the number of UTF8 characters of a string. JavaScript itself uses UTF-16, so that a string's
+         *  `length` property does not reflect its actual UTF8 size if it contains code points larger than 0xFFFF.
+         * @function
+         * @param {string} str String to calculate
+         * @returns {number} Number of UTF8 characters
+         * @expose
+         */
+        ByteBuffer.calculateUTF8Chars = function(str) {
+            return utfx.calculateUTF16asUTF8(stringSource(str))[0];
+        };
+
+        /**
+         * Calculates the number of UTF8 bytes of a string.
+         * @function
+         * @param {string} str String to calculate
+         * @returns {number} Number of UTF8 bytes
+         * @expose
+         */
+        ByteBuffer.calculateUTF8Bytes = function(str) {
+            return utfx.calculateUTF16asUTF8(stringSource(str))[1];
+        };
+
+        /**
+         * Reads an UTF8 encoded string.
+         * @param {number} length Number of characters or bytes to read.
+         * @param {string=} metrics Metrics specifying what `length` is meant to count. Defaults to
+         *  {@link ByteBuffer.METRICS_CHARS}.
+         * @param {number=} offset Offset to read from. Will use and increase {@link ByteBuffer#offset} by the number of bytes
+         *  read if omitted.
+         * @returns {string|!{string: string, length: number}} The string read if offset is omitted, else the string
+         *  read and the actual number of bytes read.
+         * @expose
+         */
+        ByteBufferPrototype.readUTF8String = function(length, metrics, offset) {
+            if (typeof metrics === 'number') {
+                offset = metrics;
+                metrics = undefined;
+            }
+            var relative = typeof offset === 'undefined';
+            if (relative) offset = this.offset;
+            if (typeof metrics === 'undefined') metrics = ByteBuffer.METRICS_CHARS;
+            if (!this.noAssert) {
+                if (typeof length !== 'number' || length % 1 !== 0)
+                    throw TypeError("Illegal length: "+length+" (not an integer)");
+                length |= 0;
+                if (typeof offset !== 'number' || offset % 1 !== 0)
+                    throw TypeError("Illegal offset: "+offset+" (not an integer)");
+                offset >>>= 0;
+                if (offset < 0 || offset + 0 > this.buffer.byteLength)
+                    throw RangeError("Illegal offset: 0 <= "+offset+" (+"+0+") <= "+this.buffer.byteLength);
+            }
+            var i = 0,
+                start = offset,
+                sd;
+            if (metrics === ByteBuffer.METRICS_CHARS) { // The same for node and the browser
+                sd = stringDestination();
+                utfx.decodeUTF8(function() {
+                    return i < length && offset < this.limit ? this.view.getUint8(offset++) : null;
+                }.bind(this), function(cp) {
+                    ++i; utfx.UTF8toUTF16(cp, sd);
+                }.bind(this));
+                if (i !== length)
+                    throw RangeError("Illegal range: Truncated data, "+i+" == "+length);
+                if (relative) {
+                    this.offset = offset;
+                    return sd();
+                } else {
+                    return {
+                        "string": sd(),
+                        "length": offset - start
+                    };
+                }
+            } else if (metrics === ByteBuffer.METRICS_BYTES) {
+                if (!this.noAssert) {
+                    if (typeof offset !== 'number' || offset % 1 !== 0)
+                        throw TypeError("Illegal offset: "+offset+" (not an integer)");
+                    offset >>>= 0;
+                    if (offset < 0 || offset + length > this.buffer.byteLength)
+                        throw RangeError("Illegal offset: 0 <= "+offset+" (+"+length+") <= "+this.buffer.byteLength);
+                }
+                var k = offset + length;
+                utfx.decodeUTF8toUTF16(function() {
+                    return offset < k ? this.view.getUint8(offset++) : null;
+                }.bind(this), sd = stringDestination(), this.noAssert);
+                if (offset !== k)
+                    throw RangeError("Illegal range: Truncated data, "+offset+" == "+k);
+                if (relative) {
+                    this.offset = offset;
+                    return sd();
+                } else {
+                    return {
+                        'string': sd(),
+                        'length': offset - start
+                    };
+                }
+            } else
+                throw TypeError("Unsupported metrics: "+metrics);
+        };
+
+        /**
+         * Reads an UTF8 encoded string. This is an alias of {@link ByteBuffer#readUTF8String}.
+         * @function
+         * @param {number} length Number of characters or bytes to read
+         * @param {number=} metrics Metrics specifying what `n` is meant to count. Defaults to
+         *  {@link ByteBuffer.METRICS_CHARS}.
+         * @param {number=} offset Offset to read from. Will use and increase {@link ByteBuffer#offset} by the number of bytes
+         *  read if omitted.
+         * @returns {string|!{string: string, length: number}} The string read if offset is omitted, else the string
+         *  read and the actual number of bytes read.
+         * @expose
+         */
+        ByteBufferPrototype.readString = ByteBufferPrototype.readUTF8String;
+
+        // types/strings/vstring
+
+        /**
+         * Writes a length as varint32 prefixed UTF8 encoded string.
+         * @param {string} str String to write
+         * @param {number=} offset Offset to write to. Will use and increase {@link ByteBuffer#offset} by the number of bytes
+         *  written if omitted.
+         * @returns {!ByteBuffer|number} `this` if `offset` is omitted, else the actual number of bytes written
+         * @expose
+         * @see ByteBuffer#writeVarint32
+         */
+        ByteBufferPrototype.writeVString = function(str, offset) {
+            var relative = typeof offset === 'undefined';
+            if (relative) offset = this.offset;
+            if (!this.noAssert) {
+                if (typeof str !== 'string')
+                    throw TypeError("Illegal str: Not a string");
+                if (typeof offset !== 'number' || offset % 1 !== 0)
+                    throw TypeError("Illegal offset: "+offset+" (not an integer)");
+                offset >>>= 0;
+                if (offset < 0 || offset + 0 > this.buffer.byteLength)
+                    throw RangeError("Illegal offset: 0 <= "+offset+" (+"+0+") <= "+this.buffer.byteLength);
+            }
+            var start = offset,
+                k, l;
+            k = utfx.calculateUTF16asUTF8(stringSource(str), this.noAssert)[1];
+            l = ByteBuffer.calculateVarint32(k);
+            offset += l+k;
+            var capacity15 = this.buffer.byteLength;
+            if (offset > capacity15)
+                this.resize((capacity15 *= 2) > offset ? capacity15 : offset);
+            offset -= l+k;
+            offset += this.writeVarint32(k, offset);
+            utfx.encodeUTF16toUTF8(stringSource(str), function(b) {
+                this.view.setUint8(offset++, b);
+            }.bind(this));
+            if (offset !== start+k+l)
+                throw RangeError("Illegal range: Truncated data, "+offset+" == "+(offset+k+l));
+            if (relative) {
+                this.offset = offset;
+                return this;
+            }
+            return offset - start;
+        };
+
+        /**
+         * Reads a length as varint32 prefixed UTF8 encoded string.
+         * @param {number=} offset Offset to read from. Will use and increase {@link ByteBuffer#offset} by the number of bytes
+         *  read if omitted.
+         * @returns {string|!{string: string, length: number}} The string read if offset is omitted, else the string
+         *  read and the actual number of bytes read.
+         * @expose
+         * @see ByteBuffer#readVarint32
+         */
+        ByteBufferPrototype.readVString = function(offset) {
+            var relative = typeof offset === 'undefined';
+            if (relative) offset = this.offset;
+            if (!this.noAssert) {
+                if (typeof offset !== 'number' || offset % 1 !== 0)
+                    throw TypeError("Illegal offset: "+offset+" (not an integer)");
+                offset >>>= 0;
+                if (offset < 0 || offset + 1 > this.buffer.byteLength)
+                    throw RangeError("Illegal offset: 0 <= "+offset+" (+"+1+") <= "+this.buffer.byteLength);
+            }
+            var temp = this.readVarint32(offset),
+                start = offset,
+                str;
+            offset += temp['length'];
+            temp = temp['value'];
+            var k = offset + temp,
+                sd = stringDestination();
+            utfx.decodeUTF8toUTF16(function() {
+                return offset < k ? this.view.getUint8(offset++) : null;
+            }.bind(this), sd, this.noAssert);
+            str = sd();
+            if (relative) {
+                this.offset = offset;
+                return str;
+            } else {
+                return {
+                    'string': str,
+                    'length': offset - start
+                };
+            }
+        };
+
+
+        /**
+         * Appends some data to this ByteBuffer. This will overwrite any contents behind the specified offset up to the appended
+         *  data's length.
+         * @param {!ByteBuffer|!ArrayBuffer|!Uint8Array|string} source Data to append. If `source` is a ByteBuffer, its offsets
+         *  will be modified according to the performed read operation.
+         * @param {(string|number)=} encoding Encoding if `data` is a string ("base64", "hex", "binary", defaults to "utf8")
+         * @param {number=} offset Offset to append at. Will use and increase {@link ByteBuffer#offset} by the number of bytes
+         *  read if omitted.
+         * @returns {!ByteBuffer} this
+         * @expose
+         * @example A relative `<01 02>03.append(<04 05>)` will result in `<01 02 04 05>, 04 05|`
+         * @example An absolute `<01 02>03.append(04 05>, 1)` will result in `<01 04>05, 04 05|`
+         */
+        ByteBufferPrototype.append = function(source, encoding, offset) {
+            if (typeof encoding === 'number' || typeof encoding !== 'string') {
+                offset = encoding;
+                encoding = undefined;
+            }
+            var relative = typeof offset === 'undefined';
+            if (relative) offset = this.offset;
+            if (!this.noAssert) {
+                if (typeof offset !== 'number' || offset % 1 !== 0)
+                    throw TypeError("Illegal offset: "+offset+" (not an integer)");
+                offset >>>= 0;
+                if (offset < 0 || offset + 0 > this.buffer.byteLength)
+                    throw RangeError("Illegal offset: 0 <= "+offset+" (+"+0+") <= "+this.buffer.byteLength);
+            }
+            if (!(source instanceof ByteBuffer))
+                source = ByteBuffer.wrap(source, encoding);
+            var length = source.limit - source.offset;
+            if (length <= 0) return this; // Nothing to append
+            offset += length;
+            var capacity16 = this.buffer.byteLength;
+            if (offset > capacity16)
+                this.resize((capacity16 *= 2) > offset ? capacity16 : offset);
+            offset -= length;
+            new Uint8Array(this.buffer, offset).set(new Uint8Array(source.buffer).subarray(source.offset, source.limit));
+            source.offset += length;
+            if (relative) this.offset += length;
+            return this;
+        };
+
+        /**
+         * Appends this ByteBuffer's contents to another ByteBuffer. This will overwrite any contents at and after the
+            specified offset up to the length of this ByteBuffer's data.
+         * @param {!ByteBuffer} target Target ByteBuffer
+         * @param {number=} offset Offset to append to. Will use and increase {@link ByteBuffer#offset} by the number of bytes
+         *  read if omitted.
+         * @returns {!ByteBuffer} this
+         * @expose
+         * @see ByteBuffer#append
+         */
+        ByteBufferPrototype.appendTo = function(target, offset) {
+            target.append(this, offset);
+            return this;
+        };
+
+        /**
+         * Enables or disables assertions of argument types and offsets. Assertions are enabled by default but you can opt to
+         *  disable them if your code already makes sure that everything is valid.
+         * @param {boolean} assert `true` to enable assertions, otherwise `false`
+         * @returns {!ByteBuffer} this
+         * @expose
+         */
+        ByteBufferPrototype.assert = function(assert) {
+            this.noAssert = !assert;
+            return this;
+        };
+
+        /**
+         * Gets the capacity of this ByteBuffer's backing buffer.
+         * @returns {number} Capacity of the backing buffer
+         * @expose
+         */
+        ByteBufferPrototype.capacity = function() {
+            return this.buffer.byteLength;
+        };
+
+        /**
+         * Clears this ByteBuffer's offsets by setting {@link ByteBuffer#offset} to `0` and {@link ByteBuffer#limit} to the
+         *  backing buffer's capacity. Discards {@link ByteBuffer#markedOffset}.
+         * @returns {!ByteBuffer} this
+         * @expose
+         */
+        ByteBufferPrototype.clear = function() {
+            this.offset = 0;
+            this.limit = this.buffer.byteLength;
+            this.markedOffset = -1;
+            return this;
+        };
+
+        /**
+         * Creates a cloned instance of this ByteBuffer, preset with this ByteBuffer's values for {@link ByteBuffer#offset},
+         *  {@link ByteBuffer#markedOffset} and {@link ByteBuffer#limit}.
+         * @param {boolean=} copy Whether to copy the backing buffer or to return another view on the same, defaults to `false`
+         * @returns {!ByteBuffer} Cloned instance
+         * @expose
+         */
+        ByteBufferPrototype.clone = function(copy) {
+            var bb = new ByteBuffer(0, this.littleEndian, this.noAssert);
+            if (copy) {
+                var buffer = new ArrayBuffer(this.buffer.byteLength);
+                new Uint8Array(buffer).set(this.buffer);
+                bb.buffer = buffer;
+                bb.view = new DataView(buffer);
+            } else {
+                bb.buffer = this.buffer;
+                bb.view = this.view;
+            }
+            bb.offset = this.offset;
+            bb.markedOffset = this.markedOffset;
+            bb.limit = this.limit;
+            return bb;
+        };
+
+        /**
+         * Compacts this ByteBuffer to be backed by a {@link ByteBuffer#buffer} of its contents' length. Contents are the bytes
+         *  between {@link ByteBuffer#offset} and {@link ByteBuffer#limit}. Will set `offset = 0` and `limit = capacity` and
+         *  adapt {@link ByteBuffer#markedOffset} to the same relative position if set.
+         * @param {number=} begin Offset to start at, defaults to {@link ByteBuffer#offset}
+         * @param {number=} end Offset to end at, defaults to {@link ByteBuffer#limit}
+         * @returns {!ByteBuffer} this
+         * @expose
+         */
+        ByteBufferPrototype.compact = function(begin, end) {
+            if (typeof begin === 'undefined') begin = this.offset;
+            if (typeof end === 'undefined') end = this.limit;
+            if (!this.noAssert) {
+                if (typeof begin !== 'number' || begin % 1 !== 0)
+                    throw TypeError("Illegal begin: Not an integer");
+                begin >>>= 0;
+                if (typeof end !== 'number' || end % 1 !== 0)
+                    throw TypeError("Illegal end: Not an integer");
+                end >>>= 0;
+                if (begin < 0 || begin > end || end > this.buffer.byteLength)
+                    throw RangeError("Illegal range: 0 <= "+begin+" <= "+end+" <= "+this.buffer.byteLength);
+            }
+            if (begin === 0 && end === this.buffer.byteLength)
+                return this; // Already compacted
+            var len = end - begin;
+            if (len === 0) {
+                this.buffer = EMPTY_BUFFER;
+                this.view = null;
+                if (this.markedOffset >= 0) this.markedOffset -= begin;
+                this.offset = 0;
+                this.limit = 0;
+                return this;
+            }
+            var buffer = new ArrayBuffer(len);
+            new Uint8Array(buffer).set(new Uint8Array(this.buffer).subarray(begin, end));
+            this.buffer = buffer;
+            this.view = new DataView(buffer);
+            if (this.markedOffset >= 0) this.markedOffset -= begin;
+            this.offset = 0;
+            this.limit = len;
+            return this;
+        };
+
+        /**
+         * Creates a copy of this ByteBuffer's contents. Contents are the bytes between {@link ByteBuffer#offset} and
+         *  {@link ByteBuffer#limit}.
+         * @param {number=} begin Begin offset, defaults to {@link ByteBuffer#offset}.
+         * @param {number=} end End offset, defaults to {@link ByteBuffer#limit}.
+         * @returns {!ByteBuffer} Copy
+         * @expose
+         */
+        ByteBufferPrototype.copy = function(begin, end) {
+            if (typeof begin === 'undefined') begin = this.offset;
+            if (typeof end === 'undefined') end = this.limit;
+            if (!this.noAssert) {
+                if (typeof begin !== 'number' || begin % 1 !== 0)
+                    throw TypeError("Illegal begin: Not an integer");
+                begin >>>= 0;
+                if (typeof end !== 'number' || end % 1 !== 0)
+                    throw TypeError("Illegal end: Not an integer");
+                end >>>= 0;
+                if (begin < 0 || begin > end || end > this.buffer.byteLength)
+                    throw RangeError("Illegal range: 0 <= "+begin+" <= "+end+" <= "+this.buffer.byteLength);
+            }
+            if (begin === end)
+                return new ByteBuffer(0, this.littleEndian, this.noAssert);
+            var capacity = end - begin,
+                bb = new ByteBuffer(capacity, this.littleEndian, this.noAssert);
+            bb.offset = 0;
+            bb.limit = capacity;
+            if (bb.markedOffset >= 0) bb.markedOffset -= begin;
+            this.copyTo(bb, 0, begin, end);
+            return bb;
+        };
+
+        /**
+         * Copies this ByteBuffer's contents to another ByteBuffer. Contents are the bytes between {@link ByteBuffer#offset} and
+         *  {@link ByteBuffer#limit}.
+         * @param {!ByteBuffer} target Target ByteBuffer
+         * @param {number=} targetOffset Offset to copy to. Will use and increase the target's {@link ByteBuffer#offset}
+         *  by the number of bytes copied if omitted.
+         * @param {number=} sourceOffset Offset to start copying from. Will use and increase {@link ByteBuffer#offset} by the
+         *  number of bytes copied if omitted.
+         * @param {number=} sourceLimit Offset to end copying from, defaults to {@link ByteBuffer#limit}
+         * @returns {!ByteBuffer} this
+         * @expose
+         */
+        ByteBufferPrototype.copyTo = function(target, targetOffset, sourceOffset, sourceLimit) {
+            var relative,
+                targetRelative;
+            if (!this.noAssert) {
+                if (!ByteBuffer.isByteBuffer(target))
+                    throw TypeError("Illegal target: Not a ByteBuffer");
+            }
+            targetOffset = (targetRelative = typeof targetOffset === 'undefined') ? target.offset : targetOffset | 0;
+            sourceOffset = (relative = typeof sourceOffset === 'undefined') ? this.offset : sourceOffset | 0;
+            sourceLimit = typeof sourceLimit === 'undefined' ? this.limit : sourceLimit | 0;
+
+            if (targetOffset < 0 || targetOffset > target.buffer.byteLength)
+                throw RangeError("Illegal target range: 0 <= "+targetOffset+" <= "+target.buffer.byteLength);
+            if (sourceOffset < 0 || sourceLimit > this.buffer.byteLength)
+                throw RangeError("Illegal source range: 0 <= "+sourceOffset+" <= "+this.buffer.byteLength);
+
+            var len = sourceLimit - sourceOffset;
+            if (len === 0)
+                return target; // Nothing to copy
+
+            target.ensureCapacity(targetOffset + len);
+
+            new Uint8Array(target.buffer).set(new Uint8Array(this.buffer).subarray(sourceOffset, sourceLimit), targetOffset);
+
+            if (relative) this.offset += len;
+            if (targetRelative) target.offset += len;
+
+            return this;
+        };
+
+        /**
+         * Makes sure that this ByteBuffer is backed by a {@link ByteBuffer#buffer} of at least the specified capacity. If the
+         *  current capacity is exceeded, it will be doubled. If double the current capacity is less than the required capacity,
+         *  the required capacity will be used instead.
+         * @param {number} capacity Required capacity
+         * @returns {!ByteBuffer} this
+         * @expose
+         */
+        ByteBufferPrototype.ensureCapacity = function(capacity) {
+            var current = this.buffer.byteLength;
+            if (current < capacity)
+                return this.resize((current *= 2) > capacity ? current : capacity);
+            return this;
+        };
+
+        /**
+         * Overwrites this ByteBuffer's contents with the specified value. Contents are the bytes between
+         *  {@link ByteBuffer#offset} and {@link ByteBuffer#limit}.
+         * @param {number|string} value Byte value to fill with. If given as a string, the first character is used.
+         * @param {number=} begin Begin offset. Will use and increase {@link ByteBuffer#offset} by the number of bytes
+         *  written if omitted. defaults to {@link ByteBuffer#offset}.
+         * @param {number=} end End offset, defaults to {@link ByteBuffer#limit}.
+         * @returns {!ByteBuffer} this
+         * @expose
+         * @example `someByteBuffer.clear().fill(0)` fills the entire backing buffer with zeroes
+         */
+        ByteBufferPrototype.fill = function(value, begin, end) {
+            var relative = typeof begin === 'undefined';
+            if (relative) begin = this.offset;
+            if (typeof value === 'string' && value.length > 0)
+                value = value.charCodeAt(0);
+            if (typeof begin === 'undefined') begin = this.offset;
+            if (typeof end === 'undefined') end = this.limit;
+            if (!this.noAssert) {
+                if (typeof value !== 'number' || value % 1 !== 0)
+                    throw TypeError("Illegal value: "+value+" (not an integer)");
+                value |= 0;
+                if (typeof begin !== 'number' || begin % 1 !== 0)
+                    throw TypeError("Illegal begin: Not an integer");
+                begin >>>= 0;
+                if (typeof end !== 'number' || end % 1 !== 0)
+                    throw TypeError("Illegal end: Not an integer");
+                end >>>= 0;
+                if (begin < 0 || begin > end || end > this.buffer.byteLength)
+                    throw RangeError("Illegal range: 0 <= "+begin+" <= "+end+" <= "+this.buffer.byteLength);
+            }
+            if (begin >= end)
+                return this; // Nothing to fill
+            while (begin < end) this.view.setUint8(begin++, value);
+            if (relative) this.offset = begin;
+            return this;
+        };
+
+        /**
+         * Makes this ByteBuffer ready for a new sequence of write or relative read operations. Sets `limit = offset` and
+         *  `offset = 0`. Make sure always to flip a ByteBuffer when all relative read or write operations are complete.
+         * @returns {!ByteBuffer} this
+         * @expose
+         */
+        ByteBufferPrototype.flip = function() {
+            this.limit = this.offset;
+            this.offset = 0;
+            return this;
+        };
+        /**
+         * Marks an offset on this ByteBuffer to be used later.
+         * @param {number=} offset Offset to mark. Defaults to {@link ByteBuffer#offset}.
+         * @returns {!ByteBuffer} this
+         * @throws {TypeError} If `offset` is not a valid number
+         * @throws {RangeError} If `offset` is out of bounds
+         * @see ByteBuffer#reset
+         * @expose
+         */
+        ByteBufferPrototype.mark = function(offset) {
+            offset = typeof offset === 'undefined' ? this.offset : offset;
+            if (!this.noAssert) {
+                if (typeof offset !== 'number' || offset % 1 !== 0)
+                    throw TypeError("Illegal offset: "+offset+" (not an integer)");
+                offset >>>= 0;
+                if (offset < 0 || offset + 0 > this.buffer.byteLength)
+                    throw RangeError("Illegal offset: 0 <= "+offset+" (+"+0+") <= "+this.buffer.byteLength);
+            }
+            this.markedOffset = offset;
+            return this;
+        };
+        /**
+         * Sets the byte order.
+         * @param {boolean} littleEndian `true` for little endian byte order, `false` for big endian
+         * @returns {!ByteBuffer} this
+         * @expose
+         */
+        ByteBufferPrototype.order = function(littleEndian) {
+            if (!this.noAssert) {
+                if (typeof littleEndian !== 'boolean')
+                    throw TypeError("Illegal littleEndian: Not a boolean");
+            }
+            this.littleEndian = !!littleEndian;
+            return this;
+        };
+
+        /**
+         * Switches (to) little endian byte order.
+         * @param {boolean=} littleEndian Defaults to `true`, otherwise uses big endian
+         * @returns {!ByteBuffer} this
+         * @expose
+         */
+        ByteBufferPrototype.LE = function(littleEndian) {
+            this.littleEndian = typeof littleEndian !== 'undefined' ? !!littleEndian : true;
+            return this;
+        };
+
+        /**
+         * Switches (to) big endian byte order.
+         * @param {boolean=} bigEndian Defaults to `true`, otherwise uses little endian
+         * @returns {!ByteBuffer} this
+         * @expose
+         */
+        ByteBufferPrototype.BE = function(bigEndian) {
+            this.littleEndian = typeof bigEndian !== 'undefined' ? !bigEndian : false;
+            return this;
+        };
+        /**
+         * Prepends some data to this ByteBuffer. This will overwrite any contents before the specified offset up to the
+         *  prepended data's length. If there is not enough space available before the specified `offset`, the backing buffer
+         *  will be resized and its contents moved accordingly.
+         * @param {!ByteBuffer|string|!ArrayBuffer} source Data to prepend. If `source` is a ByteBuffer, its offset will be
+         *  modified according to the performed read operation.
+         * @param {(string|number)=} encoding Encoding if `data` is a string ("base64", "hex", "binary", defaults to "utf8")
+         * @param {number=} offset Offset to prepend at. Will use and decrease {@link ByteBuffer#offset} by the number of bytes
+         *  prepended if omitted.
+         * @returns {!ByteBuffer} this
+         * @expose
+         * @example A relative `00<01 02 03>.prepend(<04 05>)` results in `<04 05 01 02 03>, 04 05|`
+         * @example An absolute `00<01 02 03>.prepend(<04 05>, 2)` results in `04<05 02 03>, 04 05|`
+         */
+        ByteBufferPrototype.prepend = function(source, encoding, offset) {
+            if (typeof encoding === 'number' || typeof encoding !== 'string') {
+                offset = encoding;
+                encoding = undefined;
+            }
+            var relative = typeof offset === 'undefined';
+            if (relative) offset = this.offset;
+            if (!this.noAssert) {
+                if (typeof offset !== 'number' || offset % 1 !== 0)
+                    throw TypeError("Illegal offset: "+offset+" (not an integer)");
+                offset >>>= 0;
+                if (offset < 0 || offset + 0 > this.buffer.byteLength)
+                    throw RangeError("Illegal offset: 0 <= "+offset+" (+"+0+") <= "+this.buffer.byteLength);
+            }
+            if (!(source instanceof ByteBuffer))
+                source = ByteBuffer.wrap(source, encoding);
+            var len = source.limit - source.offset;
+            if (len <= 0) return this; // Nothing to prepend
+            var diff = len - offset;
+            var arrayView;
+            if (diff > 0) { // Not enough space before offset, so resize + move
+                var buffer = new ArrayBuffer(this.buffer.byteLength + diff);
+                arrayView = new Uint8Array(buffer);
+                arrayView.set(new Uint8Array(this.buffer).subarray(offset, this.buffer.byteLength), len);
+                this.buffer = buffer;
+                this.view = new DataView(buffer);
+                this.offset += diff;
+                if (this.markedOffset >= 0) this.markedOffset += diff;
+                this.limit += diff;
+                offset += diff;
+            } else {
+                arrayView = new Uint8Array(this.buffer);
+            }
+            arrayView.set(new Uint8Array(source.buffer).subarray(source.offset, source.limit), offset - len);
+            source.offset = source.limit;
+            if (relative)
+                this.offset -= len;
+            return this;
+        };
+
+        /**
+         * Prepends this ByteBuffer to another ByteBuffer. This will overwrite any contents before the specified offset up to the
+         *  prepended data's length. If there is not enough space available before the specified `offset`, the backing buffer
+         *  will be resized and its contents moved accordingly.
+         * @param {!ByteBuffer} target Target ByteBuffer
+         * @param {number=} offset Offset to prepend at. Will use and decrease {@link ByteBuffer#offset} by the number of bytes
+         *  prepended if omitted.
+         * @returns {!ByteBuffer} this
+         * @expose
+         * @see ByteBuffer#prepend
+         */
+        ByteBufferPrototype.prependTo = function(target, offset) {
+            target.prepend(this, offset);
+            return this;
+        };
+        /**
+         * Prints debug information about this ByteBuffer's contents.
+         * @param {function(string)=} out Output function to call, defaults to console.log
+         * @expose
+         */
+        ByteBufferPrototype.printDebug = function(out) {
+            if (typeof out !== 'function') out = console.log.bind(console);
+            out(
+                this.toString()+"\n"+
+                "-------------------------------------------------------------------\n"+
+                this.toDebug(/* columns */ true)
+            );
+        };
+
+        /**
+         * Gets the number of remaining readable bytes. Contents are the bytes between {@link ByteBuffer#offset} and
+         *  {@link ByteBuffer#limit}, so this returns `limit - offset`.
+         * @returns {number} Remaining readable bytes. May be negative if `offset > limit`.
+         * @expose
+         */
+        ByteBufferPrototype.remaining = function() {
+            return this.limit - this.offset;
+        };
+        /**
+         * Resets this ByteBuffer's {@link ByteBuffer#offset}. If an offset has been marked through {@link ByteBuffer#mark}
+         *  before, `offset` will be set to {@link ByteBuffer#markedOffset}, which will then be discarded. If no offset has been
+         *  marked, sets `offset = 0`.
+         * @returns {!ByteBuffer} this
+         * @see ByteBuffer#mark
+         * @expose
+         */
+        ByteBufferPrototype.reset = function() {
+            if (this.markedOffset >= 0) {
+                this.offset = this.markedOffset;
+                this.markedOffset = -1;
+            } else {
+                this.offset = 0;
+            }
+            return this;
+        };
+        /**
+         * Resizes this ByteBuffer to be backed by a buffer of at least the given capacity. Will do nothing if already that
+         *  large or larger.
+         * @param {number} capacity Capacity required
+         * @returns {!ByteBuffer} this
+         * @throws {TypeError} If `capacity` is not a number
+         * @throws {RangeError} If `capacity < 0`
+         * @expose
+         */
+        ByteBufferPrototype.resize = function(capacity) {
+            if (!this.noAssert) {
+                if (typeof capacity !== 'number' || capacity % 1 !== 0)
+                    throw TypeError("Illegal capacity: "+capacity+" (not an integer)");
+                capacity |= 0;
+                if (capacity < 0)
+                    throw RangeError("Illegal capacity: 0 <= "+capacity);
+            }
+            if (this.buffer.byteLength < capacity) {
+                var buffer = new ArrayBuffer(capacity);
+                new Uint8Array(buffer).set(new Uint8Array(this.buffer));
+                this.buffer = buffer;
+                this.view = new DataView(buffer);
+            }
+            return this;
+        };
+        /**
+         * Reverses this ByteBuffer's contents.
+         * @param {number=} begin Offset to start at, defaults to {@link ByteBuffer#offset}
+         * @param {number=} end Offset to end at, defaults to {@link ByteBuffer#limit}
+         * @returns {!ByteBuffer} this
+         * @expose
+         */
+        ByteBufferPrototype.reverse = function(begin, end) {
+            if (typeof begin === 'undefined') begin = this.offset;
+            if (typeof end === 'undefined') end = this.limit;
+            if (!this.noAssert) {
+                if (typeof begin !== 'number' || begin % 1 !== 0)
+                    throw TypeError("Illegal begin: Not an integer");
+                begin >>>= 0;
+                if (typeof end !== 'number' || end % 1 !== 0)
+                    throw TypeError("Illegal end: Not an integer");
+                end >>>= 0;
+                if (begin < 0 || begin > end || end > this.buffer.byteLength)
+                    throw RangeError("Illegal range: 0 <= "+begin+" <= "+end+" <= "+this.buffer.byteLength);
+            }
+            if (begin === end)
+                return this; // Nothing to reverse
+            Array.prototype.reverse.call(new Uint8Array(this.buffer).subarray(begin, end));
+            this.view = new DataView(this.buffer); // FIXME: Why exactly is this necessary?
+            return this;
+        };
+        /**
+         * Skips the next `length` bytes. This will just advance
+         * @param {number} length Number of bytes to skip. May also be negative to move the offset back.
+         * @returns {!ByteBuffer} this
+         * @expose
+         */
+        ByteBufferPrototype.skip = function(length) {
+            if (!this.noAssert) {
+                if (typeof length !== 'number' || length % 1 !== 0)
+                    throw TypeError("Illegal length: "+length+" (not an integer)");
+                length |= 0;
+            }
+            var offset = this.offset + length;
+            if (!this.noAssert) {
+                if (offset < 0 || offset > this.buffer.byteLength)
+                    throw RangeError("Illegal length: 0 <= "+this.offset+" + "+length+" <= "+this.buffer.byteLength);
+            }
+            this.offset = offset;
+            return this;
+        };
+
+        /**
+         * Slices this ByteBuffer by creating a cloned instance with `offset = begin` and `limit = end`.
+         * @param {number=} begin Begin offset, defaults to {@link ByteBuffer#offset}.
+         * @param {number=} end End offset, defaults to {@link ByteBuffer#limit}.
+         * @returns {!ByteBuffer} Clone of this ByteBuffer with slicing applied, backed by the same {@link ByteBuffer#buffer}
+         * @expose
+         */
+        ByteBufferPrototype.slice = function(begin, end) {
+            if (typeof begin === 'undefined') begin = this.offset;
+            if (typeof end === 'undefined') end = this.limit;
+            if (!this.noAssert) {
+                if (typeof begin !== 'number' || begin % 1 !== 0)
+                    throw TypeError("Illegal begin: Not an integer");
+                begin >>>= 0;
+                if (typeof end !== 'number' || end % 1 !== 0)
+                    throw TypeError("Illegal end: Not an integer");
+                end >>>= 0;
+                if (begin < 0 || begin > end || end > this.buffer.byteLength)
+                    throw RangeError("Illegal range: 0 <= "+begin+" <= "+end+" <= "+this.buffer.byteLength);
+            }
+            var bb = this.clone();
+            bb.offset = begin;
+            bb.limit = end;
+            return bb;
+        };
+        /**
+         * Returns a copy of the backing buffer that contains this ByteBuffer's contents. Contents are the bytes between
+         *  {@link ByteBuffer#offset} and {@link ByteBuffer#limit}. Will transparently {@link ByteBuffer#flip} this
+         *  ByteBuffer if `offset > limit` but the actual offsets remain untouched.
+         * @param {boolean=} forceCopy If `true` returns a copy, otherwise returns a view referencing the same memory if
+         *  possible. Defaults to `false`
+         * @returns {!ArrayBuffer} Contents as an ArrayBuffer
+         * @expose
+         */
+        ByteBufferPrototype.toBuffer = function(forceCopy) {
+            var offset = this.offset,
+                limit = this.limit;
+            if (offset > limit) {
+                var t = offset;
+                offset = limit;
+                limit = t;
+            }
+            if (!this.noAssert) {
+                if (typeof offset !== 'number' || offset % 1 !== 0)
+                    throw TypeError("Illegal offset: Not an integer");
+                offset >>>= 0;
+                if (typeof limit !== 'number' || limit % 1 !== 0)
+                    throw TypeError("Illegal limit: Not an integer");
+                limit >>>= 0;
+                if (offset < 0 || offset > limit || limit > this.buffer.byteLength)
+                    throw RangeError("Illegal range: 0 <= "+offset+" <= "+limit+" <= "+this.buffer.byteLength);
+            }
+            // NOTE: It's not possible to have another ArrayBuffer reference the same memory as the backing buffer. This is
+            // possible with Uint8Array#subarray only, but we have to return an ArrayBuffer by contract. So:
+            if (!forceCopy && offset === 0 && limit === this.buffer.byteLength) {
+                return this.buffer;
+            }
+            if (offset === limit) {
+                return EMPTY_BUFFER;
+            }
+            var buffer = new ArrayBuffer(limit - offset);
+            new Uint8Array(buffer).set(new Uint8Array(this.buffer).subarray(offset, limit), 0);
+            return buffer;
+        };
+
+        /**
+         * Returns a raw buffer compacted to contain this ByteBuffer's contents. Contents are the bytes between
+         *  {@link ByteBuffer#offset} and {@link ByteBuffer#limit}. Will transparently {@link ByteBuffer#flip} this
+         *  ByteBuffer if `offset > limit` but the actual offsets remain untouched. This is an alias of
+         *  {@link ByteBuffer#toBuffer}.
+         * @function
+         * @param {boolean=} forceCopy If `true` returns a copy, otherwise returns a view referencing the same memory.
+         *  Defaults to `false`
+         * @returns {!ArrayBuffer} Contents as an ArrayBuffer
+         * @expose
+         */
+        ByteBufferPrototype.toArrayBuffer = ByteBufferPrototype.toBuffer;
+
+
+        /**
+         * Converts the ByteBuffer's contents to a string.
+         * @param {string=} encoding Output encoding. Returns an informative string representation if omitted but also allows
+         *  direct conversion to "utf8", "hex", "base64" and "binary" encoding. "debug" returns a hex representation with
+         *  highlighted offsets.
+         * @param {number=} begin Offset to begin at, defaults to {@link ByteBuffer#offset}
+         * @param {number=} end Offset to end at, defaults to {@link ByteBuffer#limit}
+         * @returns {string} String representation
+         * @throws {Error} If `encoding` is invalid
+         * @expose
+         */
+        ByteBufferPrototype.toString = function(encoding, begin, end) {
+            if (typeof encoding === 'undefined')
+                return "ByteBufferAB(offset="+this.offset+",markedOffset="+this.markedOffset+",limit="+this.limit+",capacity="+this.capacity()+")";
+            if (typeof encoding === 'number')
+                encoding = "utf8",
+                begin = encoding,
+                end = begin;
+            switch (encoding) {
+                case "utf8":
+                    return this.toUTF8(begin, end);
+                case "base64":
+                    return this.toBase64(begin, end);
+                case "hex":
+                    return this.toHex(begin, end);
+                case "binary":
+                    return this.toBinary(begin, end);
+                case "debug":
+                    return this.toDebug();
+                case "columns":
+                    return this.toColumns();
+                default:
+                    throw Error("Unsupported encoding: "+encoding);
+            }
+        };
+
+        // lxiv-embeddable
+
+        /**
+         * lxiv-embeddable (c) 2014 Daniel Wirtz <dcode@dcode.io>
+         * Released under the Apache License, Version 2.0
+         * see: https://github.com/dcodeIO/lxiv for details
+         */
+        var lxiv = function() {
+            "use strict";
+
+            /**
+             * lxiv namespace.
+             * @type {!Object.<string,*>}
+             * @exports lxiv
+             */
+            var lxiv = {};
+
+            /**
+             * Character codes for output.
+             * @type {!Array.<number>}
+             * @inner
+             */
+            var aout = [
+                65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80,
+                81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 97, 98, 99, 100, 101, 102,
+                103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118,
+                119, 120, 121, 122, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 43, 47
+            ];
+
+            /**
+             * Character codes for input.
+             * @type {!Array.<number>}
+             * @inner
+             */
+            var ain = [];
+            for (var i=0, k=aout.length; i<k; ++i)
+                ain[aout[i]] = i;
+
+            /**
+             * Encodes bytes to base64 char codes.
+             * @param {!function():number|null} src Bytes source as a function returning the next byte respectively `null` if
+             *  there are no more bytes left.
+             * @param {!function(number)} dst Characters destination as a function successively called with each encoded char
+             *  code.
+             */
+            lxiv.encode = function(src, dst) {
+                var b, t;
+                while ((b = src()) !== null) {
+                    dst(aout[(b>>2)&0x3f]);
+                    t = (b&0x3)<<4;
+                    if ((b = src()) !== null) {
+                        t |= (b>>4)&0xf;
+                        dst(aout[(t|((b>>4)&0xf))&0x3f]);
+                        t = (b&0xf)<<2;
+                        if ((b = src()) !== null)
+                            dst(aout[(t|((b>>6)&0x3))&0x3f]),
+                            dst(aout[b&0x3f]);
+                        else
+                            dst(aout[t&0x3f]),
+                            dst(61);
+                    } else
+                        dst(aout[t&0x3f]),
+                        dst(61),
+                        dst(61);
+                }
+            };
+
+            /**
+             * Decodes base64 char codes to bytes.
+             * @param {!function():number|null} src Characters source as a function returning the next char code respectively
+             *  `null` if there are no more characters left.
+             * @param {!function(number)} dst Bytes destination as a function successively called with the next byte.
+             * @throws {Error} If a character code is invalid
+             */
+            lxiv.decode = function(src, dst) {
+                var c, t1, t2;
+                function fail(c) {
+                    throw Error("Illegal character code: "+c);
+                }
+                while ((c = src()) !== null) {
+                    t1 = ain[c];
+                    if (typeof t1 === 'undefined') fail(c);
+                    if ((c = src()) !== null) {
+                        t2 = ain[c];
+                        if (typeof t2 === 'undefined') fail(c);
+                        dst((t1<<2)>>>0|(t2&0x30)>>4);
+                        if ((c = src()) !== null) {
+                            t1 = ain[c];
+                            if (typeof t1 === 'undefined')
+                                if (c === 61) break; else fail(c);
+                            dst(((t2&0xf)<<4)>>>0|(t1&0x3c)>>2);
+                            if ((c = src()) !== null) {
+                                t2 = ain[c];
+                                if (typeof t2 === 'undefined')
+                                    if (c === 61) break; else fail(c);
+                                dst(((t1&0x3)<<6)>>>0|t2);
+                            }
+                        }
+                    }
+                }
+            };
+
+            /**
+             * Tests if a string is valid base64.
+             * @param {string} str String to test
+             * @returns {boolean} `true` if valid, otherwise `false`
+             */
+            lxiv.test = function(str) {
+                return /^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$/.test(str);
+            };
+
+            return lxiv;
+        }();
+
+        // encodings/base64
+
+        /**
+         * Encodes this ByteBuffer's contents to a base64 encoded string.
+         * @param {number=} begin Offset to begin at, defaults to {@link ByteBuffer#offset}.
+         * @param {number=} end Offset to end at, defaults to {@link ByteBuffer#limit}.
+         * @returns {string} Base64 encoded string
+         * @expose
+         */
+        ByteBufferPrototype.toBase64 = function(begin, end) {
+            if (typeof begin === 'undefined')
+                begin = this.offset;
+            if (typeof end === 'undefined')
+                end = this.limit;
+            if (!this.noAssert) {
+                if (typeof begin !== 'number' || begin % 1 !== 0)
+                    throw TypeError("Illegal begin: Not an integer");
+                begin >>>= 0;
+                if (typeof end !== 'number' || end % 1 !== 0)
+                    throw TypeError("Illegal end: Not an integer");
+                end >>>= 0;
+                if (begin < 0 || begin > end || end > this.buffer.byteLength)
+                    throw RangeError("Illegal range: 0 <= "+begin+" <= "+end+" <= "+this.buffer.byteLength);
+            }
+            var sd; lxiv.encode(function() {
+                return begin < end ? this.view.getUint8(begin++) : null;
+            }.bind(this), sd = stringDestination());
+            return sd();
+        };
+
+        /**
+         * Decodes a base64 encoded string to a ByteBuffer.
+         * @param {string} str String to decode
+         * @param {boolean=} littleEndian Whether to use little or big endian byte order. Defaults to
+         *  {@link ByteBuffer.DEFAULT_ENDIAN}.
+         * @param {boolean=} noAssert Whether to skip assertions of offsets and values. Defaults to
+         *  {@link ByteBuffer.DEFAULT_NOASSERT}.
+         * @returns {!ByteBuffer} ByteBuffer
+         * @expose
+         */
+        ByteBuffer.fromBase64 = function(str, littleEndian, noAssert) {
+            if (!noAssert) {
+                if (typeof str !== 'string')
+                    throw TypeError("Illegal str: Not a string");
+                if (str.length % 4 !== 0)
+                    throw TypeError("Illegal str: Length not a multiple of 4");
+            }
+            var bb = new ByteBuffer(str.length/4*3, littleEndian, noAssert),
+                i = 0;
+            lxiv.decode(stringSource(str), function(b) {
+                bb.view.setUint8(i++, b);
+            });
+            bb.limit = i;
+            return bb;
+        };
+
+        /**
+         * Encodes a binary string to base64 like `window.btoa` does.
+         * @param {string} str Binary string
+         * @returns {string} Base64 encoded string
+         * @see https://developer.mozilla.org/en-US/docs/Web/API/Window.btoa
+         * @expose
+         */
+        ByteBuffer.btoa = function(str) {
+            return ByteBuffer.fromBinary(str).toBase64();
+        };
+
+        /**
+         * Decodes a base64 encoded string to binary like `window.atob` does.
+         * @param {string} b64 Base64 encoded string
+         * @returns {string} Binary string
+         * @see https://developer.mozilla.org/en-US/docs/Web/API/Window.atob
+         * @expose
+         */
+        ByteBuffer.atob = function(b64) {
+            return ByteBuffer.fromBase64(b64).toBinary();
+        };
+
+        // encodings/binary
+
+        /**
+         * Encodes this ByteBuffer to a binary encoded string, that is using only characters 0x00-0xFF as bytes.
+         * @param {number=} begin Offset to begin at. Defaults to {@link ByteBuffer#offset}.
+         * @param {number=} end Offset to end at. Defaults to {@link ByteBuffer#limit}.
+         * @returns {string} Binary encoded string
+         * @throws {RangeError} If `offset > limit`
+         * @expose
+         */
+        ByteBufferPrototype.toBinary = function(begin, end) {
+            begin = typeof begin === 'undefined' ? this.offset : begin;
+            end = typeof end === 'undefined' ? this.limit : end;
+            if (!this.noAssert) {
+                if (typeof begin !== 'number' || begin % 1 !== 0)
+                    throw TypeError("Illegal begin: Not an integer");
+                begin >>>= 0;
+                if (typeof end !== 'number' || end % 1 !== 0)
+                    throw TypeError("Illegal end: Not an integer");
+                end >>>= 0;
+                if (begin < 0 || begin > end || end > this.buffer.byteLength)
+                    throw RangeError("Illegal range: 0 <= "+begin+" <= "+end+" <= "+this.buffer.byteLength);
+            }
+            if (begin === end)
+                return "";
+            var cc = [], pt = [];
+            while (begin < end) {
+                cc.push(this.view.getUint8(begin++));
+                if (cc.length >= 1024)
+                    pt.push(String.fromCharCode.apply(String, cc)),
+                    cc = [];
+            }
+            return pt.join('') + String.fromCharCode.apply(String, cc);
+        };
+
+        /**
+         * Decodes a binary encoded string, that is using only characters 0x00-0xFF as bytes, to a ByteBuffer.
+         * @param {string} str String to decode
+         * @param {boolean=} littleEndian Whether to use little or big endian byte order. Defaults to
+         *  {@link ByteBuffer.DEFAULT_ENDIAN}.
+         * @param {boolean=} noAssert Whether to skip assertions of offsets and values. Defaults to
+         *  {@link ByteBuffer.DEFAULT_NOASSERT}.
+         * @returns {!ByteBuffer} ByteBuffer
+         * @expose
+         */
+        ByteBuffer.fromBinary = function(str, littleEndian, noAssert) {
+            if (!noAssert) {
+                if (typeof str !== 'string')
+                    throw TypeError("Illegal str: Not a string");
+            }
+            var i = 0, k = str.length, charCode,
+                bb = new ByteBuffer(k, littleEndian, noAssert);
+            while (i<k) {
+                charCode = str.charCodeAt(i);
+                if (!noAssert && charCode > 255)
+                    throw RangeError("Illegal charCode at "+i+": 0 <= "+charCode+" <= 255");
+                bb.view.setUint8(i++, charCode);
+            }
+            bb.limit = k;
+            return bb;
+        };
+
+        // encodings/debug
+
+        /**
+         * Encodes this ByteBuffer to a hex encoded string with marked offsets. Offset symbols are:
+         * * `<` : offset,
+         * * `'` : markedOffset,
+         * * `>` : limit,
+         * * `|` : offset and limit,
+         * * `[` : offset and markedOffset,
+         * * `]` : markedOffset and limit,
+         * * `!` : offset, markedOffset and limit
+         * @param {boolean=} columns If `true` returns two columns hex + ascii, defaults to `false`
+         * @returns {string|!Array.<string>} Debug string or array of lines if `asArray = true`
+         * @expose
+         * @example `>00'01 02<03` contains four bytes with `limit=0, markedOffset=1, offset=3`
+         * @example `00[01 02 03>` contains four bytes with `offset=markedOffset=1, limit=4`
+         * @example `00|01 02 03` contains four bytes with `offset=limit=1, markedOffset=-1`
+         * @example `|` contains zero bytes with `offset=limit=0, markedOffset=-1`
+         */
+        ByteBufferPrototype.toDebug = function(columns) {
+            var i = -1,
+                k = this.buffer.byteLength,
+                b,
+                hex = "",
+                asc = "",
+                out = "";
+            while (i<k) {
+                if (i !== -1) {
+                    b = this.view.getUint8(i);
+                    if (b < 0x10) hex += "0"+b.toString(16).toUpperCase();
+                    else hex += b.toString(16).toUpperCase();
+                    if (columns) {
+                        asc += b > 32 && b < 127 ? String.fromCharCode(b) : '.';
+                    }
+                }
+                ++i;
+                if (columns) {
+                    if (i > 0 && i % 16 === 0 && i !== k) {
+                        while (hex.length < 3*16+3) hex += " ";
+                        out += hex+asc+"\n";
+                        hex = asc = "";
+                    }
+                }
+                if (i === this.offset && i === this.limit)
+                    hex += i === this.markedOffset ? "!" : "|";
+                else if (i === this.offset)
+                    hex += i === this.markedOffset ? "[" : "<";
+                else if (i === this.limit)
+                    hex += i === this.markedOffset ? "]" : ">";
+                else
+                    hex += i === this.markedOffset ? "'" : (columns || (i !== 0 && i !== k) ? " " : "");
+            }
+            if (columns && hex !== " ") {
+                while (hex.length < 3*16+3) hex += " ";
+                out += hex+asc+"\n";
+            }
+            return columns ? out : hex;
+        };
+
+        /**
+         * Decodes a hex encoded string with marked offsets to a ByteBuffer.
+         * @param {string} str Debug string to decode (not be generated with `columns = true`)
+         * @param {boolean=} littleEndian Whether to use little or big endian byte order. Defaults to
+         *  {@link ByteBuffer.DEFAULT_ENDIAN}.
+         * @param {boolean=} noAssert Whether to skip assertions of offsets and values. Defaults to
+         *  {@link ByteBuffer.DEFAULT_NOASSERT}.
+         * @returns {!ByteBuffer} ByteBuffer
+         * @expose
+         * @see ByteBuffer#toDebug
+         */
+        ByteBuffer.fromDebug = function(str, littleEndian, noAssert) {
+            var k = str.length,
+                bb = new ByteBuffer(((k+1)/3)|0, littleEndian, noAssert);
+            var i = 0, j = 0, ch, b,
+                rs = false, // Require symbol next
+                ho = false, hm = false, hl = false, // Already has offset, markedOffset, limit?
+                fail = false;
+            while (i<k) {
+                switch (ch = str.charAt(i++)) {
+                    case '!':
+                        if (!noAssert) {
+                            if (ho || hm || hl) {
+                                fail = true; break;
+                            }
+                            ho = hm = hl = true;
+                        }
+                        bb.offset = bb.markedOffset = bb.limit = j;
+                        rs = false;
+                        break;
+                    case '|':
+                        if (!noAssert) {
+                            if (ho || hl) {
+                                fail = true; break;
+                            }
+                            ho = hl = true;
+                        }
+                        bb.offset = bb.limit = j;
+                        rs = false;
+                        break;
+                    case '[':
+                        if (!noAssert) {
+                            if (ho || hm) {
+                                fail = true; break;
+                            }
+                            ho = hm = true;
+                        }
+                        bb.offset = bb.markedOffset = j;
+                        rs = false;
+                        break;
+                    case '<':
+                        if (!noAssert) {
+                            if (ho) {
+                                fail = true; break;
+                            }
+                            ho = true;
+                        }
+                        bb.offset = j;
+                        rs = false;
+                        break;
+                    case ']':
+                        if (!noAssert) {
+                            if (hl || hm) {
+                                fail = true; break;
+                            }
+                            hl = hm = true;
+                        }
+                        bb.limit = bb.markedOffset = j;
+                        rs = false;
+                        break;
+                    case '>':
+                        if (!noAssert) {
+                            if (hl) {
+                                fail = true; break;
+                            }
+                            hl = true;
+                        }
+                        bb.limit = j;
+                        rs = false;
+                        break;
+                    case "'":
+                        if (!noAssert) {
+                            if (hm) {
+                                fail = true; break;
+                            }
+                            hm = true;
+                        }
+                        bb.markedOffset = j;
+                        rs = false;
+                        break;
+                    case ' ':
+                        rs = false;
+                        break;
+                    default:
+                        if (!noAssert) {
+                            if (rs) {
+                                fail = true; break;
+                            }
+                        }
+                        b = parseInt(ch+str.charAt(i++), 16);
+                        if (!noAssert) {
+                            if (isNaN(b) || b < 0 || b > 255)
+                                throw TypeError("Illegal str: Not a debug encoded string");
+                        }
+                        bb.view.setUint8(j++, b);
+                        rs = true;
+                }
+                if (fail)
+                    throw TypeError("Illegal str: Invalid symbol at "+i);
+            }
+            if (!noAssert) {
+                if (!ho || !hl)
+                    throw TypeError("Illegal str: Missing offset or limit");
+                if (j<bb.buffer.byteLength)
+                    throw TypeError("Illegal str: Not a debug encoded string (is it hex?) "+j+" < "+k);
+            }
+            return bb;
+        };
+
+        // encodings/hex
+
+        /**
+         * Encodes this ByteBuffer's contents to a hex encoded string.
+         * @param {number=} begin Offset to begin at. Defaults to {@link ByteBuffer#offset}.
+         * @param {number=} end Offset to end at. Defaults to {@link ByteBuffer#limit}.
+         * @returns {string} Hex encoded string
+         * @expose
+         */
+        ByteBufferPrototype.toHex = function(begin, end) {
+            begin = typeof begin === 'undefined' ? this.offset : begin;
+            end = typeof end === 'undefined' ? this.limit : end;
+            if (!this.noAssert) {
+                if (typeof begin !== 'number' || begin % 1 !== 0)
+                    throw TypeError("Illegal begin: Not an integer");
+                begin >>>= 0;
+                if (typeof end !== 'number' || end % 1 !== 0)
+                    throw TypeError("Illegal end: Not an integer");
+                end >>>= 0;
+                if (begin < 0 || begin > end || end > this.buffer.byteLength)
+                    throw RangeError("Illegal range: 0 <= "+begin+" <= "+end+" <= "+this.buffer.byteLength);
+            }
+            var out = new Array(end - begin),
+                b;
+            while (begin < end) {
+                b = this.view.getUint8(begin++);
+                if (b < 0x10)
+                    out.push("0", b.toString(16));
+                else out.push(b.toString(16));
+            }
+            return out.join('');
+        };
+
+        /**
+         * Decodes a hex encoded string to a ByteBuffer.
+         * @param {string} str String to decode
+         * @param {boolean=} littleEndian Whether to use little or big endian byte order. Defaults to
+         *  {@link ByteBuffer.DEFAULT_ENDIAN}.
+         * @param {boolean=} noAssert Whether to skip assertions of offsets and values. Defaults to
+         *  {@link ByteBuffer.DEFAULT_NOASSERT}.
+         * @returns {!ByteBuffer} ByteBuffer
+         * @expose
+         */
+        ByteBuffer.fromHex = function(str, littleEndian, noAssert) {
+            if (!noAssert) {
+                if (typeof str !== 'string')
+                    throw TypeError("Illegal str: Not a string");
+                if (str.length % 2 !== 0)
+                    throw TypeError("Illegal str: Length not a multiple of 2");
+            }
+            var k = str.length,
+                bb = new ByteBuffer((k / 2) | 0, littleEndian),
+                b;
+            for (var i=0, j=0; i<k; i+=2) {
+                b = parseInt(str.substring(i, i+2), 16);
+                if (!noAssert)
+                    if (!isFinite(b) || b < 0 || b > 255)
+                        throw TypeError("Illegal str: Contains non-hex characters");
+                bb.view.setUint8(j++, b);
+            }
+            bb.limit = j;
+            return bb;
+        };
+
+        // utfx-embeddable
+
+        /**
+         * utfx-embeddable (c) 2014 Daniel Wirtz <dcode@dcode.io>
+         * Released under the Apache License, Version 2.0
+         * see: https://github.com/dcodeIO/utfx for details
+         */
+        var utfx = function() {
+            "use strict";
+
+            /**
+             * utfx namespace.
+             * @inner
+             * @type {!Object.<string,*>}
+             */
+            var utfx = {};
+
+            /**
+             * Maximum valid code point.
+             * @type {number}
+             * @const
+             */
+            utfx.MAX_CODEPOINT = 0x10FFFF;
+
+            /**
+             * Encodes UTF8 code points to UTF8 bytes.
+             * @param {(!function():number|null) | number} src Code points source, either as a function returning the next code point
+             *  respectively `null` if there are no more code points left or a single numeric code point.
+             * @param {!function(number)} dst Bytes destination as a function successively called with the next byte
+             */
+            utfx.encodeUTF8 = function(src, dst) {
+                var cp = null;
+                if (typeof src === 'number')
+                    cp = src,
+                    src = function() { return null; };
+                while (cp !== null || (cp = src()) !== null) {
+                    if (cp < 0x80)
+                        dst(cp&0x7F);
+                    else if (cp < 0x800)
+                        dst(((cp>>6)&0x1F)|0xC0),
+                        dst((cp&0x3F)|0x80);
+                    else if (cp < 0x10000)
+                        dst(((cp>>12)&0x0F)|0xE0),
+                        dst(((cp>>6)&0x3F)|0x80),
+                        dst((cp&0x3F)|0x80);
+                    else
+                        dst(((cp>>18)&0x07)|0xF0),
+                        dst(((cp>>12)&0x3F)|0x80),
+                        dst(((cp>>6)&0x3F)|0x80),
+                        dst((cp&0x3F)|0x80);
+                    cp = null;
+                }
+            };
+
+            /**
+             * Decodes UTF8 bytes to UTF8 code points.
+             * @param {!function():number|null} src Bytes source as a function returning the next byte respectively `null` if there
+             *  are no more bytes left.
+             * @param {!function(number)} dst Code points destination as a function successively called with each decoded code point.
+             * @throws {RangeError} If a starting byte is invalid in UTF8
+             * @throws {Error} If the last sequence is truncated. Has an array property `bytes` holding the
+             *  remaining bytes.
+             */
+            utfx.decodeUTF8 = function(src, dst) {
+                var a, b, c, d, fail = function(b) {
+                    b = b.slice(0, b.indexOf(null));
+                    var err = Error(b.toString());
+                    err.name = "TruncatedError";
+                    err['bytes'] = b;
+                    throw err;
+                };
+                while ((a = src()) !== null) {
+                    if ((a&0x80) === 0)
+                        dst(a);
+                    else if ((a&0xE0) === 0xC0)
+                        ((b = src()) === null) && fail([a, b]),
+                        dst(((a&0x1F)<<6) | (b&0x3F));
+                    else if ((a&0xF0) === 0xE0)
+                        ((b=src()) === null || (c=src()) === null) && fail([a, b, c]),
+                        dst(((a&0x0F)<<12) | ((b&0x3F)<<6) | (c&0x3F));
+                    else if ((a&0xF8) === 0xF0)
+                        ((b=src()) === null || (c=src()) === null || (d=src()) === null) && fail([a, b, c ,d]),
+                        dst(((a&0x07)<<18) | ((b&0x3F)<<12) | ((c&0x3F)<<6) | (d&0x3F));
+                    else throw RangeError("Illegal starting byte: "+a);
+                }
+            };
+
+            /**
+             * Converts UTF16 characters to UTF8 code points.
+             * @param {!function():number|null} src Characters source as a function returning the next char code respectively
+             *  `null` if there are no more characters left.
+             * @param {!function(number)} dst Code points destination as a function successively called with each converted code
+             *  point.
+             */
+            utfx.UTF16toUTF8 = function(src, dst) {
+                var c1, c2 = null;
+                while (true) {
+                    if ((c1 = c2 !== null ? c2 : src()) === null)
+                        break;
+                    if (c1 >= 0xD800 && c1 <= 0xDFFF) {
+                        if ((c2 = src()) !== null) {
+                            if (c2 >= 0xDC00 && c2 <= 0xDFFF) {
+                                dst((c1-0xD800)*0x400+c2-0xDC00+0x10000);
+                                c2 = null; continue;
+                            }
+                        }
+                    }
+                    dst(c1);
+                }
+                if (c2 !== null) dst(c2);
+            };
+
+            /**
+             * Converts UTF8 code points to UTF16 characters.
+             * @param {(!function():number|null) | number} src Code points source, either as a function returning the next code point
+             *  respectively `null` if there are no more code points left or a single numeric code point.
+             * @param {!function(number)} dst Characters destination as a function successively called with each converted char code.
+             * @throws {RangeError} If a code point is out of range
+             */
+            utfx.UTF8toUTF16 = function(src, dst) {
+                var cp = null;
+                if (typeof src === 'number')
+                    cp = src, src = function() { return null; };
+                while (cp !== null || (cp = src()) !== null) {
+                    if (cp <= 0xFFFF)
+                        dst(cp);
+                    else
+                        cp -= 0x10000,
+                        dst((cp>>10)+0xD800),
+                        dst((cp%0x400)+0xDC00);
+                    cp = null;
+                }
+            };
+
+            /**
+             * Converts and encodes UTF16 characters to UTF8 bytes.
+             * @param {!function():number|null} src Characters source as a function returning the next char code respectively `null`
+             *  if there are no more characters left.
+             * @param {!function(number)} dst Bytes destination as a function successively called with the next byte.
+             */
+            utfx.encodeUTF16toUTF8 = function(src, dst) {
+                utfx.UTF16toUTF8(src, function(cp) {
+                    utfx.encodeUTF8(cp, dst);
+                });
+            };
+
+            /**
+             * Decodes and converts UTF8 bytes to UTF16 characters.
+             * @param {!function():number|null} src Bytes source as a function returning the next byte respectively `null` if there
+             *  are no more bytes left.
+             * @param {!function(number)} dst Characters destination as a function successively called with each converted char code.
+             * @throws {RangeError} If a starting byte is invalid in UTF8
+             * @throws {Error} If the last sequence is truncated. Has an array property `bytes` holding the remaining bytes.
+             */
+            utfx.decodeUTF8toUTF16 = function(src, dst) {
+                utfx.decodeUTF8(src, function(cp) {
+                    utfx.UTF8toUTF16(cp, dst);
+                });
+            };
+
+            /**
+             * Calculates the byte length of an UTF8 code point.
+             * @param {number} cp UTF8 code point
+             * @returns {number} Byte length
+             */
+            utfx.calculateCodePoint = function(cp) {
+                return (cp < 0x80) ? 1 : (cp < 0x800) ? 2 : (cp < 0x10000) ? 3 : 4;
+            };
+
+            /**
+             * Calculates the number of UTF8 bytes required to store UTF8 code points.
+             * @param {(!function():number|null)} src Code points source as a function returning the next code point respectively
+             *  `null` if there are no more code points left.
+             * @returns {number} The number of UTF8 bytes required
+             */
+            utfx.calculateUTF8 = function(src) {
+                var cp, l=0;
+                while ((cp = src()) !== null)
+                    l += utfx.calculateCodePoint(cp);
+                return l;
+            };
+
+            /**
+             * Calculates the number of UTF8 code points respectively UTF8 bytes required to store UTF16 char codes.
+             * @param {(!function():number|null)} src Characters source as a function returning the next char code respectively
+             *  `null` if there are no more characters left.
+             * @returns {!Array.<number>} The number of UTF8 code points at index 0 and the number of UTF8 bytes required at index 1.
+             */
+            utfx.calculateUTF16asUTF8 = function(src) {
+                var n=0, l=0;
+                utfx.UTF16toUTF8(src, function(cp) {
+                    ++n; l += utfx.calculateCodePoint(cp);
+                });
+                return [n,l];
+            };
+
+            return utfx;
+        }();
+
+        // encodings/utf8
+
+        /**
+         * Encodes this ByteBuffer's contents between {@link ByteBuffer#offset} and {@link ByteBuffer#limit} to an UTF8 encoded
+         *  string.
+         * @returns {string} Hex encoded string
+         * @throws {RangeError} If `offset > limit`
+         * @expose
+         */
+        ByteBufferPrototype.toUTF8 = function(begin, end) {
+            if (typeof begin === 'undefined') begin = this.offset;
+            if (typeof end === 'undefined') end = this.limit;
+            if (!this.noAssert) {
+                if (typeof begin !== 'number' || begin % 1 !== 0)
+                    throw TypeError("Illegal begin: Not an integer");
+                begin >>>= 0;
+                if (typeof end !== 'number' || end % 1 !== 0)
+                    throw TypeError("Illegal end: Not an integer");
+                end >>>= 0;
+                if (begin < 0 || begin > end || end > this.buffer.byteLength)
+                    throw RangeError("Illegal range: 0 <= "+begin+" <= "+end+" <= "+this.buffer.byteLength);
+            }
+            var sd; try {
+                utfx.decodeUTF8toUTF16(function() {
+                    return begin < end ? this.view.getUint8(begin++) : null;
+                }.bind(this), sd = stringDestination());
+            } catch (e) {
+                if (begin !== end)
+                    throw RangeError("Illegal range: Truncated data, "+begin+" != "+end);
+            }
+            return sd();
+        };
+
+        /**
+         * Decodes an UTF8 encoded string to a ByteBuffer.
+         * @param {string} str String to decode
+         * @param {boolean=} littleEndian Whether to use little or big endian byte order. Defaults to
+         *  {@link ByteBuffer.DEFAULT_ENDIAN}.
+         * @param {boolean=} noAssert Whether to skip assertions of offsets and values. Defaults to
+         *  {@link ByteBuffer.DEFAULT_NOASSERT}.
+         * @returns {!ByteBuffer} ByteBuffer
+         * @expose
+         */
+        ByteBuffer.fromUTF8 = function(str, littleEndian, noAssert) {
+            if (!noAssert)
+                if (typeof str !== 'string')
+                    throw TypeError("Illegal str: Not a string");
+            var bb = new ByteBuffer(utfx.calculateUTF16asUTF8(stringSource(str), true)[1], littleEndian, noAssert),
+                i = 0;
+            utfx.encodeUTF16toUTF8(stringSource(str), function(b) {
+                bb.view.setUint8(i++, b);
+            });
+            bb.limit = i;
+            return bb;
+        };
+
+
+        return ByteBuffer;
+    }
+
+    /* CommonJS */ if (typeof require === 'function' && typeof module === 'object' && module && typeof exports === 'object' && exports)
+        module['exports'] = (function() {
+            var Long; try { Long = require("long"); } catch (e) {}
+            return loadByteBuffer(Long);
+        })();
+    /* AMD */ else if (typeof define === 'function' && define["amd"])
+        define(["Long"], function(Long) {
+            return loadByteBuffer(Long);
+        });
+    /* Global */ else
+        (global["dcodeIO"] = global["dcodeIO"] || {})["ByteBuffer"] = loadByteBuffer(global["dcodeIO"]["Long"]);
+
+})(this);

+ 1207 - 0
3rdparty/long.js

@@ -0,0 +1,1207 @@
+/*
+ Copyright 2013 Daniel Wirtz <dcode@dcode.io>
+ Copyright 2009 The Closure Library Authors. All Rights Reserved.
+
+ Licensed under the Apache License, Version 2.0 (the "License");
+ you may not use this file except in compliance with the License.
+ You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS-IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+ */
+
+/**
+ * @license long.js (c) 2013 Daniel Wirtz <dcode@dcode.io>
+ * Released under the Apache License, Version 2.0
+ * see: https://github.com/dcodeIO/long.js for details
+ */
+(function(global, factory) {
+    /* AMD */ if (typeof define === 'function' && define["amd"])
+        define([], factory);
+    /* CommonJS */ else if (typeof require === 'function' && typeof module === "object" && module && module["exports"])
+        module["exports"] = factory();
+    /* Global */ else
+        (global["dcodeIO"] = global["dcodeIO"] || {})["Long"] = factory();
+
+})(this, function() {
+    "use strict";
+
+    /**
+     * Constructs a 64 bit two's-complement integer, given its low and high 32 bit values as *signed* integers.
+     *  See the from* functions below for more convenient ways of constructing Longs.
+     * @exports Long
+     * @class A Long class for representing a 64 bit two's-complement integer value.
+     * @param {number} low The low (signed) 32 bits of the long
+     * @param {number} high The high (signed) 32 bits of the long
+     * @param {boolean=} unsigned Whether unsigned or not, defaults to `false` for signed
+     * @constructor
+     */
+    function Long(low, high, unsigned) {
+
+        /**
+         * The low 32 bits as a signed value.
+         * @type {number}
+         */
+        this.low = low | 0;
+
+        /**
+         * The high 32 bits as a signed value.
+         * @type {number}
+         */
+        this.high = high | 0;
+
+        /**
+         * Whether unsigned or not.
+         * @type {boolean}
+         */
+        this.unsigned = !!unsigned;
+    }
+
+    // The internal representation of a long is the two given signed, 32-bit values.
+    // We use 32-bit pieces because these are the size of integers on which
+    // Javascript performs bit-operations.  For operations like addition and
+    // multiplication, we split each number into 16 bit pieces, which can easily be
+    // multiplied within Javascript's floating-point representation without overflow
+    // or change in sign.
+    //
+    // In the algorithms below, we frequently reduce the negative case to the
+    // positive case by negating the input(s) and then post-processing the result.
+    // Note that we must ALWAYS check specially whether those values are MIN_VALUE
+    // (-2^63) because -MIN_VALUE == MIN_VALUE (since 2^63 cannot be represented as
+    // a positive number, it overflows back into a negative).  Not handling this
+    // case would often result in infinite recursion.
+    //
+    // Common constant values ZERO, ONE, NEG_ONE, etc. are defined below the from*
+    // methods on which they depend.
+
+    /**
+     * An indicator used to reliably determine if an object is a Long or not.
+     * @type {boolean}
+     * @const
+     * @private
+     */
+    Long.prototype.__isLong__;
+
+    Object.defineProperty(Long.prototype, "__isLong__", {
+        value: true,
+        enumerable: false,
+        configurable: false
+    });
+
+    /**
+     * @function
+     * @param {*} obj Object
+     * @returns {boolean}
+     * @inner
+     */
+    function isLong(obj) {
+        return (obj && obj["__isLong__"]) === true;
+    }
+
+    /**
+     * Tests if the specified object is a Long.
+     * @function
+     * @param {*} obj Object
+     * @returns {boolean}
+     */
+    Long.isLong = isLong;
+
+    /**
+     * A cache of the Long representations of small integer values.
+     * @type {!Object}
+     * @inner
+     */
+    var INT_CACHE = {};
+
+    /**
+     * A cache of the Long representations of small unsigned integer values.
+     * @type {!Object}
+     * @inner
+     */
+    var UINT_CACHE = {};
+
+    /**
+     * @param {number} value
+     * @param {boolean=} unsigned
+     * @returns {!Long}
+     * @inner
+     */
+    function fromInt(value, unsigned) {
+        var obj, cachedObj, cache;
+        if (unsigned) {
+            value >>>= 0;
+            if (cache = (0 <= value && value < 256)) {
+                cachedObj = UINT_CACHE[value];
+                if (cachedObj)
+                    return cachedObj;
+            }
+            obj = fromBits(value, (value | 0) < 0 ? -1 : 0, true);
+            if (cache)
+                UINT_CACHE[value] = obj;
+            return obj;
+        } else {
+            value |= 0;
+            if (cache = (-128 <= value && value < 128)) {
+                cachedObj = INT_CACHE[value];
+                if (cachedObj)
+                    return cachedObj;
+            }
+            obj = fromBits(value, value < 0 ? -1 : 0, false);
+            if (cache)
+                INT_CACHE[value] = obj;
+            return obj;
+        }
+    }
+
+    /**
+     * Returns a Long representing the given 32 bit integer value.
+     * @function
+     * @param {number} value The 32 bit integer in question
+     * @param {boolean=} unsigned Whether unsigned or not, defaults to `false` for signed
+     * @returns {!Long} The corresponding Long value
+     */
+    Long.fromInt = fromInt;
+
+    /**
+     * @param {number} value
+     * @param {boolean=} unsigned
+     * @returns {!Long}
+     * @inner
+     */
+    function fromNumber(value, unsigned) {
+        if (isNaN(value) || !isFinite(value))
+            return unsigned ? UZERO : ZERO;
+        if (unsigned) {
+            if (value < 0)
+                return UZERO;
+            if (value >= TWO_PWR_64_DBL)
+                return MAX_UNSIGNED_VALUE;
+        } else {
+            if (value <= -TWO_PWR_63_DBL)
+                return MIN_VALUE;
+            if (value + 1 >= TWO_PWR_63_DBL)
+                return MAX_VALUE;
+        }
+        if (value < 0)
+            return fromNumber(-value, unsigned).neg();
+        return fromBits((value % TWO_PWR_32_DBL) | 0, (value / TWO_PWR_32_DBL) | 0, unsigned);
+    }
+
+    /**
+     * Returns a Long representing the given value, provided that it is a finite number. Otherwise, zero is returned.
+     * @function
+     * @param {number} value The number in question
+     * @param {boolean=} unsigned Whether unsigned or not, defaults to `false` for signed
+     * @returns {!Long} The corresponding Long value
+     */
+    Long.fromNumber = fromNumber;
+
+    /**
+     * @param {number} lowBits
+     * @param {number} highBits
+     * @param {boolean=} unsigned
+     * @returns {!Long}
+     * @inner
+     */
+    function fromBits(lowBits, highBits, unsigned) {
+        return new Long(lowBits, highBits, unsigned);
+    }
+
+    /**
+     * Returns a Long representing the 64 bit integer that comes by concatenating the given low and high bits. Each is
+     *  assumed to use 32 bits.
+     * @function
+     * @param {number} lowBits The low 32 bits
+     * @param {number} highBits The high 32 bits
+     * @param {boolean=} unsigned Whether unsigned or not, defaults to `false` for signed
+     * @returns {!Long} The corresponding Long value
+     */
+    Long.fromBits = fromBits;
+
+    /**
+     * @function
+     * @param {number} base
+     * @param {number} exponent
+     * @returns {number}
+     * @inner
+     */
+    var pow_dbl = Math.pow; // Used 4 times (4*8 to 15+4)
+
+    /**
+     * @param {string} str
+     * @param {(boolean|number)=} unsigned
+     * @param {number=} radix
+     * @returns {!Long}
+     * @inner
+     */
+    function fromString(str, unsigned, radix) {
+        if (str.length === 0)
+            throw Error('empty string');
+        if (str === "NaN" || str === "Infinity" || str === "+Infinity" || str === "-Infinity")
+            return ZERO;
+        if (typeof unsigned === 'number') {
+            // For goog.math.long compatibility
+            radix = unsigned,
+            unsigned = false;
+        } else {
+            unsigned = !! unsigned;
+        }
+        radix = radix || 10;
+        if (radix < 2 || 36 < radix)
+            throw RangeError('radix');
+
+        var p;
+        if ((p = str.indexOf('-')) > 0)
+            throw Error('interior hyphen');
+        else if (p === 0) {
+            return fromString(str.substring(1), unsigned, radix).neg();
+        }
+
+        // Do several (8) digits each time through the loop, so as to
+        // minimize the calls to the very expensive emulated div.
+        var radixToPower = fromNumber(pow_dbl(radix, 8));
+
+        var result = ZERO;
+        for (var i = 0; i < str.length; i += 8) {
+            var size = Math.min(8, str.length - i),
+                value = parseInt(str.substring(i, i + size), radix);
+            if (size < 8) {
+                var power = fromNumber(pow_dbl(radix, size));
+                result = result.mul(power).add(fromNumber(value));
+            } else {
+                result = result.mul(radixToPower);
+                result = result.add(fromNumber(value));
+            }
+        }
+        result.unsigned = unsigned;
+        return result;
+    }
+
+    /**
+     * Returns a Long representation of the given string, written using the specified radix.
+     * @function
+     * @param {string} str The textual representation of the Long
+     * @param {(boolean|number)=} unsigned Whether unsigned or not, defaults to `false` for signed
+     * @param {number=} radix The radix in which the text is written (2-36), defaults to 10
+     * @returns {!Long} The corresponding Long value
+     */
+    Long.fromString = fromString;
+
+    /**
+     * @function
+     * @param {!Long|number|string|!{low: number, high: number, unsigned: boolean}} val
+     * @returns {!Long}
+     * @inner
+     */
+    function fromValue(val) {
+        if (val /* is compatible */ instanceof Long)
+            return val;
+        if (typeof val === 'number')
+            return fromNumber(val);
+        if (typeof val === 'string')
+            return fromString(val);
+        // Throws for non-objects, converts non-instanceof Long:
+        return fromBits(val.low, val.high, val.unsigned);
+    }
+
+    /**
+     * Converts the specified value to a Long.
+     * @function
+     * @param {!Long|number|string|!{low: number, high: number, unsigned: boolean}} val Value
+     * @returns {!Long}
+     */
+    Long.fromValue = fromValue;
+
+    // NOTE: the compiler should inline these constant values below and then remove these variables, so there should be
+    // no runtime penalty for these.
+
+    /**
+     * @type {number}
+     * @const
+     * @inner
+     */
+    var TWO_PWR_16_DBL = 1 << 16;
+
+    /**
+     * @type {number}
+     * @const
+     * @inner
+     */
+    var TWO_PWR_24_DBL = 1 << 24;
+
+    /**
+     * @type {number}
+     * @const
+     * @inner
+     */
+    var TWO_PWR_32_DBL = TWO_PWR_16_DBL * TWO_PWR_16_DBL;
+
+    /**
+     * @type {number}
+     * @const
+     * @inner
+     */
+    var TWO_PWR_64_DBL = TWO_PWR_32_DBL * TWO_PWR_32_DBL;
+
+    /**
+     * @type {number}
+     * @const
+     * @inner
+     */
+    var TWO_PWR_63_DBL = TWO_PWR_64_DBL / 2;
+
+    /**
+     * @type {!Long}
+     * @const
+     * @inner
+     */
+    var TWO_PWR_24 = fromInt(TWO_PWR_24_DBL);
+
+    /**
+     * @type {!Long}
+     * @inner
+     */
+    var ZERO = fromInt(0);
+
+    /**
+     * Signed zero.
+     * @type {!Long}
+     */
+    Long.ZERO = ZERO;
+
+    /**
+     * @type {!Long}
+     * @inner
+     */
+    var UZERO = fromInt(0, true);
+
+    /**
+     * Unsigned zero.
+     * @type {!Long}
+     */
+    Long.UZERO = UZERO;
+
+    /**
+     * @type {!Long}
+     * @inner
+     */
+    var ONE = fromInt(1);
+
+    /**
+     * Signed one.
+     * @type {!Long}
+     */
+    Long.ONE = ONE;
+
+    /**
+     * @type {!Long}
+     * @inner
+     */
+    var UONE = fromInt(1, true);
+
+    /**
+     * Unsigned one.
+     * @type {!Long}
+     */
+    Long.UONE = UONE;
+
+    /**
+     * @type {!Long}
+     * @inner
+     */
+    var NEG_ONE = fromInt(-1);
+
+    /**
+     * Signed negative one.
+     * @type {!Long}
+     */
+    Long.NEG_ONE = NEG_ONE;
+
+    /**
+     * @type {!Long}
+     * @inner
+     */
+    var MAX_VALUE = fromBits(0xFFFFFFFF|0, 0x7FFFFFFF|0, false);
+
+    /**
+     * Maximum signed value.
+     * @type {!Long}
+     */
+    Long.MAX_VALUE = MAX_VALUE;
+
+    /**
+     * @type {!Long}
+     * @inner
+     */
+    var MAX_UNSIGNED_VALUE = fromBits(0xFFFFFFFF|0, 0xFFFFFFFF|0, true);
+
+    /**
+     * Maximum unsigned value.
+     * @type {!Long}
+     */
+    Long.MAX_UNSIGNED_VALUE = MAX_UNSIGNED_VALUE;
+
+    /**
+     * @type {!Long}
+     * @inner
+     */
+    var MIN_VALUE = fromBits(0, 0x80000000|0, false);
+
+    /**
+     * Minimum signed value.
+     * @type {!Long}
+     */
+    Long.MIN_VALUE = MIN_VALUE;
+
+    /**
+     * @alias Long.prototype
+     * @inner
+     */
+    var LongPrototype = Long.prototype;
+
+    /**
+     * Converts the Long to a 32 bit integer, assuming it is a 32 bit integer.
+     * @returns {number}
+     */
+    LongPrototype.toInt = function toInt() {
+        return this.unsigned ? this.low >>> 0 : this.low;
+    };
+
+    /**
+     * Converts the Long to a the nearest floating-point representation of this value (double, 53 bit mantissa).
+     * @returns {number}
+     */
+    LongPrototype.toNumber = function toNumber() {
+        if (this.unsigned)
+            return ((this.high >>> 0) * TWO_PWR_32_DBL) + (this.low >>> 0);
+        return this.high * TWO_PWR_32_DBL + (this.low >>> 0);
+    };
+
+    /**
+     * Converts the Long to a string written in the specified radix.
+     * @param {number=} radix Radix (2-36), defaults to 10
+     * @returns {string}
+     * @override
+     * @throws {RangeError} If `radix` is out of range
+     */
+    LongPrototype.toString = function toString(radix) {
+        radix = radix || 10;
+        if (radix < 2 || 36 < radix)
+            throw RangeError('radix');
+        if (this.isZero())
+            return '0';
+        if (this.isNegative()) { // Unsigned Longs are never negative
+            if (this.eq(MIN_VALUE)) {
+                // We need to change the Long value before it can be negated, so we remove
+                // the bottom-most digit in this base and then recurse to do the rest.
+                var radixLong = fromNumber(radix),
+                    div = this.div(radixLong),
+                    rem1 = div.mul(radixLong).sub(this);
+                return div.toString(radix) + rem1.toInt().toString(radix);
+            } else
+                return '-' + this.neg().toString(radix);
+        }
+
+        // Do several (6) digits each time through the loop, so as to
+        // minimize the calls to the very expensive emulated div.
+        var radixToPower = fromNumber(pow_dbl(radix, 6), this.unsigned),
+            rem = this;
+        var result = '';
+        while (true) {
+            var remDiv = rem.div(radixToPower),
+                intval = rem.sub(remDiv.mul(radixToPower)).toInt() >>> 0,
+                digits = intval.toString(radix);
+            rem = remDiv;
+            if (rem.isZero())
+                return digits + result;
+            else {
+                while (digits.length < 6)
+                    digits = '0' + digits;
+                result = '' + digits + result;
+            }
+        }
+    };
+
+    /**
+     * Gets the high 32 bits as a signed integer.
+     * @returns {number} Signed high bits
+     */
+    LongPrototype.getHighBits = function getHighBits() {
+        return this.high;
+    };
+
+    /**
+     * Gets the high 32 bits as an unsigned integer.
+     * @returns {number} Unsigned high bits
+     */
+    LongPrototype.getHighBitsUnsigned = function getHighBitsUnsigned() {
+        return this.high >>> 0;
+    };
+
+    /**
+     * Gets the low 32 bits as a signed integer.
+     * @returns {number} Signed low bits
+     */
+    LongPrototype.getLowBits = function getLowBits() {
+        return this.low;
+    };
+
+    /**
+     * Gets the low 32 bits as an unsigned integer.
+     * @returns {number} Unsigned low bits
+     */
+    LongPrototype.getLowBitsUnsigned = function getLowBitsUnsigned() {
+        return this.low >>> 0;
+    };
+
+    /**
+     * Gets the number of bits needed to represent the absolute value of this Long.
+     * @returns {number}
+     */
+    LongPrototype.getNumBitsAbs = function getNumBitsAbs() {
+        if (this.isNegative()) // Unsigned Longs are never negative
+            return this.eq(MIN_VALUE) ? 64 : this.neg().getNumBitsAbs();
+        var val = this.high != 0 ? this.high : this.low;
+        for (var bit = 31; bit > 0; bit--)
+            if ((val & (1 << bit)) != 0)
+                break;
+        return this.high != 0 ? bit + 33 : bit + 1;
+    };
+
+    /**
+     * Tests if this Long's value equals zero.
+     * @returns {boolean}
+     */
+    LongPrototype.isZero = function isZero() {
+        return this.high === 0 && this.low === 0;
+    };
+
+    /**
+     * Tests if this Long's value is negative.
+     * @returns {boolean}
+     */
+    LongPrototype.isNegative = function isNegative() {
+        return !this.unsigned && this.high < 0;
+    };
+
+    /**
+     * Tests if this Long's value is positive.
+     * @returns {boolean}
+     */
+    LongPrototype.isPositive = function isPositive() {
+        return this.unsigned || this.high >= 0;
+    };
+
+    /**
+     * Tests if this Long's value is odd.
+     * @returns {boolean}
+     */
+    LongPrototype.isOdd = function isOdd() {
+        return (this.low & 1) === 1;
+    };
+
+    /**
+     * Tests if this Long's value is even.
+     * @returns {boolean}
+     */
+    LongPrototype.isEven = function isEven() {
+        return (this.low & 1) === 0;
+    };
+
+    /**
+     * Tests if this Long's value equals the specified's.
+     * @param {!Long|number|string} other Other value
+     * @returns {boolean}
+     */
+    LongPrototype.equals = function equals(other) {
+        if (!isLong(other))
+            other = fromValue(other);
+        if (this.unsigned !== other.unsigned && (this.high >>> 31) === 1 && (other.high >>> 31) === 1)
+            return false;
+        return this.high === other.high && this.low === other.low;
+    };
+
+    /**
+     * Tests if this Long's value equals the specified's. This is an alias of {@link Long#equals}.
+     * @function
+     * @param {!Long|number|string} other Other value
+     * @returns {boolean}
+     */
+    LongPrototype.eq = LongPrototype.equals;
+
+    /**
+     * Tests if this Long's value differs from the specified's.
+     * @param {!Long|number|string} other Other value
+     * @returns {boolean}
+     */
+    LongPrototype.notEquals = function notEquals(other) {
+        return !this.eq(/* validates */ other);
+    };
+
+    /**
+     * Tests if this Long's value differs from the specified's. This is an alias of {@link Long#notEquals}.
+     * @function
+     * @param {!Long|number|string} other Other value
+     * @returns {boolean}
+     */
+    LongPrototype.neq = LongPrototype.notEquals;
+
+    /**
+     * Tests if this Long's value is less than the specified's.
+     * @param {!Long|number|string} other Other value
+     * @returns {boolean}
+     */
+    LongPrototype.lessThan = function lessThan(other) {
+        return this.comp(/* validates */ other) < 0;
+    };
+
+    /**
+     * Tests if this Long's value is less than the specified's. This is an alias of {@link Long#lessThan}.
+     * @function
+     * @param {!Long|number|string} other Other value
+     * @returns {boolean}
+     */
+    LongPrototype.lt = LongPrototype.lessThan;
+
+    /**
+     * Tests if this Long's value is less than or equal the specified's.
+     * @param {!Long|number|string} other Other value
+     * @returns {boolean}
+     */
+    LongPrototype.lessThanOrEqual = function lessThanOrEqual(other) {
+        return this.comp(/* validates */ other) <= 0;
+    };
+
+    /**
+     * Tests if this Long's value is less than or equal the specified's. This is an alias of {@link Long#lessThanOrEqual}.
+     * @function
+     * @param {!Long|number|string} other Other value
+     * @returns {boolean}
+     */
+    LongPrototype.lte = LongPrototype.lessThanOrEqual;
+
+    /**
+     * Tests if this Long's value is greater than the specified's.
+     * @param {!Long|number|string} other Other value
+     * @returns {boolean}
+     */
+    LongPrototype.greaterThan = function greaterThan(other) {
+        return this.comp(/* validates */ other) > 0;
+    };
+
+    /**
+     * Tests if this Long's value is greater than the specified's. This is an alias of {@link Long#greaterThan}.
+     * @function
+     * @param {!Long|number|string} other Other value
+     * @returns {boolean}
+     */
+    LongPrototype.gt = LongPrototype.greaterThan;
+
+    /**
+     * Tests if this Long's value is greater than or equal the specified's.
+     * @param {!Long|number|string} other Other value
+     * @returns {boolean}
+     */
+    LongPrototype.greaterThanOrEqual = function greaterThanOrEqual(other) {
+        return this.comp(/* validates */ other) >= 0;
+    };
+
+    /**
+     * Tests if this Long's value is greater than or equal the specified's. This is an alias of {@link Long#greaterThanOrEqual}.
+     * @function
+     * @param {!Long|number|string} other Other value
+     * @returns {boolean}
+     */
+    LongPrototype.gte = LongPrototype.greaterThanOrEqual;
+
+    /**
+     * Compares this Long's value with the specified's.
+     * @param {!Long|number|string} other Other value
+     * @returns {number} 0 if they are the same, 1 if the this is greater and -1
+     *  if the given one is greater
+     */
+    LongPrototype.compare = function compare(other) {
+        if (!isLong(other))
+            other = fromValue(other);
+        if (this.eq(other))
+            return 0;
+        var thisNeg = this.isNegative(),
+            otherNeg = other.isNegative();
+        if (thisNeg && !otherNeg)
+            return -1;
+        if (!thisNeg && otherNeg)
+            return 1;
+        // At this point the sign bits are the same
+        if (!this.unsigned)
+            return this.sub(other).isNegative() ? -1 : 1;
+        // Both are positive if at least one is unsigned
+        return (other.high >>> 0) > (this.high >>> 0) || (other.high === this.high && (other.low >>> 0) > (this.low >>> 0)) ? -1 : 1;
+    };
+
+    /**
+     * Compares this Long's value with the specified's. This is an alias of {@link Long#compare}.
+     * @function
+     * @param {!Long|number|string} other Other value
+     * @returns {number} 0 if they are the same, 1 if the this is greater and -1
+     *  if the given one is greater
+     */
+    LongPrototype.comp = LongPrototype.compare;
+
+    /**
+     * Negates this Long's value.
+     * @returns {!Long} Negated Long
+     */
+    LongPrototype.negate = function negate() {
+        if (!this.unsigned && this.eq(MIN_VALUE))
+            return MIN_VALUE;
+        return this.not().add(ONE);
+    };
+
+    /**
+     * Negates this Long's value. This is an alias of {@link Long#negate}.
+     * @function
+     * @returns {!Long} Negated Long
+     */
+    LongPrototype.neg = LongPrototype.negate;
+
+    /**
+     * Returns the sum of this and the specified Long.
+     * @param {!Long|number|string} addend Addend
+     * @returns {!Long} Sum
+     */
+    LongPrototype.add = function add(addend) {
+        if (!isLong(addend))
+            addend = fromValue(addend);
+
+        // Divide each number into 4 chunks of 16 bits, and then sum the chunks.
+
+        var a48 = this.high >>> 16;
+        var a32 = this.high & 0xFFFF;
+        var a16 = this.low >>> 16;
+        var a00 = this.low & 0xFFFF;
+
+        var b48 = addend.high >>> 16;
+        var b32 = addend.high & 0xFFFF;
+        var b16 = addend.low >>> 16;
+        var b00 = addend.low & 0xFFFF;
+
+        var c48 = 0, c32 = 0, c16 = 0, c00 = 0;
+        c00 += a00 + b00;
+        c16 += c00 >>> 16;
+        c00 &= 0xFFFF;
+        c16 += a16 + b16;
+        c32 += c16 >>> 16;
+        c16 &= 0xFFFF;
+        c32 += a32 + b32;
+        c48 += c32 >>> 16;
+        c32 &= 0xFFFF;
+        c48 += a48 + b48;
+        c48 &= 0xFFFF;
+        return fromBits((c16 << 16) | c00, (c48 << 16) | c32, this.unsigned);
+    };
+
+    /**
+     * Returns the difference of this and the specified Long.
+     * @param {!Long|number|string} subtrahend Subtrahend
+     * @returns {!Long} Difference
+     */
+    LongPrototype.subtract = function subtract(subtrahend) {
+        if (!isLong(subtrahend))
+            subtrahend = fromValue(subtrahend);
+        return this.add(subtrahend.neg());
+    };
+
+    /**
+     * Returns the difference of this and the specified Long. This is an alias of {@link Long#subtract}.
+     * @function
+     * @param {!Long|number|string} subtrahend Subtrahend
+     * @returns {!Long} Difference
+     */
+    LongPrototype.sub = LongPrototype.subtract;
+
+    /**
+     * Returns the product of this and the specified Long.
+     * @param {!Long|number|string} multiplier Multiplier
+     * @returns {!Long} Product
+     */
+    LongPrototype.multiply = function multiply(multiplier) {
+        if (this.isZero())
+            return ZERO;
+        if (!isLong(multiplier))
+            multiplier = fromValue(multiplier);
+        if (multiplier.isZero())
+            return ZERO;
+        if (this.eq(MIN_VALUE))
+            return multiplier.isOdd() ? MIN_VALUE : ZERO;
+        if (multiplier.eq(MIN_VALUE))
+            return this.isOdd() ? MIN_VALUE : ZERO;
+
+        if (this.isNegative()) {
+            if (multiplier.isNegative())
+                return this.neg().mul(multiplier.neg());
+            else
+                return this.neg().mul(multiplier).neg();
+        } else if (multiplier.isNegative())
+            return this.mul(multiplier.neg()).neg();
+
+        // If both longs are small, use float multiplication
+        if (this.lt(TWO_PWR_24) && multiplier.lt(TWO_PWR_24))
+            return fromNumber(this.toNumber() * multiplier.toNumber(), this.unsigned);
+
+        // Divide each long into 4 chunks of 16 bits, and then add up 4x4 products.
+        // We can skip products that would overflow.
+
+        var a48 = this.high >>> 16;
+        var a32 = this.high & 0xFFFF;
+        var a16 = this.low >>> 16;
+        var a00 = this.low & 0xFFFF;
+
+        var b48 = multiplier.high >>> 16;
+        var b32 = multiplier.high & 0xFFFF;
+        var b16 = multiplier.low >>> 16;
+        var b00 = multiplier.low & 0xFFFF;
+
+        var c48 = 0, c32 = 0, c16 = 0, c00 = 0;
+        c00 += a00 * b00;
+        c16 += c00 >>> 16;
+        c00 &= 0xFFFF;
+        c16 += a16 * b00;
+        c32 += c16 >>> 16;
+        c16 &= 0xFFFF;
+        c16 += a00 * b16;
+        c32 += c16 >>> 16;
+        c16 &= 0xFFFF;
+        c32 += a32 * b00;
+        c48 += c32 >>> 16;
+        c32 &= 0xFFFF;
+        c32 += a16 * b16;
+        c48 += c32 >>> 16;
+        c32 &= 0xFFFF;
+        c32 += a00 * b32;
+        c48 += c32 >>> 16;
+        c32 &= 0xFFFF;
+        c48 += a48 * b00 + a32 * b16 + a16 * b32 + a00 * b48;
+        c48 &= 0xFFFF;
+        return fromBits((c16 << 16) | c00, (c48 << 16) | c32, this.unsigned);
+    };
+
+    /**
+     * Returns the product of this and the specified Long. This is an alias of {@link Long#multiply}.
+     * @function
+     * @param {!Long|number|string} multiplier Multiplier
+     * @returns {!Long} Product
+     */
+    LongPrototype.mul = LongPrototype.multiply;
+
+    /**
+     * Returns this Long divided by the specified. The result is signed if this Long is signed or
+     *  unsigned if this Long is unsigned.
+     * @param {!Long|number|string} divisor Divisor
+     * @returns {!Long} Quotient
+     */
+    LongPrototype.divide = function divide(divisor) {
+        if (!isLong(divisor))
+            divisor = fromValue(divisor);
+        if (divisor.isZero())
+            throw Error('division by zero');
+        if (this.isZero())
+            return this.unsigned ? UZERO : ZERO;
+        var approx, rem, res;
+        if (!this.unsigned) {
+            // This section is only relevant for signed longs and is derived from the
+            // closure library as a whole.
+            if (this.eq(MIN_VALUE)) {
+                if (divisor.eq(ONE) || divisor.eq(NEG_ONE))
+                    return MIN_VALUE;  // recall that -MIN_VALUE == MIN_VALUE
+                else if (divisor.eq(MIN_VALUE))
+                    return ONE;
+                else {
+                    // At this point, we have |other| >= 2, so |this/other| < |MIN_VALUE|.
+                    var halfThis = this.shr(1);
+                    approx = halfThis.div(divisor).shl(1);
+                    if (approx.eq(ZERO)) {
+                        return divisor.isNegative() ? ONE : NEG_ONE;
+                    } else {
+                        rem = this.sub(divisor.mul(approx));
+                        res = approx.add(rem.div(divisor));
+                        return res;
+                    }
+                }
+            } else if (divisor.eq(MIN_VALUE))
+                return this.unsigned ? UZERO : ZERO;
+            if (this.isNegative()) {
+                if (divisor.isNegative())
+                    return this.neg().div(divisor.neg());
+                return this.neg().div(divisor).neg();
+            } else if (divisor.isNegative())
+                return this.div(divisor.neg()).neg();
+            res = ZERO;
+        } else {
+            // The algorithm below has not been made for unsigned longs. It's therefore
+            // required to take special care of the MSB prior to running it.
+            if (!divisor.unsigned)
+                divisor = divisor.toUnsigned();
+            if (divisor.gt(this))
+                return UZERO;
+            if (divisor.gt(this.shru(1))) // 15 >>> 1 = 7 ; with divisor = 8 ; true
+                return UONE;
+            res = UZERO;
+        }
+
+        // Repeat the following until the remainder is less than other:  find a
+        // floating-point that approximates remainder / other *from below*, add this
+        // into the result, and subtract it from the remainder.  It is critical that
+        // the approximate value is less than or equal to the real value so that the
+        // remainder never becomes negative.
+        rem = this;
+        while (rem.gte(divisor)) {
+            // Approximate the result of division. This may be a little greater or
+            // smaller than the actual value.
+            approx = Math.max(1, Math.floor(rem.toNumber() / divisor.toNumber()));
+
+            // We will tweak the approximate result by changing it in the 48-th digit or
+            // the smallest non-fractional digit, whichever is larger.
+            var log2 = Math.ceil(Math.log(approx) / Math.LN2),
+                delta = (log2 <= 48) ? 1 : pow_dbl(2, log2 - 48),
+
+            // Decrease the approximation until it is smaller than the remainder.  Note
+            // that if it is too large, the product overflows and is negative.
+                approxRes = fromNumber(approx),
+                approxRem = approxRes.mul(divisor);
+            while (approxRem.isNegative() || approxRem.gt(rem)) {
+                approx -= delta;
+                approxRes = fromNumber(approx, this.unsigned);
+                approxRem = approxRes.mul(divisor);
+            }
+
+            // We know the answer can't be zero... and actually, zero would cause
+            // infinite recursion since we would make no progress.
+            if (approxRes.isZero())
+                approxRes = ONE;
+
+            res = res.add(approxRes);
+            rem = rem.sub(approxRem);
+        }
+        return res;
+    };
+
+    /**
+     * Returns this Long divided by the specified. This is an alias of {@link Long#divide}.
+     * @function
+     * @param {!Long|number|string} divisor Divisor
+     * @returns {!Long} Quotient
+     */
+    LongPrototype.div = LongPrototype.divide;
+
+    /**
+     * Returns this Long modulo the specified.
+     * @param {!Long|number|string} divisor Divisor
+     * @returns {!Long} Remainder
+     */
+    LongPrototype.modulo = function modulo(divisor) {
+        if (!isLong(divisor))
+            divisor = fromValue(divisor);
+        return this.sub(this.div(divisor).mul(divisor));
+    };
+
+    /**
+     * Returns this Long modulo the specified. This is an alias of {@link Long#modulo}.
+     * @function
+     * @param {!Long|number|string} divisor Divisor
+     * @returns {!Long} Remainder
+     */
+    LongPrototype.mod = LongPrototype.modulo;
+
+    /**
+     * Returns the bitwise NOT of this Long.
+     * @returns {!Long}
+     */
+    LongPrototype.not = function not() {
+        return fromBits(~this.low, ~this.high, this.unsigned);
+    };
+
+    /**
+     * Returns the bitwise AND of this Long and the specified.
+     * @param {!Long|number|string} other Other Long
+     * @returns {!Long}
+     */
+    LongPrototype.and = function and(other) {
+        if (!isLong(other))
+            other = fromValue(other);
+        return fromBits(this.low & other.low, this.high & other.high, this.unsigned);
+    };
+
+    /**
+     * Returns the bitwise OR of this Long and the specified.
+     * @param {!Long|number|string} other Other Long
+     * @returns {!Long}
+     */
+    LongPrototype.or = function or(other) {
+        if (!isLong(other))
+            other = fromValue(other);
+        return fromBits(this.low | other.low, this.high | other.high, this.unsigned);
+    };
+
+    /**
+     * Returns the bitwise XOR of this Long and the given one.
+     * @param {!Long|number|string} other Other Long
+     * @returns {!Long}
+     */
+    LongPrototype.xor = function xor(other) {
+        if (!isLong(other))
+            other = fromValue(other);
+        return fromBits(this.low ^ other.low, this.high ^ other.high, this.unsigned);
+    };
+
+    /**
+     * Returns this Long with bits shifted to the left by the given amount.
+     * @param {number|!Long} numBits Number of bits
+     * @returns {!Long} Shifted Long
+     */
+    LongPrototype.shiftLeft = function shiftLeft(numBits) {
+        if (isLong(numBits))
+            numBits = numBits.toInt();
+        if ((numBits &= 63) === 0)
+            return this;
+        else if (numBits < 32)
+            return fromBits(this.low << numBits, (this.high << numBits) | (this.low >>> (32 - numBits)), this.unsigned);
+        else
+            return fromBits(0, this.low << (numBits - 32), this.unsigned);
+    };
+
+    /**
+     * Returns this Long with bits shifted to the left by the given amount. This is an alias of {@link Long#shiftLeft}.
+     * @function
+     * @param {number|!Long} numBits Number of bits
+     * @returns {!Long} Shifted Long
+     */
+    LongPrototype.shl = LongPrototype.shiftLeft;
+
+    /**
+     * Returns this Long with bits arithmetically shifted to the right by the given amount.
+     * @param {number|!Long} numBits Number of bits
+     * @returns {!Long} Shifted Long
+     */
+    LongPrototype.shiftRight = function shiftRight(numBits) {
+        if (isLong(numBits))
+            numBits = numBits.toInt();
+        if ((numBits &= 63) === 0)
+            return this;
+        else if (numBits < 32)
+            return fromBits((this.low >>> numBits) | (this.high << (32 - numBits)), this.high >> numBits, this.unsigned);
+        else
+            return fromBits(this.high >> (numBits - 32), this.high >= 0 ? 0 : -1, this.unsigned);
+    };
+
+    /**
+     * Returns this Long with bits arithmetically shifted to the right by the given amount. This is an alias of {@link Long#shiftRight}.
+     * @function
+     * @param {number|!Long} numBits Number of bits
+     * @returns {!Long} Shifted Long
+     */
+    LongPrototype.shr = LongPrototype.shiftRight;
+
+    /**
+     * Returns this Long with bits logically shifted to the right by the given amount.
+     * @param {number|!Long} numBits Number of bits
+     * @returns {!Long} Shifted Long
+     */
+    LongPrototype.shiftRightUnsigned = function shiftRightUnsigned(numBits) {
+        if (isLong(numBits))
+            numBits = numBits.toInt();
+        numBits &= 63;
+        if (numBits === 0)
+            return this;
+        else {
+            var high = this.high;
+            if (numBits < 32) {
+                var low = this.low;
+                return fromBits((low >>> numBits) | (high << (32 - numBits)), high >>> numBits, this.unsigned);
+            } else if (numBits === 32)
+                return fromBits(high, 0, this.unsigned);
+            else
+                return fromBits(high >>> (numBits - 32), 0, this.unsigned);
+        }
+    };
+
+    /**
+     * Returns this Long with bits logically shifted to the right by the given amount. This is an alias of {@link Long#shiftRightUnsigned}.
+     * @function
+     * @param {number|!Long} numBits Number of bits
+     * @returns {!Long} Shifted Long
+     */
+    LongPrototype.shru = LongPrototype.shiftRightUnsigned;
+
+    /**
+     * Converts this Long to signed.
+     * @returns {!Long} Signed long
+     */
+    LongPrototype.toSigned = function toSigned() {
+        if (!this.unsigned)
+            return this;
+        return fromBits(this.low, this.high, false);
+    };
+
+    /**
+     * Converts this Long to unsigned.
+     * @returns {!Long} Unsigned long
+     */
+    LongPrototype.toUnsigned = function toUnsigned() {
+        if (this.unsigned)
+            return this;
+        return fromBits(this.low, this.high, true);
+    };
+
+    /**
+     * Converts this Long to its byte representation.
+     * @param {boolean=} le Whether little or big endian, defaults to big endian
+     * @returns {!Array.<number>} Byte representation
+     */
+    LongPrototype.toBytes = function(le) {
+        return le ? this.toBytesLE() : this.toBytesBE();
+    }
+
+    /**
+     * Converts this Long to its little endian byte representation.
+     * @returns {!Array.<number>} Little endian byte representation
+     */
+    LongPrototype.toBytesLE = function() {
+        var hi = this.high,
+            lo = this.low;
+        return [
+             lo         & 0xff,
+            (lo >>>  8) & 0xff,
+            (lo >>> 16) & 0xff,
+            (lo >>> 24) & 0xff,
+             hi         & 0xff,
+            (hi >>>  8) & 0xff,
+            (hi >>> 16) & 0xff,
+            (hi >>> 24) & 0xff
+        ];
+    }
+
+    /**
+     * Converts this Long to its big endian byte representation.
+     * @returns {!Array.<number>} Big endian byte representation
+     */
+    LongPrototype.toBytesBE = function() {
+        var hi = this.high,
+            lo = this.low;
+        return [
+            (hi >>> 24) & 0xff,
+            (hi >>> 16) & 0xff,
+            (hi >>>  8) & 0xff,
+             hi         & 0xff,
+            (lo >>> 24) & 0xff,
+            (lo >>> 16) & 0xff,
+            (lo >>>  8) & 0xff,
+             lo         & 0xff
+        ];
+    }
+    return Long;
+});

+ 5222 - 0
3rdparty/protobuf.js

@@ -0,0 +1,5222 @@
+/*
+ Copyright 2013 Daniel Wirtz <dcode@dcode.io>
+
+ Licensed under the Apache License, Version 2.0 (the "License");
+ you may not use this file except in compliance with the License.
+ You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+ */
+
+/**
+ * @license protobuf.js (c) 2013 Daniel Wirtz <dcode@dcode.io>
+ * Released under the Apache License, Version 2.0
+ * see: https://github.com/dcodeIO/protobuf.js for details
+ */
+(function(global, factory) {
+    /* AMD */ if (typeof define === 'function' && define["amd"])
+        define(["bytebuffer"], factory);
+    /* CommonJS */ else if (typeof require === "function" && typeof module === "object" && module && module["exports"])
+        module["exports"] = factory(require("bytebuffer"), true);
+    /* Global */ else
+        (global["dcodeIO"] = global["dcodeIO"] || {})["ProtoBuf"] = factory(global["dcodeIO"]["ByteBuffer"]);
+
+})(this, function(ByteBuffer, isCommonJS) {
+    "use strict";
+
+    /**
+     * The ProtoBuf namespace.
+     * @exports ProtoBuf
+     * @namespace
+     * @expose
+     */
+    var ProtoBuf = {};
+
+    /**
+     * @type {!function(new: ByteBuffer, ...[*])}
+     * @expose
+     */
+    ProtoBuf.ByteBuffer = ByteBuffer;
+
+    /**
+     * @type {?function(new: Long, ...[*])}
+     * @expose
+     */
+    ProtoBuf.Long = ByteBuffer.Long || null;
+
+    /**
+     * ProtoBuf.js version.
+     * @type {string}
+     * @const
+     * @expose
+     */
+    ProtoBuf.VERSION = "5.0.1";
+
+    /**
+     * Wire types.
+     * @type {Object.<string,number>}
+     * @const
+     * @expose
+     */
+    ProtoBuf.WIRE_TYPES = {};
+
+    /**
+     * Varint wire type.
+     * @type {number}
+     * @expose
+     */
+    ProtoBuf.WIRE_TYPES.VARINT = 0;
+
+    /**
+     * Fixed 64 bits wire type.
+     * @type {number}
+     * @const
+     * @expose
+     */
+    ProtoBuf.WIRE_TYPES.BITS64 = 1;
+
+    /**
+     * Length delimited wire type.
+     * @type {number}
+     * @const
+     * @expose
+     */
+    ProtoBuf.WIRE_TYPES.LDELIM = 2;
+
+    /**
+     * Start group wire type.
+     * @type {number}
+     * @const
+     * @expose
+     */
+    ProtoBuf.WIRE_TYPES.STARTGROUP = 3;
+
+    /**
+     * End group wire type.
+     * @type {number}
+     * @const
+     * @expose
+     */
+    ProtoBuf.WIRE_TYPES.ENDGROUP = 4;
+
+    /**
+     * Fixed 32 bits wire type.
+     * @type {number}
+     * @const
+     * @expose
+     */
+    ProtoBuf.WIRE_TYPES.BITS32 = 5;
+
+    /**
+     * Packable wire types.
+     * @type {!Array.<number>}
+     * @const
+     * @expose
+     */
+    ProtoBuf.PACKABLE_WIRE_TYPES = [
+        ProtoBuf.WIRE_TYPES.VARINT,
+        ProtoBuf.WIRE_TYPES.BITS64,
+        ProtoBuf.WIRE_TYPES.BITS32
+    ];
+
+    /**
+     * Types.
+     * @dict
+     * @type {!Object.<string,{name: string, wireType: number, defaultValue: *}>}
+     * @const
+     * @expose
+     */
+    ProtoBuf.TYPES = {
+        // According to the protobuf spec.
+        "int32": {
+            name: "int32",
+            wireType: ProtoBuf.WIRE_TYPES.VARINT,
+            defaultValue: 0
+        },
+        "uint32": {
+            name: "uint32",
+            wireType: ProtoBuf.WIRE_TYPES.VARINT,
+            defaultValue: 0
+        },
+        "sint32": {
+            name: "sint32",
+            wireType: ProtoBuf.WIRE_TYPES.VARINT,
+            defaultValue: 0
+        },
+        "int64": {
+            name: "int64",
+            wireType: ProtoBuf.WIRE_TYPES.VARINT,
+            defaultValue: ProtoBuf.Long ? ProtoBuf.Long.ZERO : undefined
+        },
+        "uint64": {
+            name: "uint64",
+            wireType: ProtoBuf.WIRE_TYPES.VARINT,
+            defaultValue: ProtoBuf.Long ? ProtoBuf.Long.UZERO : undefined
+        },
+        "sint64": {
+            name: "sint64",
+            wireType: ProtoBuf.WIRE_TYPES.VARINT,
+            defaultValue: ProtoBuf.Long ? ProtoBuf.Long.ZERO : undefined
+        },
+        "bool": {
+            name: "bool",
+            wireType: ProtoBuf.WIRE_TYPES.VARINT,
+            defaultValue: false
+        },
+        "double": {
+            name: "double",
+            wireType: ProtoBuf.WIRE_TYPES.BITS64,
+            defaultValue: 0
+        },
+        "string": {
+            name: "string",
+            wireType: ProtoBuf.WIRE_TYPES.LDELIM,
+            defaultValue: ""
+        },
+        "bytes": {
+            name: "bytes",
+            wireType: ProtoBuf.WIRE_TYPES.LDELIM,
+            defaultValue: null // overridden in the code, must be a unique instance
+        },
+        "fixed32": {
+            name: "fixed32",
+            wireType: ProtoBuf.WIRE_TYPES.BITS32,
+            defaultValue: 0
+        },
+        "sfixed32": {
+            name: "sfixed32",
+            wireType: ProtoBuf.WIRE_TYPES.BITS32,
+            defaultValue: 0
+        },
+        "fixed64": {
+            name: "fixed64",
+            wireType: ProtoBuf.WIRE_TYPES.BITS64,
+            defaultValue:  ProtoBuf.Long ? ProtoBuf.Long.UZERO : undefined
+        },
+        "sfixed64": {
+            name: "sfixed64",
+            wireType: ProtoBuf.WIRE_TYPES.BITS64,
+            defaultValue: ProtoBuf.Long ? ProtoBuf.Long.ZERO : undefined
+        },
+        "float": {
+            name: "float",
+            wireType: ProtoBuf.WIRE_TYPES.BITS32,
+            defaultValue: 0
+        },
+        "enum": {
+            name: "enum",
+            wireType: ProtoBuf.WIRE_TYPES.VARINT,
+            defaultValue: 0
+        },
+        "message": {
+            name: "message",
+            wireType: ProtoBuf.WIRE_TYPES.LDELIM,
+            defaultValue: null
+        },
+        "group": {
+            name: "group",
+            wireType: ProtoBuf.WIRE_TYPES.STARTGROUP,
+            defaultValue: null
+        }
+    };
+
+    /**
+     * Valid map key types.
+     * @type {!Array.<!Object.<string,{name: string, wireType: number, defaultValue: *}>>}
+     * @const
+     * @expose
+     */
+    ProtoBuf.MAP_KEY_TYPES = [
+        ProtoBuf.TYPES["int32"],
+        ProtoBuf.TYPES["sint32"],
+        ProtoBuf.TYPES["sfixed32"],
+        ProtoBuf.TYPES["uint32"],
+        ProtoBuf.TYPES["fixed32"],
+        ProtoBuf.TYPES["int64"],
+        ProtoBuf.TYPES["sint64"],
+        ProtoBuf.TYPES["sfixed64"],
+        ProtoBuf.TYPES["uint64"],
+        ProtoBuf.TYPES["fixed64"],
+        ProtoBuf.TYPES["bool"],
+        ProtoBuf.TYPES["string"],
+        ProtoBuf.TYPES["bytes"]
+    ];
+
+    /**
+     * Minimum field id.
+     * @type {number}
+     * @const
+     * @expose
+     */
+    ProtoBuf.ID_MIN = 1;
+
+    /**
+     * Maximum field id.
+     * @type {number}
+     * @const
+     * @expose
+     */
+    ProtoBuf.ID_MAX = 0x1FFFFFFF;
+
+    /**
+     * If set to `true`, field names will be converted from underscore notation to camel case. Defaults to `false`.
+     *  Must be set prior to parsing.
+     * @type {boolean}
+     * @expose
+     */
+    ProtoBuf.convertFieldsToCamelCase = false;
+
+    /**
+     * By default, messages are populated with (setX, set_x) accessors for each field. This can be disabled by
+     *  setting this to `false` prior to building messages.
+     * @type {boolean}
+     * @expose
+     */
+    ProtoBuf.populateAccessors = true;
+
+    /**
+     * By default, messages are populated with default values if a field is not present on the wire. To disable
+     *  this behavior, set this setting to `false`.
+     * @type {boolean}
+     * @expose
+     */
+    ProtoBuf.populateDefaults = true;
+
+    /**
+     * @alias ProtoBuf.Util
+     * @expose
+     */
+    ProtoBuf.Util = (function() {
+        "use strict";
+
+        /**
+         * ProtoBuf utilities.
+         * @exports ProtoBuf.Util
+         * @namespace
+         */
+        var Util = {};
+
+        /**
+         * Flag if running in node or not.
+         * @type {boolean}
+         * @const
+         * @expose
+         */
+        Util.IS_NODE = !!(
+            typeof process === 'object' && process+'' === '[object process]' && !process['browser']
+        );
+
+        /**
+         * Constructs a XMLHttpRequest object.
+         * @return {XMLHttpRequest}
+         * @throws {Error} If XMLHttpRequest is not supported
+         * @expose
+         */
+        Util.XHR = function() {
+            // No dependencies please, ref: http://www.quirksmode.org/js/xmlhttp.html
+            var XMLHttpFactories = [
+                function () {return new XMLHttpRequest()},
+                function () {return new ActiveXObject("Msxml2.XMLHTTP")},
+                function () {return new ActiveXObject("Msxml3.XMLHTTP")},
+                function () {return new ActiveXObject("Microsoft.XMLHTTP")}
+            ];
+            /** @type {?XMLHttpRequest} */
+            var xhr = null;
+            for (var i=0;i<XMLHttpFactories.length;i++) {
+                try { xhr = XMLHttpFactories[i](); }
+                catch (e) { continue; }
+                break;
+            }
+            if (!xhr)
+                throw Error("XMLHttpRequest is not supported");
+            return xhr;
+        };
+
+        /**
+         * Fetches a resource.
+         * @param {string} path Resource path
+         * @param {function(?string)=} callback Callback receiving the resource's contents. If omitted the resource will
+         *   be fetched synchronously. If the request failed, contents will be null.
+         * @return {?string|undefined} Resource contents if callback is omitted (null if the request failed), else undefined.
+         * @expose
+         */
+        Util.fetch = function(path, callback) {
+            if (callback && typeof callback != 'function')
+                callback = null;
+            if (Util.IS_NODE) {
+                var fs = require("fs");
+                if (callback) {
+                    fs.readFile(path, function(err, data) {
+                        if (err)
+                            callback(null);
+                        else
+                            callback(""+data);
+                    });
+                } else
+                    try {
+                        return fs.readFileSync(path);
+                    } catch (e) {
+                        return null;
+                    }
+            } else {
+                var xhr = Util.XHR();
+                xhr.open('GET', path, callback ? true : false);
+                // xhr.setRequestHeader('User-Agent', 'XMLHTTP/1.0');
+                xhr.setRequestHeader('Accept', 'text/plain');
+                if (typeof xhr.overrideMimeType === 'function') xhr.overrideMimeType('text/plain');
+                if (callback) {
+                    xhr.onreadystatechange = function() {
+                        if (xhr.readyState != 4) return;
+                        if (/* remote */ xhr.status == 200 || /* local */ (xhr.status == 0 && typeof xhr.responseText === 'string'))
+                            callback(xhr.responseText);
+                        else
+                            callback(null);
+                    };
+                    if (xhr.readyState == 4)
+                        return;
+                    xhr.send(null);
+                } else {
+                    xhr.send(null);
+                    if (/* remote */ xhr.status == 200 || /* local */ (xhr.status == 0 && typeof xhr.responseText === 'string'))
+                        return xhr.responseText;
+                    return null;
+                }
+            }
+        };
+
+        /**
+         * Converts a string to camel case.
+         * @param {string} str
+         * @returns {string}
+         * @expose
+         */
+        Util.toCamelCase = function(str) {
+            return str.replace(/_([a-zA-Z])/g, function ($0, $1) {
+                return $1.toUpperCase();
+            });
+        };
+
+        return Util;
+    })();
+
+    /**
+     * Language expressions.
+     * @type {!Object.<string,!RegExp>}
+     * @expose
+     */
+    ProtoBuf.Lang = {
+
+        // Characters always ending a statement
+        DELIM: /[\s\{\}=;:\[\],'"\(\)<>]/g,
+
+        // Field rules
+        RULE: /^(?:required|optional|repeated|map)$/,
+
+        // Field types
+        TYPE: /^(?:double|float|int32|uint32|sint32|int64|uint64|sint64|fixed32|sfixed32|fixed64|sfixed64|bool|string|bytes)$/,
+
+        // Names
+        NAME: /^[a-zA-Z_][a-zA-Z_0-9]*$/,
+
+        // Type definitions
+        TYPEDEF: /^[a-zA-Z][a-zA-Z_0-9]*$/,
+
+        // Type references
+        TYPEREF: /^(?:\.?[a-zA-Z_][a-zA-Z_0-9]*)+$/,
+
+        // Fully qualified type references
+        FQTYPEREF: /^(?:\.[a-zA-Z][a-zA-Z_0-9]*)+$/,
+
+        // All numbers
+        NUMBER: /^-?(?:[1-9][0-9]*|0|0[xX][0-9a-fA-F]+|0[0-7]+|([0-9]*(\.[0-9]*)?([Ee][+-]?[0-9]+)?)|inf|nan)$/,
+
+        // Decimal numbers
+        NUMBER_DEC: /^(?:[1-9][0-9]*|0)$/,
+
+        // Hexadecimal numbers
+        NUMBER_HEX: /^0[xX][0-9a-fA-F]+$/,
+
+        // Octal numbers
+        NUMBER_OCT: /^0[0-7]+$/,
+
+        // Floating point numbers
+        NUMBER_FLT: /^([0-9]*(\.[0-9]*)?([Ee][+-]?[0-9]+)?|inf|nan)$/,
+
+        // Booleans
+        BOOL: /^(?:true|false)$/i,
+
+        // Id numbers
+        ID: /^(?:[1-9][0-9]*|0|0[xX][0-9a-fA-F]+|0[0-7]+)$/,
+
+        // Negative id numbers (enum values)
+        NEGID: /^\-?(?:[1-9][0-9]*|0|0[xX][0-9a-fA-F]+|0[0-7]+)$/,
+
+        // Whitespaces
+        WHITESPACE: /\s/,
+
+        // All strings
+        STRING: /(?:"([^"\\]*(?:\\.[^"\\]*)*)")|(?:'([^'\\]*(?:\\.[^'\\]*)*)')/g,
+
+        // Double quoted strings
+        STRING_DQ: /(?:"([^"\\]*(?:\\.[^"\\]*)*)")/g,
+
+        // Single quoted strings
+        STRING_SQ: /(?:'([^'\\]*(?:\\.[^'\\]*)*)')/g
+    };
+
+    /**
+     * @alias ProtoBuf.DotProto
+     * @expose
+     */
+    ProtoBuf.DotProto = (function(ProtoBuf, Lang) {
+        "use strict";
+
+        /**
+         * Utilities to parse .proto files.
+         * @exports ProtoBuf.DotProto
+         * @namespace
+         */
+        var DotProto = {};
+
+        /**
+         * Constructs a new Tokenizer.
+         * @exports ProtoBuf.DotProto.Tokenizer
+         * @class prototype tokenizer
+         * @param {string} proto Proto to tokenize
+         * @constructor
+         */
+        var Tokenizer = function(proto) {
+
+            /**
+             * Source to parse.
+             * @type {string}
+             * @expose
+             */
+            this.source = proto+"";
+
+            /**
+             * Current index.
+             * @type {number}
+             * @expose
+             */
+            this.index = 0;
+
+            /**
+             * Current line.
+             * @type {number}
+             * @expose
+             */
+            this.line = 1;
+
+            /**
+             * Token stack.
+             * @type {!Array.<string>}
+             * @expose
+             */
+            this.stack = [];
+
+            /**
+             * Opening character of the current string read, if any.
+             * @type {?string}
+             * @private
+             */
+            this._stringOpen = null;
+        };
+
+        /**
+         * @alias ProtoBuf.DotProto.Tokenizer.prototype
+         * @inner
+         */
+        var TokenizerPrototype = Tokenizer.prototype;
+
+        /**
+         * Reads a string beginning at the current index.
+         * @return {string}
+         * @private
+         */
+        TokenizerPrototype._readString = function() {
+            var re = this._stringOpen === '"'
+                ? Lang.STRING_DQ
+                : Lang.STRING_SQ;
+            re.lastIndex = this.index - 1; // Include the open quote
+            var match = re.exec(this.source);
+            if (!match)
+                throw Error("unterminated string");
+            this.index = re.lastIndex;
+            this.stack.push(this._stringOpen);
+            this._stringOpen = null;
+            return match[1];
+        };
+
+        /**
+         * Gets the next token and advances by one.
+         * @return {?string} Token or `null` on EOF
+         * @expose
+         */
+        TokenizerPrototype.next = function() {
+            if (this.stack.length > 0)
+                return this.stack.shift();
+            if (this.index >= this.source.length)
+                return null;
+            if (this._stringOpen !== null)
+                return this._readString();
+
+            var repeat,
+                prev,
+                next;
+            do {
+                repeat = false;
+
+                // Strip white spaces
+                while (Lang.WHITESPACE.test(next = this.source.charAt(this.index))) {
+                    if (next === '\n')
+                        ++this.line;
+                    if (++this.index === this.source.length)
+                        return null;
+                }
+
+                // Strip comments
+                if (this.source.charAt(this.index) === '/') {
+                    ++this.index;
+                    if (this.source.charAt(this.index) === '/') { // Line
+                        while (this.source.charAt(++this.index) !== '\n')
+                            if (this.index == this.source.length)
+                                return null;
+                        ++this.index;
+                        ++this.line;
+                        repeat = true;
+                    } else if ((next = this.source.charAt(this.index)) === '*') { /* Block */
+                        do {
+                            if (next === '\n')
+                                ++this.line;
+                            if (++this.index === this.source.length)
+                                return null;
+                            prev = next;
+                            next = this.source.charAt(this.index);
+                        } while (prev !== '*' || next !== '/');
+                        ++this.index;
+                        repeat = true;
+                    } else
+                        return '/';
+                }
+            } while (repeat);
+
+            if (this.index === this.source.length)
+                return null;
+
+            // Read the next token
+            var end = this.index;
+            Lang.DELIM.lastIndex = 0;
+            var delim = Lang.DELIM.test(this.source.charAt(end++));
+            if (!delim)
+                while(end < this.source.length && !Lang.DELIM.test(this.source.charAt(end)))
+                    ++end;
+            var token = this.source.substring(this.index, this.index = end);
+            if (token === '"' || token === "'")
+                this._stringOpen = token;
+            return token;
+        };
+
+        /**
+         * Peeks for the next token.
+         * @return {?string} Token or `null` on EOF
+         * @expose
+         */
+        TokenizerPrototype.peek = function() {
+            if (this.stack.length === 0) {
+                var token = this.next();
+                if (token === null)
+                    return null;
+                this.stack.push(token);
+            }
+            return this.stack[0];
+        };
+
+        /**
+         * Skips a specific token and throws if it differs.
+         * @param {string} expected Expected token
+         * @throws {Error} If the actual token differs
+         */
+        TokenizerPrototype.skip = function(expected) {
+            var actual = this.next();
+            if (actual !== expected)
+                throw Error("illegal '"+actual+"', '"+expected+"' expected");
+        };
+
+        /**
+         * Omits an optional token.
+         * @param {string} expected Expected optional token
+         * @returns {boolean} `true` if the token exists
+         */
+        TokenizerPrototype.omit = function(expected) {
+            if (this.peek() === expected) {
+                this.next();
+                return true;
+            }
+            return false;
+        };
+
+        /**
+         * Returns a string representation of this object.
+         * @return {string} String representation as of "Tokenizer(index/length)"
+         * @expose
+         */
+        TokenizerPrototype.toString = function() {
+            return "Tokenizer ("+this.index+"/"+this.source.length+" at line "+this.line+")";
+        };
+
+        /**
+         * @alias ProtoBuf.DotProto.Tokenizer
+         * @expose
+         */
+        DotProto.Tokenizer = Tokenizer;
+
+        /**
+         * Constructs a new Parser.
+         * @exports ProtoBuf.DotProto.Parser
+         * @class prototype parser
+         * @param {string} source Source
+         * @constructor
+         */
+        var Parser = function(source) {
+
+            /**
+             * Tokenizer.
+             * @type {!ProtoBuf.DotProto.Tokenizer}
+             * @expose
+             */
+            this.tn = new Tokenizer(source);
+
+            /**
+             * Whether parsing proto3 or not.
+             * @type {boolean}
+             */
+            this.proto3 = false;
+        };
+
+        /**
+         * @alias ProtoBuf.DotProto.Parser.prototype
+         * @inner
+         */
+        var ParserPrototype = Parser.prototype;
+
+        /**
+         * Parses the source.
+         * @returns {!Object}
+         * @throws {Error} If the source cannot be parsed
+         * @expose
+         */
+        ParserPrototype.parse = function() {
+            var topLevel = {
+                "name": "[ROOT]", // temporary
+                "package": null,
+                "messages": [],
+                "enums": [],
+                "imports": [],
+                "options": {},
+                "services": []
+                // "syntax": undefined
+            };
+            var token,
+                head = true,
+                weak;
+            try {
+                while (token = this.tn.next()) {
+                    switch (token) {
+                        case 'package':
+                            if (!head || topLevel["package"] !== null)
+                                throw Error("unexpected 'package'");
+                            token = this.tn.next();
+                            if (!Lang.TYPEREF.test(token))
+                                throw Error("illegal package name: " + token);
+                            this.tn.skip(";");
+                            topLevel["package"] = token;
+                            break;
+                        case 'import':
+                            if (!head)
+                                throw Error("unexpected 'import'");
+                            token = this.tn.peek();
+                            if (token === "public" || (weak = token === "weak")) // token ignored
+                                this.tn.next();
+                            token = this._readString();
+                            this.tn.skip(";");
+                            if (!weak) // import ignored
+                                topLevel["imports"].push(token);
+                            break;
+                        case 'syntax':
+                            if (!head)
+                                throw Error("unexpected 'syntax'");
+                            this.tn.skip("=");
+                            if ((topLevel["syntax"] = this._readString()) === "proto3")
+                                this.proto3 = true;
+                            this.tn.skip(";");
+                            break;
+                        case 'message':
+                            this._parseMessage(topLevel, null);
+                            head = false;
+                            break;
+                        case 'enum':
+                            this._parseEnum(topLevel);
+                            head = false;
+                            break;
+                        case 'option':
+                            this._parseOption(topLevel);
+                            break;
+                        case 'service':
+                            this._parseService(topLevel);
+                            break;
+                        case 'extend':
+                            this._parseExtend(topLevel);
+                            break;
+                        default:
+                            throw Error("unexpected '" + token + "'");
+                    }
+                }
+            } catch (e) {
+                e.message = "Parse error at line "+this.tn.line+": " + e.message;
+                throw e;
+            }
+            delete topLevel["name"];
+            return topLevel;
+        };
+
+        /**
+         * Parses the specified source.
+         * @returns {!Object}
+         * @throws {Error} If the source cannot be parsed
+         * @expose
+         */
+        Parser.parse = function(source) {
+            return new Parser(source).parse();
+        };
+
+        // ----- Conversion ------
+
+        /**
+         * Converts a numerical string to an id.
+         * @param {string} value
+         * @param {boolean=} mayBeNegative
+         * @returns {number}
+         * @inner
+         */
+        function mkId(value, mayBeNegative) {
+            var id = -1,
+                sign = 1;
+            if (value.charAt(0) == '-') {
+                sign = -1;
+                value = value.substring(1);
+            }
+            if (Lang.NUMBER_DEC.test(value))
+                id = parseInt(value);
+            else if (Lang.NUMBER_HEX.test(value))
+                id = parseInt(value.substring(2), 16);
+            else if (Lang.NUMBER_OCT.test(value))
+                id = parseInt(value.substring(1), 8);
+            else
+                throw Error("illegal id value: " + (sign < 0 ? '-' : '') + value);
+            id = (sign*id)|0; // Force to 32bit
+            if (!mayBeNegative && id < 0)
+                throw Error("illegal id value: " + (sign < 0 ? '-' : '') + value);
+            return id;
+        }
+
+        /**
+         * Converts a numerical string to a number.
+         * @param {string} val
+         * @returns {number}
+         * @inner
+         */
+        function mkNumber(val) {
+            var sign = 1;
+            if (val.charAt(0) == '-') {
+                sign = -1;
+                val = val.substring(1);
+            }
+            if (Lang.NUMBER_DEC.test(val))
+                return sign * parseInt(val, 10);
+            else if (Lang.NUMBER_HEX.test(val))
+                return sign * parseInt(val.substring(2), 16);
+            else if (Lang.NUMBER_OCT.test(val))
+                return sign * parseInt(val.substring(1), 8);
+            else if (val === 'inf')
+                return sign * Infinity;
+            else if (val === 'nan')
+                return NaN;
+            else if (Lang.NUMBER_FLT.test(val))
+                return sign * parseFloat(val);
+            throw Error("illegal number value: " + (sign < 0 ? '-' : '') + val);
+        }
+
+        // ----- Reading ------
+
+        /**
+         * Reads a string.
+         * @returns {string}
+         * @private
+         */
+        ParserPrototype._readString = function() {
+            var value = "",
+                token,
+                delim;
+            do {
+                delim = this.tn.next();
+                if (delim !== "'" && delim !== '"')
+                    throw Error("illegal string delimiter: "+delim);
+                value += this.tn.next();
+                this.tn.skip(delim);
+                token = this.tn.peek();
+            } while (token === '"' || token === '"'); // multi line?
+            return value;
+        };
+
+        /**
+         * Reads a value.
+         * @param {boolean=} mayBeTypeRef
+         * @returns {number|boolean|string}
+         * @private
+         */
+        ParserPrototype._readValue = function(mayBeTypeRef) {
+            var token = this.tn.peek(),
+                value;
+            if (token === '"' || token === "'")
+                return this._readString();
+            this.tn.next();
+            if (Lang.NUMBER.test(token))
+                return mkNumber(token);
+            if (Lang.BOOL.test(token))
+                return (token.toLowerCase() === 'true');
+            if (mayBeTypeRef && Lang.TYPEREF.test(token))
+                return token;
+            throw Error("illegal value: "+token);
+
+        };
+
+        // ----- Parsing constructs -----
+
+        /**
+         * Parses a namespace option.
+         * @param {!Object} parent Parent definition
+         * @param {boolean=} isList
+         * @private
+         */
+        ParserPrototype._parseOption = function(parent, isList) {
+            var token = this.tn.next(),
+                custom = false;
+            if (token === '(') {
+                custom = true;
+                token = this.tn.next();
+            }
+            if (!Lang.TYPEREF.test(token))
+                // we can allow options of the form google.protobuf.* since they will just get ignored anyways
+                // if (!/google\.protobuf\./.test(token)) // FIXME: Why should that not be a valid typeref?
+                    throw Error("illegal option name: "+token);
+            var name = token;
+            if (custom) { // (my_method_option).foo, (my_method_option), some_method_option, (foo.my_option).bar
+                this.tn.skip(')');
+                name = '('+name+')';
+                token = this.tn.peek();
+                if (Lang.FQTYPEREF.test(token)) {
+                    name += token;
+                    this.tn.next();
+                }
+            }
+            this.tn.skip('=');
+            this._parseOptionValue(parent, name);
+            if (!isList)
+                this.tn.skip(";");
+        };
+
+        /**
+         * Sets an option on the specified options object.
+         * @param {!Object.<string,*>} options
+         * @param {string} name
+         * @param {string|number|boolean} value
+         * @inner
+         */
+        function setOption(options, name, value) {
+            if (typeof options[name] === 'undefined')
+                options[name] = value;
+            else {
+                if (!Array.isArray(options[name]))
+                    options[name] = [ options[name] ];
+                options[name].push(value);
+            }
+        }
+
+        /**
+         * Parses an option value.
+         * @param {!Object} parent
+         * @param {string} name
+         * @private
+         */
+        ParserPrototype._parseOptionValue = function(parent, name) {
+            var token = this.tn.peek();
+            if (token !== '{') { // Plain value
+                setOption(parent["options"], name, this._readValue(true));
+            } else { // Aggregate options
+                this.tn.skip("{");
+                while ((token = this.tn.next()) !== '}') {
+                    if (!Lang.NAME.test(token))
+                        throw Error("illegal option name: " + name + "." + token);
+                    if (this.tn.omit(":"))
+                        setOption(parent["options"], name + "." + token, this._readValue(true));
+                    else
+                        this._parseOptionValue(parent, name + "." + token);
+                }
+            }
+        };
+
+        /**
+         * Parses a service definition.
+         * @param {!Object} parent Parent definition
+         * @private
+         */
+        ParserPrototype._parseService = function(parent) {
+            var token = this.tn.next();
+            if (!Lang.NAME.test(token))
+                throw Error("illegal service name at line "+this.tn.line+": "+token);
+            var name = token;
+            var svc = {
+                "name": name,
+                "rpc": {},
+                "options": {}
+            };
+            this.tn.skip("{");
+            while ((token = this.tn.next()) !== '}') {
+                if (token === "option")
+                    this._parseOption(svc);
+                else if (token === 'rpc')
+                    this._parseServiceRPC(svc);
+                else
+                    throw Error("illegal service token: "+token);
+            }
+            this.tn.omit(";");
+            parent["services"].push(svc);
+        };
+
+        /**
+         * Parses a RPC service definition of the form ['rpc', name, (request), 'returns', (response)].
+         * @param {!Object} svc Service definition
+         * @private
+         */
+        ParserPrototype._parseServiceRPC = function(svc) {
+            var type = "rpc",
+                token = this.tn.next();
+            if (!Lang.NAME.test(token))
+                throw Error("illegal rpc service method name: "+token);
+            var name = token;
+            var method = {
+                "request": null,
+                "response": null,
+                "request_stream": false,
+                "response_stream": false,
+                "options": {}
+            };
+            this.tn.skip("(");
+            token = this.tn.next();
+            if (token.toLowerCase() === "stream") {
+              method["request_stream"] = true;
+              token = this.tn.next();
+            }
+            if (!Lang.TYPEREF.test(token))
+                throw Error("illegal rpc service request type: "+token);
+            method["request"] = token;
+            this.tn.skip(")");
+            token = this.tn.next();
+            if (token.toLowerCase() !== "returns")
+                throw Error("illegal rpc service request type delimiter: "+token);
+            this.tn.skip("(");
+            token = this.tn.next();
+            if (token.toLowerCase() === "stream") {
+              method["response_stream"] = true;
+              token = this.tn.next();
+            }
+            method["response"] = token;
+            this.tn.skip(")");
+            token = this.tn.peek();
+            if (token === '{') {
+                this.tn.next();
+                while ((token = this.tn.next()) !== '}') {
+                    if (token === 'option')
+                        this._parseOption(method);
+                    else
+                        throw Error("illegal rpc service token: " + token);
+                }
+                this.tn.omit(";");
+            } else
+                this.tn.skip(";");
+            if (typeof svc[type] === 'undefined')
+                svc[type] = {};
+            svc[type][name] = method;
+        };
+
+        /**
+         * Parses a message definition.
+         * @param {!Object} parent Parent definition
+         * @param {!Object=} fld Field definition if this is a group
+         * @returns {!Object}
+         * @private
+         */
+        ParserPrototype._parseMessage = function(parent, fld) {
+            var isGroup = !!fld,
+                token = this.tn.next();
+            var msg = {
+                "name": "",
+                "fields": [],
+                "enums": [],
+                "messages": [],
+                "options": {},
+                "services": [],
+                "oneofs": {}
+                // "extensions": undefined
+            };
+            if (!Lang.NAME.test(token))
+                throw Error("illegal "+(isGroup ? "group" : "message")+" name: "+token);
+            msg["name"] = token;
+            if (isGroup) {
+                this.tn.skip("=");
+                fld["id"] = mkId(this.tn.next());
+                msg["isGroup"] = true;
+            }
+            token = this.tn.peek();
+            if (token === '[' && fld)
+                this._parseFieldOptions(fld);
+            this.tn.skip("{");
+            while ((token = this.tn.next()) !== '}') {
+                if (Lang.RULE.test(token))
+                    this._parseMessageField(msg, token);
+                else if (token === "oneof")
+                    this._parseMessageOneOf(msg);
+                else if (token === "enum")
+                    this._parseEnum(msg);
+                else if (token === "message")
+                    this._parseMessage(msg);
+                else if (token === "option")
+                    this._parseOption(msg);
+                else if (token === "service")
+                    this._parseService(msg);
+                else if (token === "extensions")
+                    msg["extensions"] = this._parseExtensionRanges();
+                else if (token === "reserved")
+                    this._parseIgnored(); // TODO
+                else if (token === "extend")
+                    this._parseExtend(msg);
+                else if (Lang.TYPEREF.test(token)) {
+                    if (!this.proto3)
+                        throw Error("illegal field rule: "+token);
+                    this._parseMessageField(msg, "optional", token);
+                } else
+                    throw Error("illegal message token: "+token);
+            }
+            this.tn.omit(";");
+            parent["messages"].push(msg);
+            return msg;
+        };
+
+        /**
+         * Parses an ignored statement.
+         * @private
+         */
+        ParserPrototype._parseIgnored = function() {
+            while (this.tn.peek() !== ';')
+                this.tn.next();
+            this.tn.skip(";");
+        };
+
+        /**
+         * Parses a message field.
+         * @param {!Object} msg Message definition
+         * @param {string} rule Field rule
+         * @param {string=} type Field type if already known (never known for maps)
+         * @returns {!Object} Field descriptor
+         * @private
+         */
+        ParserPrototype._parseMessageField = function(msg, rule, type) {
+            if (!Lang.RULE.test(rule))
+                throw Error("illegal message field rule: "+rule);
+            var fld = {
+                "rule": rule,
+                "type": "",
+                "name": "",
+                "options": {},
+                "id": 0
+            };
+            var token;
+            if (rule === "map") {
+
+                if (type)
+                    throw Error("illegal type: " + type);
+                this.tn.skip('<');
+                token = this.tn.next();
+                if (!Lang.TYPE.test(token) && !Lang.TYPEREF.test(token))
+                    throw Error("illegal message field type: " + token);
+                fld["keytype"] = token;
+                this.tn.skip(',');
+                token = this.tn.next();
+                if (!Lang.TYPE.test(token) && !Lang.TYPEREF.test(token))
+                    throw Error("illegal message field: " + token);
+                fld["type"] = token;
+                this.tn.skip('>');
+                token = this.tn.next();
+                if (!Lang.NAME.test(token))
+                    throw Error("illegal message field name: " + token);
+                fld["name"] = token;
+                this.tn.skip("=");
+                fld["id"] = mkId(this.tn.next());
+                token = this.tn.peek();
+                if (token === '[')
+                    this._parseFieldOptions(fld);
+                this.tn.skip(";");
+
+            } else {
+
+                type = typeof type !== 'undefined' ? type : this.tn.next();
+
+                if (type === "group") {
+
+                    // "A [legacy] group simply combines a nested message type and a field into a single declaration. In your
+                    // code, you can treat this message just as if it had a Result type field called result (the latter name is
+                    // converted to lower-case so that it does not conflict with the former)."
+                    var grp = this._parseMessage(msg, fld);
+                    if (!/^[A-Z]/.test(grp["name"]))
+                        throw Error('illegal group name: '+grp["name"]);
+                    fld["type"] = grp["name"];
+                    fld["name"] = grp["name"].toLowerCase();
+                    this.tn.omit(";");
+
+                } else {
+
+                    if (!Lang.TYPE.test(type) && !Lang.TYPEREF.test(type))
+                        throw Error("illegal message field type: " + type);
+                    fld["type"] = type;
+                    token = this.tn.next();
+                    if (!Lang.NAME.test(token))
+                        throw Error("illegal message field name: " + token);
+                    fld["name"] = token;
+                    this.tn.skip("=");
+                    fld["id"] = mkId(this.tn.next());
+                    token = this.tn.peek();
+                    if (token === "[")
+                        this._parseFieldOptions(fld);
+                    this.tn.skip(";");
+
+                }
+            }
+            msg["fields"].push(fld);
+            return fld;
+        };
+
+        /**
+         * Parses a message oneof.
+         * @param {!Object} msg Message definition
+         * @private
+         */
+        ParserPrototype._parseMessageOneOf = function(msg) {
+            var token = this.tn.next();
+            if (!Lang.NAME.test(token))
+                throw Error("illegal oneof name: "+token);
+            var name = token,
+                fld;
+            var fields = [];
+            this.tn.skip("{");
+            while ((token = this.tn.next()) !== "}") {
+                fld = this._parseMessageField(msg, "optional", token);
+                fld["oneof"] = name;
+                fields.push(fld["id"]);
+            }
+            this.tn.omit(";");
+            msg["oneofs"][name] = fields;
+        };
+
+        /**
+         * Parses a set of field option definitions.
+         * @param {!Object} fld Field definition
+         * @private
+         */
+        ParserPrototype._parseFieldOptions = function(fld) {
+            this.tn.skip("[");
+            var token,
+                first = true;
+            while ((token = this.tn.peek()) !== ']') {
+                if (!first)
+                    this.tn.skip(",");
+                this._parseOption(fld, true);
+                first = false;
+            }
+            this.tn.next();
+        };
+
+        /**
+         * Parses an enum.
+         * @param {!Object} msg Message definition
+         * @private
+         */
+        ParserPrototype._parseEnum = function(msg) {
+            var enm = {
+                "name": "",
+                "values": [],
+                "options": {}
+            };
+            var token = this.tn.next();
+            if (!Lang.NAME.test(token))
+                throw Error("illegal name: "+token);
+            enm["name"] = token;
+            this.tn.skip("{");
+            while ((token = this.tn.next()) !== '}') {
+                if (token === "option")
+                    this._parseOption(enm);
+                else {
+                    if (!Lang.NAME.test(token))
+                        throw Error("illegal name: "+token);
+                    this.tn.skip("=");
+                    var val = {
+                        "name": token,
+                        "id": mkId(this.tn.next(), true)
+                    };
+                    token = this.tn.peek();
+                    if (token === "[")
+                        this._parseFieldOptions({ "options": {} });
+                    this.tn.skip(";");
+                    enm["values"].push(val);
+                }
+            }
+            this.tn.omit(";");
+            msg["enums"].push(enm);
+        };
+
+        /**
+         * Parses extension / reserved ranges.
+         * @returns {!Array.<!Array.<number>>}
+         * @private
+         */
+        ParserPrototype._parseExtensionRanges = function() {
+            var ranges = [];
+            var token,
+                range,
+                value;
+            do {
+                range = [];
+                while (true) {
+                    token = this.tn.next();
+                    switch (token) {
+                        case "min":
+                            value = ProtoBuf.ID_MIN;
+                            break;
+                        case "max":
+                            value = ProtoBuf.ID_MAX;
+                            break;
+                        default:
+                            value = mkNumber(token);
+                            break;
+                    }
+                    range.push(value);
+                    if (range.length === 2)
+                        break;
+                    if (this.tn.peek() !== "to") {
+                        range.push(value);
+                        break;
+                    }
+                    this.tn.next();
+                }
+                ranges.push(range);
+            } while (this.tn.omit(","));
+            this.tn.skip(";");
+            return ranges;
+        };
+
+        /**
+         * Parses an extend block.
+         * @param {!Object} parent Parent object
+         * @private
+         */
+        ParserPrototype._parseExtend = function(parent) {
+            var token = this.tn.next();
+            if (!Lang.TYPEREF.test(token))
+                throw Error("illegal extend reference: "+token);
+            var ext = {
+                "ref": token,
+                "fields": []
+            };
+            this.tn.skip("{");
+            while ((token = this.tn.next()) !== '}') {
+                if (Lang.RULE.test(token))
+                    this._parseMessageField(ext, token);
+                else if (Lang.TYPEREF.test(token)) {
+                    if (!this.proto3)
+                        throw Error("illegal field rule: "+token);
+                    this._parseMessageField(ext, "optional", token);
+                } else
+                    throw Error("illegal extend token: "+token);
+            }
+            this.tn.omit(";");
+            parent["messages"].push(ext);
+            return ext;
+        };
+
+        // ----- General -----
+
+        /**
+         * Returns a string representation of this parser.
+         * @returns {string}
+         */
+        ParserPrototype.toString = function() {
+            return "Parser at line "+this.tn.line;
+        };
+
+        /**
+         * @alias ProtoBuf.DotProto.Parser
+         * @expose
+         */
+        DotProto.Parser = Parser;
+
+        return DotProto;
+
+    })(ProtoBuf, ProtoBuf.Lang);
+
+    /**
+     * @alias ProtoBuf.Reflect
+     * @expose
+     */
+    ProtoBuf.Reflect = (function(ProtoBuf) {
+        "use strict";
+
+        /**
+         * Reflection types.
+         * @exports ProtoBuf.Reflect
+         * @namespace
+         */
+        var Reflect = {};
+
+        /**
+         * Constructs a Reflect base class.
+         * @exports ProtoBuf.Reflect.T
+         * @constructor
+         * @abstract
+         * @param {!ProtoBuf.Builder} builder Builder reference
+         * @param {?ProtoBuf.Reflect.T} parent Parent object
+         * @param {string} name Object name
+         */
+        var T = function(builder, parent, name) {
+
+            /**
+             * Builder reference.
+             * @type {!ProtoBuf.Builder}
+             * @expose
+             */
+            this.builder = builder;
+
+            /**
+             * Parent object.
+             * @type {?ProtoBuf.Reflect.T}
+             * @expose
+             */
+            this.parent = parent;
+
+            /**
+             * Object name in namespace.
+             * @type {string}
+             * @expose
+             */
+            this.name = name;
+
+            /**
+             * Fully qualified class name
+             * @type {string}
+             * @expose
+             */
+            this.className;
+        };
+
+        /**
+         * @alias ProtoBuf.Reflect.T.prototype
+         * @inner
+         */
+        var TPrototype = T.prototype;
+
+        /**
+         * Returns the fully qualified name of this object.
+         * @returns {string} Fully qualified name as of ".PATH.TO.THIS"
+         * @expose
+         */
+        TPrototype.fqn = function() {
+            var name = this.name,
+                ptr = this;
+            do {
+                ptr = ptr.parent;
+                if (ptr == null)
+                    break;
+                name = ptr.name+"."+name;
+            } while (true);
+            return name;
+        };
+
+        /**
+         * Returns a string representation of this Reflect object (its fully qualified name).
+         * @param {boolean=} includeClass Set to true to include the class name. Defaults to false.
+         * @return String representation
+         * @expose
+         */
+        TPrototype.toString = function(includeClass) {
+            return (includeClass ? this.className + " " : "") + this.fqn();
+        };
+
+        /**
+         * Builds this type.
+         * @throws {Error} If this type cannot be built directly
+         * @expose
+         */
+        TPrototype.build = function() {
+            throw Error(this.toString(true)+" cannot be built directly");
+        };
+
+        /**
+         * @alias ProtoBuf.Reflect.T
+         * @expose
+         */
+        Reflect.T = T;
+
+        /**
+         * Constructs a new Namespace.
+         * @exports ProtoBuf.Reflect.Namespace
+         * @param {!ProtoBuf.Builder} builder Builder reference
+         * @param {?ProtoBuf.Reflect.Namespace} parent Namespace parent
+         * @param {string} name Namespace name
+         * @param {Object.<string,*>=} options Namespace options
+         * @param {string?} syntax The syntax level of this definition (e.g., proto3)
+         * @constructor
+         * @extends ProtoBuf.Reflect.T
+         */
+        var Namespace = function(builder, parent, name, options, syntax) {
+            T.call(this, builder, parent, name);
+
+            /**
+             * @override
+             */
+            this.className = "Namespace";
+
+            /**
+             * Children inside the namespace.
+             * @type {!Array.<ProtoBuf.Reflect.T>}
+             */
+            this.children = [];
+
+            /**
+             * Options.
+             * @type {!Object.<string, *>}
+             */
+            this.options = options || {};
+
+            /**
+             * Syntax level (e.g., proto2 or proto3).
+             * @type {!string}
+             */
+            this.syntax = syntax || "proto2";
+        };
+
+        /**
+         * @alias ProtoBuf.Reflect.Namespace.prototype
+         * @inner
+         */
+        var NamespacePrototype = Namespace.prototype = Object.create(T.prototype);
+
+        /**
+         * Returns an array of the namespace's children.
+         * @param {ProtoBuf.Reflect.T=} type Filter type (returns instances of this type only). Defaults to null (all children).
+         * @return {Array.<ProtoBuf.Reflect.T>}
+         * @expose
+         */
+        NamespacePrototype.getChildren = function(type) {
+            type = type || null;
+            if (type == null)
+                return this.children.slice();
+            var children = [];
+            for (var i=0, k=this.children.length; i<k; ++i)
+                if (this.children[i] instanceof type)
+                    children.push(this.children[i]);
+            return children;
+        };
+
+        /**
+         * Adds a child to the namespace.
+         * @param {ProtoBuf.Reflect.T} child Child
+         * @throws {Error} If the child cannot be added (duplicate)
+         * @expose
+         */
+        NamespacePrototype.addChild = function(child) {
+            var other;
+            if (other = this.getChild(child.name)) {
+                // Try to revert camelcase transformation on collision
+                if (other instanceof Message.Field && other.name !== other.originalName && this.getChild(other.originalName) === null)
+                    other.name = other.originalName; // Revert previous first (effectively keeps both originals)
+                else if (child instanceof Message.Field && child.name !== child.originalName && this.getChild(child.originalName) === null)
+                    child.name = child.originalName;
+                else
+                    throw Error("Duplicate name in namespace "+this.toString(true)+": "+child.name);
+            }
+            this.children.push(child);
+        };
+
+        /**
+         * Gets a child by its name or id.
+         * @param {string|number} nameOrId Child name or id
+         * @return {?ProtoBuf.Reflect.T} The child or null if not found
+         * @expose
+         */
+        NamespacePrototype.getChild = function(nameOrId) {
+            var key = typeof nameOrId === 'number' ? 'id' : 'name';
+            for (var i=0, k=this.children.length; i<k; ++i)
+                if (this.children[i][key] === nameOrId)
+                    return this.children[i];
+            return null;
+        };
+
+        /**
+         * Resolves a reflect object inside of this namespace.
+         * @param {string|!Array.<string>} qn Qualified name to resolve
+         * @param {boolean=} excludeNonNamespace Excludes non-namespace types, defaults to `false`
+         * @return {?ProtoBuf.Reflect.Namespace} The resolved type or null if not found
+         * @expose
+         */
+        NamespacePrototype.resolve = function(qn, excludeNonNamespace) {
+            var part = typeof qn === 'string' ? qn.split(".") : qn,
+                ptr = this,
+                i = 0;
+            if (part[i] === "") { // Fully qualified name, e.g. ".My.Message'
+                while (ptr.parent !== null)
+                    ptr = ptr.parent;
+                i++;
+            }
+            var child;
+            do {
+                do {
+                    if (!(ptr instanceof Reflect.Namespace)) {
+                        ptr = null;
+                        break;
+                    }
+                    child = ptr.getChild(part[i]);
+                    if (!child || !(child instanceof Reflect.T) || (excludeNonNamespace && !(child instanceof Reflect.Namespace))) {
+                        ptr = null;
+                        break;
+                    }
+                    ptr = child; i++;
+                } while (i < part.length);
+                if (ptr != null)
+                    break; // Found
+                // Else search the parent
+                if (this.parent !== null)
+                    return this.parent.resolve(qn, excludeNonNamespace);
+            } while (ptr != null);
+            return ptr;
+        };
+
+        /**
+         * Determines the shortest qualified name of the specified type, if any, relative to this namespace.
+         * @param {!ProtoBuf.Reflect.T} t Reflection type
+         * @returns {string} The shortest qualified name or, if there is none, the fqn
+         * @expose
+         */
+        NamespacePrototype.qn = function(t) {
+            var part = [], ptr = t;
+            do {
+                part.unshift(ptr.name);
+                ptr = ptr.parent;
+            } while (ptr !== null);
+            for (var len=1; len <= part.length; len++) {
+                var qn = part.slice(part.length-len);
+                if (t === this.resolve(qn, t instanceof Reflect.Namespace))
+                    return qn.join(".");
+            }
+            return t.fqn();
+        };
+
+        /**
+         * Builds the namespace and returns the runtime counterpart.
+         * @return {Object.<string,Function|Object>} Runtime namespace
+         * @expose
+         */
+        NamespacePrototype.build = function() {
+            /** @dict */
+            var ns = {};
+            var children = this.children;
+            for (var i=0, k=children.length, child; i<k; ++i) {
+                child = children[i];
+                if (child instanceof Namespace)
+                    ns[child.name] = child.build();
+            }
+            if (Object.defineProperty)
+                Object.defineProperty(ns, "$options", { "value": this.buildOpt() });
+            return ns;
+        };
+
+        /**
+         * Builds the namespace's '$options' property.
+         * @return {Object.<string,*>}
+         */
+        NamespacePrototype.buildOpt = function() {
+            var opt = {},
+                keys = Object.keys(this.options);
+            for (var i=0, k=keys.length; i<k; ++i) {
+                var key = keys[i],
+                    val = this.options[keys[i]];
+                // TODO: Options are not resolved, yet.
+                // if (val instanceof Namespace) {
+                //     opt[key] = val.build();
+                // } else {
+                opt[key] = val;
+                // }
+            }
+            return opt;
+        };
+
+        /**
+         * Gets the value assigned to the option with the specified name.
+         * @param {string=} name Returns the option value if specified, otherwise all options are returned.
+         * @return {*|Object.<string,*>}null} Option value or NULL if there is no such option
+         */
+        NamespacePrototype.getOption = function(name) {
+            if (typeof name === 'undefined')
+                return this.options;
+            return typeof this.options[name] !== 'undefined' ? this.options[name] : null;
+        };
+
+        /**
+         * @alias ProtoBuf.Reflect.Namespace
+         * @expose
+         */
+        Reflect.Namespace = Namespace;
+
+        /**
+         * Constructs a new Element implementation that checks and converts values for a
+         * particular field type, as appropriate.
+         *
+         * An Element represents a single value: either the value of a singular field,
+         * or a value contained in one entry of a repeated field or map field. This
+         * class does not implement these higher-level concepts; it only encapsulates
+         * the low-level typechecking and conversion.
+         *
+         * @exports ProtoBuf.Reflect.Element
+         * @param {{name: string, wireType: number}} type Resolved data type
+         * @param {ProtoBuf.Reflect.T|null} resolvedType Resolved type, if relevant
+         * (e.g. submessage field).
+         * @param {boolean} isMapKey Is this element a Map key? The value will be
+         * converted to string form if so.
+         * @param {string} syntax Syntax level of defining message type, e.g.,
+         * proto2 or proto3.
+         * @constructor
+         */
+        var Element = function(type, resolvedType, isMapKey, syntax) {
+
+            /**
+             * Element type, as a string (e.g., int32).
+             * @type {{name: string, wireType: number}}
+             */
+            this.type = type;
+
+            /**
+             * Element type reference to submessage or enum definition, if needed.
+             * @type {ProtoBuf.Reflect.T|null}
+             */
+            this.resolvedType = resolvedType;
+
+            /**
+             * Element is a map key.
+             * @type {boolean}
+             */
+            this.isMapKey = isMapKey;
+
+            /**
+             * Syntax level of defining message type, e.g., proto2 or proto3.
+             * @type {string}
+             */
+            this.syntax = syntax;
+
+            if (isMapKey && ProtoBuf.MAP_KEY_TYPES.indexOf(type) < 0)
+                throw Error("Invalid map key type: " + type.name);
+        };
+
+        var ElementPrototype = Element.prototype;
+
+        /**
+         * Obtains a (new) default value for the specified type.
+         * @param type {string|{name: string, wireType: number}} Field type
+         * @returns {*} Default value
+         * @inner
+         */
+        function mkDefault(type) {
+            if (typeof type === 'string')
+                type = ProtoBuf.TYPES[type];
+            if (typeof type.defaultValue === 'undefined')
+                throw Error("default value for type "+type.name+" is not supported");
+            if (type == ProtoBuf.TYPES["bytes"])
+                return new ByteBuffer(0);
+            return type.defaultValue;
+        }
+
+        /**
+         * Returns the default value for this field in proto3.
+         * @function
+         * @param type {string|{name: string, wireType: number}} the field type
+         * @returns {*} Default value
+         */
+        Element.defaultFieldValue = mkDefault;
+
+        /**
+         * Makes a Long from a value.
+         * @param {{low: number, high: number, unsigned: boolean}|string|number} value Value
+         * @param {boolean=} unsigned Whether unsigned or not, defaults to reuse it from Long-like objects or to signed for
+         *  strings and numbers
+         * @returns {!Long}
+         * @throws {Error} If the value cannot be converted to a Long
+         * @inner
+         */
+        function mkLong(value, unsigned) {
+            if (value && typeof value.low === 'number' && typeof value.high === 'number' && typeof value.unsigned === 'boolean'
+                && value.low === value.low && value.high === value.high)
+                return new ProtoBuf.Long(value.low, value.high, typeof unsigned === 'undefined' ? value.unsigned : unsigned);
+            if (typeof value === 'string')
+                return ProtoBuf.Long.fromString(value, unsigned || false, 10);
+            if (typeof value === 'number')
+                return ProtoBuf.Long.fromNumber(value, unsigned || false);
+            throw Error("not convertible to Long");
+        }
+
+        /**
+         * Checks if the given value can be set for an element of this type (singular
+         * field or one element of a repeated field or map).
+         * @param {*} value Value to check
+         * @return {*} Verified, maybe adjusted, value
+         * @throws {Error} If the value cannot be verified for this element slot
+         * @expose
+         */
+        ElementPrototype.verifyValue = function(value) {
+            var self = this;
+            function fail(val, msg) {
+                throw Error("Illegal value for "+self.toString(true)+" of type "+self.type.name+": "+val+" ("+msg+")");
+            }
+            switch (this.type) {
+                // Signed 32bit
+                case ProtoBuf.TYPES["int32"]:
+                case ProtoBuf.TYPES["sint32"]:
+                case ProtoBuf.TYPES["sfixed32"]:
+                    // Account for !NaN: value === value
+                    if (typeof value !== 'number' || (value === value && value % 1 !== 0))
+                        fail(typeof value, "not an integer");
+                    return value > 4294967295 ? value | 0 : value;
+
+                // Unsigned 32bit
+                case ProtoBuf.TYPES["uint32"]:
+                case ProtoBuf.TYPES["fixed32"]:
+                    if (typeof value !== 'number' || (value === value && value % 1 !== 0))
+                        fail(typeof value, "not an integer");
+                    return value < 0 ? value >>> 0 : value;
+
+                // Signed 64bit
+                case ProtoBuf.TYPES["int64"]:
+                case ProtoBuf.TYPES["sint64"]:
+                case ProtoBuf.TYPES["sfixed64"]: {
+                    if (ProtoBuf.Long)
+                        try {
+                            return mkLong(value, false);
+                        } catch (e) {
+                            fail(typeof value, e.message);
+                        }
+                    else
+                        fail(typeof value, "requires Long.js");
+                }
+
+                // Unsigned 64bit
+                case ProtoBuf.TYPES["uint64"]:
+                case ProtoBuf.TYPES["fixed64"]: {
+                    if (ProtoBuf.Long)
+                        try {
+                            return mkLong(value, true);
+                        } catch (e) {
+                            fail(typeof value, e.message);
+                        }
+                    else
+                        fail(typeof value, "requires Long.js");
+                }
+
+                // Bool
+                case ProtoBuf.TYPES["bool"]:
+                    if (typeof value !== 'boolean')
+                        fail(typeof value, "not a boolean");
+                    return value;
+
+                // Float
+                case ProtoBuf.TYPES["float"]:
+                case ProtoBuf.TYPES["double"]:
+                    if (typeof value !== 'number')
+                        fail(typeof value, "not a number");
+                    return value;
+
+                // Length-delimited string
+                case ProtoBuf.TYPES["string"]:
+                    if (typeof value !== 'string' && !(value && value instanceof String))
+                        fail(typeof value, "not a string");
+                    return ""+value; // Convert String object to string
+
+                // Length-delimited bytes
+                case ProtoBuf.TYPES["bytes"]:
+                    if (ByteBuffer.isByteBuffer(value))
+                        return value;
+                    return ByteBuffer.wrap(value, "base64");
+
+                // Constant enum value
+                case ProtoBuf.TYPES["enum"]: {
+                    var values = this.resolvedType.getChildren(ProtoBuf.Reflect.Enum.Value);
+                    for (i=0; i<values.length; i++)
+                        if (values[i].name == value)
+                            return values[i].id;
+                        else if (values[i].id == value)
+                            return values[i].id;
+
+                    if (this.syntax === 'proto3') {
+                        // proto3: just make sure it's an integer.
+                        if (typeof value !== 'number' || (value === value && value % 1 !== 0))
+                            fail(typeof value, "not an integer");
+                        if (value > 4294967295 || value < 0)
+                            fail(typeof value, "not in range for uint32")
+                        return value;
+                    } else {
+                        // proto2 requires enum values to be valid.
+                        fail(value, "not a valid enum value");
+                    }
+                }
+                // Embedded message
+                case ProtoBuf.TYPES["group"]:
+                case ProtoBuf.TYPES["message"]: {
+                    if (!value || typeof value !== 'object')
+                        fail(typeof value, "object expected");
+                    if (value instanceof this.resolvedType.clazz)
+                        return value;
+                    if (value instanceof ProtoBuf.Builder.Message) {
+                        // Mismatched type: Convert to object (see: https://github.com/dcodeIO/ProtoBuf.js/issues/180)
+                        var obj = {};
+                        for (var i in value)
+                            if (value.hasOwnProperty(i))
+                                obj[i] = value[i];
+                        value = obj;
+                    }
+                    // Else let's try to construct one from a key-value object
+                    return new (this.resolvedType.clazz)(value); // May throw for a hundred of reasons
+                }
+            }
+
+            // We should never end here
+            throw Error("[INTERNAL] Illegal value for "+this.toString(true)+": "+value+" (undefined type "+this.type+")");
+        };
+
+        /**
+         * Calculates the byte length of an element on the wire.
+         * @param {number} id Field number
+         * @param {*} value Field value
+         * @returns {number} Byte length
+         * @throws {Error} If the value cannot be calculated
+         * @expose
+         */
+        ElementPrototype.calculateLength = function(id, value) {
+            if (value === null) return 0; // Nothing to encode
+            // Tag has already been written
+            var n;
+            switch (this.type) {
+                case ProtoBuf.TYPES["int32"]:
+                    return value < 0 ? ByteBuffer.calculateVarint64(value) : ByteBuffer.calculateVarint32(value);
+                case ProtoBuf.TYPES["uint32"]:
+                    return ByteBuffer.calculateVarint32(value);
+                case ProtoBuf.TYPES["sint32"]:
+                    return ByteBuffer.calculateVarint32(ByteBuffer.zigZagEncode32(value));
+                case ProtoBuf.TYPES["fixed32"]:
+                case ProtoBuf.TYPES["sfixed32"]:
+                case ProtoBuf.TYPES["float"]:
+                    return 4;
+                case ProtoBuf.TYPES["int64"]:
+                case ProtoBuf.TYPES["uint64"]:
+                    return ByteBuffer.calculateVarint64(value);
+                case ProtoBuf.TYPES["sint64"]:
+                    return ByteBuffer.calculateVarint64(ByteBuffer.zigZagEncode64(value));
+                case ProtoBuf.TYPES["fixed64"]:
+                case ProtoBuf.TYPES["sfixed64"]:
+                    return 8;
+                case ProtoBuf.TYPES["bool"]:
+                    return 1;
+                case ProtoBuf.TYPES["enum"]:
+                    return ByteBuffer.calculateVarint32(value);
+                case ProtoBuf.TYPES["double"]:
+                    return 8;
+                case ProtoBuf.TYPES["string"]:
+                    n = ByteBuffer.calculateUTF8Bytes(value);
+                    return ByteBuffer.calculateVarint32(n) + n;
+                case ProtoBuf.TYPES["bytes"]:
+                    if (value.remaining() < 0)
+                        throw Error("Illegal value for "+this.toString(true)+": "+value.remaining()+" bytes remaining");
+                    return ByteBuffer.calculateVarint32(value.remaining()) + value.remaining();
+                case ProtoBuf.TYPES["message"]:
+                    n = this.resolvedType.calculate(value);
+                    return ByteBuffer.calculateVarint32(n) + n;
+                case ProtoBuf.TYPES["group"]:
+                    n = this.resolvedType.calculate(value);
+                    return n + ByteBuffer.calculateVarint32((id << 3) | ProtoBuf.WIRE_TYPES.ENDGROUP);
+            }
+            // We should never end here
+            throw Error("[INTERNAL] Illegal value to encode in "+this.toString(true)+": "+value+" (unknown type)");
+        };
+
+        /**
+         * Encodes a value to the specified buffer. Does not encode the key.
+         * @param {number} id Field number
+         * @param {*} value Field value
+         * @param {ByteBuffer} buffer ByteBuffer to encode to
+         * @return {ByteBuffer} The ByteBuffer for chaining
+         * @throws {Error} If the value cannot be encoded
+         * @expose
+         */
+        ElementPrototype.encodeValue = function(id, value, buffer) {
+            if (value === null) return buffer; // Nothing to encode
+            // Tag has already been written
+
+            switch (this.type) {
+                // 32bit signed varint
+                case ProtoBuf.TYPES["int32"]:
+                    // "If you use int32 or int64 as the type for a negative number, the resulting varint is always ten bytes
+                    // long – it is, effectively, treated like a very large unsigned integer." (see #122)
+                    if (value < 0)
+                        buffer.writeVarint64(value);
+                    else
+                        buffer.writeVarint32(value);
+                    break;
+
+                // 32bit unsigned varint
+                case ProtoBuf.TYPES["uint32"]:
+                    buffer.writeVarint32(value);
+                    break;
+
+                // 32bit varint zig-zag
+                case ProtoBuf.TYPES["sint32"]:
+                    buffer.writeVarint32ZigZag(value);
+                    break;
+
+                // Fixed unsigned 32bit
+                case ProtoBuf.TYPES["fixed32"]:
+                    buffer.writeUint32(value);
+                    break;
+
+                // Fixed signed 32bit
+                case ProtoBuf.TYPES["sfixed32"]:
+                    buffer.writeInt32(value);
+                    break;
+
+                // 64bit varint as-is
+                case ProtoBuf.TYPES["int64"]:
+                case ProtoBuf.TYPES["uint64"]:
+                    buffer.writeVarint64(value); // throws
+                    break;
+
+                // 64bit varint zig-zag
+                case ProtoBuf.TYPES["sint64"]:
+                    buffer.writeVarint64ZigZag(value); // throws
+                    break;
+
+                // Fixed unsigned 64bit
+                case ProtoBuf.TYPES["fixed64"]:
+                    buffer.writeUint64(value); // throws
+                    break;
+
+                // Fixed signed 64bit
+                case ProtoBuf.TYPES["sfixed64"]:
+                    buffer.writeInt64(value); // throws
+                    break;
+
+                // Bool
+                case ProtoBuf.TYPES["bool"]:
+                    if (typeof value === 'string')
+                        buffer.writeVarint32(value.toLowerCase() === 'false' ? 0 : !!value);
+                    else
+                        buffer.writeVarint32(value ? 1 : 0);
+                    break;
+
+                // Constant enum value
+                case ProtoBuf.TYPES["enum"]:
+                    buffer.writeVarint32(value);
+                    break;
+
+                // 32bit float
+                case ProtoBuf.TYPES["float"]:
+                    buffer.writeFloat32(value);
+                    break;
+
+                // 64bit float
+                case ProtoBuf.TYPES["double"]:
+                    buffer.writeFloat64(value);
+                    break;
+
+                // Length-delimited string
+                case ProtoBuf.TYPES["string"]:
+                    buffer.writeVString(value);
+                    break;
+
+                // Length-delimited bytes
+                case ProtoBuf.TYPES["bytes"]:
+                    if (value.remaining() < 0)
+                        throw Error("Illegal value for "+this.toString(true)+": "+value.remaining()+" bytes remaining");
+                    var prevOffset = value.offset;
+                    buffer.writeVarint32(value.remaining());
+                    buffer.append(value);
+                    value.offset = prevOffset;
+                    break;
+
+                // Embedded message
+                case ProtoBuf.TYPES["message"]:
+                    var bb = new ByteBuffer().LE();
+                    this.resolvedType.encode(value, bb);
+                    buffer.writeVarint32(bb.offset);
+                    buffer.append(bb.flip());
+                    break;
+
+                // Legacy group
+                case ProtoBuf.TYPES["group"]:
+                    this.resolvedType.encode(value, buffer);
+                    buffer.writeVarint32((id << 3) | ProtoBuf.WIRE_TYPES.ENDGROUP);
+                    break;
+
+                default:
+                    // We should never end here
+                    throw Error("[INTERNAL] Illegal value to encode in "+this.toString(true)+": "+value+" (unknown type)");
+            }
+            return buffer;
+        };
+
+        /**
+         * Decode one element value from the specified buffer.
+         * @param {ByteBuffer} buffer ByteBuffer to decode from
+         * @param {number} wireType The field wire type
+         * @param {number} id The field number
+         * @return {*} Decoded value
+         * @throws {Error} If the field cannot be decoded
+         * @expose
+         */
+        ElementPrototype.decode = function(buffer, wireType, id) {
+            if (wireType != this.type.wireType)
+                throw Error("Unexpected wire type for element");
+
+            var value, nBytes;
+            switch (this.type) {
+                // 32bit signed varint
+                case ProtoBuf.TYPES["int32"]:
+                    return buffer.readVarint32() | 0;
+
+                // 32bit unsigned varint
+                case ProtoBuf.TYPES["uint32"]:
+                    return buffer.readVarint32() >>> 0;
+
+                // 32bit signed varint zig-zag
+                case ProtoBuf.TYPES["sint32"]:
+                    return buffer.readVarint32ZigZag() | 0;
+
+                // Fixed 32bit unsigned
+                case ProtoBuf.TYPES["fixed32"]:
+                    return buffer.readUint32() >>> 0;
+
+                case ProtoBuf.TYPES["sfixed32"]:
+                    return buffer.readInt32() | 0;
+
+                // 64bit signed varint
+                case ProtoBuf.TYPES["int64"]:
+                    return buffer.readVarint64();
+
+                // 64bit unsigned varint
+                case ProtoBuf.TYPES["uint64"]:
+                    return buffer.readVarint64().toUnsigned();
+
+                // 64bit signed varint zig-zag
+                case ProtoBuf.TYPES["sint64"]:
+                    return buffer.readVarint64ZigZag();
+
+                // Fixed 64bit unsigned
+                case ProtoBuf.TYPES["fixed64"]:
+                    return buffer.readUint64();
+
+                // Fixed 64bit signed
+                case ProtoBuf.TYPES["sfixed64"]:
+                    return buffer.readInt64();
+
+                // Bool varint
+                case ProtoBuf.TYPES["bool"]:
+                    return !!buffer.readVarint32();
+
+                // Constant enum value (varint)
+                case ProtoBuf.TYPES["enum"]:
+                    // The following Builder.Message#set will already throw
+                    return buffer.readVarint32();
+
+                // 32bit float
+                case ProtoBuf.TYPES["float"]:
+                    return buffer.readFloat();
+
+                // 64bit float
+                case ProtoBuf.TYPES["double"]:
+                    return buffer.readDouble();
+
+                // Length-delimited string
+                case ProtoBuf.TYPES["string"]:
+                    return buffer.readVString();
+
+                // Length-delimited bytes
+                case ProtoBuf.TYPES["bytes"]: {
+                    nBytes = buffer.readVarint32();
+                    if (buffer.remaining() < nBytes)
+                        throw Error("Illegal number of bytes for "+this.toString(true)+": "+nBytes+" required but got only "+buffer.remaining());
+                    value = buffer.clone(); // Offset already set
+                    value.limit = value.offset+nBytes;
+                    buffer.offset += nBytes;
+                    return value;
+                }
+
+                // Length-delimited embedded message
+                case ProtoBuf.TYPES["message"]: {
+                    nBytes = buffer.readVarint32();
+                    return this.resolvedType.decode(buffer, nBytes);
+                }
+
+                // Legacy group
+                case ProtoBuf.TYPES["group"]:
+                    return this.resolvedType.decode(buffer, -1, id);
+            }
+
+            // We should never end here
+            throw Error("[INTERNAL] Illegal decode type");
+        };
+
+        /**
+         * Converts a value from a string to the canonical element type.
+         *
+         * Legal only when isMapKey is true.
+         *
+         * @param {string} str The string value
+         * @returns {*} The value
+         */
+        ElementPrototype.valueFromString = function(str) {
+            if (!this.isMapKey) {
+                throw Error("valueFromString() called on non-map-key element");
+            }
+
+            switch (this.type) {
+                case ProtoBuf.TYPES["int32"]:
+                case ProtoBuf.TYPES["sint32"]:
+                case ProtoBuf.TYPES["sfixed32"]:
+                case ProtoBuf.TYPES["uint32"]:
+                case ProtoBuf.TYPES["fixed32"]:
+                    return this.verifyValue(parseInt(str));
+
+                case ProtoBuf.TYPES["int64"]:
+                case ProtoBuf.TYPES["sint64"]:
+                case ProtoBuf.TYPES["sfixed64"]:
+                case ProtoBuf.TYPES["uint64"]:
+                case ProtoBuf.TYPES["fixed64"]:
+                      // Long-based fields support conversions from string already.
+                      return this.verifyValue(str);
+
+                case ProtoBuf.TYPES["bool"]:
+                      return str === "true";
+
+                case ProtoBuf.TYPES["string"]:
+                      return this.verifyValue(str);
+
+                case ProtoBuf.TYPES["bytes"]:
+                      return ByteBuffer.fromBinary(str);
+            }
+        };
+
+        /**
+         * Converts a value from the canonical element type to a string.
+         *
+         * It should be the case that `valueFromString(valueToString(val))` returns
+         * a value equivalent to `verifyValue(val)` for every legal value of `val`
+         * according to this element type.
+         *
+         * This may be used when the element must be stored or used as a string,
+         * e.g., as a map key on an Object.
+         *
+         * Legal only when isMapKey is true.
+         *
+         * @param {*} val The value
+         * @returns {string} The string form of the value.
+         */
+        ElementPrototype.valueToString = function(value) {
+            if (!this.isMapKey) {
+                throw Error("valueToString() called on non-map-key element");
+            }
+
+            if (this.type === ProtoBuf.TYPES["bytes"]) {
+                return value.toString("binary");
+            } else {
+                return value.toString();
+            }
+        };
+
+        /**
+         * @alias ProtoBuf.Reflect.Element
+         * @expose
+         */
+        Reflect.Element = Element;
+
+        /**
+         * Constructs a new Message.
+         * @exports ProtoBuf.Reflect.Message
+         * @param {!ProtoBuf.Builder} builder Builder reference
+         * @param {!ProtoBuf.Reflect.Namespace} parent Parent message or namespace
+         * @param {string} name Message name
+         * @param {Object.<string,*>=} options Message options
+         * @param {boolean=} isGroup `true` if this is a legacy group
+         * @param {string?} syntax The syntax level of this definition (e.g., proto3)
+         * @constructor
+         * @extends ProtoBuf.Reflect.Namespace
+         */
+        var Message = function(builder, parent, name, options, isGroup, syntax) {
+            Namespace.call(this, builder, parent, name, options, syntax);
+
+            /**
+             * @override
+             */
+            this.className = "Message";
+
+            /**
+             * Extensions range.
+             * @type {!Array.<number>|undefined}
+             * @expose
+             */
+            this.extensions = undefined;
+
+            /**
+             * Runtime message class.
+             * @type {?function(new:ProtoBuf.Builder.Message)}
+             * @expose
+             */
+            this.clazz = null;
+
+            /**
+             * Whether this is a legacy group or not.
+             * @type {boolean}
+             * @expose
+             */
+            this.isGroup = !!isGroup;
+
+            // The following cached collections are used to efficiently iterate over or look up fields when decoding.
+
+            /**
+             * Cached fields.
+             * @type {?Array.<!ProtoBuf.Reflect.Message.Field>}
+             * @private
+             */
+            this._fields = null;
+
+            /**
+             * Cached fields by id.
+             * @type {?Object.<number,!ProtoBuf.Reflect.Message.Field>}
+             * @private
+             */
+            this._fieldsById = null;
+
+            /**
+             * Cached fields by name.
+             * @type {?Object.<string,!ProtoBuf.Reflect.Message.Field>}
+             * @private
+             */
+            this._fieldsByName = null;
+        };
+
+        /**
+         * @alias ProtoBuf.Reflect.Message.prototype
+         * @inner
+         */
+        var MessagePrototype = Message.prototype = Object.create(Namespace.prototype);
+
+        /**
+         * Builds the message and returns the runtime counterpart, which is a fully functional class.
+         * @see ProtoBuf.Builder.Message
+         * @param {boolean=} rebuild Whether to rebuild or not, defaults to false
+         * @return {ProtoBuf.Reflect.Message} Message class
+         * @throws {Error} If the message cannot be built
+         * @expose
+         */
+        MessagePrototype.build = function(rebuild) {
+            if (this.clazz && !rebuild)
+                return this.clazz;
+
+            // Create the runtime Message class in its own scope
+            var clazz = (function(ProtoBuf, T) {
+
+                var fields = T.getChildren(ProtoBuf.Reflect.Message.Field),
+                    oneofs = T.getChildren(ProtoBuf.Reflect.Message.OneOf);
+
+                /**
+                 * Constructs a new runtime Message.
+                 * @name ProtoBuf.Builder.Message
+                 * @class Barebone of all runtime messages.
+                 * @param {!Object.<string,*>|string} values Preset values
+                 * @param {...string} var_args
+                 * @constructor
+                 * @throws {Error} If the message cannot be created
+                 */
+                var Message = function(values, var_args) {
+                    ProtoBuf.Builder.Message.call(this);
+
+                    // Create virtual oneof properties
+                    for (var i=0, k=oneofs.length; i<k; ++i)
+                        this[oneofs[i].name] = null;
+                    // Create fields and set default values
+                    for (i=0, k=fields.length; i<k; ++i) {
+                        var field = fields[i];
+                        this[field.name] =
+                            field.repeated ? [] :
+                            (field.map ? new ProtoBuf.Map(field) : null);
+                        if ((field.required || T.syntax === 'proto3') &&
+                            field.defaultValue !== null)
+                            this[field.name] = field.defaultValue;
+                    }
+
+                    if (arguments.length > 0) {
+                        var value;
+                        // Set field values from a values object
+                        if (arguments.length === 1 && values !== null && typeof values === 'object' &&
+                            /* not _another_ Message */ (typeof values.encode !== 'function' || values instanceof Message) &&
+                            /* not a repeated field */ !Array.isArray(values) &&
+                            /* not a Map */ !(values instanceof ProtoBuf.Map) &&
+                            /* not a ByteBuffer */ !ByteBuffer.isByteBuffer(values) &&
+                            /* not an ArrayBuffer */ !(values instanceof ArrayBuffer) &&
+                            /* not a Long */ !(ProtoBuf.Long && values instanceof ProtoBuf.Long)) {
+                            this.$set(values);
+                        } else // Set field values from arguments, in declaration order
+                            for (i=0, k=arguments.length; i<k; ++i)
+                                if (typeof (value = arguments[i]) !== 'undefined')
+                                    this.$set(fields[i].name, value); // May throw
+                    }
+                };
+
+                /**
+                 * @alias ProtoBuf.Builder.Message.prototype
+                 * @inner
+                 */
+                var MessagePrototype = Message.prototype = Object.create(ProtoBuf.Builder.Message.prototype);
+
+                /**
+                 * Adds a value to a repeated field.
+                 * @name ProtoBuf.Builder.Message#add
+                 * @function
+                 * @param {string} key Field name
+                 * @param {*} value Value to add
+                 * @param {boolean=} noAssert Whether to assert the value or not (asserts by default)
+                 * @returns {!ProtoBuf.Builder.Message} this
+                 * @throws {Error} If the value cannot be added
+                 * @expose
+                 */
+                MessagePrototype.add = function(key, value, noAssert) {
+                    var field = T._fieldsByName[key];
+                    if (!noAssert) {
+                        if (!field)
+                            throw Error(this+"#"+key+" is undefined");
+                        if (!(field instanceof ProtoBuf.Reflect.Message.Field))
+                            throw Error(this+"#"+key+" is not a field: "+field.toString(true)); // May throw if it's an enum or embedded message
+                        if (!field.repeated)
+                            throw Error(this+"#"+key+" is not a repeated field");
+                        value = field.verifyValue(value, true);
+                    }
+                    if (this[key] === null)
+                        this[key] = [];
+                    this[key].push(value);
+                    return this;
+                };
+
+                /**
+                 * Adds a value to a repeated field. This is an alias for {@link ProtoBuf.Builder.Message#add}.
+                 * @name ProtoBuf.Builder.Message#$add
+                 * @function
+                 * @param {string} key Field name
+                 * @param {*} value Value to add
+                 * @param {boolean=} noAssert Whether to assert the value or not (asserts by default)
+                 * @returns {!ProtoBuf.Builder.Message} this
+                 * @throws {Error} If the value cannot be added
+                 * @expose
+                 */
+                MessagePrototype.$add = MessagePrototype.add;
+
+                /**
+                 * Sets a field's value.
+                 * @name ProtoBuf.Builder.Message#set
+                 * @function
+                 * @param {string|!Object.<string,*>} keyOrObj String key or plain object holding multiple values
+                 * @param {(*|boolean)=} value Value to set if key is a string, otherwise omitted
+                 * @param {boolean=} noAssert Whether to not assert for an actual field / proper value type, defaults to `false`
+                 * @returns {!ProtoBuf.Builder.Message} this
+                 * @throws {Error} If the value cannot be set
+                 * @expose
+                 */
+                MessagePrototype.set = function(keyOrObj, value, noAssert) {
+                    if (keyOrObj && typeof keyOrObj === 'object') {
+                        noAssert = value;
+                        for (var ikey in keyOrObj)
+                            if (keyOrObj.hasOwnProperty(ikey) && typeof (value = keyOrObj[ikey]) !== 'undefined')
+                                this.$set(ikey, value, noAssert);
+                        return this;
+                    }
+                    var field = T._fieldsByName[keyOrObj];
+                    if (!noAssert) {
+                        if (!field)
+                            throw Error(this+"#"+keyOrObj+" is not a field: undefined");
+                        if (!(field instanceof ProtoBuf.Reflect.Message.Field))
+                            throw Error(this+"#"+keyOrObj+" is not a field: "+field.toString(true));
+                        this[field.name] = (value = field.verifyValue(value)); // May throw
+                    } else
+                        this[keyOrObj] = value;
+                    if (field && field.oneof) { // Field is part of an OneOf (not a virtual OneOf field)
+                        var currentField = this[field.oneof.name]; // Virtual field references currently set field
+                        if (value !== null) {
+                            if (currentField !== null && currentField !== field.name)
+                                this[currentField] = null; // Clear currently set field
+                            this[field.oneof.name] = field.name; // Point virtual field at this field
+                        } else if (/* value === null && */currentField === keyOrObj)
+                            this[field.oneof.name] = null; // Clear virtual field (current field explicitly cleared)
+                    }
+                    return this;
+                };
+
+                /**
+                 * Sets a field's value. This is an alias for [@link ProtoBuf.Builder.Message#set}.
+                 * @name ProtoBuf.Builder.Message#$set
+                 * @function
+                 * @param {string|!Object.<string,*>} keyOrObj String key or plain object holding multiple values
+                 * @param {(*|boolean)=} value Value to set if key is a string, otherwise omitted
+                 * @param {boolean=} noAssert Whether to not assert the value, defaults to `false`
+                 * @throws {Error} If the value cannot be set
+                 * @expose
+                 */
+                MessagePrototype.$set = MessagePrototype.set;
+
+                /**
+                 * Gets a field's value.
+                 * @name ProtoBuf.Builder.Message#get
+                 * @function
+                 * @param {string} key Key
+                 * @param {boolean=} noAssert Whether to not assert for an actual field, defaults to `false`
+                 * @return {*} Value
+                 * @throws {Error} If there is no such field
+                 * @expose
+                 */
+                MessagePrototype.get = function(key, noAssert) {
+                    if (noAssert)
+                        return this[key];
+                    var field = T._fieldsByName[key];
+                    if (!field || !(field instanceof ProtoBuf.Reflect.Message.Field))
+                        throw Error(this+"#"+key+" is not a field: undefined");
+                    if (!(field instanceof ProtoBuf.Reflect.Message.Field))
+                        throw Error(this+"#"+key+" is not a field: "+field.toString(true));
+                    return this[field.name];
+                };
+
+                /**
+                 * Gets a field's value. This is an alias for {@link ProtoBuf.Builder.Message#$get}.
+                 * @name ProtoBuf.Builder.Message#$get
+                 * @function
+                 * @param {string} key Key
+                 * @return {*} Value
+                 * @throws {Error} If there is no such field
+                 * @expose
+                 */
+                MessagePrototype.$get = MessagePrototype.get;
+
+                // Getters and setters
+
+                for (var i=0; i<fields.length; i++) {
+                    var field = fields[i];
+                    // no setters for extension fields as these are named by their fqn
+                    if (field instanceof ProtoBuf.Reflect.Message.ExtensionField)
+                        continue;
+
+                    if (T.builder.options['populateAccessors'])
+                        (function(field) {
+                            // set/get[SomeValue]
+                            var Name = field.originalName.replace(/(_[a-zA-Z])/g, function(match) {
+                                return match.toUpperCase().replace('_','');
+                            });
+                            Name = Name.substring(0,1).toUpperCase() + Name.substring(1);
+
+                            // set/get_[some_value] FIXME: Do we really need these?
+                            var name = field.originalName.replace(/([A-Z])/g, function(match) {
+                                return "_"+match;
+                            });
+
+                            /**
+                             * The current field's unbound setter function.
+                             * @function
+                             * @param {*} value
+                             * @param {boolean=} noAssert
+                             * @returns {!ProtoBuf.Builder.Message}
+                             * @inner
+                             */
+                            var setter = function(value, noAssert) {
+                                this[field.name] = noAssert ? value : field.verifyValue(value);
+                                return this;
+                            };
+
+                            /**
+                             * The current field's unbound getter function.
+                             * @function
+                             * @returns {*}
+                             * @inner
+                             */
+                            var getter = function() {
+                                return this[field.name];
+                            };
+
+                            if (T.getChild("set"+Name) === null)
+                                /**
+                                 * Sets a value. This method is present for each field, but only if there is no name conflict with
+                                 *  another field.
+                                 * @name ProtoBuf.Builder.Message#set[SomeField]
+                                 * @function
+                                 * @param {*} value Value to set
+                                 * @param {boolean=} noAssert Whether to not assert the value, defaults to `false`
+                                 * @returns {!ProtoBuf.Builder.Message} this
+                                 * @abstract
+                                 * @throws {Error} If the value cannot be set
+                                 */
+                                MessagePrototype["set"+Name] = setter;
+
+                            if (T.getChild("set_"+name) === null)
+                                /**
+                                 * Sets a value. This method is present for each field, but only if there is no name conflict with
+                                 *  another field.
+                                 * @name ProtoBuf.Builder.Message#set_[some_field]
+                                 * @function
+                                 * @param {*} value Value to set
+                                 * @param {boolean=} noAssert Whether to not assert the value, defaults to `false`
+                                 * @returns {!ProtoBuf.Builder.Message} this
+                                 * @abstract
+                                 * @throws {Error} If the value cannot be set
+                                 */
+                                MessagePrototype["set_"+name] = setter;
+
+                            if (T.getChild("get"+Name) === null)
+                                /**
+                                 * Gets a value. This method is present for each field, but only if there is no name conflict with
+                                 *  another field.
+                                 * @name ProtoBuf.Builder.Message#get[SomeField]
+                                 * @function
+                                 * @abstract
+                                 * @return {*} The value
+                                 */
+                                MessagePrototype["get"+Name] = getter;
+
+                            if (T.getChild("get_"+name) === null)
+                                /**
+                                 * Gets a value. This method is present for each field, but only if there is no name conflict with
+                                 *  another field.
+                                 * @name ProtoBuf.Builder.Message#get_[some_field]
+                                 * @function
+                                 * @return {*} The value
+                                 * @abstract
+                                 */
+                                MessagePrototype["get_"+name] = getter;
+
+                        })(field);
+                }
+
+                // En-/decoding
+
+                /**
+                 * Encodes the message.
+                 * @name ProtoBuf.Builder.Message#$encode
+                 * @function
+                 * @param {(!ByteBuffer|boolean)=} buffer ByteBuffer to encode to. Will create a new one and flip it if omitted.
+                 * @param {boolean=} noVerify Whether to not verify field values, defaults to `false`
+                 * @return {!ByteBuffer} Encoded message as a ByteBuffer
+                 * @throws {Error} If the message cannot be encoded or if required fields are missing. The later still
+                 *  returns the encoded ByteBuffer in the `encoded` property on the error.
+                 * @expose
+                 * @see ProtoBuf.Builder.Message#encode64
+                 * @see ProtoBuf.Builder.Message#encodeHex
+                 * @see ProtoBuf.Builder.Message#encodeAB
+                 */
+                MessagePrototype.encode = function(buffer, noVerify) {
+                    if (typeof buffer === 'boolean')
+                        noVerify = buffer,
+                        buffer = undefined;
+                    var isNew = false;
+                    if (!buffer)
+                        buffer = new ByteBuffer(),
+                        isNew = true;
+                    var le = buffer.littleEndian;
+                    try {
+                        T.encode(this, buffer.LE(), noVerify);
+                        return (isNew ? buffer.flip() : buffer).LE(le);
+                    } catch (e) {
+                        buffer.LE(le);
+                        throw(e);
+                    }
+                };
+
+                /**
+                 * Encodes a message using the specified data payload.
+                 * @param {!Object.<string,*>} data Data payload
+                 * @param {(!ByteBuffer|boolean)=} buffer ByteBuffer to encode to. Will create a new one and flip it if omitted.
+                 * @param {boolean=} noVerify Whether to not verify field values, defaults to `false`
+                 * @return {!ByteBuffer} Encoded message as a ByteBuffer
+                 * @expose
+                 */
+                Message.encode = function(data, buffer, noVerify) {
+                    return new Message(data).encode(buffer, noVerify);
+                };
+
+                /**
+                 * Calculates the byte length of the message.
+                 * @name ProtoBuf.Builder.Message#calculate
+                 * @function
+                 * @returns {number} Byte length
+                 * @throws {Error} If the message cannot be calculated or if required fields are missing.
+                 * @expose
+                 */
+                MessagePrototype.calculate = function() {
+                    return T.calculate(this);
+                };
+
+                /**
+                 * Encodes the varint32 length-delimited message.
+                 * @name ProtoBuf.Builder.Message#encodeDelimited
+                 * @function
+                 * @param {(!ByteBuffer|boolean)=} buffer ByteBuffer to encode to. Will create a new one and flip it if omitted.
+                 * @param {boolean=} noVerify Whether to not verify field values, defaults to `false`
+                 * @return {!ByteBuffer} Encoded message as a ByteBuffer
+                 * @throws {Error} If the message cannot be encoded or if required fields are missing. The later still
+                 *  returns the encoded ByteBuffer in the `encoded` property on the error.
+                 * @expose
+                 */
+                MessagePrototype.encodeDelimited = function(buffer, noVerify) {
+                    var isNew = false;
+                    if (!buffer)
+                        buffer = new ByteBuffer(),
+                        isNew = true;
+                    var enc = new ByteBuffer().LE();
+                    T.encode(this, enc, noVerify).flip();
+                    buffer.writeVarint32(enc.remaining());
+                    buffer.append(enc);
+                    return isNew ? buffer.flip() : buffer;
+                };
+
+                /**
+                 * Directly encodes the message to an ArrayBuffer.
+                 * @name ProtoBuf.Builder.Message#encodeAB
+                 * @function
+                 * @return {ArrayBuffer} Encoded message as ArrayBuffer
+                 * @throws {Error} If the message cannot be encoded or if required fields are missing. The later still
+                 *  returns the encoded ArrayBuffer in the `encoded` property on the error.
+                 * @expose
+                 */
+                MessagePrototype.encodeAB = function() {
+                    try {
+                        return this.encode().toArrayBuffer();
+                    } catch (e) {
+                        if (e["encoded"]) e["encoded"] = e["encoded"].toArrayBuffer();
+                        throw(e);
+                    }
+                };
+
+                /**
+                 * Returns the message as an ArrayBuffer. This is an alias for {@link ProtoBuf.Builder.Message#encodeAB}.
+                 * @name ProtoBuf.Builder.Message#toArrayBuffer
+                 * @function
+                 * @return {ArrayBuffer} Encoded message as ArrayBuffer
+                 * @throws {Error} If the message cannot be encoded or if required fields are missing. The later still
+                 *  returns the encoded ArrayBuffer in the `encoded` property on the error.
+                 * @expose
+                 */
+                MessagePrototype.toArrayBuffer = MessagePrototype.encodeAB;
+
+                /**
+                 * Directly encodes the message to a node Buffer.
+                 * @name ProtoBuf.Builder.Message#encodeNB
+                 * @function
+                 * @return {!Buffer}
+                 * @throws {Error} If the message cannot be encoded, not running under node.js or if required fields are
+                 *  missing. The later still returns the encoded node Buffer in the `encoded` property on the error.
+                 * @expose
+                 */
+                MessagePrototype.encodeNB = function() {
+                    try {
+                        return this.encode().toBuffer();
+                    } catch (e) {
+                        if (e["encoded"]) e["encoded"] = e["encoded"].toBuffer();
+                        throw(e);
+                    }
+                };
+
+                /**
+                 * Returns the message as a node Buffer. This is an alias for {@link ProtoBuf.Builder.Message#encodeNB}.
+                 * @name ProtoBuf.Builder.Message#toBuffer
+                 * @function
+                 * @return {!Buffer}
+                 * @throws {Error} If the message cannot be encoded or if required fields are missing. The later still
+                 *  returns the encoded node Buffer in the `encoded` property on the error.
+                 * @expose
+                 */
+                MessagePrototype.toBuffer = MessagePrototype.encodeNB;
+
+                /**
+                 * Directly encodes the message to a base64 encoded string.
+                 * @name ProtoBuf.Builder.Message#encode64
+                 * @function
+                 * @return {string} Base64 encoded string
+                 * @throws {Error} If the underlying buffer cannot be encoded or if required fields are missing. The later
+                 *  still returns the encoded base64 string in the `encoded` property on the error.
+                 * @expose
+                 */
+                MessagePrototype.encode64 = function() {
+                    try {
+                        return this.encode().toBase64();
+                    } catch (e) {
+                        if (e["encoded"]) e["encoded"] = e["encoded"].toBase64();
+                        throw(e);
+                    }
+                };
+
+                /**
+                 * Returns the message as a base64 encoded string. This is an alias for {@link ProtoBuf.Builder.Message#encode64}.
+                 * @name ProtoBuf.Builder.Message#toBase64
+                 * @function
+                 * @return {string} Base64 encoded string
+                 * @throws {Error} If the message cannot be encoded or if required fields are missing. The later still
+                 *  returns the encoded base64 string in the `encoded` property on the error.
+                 * @expose
+                 */
+                MessagePrototype.toBase64 = MessagePrototype.encode64;
+
+                /**
+                 * Directly encodes the message to a hex encoded string.
+                 * @name ProtoBuf.Builder.Message#encodeHex
+                 * @function
+                 * @return {string} Hex encoded string
+                 * @throws {Error} If the underlying buffer cannot be encoded or if required fields are missing. The later
+                 *  still returns the encoded hex string in the `encoded` property on the error.
+                 * @expose
+                 */
+                MessagePrototype.encodeHex = function() {
+                    try {
+                        return this.encode().toHex();
+                    } catch (e) {
+                        if (e["encoded"]) e["encoded"] = e["encoded"].toHex();
+                        throw(e);
+                    }
+                };
+
+                /**
+                 * Returns the message as a hex encoded string. This is an alias for {@link ProtoBuf.Builder.Message#encodeHex}.
+                 * @name ProtoBuf.Builder.Message#toHex
+                 * @function
+                 * @return {string} Hex encoded string
+                 * @throws {Error} If the message cannot be encoded or if required fields are missing. The later still
+                 *  returns the encoded hex string in the `encoded` property on the error.
+                 * @expose
+                 */
+                MessagePrototype.toHex = MessagePrototype.encodeHex;
+
+                /**
+                 * Clones a message object or field value to a raw object.
+                 * @param {*} obj Object to clone
+                 * @param {boolean} binaryAsBase64 Whether to include binary data as base64 strings or as a buffer otherwise
+                 * @param {boolean} longsAsStrings Whether to encode longs as strings
+                 * @param {!ProtoBuf.Reflect.T=} resolvedType The resolved field type if a field
+                 * @returns {*} Cloned object
+                 * @inner
+                 */
+                function cloneRaw(obj, binaryAsBase64, longsAsStrings, resolvedType) {
+                    if (obj === null || typeof obj !== 'object') {
+                        // Convert enum values to their respective names
+                        if (resolvedType && resolvedType instanceof ProtoBuf.Reflect.Enum) {
+                            var name = ProtoBuf.Reflect.Enum.getName(resolvedType.object, obj);
+                            if (name !== null)
+                                return name;
+                        }
+                        // Pass-through string, number, boolean, null...
+                        return obj;
+                    }
+                    // Convert ByteBuffers to raw buffer or strings
+                    if (ByteBuffer.isByteBuffer(obj))
+                        return binaryAsBase64 ? obj.toBase64() : obj.toBuffer();
+                    // Convert Longs to proper objects or strings
+                    if (ProtoBuf.Long.isLong(obj))
+                        return longsAsStrings ? obj.toString() : ProtoBuf.Long.fromValue(obj);
+                    var clone;
+                    // Clone arrays
+                    if (Array.isArray(obj)) {
+                        clone = [];
+                        obj.forEach(function(v, k) {
+                            clone[k] = cloneRaw(v, binaryAsBase64, longsAsStrings, resolvedType);
+                        });
+                        return clone;
+                    }
+                    clone = {};
+                    // Convert maps to objects
+                    if (obj instanceof ProtoBuf.Map) {
+                        var it = obj.entries();
+                        for (var e = it.next(); !e.done; e = it.next())
+                            clone[obj.keyElem.valueToString(e.value[0])] = cloneRaw(e.value[1], binaryAsBase64, longsAsStrings, obj.valueElem.resolvedType);
+                        return clone;
+                    }
+                    // Everything else is a non-null object
+                    var type = obj.$type,
+                        field = undefined;
+                    for (var i in obj)
+                        if (obj.hasOwnProperty(i)) {
+                            if (type && (field = type.getChild(i)))
+                                clone[i] = cloneRaw(obj[i], binaryAsBase64, longsAsStrings, field.resolvedType);
+                            else
+                                clone[i] = cloneRaw(obj[i], binaryAsBase64, longsAsStrings);
+                        }
+                    return clone;
+                }
+
+                /**
+                 * Returns the message's raw payload.
+                 * @param {boolean=} binaryAsBase64 Whether to include binary data as base64 strings instead of Buffers, defaults to `false`
+                 * @param {boolean} longsAsStrings Whether to encode longs as strings
+                 * @returns {Object.<string,*>} Raw payload
+                 * @expose
+                 */
+                MessagePrototype.toRaw = function(binaryAsBase64, longsAsStrings) {
+                    return cloneRaw(this, !!binaryAsBase64, !!longsAsStrings, this.$type);
+                };
+
+                /**
+                 * Encodes a message to JSON.
+                 * @returns {string} JSON string
+                 * @expose
+                 */
+                MessagePrototype.encodeJSON = function() {
+                    return JSON.stringify(
+                        cloneRaw(this,
+                             /* binary-as-base64 */ true,
+                             /* longs-as-strings */ true,
+                             this.$type
+                        )
+                    );
+                };
+
+                /**
+                 * Decodes a message from the specified buffer or string.
+                 * @name ProtoBuf.Builder.Message.decode
+                 * @function
+                 * @param {!ByteBuffer|!ArrayBuffer|!Buffer|string} buffer Buffer to decode from
+                 * @param {(number|string)=} length Message length. Defaults to decode all the remainig data.
+                 * @param {string=} enc Encoding if buffer is a string: hex, utf8 (not recommended), defaults to base64
+                 * @return {!ProtoBuf.Builder.Message} Decoded message
+                 * @throws {Error} If the message cannot be decoded or if required fields are missing. The later still
+                 *  returns the decoded message with missing fields in the `decoded` property on the error.
+                 * @expose
+                 * @see ProtoBuf.Builder.Message.decode64
+                 * @see ProtoBuf.Builder.Message.decodeHex
+                 */
+                Message.decode = function(buffer, length, enc) {
+                    if (typeof length === 'string')
+                        enc = length,
+                        length = -1;
+                    if (typeof buffer === 'string')
+                        buffer = ByteBuffer.wrap(buffer, enc ? enc : "base64");
+                    buffer = ByteBuffer.isByteBuffer(buffer) ? buffer : ByteBuffer.wrap(buffer); // May throw
+                    var le = buffer.littleEndian;
+                    try {
+                        var msg = T.decode(buffer.LE());
+                        buffer.LE(le);
+                        return msg;
+                    } catch (e) {
+                        buffer.LE(le);
+                        throw(e);
+                    }
+                };
+
+                /**
+                 * Decodes a varint32 length-delimited message from the specified buffer or string.
+                 * @name ProtoBuf.Builder.Message.decodeDelimited
+                 * @function
+                 * @param {!ByteBuffer|!ArrayBuffer|!Buffer|string} buffer Buffer to decode from
+                 * @param {string=} enc Encoding if buffer is a string: hex, utf8 (not recommended), defaults to base64
+                 * @return {ProtoBuf.Builder.Message} Decoded message or `null` if not enough bytes are available yet
+                 * @throws {Error} If the message cannot be decoded or if required fields are missing. The later still
+                 *  returns the decoded message with missing fields in the `decoded` property on the error.
+                 * @expose
+                 */
+                Message.decodeDelimited = function(buffer, enc) {
+                    if (typeof buffer === 'string')
+                        buffer = ByteBuffer.wrap(buffer, enc ? enc : "base64");
+                    buffer = ByteBuffer.isByteBuffer(buffer) ? buffer : ByteBuffer.wrap(buffer); // May throw
+                    if (buffer.remaining() < 1)
+                        return null;
+                    var off = buffer.offset,
+                        len = buffer.readVarint32();
+                    if (buffer.remaining() < len) {
+                        buffer.offset = off;
+                        return null;
+                    }
+                    try {
+                        var msg = T.decode(buffer.slice(buffer.offset, buffer.offset + len).LE());
+                        buffer.offset += len;
+                        return msg;
+                    } catch (err) {
+                        buffer.offset += len;
+                        throw err;
+                    }
+                };
+
+                /**
+                 * Decodes the message from the specified base64 encoded string.
+                 * @name ProtoBuf.Builder.Message.decode64
+                 * @function
+                 * @param {string} str String to decode from
+                 * @return {!ProtoBuf.Builder.Message} Decoded message
+                 * @throws {Error} If the message cannot be decoded or if required fields are missing. The later still
+                 *  returns the decoded message with missing fields in the `decoded` property on the error.
+                 * @expose
+                 */
+                Message.decode64 = function(str) {
+                    return Message.decode(str, "base64");
+                };
+
+                /**
+                 * Decodes the message from the specified hex encoded string.
+                 * @name ProtoBuf.Builder.Message.decodeHex
+                 * @function
+                 * @param {string} str String to decode from
+                 * @return {!ProtoBuf.Builder.Message} Decoded message
+                 * @throws {Error} If the message cannot be decoded or if required fields are missing. The later still
+                 *  returns the decoded message with missing fields in the `decoded` property on the error.
+                 * @expose
+                 */
+                Message.decodeHex = function(str) {
+                    return Message.decode(str, "hex");
+                };
+
+                /**
+                 * Decodes the message from a JSON string.
+                 * @name ProtoBuf.Builder.Message.decodeJSON
+                 * @function
+                 * @param {string} str String to decode from
+                 * @return {!ProtoBuf.Builder.Message} Decoded message
+                 * @throws {Error} If the message cannot be decoded or if required fields are
+                 * missing.
+                 * @expose
+                 */
+                Message.decodeJSON = function(str) {
+                    return new Message(JSON.parse(str));
+                };
+
+                // Utility
+
+                /**
+                 * Returns a string representation of this Message.
+                 * @name ProtoBuf.Builder.Message#toString
+                 * @function
+                 * @return {string} String representation as of ".Fully.Qualified.MessageName"
+                 * @expose
+                 */
+                MessagePrototype.toString = function() {
+                    return T.toString();
+                };
+
+                // Properties
+
+                /**
+                 * Message options.
+                 * @name ProtoBuf.Builder.Message.$options
+                 * @type {Object.<string,*>}
+                 * @expose
+                 */
+                var $optionsS; // cc needs this
+
+                /**
+                 * Message options.
+                 * @name ProtoBuf.Builder.Message#$options
+                 * @type {Object.<string,*>}
+                 * @expose
+                 */
+                var $options;
+
+                /**
+                 * Reflection type.
+                 * @name ProtoBuf.Builder.Message.$type
+                 * @type {!ProtoBuf.Reflect.Message}
+                 * @expose
+                 */
+                var $typeS;
+
+                /**
+                 * Reflection type.
+                 * @name ProtoBuf.Builder.Message#$type
+                 * @type {!ProtoBuf.Reflect.Message}
+                 * @expose
+                 */
+                var $type;
+
+                if (Object.defineProperty)
+                    Object.defineProperty(Message, '$options', { "value": T.buildOpt() }),
+                    Object.defineProperty(MessagePrototype, "$options", { "value": Message["$options"] }),
+                    Object.defineProperty(Message, "$type", { "value": T }),
+                    Object.defineProperty(MessagePrototype, "$type", { "value": T });
+
+                return Message;
+
+            })(ProtoBuf, this);
+
+            // Static enums and prototyped sub-messages / cached collections
+            this._fields = [];
+            this._fieldsById = {};
+            this._fieldsByName = {};
+            for (var i=0, k=this.children.length, child; i<k; i++) {
+                child = this.children[i];
+                if (child instanceof Enum || child instanceof Message || child instanceof Service) {
+                    if (clazz.hasOwnProperty(child.name))
+                        throw Error("Illegal reflect child of "+this.toString(true)+": "+child.toString(true)+" cannot override static property '"+child.name+"'");
+                    clazz[child.name] = child.build();
+                } else if (child instanceof Message.Field)
+                    child.build(),
+                    this._fields.push(child),
+                    this._fieldsById[child.id] = child,
+                    this._fieldsByName[child.name] = child;
+                else if (!(child instanceof Message.OneOf) && !(child instanceof Extension)) // Not built
+                    throw Error("Illegal reflect child of "+this.toString(true)+": "+this.children[i].toString(true));
+            }
+
+            return this.clazz = clazz;
+        };
+
+        /**
+         * Encodes a runtime message's contents to the specified buffer.
+         * @param {!ProtoBuf.Builder.Message} message Runtime message to encode
+         * @param {ByteBuffer} buffer ByteBuffer to write to
+         * @param {boolean=} noVerify Whether to not verify field values, defaults to `false`
+         * @return {ByteBuffer} The ByteBuffer for chaining
+         * @throws {Error} If required fields are missing or the message cannot be encoded for another reason
+         * @expose
+         */
+        MessagePrototype.encode = function(message, buffer, noVerify) {
+            var fieldMissing = null,
+                field;
+            for (var i=0, k=this._fields.length, val; i<k; ++i) {
+                field = this._fields[i];
+                val = message[field.name];
+                if (field.required && val === null) {
+                    if (fieldMissing === null)
+                        fieldMissing = field;
+                } else
+                    field.encode(noVerify ? val : field.verifyValue(val), buffer, message);
+            }
+            if (fieldMissing !== null) {
+                var err = Error("Missing at least one required field for "+this.toString(true)+": "+fieldMissing);
+                err["encoded"] = buffer; // Still expose what we got
+                throw(err);
+            }
+            return buffer;
+        };
+
+        /**
+         * Calculates a runtime message's byte length.
+         * @param {!ProtoBuf.Builder.Message} message Runtime message to encode
+         * @returns {number} Byte length
+         * @throws {Error} If required fields are missing or the message cannot be calculated for another reason
+         * @expose
+         */
+        MessagePrototype.calculate = function(message) {
+            for (var n=0, i=0, k=this._fields.length, field, val; i<k; ++i) {
+                field = this._fields[i];
+                val = message[field.name];
+                if (field.required && val === null)
+                   throw Error("Missing at least one required field for "+this.toString(true)+": "+field);
+                else
+                    n += field.calculate(val, message);
+            }
+            return n;
+        };
+
+        /**
+         * Skips all data until the end of the specified group has been reached.
+         * @param {number} expectedId Expected GROUPEND id
+         * @param {!ByteBuffer} buf ByteBuffer
+         * @returns {boolean} `true` if a value as been skipped, `false` if the end has been reached
+         * @throws {Error} If it wasn't possible to find the end of the group (buffer overrun or end tag mismatch)
+         * @inner
+         */
+        function skipTillGroupEnd(expectedId, buf) {
+            var tag = buf.readVarint32(), // Throws on OOB
+                wireType = tag & 0x07,
+                id = tag >>> 3;
+            switch (wireType) {
+                case ProtoBuf.WIRE_TYPES.VARINT:
+                    do tag = buf.readUint8();
+                    while ((tag & 0x80) === 0x80);
+                    break;
+                case ProtoBuf.WIRE_TYPES.BITS64:
+                    buf.offset += 8;
+                    break;
+                case ProtoBuf.WIRE_TYPES.LDELIM:
+                    tag = buf.readVarint32(); // reads the varint
+                    buf.offset += tag;        // skips n bytes
+                    break;
+                case ProtoBuf.WIRE_TYPES.STARTGROUP:
+                    skipTillGroupEnd(id, buf);
+                    break;
+                case ProtoBuf.WIRE_TYPES.ENDGROUP:
+                    if (id === expectedId)
+                        return false;
+                    else
+                        throw Error("Illegal GROUPEND after unknown group: "+id+" ("+expectedId+" expected)");
+                case ProtoBuf.WIRE_TYPES.BITS32:
+                    buf.offset += 4;
+                    break;
+                default:
+                    throw Error("Illegal wire type in unknown group "+expectedId+": "+wireType);
+            }
+            return true;
+        }
+
+        /**
+         * Decodes an encoded message and returns the decoded message.
+         * @param {ByteBuffer} buffer ByteBuffer to decode from
+         * @param {number=} length Message length. Defaults to decode all remaining data.
+         * @param {number=} expectedGroupEndId Expected GROUPEND id if this is a legacy group
+         * @return {ProtoBuf.Builder.Message} Decoded message
+         * @throws {Error} If the message cannot be decoded
+         * @expose
+         */
+        MessagePrototype.decode = function(buffer, length, expectedGroupEndId) {
+            length = typeof length === 'number' ? length : -1;
+            var start = buffer.offset,
+                msg = new (this.clazz)(),
+                tag, wireType, id, field;
+            while (buffer.offset < start+length || (length === -1 && buffer.remaining() > 0)) {
+                tag = buffer.readVarint32();
+                wireType = tag & 0x07;
+                id = tag >>> 3;
+                if (wireType === ProtoBuf.WIRE_TYPES.ENDGROUP) {
+                    if (id !== expectedGroupEndId)
+                        throw Error("Illegal group end indicator for "+this.toString(true)+": "+id+" ("+(expectedGroupEndId ? expectedGroupEndId+" expected" : "not a group")+")");
+                    break;
+                }
+                if (!(field = this._fieldsById[id])) {
+                    // "messages created by your new code can be parsed by your old code: old binaries simply ignore the new field when parsing."
+                    switch (wireType) {
+                        case ProtoBuf.WIRE_TYPES.VARINT:
+                            buffer.readVarint32();
+                            break;
+                        case ProtoBuf.WIRE_TYPES.BITS32:
+                            buffer.offset += 4;
+                            break;
+                        case ProtoBuf.WIRE_TYPES.BITS64:
+                            buffer.offset += 8;
+                            break;
+                        case ProtoBuf.WIRE_TYPES.LDELIM:
+                            var len = buffer.readVarint32();
+                            buffer.offset += len;
+                            break;
+                        case ProtoBuf.WIRE_TYPES.STARTGROUP:
+                            while (skipTillGroupEnd(id, buffer)) {}
+                            break;
+                        default:
+                            throw Error("Illegal wire type for unknown field "+id+" in "+this.toString(true)+"#decode: "+wireType);
+                    }
+                    continue;
+                }
+                if (field.repeated && !field.options["packed"]) {
+                    msg[field.name].push(field.decode(wireType, buffer));
+                } else if (field.map) {
+                    var keyval = field.decode(wireType, buffer);
+                    msg[field.name].set(keyval[0], keyval[1]);
+                } else {
+                    msg[field.name] = field.decode(wireType, buffer);
+                    if (field.oneof) { // Field is part of an OneOf (not a virtual OneOf field)
+                        var currentField = msg[field.oneof.name]; // Virtual field references currently set field
+                        if (currentField !== null && currentField !== field.name)
+                            msg[currentField] = null; // Clear currently set field
+                        msg[field.oneof.name] = field.name; // Point virtual field at this field
+                    }
+                }
+            }
+
+            // Check if all required fields are present and set default values for optional fields that are not
+            for (var i=0, k=this._fields.length; i<k; ++i) {
+                field = this._fields[i];
+                if (msg[field.name] === null) {
+                    if (this.syntax === "proto3") { // Proto3 sets default values by specification
+                        msg[field.name] = field.defaultValue;
+                    } else if (field.required) {
+                        var err = Error("Missing at least one required field for " + this.toString(true) + ": " + field.name);
+                        err["decoded"] = msg; // Still expose what we got
+                        throw(err);
+                    } else if (ProtoBuf.populateDefaults && field.defaultValue !== null)
+                        msg[field.name] = field.defaultValue;
+                }
+            }
+            return msg;
+        };
+
+        /**
+         * @alias ProtoBuf.Reflect.Message
+         * @expose
+         */
+        Reflect.Message = Message;
+
+        /**
+         * Constructs a new Message Field.
+         * @exports ProtoBuf.Reflect.Message.Field
+         * @param {!ProtoBuf.Builder} builder Builder reference
+         * @param {!ProtoBuf.Reflect.Message} message Message reference
+         * @param {string} rule Rule, one of requried, optional, repeated
+         * @param {string?} keytype Key data type, if any.
+         * @param {string} type Data type, e.g. int32
+         * @param {string} name Field name
+         * @param {number} id Unique field id
+         * @param {Object.<string,*>=} options Options
+         * @param {!ProtoBuf.Reflect.Message.OneOf=} oneof Enclosing OneOf
+         * @param {string?} syntax The syntax level of this definition (e.g., proto3)
+         * @constructor
+         * @extends ProtoBuf.Reflect.T
+         */
+        var Field = function(builder, message, rule, keytype, type, name, id, options, oneof, syntax) {
+            T.call(this, builder, message, name);
+
+            /**
+             * @override
+             */
+            this.className = "Message.Field";
+
+            /**
+             * Message field required flag.
+             * @type {boolean}
+             * @expose
+             */
+            this.required = rule === "required";
+
+            /**
+             * Message field repeated flag.
+             * @type {boolean}
+             * @expose
+             */
+            this.repeated = rule === "repeated";
+
+            /**
+             * Message field map flag.
+             * @type {boolean}
+             * @expose
+             */
+            this.map = rule === "map";
+
+            /**
+             * Message field key type. Type reference string if unresolved, protobuf
+             * type if resolved. Valid only if this.map === true, null otherwise.
+             * @type {string|{name: string, wireType: number}|null}
+             * @expose
+             */
+            this.keyType = keytype || null;
+
+            /**
+             * Message field type. Type reference string if unresolved, protobuf type if
+             * resolved. In a map field, this is the value type.
+             * @type {string|{name: string, wireType: number}}
+             * @expose
+             */
+            this.type = type;
+
+            /**
+             * Resolved type reference inside the global namespace.
+             * @type {ProtoBuf.Reflect.T|null}
+             * @expose
+             */
+            this.resolvedType = null;
+
+            /**
+             * Unique message field id.
+             * @type {number}
+             * @expose
+             */
+            this.id = id;
+
+            /**
+             * Message field options.
+             * @type {!Object.<string,*>}
+             * @dict
+             * @expose
+             */
+            this.options = options || {};
+
+            /**
+             * Default value.
+             * @type {*}
+             * @expose
+             */
+            this.defaultValue = null;
+
+            /**
+             * Enclosing OneOf.
+             * @type {?ProtoBuf.Reflect.Message.OneOf}
+             * @expose
+             */
+            this.oneof = oneof || null;
+
+            /**
+             * Syntax level of this definition (e.g., proto3).
+             * @type {string}
+             * @expose
+             */
+            this.syntax = syntax || 'proto2';
+
+            /**
+             * Original field name.
+             * @type {string}
+             * @expose
+             */
+            this.originalName = this.name; // Used to revert camelcase transformation on naming collisions
+
+            /**
+             * Element implementation. Created in build() after types are resolved.
+             * @type {ProtoBuf.Element}
+             * @expose
+             */
+            this.element = null;
+
+            /**
+             * Key element implementation, for map fields. Created in build() after
+             * types are resolved.
+             * @type {ProtoBuf.Element}
+             * @expose
+             */
+            this.keyElement = null;
+
+            // Convert field names to camel case notation if the override is set
+            if (this.builder.options['convertFieldsToCamelCase'] && !(this instanceof Message.ExtensionField))
+                this.name = ProtoBuf.Util.toCamelCase(this.name);
+        };
+
+        /**
+         * @alias ProtoBuf.Reflect.Message.Field.prototype
+         * @inner
+         */
+        var FieldPrototype = Field.prototype = Object.create(T.prototype);
+
+        /**
+         * Builds the field.
+         * @override
+         * @expose
+         */
+        FieldPrototype.build = function() {
+            this.element = new Element(this.type, this.resolvedType, false, this.syntax);
+            if (this.map)
+                this.keyElement = new Element(this.keyType, undefined, true, this.syntax);
+
+            // In proto3, fields do not have field presence, and every field is set to
+            // its type's default value ("", 0, 0.0, or false).
+            if (this.syntax === 'proto3' && !this.repeated && !this.map)
+                this.defaultValue = Element.defaultFieldValue(this.type);
+
+            // Otherwise, default values are present when explicitly specified
+            else if (typeof this.options['default'] !== 'undefined')
+                this.defaultValue = this.verifyValue(this.options['default']);
+        };
+
+        /**
+         * Checks if the given value can be set for this field.
+         * @param {*} value Value to check
+         * @param {boolean=} skipRepeated Whether to skip the repeated value check or not. Defaults to false.
+         * @return {*} Verified, maybe adjusted, value
+         * @throws {Error} If the value cannot be set for this field
+         * @expose
+         */
+        FieldPrototype.verifyValue = function(value, skipRepeated) {
+            skipRepeated = skipRepeated || false;
+            var self = this;
+            function fail(val, msg) {
+                throw Error("Illegal value for "+self.toString(true)+" of type "+self.type.name+": "+val+" ("+msg+")");
+            }
+            if (value === null) { // NULL values for optional fields
+                if (this.required)
+                    fail(typeof value, "required");
+                if (this.syntax === 'proto3' && this.type !== ProtoBuf.TYPES["message"])
+                    fail(typeof value, "proto3 field without field presence cannot be null");
+                return null;
+            }
+            var i;
+            if (this.repeated && !skipRepeated) { // Repeated values as arrays
+                if (!Array.isArray(value))
+                    value = [value];
+                var res = [];
+                for (i=0; i<value.length; i++)
+                    res.push(this.element.verifyValue(value[i]));
+                return res;
+            }
+            if (this.map && !skipRepeated) { // Map values as objects
+                if (!(value instanceof ProtoBuf.Map)) {
+                    // If not already a Map, attempt to convert.
+                    if (!(value instanceof Object)) {
+                        fail(typeof value,
+                             "expected ProtoBuf.Map or raw object for map field");
+                    }
+                    return new ProtoBuf.Map(this, value);
+                } else {
+                    return value;
+                }
+            }
+            // All non-repeated fields expect no array
+            if (!this.repeated && Array.isArray(value))
+                fail(typeof value, "no array expected");
+
+            return this.element.verifyValue(value);
+        };
+
+        /**
+         * Determines whether the field will have a presence on the wire given its
+         * value.
+         * @param {*} value Verified field value
+         * @param {!ProtoBuf.Builder.Message} message Runtime message
+         * @return {boolean} Whether the field will be present on the wire
+         */
+        FieldPrototype.hasWirePresence = function(value, message) {
+            if (this.syntax !== 'proto3')
+                return (value !== null);
+            if (this.oneof && message[this.oneof.name] === this.name)
+                return true;
+            switch (this.type) {
+                case ProtoBuf.TYPES["int32"]:
+                case ProtoBuf.TYPES["sint32"]:
+                case ProtoBuf.TYPES["sfixed32"]:
+                case ProtoBuf.TYPES["uint32"]:
+                case ProtoBuf.TYPES["fixed32"]:
+                    return value !== 0;
+
+                case ProtoBuf.TYPES["int64"]:
+                case ProtoBuf.TYPES["sint64"]:
+                case ProtoBuf.TYPES["sfixed64"]:
+                case ProtoBuf.TYPES["uint64"]:
+                case ProtoBuf.TYPES["fixed64"]:
+                    return value.low !== 0 || value.high !== 0;
+
+                case ProtoBuf.TYPES["bool"]:
+                    return value;
+
+                case ProtoBuf.TYPES["float"]:
+                case ProtoBuf.TYPES["double"]:
+                    return value !== 0.0;
+
+                case ProtoBuf.TYPES["string"]:
+                    return value.length > 0;
+
+                case ProtoBuf.TYPES["bytes"]:
+                    return value.remaining() > 0;
+
+                case ProtoBuf.TYPES["enum"]:
+                    return value !== 0;
+
+                case ProtoBuf.TYPES["message"]:
+                    return value !== null;
+                default:
+                    return true;
+            }
+        };
+
+        /**
+         * Encodes the specified field value to the specified buffer.
+         * @param {*} value Verified field value
+         * @param {ByteBuffer} buffer ByteBuffer to encode to
+         * @param {!ProtoBuf.Builder.Message} message Runtime message
+         * @return {ByteBuffer} The ByteBuffer for chaining
+         * @throws {Error} If the field cannot be encoded
+         * @expose
+         */
+        FieldPrototype.encode = function(value, buffer, message) {
+            if (this.type === null || typeof this.type !== 'object')
+                throw Error("[INTERNAL] Unresolved type in "+this.toString(true)+": "+this.type);
+            if (value === null || (this.repeated && value.length == 0))
+                return buffer; // Optional omitted
+            try {
+                if (this.repeated) {
+                    var i;
+                    // "Only repeated fields of primitive numeric types (types which use the varint, 32-bit, or 64-bit wire
+                    // types) can be declared 'packed'."
+                    if (this.options["packed"] && ProtoBuf.PACKABLE_WIRE_TYPES.indexOf(this.type.wireType) >= 0) {
+                        // "All of the elements of the field are packed into a single key-value pair with wire type 2
+                        // (length-delimited). Each element is encoded the same way it would be normally, except without a
+                        // tag preceding it."
+                        buffer.writeVarint32((this.id << 3) | ProtoBuf.WIRE_TYPES.LDELIM);
+                        buffer.ensureCapacity(buffer.offset += 1); // We do not know the length yet, so let's assume a varint of length 1
+                        var start = buffer.offset; // Remember where the contents begin
+                        for (i=0; i<value.length; i++)
+                            this.element.encodeValue(this.id, value[i], buffer);
+                        var len = buffer.offset-start,
+                            varintLen = ByteBuffer.calculateVarint32(len);
+                        if (varintLen > 1) { // We need to move the contents
+                            var contents = buffer.slice(start, buffer.offset);
+                            start += varintLen-1;
+                            buffer.offset = start;
+                            buffer.append(contents);
+                        }
+                        buffer.writeVarint32(len, start-varintLen);
+                    } else {
+                        // "If your message definition has repeated elements (without the [packed=true] option), the encoded
+                        // message has zero or more key-value pairs with the same tag number"
+                        for (i=0; i<value.length; i++)
+                            buffer.writeVarint32((this.id << 3) | this.type.wireType),
+                            this.element.encodeValue(this.id, value[i], buffer);
+                    }
+                } else if (this.map) {
+                    // Write out each map entry as a submessage.
+                    value.forEach(function(val, key, m) {
+                        // Compute the length of the submessage (key, val) pair.
+                        var length =
+                            ByteBuffer.calculateVarint32((1 << 3) | this.keyType.wireType) +
+                            this.keyElement.calculateLength(1, key) +
+                            ByteBuffer.calculateVarint32((2 << 3) | this.type.wireType) +
+                            this.element.calculateLength(2, val);
+
+                        // Submessage with wire type of length-delimited.
+                        buffer.writeVarint32((this.id << 3) | ProtoBuf.WIRE_TYPES.LDELIM);
+                        buffer.writeVarint32(length);
+
+                        // Write out the key and val.
+                        buffer.writeVarint32((1 << 3) | this.keyType.wireType);
+                        this.keyElement.encodeValue(1, key, buffer);
+                        buffer.writeVarint32((2 << 3) | this.type.wireType);
+                        this.element.encodeValue(2, val, buffer);
+                    }, this);
+                } else {
+                    if (this.hasWirePresence(value, message)) {
+                        buffer.writeVarint32((this.id << 3) | this.type.wireType);
+                        this.element.encodeValue(this.id, value, buffer);
+                    }
+                }
+            } catch (e) {
+                throw Error("Illegal value for "+this.toString(true)+": "+value+" ("+e+")");
+            }
+            return buffer;
+        };
+
+        /**
+         * Calculates the length of this field's value on the network level.
+         * @param {*} value Field value
+         * @param {!ProtoBuf.Builder.Message} message Runtime message
+         * @returns {number} Byte length
+         * @expose
+         */
+        FieldPrototype.calculate = function(value, message) {
+            value = this.verifyValue(value); // May throw
+            if (this.type === null || typeof this.type !== 'object')
+                throw Error("[INTERNAL] Unresolved type in "+this.toString(true)+": "+this.type);
+            if (value === null || (this.repeated && value.length == 0))
+                return 0; // Optional omitted
+            var n = 0;
+            try {
+                if (this.repeated) {
+                    var i, ni;
+                    if (this.options["packed"] && ProtoBuf.PACKABLE_WIRE_TYPES.indexOf(this.type.wireType) >= 0) {
+                        n += ByteBuffer.calculateVarint32((this.id << 3) | ProtoBuf.WIRE_TYPES.LDELIM);
+                        ni = 0;
+                        for (i=0; i<value.length; i++)
+                            ni += this.element.calculateLength(this.id, value[i]);
+                        n += ByteBuffer.calculateVarint32(ni);
+                        n += ni;
+                    } else {
+                        for (i=0; i<value.length; i++)
+                            n += ByteBuffer.calculateVarint32((this.id << 3) | this.type.wireType),
+                            n += this.element.calculateLength(this.id, value[i]);
+                    }
+                } else if (this.map) {
+                    // Each map entry becomes a submessage.
+                    value.forEach(function(val, key, m) {
+                        // Compute the length of the submessage (key, val) pair.
+                        var length =
+                            ByteBuffer.calculateVarint32((1 << 3) | this.keyType.wireType) +
+                            this.keyElement.calculateLength(1, key) +
+                            ByteBuffer.calculateVarint32((2 << 3) | this.type.wireType) +
+                            this.element.calculateLength(2, val);
+
+                        n += ByteBuffer.calculateVarint32((this.id << 3) | ProtoBuf.WIRE_TYPES.LDELIM);
+                        n += ByteBuffer.calculateVarint32(length);
+                        n += length;
+                    }, this);
+                } else {
+                    if (this.hasWirePresence(value, message)) {
+                        n += ByteBuffer.calculateVarint32((this.id << 3) | this.type.wireType);
+                        n += this.element.calculateLength(this.id, value);
+                    }
+                }
+            } catch (e) {
+                throw Error("Illegal value for "+this.toString(true)+": "+value+" ("+e+")");
+            }
+            return n;
+        };
+
+        /**
+         * Decode the field value from the specified buffer.
+         * @param {number} wireType Leading wire type
+         * @param {ByteBuffer} buffer ByteBuffer to decode from
+         * @param {boolean=} skipRepeated Whether to skip the repeated check or not. Defaults to false.
+         * @return {*} Decoded value: array for packed repeated fields, [key, value] for
+         *             map fields, or an individual value otherwise.
+         * @throws {Error} If the field cannot be decoded
+         * @expose
+         */
+        FieldPrototype.decode = function(wireType, buffer, skipRepeated) {
+            var value, nBytes;
+
+            // We expect wireType to match the underlying type's wireType unless we see
+            // a packed repeated field, or unless this is a map field.
+            var wireTypeOK =
+                (!this.map && wireType == this.type.wireType) ||
+                (!skipRepeated && this.repeated && this.options["packed"] &&
+                 wireType == ProtoBuf.WIRE_TYPES.LDELIM) ||
+                (this.map && wireType == ProtoBuf.WIRE_TYPES.LDELIM);
+            if (!wireTypeOK)
+                throw Error("Illegal wire type for field "+this.toString(true)+": "+wireType+" ("+this.type.wireType+" expected)");
+
+            // Handle packed repeated fields.
+            if (wireType == ProtoBuf.WIRE_TYPES.LDELIM && this.repeated && this.options["packed"] && ProtoBuf.PACKABLE_WIRE_TYPES.indexOf(this.type.wireType) >= 0) {
+                if (!skipRepeated) {
+                    nBytes = buffer.readVarint32();
+                    nBytes = buffer.offset + nBytes; // Limit
+                    var values = [];
+                    while (buffer.offset < nBytes)
+                        values.push(this.decode(this.type.wireType, buffer, true));
+                    return values;
+                }
+                // Read the next value otherwise...
+            }
+
+            // Handle maps.
+            if (this.map) {
+                // Read one (key, value) submessage, and return [key, value]
+                var key = Element.defaultFieldValue(this.keyType);
+                value = Element.defaultFieldValue(this.type);
+
+                // Read the length
+                nBytes = buffer.readVarint32();
+                if (buffer.remaining() < nBytes)
+                    throw Error("Illegal number of bytes for "+this.toString(true)+": "+nBytes+" required but got only "+buffer.remaining());
+
+                // Get a sub-buffer of this key/value submessage
+                var msgbuf = buffer.clone();
+                msgbuf.limit = msgbuf.offset + nBytes;
+                buffer.offset += nBytes;
+
+                while (msgbuf.remaining() > 0) {
+                    var tag = msgbuf.readVarint32();
+                    wireType = tag & 0x07;
+                    var id = tag >>> 3;
+                    if (id === 1) {
+                        key = this.keyElement.decode(msgbuf, wireType, id);
+                    } else if (id === 2) {
+                        value = this.element.decode(msgbuf, wireType, id);
+                    } else {
+                        throw Error("Unexpected tag in map field key/value submessage");
+                    }
+                }
+
+                return [key, value];
+            }
+
+            // Handle singular and non-packed repeated field values.
+            return this.element.decode(buffer, wireType, this.id);
+        };
+
+        /**
+         * @alias ProtoBuf.Reflect.Message.Field
+         * @expose
+         */
+        Reflect.Message.Field = Field;
+
+        /**
+         * Constructs a new Message ExtensionField.
+         * @exports ProtoBuf.Reflect.Message.ExtensionField
+         * @param {!ProtoBuf.Builder} builder Builder reference
+         * @param {!ProtoBuf.Reflect.Message} message Message reference
+         * @param {string} rule Rule, one of requried, optional, repeated
+         * @param {string} type Data type, e.g. int32
+         * @param {string} name Field name
+         * @param {number} id Unique field id
+         * @param {!Object.<string,*>=} options Options
+         * @constructor
+         * @extends ProtoBuf.Reflect.Message.Field
+         */
+        var ExtensionField = function(builder, message, rule, type, name, id, options) {
+            Field.call(this, builder, message, rule, /* keytype = */ null, type, name, id, options);
+
+            /**
+             * Extension reference.
+             * @type {!ProtoBuf.Reflect.Extension}
+             * @expose
+             */
+            this.extension;
+        };
+
+        // Extends Field
+        ExtensionField.prototype = Object.create(Field.prototype);
+
+        /**
+         * @alias ProtoBuf.Reflect.Message.ExtensionField
+         * @expose
+         */
+        Reflect.Message.ExtensionField = ExtensionField;
+
+        /**
+         * Constructs a new Message OneOf.
+         * @exports ProtoBuf.Reflect.Message.OneOf
+         * @param {!ProtoBuf.Builder} builder Builder reference
+         * @param {!ProtoBuf.Reflect.Message} message Message reference
+         * @param {string} name OneOf name
+         * @constructor
+         * @extends ProtoBuf.Reflect.T
+         */
+        var OneOf = function(builder, message, name) {
+            T.call(this, builder, message, name);
+
+            /**
+             * Enclosed fields.
+             * @type {!Array.<!ProtoBuf.Reflect.Message.Field>}
+             * @expose
+             */
+            this.fields = [];
+        };
+
+        /**
+         * @alias ProtoBuf.Reflect.Message.OneOf
+         * @expose
+         */
+        Reflect.Message.OneOf = OneOf;
+
+        /**
+         * Constructs a new Enum.
+         * @exports ProtoBuf.Reflect.Enum
+         * @param {!ProtoBuf.Builder} builder Builder reference
+         * @param {!ProtoBuf.Reflect.T} parent Parent Reflect object
+         * @param {string} name Enum name
+         * @param {Object.<string,*>=} options Enum options
+         * @param {string?} syntax The syntax level (e.g., proto3)
+         * @constructor
+         * @extends ProtoBuf.Reflect.Namespace
+         */
+        var Enum = function(builder, parent, name, options, syntax) {
+            Namespace.call(this, builder, parent, name, options, syntax);
+
+            /**
+             * @override
+             */
+            this.className = "Enum";
+
+            /**
+             * Runtime enum object.
+             * @type {Object.<string,number>|null}
+             * @expose
+             */
+            this.object = null;
+        };
+
+        /**
+         * Gets the string name of an enum value.
+         * @param {!ProtoBuf.Builder.Enum} enm Runtime enum
+         * @param {number} value Enum value
+         * @returns {?string} Name or `null` if not present
+         * @expose
+         */
+        Enum.getName = function(enm, value) {
+            var keys = Object.keys(enm);
+            for (var i=0, key; i<keys.length; ++i)
+                if (enm[key = keys[i]] === value)
+                    return key;
+            return null;
+        };
+
+        /**
+         * @alias ProtoBuf.Reflect.Enum.prototype
+         * @inner
+         */
+        var EnumPrototype = Enum.prototype = Object.create(Namespace.prototype);
+
+        /**
+         * Builds this enum and returns the runtime counterpart.
+         * @param {boolean} rebuild Whether to rebuild or not, defaults to false
+         * @returns {!Object.<string,number>}
+         * @expose
+         */
+        EnumPrototype.build = function(rebuild) {
+            if (this.object && !rebuild)
+                return this.object;
+            var enm = new ProtoBuf.Builder.Enum(),
+                values = this.getChildren(Enum.Value);
+            for (var i=0, k=values.length; i<k; ++i)
+                enm[values[i]['name']] = values[i]['id'];
+            if (Object.defineProperty)
+                Object.defineProperty(enm, '$options', {
+                    "value": this.buildOpt(),
+                    "enumerable": false
+                });
+            return this.object = enm;
+        };
+
+        /**
+         * @alias ProtoBuf.Reflect.Enum
+         * @expose
+         */
+        Reflect.Enum = Enum;
+
+        /**
+         * Constructs a new Enum Value.
+         * @exports ProtoBuf.Reflect.Enum.Value
+         * @param {!ProtoBuf.Builder} builder Builder reference
+         * @param {!ProtoBuf.Reflect.Enum} enm Enum reference
+         * @param {string} name Field name
+         * @param {number} id Unique field id
+         * @constructor
+         * @extends ProtoBuf.Reflect.T
+         */
+        var Value = function(builder, enm, name, id) {
+            T.call(this, builder, enm, name);
+
+            /**
+             * @override
+             */
+            this.className = "Enum.Value";
+
+            /**
+             * Unique enum value id.
+             * @type {number}
+             * @expose
+             */
+            this.id = id;
+        };
+
+        // Extends T
+        Value.prototype = Object.create(T.prototype);
+
+        /**
+         * @alias ProtoBuf.Reflect.Enum.Value
+         * @expose
+         */
+        Reflect.Enum.Value = Value;
+
+        /**
+         * An extension (field).
+         * @exports ProtoBuf.Reflect.Extension
+         * @constructor
+         * @param {!ProtoBuf.Builder} builder Builder reference
+         * @param {!ProtoBuf.Reflect.T} parent Parent object
+         * @param {string} name Object name
+         * @param {!ProtoBuf.Reflect.Message.Field} field Extension field
+         */
+        var Extension = function(builder, parent, name, field) {
+            T.call(this, builder, parent, name);
+
+            /**
+             * Extended message field.
+             * @type {!ProtoBuf.Reflect.Message.Field}
+             * @expose
+             */
+            this.field = field;
+        };
+
+        // Extends T
+        Extension.prototype = Object.create(T.prototype);
+
+        /**
+         * @alias ProtoBuf.Reflect.Extension
+         * @expose
+         */
+        Reflect.Extension = Extension;
+
+        /**
+         * Constructs a new Service.
+         * @exports ProtoBuf.Reflect.Service
+         * @param {!ProtoBuf.Builder} builder Builder reference
+         * @param {!ProtoBuf.Reflect.Namespace} root Root
+         * @param {string} name Service name
+         * @param {Object.<string,*>=} options Options
+         * @constructor
+         * @extends ProtoBuf.Reflect.Namespace
+         */
+        var Service = function(builder, root, name, options) {
+            Namespace.call(this, builder, root, name, options);
+
+            /**
+             * @override
+             */
+            this.className = "Service";
+
+            /**
+             * Built runtime service class.
+             * @type {?function(new:ProtoBuf.Builder.Service)}
+             */
+            this.clazz = null;
+        };
+
+        /**
+         * @alias ProtoBuf.Reflect.Service.prototype
+         * @inner
+         */
+        var ServicePrototype = Service.prototype = Object.create(Namespace.prototype);
+
+        /**
+         * Builds the service and returns the runtime counterpart, which is a fully functional class.
+         * @see ProtoBuf.Builder.Service
+         * @param {boolean=} rebuild Whether to rebuild or not
+         * @return {Function} Service class
+         * @throws {Error} If the message cannot be built
+         * @expose
+         */
+        ServicePrototype.build = function(rebuild) {
+            if (this.clazz && !rebuild)
+                return this.clazz;
+
+            // Create the runtime Service class in its own scope
+            return this.clazz = (function(ProtoBuf, T) {
+
+                /**
+                 * Constructs a new runtime Service.
+                 * @name ProtoBuf.Builder.Service
+                 * @param {function(string, ProtoBuf.Builder.Message, function(Error, ProtoBuf.Builder.Message=))=} rpcImpl RPC implementation receiving the method name and the message
+                 * @class Barebone of all runtime services.
+                 * @constructor
+                 * @throws {Error} If the service cannot be created
+                 */
+                var Service = function(rpcImpl) {
+                    ProtoBuf.Builder.Service.call(this);
+
+                    /**
+                     * Service implementation.
+                     * @name ProtoBuf.Builder.Service#rpcImpl
+                     * @type {!function(string, ProtoBuf.Builder.Message, function(Error, ProtoBuf.Builder.Message=))}
+                     * @expose
+                     */
+                    this.rpcImpl = rpcImpl || function(name, msg, callback) {
+                        // This is what a user has to implement: A function receiving the method name, the actual message to
+                        // send (type checked) and the callback that's either provided with the error as its first
+                        // argument or null and the actual response message.
+                        setTimeout(callback.bind(this, Error("Not implemented, see: https://github.com/dcodeIO/ProtoBuf.js/wiki/Services")), 0); // Must be async!
+                    };
+                };
+
+                /**
+                 * @alias ProtoBuf.Builder.Service.prototype
+                 * @inner
+                 */
+                var ServicePrototype = Service.prototype = Object.create(ProtoBuf.Builder.Service.prototype);
+
+                /**
+                 * Asynchronously performs an RPC call using the given RPC implementation.
+                 * @name ProtoBuf.Builder.Service.[Method]
+                 * @function
+                 * @param {!function(string, ProtoBuf.Builder.Message, function(Error, ProtoBuf.Builder.Message=))} rpcImpl RPC implementation
+                 * @param {ProtoBuf.Builder.Message} req Request
+                 * @param {function(Error, (ProtoBuf.Builder.Message|ByteBuffer|Buffer|string)=)} callback Callback receiving
+                 *  the error if any and the response either as a pre-parsed message or as its raw bytes
+                 * @abstract
+                 */
+
+                /**
+                 * Asynchronously performs an RPC call using the instance's RPC implementation.
+                 * @name ProtoBuf.Builder.Service#[Method]
+                 * @function
+                 * @param {ProtoBuf.Builder.Message} req Request
+                 * @param {function(Error, (ProtoBuf.Builder.Message|ByteBuffer|Buffer|string)=)} callback Callback receiving
+                 *  the error if any and the response either as a pre-parsed message or as its raw bytes
+                 * @abstract
+                 */
+
+                var rpc = T.getChildren(ProtoBuf.Reflect.Service.RPCMethod);
+                for (var i=0; i<rpc.length; i++) {
+                    (function(method) {
+
+                        // service#Method(message, callback)
+                        ServicePrototype[method.name] = function(req, callback) {
+                            try {
+                                try {
+                                    // If given as a buffer, decode the request. Will throw a TypeError if not a valid buffer.
+                                    req = method.resolvedRequestType.clazz.decode(ByteBuffer.wrap(req));
+                                } catch (err) {
+                                    if (!(err instanceof TypeError))
+                                        throw err;
+                                }
+                                if (req === null || typeof req !== 'object')
+                                    throw Error("Illegal arguments");
+                                if (!(req instanceof method.resolvedRequestType.clazz))
+                                    req = new method.resolvedRequestType.clazz(req);
+                                this.rpcImpl(method.fqn(), req, function(err, res) { // Assumes that this is properly async
+                                    if (err) {
+                                        callback(err);
+                                        return;
+                                    }
+                                    // Coalesce to empty string when service response has empty content
+                                    if (res === null)
+                                        res = ''
+                                    try { res = method.resolvedResponseType.clazz.decode(res); } catch (notABuffer) {}
+                                    if (!res || !(res instanceof method.resolvedResponseType.clazz)) {
+                                        callback(Error("Illegal response type received in service method "+ T.name+"#"+method.name));
+                                        return;
+                                    }
+                                    callback(null, res);
+                                });
+                            } catch (err) {
+                                setTimeout(callback.bind(this, err), 0);
+                            }
+                        };
+
+                        // Service.Method(rpcImpl, message, callback)
+                        Service[method.name] = function(rpcImpl, req, callback) {
+                            new Service(rpcImpl)[method.name](req, callback);
+                        };
+
+                        if (Object.defineProperty)
+                            Object.defineProperty(Service[method.name], "$options", { "value": method.buildOpt() }),
+                            Object.defineProperty(ServicePrototype[method.name], "$options", { "value": Service[method.name]["$options"] });
+                    })(rpc[i]);
+                }
+
+                // Properties
+
+                /**
+                 * Service options.
+                 * @name ProtoBuf.Builder.Service.$options
+                 * @type {Object.<string,*>}
+                 * @expose
+                 */
+                var $optionsS; // cc needs this
+
+                /**
+                 * Service options.
+                 * @name ProtoBuf.Builder.Service#$options
+                 * @type {Object.<string,*>}
+                 * @expose
+                 */
+                var $options;
+
+                /**
+                 * Reflection type.
+                 * @name ProtoBuf.Builder.Service.$type
+                 * @type {!ProtoBuf.Reflect.Service}
+                 * @expose
+                 */
+                var $typeS;
+
+                /**
+                 * Reflection type.
+                 * @name ProtoBuf.Builder.Service#$type
+                 * @type {!ProtoBuf.Reflect.Service}
+                 * @expose
+                 */
+                var $type;
+
+                if (Object.defineProperty)
+                    Object.defineProperty(Service, "$options", { "value": T.buildOpt() }),
+                    Object.defineProperty(ServicePrototype, "$options", { "value": Service["$options"] }),
+                    Object.defineProperty(Service, "$type", { "value": T }),
+                    Object.defineProperty(ServicePrototype, "$type", { "value": T });
+
+                return Service;
+
+            })(ProtoBuf, this);
+        };
+
+        /**
+         * @alias ProtoBuf.Reflect.Service
+         * @expose
+         */
+        Reflect.Service = Service;
+
+        /**
+         * Abstract service method.
+         * @exports ProtoBuf.Reflect.Service.Method
+         * @param {!ProtoBuf.Builder} builder Builder reference
+         * @param {!ProtoBuf.Reflect.Service} svc Service
+         * @param {string} name Method name
+         * @param {Object.<string,*>=} options Options
+         * @constructor
+         * @extends ProtoBuf.Reflect.T
+         */
+        var Method = function(builder, svc, name, options) {
+            T.call(this, builder, svc, name);
+
+            /**
+             * @override
+             */
+            this.className = "Service.Method";
+
+            /**
+             * Options.
+             * @type {Object.<string, *>}
+             * @expose
+             */
+            this.options = options || {};
+        };
+
+        /**
+         * @alias ProtoBuf.Reflect.Service.Method.prototype
+         * @inner
+         */
+        var MethodPrototype = Method.prototype = Object.create(T.prototype);
+
+        /**
+         * Builds the method's '$options' property.
+         * @name ProtoBuf.Reflect.Service.Method#buildOpt
+         * @function
+         * @return {Object.<string,*>}
+         */
+        MethodPrototype.buildOpt = NamespacePrototype.buildOpt;
+
+        /**
+         * @alias ProtoBuf.Reflect.Service.Method
+         * @expose
+         */
+        Reflect.Service.Method = Method;
+
+        /**
+         * RPC service method.
+         * @exports ProtoBuf.Reflect.Service.RPCMethod
+         * @param {!ProtoBuf.Builder} builder Builder reference
+         * @param {!ProtoBuf.Reflect.Service} svc Service
+         * @param {string} name Method name
+         * @param {string} request Request message name
+         * @param {string} response Response message name
+         * @param {boolean} request_stream Whether requests are streamed
+         * @param {boolean} response_stream Whether responses are streamed
+         * @param {Object.<string,*>=} options Options
+         * @constructor
+         * @extends ProtoBuf.Reflect.Service.Method
+         */
+        var RPCMethod = function(builder, svc, name, request, response, request_stream, response_stream, options) {
+            Method.call(this, builder, svc, name, options);
+
+            /**
+             * @override
+             */
+            this.className = "Service.RPCMethod";
+
+            /**
+             * Request message name.
+             * @type {string}
+             * @expose
+             */
+            this.requestName = request;
+
+            /**
+             * Response message name.
+             * @type {string}
+             * @expose
+             */
+            this.responseName = response;
+
+            /**
+             * Whether requests are streamed
+             * @type {bool}
+             * @expose
+             */
+            this.requestStream = request_stream;
+
+            /**
+             * Whether responses are streamed
+             * @type {bool}
+             * @expose
+             */
+            this.responseStream = response_stream;
+
+            /**
+             * Resolved request message type.
+             * @type {ProtoBuf.Reflect.Message}
+             * @expose
+             */
+            this.resolvedRequestType = null;
+
+            /**
+             * Resolved response message type.
+             * @type {ProtoBuf.Reflect.Message}
+             * @expose
+             */
+            this.resolvedResponseType = null;
+        };
+
+        // Extends Method
+        RPCMethod.prototype = Object.create(Method.prototype);
+
+        /**
+         * @alias ProtoBuf.Reflect.Service.RPCMethod
+         * @expose
+         */
+        Reflect.Service.RPCMethod = RPCMethod;
+
+        return Reflect;
+
+    })(ProtoBuf);
+
+    /**
+     * @alias ProtoBuf.Builder
+     * @expose
+     */
+    ProtoBuf.Builder = (function(ProtoBuf, Lang, Reflect) {
+        "use strict";
+
+        /**
+         * Constructs a new Builder.
+         * @exports ProtoBuf.Builder
+         * @class Provides the functionality to build protocol messages.
+         * @param {Object.<string,*>=} options Options
+         * @constructor
+         */
+        var Builder = function(options) {
+
+            /**
+             * Namespace.
+             * @type {ProtoBuf.Reflect.Namespace}
+             * @expose
+             */
+            this.ns = new Reflect.Namespace(this, null, ""); // Global namespace
+
+            /**
+             * Namespace pointer.
+             * @type {ProtoBuf.Reflect.T}
+             * @expose
+             */
+            this.ptr = this.ns;
+
+            /**
+             * Resolved flag.
+             * @type {boolean}
+             * @expose
+             */
+            this.resolved = false;
+
+            /**
+             * The current building result.
+             * @type {Object.<string,ProtoBuf.Builder.Message|Object>|null}
+             * @expose
+             */
+            this.result = null;
+
+            /**
+             * Imported files.
+             * @type {Array.<string>}
+             * @expose
+             */
+            this.files = {};
+
+            /**
+             * Import root override.
+             * @type {?string}
+             * @expose
+             */
+            this.importRoot = null;
+
+            /**
+             * Options.
+             * @type {!Object.<string, *>}
+             * @expose
+             */
+            this.options = options || {};
+        };
+
+        /**
+         * @alias ProtoBuf.Builder.prototype
+         * @inner
+         */
+        var BuilderPrototype = Builder.prototype;
+
+        // ----- Definition tests -----
+
+        /**
+         * Tests if a definition most likely describes a message.
+         * @param {!Object} def
+         * @returns {boolean}
+         * @expose
+         */
+        Builder.isMessage = function(def) {
+            // Messages require a string name
+            if (typeof def["name"] !== 'string')
+                return false;
+            // Messages do not contain values (enum) or rpc methods (service)
+            if (typeof def["values"] !== 'undefined' || typeof def["rpc"] !== 'undefined')
+                return false;
+            return true;
+        };
+
+        /**
+         * Tests if a definition most likely describes a message field.
+         * @param {!Object} def
+         * @returns {boolean}
+         * @expose
+         */
+        Builder.isMessageField = function(def) {
+            // Message fields require a string rule, name and type and an id
+            if (typeof def["rule"] !== 'string' || typeof def["name"] !== 'string' || typeof def["type"] !== 'string' || typeof def["id"] === 'undefined')
+                return false;
+            return true;
+        };
+
+        /**
+         * Tests if a definition most likely describes an enum.
+         * @param {!Object} def
+         * @returns {boolean}
+         * @expose
+         */
+        Builder.isEnum = function(def) {
+            // Enums require a string name
+            if (typeof def["name"] !== 'string')
+                return false;
+            // Enums require at least one value
+            if (typeof def["values"] === 'undefined' || !Array.isArray(def["values"]) || def["values"].length === 0)
+                return false;
+            return true;
+        };
+
+        /**
+         * Tests if a definition most likely describes a service.
+         * @param {!Object} def
+         * @returns {boolean}
+         * @expose
+         */
+        Builder.isService = function(def) {
+            // Services require a string name and an rpc object
+            if (typeof def["name"] !== 'string' || typeof def["rpc"] !== 'object' || !def["rpc"])
+                return false;
+            return true;
+        };
+
+        /**
+         * Tests if a definition most likely describes an extended message
+         * @param {!Object} def
+         * @returns {boolean}
+         * @expose
+         */
+        Builder.isExtend = function(def) {
+            // Extends rquire a string ref
+            if (typeof def["ref"] !== 'string')
+                return false;
+            return true;
+        };
+
+        // ----- Building -----
+
+        /**
+         * Resets the pointer to the root namespace.
+         * @returns {!ProtoBuf.Builder} this
+         * @expose
+         */
+        BuilderPrototype.reset = function() {
+            this.ptr = this.ns;
+            return this;
+        };
+
+        /**
+         * Defines a namespace on top of the current pointer position and places the pointer on it.
+         * @param {string} namespace
+         * @return {!ProtoBuf.Builder} this
+         * @expose
+         */
+        BuilderPrototype.define = function(namespace) {
+            if (typeof namespace !== 'string' || !Lang.TYPEREF.test(namespace))
+                throw Error("illegal namespace: "+namespace);
+            namespace.split(".").forEach(function(part) {
+                var ns = this.ptr.getChild(part);
+                if (ns === null) // Keep existing
+                    this.ptr.addChild(ns = new Reflect.Namespace(this, this.ptr, part));
+                this.ptr = ns;
+            }, this);
+            return this;
+        };
+
+        /**
+         * Creates the specified definitions at the current pointer position.
+         * @param {!Array.<!Object>} defs Messages, enums or services to create
+         * @returns {!ProtoBuf.Builder} this
+         * @throws {Error} If a message definition is invalid
+         * @expose
+         */
+        BuilderPrototype.create = function(defs) {
+            if (!defs)
+                return this; // Nothing to create
+            if (!Array.isArray(defs))
+                defs = [defs];
+            else {
+                if (defs.length === 0)
+                    return this;
+                defs = defs.slice();
+            }
+
+            // It's quite hard to keep track of scopes and memory here, so let's do this iteratively.
+            var stack = [defs];
+            while (stack.length > 0) {
+                defs = stack.pop();
+
+                if (!Array.isArray(defs)) // Stack always contains entire namespaces
+                    throw Error("not a valid namespace: "+JSON.stringify(defs));
+
+                while (defs.length > 0) {
+                    var def = defs.shift(); // Namespaces always contain an array of messages, enums and services
+
+                    if (Builder.isMessage(def)) {
+                        var obj = new Reflect.Message(this, this.ptr, def["name"], def["options"], def["isGroup"], def["syntax"]);
+
+                        // Create OneOfs
+                        var oneofs = {};
+                        if (def["oneofs"])
+                            Object.keys(def["oneofs"]).forEach(function(name) {
+                                obj.addChild(oneofs[name] = new Reflect.Message.OneOf(this, obj, name));
+                            }, this);
+
+                        // Create fields
+                        if (def["fields"])
+                            def["fields"].forEach(function(fld) {
+                                if (obj.getChild(fld["id"]|0) !== null)
+                                    throw Error("duplicate or invalid field id in "+obj.name+": "+fld['id']);
+                                if (fld["options"] && typeof fld["options"] !== 'object')
+                                    throw Error("illegal field options in "+obj.name+"#"+fld["name"]);
+                                var oneof = null;
+                                if (typeof fld["oneof"] === 'string' && !(oneof = oneofs[fld["oneof"]]))
+                                    throw Error("illegal oneof in "+obj.name+"#"+fld["name"]+": "+fld["oneof"]);
+                                fld = new Reflect.Message.Field(this, obj, fld["rule"], fld["keytype"], fld["type"], fld["name"], fld["id"], fld["options"], oneof, def["syntax"]);
+                                if (oneof)
+                                    oneof.fields.push(fld);
+                                obj.addChild(fld);
+                            }, this);
+
+                        // Push children to stack
+                        var subObj = [];
+                        if (def["enums"])
+                            def["enums"].forEach(function(enm) {
+                                subObj.push(enm);
+                            });
+                        if (def["messages"])
+                            def["messages"].forEach(function(msg) {
+                                subObj.push(msg);
+                            });
+                        if (def["services"])
+                            def["services"].forEach(function(svc) {
+                                subObj.push(svc);
+                            });
+
+                        // Set extension ranges
+                        if (def["extensions"]) {
+                            if (typeof def["extensions"][0] === 'number') // pre 5.0.1
+                                obj.extensions = [ def["extensions"] ];
+                            else
+                                obj.extensions = def["extensions"];
+                        }
+
+                        // Create on top of current namespace
+                        this.ptr.addChild(obj);
+                        if (subObj.length > 0) {
+                            stack.push(defs); // Push the current level back
+                            defs = subObj; // Continue processing sub level
+                            subObj = null;
+                            this.ptr = obj; // And move the pointer to this namespace
+                            obj = null;
+                            continue;
+                        }
+                        subObj = null;
+
+                    } else if (Builder.isEnum(def)) {
+
+                        obj = new Reflect.Enum(this, this.ptr, def["name"], def["options"], def["syntax"]);
+                        def["values"].forEach(function(val) {
+                            obj.addChild(new Reflect.Enum.Value(this, obj, val["name"], val["id"]));
+                        }, this);
+                        this.ptr.addChild(obj);
+
+                    } else if (Builder.isService(def)) {
+
+                        obj = new Reflect.Service(this, this.ptr, def["name"], def["options"]);
+                        Object.keys(def["rpc"]).forEach(function(name) {
+                            var mtd = def["rpc"][name];
+                            obj.addChild(new Reflect.Service.RPCMethod(this, obj, name, mtd["request"], mtd["response"], !!mtd["request_stream"], !!mtd["response_stream"], mtd["options"]));
+                        }, this);
+                        this.ptr.addChild(obj);
+
+                    } else if (Builder.isExtend(def)) {
+
+                        obj = this.ptr.resolve(def["ref"], true);
+                        if (obj) {
+                            def["fields"].forEach(function(fld) {
+                                if (obj.getChild(fld['id']|0) !== null)
+                                    throw Error("duplicate extended field id in "+obj.name+": "+fld['id']);
+                                // Check if field id is allowed to be extended
+                                if (obj.extensions) {
+                                    var valid = false;
+                                    obj.extensions.forEach(function(range) {
+                                        if (fld["id"] >= range[0] && fld["id"] <= range[1])
+                                            valid = true;
+                                    });
+                                    if (!valid)
+                                        throw Error("illegal extended field id in "+obj.name+": "+fld['id']+" (not within valid ranges)");
+                                }
+                                // Convert extension field names to camel case notation if the override is set
+                                var name = fld["name"];
+                                if (this.options['convertFieldsToCamelCase'])
+                                    name = ProtoBuf.Util.toCamelCase(name);
+                                // see #161: Extensions use their fully qualified name as their runtime key and...
+                                var field = new Reflect.Message.ExtensionField(this, obj, fld["rule"], fld["type"], this.ptr.fqn()+'.'+name, fld["id"], fld["options"]);
+                                // ...are added on top of the current namespace as an extension which is used for
+                                // resolving their type later on (the extension always keeps the original name to
+                                // prevent naming collisions)
+                                var ext = new Reflect.Extension(this, this.ptr, fld["name"], field);
+                                field.extension = ext;
+                                this.ptr.addChild(ext);
+                                obj.addChild(field);
+                            }, this);
+
+                        } else if (!/\.?google\.protobuf\./.test(def["ref"])) // Silently skip internal extensions
+                            throw Error("extended message "+def["ref"]+" is not defined");
+
+                    } else
+                        throw Error("not a valid definition: "+JSON.stringify(def));
+
+                    def = null;
+                    obj = null;
+                }
+                // Break goes here
+                defs = null;
+                this.ptr = this.ptr.parent; // Namespace done, continue at parent
+            }
+            this.resolved = false; // Require re-resolve
+            this.result = null; // Require re-build
+            return this;
+        };
+
+        /**
+         * Propagates syntax to all children.
+         * @param {!Object} parent
+         * @inner
+         */
+        function propagateSyntax(parent) {
+            if (parent['messages']) {
+                parent['messages'].forEach(function(child) {
+                    child["syntax"] = parent["syntax"];
+                    propagateSyntax(child);
+                });
+            }
+            if (parent['enums']) {
+                parent['enums'].forEach(function(child) {
+                    child["syntax"] = parent["syntax"];
+                });
+            }
+        }
+
+        /**
+         * Imports another definition into this builder.
+         * @param {Object.<string,*>} json Parsed import
+         * @param {(string|{root: string, file: string})=} filename Imported file name
+         * @returns {!ProtoBuf.Builder} this
+         * @throws {Error} If the definition or file cannot be imported
+         * @expose
+         */
+        BuilderPrototype["import"] = function(json, filename) {
+            var delim = '/';
+
+            // Make sure to skip duplicate imports
+
+            if (typeof filename === 'string') {
+
+                if (ProtoBuf.Util.IS_NODE)
+                    filename = require("path")['resolve'](filename);
+                if (this.files[filename] === true)
+                    return this.reset();
+                this.files[filename] = true;
+
+            } else if (typeof filename === 'object') { // Object with root, file.
+
+                var root = filename.root;
+                if (ProtoBuf.Util.IS_NODE)
+                    root = require("path")['resolve'](root);
+                if (root.indexOf("\\") >= 0 || filename.file.indexOf("\\") >= 0)
+                    delim = '\\';
+                var fname = root + delim + filename.file;
+                if (this.files[fname] === true)
+                    return this.reset();
+                this.files[fname] = true;
+            }
+
+            // Import imports
+
+            if (json['imports'] && json['imports'].length > 0) {
+                var importRoot,
+                    resetRoot = false;
+
+                if (typeof filename === 'object') { // If an import root is specified, override
+
+                    this.importRoot = filename["root"]; resetRoot = true; // ... and reset afterwards
+                    importRoot = this.importRoot;
+                    filename = filename["file"];
+                    if (importRoot.indexOf("\\") >= 0 || filename.indexOf("\\") >= 0)
+                        delim = '\\';
+
+                } else if (typeof filename === 'string') {
+
+                    if (this.importRoot) // If import root is overridden, use it
+                        importRoot = this.importRoot;
+                    else { // Otherwise compute from filename
+                        if (filename.indexOf("/") >= 0) { // Unix
+                            importRoot = filename.replace(/\/[^\/]*$/, "");
+                            if (/* /file.proto */ importRoot === "")
+                                importRoot = "/";
+                        } else if (filename.indexOf("\\") >= 0) { // Windows
+                            importRoot = filename.replace(/\\[^\\]*$/, "");
+                            delim = '\\';
+                        } else
+                            importRoot = ".";
+                    }
+
+                } else
+                    importRoot = null;
+
+                for (var i=0; i<json['imports'].length; i++) {
+                    if (typeof json['imports'][i] === 'string') { // Import file
+                        if (!importRoot)
+                            throw Error("cannot determine import root");
+                        var importFilename = json['imports'][i];
+                        if (importFilename === "google/protobuf/descriptor.proto")
+                            continue; // Not needed and therefore not used
+                        importFilename = importRoot + delim + importFilename;
+                        if (this.files[importFilename] === true)
+                            continue; // Already imported
+                        if (/\.proto$/i.test(importFilename) && !ProtoBuf.DotProto)       // If this is a light build
+                            importFilename = importFilename.replace(/\.proto$/, ".json"); // always load the JSON file
+                        var contents = ProtoBuf.Util.fetch(importFilename);
+                        if (contents === null)
+                            throw Error("failed to import '"+importFilename+"' in '"+filename+"': file not found");
+                        if (/\.json$/i.test(importFilename)) // Always possible
+                            this["import"](JSON.parse(contents+""), importFilename); // May throw
+                        else
+                            this["import"](ProtoBuf.DotProto.Parser.parse(contents), importFilename); // May throw
+                    } else // Import structure
+                        if (!filename)
+                            this["import"](json['imports'][i]);
+                        else if (/\.(\w+)$/.test(filename)) // With extension: Append _importN to the name portion to make it unique
+                            this["import"](json['imports'][i], filename.replace(/^(.+)\.(\w+)$/, function($0, $1, $2) { return $1+"_import"+i+"."+$2; }));
+                        else // Without extension: Append _importN to make it unique
+                            this["import"](json['imports'][i], filename+"_import"+i);
+                }
+                if (resetRoot) // Reset import root override when all imports are done
+                    this.importRoot = null;
+            }
+
+            // Import structures
+
+            if (json['package'])
+                this.define(json['package']);
+            if (json['syntax'])
+                propagateSyntax(json);
+            var base = this.ptr;
+            if (json['options'])
+                Object.keys(json['options']).forEach(function(key) {
+                    base.options[key] = json['options'][key];
+                });
+            if (json['messages'])
+                this.create(json['messages']),
+                this.ptr = base;
+            if (json['enums'])
+                this.create(json['enums']),
+                this.ptr = base;
+            if (json['services'])
+                this.create(json['services']),
+                this.ptr = base;
+            if (json['extends'])
+                this.create(json['extends']);
+
+            return this.reset();
+        };
+
+        /**
+         * Resolves all namespace objects.
+         * @throws {Error} If a type cannot be resolved
+         * @returns {!ProtoBuf.Builder} this
+         * @expose
+         */
+        BuilderPrototype.resolveAll = function() {
+            // Resolve all reflected objects
+            var res;
+            if (this.ptr == null || typeof this.ptr.type === 'object')
+                return this; // Done (already resolved)
+
+            if (this.ptr instanceof Reflect.Namespace) { // Resolve children
+
+                this.ptr.children.forEach(function(child) {
+                    this.ptr = child;
+                    this.resolveAll();
+                }, this);
+
+            } else if (this.ptr instanceof Reflect.Message.Field) { // Resolve type
+
+                if (!Lang.TYPE.test(this.ptr.type)) {
+                    if (!Lang.TYPEREF.test(this.ptr.type))
+                        throw Error("illegal type reference in "+this.ptr.toString(true)+": "+this.ptr.type);
+                    res = (this.ptr instanceof Reflect.Message.ExtensionField ? this.ptr.extension.parent : this.ptr.parent).resolve(this.ptr.type, true);
+                    if (!res)
+                        throw Error("unresolvable type reference in "+this.ptr.toString(true)+": "+this.ptr.type);
+                    this.ptr.resolvedType = res;
+                    if (res instanceof Reflect.Enum) {
+                        this.ptr.type = ProtoBuf.TYPES["enum"];
+                        if (this.ptr.syntax === 'proto3' && res.syntax !== 'proto3')
+                            throw Error("proto3 message cannot reference proto2 enum");
+                    }
+                    else if (res instanceof Reflect.Message)
+                        this.ptr.type = res.isGroup ? ProtoBuf.TYPES["group"] : ProtoBuf.TYPES["message"];
+                    else
+                        throw Error("illegal type reference in "+this.ptr.toString(true)+": "+this.ptr.type);
+                } else
+                    this.ptr.type = ProtoBuf.TYPES[this.ptr.type];
+
+                // If it's a map field, also resolve the key type. The key type can be only a numeric, string, or bool type
+                // (i.e., no enums or messages), so we don't need to resolve against the current namespace.
+                if (this.ptr.map) {
+                    if (!Lang.TYPE.test(this.ptr.keyType))
+                        throw Error("illegal key type for map field in "+this.ptr.toString(true)+": "+this.ptr.keyType);
+                    this.ptr.keyType = ProtoBuf.TYPES[this.ptr.keyType];
+                }
+
+            } else if (this.ptr instanceof ProtoBuf.Reflect.Service.Method) {
+
+                if (this.ptr instanceof ProtoBuf.Reflect.Service.RPCMethod) {
+                    res = this.ptr.parent.resolve(this.ptr.requestName, true);
+                    if (!res || !(res instanceof ProtoBuf.Reflect.Message))
+                        throw Error("Illegal type reference in "+this.ptr.toString(true)+": "+this.ptr.requestName);
+                    this.ptr.resolvedRequestType = res;
+                    res = this.ptr.parent.resolve(this.ptr.responseName, true);
+                    if (!res || !(res instanceof ProtoBuf.Reflect.Message))
+                        throw Error("Illegal type reference in "+this.ptr.toString(true)+": "+this.ptr.responseName);
+                    this.ptr.resolvedResponseType = res;
+                } else // Should not happen as nothing else is implemented
+                    throw Error("illegal service type in "+this.ptr.toString(true));
+
+            } else if (
+                !(this.ptr instanceof ProtoBuf.Reflect.Message.OneOf) && // Not built
+                !(this.ptr instanceof ProtoBuf.Reflect.Extension) && // Not built
+                !(this.ptr instanceof ProtoBuf.Reflect.Enum.Value) // Built in enum
+            )
+                throw Error("illegal object in namespace: "+typeof(this.ptr)+": "+this.ptr);
+
+            return this.reset();
+        };
+
+        /**
+         * Builds the protocol. This will first try to resolve all definitions and, if this has been successful,
+         * return the built package.
+         * @param {(string|Array.<string>)=} path Specifies what to return. If omitted, the entire namespace will be returned.
+         * @returns {!ProtoBuf.Builder.Message|!Object.<string,*>}
+         * @throws {Error} If a type could not be resolved
+         * @expose
+         */
+        BuilderPrototype.build = function(path) {
+            this.reset();
+            if (!this.resolved)
+                this.resolveAll(),
+                this.resolved = true,
+                this.result = null; // Require re-build
+            if (this.result === null) // (Re-)Build
+                this.result = this.ns.build();
+            if (!path)
+                return this.result;
+            var part = typeof path === 'string' ? path.split(".") : path,
+                ptr = this.result; // Build namespace pointer (no hasChild etc.)
+            for (var i=0; i<part.length; i++)
+                if (ptr[part[i]])
+                    ptr = ptr[part[i]];
+                else {
+                    ptr = null;
+                    break;
+                }
+            return ptr;
+        };
+
+        /**
+         * Similar to {@link ProtoBuf.Builder#build}, but looks up the internal reflection descriptor.
+         * @param {string=} path Specifies what to return. If omitted, the entire namespace wiil be returned.
+         * @param {boolean=} excludeNonNamespace Excludes non-namespace types like fields, defaults to `false`
+         * @returns {?ProtoBuf.Reflect.T} Reflection descriptor or `null` if not found
+         */
+        BuilderPrototype.lookup = function(path, excludeNonNamespace) {
+            return path ? this.ns.resolve(path, excludeNonNamespace) : this.ns;
+        };
+
+        /**
+         * Returns a string representation of this object.
+         * @return {string} String representation as of "Builder"
+         * @expose
+         */
+        BuilderPrototype.toString = function() {
+            return "Builder";
+        };
+
+        // ----- Base classes -----
+        // Exist for the sole purpose of being able to "... instanceof ProtoBuf.Builder.Message" etc.
+
+        /**
+         * @alias ProtoBuf.Builder.Message
+         */
+        Builder.Message = function() {};
+
+        /**
+         * @alias ProtoBuf.Builder.Enum
+         */
+        Builder.Enum = function() {};
+
+        /**
+         * @alias ProtoBuf.Builder.Message
+         */
+        Builder.Service = function() {};
+
+        return Builder;
+
+    })(ProtoBuf, ProtoBuf.Lang, ProtoBuf.Reflect);
+
+    /**
+     * @alias ProtoBuf.Map
+     * @expose
+     */
+    ProtoBuf.Map = (function(ProtoBuf, Reflect) {
+        "use strict";
+
+        /**
+         * Constructs a new Map. A Map is a container that is used to implement map
+         * fields on message objects. It closely follows the ES6 Map API; however,
+         * it is distinct because we do not want to depend on external polyfills or
+         * on ES6 itself.
+         *
+         * @exports ProtoBuf.Map
+         * @param {!ProtoBuf.Reflect.Field} field Map field
+         * @param {Object.<string,*>=} contents Initial contents
+         * @constructor
+         */
+        var Map = function(field, contents) {
+            if (!field.map)
+                throw Error("field is not a map");
+
+            /**
+             * The field corresponding to this map.
+             * @type {!ProtoBuf.Reflect.Field}
+             */
+            this.field = field;
+
+            /**
+             * Element instance corresponding to key type.
+             * @type {!ProtoBuf.Reflect.Element}
+             */
+            this.keyElem = new Reflect.Element(field.keyType, null, true, field.syntax);
+
+            /**
+             * Element instance corresponding to value type.
+             * @type {!ProtoBuf.Reflect.Element}
+             */
+            this.valueElem = new Reflect.Element(field.type, field.resolvedType, false, field.syntax);
+
+            /**
+             * Internal map: stores mapping of (string form of key) -> (key, value)
+             * pair.
+             *
+             * We provide map semantics for arbitrary key types, but we build on top
+             * of an Object, which has only string keys. In order to avoid the need
+             * to convert a string key back to its native type in many situations,
+             * we store the native key value alongside the value. Thus, we only need
+             * a one-way mapping from a key type to its string form that guarantees
+             * uniqueness and equality (i.e., str(K1) === str(K2) if and only if K1
+             * === K2).
+             *
+             * @type {!Object<string, {key: *, value: *}>}
+             */
+            this.map = {};
+
+            /**
+             * Returns the number of elements in the map.
+             */
+            Object.defineProperty(this, "size", {
+                get: function() { return Object.keys(this.map).length; }
+            });
+
+            // Fill initial contents from a raw object.
+            if (contents) {
+                var keys = Object.keys(contents);
+                for (var i = 0; i < keys.length; i++) {
+                    var key = this.keyElem.valueFromString(keys[i]);
+                    var val = this.valueElem.verifyValue(contents[keys[i]]);
+                    this.map[this.keyElem.valueToString(key)] =
+                        { key: key, value: val };
+                }
+            }
+        };
+
+        var MapPrototype = Map.prototype;
+
+        /**
+         * Helper: return an iterator over an array.
+         * @param {!Array<*>} arr the array
+         * @returns {!Object} an iterator
+         * @inner
+         */
+        function arrayIterator(arr) {
+            var idx = 0;
+            return {
+                next: function() {
+                    if (idx < arr.length)
+                        return { done: false, value: arr[idx++] };
+                    return { done: true };
+                }
+            }
+        }
+
+        /**
+         * Clears the map.
+         */
+        MapPrototype.clear = function() {
+            this.map = {};
+        };
+
+        /**
+         * Deletes a particular key from the map.
+         * @returns {boolean} Whether any entry with this key was deleted.
+         */
+        MapPrototype["delete"] = function(key) {
+            var keyValue = this.keyElem.valueToString(this.keyElem.verifyValue(key));
+            var hadKey = keyValue in this.map;
+            delete this.map[keyValue];
+            return hadKey;
+        };
+
+        /**
+         * Returns an iterator over [key, value] pairs in the map.
+         * @returns {Object} The iterator
+         */
+        MapPrototype.entries = function() {
+            var entries = [];
+            var strKeys = Object.keys(this.map);
+            for (var i = 0, entry; i < strKeys.length; i++)
+                entries.push([(entry=this.map[strKeys[i]]).key, entry.value]);
+            return arrayIterator(entries);
+        };
+
+        /**
+         * Returns an iterator over keys in the map.
+         * @returns {Object} The iterator
+         */
+        MapPrototype.keys = function() {
+            var keys = [];
+            var strKeys = Object.keys(this.map);
+            for (var i = 0; i < strKeys.length; i++)
+                keys.push(this.map[strKeys[i]].key);
+            return arrayIterator(keys);
+        };
+
+        /**
+         * Returns an iterator over values in the map.
+         * @returns {!Object} The iterator
+         */
+        MapPrototype.values = function() {
+            var values = [];
+            var strKeys = Object.keys(this.map);
+            for (var i = 0; i < strKeys.length; i++)
+                values.push(this.map[strKeys[i]].value);
+            return arrayIterator(values);
+        };
+
+        /**
+         * Iterates over entries in the map, calling a function on each.
+         * @param {function(this:*, *, *, *)} cb The callback to invoke with value, key, and map arguments.
+         * @param {Object=} thisArg The `this` value for the callback
+         */
+        MapPrototype.forEach = function(cb, thisArg) {
+            var strKeys = Object.keys(this.map);
+            for (var i = 0, entry; i < strKeys.length; i++)
+                cb.call(thisArg, (entry=this.map[strKeys[i]]).value, entry.key, this);
+        };
+
+        /**
+         * Sets a key in the map to the given value.
+         * @param {*} key The key
+         * @param {*} value The value
+         * @returns {!ProtoBuf.Map} The map instance
+         */
+        MapPrototype.set = function(key, value) {
+            var keyValue = this.keyElem.verifyValue(key);
+            var valValue = this.valueElem.verifyValue(value);
+            this.map[this.keyElem.valueToString(keyValue)] =
+                { key: keyValue, value: valValue };
+            return this;
+        };
+
+        /**
+         * Gets the value corresponding to a key in the map.
+         * @param {*} key The key
+         * @returns {*|undefined} The value, or `undefined` if key not present
+         */
+        MapPrototype.get = function(key) {
+            var keyValue = this.keyElem.valueToString(this.keyElem.verifyValue(key));
+            if (!(keyValue in this.map))
+                return undefined;
+            return this.map[keyValue].value;
+        };
+
+        /**
+         * Determines whether the given key is present in the map.
+         * @param {*} key The key
+         * @returns {boolean} `true` if the key is present
+         */
+        MapPrototype.has = function(key) {
+            var keyValue = this.keyElem.valueToString(this.keyElem.verifyValue(key));
+            return (keyValue in this.map);
+        };
+
+        return Map;
+    })(ProtoBuf, ProtoBuf.Reflect);
+
+
+    /**
+     * Loads a .proto string and returns the Builder.
+     * @param {string} proto .proto file contents
+     * @param {(ProtoBuf.Builder|string|{root: string, file: string})=} builder Builder to append to. Will create a new one if omitted.
+     * @param {(string|{root: string, file: string})=} filename The corresponding file name if known. Must be specified for imports.
+     * @return {ProtoBuf.Builder} Builder to create new messages
+     * @throws {Error} If the definition cannot be parsed or built
+     * @expose
+     */
+    ProtoBuf.loadProto = function(proto, builder, filename) {
+        if (typeof builder === 'string' || (builder && typeof builder["file"] === 'string' && typeof builder["root"] === 'string'))
+            filename = builder,
+            builder = undefined;
+        return ProtoBuf.loadJson(ProtoBuf.DotProto.Parser.parse(proto), builder, filename);
+    };
+
+    /**
+     * Loads a .proto string and returns the Builder. This is an alias of {@link ProtoBuf.loadProto}.
+     * @function
+     * @param {string} proto .proto file contents
+     * @param {(ProtoBuf.Builder|string)=} builder Builder to append to. Will create a new one if omitted.
+     * @param {(string|{root: string, file: string})=} filename The corresponding file name if known. Must be specified for imports.
+     * @return {ProtoBuf.Builder} Builder to create new messages
+     * @throws {Error} If the definition cannot be parsed or built
+     * @expose
+     */
+    ProtoBuf.protoFromString = ProtoBuf.loadProto; // Legacy
+
+    /**
+     * Loads a .proto file and returns the Builder.
+     * @param {string|{root: string, file: string}} filename Path to proto file or an object specifying 'file' with
+     *  an overridden 'root' path for all imported files.
+     * @param {function(?Error, !ProtoBuf.Builder=)=} callback Callback that will receive `null` as the first and
+     *  the Builder as its second argument on success, otherwise the error as its first argument. If omitted, the
+     *  file will be read synchronously and this function will return the Builder.
+     * @param {ProtoBuf.Builder=} builder Builder to append to. Will create a new one if omitted.
+     * @return {?ProtoBuf.Builder|undefined} The Builder if synchronous (no callback specified, will be NULL if the
+     *   request has failed), else undefined
+     * @expose
+     */
+    ProtoBuf.loadProtoFile = function(filename, callback, builder) {
+        if (callback && typeof callback === 'object')
+            builder = callback,
+            callback = null;
+        else if (!callback || typeof callback !== 'function')
+            callback = null;
+        if (callback)
+            return ProtoBuf.Util.fetch(typeof filename === 'string' ? filename : filename["root"]+"/"+filename["file"], function(contents) {
+                if (contents === null) {
+                    callback(Error("Failed to fetch file"));
+                    return;
+                }
+                try {
+                    callback(null, ProtoBuf.loadProto(contents, builder, filename));
+                } catch (e) {
+                    callback(e);
+                }
+            });
+        var contents = ProtoBuf.Util.fetch(typeof filename === 'object' ? filename["root"]+"/"+filename["file"] : filename);
+        return contents === null ? null : ProtoBuf.loadProto(contents, builder, filename);
+    };
+
+    /**
+     * Loads a .proto file and returns the Builder. This is an alias of {@link ProtoBuf.loadProtoFile}.
+     * @function
+     * @param {string|{root: string, file: string}} filename Path to proto file or an object specifying 'file' with
+     *  an overridden 'root' path for all imported files.
+     * @param {function(?Error, !ProtoBuf.Builder=)=} callback Callback that will receive `null` as the first and
+     *  the Builder as its second argument on success, otherwise the error as its first argument. If omitted, the
+     *  file will be read synchronously and this function will return the Builder.
+     * @param {ProtoBuf.Builder=} builder Builder to append to. Will create a new one if omitted.
+     * @return {!ProtoBuf.Builder|undefined} The Builder if synchronous (no callback specified, will be NULL if the
+     *   request has failed), else undefined
+     * @expose
+     */
+    ProtoBuf.protoFromFile = ProtoBuf.loadProtoFile; // Legacy
+
+
+    /**
+     * Constructs a new empty Builder.
+     * @param {Object.<string,*>=} options Builder options, defaults to global options set on ProtoBuf
+     * @return {!ProtoBuf.Builder} Builder
+     * @expose
+     */
+    ProtoBuf.newBuilder = function(options) {
+        options = options || {};
+        if (typeof options['convertFieldsToCamelCase'] === 'undefined')
+            options['convertFieldsToCamelCase'] = ProtoBuf.convertFieldsToCamelCase;
+        if (typeof options['populateAccessors'] === 'undefined')
+            options['populateAccessors'] = ProtoBuf.populateAccessors;
+        return new ProtoBuf.Builder(options);
+    };
+
+    /**
+     * Loads a .json definition and returns the Builder.
+     * @param {!*|string} json JSON definition
+     * @param {(ProtoBuf.Builder|string|{root: string, file: string})=} builder Builder to append to. Will create a new one if omitted.
+     * @param {(string|{root: string, file: string})=} filename The corresponding file name if known. Must be specified for imports.
+     * @return {ProtoBuf.Builder} Builder to create new messages
+     * @throws {Error} If the definition cannot be parsed or built
+     * @expose
+     */
+    ProtoBuf.loadJson = function(json, builder, filename) {
+        if (typeof builder === 'string' || (builder && typeof builder["file"] === 'string' && typeof builder["root"] === 'string'))
+            filename = builder,
+            builder = null;
+        if (!builder || typeof builder !== 'object')
+            builder = ProtoBuf.newBuilder();
+        if (typeof json === 'string')
+            json = JSON.parse(json);
+        builder["import"](json, filename);
+        builder.resolveAll();
+        return builder;
+    };
+
+    /**
+     * Loads a .json file and returns the Builder.
+     * @param {string|!{root: string, file: string}} filename Path to json file or an object specifying 'file' with
+     *  an overridden 'root' path for all imported files.
+     * @param {function(?Error, !ProtoBuf.Builder=)=} callback Callback that will receive `null` as the first and
+     *  the Builder as its second argument on success, otherwise the error as its first argument. If omitted, the
+     *  file will be read synchronously and this function will return the Builder.
+     * @param {ProtoBuf.Builder=} builder Builder to append to. Will create a new one if omitted.
+     * @return {?ProtoBuf.Builder|undefined} The Builder if synchronous (no callback specified, will be NULL if the
+     *   request has failed), else undefined
+     * @expose
+     */
+    ProtoBuf.loadJsonFile = function(filename, callback, builder) {
+        if (callback && typeof callback === 'object')
+            builder = callback,
+            callback = null;
+        else if (!callback || typeof callback !== 'function')
+            callback = null;
+        if (callback)
+            return ProtoBuf.Util.fetch(typeof filename === 'string' ? filename : filename["root"]+"/"+filename["file"], function(contents) {
+                if (contents === null) {
+                    callback(Error("Failed to fetch file"));
+                    return;
+                }
+                try {
+                    callback(null, ProtoBuf.loadJson(JSON.parse(contents), builder, filename));
+                } catch (e) {
+                    callback(e);
+                }
+            });
+        var contents = ProtoBuf.Util.fetch(typeof filename === 'object' ? filename["root"]+"/"+filename["file"] : filename);
+        return contents === null ? null : ProtoBuf.loadJson(JSON.parse(contents), builder, filename);
+    };
+
+    return ProtoBuf;
+});

+ 396 - 47
css/converse.css

@@ -2346,7 +2346,7 @@
   --primary: #387592;
   --primary: #387592;
   --secondary: #6c757d;
   --secondary: #6c757d;
   --success: #3AA569;
   --success: #3AA569;
-  --info: #17a2b8;
+  --info: #3AA569;
   --warning: #ffc107;
   --warning: #ffc107;
   --danger: #E77051;
   --danger: #E77051;
   --light: #f8f9fa;
   --light: #f8f9fa;
@@ -3594,24 +3594,24 @@
       box-shadow: 0 0 0 0.2rem rgba(58, 165, 105, 0.5); }
       box-shadow: 0 0 0 0.2rem rgba(58, 165, 105, 0.5); }
 #conversejs .btn-info {
 #conversejs .btn-info {
   color: #fff;
   color: #fff;
-  background-color: #17a2b8;
-  border-color: #17a2b8; }
+  background-color: #3AA569;
+  border-color: #3AA569; }
   #conversejs .btn-info:hover {
   #conversejs .btn-info:hover {
     color: #fff;
     color: #fff;
-    background-color: #138496;
-    border-color: #117a8b; }
+    background-color: #308957;
+    border-color: #2d7f51; }
   #conversejs .btn-info:focus, #conversejs .btn-info.focus {
   #conversejs .btn-info:focus, #conversejs .btn-info.focus {
-    box-shadow: 0 0 0 0.2rem rgba(23, 162, 184, 0.5); }
+    box-shadow: 0 0 0 0.2rem rgba(58, 165, 105, 0.5); }
   #conversejs .btn-info.disabled, #conversejs .btn-info:disabled {
   #conversejs .btn-info.disabled, #conversejs .btn-info:disabled {
     color: #fff;
     color: #fff;
-    background-color: #17a2b8;
-    border-color: #17a2b8; }
+    background-color: #3AA569;
+    border-color: #3AA569; }
   #conversejs .btn-info:not(:disabled):not(.disabled):active, #conversejs .btn-info:not(:disabled):not(.disabled).active, .show > #conversejs .btn-info.dropdown-toggle {
   #conversejs .btn-info:not(:disabled):not(.disabled):active, #conversejs .btn-info:not(:disabled):not(.disabled).active, .show > #conversejs .btn-info.dropdown-toggle {
     color: #fff;
     color: #fff;
-    background-color: #117a8b;
-    border-color: #10707f; }
+    background-color: #2d7f51;
+    border-color: #29764b; }
     #conversejs .btn-info:not(:disabled):not(.disabled):active:focus, #conversejs .btn-info:not(:disabled):not(.disabled).active:focus, .show > #conversejs .btn-info.dropdown-toggle:focus {
     #conversejs .btn-info:not(:disabled):not(.disabled):active:focus, #conversejs .btn-info:not(:disabled):not(.disabled).active:focus, .show > #conversejs .btn-info.dropdown-toggle:focus {
-      box-shadow: 0 0 0 0.2rem rgba(23, 162, 184, 0.5); }
+      box-shadow: 0 0 0 0.2rem rgba(58, 165, 105, 0.5); }
 #conversejs .btn-warning {
 #conversejs .btn-warning {
   color: #212529;
   color: #212529;
   background-color: #ffc107;
   background-color: #ffc107;
@@ -3753,25 +3753,25 @@
     #conversejs .btn-outline-success:not(:disabled):not(.disabled):active:focus, #conversejs .btn-outline-success:not(:disabled):not(.disabled).active:focus, .show > #conversejs .btn-outline-success.dropdown-toggle:focus {
     #conversejs .btn-outline-success:not(:disabled):not(.disabled):active:focus, #conversejs .btn-outline-success:not(:disabled):not(.disabled).active:focus, .show > #conversejs .btn-outline-success.dropdown-toggle:focus {
       box-shadow: 0 0 0 0.2rem rgba(58, 165, 105, 0.5); }
       box-shadow: 0 0 0 0.2rem rgba(58, 165, 105, 0.5); }
 #conversejs .btn-outline-info {
 #conversejs .btn-outline-info {
-  color: #17a2b8;
+  color: #3AA569;
   background-color: transparent;
   background-color: transparent;
   background-image: none;
   background-image: none;
-  border-color: #17a2b8; }
+  border-color: #3AA569; }
   #conversejs .btn-outline-info:hover {
   #conversejs .btn-outline-info:hover {
     color: #fff;
     color: #fff;
-    background-color: #17a2b8;
-    border-color: #17a2b8; }
+    background-color: #3AA569;
+    border-color: #3AA569; }
   #conversejs .btn-outline-info:focus, #conversejs .btn-outline-info.focus {
   #conversejs .btn-outline-info:focus, #conversejs .btn-outline-info.focus {
-    box-shadow: 0 0 0 0.2rem rgba(23, 162, 184, 0.5); }
+    box-shadow: 0 0 0 0.2rem rgba(58, 165, 105, 0.5); }
   #conversejs .btn-outline-info.disabled, #conversejs .btn-outline-info:disabled {
   #conversejs .btn-outline-info.disabled, #conversejs .btn-outline-info:disabled {
-    color: #17a2b8;
+    color: #3AA569;
     background-color: transparent; }
     background-color: transparent; }
   #conversejs .btn-outline-info:not(:disabled):not(.disabled):active, #conversejs .btn-outline-info:not(:disabled):not(.disabled).active, .show > #conversejs .btn-outline-info.dropdown-toggle {
   #conversejs .btn-outline-info:not(:disabled):not(.disabled):active, #conversejs .btn-outline-info:not(:disabled):not(.disabled).active, .show > #conversejs .btn-outline-info.dropdown-toggle {
     color: #fff;
     color: #fff;
-    background-color: #17a2b8;
-    border-color: #17a2b8; }
+    background-color: #3AA569;
+    border-color: #3AA569; }
     #conversejs .btn-outline-info:not(:disabled):not(.disabled):active:focus, #conversejs .btn-outline-info:not(:disabled):not(.disabled).active:focus, .show > #conversejs .btn-outline-info.dropdown-toggle:focus {
     #conversejs .btn-outline-info:not(:disabled):not(.disabled):active:focus, #conversejs .btn-outline-info:not(:disabled):not(.disabled).active:focus, .show > #conversejs .btn-outline-info.dropdown-toggle:focus {
-      box-shadow: 0 0 0 0.2rem rgba(23, 162, 184, 0.5); }
+      box-shadow: 0 0 0 0.2rem rgba(58, 165, 105, 0.5); }
 #conversejs .btn-outline-warning {
 #conversejs .btn-outline-warning {
   color: #ffc107;
   color: #ffc107;
   background-color: transparent;
   background-color: transparent;
@@ -4371,6 +4371,321 @@
     background-color: #e9ecef;
     background-color: #e9ecef;
     border-left: 1px solid #ced4da;
     border-left: 1px solid #ced4da;
     border-radius: 0 0.25rem 0.25rem 0; }
     border-radius: 0 0.25rem 0.25rem 0; }
+#conversejs .nav {
+  display: flex;
+  flex-wrap: wrap;
+  padding-left: 0;
+  margin-bottom: 0;
+  list-style: none; }
+#conversejs .nav-link {
+  display: block;
+  padding: 0.5rem 1rem; }
+  #conversejs .nav-link:hover, #conversejs .nav-link:focus {
+    text-decoration: none; }
+  #conversejs .nav-link.disabled {
+    color: #6c757d; }
+#conversejs .nav-tabs {
+  border-bottom: 1px solid #dee2e6; }
+  #conversejs .nav-tabs .nav-item {
+    margin-bottom: -1px; }
+  #conversejs .nav-tabs .nav-link {
+    border: 1px solid transparent;
+    border-top-left-radius: 0.25rem;
+    border-top-right-radius: 0.25rem; }
+    #conversejs .nav-tabs .nav-link:hover, #conversejs .nav-tabs .nav-link:focus {
+      border-color: #e9ecef #e9ecef #dee2e6; }
+    #conversejs .nav-tabs .nav-link.disabled {
+      color: #6c757d;
+      background-color: transparent;
+      border-color: transparent; }
+  #conversejs .nav-tabs .nav-link.active,
+  #conversejs .nav-tabs .nav-item.show .nav-link {
+    color: #495057;
+    background-color: #fff;
+    border-color: #dee2e6 #dee2e6 #fff; }
+  #conversejs .nav-tabs .dropdown-menu {
+    margin-top: -1px;
+    border-top-left-radius: 0;
+    border-top-right-radius: 0; }
+#conversejs .nav-pills .nav-link {
+  border-radius: 0.25rem; }
+#conversejs .nav-pills .nav-link.active,
+#conversejs .nav-pills .show > .nav-link {
+  color: #fff;
+  background-color: #387592; }
+#conversejs .nav-fill .nav-item {
+  flex: 1 1 auto;
+  text-align: center; }
+#conversejs .nav-justified .nav-item {
+  flex-basis: 0;
+  flex-grow: 1;
+  text-align: center; }
+#conversejs .tab-content > .tab-pane {
+  display: none; }
+#conversejs .tab-content > .active {
+  display: block; }
+#conversejs .navbar {
+  position: relative;
+  display: flex;
+  flex-wrap: wrap;
+  align-items: center;
+  justify-content: space-between;
+  padding: 0.5rem 1rem; }
+  #conversejs .navbar > .container,
+  #conversejs .navbar > .container-fluid {
+    display: flex;
+    flex-wrap: wrap;
+    align-items: center;
+    justify-content: space-between; }
+#conversejs .navbar-brand {
+  display: inline-block;
+  padding-top: 0.3125rem;
+  padding-bottom: 0.3125rem;
+  margin-right: 1rem;
+  font-size: 1.25rem;
+  line-height: inherit;
+  white-space: nowrap; }
+  #conversejs .navbar-brand:hover, #conversejs .navbar-brand:focus {
+    text-decoration: none; }
+#conversejs .navbar-nav {
+  display: flex;
+  flex-direction: column;
+  padding-left: 0;
+  margin-bottom: 0;
+  list-style: none; }
+  #conversejs .navbar-nav .nav-link {
+    padding-right: 0;
+    padding-left: 0; }
+  #conversejs .navbar-nav .dropdown-menu {
+    position: static;
+    float: none; }
+#conversejs .navbar-text {
+  display: inline-block;
+  padding-top: 0.5rem;
+  padding-bottom: 0.5rem; }
+#conversejs .navbar-collapse {
+  flex-basis: 100%;
+  flex-grow: 1;
+  align-items: center; }
+#conversejs .navbar-toggler {
+  padding: 0.25rem 0.75rem;
+  font-size: 1.25rem;
+  line-height: 1;
+  background-color: transparent;
+  border: 1px solid transparent;
+  border-radius: 0.25rem; }
+  #conversejs .navbar-toggler:hover, #conversejs .navbar-toggler:focus {
+    text-decoration: none; }
+  #conversejs .navbar-toggler:not(:disabled):not(.disabled) {
+    cursor: pointer; }
+#conversejs .navbar-toggler-icon {
+  display: inline-block;
+  width: 1.5em;
+  height: 1.5em;
+  vertical-align: middle;
+  content: "";
+  background: no-repeat center center;
+  background-size: 100% 100%; }
+@media (max-width: 575.98px) {
+  #conversejs .navbar-expand-sm > .container,
+  #conversejs .navbar-expand-sm > .container-fluid {
+    padding-right: 0;
+    padding-left: 0; } }
+@media (min-width: 576px) {
+  #conversejs .navbar-expand-sm {
+    flex-flow: row nowrap;
+    justify-content: flex-start; }
+    #conversejs .navbar-expand-sm .navbar-nav {
+      flex-direction: row; }
+      #conversejs .navbar-expand-sm .navbar-nav .dropdown-menu {
+        position: absolute; }
+      #conversejs .navbar-expand-sm .navbar-nav .dropdown-menu-right {
+        right: 0;
+        left: auto; }
+      #conversejs .navbar-expand-sm .navbar-nav .nav-link {
+        padding-right: 0.5rem;
+        padding-left: 0.5rem; }
+    #conversejs .navbar-expand-sm > .container,
+    #conversejs .navbar-expand-sm > .container-fluid {
+      flex-wrap: nowrap; }
+    #conversejs .navbar-expand-sm .navbar-collapse {
+      display: flex !important;
+      flex-basis: auto; }
+    #conversejs .navbar-expand-sm .navbar-toggler {
+      display: none; }
+    #conversejs .navbar-expand-sm .dropup .dropdown-menu {
+      top: auto;
+      bottom: 100%; } }
+@media (max-width: 767.98px) {
+  #conversejs .navbar-expand-md > .container,
+  #conversejs .navbar-expand-md > .container-fluid {
+    padding-right: 0;
+    padding-left: 0; } }
+@media (min-width: 768px) {
+  #conversejs .navbar-expand-md {
+    flex-flow: row nowrap;
+    justify-content: flex-start; }
+    #conversejs .navbar-expand-md .navbar-nav {
+      flex-direction: row; }
+      #conversejs .navbar-expand-md .navbar-nav .dropdown-menu {
+        position: absolute; }
+      #conversejs .navbar-expand-md .navbar-nav .dropdown-menu-right {
+        right: 0;
+        left: auto; }
+      #conversejs .navbar-expand-md .navbar-nav .nav-link {
+        padding-right: 0.5rem;
+        padding-left: 0.5rem; }
+    #conversejs .navbar-expand-md > .container,
+    #conversejs .navbar-expand-md > .container-fluid {
+      flex-wrap: nowrap; }
+    #conversejs .navbar-expand-md .navbar-collapse {
+      display: flex !important;
+      flex-basis: auto; }
+    #conversejs .navbar-expand-md .navbar-toggler {
+      display: none; }
+    #conversejs .navbar-expand-md .dropup .dropdown-menu {
+      top: auto;
+      bottom: 100%; } }
+@media (max-width: 991.98px) {
+  #conversejs .navbar-expand-lg > .container,
+  #conversejs .navbar-expand-lg > .container-fluid {
+    padding-right: 0;
+    padding-left: 0; } }
+@media (min-width: 992px) {
+  #conversejs .navbar-expand-lg {
+    flex-flow: row nowrap;
+    justify-content: flex-start; }
+    #conversejs .navbar-expand-lg .navbar-nav {
+      flex-direction: row; }
+      #conversejs .navbar-expand-lg .navbar-nav .dropdown-menu {
+        position: absolute; }
+      #conversejs .navbar-expand-lg .navbar-nav .dropdown-menu-right {
+        right: 0;
+        left: auto; }
+      #conversejs .navbar-expand-lg .navbar-nav .nav-link {
+        padding-right: 0.5rem;
+        padding-left: 0.5rem; }
+    #conversejs .navbar-expand-lg > .container,
+    #conversejs .navbar-expand-lg > .container-fluid {
+      flex-wrap: nowrap; }
+    #conversejs .navbar-expand-lg .navbar-collapse {
+      display: flex !important;
+      flex-basis: auto; }
+    #conversejs .navbar-expand-lg .navbar-toggler {
+      display: none; }
+    #conversejs .navbar-expand-lg .dropup .dropdown-menu {
+      top: auto;
+      bottom: 100%; } }
+@media (max-width: 1199.98px) {
+  #conversejs .navbar-expand-xl > .container,
+  #conversejs .navbar-expand-xl > .container-fluid {
+    padding-right: 0;
+    padding-left: 0; } }
+@media (min-width: 1200px) {
+  #conversejs .navbar-expand-xl {
+    flex-flow: row nowrap;
+    justify-content: flex-start; }
+    #conversejs .navbar-expand-xl .navbar-nav {
+      flex-direction: row; }
+      #conversejs .navbar-expand-xl .navbar-nav .dropdown-menu {
+        position: absolute; }
+      #conversejs .navbar-expand-xl .navbar-nav .dropdown-menu-right {
+        right: 0;
+        left: auto; }
+      #conversejs .navbar-expand-xl .navbar-nav .nav-link {
+        padding-right: 0.5rem;
+        padding-left: 0.5rem; }
+    #conversejs .navbar-expand-xl > .container,
+    #conversejs .navbar-expand-xl > .container-fluid {
+      flex-wrap: nowrap; }
+    #conversejs .navbar-expand-xl .navbar-collapse {
+      display: flex !important;
+      flex-basis: auto; }
+    #conversejs .navbar-expand-xl .navbar-toggler {
+      display: none; }
+    #conversejs .navbar-expand-xl .dropup .dropdown-menu {
+      top: auto;
+      bottom: 100%; } }
+#conversejs .navbar-expand {
+  flex-flow: row nowrap;
+  justify-content: flex-start; }
+  #conversejs .navbar-expand > .container,
+  #conversejs .navbar-expand > .container-fluid {
+    padding-right: 0;
+    padding-left: 0; }
+  #conversejs .navbar-expand .navbar-nav {
+    flex-direction: row; }
+    #conversejs .navbar-expand .navbar-nav .dropdown-menu {
+      position: absolute; }
+    #conversejs .navbar-expand .navbar-nav .dropdown-menu-right {
+      right: 0;
+      left: auto; }
+    #conversejs .navbar-expand .navbar-nav .nav-link {
+      padding-right: 0.5rem;
+      padding-left: 0.5rem; }
+  #conversejs .navbar-expand > .container,
+  #conversejs .navbar-expand > .container-fluid {
+    flex-wrap: nowrap; }
+  #conversejs .navbar-expand .navbar-collapse {
+    display: flex !important;
+    flex-basis: auto; }
+  #conversejs .navbar-expand .navbar-toggler {
+    display: none; }
+  #conversejs .navbar-expand .dropup .dropdown-menu {
+    top: auto;
+    bottom: 100%; }
+#conversejs .navbar-light .navbar-brand {
+  color: rgba(0, 0, 0, 0.9); }
+  #conversejs .navbar-light .navbar-brand:hover, #conversejs .navbar-light .navbar-brand:focus {
+    color: rgba(0, 0, 0, 0.9); }
+#conversejs .navbar-light .navbar-nav .nav-link {
+  color: rgba(0, 0, 0, 0.5); }
+  #conversejs .navbar-light .navbar-nav .nav-link:hover, #conversejs .navbar-light .navbar-nav .nav-link:focus {
+    color: rgba(0, 0, 0, 0.7); }
+  #conversejs .navbar-light .navbar-nav .nav-link.disabled {
+    color: rgba(0, 0, 0, 0.3); }
+#conversejs .navbar-light .navbar-nav .show > .nav-link,
+#conversejs .navbar-light .navbar-nav .active > .nav-link,
+#conversejs .navbar-light .navbar-nav .nav-link.show,
+#conversejs .navbar-light .navbar-nav .nav-link.active {
+  color: rgba(0, 0, 0, 0.9); }
+#conversejs .navbar-light .navbar-toggler {
+  color: rgba(0, 0, 0, 0.5);
+  border-color: rgba(0, 0, 0, 0.1); }
+#conversejs .navbar-light .navbar-toggler-icon {
+  background-image: url("data:image/svg+xml;charset=utf8,%3Csvg viewBox='0 0 30 30' xmlns='http://www.w3.org/2000/svg'%3E%3Cpath stroke='rgba(0, 0, 0, 0.5)' stroke-width='2' stroke-linecap='round' stroke-miterlimit='10' d='M4 7h22M4 15h22M4 23h22'/%3E%3C/svg%3E"); }
+#conversejs .navbar-light .navbar-text {
+  color: rgba(0, 0, 0, 0.5); }
+  #conversejs .navbar-light .navbar-text a {
+    color: rgba(0, 0, 0, 0.9); }
+    #conversejs .navbar-light .navbar-text a:hover, #conversejs .navbar-light .navbar-text a:focus {
+      color: rgba(0, 0, 0, 0.9); }
+#conversejs .navbar-dark .navbar-brand {
+  color: #fff; }
+  #conversejs .navbar-dark .navbar-brand:hover, #conversejs .navbar-dark .navbar-brand:focus {
+    color: #fff; }
+#conversejs .navbar-dark .navbar-nav .nav-link {
+  color: rgba(255, 255, 255, 0.5); }
+  #conversejs .navbar-dark .navbar-nav .nav-link:hover, #conversejs .navbar-dark .navbar-nav .nav-link:focus {
+    color: rgba(255, 255, 255, 0.75); }
+  #conversejs .navbar-dark .navbar-nav .nav-link.disabled {
+    color: rgba(255, 255, 255, 0.25); }
+#conversejs .navbar-dark .navbar-nav .show > .nav-link,
+#conversejs .navbar-dark .navbar-nav .active > .nav-link,
+#conversejs .navbar-dark .navbar-nav .nav-link.show,
+#conversejs .navbar-dark .navbar-nav .nav-link.active {
+  color: #fff; }
+#conversejs .navbar-dark .navbar-toggler {
+  color: rgba(255, 255, 255, 0.5);
+  border-color: rgba(255, 255, 255, 0.1); }
+#conversejs .navbar-dark .navbar-toggler-icon {
+  background-image: url("data:image/svg+xml;charset=utf8,%3Csvg viewBox='0 0 30 30' xmlns='http://www.w3.org/2000/svg'%3E%3Cpath stroke='rgba(255, 255, 255, 0.5)' stroke-width='2' stroke-linecap='round' stroke-miterlimit='10' d='M4 7h22M4 15h22M4 23h22'/%3E%3C/svg%3E"); }
+#conversejs .navbar-dark .navbar-text {
+  color: rgba(255, 255, 255, 0.5); }
+  #conversejs .navbar-dark .navbar-text a {
+    color: #fff; }
+    #conversejs .navbar-dark .navbar-text a:hover, #conversejs .navbar-dark .navbar-text a:focus {
+      color: #fff; }
 #conversejs .card {
 #conversejs .card {
   position: relative;
   position: relative;
   display: flex;
   display: flex;
@@ -4582,11 +4897,11 @@
     background-color: #2d7f51; }
     background-color: #2d7f51; }
 #conversejs .badge-info {
 #conversejs .badge-info {
   color: #fff;
   color: #fff;
-  background-color: #17a2b8; }
+  background-color: #3AA569; }
   #conversejs .badge-info[href]:hover, #conversejs .badge-info[href]:focus {
   #conversejs .badge-info[href]:hover, #conversejs .badge-info[href]:focus {
     color: #fff;
     color: #fff;
     text-decoration: none;
     text-decoration: none;
-    background-color: #117a8b; }
+    background-color: #2d7f51; }
 #conversejs .badge-warning {
 #conversejs .badge-warning {
   color: #212529;
   color: #212529;
   background-color: #ffc107; }
   background-color: #ffc107; }
@@ -4658,13 +4973,13 @@
   #conversejs .alert-success .alert-link {
   #conversejs .alert-success .alert-link {
     color: #11301f; }
     color: #11301f; }
 #conversejs .alert-info {
 #conversejs .alert-info {
-  color: #0c5460;
-  background-color: #d1ecf1;
-  border-color: #bee5eb; }
+  color: #1e5637;
+  background-color: #d8ede1;
+  border-color: #c8e6d5; }
   #conversejs .alert-info hr {
   #conversejs .alert-info hr {
-    border-top-color: #abdde5; }
+    border-top-color: #b6dec8; }
   #conversejs .alert-info .alert-link {
   #conversejs .alert-info .alert-link {
-    color: #062c33; }
+    color: #11301f; }
 #conversejs .alert-warning {
 #conversejs .alert-warning {
   color: #856404;
   color: #856404;
   background-color: #fff3cd;
   background-color: #fff3cd;
@@ -4782,15 +5097,15 @@
     background-color: #1e5637;
     background-color: #1e5637;
     border-color: #1e5637; }
     border-color: #1e5637; }
 #conversejs .list-group-item-info {
 #conversejs .list-group-item-info {
-  color: #0c5460;
-  background-color: #bee5eb; }
+  color: #1e5637;
+  background-color: #c8e6d5; }
   #conversejs .list-group-item-info.list-group-item-action:hover, #conversejs .list-group-item-info.list-group-item-action:focus {
   #conversejs .list-group-item-info.list-group-item-action:hover, #conversejs .list-group-item-info.list-group-item-action:focus {
-    color: #0c5460;
-    background-color: #abdde5; }
+    color: #1e5637;
+    background-color: #b6dec8; }
   #conversejs .list-group-item-info.list-group-item-action.active {
   #conversejs .list-group-item-info.list-group-item-action.active {
     color: #fff;
     color: #fff;
-    background-color: #0c5460;
-    border-color: #0c5460; }
+    background-color: #1e5637;
+    border-color: #1e5637; }
 #conversejs .list-group-item-warning {
 #conversejs .list-group-item-warning {
   color: #856404;
   color: #856404;
   background-color: #ffeeba; }
   background-color: #ffeeba; }
@@ -5179,11 +5494,11 @@
 #conversejs button.bg-success:focus {
 #conversejs button.bg-success:focus {
   background-color: #2d7f51 !important; }
   background-color: #2d7f51 !important; }
 #conversejs .bg-info {
 #conversejs .bg-info {
-  background-color: #17a2b8 !important; }
+  background-color: #3AA569 !important; }
 #conversejs a.bg-info:hover, #conversejs a.bg-info:focus,
 #conversejs a.bg-info:hover, #conversejs a.bg-info:focus,
 #conversejs button.bg-info:hover,
 #conversejs button.bg-info:hover,
 #conversejs button.bg-info:focus {
 #conversejs button.bg-info:focus {
-  background-color: #117a8b !important; }
+  background-color: #2d7f51 !important; }
 #conversejs .bg-warning {
 #conversejs .bg-warning {
   background-color: #ffc107 !important; }
   background-color: #ffc107 !important; }
 #conversejs a.bg-warning:hover, #conversejs a.bg-warning:focus,
 #conversejs a.bg-warning:hover, #conversejs a.bg-warning:focus,
@@ -5239,7 +5554,7 @@
 #conversejs .border-success {
 #conversejs .border-success {
   border-color: #3AA569 !important; }
   border-color: #3AA569 !important; }
 #conversejs .border-info {
 #conversejs .border-info {
-  border-color: #17a2b8 !important; }
+  border-color: #3AA569 !important; }
 #conversejs .border-warning {
 #conversejs .border-warning {
   border-color: #ffc107 !important; }
   border-color: #ffc107 !important; }
 #conversejs .border-danger {
 #conversejs .border-danger {
@@ -6792,9 +7107,9 @@
 #conversejs a.text-success:hover, #conversejs a.text-success:focus {
 #conversejs a.text-success:hover, #conversejs a.text-success:focus {
   color: #2d7f51 !important; }
   color: #2d7f51 !important; }
 #conversejs .text-info {
 #conversejs .text-info {
-  color: #17a2b8 !important; }
+  color: #3AA569 !important; }
 #conversejs a.text-info:hover, #conversejs a.text-info:focus {
 #conversejs a.text-info:hover, #conversejs a.text-info:focus {
-  color: #117a8b !important; }
+  color: #2d7f51 !important; }
 #conversejs .text-warning {
 #conversejs .text-warning {
   color: #ffc107 !important; }
   color: #ffc107 !important; }
 #conversejs a.text-warning:hover, #conversejs a.text-warning:focus {
 #conversejs a.text-warning:hover, #conversejs a.text-warning:focus {
@@ -6868,6 +7183,8 @@ body.reset {
   font-size: 14px;
   font-size: 14px;
   direction: ltr;
   direction: ltr;
   z-index: 1031; }
   z-index: 1031; }
+  #conversejs .nopadding {
+    padding: 0 !important; }
   #conversejs.converse-overlayed > .row {
   #conversejs.converse-overlayed > .row {
     flex-direction: row-reverse; }
     flex-direction: row-reverse; }
   #conversejs.converse-fullscreen .converse-chatboxes, #conversejs.converse-mobile .converse-chatboxes {
   #conversejs.converse-fullscreen .converse-chatboxes, #conversejs.converse-mobile .converse-chatboxes {
@@ -7213,8 +7530,9 @@ body.reset {
 @media screen and (max-height: 450px) {
 @media screen and (max-height: 450px) {
   #conversejs {
   #conversejs {
     left: 0; } }
     left: 0; } }
-#conversejs form .form-group {
-  margin-bottom: 2em; }
+#conversejs .btn--small {
+  font-size: 80%;
+  font-weight: normal; }
 #conversejs form .form-check-label {
 #conversejs form .form-check-label {
   margin-top: 0.3rem; }
   margin-top: 0.3rem; }
 #conversejs form .form-control::-webkit-input-placeholder {
 #conversejs form .form-control::-webkit-input-placeholder {
@@ -7286,12 +7604,11 @@ body.reset {
       color: #79a5ba; }
       color: #79a5ba; }
     #conversejs form.converse-form .text-muted.error {
     #conversejs form.converse-form .text-muted.error {
       color: #A53214; }
       color: #A53214; }
+#conversejs form.converse-form--modal {
+  padding-bottom: 0; }
 #conversejs form.converse-centered-form {
 #conversejs form.converse-centered-form {
   text-align: center; }
   text-align: center; }
 
 
-#conversejs #user-profile-modal label {
-  font-weight: bold; }
-
 #conversejs .chatbox-navback {
 #conversejs .chatbox-navback {
   display: none; }
   display: none; }
 #conversejs .flyout {
 #conversejs .flyout {
@@ -7337,6 +7654,13 @@ body.reset {
     font-size: 80%; }
     font-size: 80%; }
   #conversejs .chat-head .chatbox-buttons {
   #conversejs .chat-head .chatbox-buttons {
     flex-direction: row-reverse;
     flex-direction: row-reverse;
+    position: relative;
+    width: 100%;
+    min-height: 1px;
+    padding-right: 15px;
+    padding-left: 15px;
+    flex: 0 0 25%;
+    max-width: 25%;
     padding: 0; }
     padding: 0; }
   #conversejs .chat-head .user-custom-message {
   #conversejs .chat-head .user-custom-message {
     color: #e7f7ee;
     color: #e7f7ee;
@@ -7843,10 +8167,6 @@ body.reset {
   padding: 0.3em 0;
   padding: 0.3em 0;
   clear: left;
   clear: left;
   width: 100%; }
   width: 100%; }
-#conversejs #converse-modals .set-xmpp-status {
-  margin: 1em; }
-  #conversejs #converse-modals .set-xmpp-status .custom-control-label {
-    margin-top: 0.25em; }
 #conversejs #controlbox {
 #conversejs #controlbox {
   margin-right: 1.5em; }
   margin-right: 1.5em; }
   #conversejs #controlbox .box-flyout {
   #conversejs #controlbox .box-flyout {
@@ -8247,6 +8567,35 @@ body.reset {
 
 
   #conversejs.converse-overlayed .converse-chatboxes .chatbox .box-flyout {
   #conversejs.converse-overlayed .converse-chatboxes .chatbox .box-flyout {
     margin-left: 30px; } }
     margin-left: 30px; } }
+#conversejs #converse-modals .set-xmpp-status {
+  margin: 1em; }
+  #conversejs #converse-modals .set-xmpp-status .custom-control-label {
+    margin-top: 0.25em; }
+#conversejs #converse-modals #omemo-tabpanel {
+  margin-top: 1em; }
+#conversejs #converse-modals .btn {
+  font-weight: normal; }
+#conversejs #converse-modals #user-profile-modal .profile-form label {
+  font-weight: bold; }
+#conversejs #converse-modals #user-profile-modal .fingerprint-removal label {
+  display: flex;
+  padding: 0.75rem 1.25rem; }
+#conversejs #converse-modals #user-profile-modal .list-group-item {
+  display: flex;
+  justify-content: left;
+  font-size: 95%; }
+  #conversejs #converse-modals #user-profile-modal .list-group-item input[type="checkbox"] {
+    margin-right: 1em; }
+#conversejs #converse-modals .fingerprints {
+  width: 100%;
+  margin-bottom: 1em; }
+#conversejs #converse-modals .fingerprint-trust {
+  display: flex;
+  justify-content: space-between;
+  font-size: 95%; }
+  #conversejs #converse-modals .fingerprint-trust .fingerprint {
+    margin-left: 1em; }
+
 #conversejs #converse-roster {
 #conversejs #converse-roster {
   text-align: left;
   text-align: left;
   width: 100%;
   width: 100%;

+ 1 - 0
dev.html

@@ -11,6 +11,7 @@
     <link rel="shortcut icon" type="image/ico" href="css/images/favicon.ico"/>
     <link rel="shortcut icon" type="image/ico" href="css/images/favicon.ico"/>
     <link type="text/css" rel="stylesheet" media="screen" href="css/fullpage.css" />
     <link type="text/css" rel="stylesheet" media="screen" href="css/fullpage.css" />
     <link type="text/css" rel="stylesheet" media="screen" href="css/converse.css" />
     <link type="text/css" rel="stylesheet" media="screen" href="css/converse.css" />
+    <script src="3rdparty/libsignal-protocol-javascript/dist/libsignal-protocol.js"></script>
     <script src="dist/converse.js"></script>
     <script src="dist/converse.js"></script>
 </head>
 </head>
 
 

File diff suppressed because it is too large
+ 591 - 332
dist/converse.js


+ 1 - 1
locale/af/LC_MESSAGES/converse.po

@@ -8,7 +8,7 @@ msgstr ""
 "Project-Id-Version: Converse.js 0.4\n"
 "Project-Id-Version: Converse.js 0.4\n"
 "Report-Msgid-Bugs-To: \n"
 "Report-Msgid-Bugs-To: \n"
 "POT-Creation-Date: 2018-07-22 11:17+0200\n"
 "POT-Creation-Date: 2018-07-22 11:17+0200\n"
-"PO-Revision-Date: 2018-07-22 12:12+0200\n"
+"PO-Revision-Date: 2018-07-22 15:37+0200\n"
 "Last-Translator: JC Brand <jc@opkode.com>\n"
 "Last-Translator: JC Brand <jc@opkode.com>\n"
 "Language-Team: Afrikaans <https://hosted.weblate.org/projects/conversejs/"
 "Language-Team: Afrikaans <https://hosted.weblate.org/projects/conversejs/"
 "translations/af/>\n"
 "translations/af/>\n"

+ 21 - 0
mockup/chatbox.html

@@ -134,12 +134,33 @@
                                     <span class="chat-msg__heading">
                                     <span class="chat-msg__heading">
                                         <span class="chat-msg__author">Juliet Capulet</span>
                                         <span class="chat-msg__author">Juliet Capulet</span>
                                         <span class="chat-msg__time">15:31</span>
                                         <span class="chat-msg__time">15:31</span>
+                                        <span class="fa fa-lock"></span>
                                     </span>
                                     </span>
                                     <div class="chat-msg__body">
                                     <div class="chat-msg__body">
                                         <div class="chat-msg__message">
                                         <div class="chat-msg__message">
                                             <span class="chat-msg__text">
                                             <span class="chat-msg__text">
                                             O Romeo, Romeo! wherefore art thou Romeo?
                                             O Romeo, Romeo! wherefore art thou Romeo?
                                             Deny thy father and refuse thy name;
                                             Deny thy father and refuse thy name;
+                                            </span>
+                                        </div>
+                                    </div>
+                                </div>
+                                <div class="chat-msg__actions">
+                                    <button class="chat-msg__action fa fa-pencil" title="Edit this message">&nbsp;</button>
+                                </div>
+                            </div>
+
+                            <div class="message chat-msg chat-msg--followup">
+                                <canvas height="36" width="36" class="avatar chat-msg__avatar"></canvas>
+                                <div class="chat-msg__content">
+                                    <span class="chat-msg__heading">
+                                        <span class="chat-msg__author">Juliet Capulet</span>
+                                        <span class="chat-msg__time">15:31</span>
+                                        <span class="fa fa-lock"></span>
+                                    </span>
+                                    <div class="chat-msg__body">
+                                        <div class="chat-msg__message">
+                                            <span class="chat-msg__text">
                                             Or, if thou wilt not, be but sworn my love,
                                             Or, if thou wilt not, be but sworn my love,
                                             And I'll no longer be a Capulet.
                                             And I'll no longer be a Capulet.
                                             </span>
                                             </span>

File diff suppressed because it is too large
+ 385 - 857
package-lock.json


+ 3 - 0
package.json

@@ -46,6 +46,7 @@
     "bootstrap": "^4.0.0",
     "bootstrap": "^4.0.0",
     "bootstrap.native": "^2.0.23",
     "bootstrap.native": "^2.0.23",
     "bourbon": "^4.3.2",
     "bourbon": "^4.3.2",
+    "bytebuffer": "^3.5.5",
     "clean-css-cli": "^4.0.10",
     "clean-css-cli": "^4.0.10",
     "emojione": "^3.0.3",
     "emojione": "^3.0.3",
     "es6-promise": "^4.1.0",
     "es6-promise": "^4.1.0",
@@ -65,6 +66,7 @@
     "jshint": "^2.9.4",
     "jshint": "^2.9.4",
     "lodash": "4.17.4",
     "lodash": "4.17.4",
     "lodash-template-loader": "^2.0.0",
     "lodash-template-loader": "^2.0.0",
+    "long": "^3.1.0",
     "lodash-template-webpack-loader": "jcbrand/lodash-template-webpack-loader",
     "lodash-template-webpack-loader": "jcbrand/lodash-template-webpack-loader",
     "minimist": "^1.2.0",
     "minimist": "^1.2.0",
     "moment": "~> 2.19.3 ",
     "moment": "~> 2.19.3 ",
@@ -72,6 +74,7 @@
     "otr": "0.2.16",
     "otr": "0.2.16",
     "pluggable.js": "2.0.0",
     "pluggable.js": "2.0.0",
     "po2json": "^0.4.4",
     "po2json": "^0.4.4",
+    "protobufjs": "5.0.1",
     "requirejs": "2.3.5",
     "requirejs": "2.3.5",
     "run-headless-chromium": "^0.1.1",
     "run-headless-chromium": "^0.1.1",
     "sinon": "^2.1.0",
     "sinon": "^2.1.0",

+ 2 - 0
sass/_chatbox.scss

@@ -61,6 +61,8 @@
         }
         }
         .chatbox-buttons {
         .chatbox-buttons {
             flex-direction: row-reverse;
             flex-direction: row-reverse;
+            @include make-col-ready();
+            @include make-col(3);
             padding: 0;
             padding: 0;
         }
         }
 
 

+ 0 - 9
sass/_controlbox.scss

@@ -60,15 +60,6 @@
         width: 100%;
         width: 100%;
     }
     }
 
 
-    #converse-modals {
-        .set-xmpp-status {
-            margin: 1em;
-            .custom-control-label {
-                margin-top: 0.25em;
-            }
-        }
-    }
-
     #controlbox {
     #controlbox {
         .box-flyout {
         .box-flyout {
             background-color: white;
             background-color: white;

+ 4 - 0
sass/_core.scss

@@ -61,6 +61,10 @@ body.reset {
     direction: ltr;
     direction: ltr;
     z-index: 1031; // One more than bootstrap navbar
     z-index: 1031; // One more than bootstrap navbar
 
 
+    .nopadding {
+        padding: 0 !important;
+    }
+
     &.converse-overlayed {
     &.converse-overlayed {
         > .row {
         > .row {
             flex-direction: row-reverse;
             flex-direction: row-reverse;

+ 10 - 4
sass/_forms.scss

@@ -1,9 +1,10 @@
 #conversejs {
 #conversejs {
-    form {
-        .form-group {
-            margin-bottom: 2em;
-        }
+    .btn--small {
+        font-size: 80%;
+        font-weight: normal;
+    }
 
 
+    form {
         .form-check-label {
         .form-check-label {
             margin-top: $form-check-input-margin-y;
             margin-top: $form-check-input-margin-y;
         }
         }
@@ -103,6 +104,11 @@
                 }
                 }
             }
             }
         }
         }
+
+        &.converse-form--modal {
+            padding-bottom: 0;
+        }
+
         &.converse-centered-form {
         &.converse-centered-form {
             text-align: center;
             text-align: center;
         }
         }

+ 56 - 0
sass/_modal.scss

@@ -0,0 +1,56 @@
+#conversejs {
+    #converse-modals {
+        .set-xmpp-status {
+            margin: 1em;
+            .custom-control-label {
+                margin-top: 0.25em;
+            }
+        }
+
+        #omemo-tabpanel {
+            margin-top: 1em;
+        }
+
+        .btn {
+            font-weight: normal;
+        }
+
+        #user-profile-modal {
+            .profile-form {
+                label {
+                    font-weight: bold;
+                }
+            }
+            .fingerprint-removal {
+                label {
+                    display: flex;
+                    padding: 0.75rem 1.25rem;
+                }
+            }
+
+            .list-group-item {
+                display: flex;
+                justify-content: left;
+                font-size: 95%;
+
+                input[type="checkbox"] {
+                    margin-right: 1em;
+                }
+            }
+        }
+
+        .fingerprints {
+            width: 100%;
+            margin-bottom: 1em;
+        }
+        
+        .fingerprint-trust {
+            display: flex;
+            justify-content: space-between;
+            font-size: 95%;
+            .fingerprint {
+                margin-left: 1em;
+            }
+        }
+    }
+}

+ 0 - 7
sass/_profile.scss

@@ -1,7 +0,0 @@
-#conversejs {
-    #user-profile-modal {
-        label {
-            font-weight: bold;
-        }
-    }
-}

+ 2 - 0
sass/_variables.scss

@@ -34,6 +34,8 @@ $green: #3AA569;
 $dark-green: #1E9652;
 $dark-green: #1E9652;
 $darkest-green: #0E763B;
 $darkest-green: #0E763B;
 
 
+$info:  $green !default;
+
 $lightest-green: #E7FBF0;
 $lightest-green: #E7FBF0;
 $light-green: #5CBC86;
 $light-green: #5CBC86;
 $green: #3AA569;
 $green: #3AA569;

+ 3 - 1
sass/converse.scss

@@ -26,6 +26,8 @@
     @import "bootstrap/scss/button-group";
     @import "bootstrap/scss/button-group";
     @import "bootstrap/scss/input-group";
     @import "bootstrap/scss/input-group";
     @import "bootstrap/scss/custom-forms";
     @import "bootstrap/scss/custom-forms";
+    @import "bootstrap/scss/nav";
+    @import "bootstrap/scss/navbar";
     @import "bootstrap/scss/card";
     @import "bootstrap/scss/card";
     @import "bootstrap/scss/breadcrumb";
     @import "bootstrap/scss/breadcrumb";
     @import "bootstrap/scss/badge";
     @import "bootstrap/scss/badge";
@@ -40,9 +42,9 @@
 }
 }
 @import "core";
 @import "core";
 @import "forms";
 @import "forms";
-@import "profile";
 @import "chatbox";
 @import "chatbox";
 @import "controlbox";
 @import "controlbox";
+@import "modal";
 @import "roster";
 @import "roster";
 @import "lists";
 @import "lists";
 @import "chatrooms";
 @import "chatrooms";

+ 47 - 45
spec/bookmarks.js

@@ -314,52 +314,54 @@
                 [{'category': 'pubsub', 'type': 'pep'}],
                 [{'category': 'pubsub', 'type': 'pep'}],
                 ['http://jabber.org/protocol/pubsub#publish-options']
                 ['http://jabber.org/protocol/pubsub#publish-options']
             ).then(function () {
             ).then(function () {
-                test_utils.waitUntil(function () {
-                    return _converse.bookmarks;
-                }, 300).then(function () {
-                    /* The stored data is automatically pushed to all of the user's
-                    * connected resources.
-                    *
-                    * Publisher receives event notification
-                    * -------------------------------------
-                    * <message from='juliet@capulet.lit'
-                    *         to='juliet@capulet.lit/balcony'
-                    *         type='headline'
-                    *         id='rnfoo1'>
-                    * <event xmlns='http://jabber.org/protocol/pubsub#event'>
-                    *     <items node='storage:bookmarks'>
-                    *     <item id='current'>
-                    *         <storage xmlns='storage:bookmarks'>
-                    *         <conference name='The Play&apos;s the Thing'
-                    *                     autojoin='true'
-                    *                     jid='theplay@conference.shakespeare.lit'>
-                    *             <nick>JC</nick>
-                    *         </conference>
-                    *         </storage>
-                    *     </item>
-                    *     </items>
-                    * </event>
-                    * </message>
-                    */
-                    var stanza = $msg({
-                        'from': 'dummy@localhost',
-                        'to': 'dummy@localhost/resource',
-                        'type': 'headline',
-                        'id': 'rnfoo1'
-                    }).c('event', {'xmlns': 'http://jabber.org/protocol/pubsub#event'})
-                        .c('items', {'node': 'storage:bookmarks'})
-                            .c('item', {'id': 'current'})
-                                .c('storage', {'xmlns': 'storage:bookmarks'})
-                                    .c('conference', {'name': 'The Play&apos;s the Thing',
-                                                    'autojoin': 'true',
-                                                    'jid':'theplay@conference.shakespeare.lit'})
-                                        .c('nick').t('JC');
+                return test_utils.waitUntil(() => _converse.bookmarks);
+            }).then(function () {
+                // Emit here instead of mocking fetching of bookmarks.
+                _converse.emit('bookmarksInitialized');
 
 
-                    _converse.connection._dataRecv(test_utils.createRequest(stanza));
-                    expect(_converse.bookmarks.length).toBe(1);
-                    expect(_converse.chatboxviews.get('theplay@conference.shakespeare.lit')).not.toBeUndefined();
-                    done();
-                });
+               /* The stored data is automatically pushed to all of the user's
+                * connected resources.
+                *
+                * Publisher receives event notification
+                * -------------------------------------
+                * <message from='juliet@capulet.lit'
+                *         to='juliet@capulet.lit/balcony'
+                *         type='headline'
+                *         id='rnfoo1'>
+                * <event xmlns='http://jabber.org/protocol/pubsub#event'>
+                *     <items node='storage:bookmarks'>
+                *     <item id='current'>
+                *         <storage xmlns='storage:bookmarks'>
+                *         <conference name='The Play&apos;s the Thing'
+                *                     autojoin='true'
+                *                     jid='theplay@conference.shakespeare.lit'>
+                *             <nick>JC</nick>
+                *         </conference>
+                *         </storage>
+                *     </item>
+                *     </items>
+                * </event>
+                * </message>
+                */
+               var stanza = $msg({
+                   'from': 'dummy@localhost',
+                   'to': 'dummy@localhost/resource',
+                   'type': 'headline',
+                   'id': 'rnfoo1'
+               }).c('event', {'xmlns': 'http://jabber.org/protocol/pubsub#event'})
+                   .c('items', {'node': 'storage:bookmarks'})
+                       .c('item', {'id': 'current'})
+                           .c('storage', {'xmlns': 'storage:bookmarks'})
+                               .c('conference', {'name': 'The Play&apos;s the Thing',
+                                               'autojoin': 'true',
+                                               'jid':'theplay@conference.shakespeare.lit'})
+                                   .c('nick').t('JC');
+               _converse.connection._dataRecv(test_utils.createRequest(stanza));
+                return test_utils.waitUntil(() => _converse.bookmarks.length);
+            }).then(function () {
+               expect(_converse.bookmarks.length).toBe(1);
+               expect(_converse.chatboxviews.get('theplay@conference.shakespeare.lit')).not.toBeUndefined();
+               done();
             });
             });
         }));
         }));
 
 

+ 161 - 122
spec/chatbox.js

@@ -27,10 +27,11 @@
                 _converse.emit('rosterContactsFetched');
                 _converse.emit('rosterContactsFetched');
                 test_utils.openControlBox();
                 test_utils.openControlBox();
 
 
+                let view;
                 const contact_jid = mock.cur_names[0].replace(/ /g,'.').toLowerCase() + '@localhost';
                 const contact_jid = mock.cur_names[0].replace(/ /g,'.').toLowerCase() + '@localhost';
-                test_utils.openChatBoxFor(_converse, contact_jid);
-                test_utils.waitUntil(() => _converse.chatboxes.length == 2).then(() => {
-                    var view = _converse.chatboxviews.get(contact_jid);
+                test_utils.openChatBoxFor(_converse, contact_jid)
+                .then(() => {
+                    view = _converse.chatboxviews.get(contact_jid);
                     test_utils.sendMessage(view, '/help');
                     test_utils.sendMessage(view, '/help');
 
 
                     const info_messages = Array.prototype.slice.call(view.el.querySelectorAll('.chat-info:not(.chat-date)'), 0);
                     const info_messages = Array.prototype.slice.call(view.el.querySelectorAll('.chat-info:not(.chat-date)'), 0);
@@ -39,13 +40,15 @@
                     expect(info_messages.pop().textContent).toBe('/me: Write in the third person');
                     expect(info_messages.pop().textContent).toBe('/me: Write in the third person');
                     expect(info_messages.pop().textContent).toBe('/clear: Remove messages');
                     expect(info_messages.pop().textContent).toBe('/clear: Remove messages');
 
 
-                    var msg = $msg({
+                    const msg = $msg({
                             from: contact_jid,
                             from: contact_jid,
                             to: _converse.connection.jid,
                             to: _converse.connection.jid,
                             type: 'chat',
                             type: 'chat',
                             id: (new Date()).getTime()
                             id: (new Date()).getTime()
                         }).c('body').t('hello world').tree();
                         }).c('body').t('hello world').tree();
                     _converse.chatboxes.onMessage(msg);
                     _converse.chatboxes.onMessage(msg);
+                    return test_utils.waitUntil(() => view.content.querySelectorAll('.chat-msg').length);
+                }).then(() => {
                     expect(view.content.lastElementChild.textContent.trim().indexOf('hello world')).not.toBe(-1);
                     expect(view.content.lastElementChild.textContent.trim().indexOf('hello world')).not.toBe(-1);
                     done();
                     done();
                 });
                 });
@@ -618,10 +621,10 @@
                             expect(view.model.get('chat_state')).toBe('inactive');
                             expect(view.model.get('chat_state')).toBe('inactive');
                             spyOn(_converse.connection, 'send');
                             spyOn(_converse.connection, 'send');
                             view.model.maximize();
                             view.model.maximize();
-                            return test_utils.waitUntil(() => view.model.get('chat_state') === 'active', 700);
+                            return test_utils.waitUntil(() => view.model.get('chat_state') === 'active', 1000);
                         }).then(() => {
                         }).then(() => {
                             expect(_converse.connection.send).toHaveBeenCalled();
                             expect(_converse.connection.send).toHaveBeenCalled();
-                            var calls = _.filter(_converse.connection.send.calls.all(), function (call) {
+                            const calls = _.filter(_converse.connection.send.calls.all(), function (call) {
                                 return call.args[0] instanceof Strophe.Builder;
                                 return call.args[0] instanceof Strophe.Builder;
                             });
                             });
                             expect(calls.length).toBe(1);
                             expect(calls.length).toBe(1);
@@ -632,7 +635,7 @@
                             expect($stanza.children().get(1).tagName).toBe('no-store');
                             expect($stanza.children().get(1).tagName).toBe('no-store');
                             expect($stanza.children().get(2).tagName).toBe('no-permanent-store');
                             expect($stanza.children().get(2).tagName).toBe('no-permanent-store');
                             done();
                             done();
-                        });
+                        }).catch(_.partial(_converse.log, _, Strophe.LogLevel.FATAL))
                     }));
                     }));
                 });
                 });
 
 
@@ -741,7 +744,7 @@
                             spyOn(_converse, 'log');
                             spyOn(_converse, 'log');
                             recipient_jid = mock.cur_names[5].replace(/ /g,'.').toLowerCase() + '@localhost';
                             recipient_jid = mock.cur_names[5].replace(/ /g,'.').toLowerCase() + '@localhost';
                             return test_utils.openChatBoxFor(_converse, recipient_jid);
                             return test_utils.openChatBoxFor(_converse, recipient_jid);
-                        }).then(() => {
+                        }).then((view) => {
                             var msg = $msg({
                             var msg = $msg({
                                     'from': _converse.bare_jid,
                                     'from': _converse.bare_jid,
                                     'id': (new Date()).getTime(),
                                     'id': (new Date()).getTime(),
@@ -757,7 +760,8 @@
                                         'type': 'chat'
                                         'type': 'chat'
                                 }).c('composing', {'xmlns': Strophe.NS.CHATSTATES}).tree();
                                 }).c('composing', {'xmlns': Strophe.NS.CHATSTATES}).tree();
                             _converse.chatboxes.onMessage(msg);
                             _converse.chatboxes.onMessage(msg);
-
+                            return test_utils.waitUntil(() => view.model.messages.length);
+                        }).then(() => {
                             // Check that the chatbox and its view now exist
                             // Check that the chatbox and its view now exist
                             var chatbox = _converse.chatboxes.get(recipient_jid);
                             var chatbox = _converse.chatboxes.get(recipient_jid);
                             var chatboxview = _converse.chatboxviews.get(recipient_jid);
                             var chatboxview = _converse.chatboxviews.get(recipient_jid);
@@ -886,7 +890,7 @@
                             spyOn(_converse, 'log');
                             spyOn(_converse, 'log');
                             recipient_jid = mock.cur_names[5].replace(/ /g,'.').toLowerCase() + '@localhost';
                             recipient_jid = mock.cur_names[5].replace(/ /g,'.').toLowerCase() + '@localhost';
                             return test_utils.openChatBoxFor(_converse, recipient_jid);
                             return test_utils.openChatBoxFor(_converse, recipient_jid);
-                        }).then(() => {
+                        }).then((view) => {
                             var msg = $msg({
                             var msg = $msg({
                                     'from': _converse.bare_jid,
                                     'from': _converse.bare_jid,
                                     'id': (new Date()).getTime(),
                                     'id': (new Date()).getTime(),
@@ -902,7 +906,8 @@
                                         'type': 'chat'
                                         'type': 'chat'
                                 }).c('paused', {'xmlns': Strophe.NS.CHATSTATES}).tree();
                                 }).c('paused', {'xmlns': Strophe.NS.CHATSTATES}).tree();
                             _converse.chatboxes.onMessage(msg);
                             _converse.chatboxes.onMessage(msg);
-
+                            return test_utils.waitUntil(() => view.model.messages.length);
+                        }).then(() => {
                             // Check that the chatbox and its view now exist
                             // Check that the chatbox and its view now exist
                             var chatbox = _converse.chatboxes.get(recipient_jid);
                             var chatbox = _converse.chatboxes.get(recipient_jid);
                             var chatboxview = _converse.chatboxviews.get(recipient_jid);
                             var chatboxview = _converse.chatboxviews.get(recipient_jid);
@@ -936,11 +941,11 @@
                         let view;
                         let view;
                         const contact_jid = mock.cur_names[0].replace(/ /g,'.').toLowerCase() + '@localhost';
                         const contact_jid = mock.cur_names[0].replace(/ /g,'.').toLowerCase() + '@localhost';
                         test_utils.openControlBox();
                         test_utils.openControlBox();
-                        test_utils.waitUntil(() => _converse.rosterview.el.querySelectorAll('.roster-group').length, 500)
+                        test_utils.waitUntil(() => _converse.rosterview.el.querySelectorAll('.roster-group').length, 1000)
                         .then(() => test_utils.openChatBoxFor(_converse, contact_jid))
                         .then(() => test_utils.openChatBoxFor(_converse, contact_jid))
                         .then(() => {
                         .then(() => {
                             view = _converse.chatboxviews.get(contact_jid);
                             view = _converse.chatboxviews.get(contact_jid);
-                            return test_utils.waitUntil(() => view.model.get('chat_state') === 'active', 500);
+                            return test_utils.waitUntil(() => view.model.get('chat_state') === 'active', 1000);
                         }).then(() => {
                         }).then(() => {
                             console.log('chat_state set to active');
                             console.log('chat_state set to active');
                             expect(view.model.get('chat_state')).toBe('active');
                             expect(view.model.get('chat_state')).toBe('active');
@@ -954,10 +959,9 @@
                             view = _converse.chatboxviews.get(contact_jid);
                             view = _converse.chatboxviews.get(contact_jid);
                             expect(view.model.get('chat_state')).toBe('composing');
                             expect(view.model.get('chat_state')).toBe('composing');
                             spyOn(_converse.connection, 'send');
                             spyOn(_converse.connection, 'send');
-                            return test_utils.waitUntil(() => view.model.get('chat_state') === 'paused', 500);
-                        }).then(() => {
-                            return test_utils.waitUntil(() => view.model.get('chat_state') === 'inactive', 500);
-                        }).then(() => {
+                            return test_utils.waitUntil(() => view.model.get('chat_state') === 'paused', 1000);
+                        }).then(() => test_utils.waitUntil(() => view.model.get('chat_state') === 'inactive', 1000))
+                          .then(() => {
                             console.log('chat_state set to inactive');
                             console.log('chat_state set to inactive');
                             expect(_converse.connection.send).toHaveBeenCalled();
                             expect(_converse.connection.send).toHaveBeenCalled();
                             var calls = _.filter(_converse.connection.send.calls.all(), function (call) {
                             var calls = _.filter(_converse.connection.send.calls.all(), function (call) {
@@ -1041,17 +1045,18 @@
                         _converse.emit('rosterContactsFetched');
                         _converse.emit('rosterContactsFetched');
                         test_utils.openControlBox();
                         test_utils.openControlBox();
                         const sender_jid = mock.cur_names[1].replace(/ /g,'.').toLowerCase() + '@localhost';
                         const sender_jid = mock.cur_names[1].replace(/ /g,'.').toLowerCase() + '@localhost';
+                        let view;
 
 
                         // See XEP-0085 http://xmpp.org/extensions/xep-0085.html#definitions
                         // See XEP-0085 http://xmpp.org/extensions/xep-0085.html#definitions
                         spyOn(_converse, 'emit');
                         spyOn(_converse, 'emit');
                         test_utils.openChatBoxFor(_converse, sender_jid)
                         test_utils.openChatBoxFor(_converse, sender_jid)
                         .then(() => {
                         .then(() => {
-                            var view = _converse.chatboxviews.get(sender_jid);
+                            view = _converse.chatboxviews.get(sender_jid);
                             expect(view.el.querySelectorAll('.chat-event').length).toBe(0);
                             expect(view.el.querySelectorAll('.chat-event').length).toBe(0);
                             // Insert <composing> message, to also check that
                             // Insert <composing> message, to also check that
                             // text messages are inserted correctly with
                             // text messages are inserted correctly with
                             // temporary chat events in the chat contents.
                             // temporary chat events in the chat contents.
-                            var msg = $msg({
+                            const msg = $msg({
                                     'to': _converse.bare_jid,
                                     'to': _converse.bare_jid,
                                     'xmlns': 'jabber:client',
                                     'xmlns': 'jabber:client',
                                     'from': sender_jid,
                                     'from': sender_jid,
@@ -1059,14 +1064,18 @@
                                 .c('composing', {'xmlns': Strophe.NS.CHATSTATES}).up()
                                 .c('composing', {'xmlns': Strophe.NS.CHATSTATES}).up()
                                 .tree();
                                 .tree();
                             _converse.chatboxes.onMessage(msg);
                             _converse.chatboxes.onMessage(msg);
+                            return test_utils.waitUntil(() => view.model.messages.length);
+                        }).then(() => {
                             expect(view.el.querySelectorAll('.chat-state-notification').length).toBe(1);
                             expect(view.el.querySelectorAll('.chat-state-notification').length).toBe(1);
-                            msg = $msg({
+                            const msg = $msg({
                                     from: sender_jid,
                                     from: sender_jid,
                                     to: _converse.connection.jid,
                                     to: _converse.connection.jid,
                                     type: 'chat',
                                     type: 'chat',
                                     id: (new Date()).getTime()
                                     id: (new Date()).getTime()
                                 }).c('body').c('inactive', {'xmlns': Strophe.NS.CHATSTATES}).tree();
                                 }).c('body').c('inactive', {'xmlns': Strophe.NS.CHATSTATES}).tree();
                             _converse.chatboxes.onMessage(msg);
                             _converse.chatboxes.onMessage(msg);
+                            return test_utils.waitUntil(() => (view.model.messages.length > 1));
+                        }).then(() => {
                             expect(_converse.emit).toHaveBeenCalledWith('message', jasmine.any(Object));
                             expect(_converse.emit).toHaveBeenCalledWith('message', jasmine.any(Object));
                             expect($(view.el).find('.chat-state-notification').length).toBe(0);
                             expect($(view.el).find('.chat-state-notification').length).toBe(0);
                             done();
                             done();
@@ -1178,12 +1187,15 @@
                       .c('active', {'xmlns': Strophe.NS.CHATSTATES}).tree();
                       .c('active', {'xmlns': Strophe.NS.CHATSTATES}).tree();
                 _converse.windowState = 'hidden';
                 _converse.windowState = 'hidden';
                 _converse.chatboxes.onMessage(msg);
                 _converse.chatboxes.onMessage(msg);
-                expect(_converse.incrementMsgCounter).toHaveBeenCalled();
-                expect(_converse.clearMsgCounter).not.toHaveBeenCalled();
-                expect(_converse.msg_counter).toBe(1);
-                expect(_converse.emit).toHaveBeenCalledWith('message', jasmine.any(Object));
-                _converse.windowSate = previous_state;
-                done();
+                return test_utils.waitUntil(() => _converse.api.chats.get().length)
+                .then(() => {
+                    expect(_converse.incrementMsgCounter).toHaveBeenCalled();
+                    expect(_converse.clearMsgCounter).not.toHaveBeenCalled();
+                    expect(_converse.msg_counter).toBe(1);
+                    expect(_converse.emit).toHaveBeenCalledWith('message', jasmine.any(Object));
+                    _converse.windowSate = previous_state;
+                    done();
+                });
             }));
             }));
 
 
             it("is cleared when the window is focused",
             it("is cleared when the window is focused",
@@ -1237,7 +1249,8 @@
                 // initial state
                 // initial state
                 expect(_converse.msg_counter).toBe(0);
                 expect(_converse.msg_counter).toBe(0);
 
 
-                var message = 'This message will always increment the message counter from zero',
+                let view;
+                const message = 'This message will always increment the message counter from zero',
                     sender_jid = mock.cur_names[0].replace(/ /g,'.').toLowerCase() + '@localhost',
                     sender_jid = mock.cur_names[0].replace(/ /g,'.').toLowerCase() + '@localhost',
                     msgFactory = function () {
                     msgFactory = function () {
                         return $msg({
                         return $msg({
@@ -1254,24 +1267,29 @@
                 // leave converse-chat page
                 // leave converse-chat page
                 _converse.windowState = 'hidden';
                 _converse.windowState = 'hidden';
                 _converse.chatboxes.onMessage(msgFactory());
                 _converse.chatboxes.onMessage(msgFactory());
-                expect(_converse.msg_counter).toBe(1);
+                return test_utils.waitUntil(() => _converse.api.chats.get().length)
+                .then(() => {
+                    expect(_converse.msg_counter).toBe(1);
 
 
-                // come back to converse-chat page
-                _converse.saveWindowState(null, 'focus');
-                var view = _converse.chatboxviews.get(sender_jid);
-                expect(u.isVisible(view.el)).toBeTruthy();
-                expect(_converse.msg_counter).toBe(0);
+                    // come back to converse-chat page
+                    _converse.saveWindowState(null, 'focus');
+                    view = _converse.chatboxviews.get(sender_jid);
+                    expect(u.isVisible(view.el)).toBeTruthy();
+                    expect(_converse.msg_counter).toBe(0);
 
 
-                // close chatbox and leave converse-chat page again
-                view.close();
-                _converse.windowState = 'hidden';
+                    // close chatbox and leave converse-chat page again
+                    view.close();
+                    _converse.windowState = 'hidden';
 
 
-                // check that msg_counter is incremented from zero again
-                _converse.chatboxes.onMessage(msgFactory());
-                view = _converse.chatboxviews.get(sender_jid);
-                expect(u.isVisible(view.el)).toBeTruthy();
-                expect(_converse.msg_counter).toBe(1);
-                done();
+                    // check that msg_counter is incremented from zero again
+                    _converse.chatboxes.onMessage(msgFactory());
+                    return test_utils.waitUntil(() => _converse.api.chats.get().length)
+                }).then(() => {
+                    view = _converse.chatboxviews.get(sender_jid);
+                    expect(u.isVisible(view.el)).toBeTruthy();
+                    expect(_converse.msg_counter).toBe(1);
+                    done();
+                });
             }));
             }));
         });
         });
 
 
@@ -1288,13 +1306,17 @@
                 const sender_jid = mock.cur_names[0].replace(/ /g,'.').toLowerCase() + '@localhost',
                 const sender_jid = mock.cur_names[0].replace(/ /g,'.').toLowerCase() + '@localhost',
                       msg = test_utils.createChatMessage(_converse, sender_jid, 'This message will be unread');
                       msg = test_utils.createChatMessage(_converse, sender_jid, 'This message will be unread');
 
 
+                let view;
                 test_utils.openChatBoxFor(_converse, sender_jid)
                 test_utils.openChatBoxFor(_converse, sender_jid)
-                .then((view) => {
+                .then((v) => {
+                    view = v;
                     view.model.save('scrolled', true);
                     view.model.save('scrolled', true);
                     _converse.chatboxes.onMessage(msg);
                     _converse.chatboxes.onMessage(msg);
+                    return test_utils.waitUntil(() => view.model.messages.length);
+                }).then(() => {
                     expect(view.model.get('num_unread')).toBe(1);
                     expect(view.model.get('num_unread')).toBe(1);
                     done();
                     done();
-                });
+                }).catch(_.partial(_converse.log, _, Strophe.LogLevel.FATAL))
             }));
             }));
 
 
             it("is not incremented when the message is received and ChatBoxView is scrolled down",
             it("is not incremented when the message is received and ChatBoxView is scrolled down",
@@ -1323,15 +1345,18 @@
                 test_utils.createContacts(_converse, 'current');
                 test_utils.createContacts(_converse, 'current');
                 _converse.emit('rosterContactsFetched');
                 _converse.emit('rosterContactsFetched');
 
 
-                var sender_jid = mock.cur_names[0].replace(/ /g,'.').toLowerCase() + '@localhost';
-                var msgFactory = function () {
+                let chatbox;
+                const sender_jid = mock.cur_names[0].replace(/ /g,'.').toLowerCase() + '@localhost';
+                const msgFactory = function () {
                     return test_utils.createChatMessage(_converse, sender_jid, 'This message will be unread');
                     return test_utils.createChatMessage(_converse, sender_jid, 'This message will be unread');
                 };
                 };
                 test_utils.openChatBoxFor(_converse, sender_jid)
                 test_utils.openChatBoxFor(_converse, sender_jid)
                 .then(() => {
                 .then(() => {
-                    var chatbox = _converse.chatboxes.get(sender_jid);
+                    chatbox = _converse.chatboxes.get(sender_jid);
                     _converse.windowState = 'hidden';
                     _converse.windowState = 'hidden';
                     _converse.chatboxes.onMessage(msgFactory());
                     _converse.chatboxes.onMessage(msgFactory());
+                    return test_utils.waitUntil(() => chatbox.messages.length);
+                }).then(() => {
                     expect(chatbox.get('num_unread')).toBe(1);
                     expect(chatbox.get('num_unread')).toBe(1);
                     done();
                     done();
                 });
                 });
@@ -1344,17 +1369,19 @@
 
 
                 test_utils.createContacts(_converse, 'current');
                 test_utils.createContacts(_converse, 'current');
                 _converse.emit('rosterContactsFetched');
                 _converse.emit('rosterContactsFetched');
-
-                var sender_jid = mock.cur_names[0].replace(/ /g,'.').toLowerCase() + '@localhost';
-                var msgFactory = function () {
+                let chatbox;
+                const sender_jid = mock.cur_names[0].replace(/ /g,'.').toLowerCase() + '@localhost';
+                const msgFactory = function () {
                     return test_utils.createChatMessage(_converse, sender_jid, 'This message will be unread');
                     return test_utils.createChatMessage(_converse, sender_jid, 'This message will be unread');
                 };
                 };
                 test_utils.openChatBoxFor(_converse, sender_jid)
                 test_utils.openChatBoxFor(_converse, sender_jid)
                 .then(() => {
                 .then(() => {
-                    var chatbox = _converse.chatboxes.get(sender_jid);
+                    chatbox = _converse.chatboxes.get(sender_jid);
                     chatbox.save('scrolled', true);
                     chatbox.save('scrolled', true);
                     _converse.windowState = 'hidden';
                     _converse.windowState = 'hidden';
                     _converse.chatboxes.onMessage(msgFactory());
                     _converse.chatboxes.onMessage(msgFactory());
+                    return test_utils.waitUntil(() => chatbox.messages.length);
+                }).then(() => {
                     expect(chatbox.get('num_unread')).toBe(1);
                     expect(chatbox.get('num_unread')).toBe(1);
                     done();
                     done();
                 });
                 });
@@ -1368,20 +1395,23 @@
                 test_utils.createContacts(_converse, 'current');
                 test_utils.createContacts(_converse, 'current');
                 _converse.emit('rosterContactsFetched');
                 _converse.emit('rosterContactsFetched');
 
 
+                let chatbox;
                 const sender_jid = mock.cur_names[0].replace(/ /g,'.').toLowerCase() + '@localhost';
                 const sender_jid = mock.cur_names[0].replace(/ /g,'.').toLowerCase() + '@localhost';
-                var msgFactory = function () {
+                const msgFactory = function () {
                     return test_utils.createChatMessage(_converse, sender_jid, 'This message will be unread');
                     return test_utils.createChatMessage(_converse, sender_jid, 'This message will be unread');
                 };
                 };
                 test_utils.openChatBoxFor(_converse, sender_jid)
                 test_utils.openChatBoxFor(_converse, sender_jid)
                 .then(() => {
                 .then(() => {
-                    const chatbox = _converse.chatboxes.get(sender_jid);
+                    chatbox = _converse.chatboxes.get(sender_jid);
                     _converse.windowState = 'hidden';
                     _converse.windowState = 'hidden';
                     _converse.chatboxes.onMessage(msgFactory());
                     _converse.chatboxes.onMessage(msgFactory());
+                    return test_utils.waitUntil(() => chatbox.messages.length);
+                }).then(() => {
                     expect(chatbox.get('num_unread')).toBe(1);
                     expect(chatbox.get('num_unread')).toBe(1);
                     _converse.saveWindowState(null, 'focus');
                     _converse.saveWindowState(null, 'focus');
                     expect(chatbox.get('num_unread')).toBe(0);
                     expect(chatbox.get('num_unread')).toBe(0);
                     done();
                     done();
-                });
+                }).catch(_.partial(_converse.log, _, Strophe.LogLevel.FATAL))
             }));
             }));
 
 
             it("is not cleared when ChatBoxView was scrolled up and the windows become focused",
             it("is not cleared when ChatBoxView was scrolled up and the windows become focused",
@@ -1391,21 +1421,24 @@
 
 
                 test_utils.createContacts(_converse, 'current');
                 test_utils.createContacts(_converse, 'current');
                 _converse.emit('rosterContactsFetched');
                 _converse.emit('rosterContactsFetched');
-                var sender_jid = mock.cur_names[0].replace(/ /g,'.').toLowerCase() + '@localhost';
-                var msgFactory = function () {
+                let chatbox;
+                const sender_jid = mock.cur_names[0].replace(/ /g,'.').toLowerCase() + '@localhost';
+                const msgFactory = function () {
                     return test_utils.createChatMessage(_converse, sender_jid, 'This message will be unread');
                     return test_utils.createChatMessage(_converse, sender_jid, 'This message will be unread');
                 };
                 };
                 test_utils.openChatBoxFor(_converse, sender_jid)
                 test_utils.openChatBoxFor(_converse, sender_jid)
                 .then(() => {
                 .then(() => {
-                    var chatbox = _converse.chatboxes.get(sender_jid);
+                    chatbox = _converse.chatboxes.get(sender_jid);
                     chatbox.save('scrolled', true);
                     chatbox.save('scrolled', true);
                     _converse.windowState = 'hidden';
                     _converse.windowState = 'hidden';
                     _converse.chatboxes.onMessage(msgFactory());
                     _converse.chatboxes.onMessage(msgFactory());
+                    return test_utils.waitUntil(() => chatbox.messages.length);
+                }).then(() => {
                     expect(chatbox.get('num_unread')).toBe(1);
                     expect(chatbox.get('num_unread')).toBe(1);
                     _converse.saveWindowState(null, 'focus');
                     _converse.saveWindowState(null, 'focus');
                     expect(chatbox.get('num_unread')).toBe(1);
                     expect(chatbox.get('num_unread')).toBe(1);
                     done();
                     done();
-                });
+                }).catch(_.partial(_converse.log, _, Strophe.LogLevel.FATAL))
             }));
             }));
         });
         });
 
 
@@ -1419,28 +1452,29 @@
                 test_utils.createContacts(_converse, 'current');
                 test_utils.createContacts(_converse, 'current');
                 _converse.emit('rosterContactsFetched');
                 _converse.emit('rosterContactsFetched');
 
 
+                let msg, chatbox, indicator_el, selector;
                 const sender_jid = mock.cur_names[0].replace(/ /g,'.').toLowerCase() + '@localhost';
                 const sender_jid = mock.cur_names[0].replace(/ /g,'.').toLowerCase() + '@localhost';
                 test_utils.waitUntil(() => _converse.rosterview.el.querySelectorAll('.roster-group').length, 500)
                 test_utils.waitUntil(() => _converse.rosterview.el.querySelectorAll('.roster-group').length, 500)
                 .then(() => test_utils.openChatBoxFor(_converse, sender_jid))
                 .then(() => test_utils.openChatBoxFor(_converse, sender_jid))
                 .then(() => {
                 .then(() => {
-                    var chatbox = _converse.chatboxes.get(sender_jid);
+                    chatbox = _converse.chatboxes.get(sender_jid);
                     chatbox.save('scrolled', true);
                     chatbox.save('scrolled', true);
-
-                    var msg = test_utils.createChatMessage(_converse, sender_jid, 'This message will be unread');
+                    msg = test_utils.createChatMessage(_converse, sender_jid, 'This message will be unread');
                     _converse.chatboxes.onMessage(msg);
                     _converse.chatboxes.onMessage(msg);
-
-                    var selector = 'a.open-chat:contains("' + chatbox.get('fullname') + '") .msgs-indicator',
-                        indicator_el = sizzle(selector, _converse.rosterview.el).pop();
-
+                    return test_utils.waitUntil(() => chatbox.messages.length);
+                }).then(() => {
+                    selector = 'a.open-chat:contains("' + chatbox.get('fullname') + '") .msgs-indicator';
+                    indicator_el = sizzle(selector, _converse.rosterview.el).pop();
                     expect(indicator_el.textContent).toBe('1');
                     expect(indicator_el.textContent).toBe('1');
 
 
                     msg = test_utils.createChatMessage(_converse, sender_jid, 'This message will be unread too');
                     msg = test_utils.createChatMessage(_converse, sender_jid, 'This message will be unread too');
                     _converse.chatboxes.onMessage(msg);
                     _converse.chatboxes.onMessage(msg);
-
+                    return test_utils.waitUntil(() => chatbox.messages.length);
+                }).then(() => {
                     indicator_el = sizzle(selector, _converse.rosterview.el).pop();
                     indicator_el = sizzle(selector, _converse.rosterview.el).pop();
                     expect(indicator_el.textContent).toBe('2');
                     expect(indicator_el.textContent).toBe('2');
                     done();
                     done();
-                });
+                }).catch(_.partial(_converse.log, _, Strophe.LogLevel.FATAL))
             }));
             }));
 
 
             it("is updated when message is received and chatbox is minimized",
             it("is updated when message is received and chatbox is minimized",
@@ -1452,28 +1486,30 @@
                 _converse.emit('rosterContactsFetched');
                 _converse.emit('rosterContactsFetched');
                 const sender_jid = mock.cur_names[0].replace(/ /g,'.').toLowerCase() + '@localhost';
                 const sender_jid = mock.cur_names[0].replace(/ /g,'.').toLowerCase() + '@localhost';
 
 
+                let chatbox, indicator_el, msg, selector;
                 test_utils.waitUntil(() => _converse.rosterview.el.querySelectorAll('.roster-group').length, 500)
                 test_utils.waitUntil(() => _converse.rosterview.el.querySelectorAll('.roster-group').length, 500)
                 .then(() => test_utils.openChatBoxFor(_converse, sender_jid))
                 .then(() => test_utils.openChatBoxFor(_converse, sender_jid))
                 .then(() => {
                 .then(() => {
-                    var chatbox = _converse.chatboxes.get(sender_jid);
+                    chatbox = _converse.chatboxes.get(sender_jid);
                     var chatboxview = _converse.chatboxviews.get(sender_jid);
                     var chatboxview = _converse.chatboxviews.get(sender_jid);
                     chatboxview.minimize();
                     chatboxview.minimize();
 
 
-                    var msg = test_utils.createChatMessage(_converse, sender_jid, 'This message will be unread');
+                    msg = test_utils.createChatMessage(_converse, sender_jid, 'This message will be unread');
                     _converse.chatboxes.onMessage(msg);
                     _converse.chatboxes.onMessage(msg);
-
-                    var selector = 'a.open-chat:contains("' + chatbox.get('fullname') + '") .msgs-indicator',
-                        indicator_el = sizzle(selector, _converse.rosterview.el).pop();
-
+                    return test_utils.waitUntil(() => chatbox.messages.length);
+                }).then(() => {
+                    selector = 'a.open-chat:contains("' + chatbox.get('fullname') + '") .msgs-indicator';
+                    indicator_el = sizzle(selector, _converse.rosterview.el).pop();
                     expect(indicator_el.textContent).toBe('1');
                     expect(indicator_el.textContent).toBe('1');
 
 
                     msg = test_utils.createChatMessage(_converse, sender_jid, 'This message will be unread too');
                     msg = test_utils.createChatMessage(_converse, sender_jid, 'This message will be unread too');
                     _converse.chatboxes.onMessage(msg);
                     _converse.chatboxes.onMessage(msg);
-
+                    return test_utils.waitUntil(() => chatbox.messages.length);
+                }).then(() => {
                     indicator_el = sizzle(selector, _converse.rosterview.el).pop();
                     indicator_el = sizzle(selector, _converse.rosterview.el).pop();
                     expect(indicator_el.textContent).toBe('2');
                     expect(indicator_el.textContent).toBe('2');
                     done();
                     done();
-                });
+                }).catch(_.partial(_converse.log, _, Strophe.LogLevel.FATAL))
             }));
             }));
 
 
             it("is cleared when chatbox is maximzied after receiving messages in minimized mode",
             it("is cleared when chatbox is maximzied after receiving messages in minimized mode",
@@ -1484,27 +1520,28 @@
                 test_utils.createContacts(_converse, 'current');
                 test_utils.createContacts(_converse, 'current');
                 _converse.emit('rosterContactsFetched');
                 _converse.emit('rosterContactsFetched');
                 const sender_jid = mock.cur_names[0].replace(/ /g,'.').toLowerCase() + '@localhost';
                 const sender_jid = mock.cur_names[0].replace(/ /g,'.').toLowerCase() + '@localhost';
-
+                let chatbox, view, select_msgs_indicator;
+                const msgFactory = function () {
+                    return test_utils.createChatMessage(_converse, sender_jid, 'This message will be received as unread, but eventually will be read');
+                };
                 test_utils.waitUntil(() => _converse.rosterview.el.querySelectorAll('.roster-group').length, 500)
                 test_utils.waitUntil(() => _converse.rosterview.el.querySelectorAll('.roster-group').length, 500)
                 .then(() => test_utils.openChatBoxFor(_converse, sender_jid))
                 .then(() => test_utils.openChatBoxFor(_converse, sender_jid))
                 .then(() => {
                 .then(() => {
-                    var chatbox = _converse.chatboxes.get(sender_jid);
-                    var chatboxview = _converse.chatboxviews.get(sender_jid);
+                    chatbox = _converse.chatboxes.get(sender_jid);
+                    view = _converse.chatboxviews.get(sender_jid);
                     var msgsIndicatorSelector = 'a.open-chat:contains("' + chatbox.get('fullname') + '") .msgs-indicator';
                     var msgsIndicatorSelector = 'a.open-chat:contains("' + chatbox.get('fullname') + '") .msgs-indicator';
-                    var selectMsgsIndicator = () => $(_converse.rosterview.el).find(msgsIndicatorSelector);
-                    var msgFactory = function () {
-                        return test_utils.createChatMessage(_converse, sender_jid, 'This message will be received as unread, but eventually will be read');
-                    };
-                    chatboxview.minimize();
-
+                    select_msgs_indicator = () => $(_converse.rosterview.el).find(msgsIndicatorSelector);
+                    view.minimize();
                     _converse.chatboxes.onMessage(msgFactory());
                     _converse.chatboxes.onMessage(msgFactory());
-                    expect(selectMsgsIndicator().text()).toBe('1');
-
+                    return test_utils.waitUntil(() => chatbox.messages.length);
+                }).then(() => {
+                    expect(select_msgs_indicator().text()).toBe('1');
                     _converse.chatboxes.onMessage(msgFactory());
                     _converse.chatboxes.onMessage(msgFactory());
-                    expect(selectMsgsIndicator().text()).toBe('2');
-
-                    chatboxview.maximize();
-                    expect(selectMsgsIndicator().length).toBe(0);
+                    return test_utils.waitUntil(() => chatbox.messages.length);
+                }).then(() => {
+                    expect(select_msgs_indicator().text()).toBe('2');
+                    view.maximize();
+                    expect(select_msgs_indicator().length).toBe(0);
                     done();
                     done();
                 });
                 });
             }));
             }));
@@ -1518,27 +1555,27 @@
                 _converse.emit('rosterContactsFetched');
                 _converse.emit('rosterContactsFetched');
                 const sender_jid = mock.cur_names[0].replace(/ /g,'.').toLowerCase() + '@localhost';
                 const sender_jid = mock.cur_names[0].replace(/ /g,'.').toLowerCase() + '@localhost';
 
 
+                let view, chatbox, select_msgs_indicator;
                 test_utils.waitUntil(() => _converse.rosterview.el.querySelectorAll('.roster-group').length, 500)
                 test_utils.waitUntil(() => _converse.rosterview.el.querySelectorAll('.roster-group').length, 500)
                 .then(() => test_utils.openChatBoxFor(_converse, sender_jid))
                 .then(() => test_utils.openChatBoxFor(_converse, sender_jid))
                 .then(() => {
                 .then(() => {
-                    var chatbox = _converse.chatboxes.get(sender_jid);
-                    var chatboxview = _converse.chatboxviews.get(sender_jid);
+                    chatbox = _converse.chatboxes.get(sender_jid);
+                    view = _converse.chatboxviews.get(sender_jid);
                     var msgFactory = function () {
                     var msgFactory = function () {
                         return test_utils.createChatMessage(_converse, sender_jid, 'This message will be received as unread, but eventually will be read');
                         return test_utils.createChatMessage(_converse, sender_jid, 'This message will be received as unread, but eventually will be read');
                     };
                     };
-                    var msgsIndicatorSelector = 'a.open-chat:contains("' + chatbox.get('fullname') + '") .msgs-indicator',
-                        selectMsgsIndicator = () => $(_converse.rosterview.el).find(msgsIndicatorSelector);
-
+                    var msgsIndicatorSelector = 'a.open-chat:contains("' + chatbox.get('fullname') + '") .msgs-indicator';
+                    select_msgs_indicator = () => $(_converse.rosterview.el).find(msgsIndicatorSelector);
                     chatbox.save('scrolled', true);
                     chatbox.save('scrolled', true);
-
                     _converse.chatboxes.onMessage(msgFactory());
                     _converse.chatboxes.onMessage(msgFactory());
-                    expect(selectMsgsIndicator().text()).toBe('1');
-
-                    chatboxview.viewUnreadMessages();
+                    return test_utils.waitUntil(() => view.model.messages.length);
+                }).then(() => {
+                    expect(select_msgs_indicator().text()).toBe('1');
+                    view.viewUnreadMessages();
                     _converse.rosterview.render();
                     _converse.rosterview.render();
-                    expect(selectMsgsIndicator().length).toBe(0);
+                    expect(select_msgs_indicator().length).toBe(0);
                     done();
                     done();
-                });
+                }).catch(_.partial(_converse.log, _, Strophe.LogLevel.FATAL))
             }));
             }));
 
 
             it("is not cleared after user clicks on roster view when chatbox is already opened and scrolled up",
             it("is not cleared after user clicks on roster view when chatbox is already opened and scrolled up",
@@ -1550,24 +1587,25 @@
                 _converse.emit('rosterContactsFetched');
                 _converse.emit('rosterContactsFetched');
                 const sender_jid = mock.cur_names[0].replace(/ /g,'.').toLowerCase() + '@localhost';
                 const sender_jid = mock.cur_names[0].replace(/ /g,'.').toLowerCase() + '@localhost';
 
 
+                let select_msgs_indicator, view;
                 test_utils.waitUntil(() => _converse.rosterview.el.querySelectorAll('.roster-group').length, 500)
                 test_utils.waitUntil(() => _converse.rosterview.el.querySelectorAll('.roster-group').length, 500)
                 .then(() => test_utils.openChatBoxFor(_converse, sender_jid))
                 .then(() => test_utils.openChatBoxFor(_converse, sender_jid))
                 .then(() => {
                 .then(() => {
-                    var chatbox = _converse.chatboxes.get(sender_jid);
-                    var chatboxview = _converse.chatboxviews.get(sender_jid);
+                    const chatbox = _converse.chatboxes.get(sender_jid);
+                    view = _converse.chatboxviews.get(sender_jid);
                     var msgFactory = function () {
                     var msgFactory = function () {
                         return test_utils.createChatMessage(_converse, sender_jid, 'This message will be received as unread, but eventually will be read');
                         return test_utils.createChatMessage(_converse, sender_jid, 'This message will be received as unread, but eventually will be read');
                     };
                     };
-                    var msgsIndicatorSelector = 'a.open-chat:contains("' + chatbox.get('fullname') + '") .msgs-indicator',
-                        selectMsgsIndicator = () => $(_converse.rosterview.el).find(msgsIndicatorSelector);
-
+                    var msgsIndicatorSelector = 'a.open-chat:contains("' + chatbox.get('fullname') + '") .msgs-indicator';
+                    select_msgs_indicator = () => $(_converse.rosterview.el).find(msgsIndicatorSelector);
                     chatbox.save('scrolled', true);
                     chatbox.save('scrolled', true);
-
                     _converse.chatboxes.onMessage(msgFactory());
                     _converse.chatboxes.onMessage(msgFactory());
-                    expect(selectMsgsIndicator().text()).toBe('1');
-
-                    test_utils.openChatBoxFor(_converse, sender_jid);
-                    expect(selectMsgsIndicator().text()).toBe('1');
+                    return test_utils.waitUntil(() => view.model.messages.length);
+                }).then(() => {
+                    expect(select_msgs_indicator().text()).toBe('1');
+                    return test_utils.openChatBoxFor(_converse, sender_jid);
+                }).then(() => {
+                    expect(select_msgs_indicator().text()).toBe('1');
                     done();
                     done();
                 });
                 });
             }));
             }));
@@ -1584,20 +1622,21 @@
                 _converse.emit('rosterContactsFetched');
                 _converse.emit('rosterContactsFetched');
                 const sender_jid = mock.cur_names[0].replace(/ /g,'.').toLowerCase() + '@localhost';
                 const sender_jid = mock.cur_names[0].replace(/ /g,'.').toLowerCase() + '@localhost';
 
 
+                let selectUnreadMsgCount;
                 test_utils.openChatBoxFor(_converse, sender_jid)
                 test_utils.openChatBoxFor(_converse, sender_jid)
                 .then(() => {
                 .then(() => {
                     const msgFactory = function () {
                     const msgFactory = function () {
                         return test_utils.createChatMessage(_converse, sender_jid, 'This message will be received as unread, but eventually will be read');
                         return test_utils.createChatMessage(_converse, sender_jid, 'This message will be received as unread, but eventually will be read');
                     };
                     };
-                    const selectUnreadMsgCount = function () {
+                    selectUnreadMsgCount = function () {
                         const minimizedChatBoxView = _converse.minimized_chats.get(sender_jid);
                         const minimizedChatBoxView = _converse.minimized_chats.get(sender_jid);
                         return minimizedChatBoxView.el.querySelector('.message-count');
                         return minimizedChatBoxView.el.querySelector('.message-count');
                     };
                     };
-
                     const chatbox = _converse.chatboxes.get(sender_jid);
                     const chatbox = _converse.chatboxes.get(sender_jid);
                     chatbox.save('scrolled', true);
                     chatbox.save('scrolled', true);
                     _converse.chatboxes.onMessage(msgFactory());
                     _converse.chatboxes.onMessage(msgFactory());
-
+                    return test_utils.waitUntil(() => chatbox.messages.length);
+                }).then(() => {
                     const chatboxview = _converse.chatboxviews.get(sender_jid);
                     const chatboxview = _converse.chatboxviews.get(sender_jid);
                     chatboxview.minimize();
                     chatboxview.minimize();
 
 
@@ -1605,7 +1644,7 @@
                     expect(u.isVisible(unread_count)).toBeTruthy();
                     expect(u.isVisible(unread_count)).toBeTruthy();
                     expect(unread_count.innerHTML).toBe('1');
                     expect(unread_count.innerHTML).toBe('1');
                     done();
                     done();
-                });
+                }).catch(_.partial(_converse.log, _, Strophe.LogLevel.FATAL))
             }));
             }));
 
 
             it("is incremented when message is received and windows is not focused",
             it("is incremented when message is received and windows is not focused",
@@ -1617,27 +1656,27 @@
                 _converse.emit('rosterContactsFetched');
                 _converse.emit('rosterContactsFetched');
                 const sender_jid = mock.cur_names[0].replace(/ /g,'.').toLowerCase() + '@localhost';
                 const sender_jid = mock.cur_names[0].replace(/ /g,'.').toLowerCase() + '@localhost';
 
 
+                let selectUnreadMsgCount;
                 test_utils.openChatBoxFor(_converse, sender_jid)
                 test_utils.openChatBoxFor(_converse, sender_jid)
                 .then(() => {
                 .then(() => {
                     const msgFactory = function () {
                     const msgFactory = function () {
                         return test_utils.createChatMessage(_converse, sender_jid,
                         return test_utils.createChatMessage(_converse, sender_jid,
                             'This message will be received as unread, but eventually will be read');
                             'This message will be received as unread, but eventually will be read');
                     };
                     };
-                    const selectUnreadMsgCount = function () {
+                    selectUnreadMsgCount = function () {
                         const minimizedChatBoxView = _converse.minimized_chats.get(sender_jid);
                         const minimizedChatBoxView = _converse.minimized_chats.get(sender_jid);
                         return minimizedChatBoxView.el.querySelector('.message-count');
                         return minimizedChatBoxView.el.querySelector('.message-count');
                     };
                     };
-
-                    const chatboxview = _converse.chatboxviews.get(sender_jid);
-                    chatboxview.minimize();
-
+                    const view = _converse.chatboxviews.get(sender_jid);
+                    view.minimize();
                     _converse.chatboxes.onMessage(msgFactory());
                     _converse.chatboxes.onMessage(msgFactory());
-
+                    return test_utils.waitUntil(() => view.model.messages.length);
+                }).then(() => {
                     const unread_count = selectUnreadMsgCount();
                     const unread_count = selectUnreadMsgCount();
                     expect(u.isVisible(unread_count)).toBeTruthy();
                     expect(u.isVisible(unread_count)).toBeTruthy();
                     expect(unread_count.innerHTML).toBe('1');
                     expect(unread_count.innerHTML).toBe('1');
                     done();
                     done();
-                });
+                }).catch(_.partial(_converse.log, _, Strophe.LogLevel.FATAL))
             }));
             }));
 
 
             it("will render Openstreetmap-URL from geo-URI",
             it("will render Openstreetmap-URL from geo-URI",

+ 36 - 39
spec/chatroom.js

@@ -738,7 +738,6 @@
                     _converse.connection._dataRecv(test_utils.createRequest(stanza));
                     _converse.connection._dataRecv(test_utils.createRequest(stanza));
 
 
                     jasmine.clock().tick(ONE_DAY_LATER);
                     jasmine.clock().tick(ONE_DAY_LATER);
-
                     // Test a user leaving a groupchat
                     // Test a user leaving a groupchat
                     presence = $pres({
                     presence = $pres({
                             to: 'dummy@localhost/_converse.js-29092160',
                             to: 'dummy@localhost/_converse.js-29092160',
@@ -766,11 +765,9 @@
                     expect($chat_content.find('div.chat-info:last').html()).toBe(
                     expect($chat_content.find('div.chat-info:last').html()).toBe(
                         'newguy has left the groupchat. '+
                         'newguy has left the groupchat. '+
                         '"Disconnected: Replaced by new connection"');
                         '"Disconnected: Replaced by new connection"');
-
                     jasmine.clock().uninstall();
                     jasmine.clock().uninstall();
                     done();
                     done();
-                    return;
-                });
+                }).catch(_.partial(_converse.log, _, Strophe.LogLevel.FATAL))
             }));
             }));
 
 
             it("shows its description in the chat heading",
             it("shows its description in the chat heading",
@@ -827,14 +824,14 @@
                     null, ['rosterGroupsFetched'], {},
                     null, ['rosterGroupsFetched'], {},
                     function (done, _converse) {
                     function (done, _converse) {
 
 
+                let view;
                 test_utils.waitUntilDiscoConfirmed(_converse, 'localhost', [], ['vcard-temp'])
                 test_utils.waitUntilDiscoConfirmed(_converse, 'localhost', [], ['vcard-temp'])
-                .then(function () {
-                    return test_utils.waitUntil(() => _converse.xmppstatus.vcard.get('fullname'))
-                }).then(function () {
+                .then(() => test_utils.waitUntil(() => _converse.xmppstatus.vcard.get('fullname')))
+                .then(() => {
                     test_utils.createContacts(_converse, 'current');
                     test_utils.createContacts(_converse, 'current');
                     return test_utils.openAndEnterChatRoom(_converse, 'lounge', 'localhost', 'dummy');
                     return test_utils.openAndEnterChatRoom(_converse, 'lounge', 'localhost', 'dummy');
-                }).then(function () {
-                    var view = _converse.chatboxviews.get('lounge@localhost');
+                }).then(() => {
+                    view = _converse.chatboxviews.get('lounge@localhost');
                     if (!$(view.el).find('.chat-area').length) { view.renderChatArea(); }
                     if (!$(view.el).find('.chat-area').length) { view.renderChatArea(); }
                     var message = '/me is tired';
                     var message = '/me is tired';
                     var nick = mock.chatroom_names[0],
                     var nick = mock.chatroom_names[0],
@@ -1495,20 +1492,18 @@
                     null, ['rosterGroupsFetched'], {},
                     null, ['rosterGroupsFetched'], {},
                     function (done, _converse) {
                     function (done, _converse) {
 
 
+                let view;
+                const text = 'This is a received message';
                 test_utils.openAndEnterChatRoom(_converse, 'lounge', 'localhost', 'dummy').then(function () {
                 test_utils.openAndEnterChatRoom(_converse, 'lounge', 'localhost', 'dummy').then(function () {
                     spyOn(_converse, 'emit');
                     spyOn(_converse, 'emit');
-                    var view = _converse.chatboxviews.get('lounge@localhost');
-
-
+                    view = _converse.chatboxviews.get('lounge@localhost');
                     if (!$(view.el).find('.chat-area').length) { view.renderChatArea(); }
                     if (!$(view.el).find('.chat-area').length) { view.renderChatArea(); }
                     var nick = mock.chatroom_names[0];
                     var nick = mock.chatroom_names[0];
-
                     view.model.occupants.create({
                     view.model.occupants.create({
                         'nick': nick,
                         'nick': nick,
                         'muc_jid': `${view.model.get('jid')}/${nick}`
                         'muc_jid': `${view.model.get('jid')}/${nick}`
                     });
                     });
 
 
-                    var text = 'This is a received message';
                     var message = $msg({
                     var message = $msg({
                         from: 'lounge@localhost/'+nick,
                         from: 'lounge@localhost/'+nick,
                         id: '1',
                         id: '1',
@@ -1585,7 +1580,7 @@
                             }).c('body').t('Message: '+i).tree());
                             }).c('body').t('Message: '+i).tree());
                     }
                     }
                     // Give enough time for `markScrolled` to have been called
                     // Give enough time for `markScrolled` to have been called
-                    setTimeout(function () {
+                    setTimeout(() => {
                         view.content.scrollTop = 0;
                         view.content.scrollTop = 0;
                         view.model.onMessage(
                         view.model.onMessage(
                             $msg({
                             $msg({
@@ -3435,19 +3430,19 @@
                 var roomspanel = _converse.chatboxviews.get('controlbox').roomspanel;
                 var roomspanel = _converse.chatboxviews.get('controlbox').roomspanel;
                 expect(roomspanel.el.querySelectorAll('.available-room').length).toBe(0);
                 expect(roomspanel.el.querySelectorAll('.available-room').length).toBe(0);
 
 
-                var room_jid = 'kitchen@conference.shakespeare.lit';
-                test_utils.openAndEnterChatRoom(
-                        _converse, 'kitchen', 'conference.shakespeare.lit', 'fires').then(function () {
-
+                let view, nick;
+                const room_jid = 'kitchen@conference.shakespeare.lit';
+                const message = 'fires: Your attention is required';
+                test_utils.openAndEnterChatRoom(_converse, 'kitchen', 'conference.shakespeare.lit', 'fires')
+                .then(() => {
                     expect(roomspanel.el.querySelectorAll('.available-room').length).toBe(1);
                     expect(roomspanel.el.querySelectorAll('.available-room').length).toBe(1);
                     expect(roomspanel.el.querySelectorAll('.msgs-indicator').length).toBe(0);
                     expect(roomspanel.el.querySelectorAll('.msgs-indicator').length).toBe(0);
 
 
-                    var view = _converse.chatboxviews.get(room_jid);
+                    view = _converse.chatboxviews.get(room_jid);
                     view.model.set({'minimized': true});
                     view.model.set({'minimized': true});
 
 
                     var contact_jid = mock.cur_names[5].replace(/ /g,'.').toLowerCase() + '@localhost';
                     var contact_jid = mock.cur_names[5].replace(/ /g,'.').toLowerCase() + '@localhost';
-                    var message = 'fires: Your attention is required';
-                    var nick = mock.chatroom_names[0];
+                    nick = mock.chatroom_names[0];
 
 
                     view.model.onMessage($msg({
                     view.model.onMessage($msg({
                             from: room_jid+'/'+nick,
                             from: room_jid+'/'+nick,
@@ -3455,7 +3450,8 @@
                             to: 'dummy@localhost',
                             to: 'dummy@localhost',
                             type: 'groupchat'
                             type: 'groupchat'
                         }).c('body').t(message).tree());
                         }).c('body').t(message).tree());
-
+                    return test_utils.waitUntil(() => view.model.messages.length);
+                }).then(() => {
                     expect(roomspanel.el.querySelectorAll('.available-room').length).toBe(1);
                     expect(roomspanel.el.querySelectorAll('.available-room').length).toBe(1);
                     expect(roomspanel.el.querySelectorAll('.msgs-indicator').length).toBe(1);
                     expect(roomspanel.el.querySelectorAll('.msgs-indicator').length).toBe(1);
                     expect(roomspanel.el.querySelector('.msgs-indicator').textContent).toBe('1');
                     expect(roomspanel.el.querySelector('.msgs-indicator').textContent).toBe('1');
@@ -3466,17 +3462,16 @@
                         'to': 'dummy@localhost',
                         'to': 'dummy@localhost',
                         'type': 'groupchat'
                         'type': 'groupchat'
                     }).c('body').t(message).tree());
                     }).c('body').t(message).tree());
-
+                    return test_utils.waitUntil(() => view.model.messages.length > 1);
+                }).then(() => {
                     expect(roomspanel.el.querySelectorAll('.available-room').length).toBe(1);
                     expect(roomspanel.el.querySelectorAll('.available-room').length).toBe(1);
                     expect(roomspanel.el.querySelectorAll('.msgs-indicator').length).toBe(1);
                     expect(roomspanel.el.querySelectorAll('.msgs-indicator').length).toBe(1);
                     expect(roomspanel.el.querySelector('.msgs-indicator').textContent).toBe('2');
                     expect(roomspanel.el.querySelector('.msgs-indicator').textContent).toBe('2');
-
                     view.model.set({'minimized': false});
                     view.model.set({'minimized': false});
-
                     expect(roomspanel.el.querySelectorAll('.available-room').length).toBe(1);
                     expect(roomspanel.el.querySelectorAll('.available-room').length).toBe(1);
                     expect(roomspanel.el.querySelectorAll('.msgs-indicator').length).toBe(0);
                     expect(roomspanel.el.querySelectorAll('.msgs-indicator').length).toBe(0);
                     done();
                     done();
-                });
+                }).catch(_.partial(_converse.log, _, Strophe.LogLevel.FATAL))
             }));
             }));
 
 
             describe("A Chat Status Notification", function () {
             describe("A Chat Status Notification", function () {
@@ -3488,14 +3483,15 @@
                             null, ['rosterGroupsFetched'], {},
                             null, ['rosterGroupsFetched'], {},
                             function (done, _converse) {
                             function (done, _converse) {
 
 
+                        let view;
+                        const room_jid = 'coven@chat.shakespeare.lit';
                         test_utils.openAndEnterChatRoom(
                         test_utils.openAndEnterChatRoom(
                                 _converse, 'coven', 'chat.shakespeare.lit', 'some1').then(function () {
                                 _converse, 'coven', 'chat.shakespeare.lit', 'some1').then(function () {
 
 
-                            var room_jid = 'coven@chat.shakespeare.lit';
-                            var view = _converse.chatboxviews.get('coven@chat.shakespeare.lit');
-                            var $chat_content = $(view.el).find('.chat-content');
+                            view = _converse.chatboxviews.get('coven@chat.shakespeare.lit');
+                            const chat_content = view.el.querySelector('.chat-content');
 
 
-                            expect($chat_content.find('div.chat-info:first').html()).toBe("some1 has entered the groupchat");
+                            expect($(chat_content).find('div.chat-info:first').html()).toBe("some1 has entered the groupchat");
 
 
                             let presence = $pres({
                             let presence = $pres({
                                     to: 'dummy@localhost/_converse.js-29092160',
                                     to: 'dummy@localhost/_converse.js-29092160',
@@ -3508,8 +3504,8 @@
                                     'role': 'participant'
                                     'role': 'participant'
                                 });
                                 });
                             _converse.connection._dataRecv(test_utils.createRequest(presence));
                             _converse.connection._dataRecv(test_utils.createRequest(presence));
-                            expect($chat_content[0].querySelectorAll('div.chat-info').length).toBe(2);
-                            expect($chat_content.find('div.chat-info:last').html()).toBe("newguy has entered the groupchat");
+                            expect(chat_content.querySelectorAll('div.chat-info').length).toBe(2);
+                            expect($(chat_content).find('div.chat-info:last').html()).toBe("newguy has entered the groupchat");
 
 
                             presence = $pres({
                             presence = $pres({
                                     to: 'dummy@localhost/_converse.js-29092160',
                                     to: 'dummy@localhost/_converse.js-29092160',
@@ -3522,8 +3518,8 @@
                                     'role': 'participant'
                                     'role': 'participant'
                                 });
                                 });
                             _converse.connection._dataRecv(test_utils.createRequest(presence));
                             _converse.connection._dataRecv(test_utils.createRequest(presence));
-                            expect($chat_content[0].querySelectorAll('div.chat-info').length).toBe(3);
-                            expect($chat_content.find('div.chat-info:last').html()).toBe("nomorenicks has entered the groupchat");
+                            expect(chat_content.querySelectorAll('div.chat-info').length).toBe(3);
+                            expect($(chat_content).find('div.chat-info:last').html()).toBe("nomorenicks has entered the groupchat");
 
 
                             // See XEP-0085 http://xmpp.org/extensions/xep-0085.html#definitions
                             // See XEP-0085 http://xmpp.org/extensions/xep-0085.html#definitions
 
 
@@ -3536,7 +3532,8 @@
                                 }).c('body').c('composing', {'xmlns': Strophe.NS.CHATSTATES}).tree();
                                 }).c('body').c('composing', {'xmlns': Strophe.NS.CHATSTATES}).tree();
 
 
                             view.model.onMessage(msg);
                             view.model.onMessage(msg);
-
+                            return test_utils.waitUntil(() => view.el.querySelectorAll('.chat-state-notification').length);
+                        }).then(() => {
                             // Check that the notification appears inside the chatbox in the DOM
                             // Check that the notification appears inside the chatbox in the DOM
                             var events = view.el.querySelectorAll('.chat-event');
                             var events = view.el.querySelectorAll('.chat-event');
                             expect(events.length).toBe(3);
                             expect(events.length).toBe(3);
@@ -3554,7 +3551,7 @@
                             });
                             });
 
 
                             // Check that it doesn't appear twice
                             // Check that it doesn't appear twice
-                            msg = $msg({
+                            let msg = $msg({
                                     from: room_jid+'/newguy',
                                     from: room_jid+'/newguy',
                                     id: (new Date()).getTime(),
                                     id: (new Date()).getTime(),
                                     to: 'dummy@localhost',
                                     to: 'dummy@localhost',
@@ -3632,7 +3629,7 @@
                             notifications = view.el.querySelectorAll('.chat-state-notification');
                             notifications = view.el.querySelectorAll('.chat-state-notification');
                             expect(notifications.length).toBe(0);
                             expect(notifications.length).toBe(0);
                             done();
                             done();
-                        });
+                        }).catch(_.partial(_converse.log, _, Strophe.LogLevel.FATAL))
                     }));
                     }));
                 });
                 });
 
 
@@ -3776,7 +3773,7 @@
                             expect(notifications[0].textContent).toEqual('nomorenicks is typing');
                             expect(notifications[0].textContent).toEqual('nomorenicks is typing');
                             expect(notifications[1].textContent).toEqual('newguy has stopped typing');
                             expect(notifications[1].textContent).toEqual('newguy has stopped typing');
                             done();
                             done();
-                        });
+                        }).catch(_.partial(_converse.log, _, Strophe.LogLevel.FATAL))
                     }));
                     }));
                 });
                 });
             });
             });

+ 16 - 11
spec/controlbox.js

@@ -1,11 +1,12 @@
 (function (root, factory) {
 (function (root, factory) {
     define(["jquery", "jasmine", "mock", "test-utils"], factory);
     define(["jquery", "jasmine", "mock", "test-utils"], factory);
 } (this, function ($, jasmine, mock, test_utils) {
 } (this, function ($, jasmine, mock, test_utils) {
-    var _ = converse.env._;
-    var $pres = converse.env.$pres;
-    var $msg = converse.env.$msg;
-    var $iq = converse.env.$iq;
-    var u = converse.env.utils;
+    const _ = converse.env._,
+          $pres = converse.env.$pres,
+          $msg = converse.env.$msg,
+          $iq = converse.env.$iq,
+          u = converse.env.utils,
+          Strophe = converse.env.Strophe;
 
 
 
 
     describe("The Controlbox", function () {
     describe("The Controlbox", function () {
@@ -72,18 +73,18 @@
                 test_utils.createContacts(_converse, 'all').openControlBox();
                 test_utils.createContacts(_converse, 'all').openControlBox();
                 _converse.emit('rosterContactsFetched');
                 _converse.emit('rosterContactsFetched');
 
 
+                let chatview;
                 const sender_jid = mock.cur_names[0].replace(/ /g,'.').toLowerCase() + '@localhost';
                 const sender_jid = mock.cur_names[0].replace(/ /g,'.').toLowerCase() + '@localhost';
 
 
                 test_utils.openChatBoxFor(_converse, sender_jid);
                 test_utils.openChatBoxFor(_converse, sender_jid);
                 return test_utils.waitUntil(() => _converse.chatboxes.length).then(() => {
                 return test_utils.waitUntil(() => _converse.chatboxes.length).then(() => {
-
-                    const chatview = _converse.chatboxviews.get(sender_jid);
+                    chatview = _converse.chatboxviews.get(sender_jid);
                     chatview.model.set({'minimized': true});
                     chatview.model.set({'minimized': true});
 
 
                     expect(_.isNull(_converse.chatboxviews.el.querySelector('.restore-chat .message-count'))).toBeTruthy();
                     expect(_.isNull(_converse.chatboxviews.el.querySelector('.restore-chat .message-count'))).toBeTruthy();
                     expect(_.isNull(_converse.rosterview.el.querySelector('.msgs-indicator'))).toBeTruthy();
                     expect(_.isNull(_converse.rosterview.el.querySelector('.msgs-indicator'))).toBeTruthy();
 
 
-                    var msg = $msg({
+                    const msg = $msg({
                             from: sender_jid,
                             from: sender_jid,
                             to: _converse.connection.jid,
                             to: _converse.connection.jid,
                             type: 'chat',
                             type: 'chat',
@@ -91,10 +92,13 @@
                         }).c('body').t('hello').up()
                         }).c('body').t('hello').up()
                         .c('active', {'xmlns': 'http://jabber.org/protocol/chatstates'}).tree();
                         .c('active', {'xmlns': 'http://jabber.org/protocol/chatstates'}).tree();
                     _converse.chatboxes.onMessage(msg);
                     _converse.chatboxes.onMessage(msg);
+                    return test_utils.waitUntil(() => _converse.rosterview.el.querySelectorAll(".msgs-indicator"));
+                }).then(() => {
+                    spyOn(chatview.model, 'incrementUnreadMsgCounter').and.callThrough();
                     expect(_converse.chatboxviews.el.querySelector('.restore-chat .message-count').textContent).toBe('1');
                     expect(_converse.chatboxviews.el.querySelector('.restore-chat .message-count').textContent).toBe('1');
                     expect(_converse.rosterview.el.querySelector('.msgs-indicator').textContent).toBe('1');
                     expect(_converse.rosterview.el.querySelector('.msgs-indicator').textContent).toBe('1');
 
 
-                    msg = $msg({
+                    const msg = $msg({
                             from: sender_jid,
                             from: sender_jid,
                             to: _converse.connection.jid,
                             to: _converse.connection.jid,
                             type: 'chat',
                             type: 'chat',
@@ -102,14 +106,15 @@
                         }).c('body').t('hello again').up()
                         }).c('body').t('hello again').up()
                         .c('active', {'xmlns': 'http://jabber.org/protocol/chatstates'}).tree();
                         .c('active', {'xmlns': 'http://jabber.org/protocol/chatstates'}).tree();
                     _converse.chatboxes.onMessage(msg);
                     _converse.chatboxes.onMessage(msg);
+                return test_utils.waitUntil(() => chatview.model.incrementUnreadMsgCounter.calls.count());
+            }).then(() => {
                     expect(_converse.chatboxviews.el.querySelector('.restore-chat .message-count').textContent).toBe('2');
                     expect(_converse.chatboxviews.el.querySelector('.restore-chat .message-count').textContent).toBe('2');
                     expect(_converse.rosterview.el.querySelector('.msgs-indicator').textContent).toBe('2');
                     expect(_converse.rosterview.el.querySelector('.msgs-indicator').textContent).toBe('2');
-
                     chatview.model.set({'minimized': false});
                     chatview.model.set({'minimized': false});
                     expect(_.isNull(_converse.chatboxviews.el.querySelector('.restore-chat .message-count'))).toBeTruthy();
                     expect(_.isNull(_converse.chatboxviews.el.querySelector('.restore-chat .message-count'))).toBeTruthy();
                     expect(_.isNull(_converse.rosterview.el.querySelector('.msgs-indicator'))).toBeTruthy();
                     expect(_.isNull(_converse.rosterview.el.querySelector('.msgs-indicator'))).toBeTruthy();
                     done();
                     done();
-                });
+                }).catch(_.partial(_converse.log, _, Strophe.LogLevel.FATAL));
             }));
             }));
         });
         });
 
 

+ 1 - 1
spec/converse.js

@@ -311,7 +311,7 @@
                     }).catch(_.partial(console.error, _));
                     }).catch(_.partial(console.error, _));
             }));
             }));
 
 
-            it("has a method 'open' which opens and returns promise that resolves to a chat model", mock.initConverseWithPromises(
+            it("has a method 'open' which opens and returns a promise that resolves to a chat model", mock.initConverseWithPromises(
                 null, ['rosterGroupsFetched', 'chatBoxesInitialized'], {}, function (done, _converse) {
                 null, ['rosterGroupsFetched', 'chatBoxesInitialized'], {}, function (done, _converse) {
 
 
                 test_utils.openControlBox();
                 test_utils.openControlBox();

+ 23 - 7
spec/mam.js

@@ -20,15 +20,27 @@
                     null, ['discoInitialized'], {},
                     null, ['discoInitialized'], {},
                     function (done, _converse) {
                     function (done, _converse) {
 
 
-                test_utils.openAndEnterChatRoom(_converse, 'trek-radio', 'conference.lightwitch.org', 'jcbrand').then(function () {
-                    var chatroomview = _converse.chatboxviews.get('trek-radio@conference.lightwitch.org');
-                    var stanza = Strophe.xmlHtmlNode(
+                let view, stanza;
+
+                test_utils.openAndEnterChatRoom(_converse, 'trek-radio', 'conference.lightwitch.org', 'jcbrand')
+                .then(() => {
+                    view = _converse.chatboxviews.get('trek-radio@conference.lightwitch.org');
+                    stanza = Strophe.xmlHtmlNode(
                         `<message xmlns="jabber:client" to="jcbrand@lightwitch.org/converse.js-73057452" type="groupchat" from="trek-radio@conference.lightwitch.org/comndrdukath#0805 (STO)">
                         `<message xmlns="jabber:client" to="jcbrand@lightwitch.org/converse.js-73057452" type="groupchat" from="trek-radio@conference.lightwitch.org/comndrdukath#0805 (STO)">
                             <body>negan</body>
                             <body>negan</body>
                             <stanza-id xmlns="urn:xmpp:sid:0" id="45fbbf2a-1059-479d-9283-c8effaf05621" by="trek-radio@conference.lightwitch.org"/>
                             <stanza-id xmlns="urn:xmpp:sid:0" id="45fbbf2a-1059-479d-9283-c8effaf05621" by="trek-radio@conference.lightwitch.org"/>
                          </message>`).firstElementChild;
                          </message>`).firstElementChild;
                     _converse.connection._dataRecv(test_utils.createRequest(stanza));
                     _converse.connection._dataRecv(test_utils.createRequest(stanza));
-
+                    return test_utils.waitUntil(() => view.content.querySelectorAll('.chat-msg').length)
+                }).then(() => {
+                    // XXX: we wait here until the first message appears before
+                    // sending the duplicate. If we don't do that, then the
+                    // duplicate appears before the promise for `createMessage`
+                    // has been resolved, which means that the `isDuplicate`
+                    // check fails because the first message doesn't exist yet.
+                    //
+                    // Not sure whether such a race-condition might pose a problem
+                    // in "real-world" situations.
                     stanza = Strophe.xmlHtmlNode(
                     stanza = Strophe.xmlHtmlNode(
                         `<message xmlns="jabber:client" to="jcbrand@lightwitch.org/converse.js-73057452">
                         `<message xmlns="jabber:client" to="jcbrand@lightwitch.org/converse.js-73057452">
                             <result xmlns="urn:xmpp:mam:2" queryid="82d9db27-6cf8-4787-8c2c-5a560263d823" id="45fbbf2a-1059-479d-9283-c8effaf05621">
                             <result xmlns="urn:xmpp:mam:2" queryid="82d9db27-6cf8-4787-8c2c-5a560263d823" id="45fbbf2a-1059-479d-9283-c8effaf05621">
@@ -39,10 +51,14 @@
                                 </forwarded>
                                 </forwarded>
                             </result>
                             </result>
                         </message>`).firstElementChild;
                         </message>`).firstElementChild;
-                    chatroomview.model.onMessage(stanza);
-                    expect(chatroomview.content.querySelectorAll('.chat-msg').length).toBe(1);
+
+                    spyOn(view.model, 'isDuplicate').and.callThrough();
+                    view.model.onMessage(stanza);
+                    return test_utils.waitUntil(() => view.model.isDuplicate.calls.count());
+                }).then(() => {
+                    expect(view.content.querySelectorAll('.chat-msg').length).toBe(1);
                     done();
                     done();
-                });
+                }).catch(_.partial(_converse.log, _, Strophe.LogLevel.FATAL));
             }))
             }))
         });
         });
 
 

File diff suppressed because it is too large
+ 292 - 252
spec/messages.js


+ 19 - 15
spec/minchats.js

@@ -4,6 +4,7 @@
     const _ = converse.env._;
     const _ = converse.env._;
     const  $msg = converse.env.$msg;
     const  $msg = converse.env.$msg;
     const u = converse.env.utils;
     const u = converse.env.utils;
+    const Strophe = converse.env.Strophe;
 
 
     describe("The Minimized Chats Widget", function () {
     describe("The Minimized Chats Widget", function () {
 
 
@@ -43,7 +44,7 @@
                 expect(_converse.minimized_chats.keys().length).toBe(2);
                 expect(_converse.minimized_chats.keys().length).toBe(2);
                 expect(_.includes(_converse.minimized_chats.keys(), contact_jid)).toBeTruthy();
                 expect(_.includes(_converse.minimized_chats.keys(), contact_jid)).toBeTruthy();
                 done();
                 done();
-            });
+            }).catch(_.partial(_converse.log, _, Strophe.LogLevel.FATAL))
         }));
         }));
 
 
         it("can be toggled to hide or show minimized chats",
         it("can be toggled to hide or show minimized chats",
@@ -74,7 +75,7 @@
             }).then(() => {
             }).then(() => {
                 expect(_converse.minimized_chats.toggleview.model.get('collapsed')).toBeTruthy();
                 expect(_converse.minimized_chats.toggleview.model.get('collapsed')).toBeTruthy();
                 done();
                 done();
-            });
+            }).catch(_.partial(_converse.log, _, Strophe.LogLevel.FATAL))
         }));
         }));
 
 
         it("shows the number messages received to minimized chats",
         it("shows the number messages received to minimized chats",
@@ -99,7 +100,7 @@
                 contact_jid = mock.cur_names[i].replace(/ /g,'.').toLowerCase() + '@localhost';
                 contact_jid = mock.cur_names[i].replace(/ /g,'.').toLowerCase() + '@localhost';
                 test_utils.openChatBoxFor(_converse, contact_jid);
                 test_utils.openChatBoxFor(_converse, contact_jid);
             }
             }
-            return test_utils.waitUntil(() => _converse.chatboxes.length == 4).then(() => {
+            test_utils.waitUntil(() => _converse.chatboxes.length == 4).then(() => {
                 for (i=0; i<3; i++) {
                 for (i=0; i<3; i++) {
                     chatview = _converse.chatboxviews.get(contact_jid);
                     chatview = _converse.chatboxviews.get(contact_jid);
                     chatview.model.set({'minimized': true});
                     chatview.model.set({'minimized': true});
@@ -111,9 +112,11 @@
                     }).c('body').t('This message is sent to a minimized chatbox').up()
                     }).c('body').t('This message is sent to a minimized chatbox').up()
                     .c('active', {'xmlns': 'http://jabber.org/protocol/chatstates'}).tree();
                     .c('active', {'xmlns': 'http://jabber.org/protocol/chatstates'}).tree();
                     _converse.chatboxes.onMessage(msg);
                     _converse.chatboxes.onMessage(msg);
-                    expect($(_converse.minimized_chats.toggleview.el.querySelector('.unread-message-count')).is(':visible')).toBeTruthy();
-                    expect($(_converse.minimized_chats.toggleview.el.querySelector('.unread-message-count')).text()).toBe((i+1).toString());
                 }
                 }
+                return test_utils.waitUntil(() => chatview.model.messages.length);
+            }).then(() => {
+                expect(u.isVisible(_converse.minimized_chats.toggleview.el.querySelector('.unread-message-count'))).toBeTruthy();
+                expect(_converse.minimized_chats.toggleview.el.querySelector('.unread-message-count').textContent).toBe((3).toString());
                 // Chat state notifications don't increment the unread messages counter
                 // Chat state notifications don't increment the unread messages counter
                 // <composing> state
                 // <composing> state
                 _converse.chatboxes.onMessage($msg({
                 _converse.chatboxes.onMessage($msg({
@@ -122,7 +125,7 @@
                     type: 'chat',
                     type: 'chat',
                     id: (new Date()).getTime()
                     id: (new Date()).getTime()
                 }).c('composing', {'xmlns': 'http://jabber.org/protocol/chatstates'}).tree());
                 }).c('composing', {'xmlns': 'http://jabber.org/protocol/chatstates'}).tree());
-                expect($(_converse.minimized_chats.toggleview.el.querySelector('.unread-message-count')).text()).toBe((i).toString());
+                expect(_converse.minimized_chats.toggleview.el.querySelector('.unread-message-count').textContent).toBe((i).toString());
 
 
                 // <paused> state
                 // <paused> state
                 _converse.chatboxes.onMessage($msg({
                 _converse.chatboxes.onMessage($msg({
@@ -131,7 +134,7 @@
                     type: 'chat',
                     type: 'chat',
                     id: (new Date()).getTime()
                     id: (new Date()).getTime()
                 }).c('paused', {'xmlns': 'http://jabber.org/protocol/chatstates'}).tree());
                 }).c('paused', {'xmlns': 'http://jabber.org/protocol/chatstates'}).tree());
-                expect($(_converse.minimized_chats.toggleview.el.querySelector('.unread-message-count')).text()).toBe((i).toString());
+                expect(_converse.minimized_chats.toggleview.el.querySelector('.unread-message-count').textContent).toBe((i).toString());
 
 
                 // <gone> state
                 // <gone> state
                 _converse.chatboxes.onMessage($msg({
                 _converse.chatboxes.onMessage($msg({
@@ -140,7 +143,7 @@
                     type: 'chat',
                     type: 'chat',
                     id: (new Date()).getTime()
                     id: (new Date()).getTime()
                 }).c('gone', {'xmlns': 'http://jabber.org/protocol/chatstates'}).tree());
                 }).c('gone', {'xmlns': 'http://jabber.org/protocol/chatstates'}).tree());
-                expect($(_converse.minimized_chats.toggleview.el.querySelector('.unread-message-count')).text()).toBe((i).toString());
+                expect(_converse.minimized_chats.toggleview.el.querySelector('.unread-message-count').textContent).toBe((i).toString());
 
 
                 // <inactive> state
                 // <inactive> state
                 _converse.chatboxes.onMessage($msg({
                 _converse.chatboxes.onMessage($msg({
@@ -149,9 +152,9 @@
                     type: 'chat',
                     type: 'chat',
                     id: (new Date()).getTime()
                     id: (new Date()).getTime()
                 }).c('inactive', {'xmlns': 'http://jabber.org/protocol/chatstates'}).tree());
                 }).c('inactive', {'xmlns': 'http://jabber.org/protocol/chatstates'}).tree());
-                expect($(_converse.minimized_chats.toggleview.el.querySelector('.unread-message-count')).text()).toBe((i).toString());
+                expect(_converse.minimized_chats.toggleview.el.querySelector('.unread-message-count').textContent).toBe((i).toString());
                 done();
                 done();
-            });
+            }).catch(_.partial(_converse.log, _, Strophe.LogLevel.FATAL))
         }));
         }));
 
 
         it("shows the number messages received to minimized groupchats",
         it("shows the number messages received to minimized groupchats",
@@ -159,7 +162,7 @@
                 null, ['rosterGroupsFetched'], {},
                 null, ['rosterGroupsFetched'], {},
                 function (done, _converse) {
                 function (done, _converse) {
 
 
-            var room_jid = 'kitchen@conference.shakespeare.lit';
+            const room_jid = 'kitchen@conference.shakespeare.lit';
             test_utils.openAndEnterChatRoom(
             test_utils.openAndEnterChatRoom(
                 _converse, 'kitchen', 'conference.shakespeare.lit', 'fires').then(function () {
                 _converse, 'kitchen', 'conference.shakespeare.lit', 'fires').then(function () {
                 var view = _converse.chatboxviews.get(room_jid);
                 var view = _converse.chatboxviews.get(room_jid);
@@ -175,11 +178,12 @@
                         type: 'groupchat'
                         type: 'groupchat'
                     }).c('body').t(message).tree();
                     }).c('body').t(message).tree();
                 view.model.onMessage(msg);
                 view.model.onMessage(msg);
-
-                expect($(_converse.minimized_chats.toggleview.el.querySelector('.unread-message-count')).is(':visible')).toBeTruthy();
-                expect($(_converse.minimized_chats.toggleview.el.querySelector('.unread-message-count')).text()).toBe('1');
+                return test_utils.waitUntil(() => view.model.messages.length);
+            }).then(() => {
+                expect(u.isVisible(_converse.minimized_chats.toggleview.el.querySelector('.unread-message-count'))).toBeTruthy();
+                expect(_converse.minimized_chats.toggleview.el.querySelector('.unread-message-count').textContent).toBe('1');
                 done();
                 done();
-            });
+            }).catch(_.partial(_converse.log, _, Strophe.LogLevel.FATAL))
         }));
         }));
     });
     });
 }));
 }));

+ 971 - 0
spec/omemo.js

@@ -0,0 +1,971 @@
+(function (root, factory) {
+    define(["jasmine", "mock", "test-utils"], factory);
+} (this, function (jasmine, mock, test_utils) {
+    var Strophe = converse.env.Strophe;
+    var b64_sha1 = converse.env.b64_sha1;
+    var $iq = converse.env.$iq;
+    var $msg = converse.env.$msg;
+    var _ = converse.env._;
+    var u = converse.env.utils;
+
+
+    function deviceListFetched (_converse, jid) {
+        return _.get(_.filter(
+            _converse.connection.IQ_stanzas,
+            iq => iq.nodeTree.querySelector(`iq[to="${jid}"] items[node="eu.siacs.conversations.axolotl.devicelist"]`)
+        ).pop(), 'nodeTree');
+    }
+
+    function ownDeviceHasBeenPublished (_converse) {
+        return _.get(_.filter(
+            _converse.connection.IQ_stanzas,
+            iq => iq.nodeTree.querySelector('iq[from="'+_converse.bare_jid+'"] publish[node="eu.siacs.conversations.axolotl.devicelist"]')
+        ).pop(), 'nodeTree');
+    }
+
+    function bundleHasBeenPublished (_converse) {
+        return _.get(_.filter(
+            _converse.connection.IQ_stanzas,
+            iq => iq.nodeTree.querySelector('publish[node="eu.siacs.conversations.axolotl.bundles:123456789"]')
+        ).pop(), 'nodeTree');
+    }
+
+    function bundleFetched (_converse, jid, device_id) {
+        return _.get(_.filter(
+            _converse.connection.IQ_stanzas,
+            (iq) => iq.nodeTree.querySelector(`iq[to="${jid}"] items[node="eu.siacs.conversations.axolotl.bundles:${device_id}"]`)
+        ).pop(), 'nodeTree');
+    }
+
+    function initializedOMEMO (_converse) {
+        return test_utils.waitUntil(() => deviceListFetched(_converse, _converse.bare_jid))
+        .then(iq_stanza => {
+            const stanza = $iq({
+                'from': _converse.bare_jid,
+                'id': iq_stanza.getAttribute('id'),
+                'to': _converse.bare_jid,
+                'type': 'result',
+            }).c('pubsub', {'xmlns': "http://jabber.org/protocol/pubsub"})
+                .c('items', {'node': "eu.siacs.conversations.axolotl.devicelist"})
+                    .c('item', {'xmlns': "http://jabber.org/protocol/pubsub"}) // TODO: must have an id attribute
+                        .c('list', {'xmlns': "eu.siacs.conversations.axolotl"})
+                            .c('device', {'id': '482886413b977930064a5888b92134fe'});
+            _converse.connection._dataRecv(test_utils.createRequest(stanza));
+            return test_utils.waitUntil(() => ownDeviceHasBeenPublished(_converse))
+        }).then(iq_stanza => {
+            const stanza = $iq({
+                'from': _converse.bare_jid,
+                'id': iq_stanza.getAttribute('id'),
+                'to': _converse.bare_jid,
+                'type': 'result'});
+            _converse.connection._dataRecv(test_utils.createRequest(stanza));
+            return test_utils.waitUntil(() => bundleHasBeenPublished(_converse))
+        }).then(iq_stanza => {
+            const stanza = $iq({
+                'from': _converse.bare_jid,
+                'id': iq_stanza.getAttribute('id'),
+                'to': _converse.bare_jid,
+                'type': 'result'});
+            _converse.connection._dataRecv(test_utils.createRequest(stanza));
+            return _converse.api.waitUntil('OMEMOInitialized');
+        });
+    }
+
+
+    describe("The OMEMO module", function() {
+
+        it("adds methods for encrypting and decrypting messages via AES GCM",
+                mock.initConverseWithPromises(
+                    null, ['rosterGroupsFetched', 'chatBoxesFetched'], {},
+                    function (done, _converse) {
+
+            const message = 'This message will be encrypted'
+            let view;
+            test_utils.createContacts(_converse, 'current', 1);
+            _converse.emit('rosterContactsFetched');
+            const contact_jid = mock.cur_names[0].replace(/ /g,'.').toLowerCase() + '@localhost';
+            test_utils.openChatBoxFor(_converse, contact_jid)
+            .then((v) => {
+                view = v;
+                return view.model.encryptMessage(message);
+            }).then((payload) => {
+                return view.model.decryptMessage(payload);
+            }).then((result) => {
+                expect(result).toBe(message);
+                done();
+            });
+        }));
+
+
+        it("enables encrypted messages to be sent and received",
+                mock.initConverseWithPromises(
+                    null, ['rosterGroupsFetched', 'chatBoxesFetched'], {},
+                    function (done, _converse) {
+
+            let view, sent_stanza;
+            test_utils.createContacts(_converse, 'current', 1);
+            _converse.emit('rosterContactsFetched');
+            const contact_jid = mock.cur_names[0].replace(/ /g,'.').toLowerCase() + '@localhost';
+
+            return test_utils.waitUntil(() => initializedOMEMO(_converse))
+            .then(() => test_utils.openChatBoxFor(_converse, contact_jid))
+            .then(() => test_utils.waitUntil(() => deviceListFetched(_converse, contact_jid)))
+            .then(iq_stanza => {
+                const stanza = $iq({
+                    'from': contact_jid,
+                    'id': iq_stanza.getAttribute('id'),
+                    'to': _converse.connection.jid,
+                    'type': 'result',
+                }).c('pubsub', {'xmlns': "http://jabber.org/protocol/pubsub"})
+                    .c('items', {'node': "eu.siacs.conversations.axolotl.devicelist"})
+                        .c('item', {'xmlns': "http://jabber.org/protocol/pubsub"}) // TODO: must have an id attribute
+                            .c('list', {'xmlns': "eu.siacs.conversations.axolotl"})
+                                .c('device', {'id': '555'});
+                _converse.connection._dataRecv(test_utils.createRequest(stanza));
+                return test_utils.waitUntil(() => _converse.omemo_store);
+            }).then(() => {
+                const devicelist = _converse.devicelists.get({'jid': contact_jid});
+                expect(devicelist.devices.length).toBe(1);
+
+                view = _converse.chatboxviews.get(contact_jid);
+                view.model.set('omemo_active', true);
+
+                const textarea = view.el.querySelector('.chat-textarea');
+                textarea.value = 'This message will be encrypted';
+                view.keyPressed({
+                    target: textarea,
+                    preventDefault: _.noop,
+                    keyCode: 13 // Enter
+                });
+                return test_utils.waitUntil(() => bundleFetched(_converse, contact_jid, '555'));
+            }).then((iq_stanza) => {
+                const stanza = $iq({
+                    'from': contact_jid,
+                    'id': iq_stanza.getAttribute('id'),
+                    'to': _converse.bare_jid,
+                    'type': 'result',
+                }).c('pubsub', {
+                    'xmlns': 'http://jabber.org/protocol/pubsub'
+                    }).c('items', {'node': "eu.siacs.conversations.axolotl.bundles:555"})
+                        .c('item')
+                            .c('bundle', {'xmlns': 'eu.siacs.conversations.axolotl'})
+                                .c('signedPreKeyPublic', {'signedPreKeyId': '4223'}).t(btoa('1111')).up()
+                                .c('signedPreKeySignature').t(btoa('2222')).up()
+                                .c('identityKey').t(btoa('3333')).up()
+                                .c('prekeys')
+                                    .c('preKeyPublic', {'preKeyId': '1'}).t(btoa('1001')).up()
+                                    .c('preKeyPublic', {'preKeyId': '2'}).t(btoa('1002')).up()
+                                    .c('preKeyPublic', {'preKeyId': '3'}).t(btoa('1003'));
+                _converse.connection._dataRecv(test_utils.createRequest(stanza));
+
+                return test_utils.waitUntil(() => bundleFetched(_converse, _converse.bare_jid, '482886413b977930064a5888b92134fe'));
+            }).then(iq_stanza => {
+                const stanza = $iq({
+                    'from': _converse.bare_jid,
+                    'id': iq_stanza.getAttribute('id'),
+                    'to': _converse.bare_jid,
+                    'type': 'result',
+                }).c('pubsub', {
+                    'xmlns': 'http://jabber.org/protocol/pubsub'
+                    }).c('items', {'node': "eu.siacs.conversations.axolotl.bundles:482886413b977930064a5888b92134fe"})
+                        .c('item')
+                            .c('bundle', {'xmlns': 'eu.siacs.conversations.axolotl'})
+                                .c('signedPreKeyPublic', {'signedPreKeyId': '4223'}).t(btoa('100000')).up()
+                                .c('signedPreKeySignature').t(btoa('200000')).up()
+                                .c('identityKey').t(btoa('300000')).up()
+                                .c('prekeys')
+                                    .c('preKeyPublic', {'preKeyId': '1'}).t(btoa('1991')).up()
+                                    .c('preKeyPublic', {'preKeyId': '2'}).t(btoa('1992')).up()
+                                    .c('preKeyPublic', {'preKeyId': '3'}).t(btoa('1993'));
+
+                spyOn(_converse.connection, 'send').and.callFake(stanza => { sent_stanza = stanza });
+                _converse.connection._dataRecv(test_utils.createRequest(stanza));
+                return test_utils.waitUntil(() => sent_stanza);
+            }).then(() => {
+                expect(sent_stanza.toLocaleString()).toBe(
+                    `<message from='dummy@localhost/resource' to='max.frankfurter@localhost' `+
+                             `type='chat' id='${sent_stanza.nodeTree.getAttribute('id')}' xmlns='jabber:client'>`+
+                        `<body>This is an OMEMO encrypted message which your client doesn’t seem to support. Find more information on https://conversations.im/omemo</body>`+
+                        `<encrypted xmlns='eu.siacs.conversations.axolotl'>`+
+                            `<header sid='123456789'>`+
+                                `<key rid='482886413b977930064a5888b92134fe'>eyJ0eXBlIjoxLCJib2R5IjoiYzFwaDNSNzNYNyIsInJlZ2lzdHJhdGlvbklkIjoiMTMzNyJ9</key>`+
+                                `<key rid='555'>eyJ0eXBlIjoxLCJib2R5IjoiYzFwaDNSNzNYNyIsInJlZ2lzdHJhdGlvbklkIjoiMTMzNyJ9</key>`+
+                                `<iv>${sent_stanza.nodeTree.querySelector('iv').textContent}</iv>`+
+                            `</header>`+
+                            `<payload>${sent_stanza.nodeTree.querySelector('payload').textContent}</payload>`+
+                        `</encrypted>`+
+                        `<store xmlns='urn:xmpp:hints'/>`+
+                    `</message>`);
+
+                // Test reception of an encrypted message
+                return view.model.encryptMessage('This is an encrypted message from the contact')
+            }).then((obj) => {
+                // XXX: Normally the key will be encrypted via libsignal.
+                // However, we're mocking libsignal in the tests, so we include
+                // it as plaintext in the message.
+                const key = btoa(JSON.stringify({
+                    'type': 1,
+                    'body': obj.key_and_tag,
+                    'registrationId': '1337'
+                }));
+                const stanza = $msg({
+                        'from': contact_jid,
+                        'to': _converse.connection.jid,
+                        'type': 'chat',
+                        'id': 'qwerty'
+                    }).c('body').t('This is a fallback message').up()
+                        .c('encrypted', {'xmlns': Strophe.NS.OMEMO})
+                            .c('header', {'sid':  '555'})
+                                .c('key', {'rid':  _converse.omemo_store.get('device_id')}).t(key).up()
+                                .c('iv').t(obj.iv)
+                                .up().up()
+                            .c('payload').t(obj.payload);
+                _converse.connection._dataRecv(test_utils.createRequest(stanza));
+                return test_utils.waitUntil(() => view.model.messages.length > 1);
+            }).then(() => {
+                expect(view.model.messages.length).toBe(2);
+                const last_msg = view.model.messages.at(1);
+                expect(view.el.querySelectorAll('.chat-msg__body')[1].textContent.trim())
+                    .toBe('This is an encrypted message from the contact');
+                done();
+            }).catch(_.partial(_converse.log, _, Strophe.LogLevel.FATAL))
+        }));
+
+
+        it("can receive a PreKeySignalMessage",
+                mock.initConverseWithPromises(
+                    null, ['rosterGroupsFetched', 'chatBoxesFetched'], {},
+                    function (done, _converse) {
+
+            _converse.NUM_PREKEYS = 5; // Restrict to 5, otherwise the resulting stanza is too large to easily test
+            let view, sent_stanza;
+            test_utils.createContacts(_converse, 'current', 1);
+            _converse.emit('rosterContactsFetched');
+            const contact_jid = mock.cur_names[0].replace(/ /g,'.').toLowerCase() + '@localhost';
+
+            return test_utils.waitUntil(() => initializedOMEMO(_converse))
+            .then(() => _converse.ChatBox.prototype.encryptMessage('This is an encrypted message from the contact'))
+            .then(obj => {
+                // XXX: Normally the key will be encrypted via libsignal.
+                // However, we're mocking libsignal in the tests, so we include
+                // it as plaintext in the message.
+                const key = btoa(JSON.stringify({
+                    'type': 1,
+                    'body': obj.key_and_tag,
+                    'registrationId': '1337'
+                }));
+                const stanza = $msg({
+                        'from': contact_jid,
+                        'to': _converse.connection.jid,
+                        'type': 'chat',
+                        'id': 'qwerty'
+                    }).c('body').t('This is a fallback message').up()
+                        .c('encrypted', {'xmlns': Strophe.NS.OMEMO})
+                            .c('header', {'sid':  '555'})
+                                .c('key', {'prekey': 'true', 'rid':  _converse.omemo_store.get('device_id')}).t(key).up()
+                                .c('iv').t(obj.iv)
+                                .up().up()
+                            .c('payload').t(obj.payload);
+
+                const generateMissingPreKeys = _converse.omemo_store.generateMissingPreKeys;
+                spyOn(_converse.omemo_store, 'generateMissingPreKeys').and.callFake(() => {
+                    // Since it's difficult to override
+                    // decryptPreKeyWhisperMessage, where a prekey will be
+                    // removed from the store, we do it here, before the
+                    // missing prekeys are generated.
+                    _converse.omemo_store.removePreKey(1);
+                    return generateMissingPreKeys.apply(_converse.omemo_store, arguments);
+                });
+                _converse.connection._dataRecv(test_utils.createRequest(stanza));
+                return test_utils.waitUntil(() => _converse.chatboxviews.get(contact_jid))
+            }).then(iq_stanza => deviceListFetched(_converse, contact_jid))
+            .then(iq_stanza => {
+                const stanza = $iq({
+                    'from': contact_jid,
+                    'id': iq_stanza.getAttribute('id'),
+                    'to': _converse.connection.jid,
+                    'type': 'result',
+                }).c('pubsub', {'xmlns': "http://jabber.org/protocol/pubsub"})
+                    .c('items', {'node': "eu.siacs.conversations.axolotl.devicelist"})
+                        .c('item', {'xmlns': "http://jabber.org/protocol/pubsub"}) // TODO: must have an id attribute
+                            .c('list', {'xmlns': "eu.siacs.conversations.axolotl"})
+                                .c('device', {'id': '555'});
+
+                // XXX: the bundle gets published twice, we want to make sure
+                // that we wait for the 2nd, so we clear all the already sent
+                // stanzas.
+                _converse.connection.IQ_stanzas = [];
+                _converse.connection._dataRecv(test_utils.createRequest(stanza));
+                return test_utils.waitUntil(() => _converse.omemo_store);
+            }).then(() => test_utils.waitUntil(() => bundleHasBeenPublished(_converse)))
+            .then(iq_stanza => {
+                expect(iq_stanza.outerHTML).toBe(
+                    `<iq from="dummy@localhost" type="set" xmlns="jabber:client" id="${iq_stanza.getAttribute('id')}">`+
+                        `<pubsub xmlns="http://jabber.org/protocol/pubsub">`+
+                            `<publish node="eu.siacs.conversations.axolotl.bundles:123456789">`+
+                                `<item>`+
+                                    `<bundle xmlns="eu.siacs.conversations.axolotl">`+
+                                        `<signedPreKeyPublic signedPreKeyId="0">${btoa('1234')}</signedPreKeyPublic>`+
+                                            `<signedPreKeySignature>${btoa('11112222333344445555')}</signedPreKeySignature>`+
+                                            `<identityKey>${btoa('1234')}</identityKey>`+
+                                        `<prekeys>`+
+                                            `<preKeyPublic preKeyId="0">${btoa('1234')}</preKeyPublic>`+
+                                            `<preKeyPublic preKeyId="1">${btoa('1234')}</preKeyPublic>`+
+                                            `<preKeyPublic preKeyId="2">${btoa('1234')}</preKeyPublic>`+
+                                            `<preKeyPublic preKeyId="3">${btoa('1234')}</preKeyPublic>`+
+                                            `<preKeyPublic preKeyId="4">${btoa('1234')}</preKeyPublic>`+
+                                        `</prekeys>`+
+                                    `</bundle>`+
+                                `</item>`+
+                            `</publish>`+
+                        `</pubsub>`+
+                    `</iq>`)
+                const own_device = _converse.devicelists.get(_converse.bare_jid).devices.get(_converse.omemo_store.get('device_id'));
+                expect(own_device.get('bundle').prekeys.length).toBe(5);
+                expect(_converse.omemo_store.generateMissingPreKeys).toHaveBeenCalled();
+                done();
+            }).catch(_.partial(_converse.log, _, Strophe.LogLevel.FATAL))
+        }));
+
+
+        it("updates device lists based on PEP messages",
+            mock.initConverseWithPromises(
+                null, ['rosterGroupsFetched'], {},
+                function (done, _converse) {
+
+            test_utils.createContacts(_converse, 'current', 1);
+            const contact_jid = mock.cur_names[0].replace(/ /g,'.').toLowerCase() + '@localhost';
+
+            // Wait until own devices are fetched
+            test_utils.waitUntil(() => deviceListFetched(_converse, _converse.bare_jid))
+            .then(iq_stanza => {
+                expect(iq_stanza.outerHTML).toBe(
+                    '<iq type="get" from="dummy@localhost" to="dummy@localhost" xmlns="jabber:client" id="'+iq_stanza.getAttribute("id")+'">'+
+                        '<pubsub xmlns="http://jabber.org/protocol/pubsub">'+
+                            '<items node="eu.siacs.conversations.axolotl.devicelist"/>'+
+                        '</pubsub>'+
+                    '</iq>');
+
+                const stanza = $iq({
+                    'from': _converse.bare_jid,
+                    'id': iq_stanza.getAttribute('id'),
+                    'to': _converse.bare_jid,
+                    'type': 'result',
+                }).c('pubsub', {'xmlns': "http://jabber.org/protocol/pubsub"})
+                    .c('items', {'node': "eu.siacs.conversations.axolotl.devicelist"})
+                        .c('item', {'xmlns': "http://jabber.org/protocol/pubsub"}) // TODO: must have an id attribute
+                            .c('list', {'xmlns': "eu.siacs.conversations.axolotl"})
+                                .c('device', {'id': '555'});
+                _converse.connection._dataRecv(test_utils.createRequest(stanza));
+                return test_utils.waitUntil(() => _converse.omemo_store);
+            }).then(() => {
+                expect(_converse.devicelists.length).toBe(1);
+                const devicelist = _converse.devicelists.get(_converse.bare_jid);
+                expect(devicelist.devices.length).toBe(2);
+                expect(devicelist.devices.at(0).get('id')).toBe('555');
+                expect(devicelist.devices.at(1).get('id')).toBe('123456789');
+                return test_utils.waitUntil(() => ownDeviceHasBeenPublished(_converse));
+            }).then(iq_stanza => {
+                const stanza = $iq({
+                    'from': _converse.bare_jid,
+                    'id': iq_stanza.getAttribute('id'),
+                    'to': _converse.bare_jid,
+                    'type': 'result'});
+                _converse.connection._dataRecv(test_utils.createRequest(stanza));
+                return test_utils.waitUntil(() => bundleHasBeenPublished(_converse));
+            }).then(iq_stanza => {
+                const stanza = $iq({
+                    'from': _converse.bare_jid,
+                    'id': iq_stanza.getAttribute('id'),
+                    'to': _converse.bare_jid,
+                    'type': 'result'});
+                _converse.connection._dataRecv(test_utils.createRequest(stanza));
+                return _converse.api.waitUntil('OMEMOInitialized');
+            }).then(() => {
+                let stanza = $msg({
+                    'from': contact_jid,
+                    'to': _converse.bare_jid,
+                    'type': 'headline',
+                    'id': 'update_01',
+                }).c('event', {'xmlns': 'http://jabber.org/protocol/pubsub#event'})
+                    .c('items', {'node': 'eu.siacs.conversations.axolotl.devicelist'})
+                        .c('item')
+                            .c('list', {'xmlns': 'eu.siacs.conversations.axolotl'})
+                                .c('device', {'id': '1234'})
+                                .c('device', {'id': '4223'})
+                _converse.connection._dataRecv(test_utils.createRequest(stanza));
+
+                expect(_converse.devicelists.length).toBe(2);
+                let devices = _converse.devicelists.get(contact_jid).devices;
+                expect(devices.length).toBe(2);
+                expect(_.map(devices.models, 'attributes.id').sort().join()).toBe('1234,4223');
+
+                stanza = $msg({
+                    'from': contact_jid,
+                    'to': _converse.bare_jid,
+                    'type': 'headline',
+                    'id': 'update_02',
+                }).c('event', {'xmlns': 'http://jabber.org/protocol/pubsub#event'})
+                    .c('items', {'node': 'eu.siacs.conversations.axolotl.devicelist'})
+                        .c('item')
+                            .c('list', {'xmlns': 'eu.siacs.conversations.axolotl'})
+                                .c('device', {'id': '4223'})
+                                .c('device', {'id': '4224'})
+                _converse.connection._dataRecv(test_utils.createRequest(stanza));
+
+                expect(_converse.devicelists.length).toBe(2);
+                expect(devices.length).toBe(2);
+                expect(_.map(devices.models, 'attributes.id').sort().join()).toBe('4223,4224');
+
+                // Check that own devicelist gets updated
+                stanza = $msg({
+                    'from': _converse.bare_jid,
+                    'to': _converse.bare_jid,
+                    'type': 'headline',
+                    'id': 'update_03',
+                }).c('event', {'xmlns': 'http://jabber.org/protocol/pubsub#event'})
+                    .c('items', {'node': 'eu.siacs.conversations.axolotl.devicelist'})
+                        .c('item')
+                            .c('list', {'xmlns': 'eu.siacs.conversations.axolotl'})
+                                .c('device', {'id': '123456789'})
+                                .c('device', {'id': '555'})
+                                .c('device', {'id': '777'})
+                _converse.connection._dataRecv(test_utils.createRequest(stanza));
+
+                expect(_converse.devicelists.length).toBe(2);
+                devices = _converse.devicelists.get(_converse.bare_jid).devices;
+                expect(devices.length).toBe(3);
+                expect(_.map(devices.models, 'attributes.id').sort().join()).toBe('123456789,555,777');
+
+                _converse.connection.IQ_stanzas = [];
+
+                // Check that own device gets re-added
+                stanza = $msg({
+                    'from': _converse.bare_jid,
+                    'to': _converse.bare_jid,
+                    'type': 'headline',
+                    'id': 'update_04',
+                }).c('event', {'xmlns': 'http://jabber.org/protocol/pubsub#event'})
+                    .c('items', {'node': 'eu.siacs.conversations.axolotl.devicelist'})
+                        .c('item')
+                            .c('list', {'xmlns': 'eu.siacs.conversations.axolotl'})
+                                .c('device', {'id': '444'})
+                _converse.connection._dataRecv(test_utils.createRequest(stanza));
+
+                return test_utils.waitUntil(() => ownDeviceHasBeenPublished(_converse));
+            }).then(iq_stanza => {
+                // Check that our own device is added again, but that removed
+                // devices are not added.
+                expect(iq_stanza.outerHTML).toBe(
+                    '<iq from="dummy@localhost" type="set" xmlns="jabber:client" id="'+iq_stanza.getAttribute('id')+'">'+
+                        '<pubsub xmlns="http://jabber.org/protocol/pubsub">'+
+                            '<publish node="eu.siacs.conversations.axolotl.devicelist">'+
+                                '<item>'+
+                                    '<list xmlns="eu.siacs.conversations.axolotl">'+
+                                        '<device id="123456789"/>'+
+                                        '<device id="444"/>'+
+                                    '</list>'+
+                                '</item>'+
+                            '</publish>'+
+                        '</pubsub>'+
+                    '</iq>');
+                expect(_converse.devicelists.length).toBe(2);
+                const devices = _converse.devicelists.get(_converse.bare_jid).devices;
+                // The device id for this device (123456789) was also generated and added to the list,
+                // which is why we have 2 devices now.
+                expect(devices.length).toBe(2);
+                expect(_.map(devices.models, 'attributes.id').sort().join()).toBe('123456789,444');
+                done();
+            }).catch(_.partial(_converse.log, _, Strophe.LogLevel.FATAL))
+        }));
+
+
+        it("updates device bundles based on PEP messages",
+            mock.initConverseWithPromises(
+                null, ['rosterGroupsFetched'], {},
+                function (done, _converse) {
+
+            test_utils.createContacts(_converse, 'current');
+            const contact_jid = mock.cur_names[3].replace(/ /g,'.').toLowerCase() + '@localhost';
+
+            test_utils.waitUntil(() => deviceListFetched(_converse, _converse.bare_jid))
+            .then(iq_stanza => {
+                expect(iq_stanza.outerHTML).toBe(
+                    '<iq type="get" from="dummy@localhost" to="dummy@localhost" xmlns="jabber:client" id="'+iq_stanza.getAttribute("id")+'">'+
+                        '<pubsub xmlns="http://jabber.org/protocol/pubsub">'+
+                            '<items node="eu.siacs.conversations.axolotl.devicelist"/>'+
+                        '</pubsub>'+
+                    '</iq>');
+
+                const stanza = $iq({
+                    'from': contact_jid,
+                    'id': iq_stanza.getAttribute('id'),
+                    'to': _converse.bare_jid,
+                    'type': 'result',
+                }).c('pubsub', {'xmlns': "http://jabber.org/protocol/pubsub"})
+                    .c('items', {'node': "eu.siacs.conversations.axolotl.devicelist"})
+                        .c('item', {'xmlns': "http://jabber.org/protocol/pubsub"}) // TODO: must have an id attribute
+                            .c('list', {'xmlns': "eu.siacs.conversations.axolotl"})
+                                .c('device', {'id': '555'});
+                _converse.connection._dataRecv(test_utils.createRequest(stanza));
+                return test_utils.waitUntil(() => _converse.omemo_store);
+            }).then(() => {
+                expect(_converse.devicelists.length).toBe(1);
+                const devicelist = _converse.devicelists.get(_converse.bare_jid);
+                expect(devicelist.devices.length).toBe(2);
+                expect(devicelist.devices.at(0).get('id')).toBe('555');
+                expect(devicelist.devices.at(1).get('id')).toBe('123456789');
+                return test_utils.waitUntil(() => ownDeviceHasBeenPublished(_converse));
+            }).then(iq_stanza => {
+                const stanza = $iq({
+                    'from': _converse.bare_jid,
+                    'id': iq_stanza.getAttribute('id'),
+                    'to': _converse.bare_jid,
+                    'type': 'result'});
+                _converse.connection._dataRecv(test_utils.createRequest(stanza));
+                return test_utils.waitUntil(() => bundleHasBeenPublished(_converse));
+            }).then(iq_stanza => {
+                const stanza = $iq({
+                    'from': _converse.bare_jid,
+                    'id': iq_stanza.getAttribute('id'),
+                    'to': _converse.bare_jid,
+                    'type': 'result'});
+                _converse.connection._dataRecv(test_utils.createRequest(stanza));
+                return _converse.api.waitUntil('OMEMOInitialized');
+            }).then(() => {
+                let stanza = $msg({
+                    'from': contact_jid,
+                    'to': _converse.bare_jid,
+                    'type': 'headline',
+                    'id': 'update_01',
+                }).c('event', {'xmlns': 'http://jabber.org/protocol/pubsub#event'})
+                    .c('items', {'node': 'eu.siacs.conversations.axolotl.bundles:555'})
+                        .c('item')
+                            .c('bundle', {'xmlns': 'eu.siacs.conversations.axolotl'})
+                                .c('signedPreKeyPublic', {'signedPreKeyId': '4223'}).t('1111').up()
+                                .c('signedPreKeySignature').t('2222').up()
+                                .c('identityKey').t('3333').up()
+                                .c('prekeys')
+                                    .c('preKeyPublic', {'preKeyId': '1001'}).up()
+                                    .c('preKeyPublic', {'preKeyId': '1002'}).up()
+                                    .c('preKeyPublic', {'preKeyId': '1003'});
+                _converse.connection._dataRecv(test_utils.createRequest(stanza));
+
+                expect(_converse.devicelists.length).toBe(2);
+                let devicelist = _converse.devicelists.get(contact_jid);
+                expect(devicelist.devices.length).toBe(1);
+                let device = devicelist.devices.at(0);
+                expect(device.get('bundle').identity_key).toBe('3333');
+                expect(device.get('bundle').signed_prekey.public_key).toBe('1111');
+                expect(device.get('bundle').signed_prekey.id).toBe(4223);
+                expect(device.get('bundle').signed_prekey.signature).toBe('2222');
+                expect(device.get('bundle').prekeys.length).toBe(3);
+                expect(device.get('bundle').prekeys[0].id).toBe(1001);
+                expect(device.get('bundle').prekeys[1].id).toBe(1002);
+                expect(device.get('bundle').prekeys[2].id).toBe(1003);
+
+                stanza = $msg({
+                    'from': contact_jid,
+                    'to': _converse.bare_jid,
+                    'type': 'headline',
+                    'id': 'update_02',
+                }).c('event', {'xmlns': 'http://jabber.org/protocol/pubsub#event'})
+                    .c('items', {'node': 'eu.siacs.conversations.axolotl.bundles:555'})
+                        .c('item')
+                            .c('bundle', {'xmlns': 'eu.siacs.conversations.axolotl'})
+                                .c('signedPreKeyPublic', {'signedPreKeyId': '4223'}).t('5555').up()
+                                .c('signedPreKeySignature').t('6666').up()
+                                .c('identityKey').t('7777').up()
+                                .c('prekeys')
+                                    .c('preKeyPublic', {'preKeyId': '2001'}).up()
+                                    .c('preKeyPublic', {'preKeyId': '2002'}).up()
+                                    .c('preKeyPublic', {'preKeyId': '2003'});
+                _converse.connection._dataRecv(test_utils.createRequest(stanza));
+
+                expect(_converse.devicelists.length).toBe(2);
+                devicelist = _converse.devicelists.get(contact_jid);
+                expect(devicelist.devices.length).toBe(1);
+                device = devicelist.devices.at(0);
+                expect(device.get('bundle').identity_key).toBe('7777');
+                expect(device.get('bundle').signed_prekey.public_key).toBe('5555');
+                expect(device.get('bundle').signed_prekey.id).toBe(4223);
+                expect(device.get('bundle').signed_prekey.signature).toBe('6666');
+                expect(device.get('bundle').prekeys.length).toBe(3);
+                expect(device.get('bundle').prekeys[0].id).toBe(2001);
+                expect(device.get('bundle').prekeys[1].id).toBe(2002);
+                expect(device.get('bundle').prekeys[2].id).toBe(2003);
+
+                stanza = $msg({
+                    'from': _converse.bare_jid,
+                    'to': _converse.bare_jid,
+                    'type': 'headline',
+                    'id': 'update_03',
+                }).c('event', {'xmlns': 'http://jabber.org/protocol/pubsub#event'})
+                    .c('items', {'node': 'eu.siacs.conversations.axolotl.bundles:123456789'})
+                        .c('item')
+                            .c('bundle', {'xmlns': 'eu.siacs.conversations.axolotl'})
+                                .c('signedPreKeyPublic', {'signedPreKeyId': '9999'}).t('8888').up()
+                                .c('signedPreKeySignature').t('3333').up()
+                                .c('identityKey').t('1111').up()
+                                .c('prekeys')
+                                    .c('preKeyPublic', {'preKeyId': '3001'}).up()
+                                    .c('preKeyPublic', {'preKeyId': '3002'}).up()
+                                    .c('preKeyPublic', {'preKeyId': '3003'});
+                _converse.connection._dataRecv(test_utils.createRequest(stanza));
+
+                expect(_converse.devicelists.length).toBe(2);
+                devicelist = _converse.devicelists.get(_converse.bare_jid);
+                expect(devicelist.devices.length).toBe(2);
+                expect(devicelist.devices.at(0).get('id')).toBe('555');
+                expect(devicelist.devices.at(1).get('id')).toBe('123456789');
+                device = devicelist.devices.at(1);
+                expect(device.get('bundle').identity_key).toBe('1111');
+                expect(device.get('bundle').signed_prekey.public_key).toBe('8888');
+                expect(device.get('bundle').signed_prekey.id).toBe(9999);
+                expect(device.get('bundle').signed_prekey.signature).toBe('3333');
+                expect(device.get('bundle').prekeys.length).toBe(3);
+                expect(device.get('bundle').prekeys[0].id).toBe(3001);
+                expect(device.get('bundle').prekeys[1].id).toBe(3002);
+                expect(device.get('bundle').prekeys[2].id).toBe(3003);
+                done();
+            }).catch(_.partial(_converse.log, _, Strophe.LogLevel.FATAL))
+        }));
+
+        it("publishes a bundle with which an encrypted session can be created",
+                mock.initConverseWithPromises(
+                    null, ['rosterGroupsFetched', 'chatBoxesFetched'], {},
+                    function (done, _converse) {
+
+            _converse.NUM_PREKEYS = 2; // Restrict to 2, otherwise the resulting stanza is too large to easily test
+
+            test_utils.createContacts(_converse, 'current', 1);
+            _converse.emit('rosterContactsFetched');
+            const contact_jid = mock.cur_names[0].replace(/ /g,'.').toLowerCase() + '@localhost';
+
+            test_utils.waitUntil(() => deviceListFetched(_converse, _converse.bare_jid))
+            .then(iq_stanza => {
+                const stanza = $iq({
+                    'from': contact_jid,
+                    'id': iq_stanza.getAttribute('id'),
+                    'to': _converse.bare_jid,
+                    'type': 'result',
+                }).c('pubsub', {'xmlns': "http://jabber.org/protocol/pubsub"})
+                    .c('items', {'node': "eu.siacs.conversations.axolotl.devicelist"})
+                        .c('item', {'xmlns': "http://jabber.org/protocol/pubsub"}) // TODO: must have an id attribute
+                            .c('list', {'xmlns': "eu.siacs.conversations.axolotl"})
+                                .c('device', {'id': '482886413b977930064a5888b92134fe'});
+                _converse.connection._dataRecv(test_utils.createRequest(stanza));
+                expect(_converse.devicelists.length).toBe(1);
+                return test_utils.openChatBoxFor(_converse, contact_jid);
+
+            }).then(() => ownDeviceHasBeenPublished(_converse))
+              .then(iq_stanza => {
+                const stanza = $iq({
+                    'from': _converse.bare_jid,
+                    'id': iq_stanza.getAttribute('id'),
+                    'to': _converse.bare_jid,
+                    'type': 'result'});
+                _converse.connection._dataRecv(test_utils.createRequest(stanza));
+
+                return test_utils.waitUntil(() => bundleHasBeenPublished(_converse));
+            }).then(iq_stanza => {
+                expect(iq_stanza.outerHTML).toBe(
+                    `<iq from="dummy@localhost" type="set" xmlns="jabber:client" id="${iq_stanza.getAttribute('id')}">`+
+                        `<pubsub xmlns="http://jabber.org/protocol/pubsub">`+
+                            `<publish node="eu.siacs.conversations.axolotl.bundles:123456789">`+
+                                `<item>`+
+                                    `<bundle xmlns="eu.siacs.conversations.axolotl">`+
+                                        `<signedPreKeyPublic signedPreKeyId="0">${btoa('1234')}</signedPreKeyPublic>`+
+                                            `<signedPreKeySignature>${btoa('11112222333344445555')}</signedPreKeySignature>`+
+                                            `<identityKey>${btoa('1234')}</identityKey>`+
+                                        `<prekeys>`+
+                                            `<preKeyPublic preKeyId="0">${btoa('1234')}</preKeyPublic>`+
+                                            `<preKeyPublic preKeyId="1">${btoa('1234')}</preKeyPublic>`+
+                                        `</prekeys>`+
+                                    `</bundle>`+
+                                `</item>`+
+                            `</publish>`+
+                        `</pubsub>`+
+                    `</iq>`)
+
+                const stanza = $iq({
+                    'from': _converse.bare_jid,
+                    'id': iq_stanza.getAttribute('id'),
+                    'to': _converse.bare_jid,
+                    'type': 'result'});
+                _converse.connection._dataRecv(test_utils.createRequest(stanza));
+                return _converse.api.waitUntil('OMEMOInitialized');
+            }).then(done).catch(_.partial(console.error, _));
+        }));
+
+
+        it("adds a toolbar button for starting an encrypted chat session",
+            mock.initConverseWithPromises(
+                null, ['rosterGroupsFetched', 'chatBoxesFetched'], {},
+                function (done, _converse) {
+
+            let modal;
+            test_utils.createContacts(_converse, 'current', 1);
+            _converse.emit('rosterContactsFetched');
+            const contact_jid = mock.cur_names[0].replace(/ /g,'.').toLowerCase() + '@localhost';
+
+            test_utils.waitUntil(() => deviceListFetched(_converse, _converse.bare_jid))
+            .then(iq_stanza => {
+                expect(iq_stanza.outerHTML).toBe(
+                    '<iq type="get" from="dummy@localhost" to="dummy@localhost" xmlns="jabber:client" id="'+iq_stanza.getAttribute("id")+'">'+
+                        '<pubsub xmlns="http://jabber.org/protocol/pubsub">'+
+                            '<items node="eu.siacs.conversations.axolotl.devicelist"/>'+
+                        '</pubsub>'+
+                    '</iq>');
+
+                const stanza = $iq({
+                    'from': _converse.bare_jid,
+                    'id': iq_stanza.getAttribute('id'),
+                    'to': _converse.bare_jid,
+                    'type': 'result',
+                }).c('pubsub', {'xmlns': "http://jabber.org/protocol/pubsub"})
+                    .c('items', {'node': "eu.siacs.conversations.axolotl.devicelist"})
+                        .c('item', {'xmlns': "http://jabber.org/protocol/pubsub"}) // TODO: must have an id attribute
+                            .c('list', {'xmlns': "eu.siacs.conversations.axolotl"})
+                                .c('device', {'id': '482886413b977930064a5888b92134fe'});
+                _converse.connection._dataRecv(test_utils.createRequest(stanza));
+                return test_utils.waitUntil(() => _converse.omemo_store);
+            }).then(() => {
+                expect(_converse.devicelists.length).toBe(1);
+                const devicelist = _converse.devicelists.get(_converse.bare_jid);
+                expect(devicelist.devices.length).toBe(2);
+                expect(devicelist.devices.at(0).get('id')).toBe('482886413b977930064a5888b92134fe');
+                expect(devicelist.devices.at(1).get('id')).toBe('123456789');
+                // Check that own device was published
+                return test_utils.waitUntil(() => ownDeviceHasBeenPublished(_converse));
+            }).then(iq_stanza => {
+                expect(iq_stanza.outerHTML).toBe(
+                    '<iq from="dummy@localhost" type="set" xmlns="jabber:client" id="'+iq_stanza.getAttribute('id')+'">'+
+                        '<pubsub xmlns="http://jabber.org/protocol/pubsub">'+
+                            '<publish node="eu.siacs.conversations.axolotl.devicelist">'+
+                                '<item>'+
+                                    '<list xmlns="eu.siacs.conversations.axolotl">'+
+                                        '<device id="482886413b977930064a5888b92134fe"/>'+
+                                        '<device id="123456789"/>'+
+                                    '</list>'+
+                                '</item>'+
+                            '</publish>'+
+                    '</pubsub>'+
+                    '</iq>');
+
+                const stanza = $iq({
+                    'from': _converse.bare_jid,
+                    'id': iq_stanza.getAttribute('id'),
+                    'to': _converse.bare_jid,
+                    'type': 'result'});
+                _converse.connection._dataRecv(test_utils.createRequest(stanza));
+
+                return test_utils.waitUntil(() => bundleHasBeenPublished(_converse));
+            }).then(iq_stanza => {
+                expect(iq_stanza.getAttributeNames().sort().join()).toBe(["from", "type", "xmlns", "id"].sort().join());
+                expect(iq_stanza.querySelector('prekeys').childNodes.length).toBe(100);
+
+                const signed_prekeys = iq_stanza.querySelectorAll('signedPreKeyPublic');
+                expect(signed_prekeys.length).toBe(1);
+                const signed_prekey = signed_prekeys[0];
+                expect(signed_prekey.getAttribute('signedPreKeyId')).toBe('0')
+                expect(iq_stanza.querySelectorAll('signedPreKeySignature').length).toBe(1);
+                expect(iq_stanza.querySelectorAll('identityKey').length).toBe(1);
+
+                const stanza = $iq({
+                    'from': _converse.bare_jid,
+                    'id': iq_stanza.getAttribute('id'),
+                    'to': _converse.bare_jid,
+                    'type': 'result'});
+                _converse.connection._dataRecv(test_utils.createRequest(stanza));
+                return _converse.api.waitUntil('OMEMOInitialized', 1000);
+            }).then(() => {
+                return test_utils.openChatBoxFor(_converse, contact_jid);
+            }).then(() => {
+                return test_utils.waitUntil(() => deviceListFetched(_converse, contact_jid));
+            }).then(iq_stanza => {
+                expect(iq_stanza.outerHTML).toBe(
+                    '<iq type="get" from="dummy@localhost" to="'+contact_jid+'" xmlns="jabber:client" id="'+iq_stanza.getAttribute("id")+'">'+
+                        '<pubsub xmlns="http://jabber.org/protocol/pubsub">'+
+                            '<items node="eu.siacs.conversations.axolotl.devicelist"/>'+
+                        '</pubsub>'+
+                    '</iq>');
+
+                const stanza = $iq({
+                    'from': contact_jid,
+                    'id': iq_stanza.getAttribute('id'),
+                    'to': _converse.bare_jid,
+                    'type': 'result',
+                }).c('pubsub', {'xmlns': "http://jabber.org/protocol/pubsub"})
+                    .c('items', {'node': "eu.siacs.conversations.axolotl.devicelist"})
+                        .c('item', {'xmlns': "http://jabber.org/protocol/pubsub"}) // TODO: must have an id attribute
+                            .c('list', {'xmlns': "eu.siacs.conversations.axolotl"})
+                                .c('device', {'id': '368866411b877c30064a5f62b917cffe'}).up()
+                                .c('device', {'id': '3300659945416e274474e469a1f0154c'}).up()
+                                .c('device', {'id': '4e30f35051b7b8b42abe083742187228'}).up()
+                                .c('device', {'id': 'ae890ac52d0df67ed7cfdf51b644e901'});
+                _converse.connection._dataRecv(test_utils.createRequest(stanza));
+                const devicelist = _converse.devicelists.get(contact_jid);
+                return test_utils.waitUntil(() => devicelist.devices.length);
+            }).then(() => {
+                expect(_converse.devicelists.length).toBe(2);
+                const devicelist = _converse.devicelists.get(contact_jid);
+                expect(devicelist.devices.length).toBe(4);
+                expect(devicelist.devices.at(0).get('id')).toBe('368866411b877c30064a5f62b917cffe');
+                expect(devicelist.devices.at(1).get('id')).toBe('3300659945416e274474e469a1f0154c');
+                expect(devicelist.devices.at(2).get('id')).toBe('4e30f35051b7b8b42abe083742187228');
+                expect(devicelist.devices.at(3).get('id')).toBe('ae890ac52d0df67ed7cfdf51b644e901');
+                return test_utils.waitUntil(() => _converse.chatboxviews.get(contact_jid).el.querySelector('.chat-toolbar'));
+            }).then(() => {
+                const view = _converse.chatboxviews.get(contact_jid);
+                const toolbar = view.el.querySelector('.chat-toolbar');
+                expect(view.model.get('omemo_active')).toBe(undefined);
+                const toggle = toolbar.querySelector('.toggle-omemo');
+                expect(_.isNull(toggle)).toBe(false);
+                expect(u.hasClass('fa-unlock', toggle)).toBe(true);
+                expect(u.hasClass('fa-lock', toggle)).toBe(false);
+
+                spyOn(view, 'toggleOMEMO').and.callThrough();
+                view.delegateEvents(); // We need to rebind all events otherwise our spy won't be called
+                toolbar.querySelector('.toggle-omemo').click();
+                expect(view.toggleOMEMO).toHaveBeenCalled();
+                expect(view.model.get('omemo_active')).toBe(true);
+
+                return test_utils.waitUntil(() => u.hasClass('fa-lock', toolbar.querySelector('.toggle-omemo')));
+            }).then(() => {
+                const view = _converse.chatboxviews.get(contact_jid);
+                const toolbar = view.el.querySelector('.chat-toolbar');
+                const toggle = toolbar.querySelector('.toggle-omemo');
+                expect(u.hasClass('fa-unlock', toggle)).toBe(false);
+                expect(u.hasClass('fa-lock', toggle)).toBe(true);
+
+                const textarea = view.el.querySelector('.chat-textarea');
+                textarea.value = 'This message will be sent encrypted';
+                view.keyPressed({
+                    target: textarea,
+                    preventDefault: _.noop,
+                    keyCode: 13
+                });
+                done();
+            }).catch(_.partial(console.error, _));
+        }));
+
+
+        it("shows OMEMO device fingerprints in the user details modal",
+            mock.initConverseWithPromises(
+                null, ['rosterGroupsFetched', 'chatBoxesFetched'], {},
+                function (done, _converse) {
+
+            let modal;
+            test_utils.createContacts(_converse, 'current', 1);
+            _converse.emit('rosterContactsFetched');
+            const contact_jid = mock.cur_names[0].replace(/ /g,'.').toLowerCase() + '@localhost';
+            test_utils.openChatBoxFor(_converse, contact_jid)
+            .then(() => {
+                // We simply emit, to avoid doing all the setup work
+                _converse.emit('OMEMOInitialized');
+
+                const view = _converse.chatboxviews.get(contact_jid);
+                const show_modal_button = view.el.querySelector('.show-user-details-modal');
+                show_modal_button.click();
+                modal = view.user_details_modal;
+
+                return test_utils.waitUntil(() => u.isVisible(modal.el), 1000);
+            }).then(() => test_utils.waitUntil(() => deviceListFetched(_converse, contact_jid)))
+            .then(iq_stanza => {
+                expect(iq_stanza.outerHTML).toBe(
+                    `<iq type="get" from="dummy@localhost" to="max.frankfurter@localhost" xmlns="jabber:client" id="${iq_stanza.getAttribute('id')}">`+
+                        `<pubsub xmlns="http://jabber.org/protocol/pubsub"><items node="eu.siacs.conversations.axolotl.devicelist"/></pubsub>`+
+                    `</iq>`);
+
+                const stanza = $iq({
+                    'from': contact_jid,
+                    'id': iq_stanza.getAttribute('id'),
+                    'to': _converse.bare_jid,
+                    'type': 'result',
+                }).c('pubsub', {'xmlns': "http://jabber.org/protocol/pubsub"})
+                    .c('items', {'node': "eu.siacs.conversations.axolotl.devicelist"})
+                        .c('item', {'xmlns': "http://jabber.org/protocol/pubsub"}) // TODO: must have an id attribute
+                            .c('list', {'xmlns': "eu.siacs.conversations.axolotl"})
+                                .c('device', {'id': '555'});
+                _converse.connection._dataRecv(test_utils.createRequest(stanza));
+
+                return test_utils.waitUntil(() => u.isVisible(modal.el), 1000);
+            }).then(() => test_utils.waitUntil(() => bundleFetched(_converse, contact_jid, '555')))
+              .then(iq_stanza => {
+                expect(iq_stanza.outerHTML).toBe(
+                    `<iq type="get" from="dummy@localhost" to="max.frankfurter@localhost" xmlns="jabber:client" id="${iq_stanza.getAttribute('id')}">`+
+                        `<pubsub xmlns="http://jabber.org/protocol/pubsub">`+
+                            `<items node="eu.siacs.conversations.axolotl.bundles:555"/>`+
+                        `</pubsub>`+
+                    `</iq>`);
+
+                const stanza = $iq({
+                    'from': contact_jid,
+                    'id': iq_stanza.getAttribute('id'),
+                    'to': _converse.bare_jid,
+                    'type': 'result',
+                }).c('pubsub', {
+                    'xmlns': 'http://jabber.org/protocol/pubsub'
+                    }).c('items', {'node': "eu.siacs.conversations.axolotl.bundles:555"})
+                        .c('item')
+                            .c('bundle', {'xmlns': 'eu.siacs.conversations.axolotl'})
+                                .c('signedPreKeyPublic', {'signedPreKeyId': '4223'}).t(btoa('1111')).up()
+                                .c('signedPreKeySignature').t(btoa('2222')).up()
+                                .c('identityKey').t('BQmHEOHjsYm3w5M8VqxAtqJmLCi7CaxxsdZz6G0YpuMI').up()
+                                .c('prekeys')
+                                    .c('preKeyPublic', {'preKeyId': '1'}).t(btoa('1001')).up()
+                                    .c('preKeyPublic', {'preKeyId': '2'}).t(btoa('1002')).up()
+                                    .c('preKeyPublic', {'preKeyId': '3'}).t(btoa('1003'));
+                _converse.connection._dataRecv(test_utils.createRequest(stanza));
+
+                const view = _converse.chatboxviews.get(contact_jid);
+                const modal = view.user_details_modal;
+                return test_utils.waitUntil(() => modal.el.querySelectorAll('.fingerprints .fingerprint').length);
+            }).then(() => {
+                const view = _converse.chatboxviews.get(contact_jid);
+                const modal = view.user_details_modal;
+                expect(modal.el.querySelectorAll('.fingerprints .fingerprint').length).toBe(1);
+                const el = modal.el.querySelector('.fingerprints .fingerprint');
+                expect(el.textContent.trim()).toBe(
+                    u.formatFingerprint(u.arrayBufferToHex(u.base64ToArrayBuffer('BQmHEOHjsYm3w5M8VqxAtqJmLCi7CaxxsdZz6G0YpuMI')))
+                );
+
+                expect(modal.el.querySelectorAll('input[type="radio"]').length).toBe(2);
+
+                const devicelist = _converse.devicelists.get(contact_jid);
+                expect(devicelist.devices.get('555').get('trusted')).toBe(0);
+
+                let trusted_radio = modal.el.querySelector('input[type="radio"][name="555"][value="1"]');
+                expect(trusted_radio.checked).toBe(true);
+
+                let untrusted_radio = modal.el.querySelector('input[type="radio"][name="555"][value="-1"]');
+                expect(untrusted_radio.checked).toBe(false);
+
+                // Test that the device can be set to untrusted
+                untrusted_radio.click();
+                trusted_radio = document.querySelector('input[type="radio"][name="555"][value="1"]');
+                expect(trusted_radio.hasAttribute('checked')).toBe(false);
+                expect(devicelist.devices.get('555').get('trusted')).toBe(-1);
+
+                untrusted_radio = document.querySelector('input[type="radio"][name="555"][value="-1"]');
+                expect(untrusted_radio.hasAttribute('checked')).toBe(true);
+
+                trusted_radio.click();
+                expect(devicelist.devices.get('555').get('trusted')).toBe(1);
+                done();
+            }).catch(_.partial(_converse.log, _, Strophe.LogLevel.FATAL))
+        }));
+    });
+
+    describe("A chatbox with an active OMEMO session", function() {
+
+        it("will not show the spoiler toolbar button",
+            mock.initConverseWithPromises(
+                null, ['rosterGroupsFetched'], {},
+                function (done, _converse) {
+            // TODO
+            done()
+        }));
+    });
+}));

+ 5 - 5
spec/presence.js

@@ -47,7 +47,7 @@
                 "<presence xmlns='jabber:client'>"+
                 "<presence xmlns='jabber:client'>"+
                     "<status>Hello world</status>"+
                     "<status>Hello world</status>"+
                     "<priority>0</priority>"+
                     "<priority>0</priority>"+
-                    "<c xmlns='http://jabber.org/protocol/caps' hash='sha-1' node='https://conversejs.org' ver='wmJWAEmiBuDhg0VUoDmqHp3qXJ0='/>"+
+                    "<c xmlns='http://jabber.org/protocol/caps' hash='sha-1' node='https://conversejs.org' ver='ggltNSI5YG/7dFKB57Bk2dRYRU0='/>"+
                 "</presence>"
                 "</presence>"
             );
             );
             _converse.priority = 2;
             _converse.priority = 2;
@@ -57,7 +57,7 @@
                     "<show>away</show>"+
                     "<show>away</show>"+
                     "<status>Going jogging</status>"+
                     "<status>Going jogging</status>"+
                     "<priority>2</priority>"+
                     "<priority>2</priority>"+
-                    "<c xmlns='http://jabber.org/protocol/caps' hash='sha-1' node='https://conversejs.org' ver='wmJWAEmiBuDhg0VUoDmqHp3qXJ0='/>"+
+                    "<c xmlns='http://jabber.org/protocol/caps' hash='sha-1' node='https://conversejs.org' ver='ggltNSI5YG/7dFKB57Bk2dRYRU0='/>"+
                 "</presence>"
                 "</presence>"
             );
             );
 
 
@@ -68,7 +68,7 @@
                     "<show>dnd</show>"+
                     "<show>dnd</show>"+
                     "<status>Doing taxes</status>"+
                     "<status>Doing taxes</status>"+
                     "<priority>0</priority>"+
                     "<priority>0</priority>"+
-                    "<c xmlns='http://jabber.org/protocol/caps' hash='sha-1' node='https://conversejs.org' ver='wmJWAEmiBuDhg0VUoDmqHp3qXJ0='/>"+
+                    "<c xmlns='http://jabber.org/protocol/caps' hash='sha-1' node='https://conversejs.org' ver='ggltNSI5YG/7dFKB57Bk2dRYRU0='/>"+
                 "</presence>"
                 "</presence>"
             );
             );
         }));
         }));
@@ -97,7 +97,7 @@
                     .toBe("<presence xmlns='jabber:client'>"+
                     .toBe("<presence xmlns='jabber:client'>"+
                           "<status>My custom status</status>"+
                           "<status>My custom status</status>"+
                           "<priority>0</priority>"+
                           "<priority>0</priority>"+
-                          "<c xmlns='http://jabber.org/protocol/caps' hash='sha-1' node='https://conversejs.org' ver='wmJWAEmiBuDhg0VUoDmqHp3qXJ0='/>"+
+                          "<c xmlns='http://jabber.org/protocol/caps' hash='sha-1' node='https://conversejs.org' ver='ggltNSI5YG/7dFKB57Bk2dRYRU0='/>"+
                           "</presence>")
                           "</presence>")
 
 
                 return test_utils.waitUntil(function () {
                 return test_utils.waitUntil(function () {
@@ -113,7 +113,7 @@
                 modal.el.querySelector('[type="submit"]').click();
                 modal.el.querySelector('[type="submit"]').click();
                 expect(_converse.connection.send.calls.mostRecent().args[0].toLocaleString())
                 expect(_converse.connection.send.calls.mostRecent().args[0].toLocaleString())
                     .toBe("<presence xmlns='jabber:client'><show>dnd</show><status>My custom status</status><priority>0</priority>"+
                     .toBe("<presence xmlns='jabber:client'><show>dnd</show><status>My custom status</status><priority>0</priority>"+
-                          "<c xmlns='http://jabber.org/protocol/caps' hash='sha-1' node='https://conversejs.org' ver='wmJWAEmiBuDhg0VUoDmqHp3qXJ0='/>"+
+                          "<c xmlns='http://jabber.org/protocol/caps' hash='sha-1' node='https://conversejs.org' ver='ggltNSI5YG/7dFKB57Bk2dRYRU0='/>"+
                           "</presence>")
                           "</presence>")
                 done();
                 done();
             });
             });

+ 13 - 7
spec/roomslist.js

@@ -229,14 +229,16 @@
                                          // have to mock stanza traffic.
                                          // have to mock stanza traffic.
                 }, function (done, _converse) {
                 }, function (done, _converse) {
 
 
+            let view, nick;
+            const room_jid = 'kitchen@conference.shakespeare.lit';
+
             test_utils.waitUntil(() => !_.isUndefined(_converse.rooms_list_view), 500)
             test_utils.waitUntil(() => !_.isUndefined(_converse.rooms_list_view), 500)
             .then(() => test_utils.openAndEnterChatRoom(_converse, 'kitchen', 'conference.shakespeare.lit', 'romeo'))
             .then(() => test_utils.openAndEnterChatRoom(_converse, 'kitchen', 'conference.shakespeare.lit', 'romeo'))
             .then(() => {
             .then(() => {
-                const room_jid = 'kitchen@conference.shakespeare.lit';
-                const view = _converse.chatboxviews.get(room_jid);
+                view = _converse.chatboxviews.get(room_jid);
                 view.model.set({'minimized': true});
                 view.model.set({'minimized': true});
                 const contact_jid = mock.cur_names[5].replace(/ /g,'.').toLowerCase() + '@localhost';
                 const contact_jid = mock.cur_names[5].replace(/ /g,'.').toLowerCase() + '@localhost';
-                const nick = mock.chatroom_names[0];
+                nick = mock.chatroom_names[0];
                 view.model.onMessage(
                 view.model.onMessage(
                     $msg({
                     $msg({
                         from: room_jid+'/'+nick,
                         from: room_jid+'/'+nick,
@@ -260,9 +262,11 @@
                         type: 'groupchat'
                         type: 'groupchat'
                     }).c('body').t('romeo: Your attention is required').tree()
                     }).c('body').t('romeo: Your attention is required').tree()
                 );
                 );
-                var indicator_el = _converse.rooms_list_view.el.querySelector(".msgs-indicator");
+                return test_utils.waitUntil(() => _converse.rooms_list_view.el.querySelectorAll(".msgs-indicator"));
+            }).then(() => {
+                spyOn(view.model, 'incrementUnreadMsgCounter').and.callThrough();
+                const indicator_el = _converse.rooms_list_view.el.querySelector(".msgs-indicator");
                 expect(indicator_el.textContent).toBe('1');
                 expect(indicator_el.textContent).toBe('1');
-
                 view.model.onMessage(
                 view.model.onMessage(
                     $msg({
                     $msg({
                         from: room_jid+'/'+nick,
                         from: room_jid+'/'+nick,
@@ -271,14 +275,16 @@
                         type: 'groupchat'
                         type: 'groupchat'
                     }).c('body').t('romeo: and another thing...').tree()
                     }).c('body').t('romeo: and another thing...').tree()
                 );
                 );
-                indicator_el = _converse.rooms_list_view.el.querySelector(".msgs-indicator");
+                return test_utils.waitUntil(() => view.model.incrementUnreadMsgCounter.calls.count());
+            }).then(() => {
+                let indicator_el = _converse.rooms_list_view.el.querySelector(".msgs-indicator");
                 expect(indicator_el.textContent).toBe('2');
                 expect(indicator_el.textContent).toBe('2');
 
 
                 // When the chat gets maximized again, the unread indicators are removed
                 // When the chat gets maximized again, the unread indicators are removed
                 view.model.set({'minimized': false});
                 view.model.set({'minimized': false});
                 indicator_el = _converse.rooms_list_view.el.querySelector(".msgs-indicator");
                 indicator_el = _converse.rooms_list_view.el.querySelector(".msgs-indicator");
                 expect(_.isNull(indicator_el));
                 expect(_.isNull(indicator_el));
-                room_el = _converse.rooms_list_view.el.querySelector(".available-chatroom");
+                const room_el = _converse.rooms_list_view.el.querySelector(".available-chatroom");
                 expect(_.includes(room_el.classList, 'unread-msgs')).toBeFalsy();
                 expect(_.includes(room_el.classList, 'unread-msgs')).toBeFalsy();
                 done();
                 done();
             }).catch(_.partial(_converse.log, _, Strophe.LogLevel.FATAL));
             }).catch(_.partial(_converse.log, _, Strophe.LogLevel.FATAL));

+ 0 - 11
spec/roster.js

@@ -389,7 +389,6 @@
                     function (done, _converse) {
                     function (done, _converse) {
 
 
                 _converse.roster_groups = true;
                 _converse.roster_groups = true;
-                spyOn(_converse, 'emit');
                 spyOn(_converse.rosterview, 'update').and.callThrough();
                 spyOn(_converse.rosterview, 'update').and.callThrough();
                 _converse.rosterview.render();
                 _converse.rosterview.render();
                 test_utils.openControlBox();
                 test_utils.openControlBox();
@@ -430,7 +429,6 @@
                     function (done, _converse) {
                     function (done, _converse) {
 
 
                 _converse.roster_groups = true;
                 _converse.roster_groups = true;
-                spyOn(_converse, 'emit');
                 spyOn(_converse.rosterview, 'update').and.callThrough();
                 spyOn(_converse.rosterview, 'update').and.callThrough();
                 _converse.rosterview.render();
                 _converse.rosterview.render();
 
 
@@ -477,7 +475,6 @@
 
 
                 _converse.roster_groups = true;
                 _converse.roster_groups = true;
                 var groups = ['colleagues', 'friends'];
                 var groups = ['colleagues', 'friends'];
-                spyOn(_converse, 'emit');
                 spyOn(_converse.rosterview, 'update').and.callThrough();
                 spyOn(_converse.rosterview, 'update').and.callThrough();
                 test_utils.openControlBox();
                 test_utils.openControlBox();
                 _converse.rosterview.render();
                 _converse.rosterview.render();
@@ -576,7 +573,6 @@
                     null, ['rosterGroupsFetched'], {},
                     null, ['rosterGroupsFetched'], {},
                     function (done, _converse) {
                     function (done, _converse) {
 
 
-                spyOn(_converse, 'emit');
                 spyOn(_converse.rosterview, 'update').and.callThrough();
                 spyOn(_converse.rosterview, 'update').and.callThrough();
                 test_utils.openControlBox();
                 test_utils.openControlBox();
                 _converse.roster.create({
                 _converse.roster.create({
@@ -726,7 +722,6 @@
 
 
                 var i, t;
                 var i, t;
                 test_utils.openControlBox();
                 test_utils.openControlBox();
-                spyOn(_converse, 'emit');
                 spyOn(_converse.rosterview, 'update').and.callThrough();
                 spyOn(_converse.rosterview, 'update').and.callThrough();
                 for (i=0; i<mock.pend_names.length; i++) {
                 for (i=0; i<mock.pend_names.length; i++) {
                     _converse.roster.create({
                     _converse.roster.create({
@@ -908,7 +903,6 @@
                 test_utils.waitUntil(() => $(_converse.rosterview.el).find('.roster-group li').length, 700)
                 test_utils.waitUntil(() => $(_converse.rosterview.el).find('.roster-group li').length, 700)
                 .then(function () {
                 .then(function () {
                     var jid, t;
                     var jid, t;
-                    spyOn(_converse, 'emit');
                     spyOn(_converse.rosterview, 'update').and.callThrough();
                     spyOn(_converse.rosterview, 'update').and.callThrough();
                     var $roster = $(_converse.rosterview.el);
                     var $roster = $(_converse.rosterview.el);
                     for (var i=0; i<mock.cur_names.length; i++) {
                     for (var i=0; i<mock.cur_names.length; i++) {
@@ -935,7 +929,6 @@
                     return $(_converse.rosterview.el).find('.roster-group li').length;
                     return $(_converse.rosterview.el).find('.roster-group li').length;
                 }, 700).then(function () {
                 }, 700).then(function () {
                     var jid, t;
                     var jid, t;
-                    spyOn(_converse, 'emit');
                     spyOn(_converse.rosterview, 'update').and.callThrough();
                     spyOn(_converse.rosterview, 'update').and.callThrough();
                     var $roster = $(_converse.rosterview.el);
                     var $roster = $(_converse.rosterview.el);
                     for (var i=0; i<mock.cur_names.length; i++) {
                     for (var i=0; i<mock.cur_names.length; i++) {
@@ -963,7 +956,6 @@
                     return $(_converse.rosterview.el).find('.roster-group li').length;
                     return $(_converse.rosterview.el).find('.roster-group li').length;
                 }, 700).then(function () {
                 }, 700).then(function () {
                     var jid, t;
                     var jid, t;
-                    spyOn(_converse, 'emit');
                     spyOn(_converse.rosterview, 'update').and.callThrough();
                     spyOn(_converse.rosterview, 'update').and.callThrough();
                     var $roster = $(_converse.rosterview.el);
                     var $roster = $(_converse.rosterview.el);
                     for (var i=0; i<mock.cur_names.length; i++) {
                     for (var i=0; i<mock.cur_names.length; i++) {
@@ -991,7 +983,6 @@
                         return $(_converse.rosterview.el).find('.roster-group li').length;
                         return $(_converse.rosterview.el).find('.roster-group li').length;
                 }, 700).then(function () {
                 }, 700).then(function () {
                     var jid, t;
                     var jid, t;
-                    spyOn(_converse, 'emit');
                     spyOn(_converse.rosterview, 'update').and.callThrough();
                     spyOn(_converse.rosterview, 'update').and.callThrough();
                     var $roster = $(_converse.rosterview.el);
                     var $roster = $(_converse.rosterview.el);
                     for (var i=0; i<mock.cur_names.length; i++) {
                     for (var i=0; i<mock.cur_names.length; i++) {
@@ -1020,7 +1011,6 @@
                     }, 500)
                     }, 500)
                 .then(function () {
                 .then(function () {
                     var jid, t;
                     var jid, t;
-                    spyOn(_converse, 'emit');
                     spyOn(_converse.rosterview, 'update').and.callThrough();
                     spyOn(_converse.rosterview, 'update').and.callThrough();
                     var $roster = $(_converse.rosterview.el);
                     var $roster = $(_converse.rosterview.el);
                     for (var i=0; i<mock.cur_names.length; i++) {
                     for (var i=0; i<mock.cur_names.length; i++) {
@@ -1151,7 +1141,6 @@
                         names.push($(item).text().replace(/^\s+|\s+$/g, ''));
                         names.push($(item).text().replace(/^\s+|\s+$/g, ''));
                     }
                     }
                 };
                 };
-                spyOn(_converse, 'emit');
                 spyOn(_converse.rosterview, 'update').and.callThrough();
                 spyOn(_converse.rosterview, 'update').and.callThrough();
                 spyOn(_converse.controlboxtoggle, 'showControlBox').and.callThrough();
                 spyOn(_converse.controlboxtoggle, 'showControlBox').and.callThrough();
                 for (i=0; i<mock.req_names.length; i++) {
                 for (i=0; i<mock.req_names.length; i++) {

+ 3 - 2
spec/user-details-modal.js

@@ -78,8 +78,9 @@
                 remove_contact_button.click();
                 remove_contact_button.click();
                 return test_utils.waitUntil(() => u.isVisible(document.querySelector('.alert-danger')), 2000);
                 return test_utils.waitUntil(() => u.isVisible(document.querySelector('.alert-danger')), 2000);
             }).then(function () {
             }).then(function () {
-                expect(document.querySelector('.alert-danger .modal-title').textContent).toBe("Error");
-                expect(document.querySelector('.modal:not(#user-profile-modal) .modal-body p').textContent.trim())
+                const header = document.querySelector('.alert-danger .modal-title');
+                expect(header.textContent).toBe("Error");
+                expect(u.ancestor(header, '.modal-content').querySelector('.modal-body p').textContent.trim())
                     .toBe("Sorry, there was an error while trying to remove Max Frankfurter as a contact.");
                     .toBe("Sorry, there was an error while trying to remove Max Frankfurter as a contact.");
                 document.querySelector('.alert-danger  button.close').click();
                 document.querySelector('.alert-danger  button.close').click();
                 const show_modal_button = view.el.querySelector('.show-user-details-modal');
                 const show_modal_button = view.el.querySelector('.show-user-details-modal');

+ 4 - 2
src/converse-bookmarks.js

@@ -577,8 +577,10 @@
                 // Add a handler for bookmarks pushed from other connected clients
                 // Add a handler for bookmarks pushed from other connected clients
                 // (from the same user obviously)
                 // (from the same user obviously)
                 _converse.connection.addHandler((message) => {
                 _converse.connection.addHandler((message) => {
-                    if (message.querySelector('event[xmlns="'+Strophe.NS.PUBSUB+'#event"]')) {
-                        _converse.bookmarks.createBookmarksFromStanza(message);
+                    if (sizzle('event[xmlns="'+Strophe.NS.PUBSUB+'#event"] items[node="storage:bookmarks"]', message).length) {
+                        _converse.api.waitUntil('bookmarksInitialized')
+                            .then(() => _converse.bookmarks.createBookmarksFromStanza(message))
+                            .catch(_.partial(_converse.log, _, Strophe.LogLevel.FATAL));
                     }
                     }
                 }, null, 'message', 'headline', null, _converse.bare_jid);
                 }, null, 'message', 'headline', null, _converse.bare_jid);
             });
             });

+ 53 - 38
src/converse-chatboxes.js

@@ -265,7 +265,7 @@
 
 
                     this.messages.on('change:upload', (message) => {
                     this.messages.on('change:upload', (message) => {
                         if (message.get('upload') === _converse.SUCCESS) {
                         if (message.get('upload') === _converse.SUCCESS) {
-                            this.sendMessageStanza(message);
+                            this.sendMessageStanza(this.createMessageStanza(message));
                         }
                         }
                     });
                     });
 
 
@@ -295,8 +295,16 @@
                     const replace = sizzle(`replace[xmlns="${Strophe.NS.MESSAGE_CORRECT}"]`, stanza).pop();
                     const replace = sizzle(`replace[xmlns="${Strophe.NS.MESSAGE_CORRECT}"]`, stanza).pop();
                     if (replace) {
                     if (replace) {
                         const msgid = replace && replace.getAttribute('id') || stanza.getAttribute('id'),
                         const msgid = replace && replace.getAttribute('id') || stanza.getAttribute('id'),
-                            message = msgid && this.messages.findWhere({msgid}),
-                            older_versions = message.get('older_versions') || [];
+                            message = msgid && this.messages.findWhere({msgid});
+
+                        if (!message) {
+                            // XXX: Looks like we received a correction for a
+                            // non-existing message, probably due to MAM.
+                            // Not clear what can be done about this... we'll
+                            // just create it as a separate message for now.
+                            return false;
+                        }
+                        const older_versions = message.get('older_versions') || [];
                         older_versions.push(message.get('message'));
                         older_versions.push(message.get('message'));
                         message.save({
                         message.save({
                             'message': _converse.chatboxes.getMessageBody(stanza),
                             'message': _converse.chatboxes.getMessageBody(stanza),
@@ -355,39 +363,37 @@
                     return stanza;
                     return stanza;
                 },
                 },
 
 
-                sendMessageStanza (message) {
-                    const messageStanza = this.createMessageStanza(message);
-                    _converse.connection.send(messageStanza);
+                sendMessageStanza (stanza) {
+                    _converse.connection.send(stanza);
                     if (_converse.forward_messages) {
                     if (_converse.forward_messages) {
                         // Forward the message, so that other connected resources are also aware of it.
                         // Forward the message, so that other connected resources are also aware of it.
                         _converse.connection.send(
                         _converse.connection.send(
                             $msg({
                             $msg({
                                 'to': _converse.bare_jid,
                                 'to': _converse.bare_jid,
                                 'type': this.get('message_type'),
                                 'type': this.get('message_type'),
-                                'id': message.get('msgid')
                             }).c('forwarded', {'xmlns': Strophe.NS.FORWARD})
                             }).c('forwarded', {'xmlns': Strophe.NS.FORWARD})
                                 .c('delay', {
                                 .c('delay', {
                                         'xmns': Strophe.NS.DELAY,
                                         'xmns': Strophe.NS.DELAY,
                                         'stamp': moment().format()
                                         'stamp': moment().format()
                                 }).up()
                                 }).up()
-                              .cnode(messageStanza.tree())
+                              .cnode(stanza.tree())
                         );
                         );
                     }
                     }
                 },
                 },
 
 
                 getOutgoingMessageAttributes (text, spoiler_hint) {
                 getOutgoingMessageAttributes (text, spoiler_hint) {
-                    const fullname = _converse.xmppstatus.get('fullname'),
-                        is_spoiler = this.get('composing_spoiler');
-
-                    return {
-                        'fullname': fullname,
+                    const is_spoiler = this.get('composing_spoiler');
+                    return _.extend(this.toJSON(), {
+                        'id': _converse.connection.getUniqueId(),
+                        'fullname': _converse.xmppstatus.get('fullname'),
                         'from': _converse.bare_jid,
                         'from': _converse.bare_jid,
                         'sender': 'me',
                         'sender': 'me',
                         'time': moment().format(),
                         'time': moment().format(),
                         'message': text ? u.httpToGeoUri(emojione.shortnameToUnicode(text), _converse) : undefined,
                         'message': text ? u.httpToGeoUri(emojione.shortnameToUnicode(text), _converse) : undefined,
                         'is_spoiler': is_spoiler,
                         'is_spoiler': is_spoiler,
-                        'spoiler_hint': is_spoiler ? spoiler_hint : undefined
-                    };
+                        'spoiler_hint': is_spoiler ? spoiler_hint : undefined,
+                        'type': this.get('message_type')
+                    });
                 },
                 },
 
 
                 sendMessage (attrs) {
                 sendMessage (attrs) {
@@ -396,7 +402,7 @@
                      *  Parameters:
                      *  Parameters:
                      *    (Message) message - The chat message
                      *    (Message) message - The chat message
                      */
                      */
-                    const message = this.messages.findWhere('correcting')
+                    let message = this.messages.findWhere('correcting')
                     if (message) {
                     if (message) {
                         const older_versions = message.get('older_versions') || [];
                         const older_versions = message.get('older_versions') || [];
                         older_versions.push(message.get('message'));
                         older_versions.push(message.get('message'));
@@ -407,9 +413,10 @@
                             'older_versions': older_versions,
                             'older_versions': older_versions,
                             'references': attrs.references
                             'references': attrs.references
                         });
                         });
-                        return this.sendMessageStanza(message);
+                    } else {
+                        message = this.messages.create(attrs);
                     }
                     }
-                    return this.sendMessageStanza(this.messages.create(attrs));
+                    return this.sendMessageStanza(this.createMessageStanza(message));
                 },
                 },
 
 
                 sendChatState () {
                 sendChatState () {
@@ -453,8 +460,7 @@
                                         this.getOutgoingMessageAttributes(), {
                                         this.getOutgoingMessageAttributes(), {
                                         'file': file,
                                         'file': file,
                                         'progress': 0,
                                         'progress': 0,
-                                        'slot_request_url': slot_request_url,
-                                        'type': this.get('message_type'),
+                                        'slot_request_url': slot_request_url
                                     })
                                     })
                                 );
                                 );
                             }
                             }
@@ -512,11 +518,7 @@
                     if (attrs.type === 'groupchat') {
                     if (attrs.type === 'groupchat') {
                         attrs.from = stanza.getAttribute('from');
                         attrs.from = stanza.getAttribute('from');
                         attrs.nick = Strophe.unescapeNode(Strophe.getResourceFromJid(attrs.from));
                         attrs.nick = Strophe.unescapeNode(Strophe.getResourceFromJid(attrs.from));
-                        if (Strophe.getResourceFromJid(attrs.from) === this.get('nick')) {
-                            attrs.sender = 'me';
-                        } else {
-                            attrs.sender = 'them';
-                        }
+                        attrs.sender = attrs.nick === this.get('nick') ? 'me': 'them';
                     } else {
                     } else {
                         attrs.from = Strophe.getBareJidFromJid(stanza.getAttribute('from'));
                         attrs.from = Strophe.getBareJidFromJid(stanza.getAttribute('from'));
                         if (attrs.from === _converse.bare_jid) {
                         if (attrs.from === _converse.bare_jid) {
@@ -541,17 +543,27 @@
                     /* Create a Backbone.Message object inside this chat box
                     /* Create a Backbone.Message object inside this chat box
                      * based on the identified message stanza.
                      * based on the identified message stanza.
                      */
                      */
-                    const attrs = this.getMessageAttributesFromStanza(message, original_stanza);
-                    const is_csn = u.isOnlyChatStateNotification(attrs);
-                    if (is_csn && (attrs.is_delayed || (attrs.type === 'groupchat' && Strophe.getResourceFromJid(attrs.from) == this.get('nick')))) {
-                        // XXX: MUC leakage
-                        // No need showing delayed or our own CSN messages
-                        return;
-                    } else if (!is_csn && !attrs.file && !attrs.message && !attrs.oob_url && attrs.type !== 'error') {
-                        // TODO: handle <subject> messages (currently being done by ChatRoom)
-                        return;
+                    const that = this;
+                    function _create (attrs) {
+                        const is_csn = u.isOnlyChatStateNotification(attrs);
+                        if (is_csn && (attrs.is_delayed ||
+                                (attrs.type === 'groupchat' && Strophe.getResourceFromJid(attrs.from) == that.get('nick')))) {
+                            // XXX: MUC leakage
+                            // No need showing delayed or our own CSN messages
+                            return;
+                        } else if (!is_csn && !attrs.file && !attrs.message && !attrs.oob_url && attrs.type !== 'error') {
+                            // TODO: handle <subject> messages (currently being done by ChatRoom)
+                            return;
+                        } else {
+                            return that.messages.create(attrs);
+                        }
+                    }
+                    const result = this.getMessageAttributesFromStanza(message, original_stanza)
+                    if (typeof result.then === "function") {
+                        return new Promise((resolve, reject) => result.then(attrs => resolve(_create(attrs))));
                     } else {
                     } else {
-                        return this.messages.create(attrs);
+                        const message = _create(result)
+                        return Promise.resolve(message);
                     }
                     }
                 },
                 },
 
 
@@ -621,7 +633,7 @@
 
 
                 onConnected () {
                 onConnected () {
                     this.browserStorage = new Backbone.BrowserStorage.session(
                     this.browserStorage = new Backbone.BrowserStorage.session(
-                        b64_sha1(`converse.chatboxes-${_converse.bare_jid}`));
+                        `converse.chatboxes-${_converse.bare_jid}`);
                     this.registerMessageHandler();
                     this.registerMessageHandler();
                     this.fetch({
                     this.fetch({
                         'add': true,
                         'add': true,
@@ -728,8 +740,11 @@
                     if (chatbox && !chatbox.handleMessageCorrection(stanza)) {
                     if (chatbox && !chatbox.handleMessageCorrection(stanza)) {
                         const msgid = stanza.getAttribute('id'),
                         const msgid = stanza.getAttribute('id'),
                               message = msgid && chatbox.messages.findWhere({msgid});
                               message = msgid && chatbox.messages.findWhere({msgid});
-                        if (!message) { // Only create the message when we're sure it's not a duplicate
-                            chatbox.incrementUnreadMsgCounter(chatbox.createMessage(stanza, original_stanza));
+                        if (!message) {
+                            // Only create the message when we're sure it's not a duplicate
+                            chatbox.createMessage(stanza, original_stanza)
+                                .then(msg => chatbox.incrementUnreadMsgCounter(msg))
+                                .catch(_.partial(_converse.log, _, Strophe.LogLevel.FATAL));
                         }
                         }
                     }
                     }
                     _converse.emit('message', {'stanza': original_stanza, 'chatbox': chatbox});
                     _converse.emit('message', {'stanza': original_stanza, 'chatbox': chatbox});

+ 19 - 16
src/converse-chatview.js

@@ -110,9 +110,11 @@
             emojione.ascii = true;
             emojione.ascii = true;
 
 
             function onWindowStateChanged (data) {
             function onWindowStateChanged (data) {
-                _converse.chatboxviews.each(function (chatboxview) {
-                    chatboxview.onWindowStateChanged(data.state);
-                });
+                if (_converse.chatboxviews) {
+                    _converse.chatboxviews.each(chatboxview => {
+                        chatboxview.onWindowStateChanged(data.state);
+                    });
+                }
             }
             }
             _converse.api.listen.on('windowStateChanged', onWindowStateChanged);
             _converse.api.listen.on('windowStateChanged', onWindowStateChanged);
 
 
@@ -228,32 +230,30 @@
 
 
                 events: {
                 events: {
                     'click button.remove-contact': 'removeContact',
                     'click button.remove-contact': 'removeContact',
-                    'click button.refresh-contact': 'refreshContact'
+                    'click button.refresh-contact': 'refreshContact',
+                    'click .fingerprint-trust .btn input': 'toggleDeviceTrust'
                 },
                 },
 
 
                 initialize () {
                 initialize () {
                     _converse.BootstrapModal.prototype.initialize.apply(this, arguments);
                     _converse.BootstrapModal.prototype.initialize.apply(this, arguments);
                     this.model.on('contactAdded', this.registerContactEventHandlers, this);
                     this.model.on('contactAdded', this.registerContactEventHandlers, this);
+                    this.model.on('change', this.render, this);
                     this.registerContactEventHandlers();
                     this.registerContactEventHandlers();
+                    _converse.emit('userDetailsModalInitialized', this.model);
                 },
                 },
 
 
                 toHTML () {
                 toHTML () {
                     return tpl_user_details_modal(_.extend(
                     return tpl_user_details_modal(_.extend(
                         this.model.toJSON(),
                         this.model.toJSON(),
                         this.model.vcard.toJSON(), {
                         this.model.vcard.toJSON(), {
+                        '_': _,
+                        '__': __,
+                        'view': this,
+                        '_converse': _converse,
                         'allow_contact_removal': _converse.allow_contact_removal,
                         'allow_contact_removal': _converse.allow_contact_removal,
-                        'alt_profile_image': __("The User's Profile Image"),
                         'display_name': this.model.getDisplayName(),
                         'display_name': this.model.getDisplayName(),
                         'is_roster_contact': !_.isUndefined(this.model.contact),
                         'is_roster_contact': !_.isUndefined(this.model.contact),
-                        'label_close': __('Close'),
-                        'label_email': __('Email'),
-                        'label_fullname': __('Full Name'),
-                        'label_jid': __('Jabber ID'),
-                        'label_nickname': __('Nickname'),
-                        'label_remove': __('Remove as contact'),
-                        'label_refresh': __('Refresh'),
-                        'label_role': __('Role'),
-                        'label_url': __('URL')
+                        'utils': u
                     }));
                     }));
                 },
                 },
 
 
@@ -379,6 +379,7 @@
                     this.addSpoilerButton(options);
                     this.addSpoilerButton(options);
                     this.addFileUploadButton();
                     this.addFileUploadButton();
                     this.insertEmojiPicker();
                     this.insertEmojiPicker();
+                    _converse.emit('renderToolbar', this);
                     return this;
                     return this;
                 },
                 },
 
 
@@ -737,14 +738,16 @@
 
 
                     if (!u.hasClass('chat-msg--action', el) && !u.hasClass('chat-msg--action', previous_el) &&
                     if (!u.hasClass('chat-msg--action', el) && !u.hasClass('chat-msg--action', previous_el) &&
                             previous_el.getAttribute('data-from') === from &&
                             previous_el.getAttribute('data-from') === from &&
-                            date.isBefore(moment(previous_el.getAttribute('data-isodate')).add(10, 'minutes'))) {
+                            date.isBefore(moment(previous_el.getAttribute('data-isodate')).add(10, 'minutes')) &&
+                            el.getAttribute('data-encrypted') === previous_el.getAttribute('data-encrypted')) {
                         u.addClass('chat-msg--followup', el);
                         u.addClass('chat-msg--followup', el);
                     }
                     }
                     if (!next_el) { return; }
                     if (!next_el) { return; }
 
 
                     if (!u.hasClass('chat-msg--action', 'el') &&
                     if (!u.hasClass('chat-msg--action', 'el') &&
                             next_el.getAttribute('data-from') === from &&
                             next_el.getAttribute('data-from') === from &&
-                            moment(next_el.getAttribute('data-isodate')).isBefore(date.add(10, 'minutes'))) {
+                            moment(next_el.getAttribute('data-isodate')).isBefore(date.add(10, 'minutes')) &&
+                            el.getAttribute('data-encrypted') === next_el.getAttribute('data-encrypted')) {
                         u.addClass('chat-msg--followup', next_el);
                         u.addClass('chat-msg--followup', next_el);
                     } else {
                     } else {
                         u.removeClass('chat-msg--followup', next_el);
                         u.removeClass('chat-msg--followup', next_el);

+ 1 - 0
src/converse-core.js

@@ -86,6 +86,7 @@
         'converse-muc',
         'converse-muc',
         'converse-muc-views',
         'converse-muc-views',
         'converse-notification',
         'converse-notification',
+        'converse-omemo',
         'converse-oauth',
         'converse-oauth',
         'converse-ping',
         'converse-ping',
         'converse-profile',
         'converse-profile',

+ 0 - 1
src/converse-disco.js

@@ -6,7 +6,6 @@
 
 
 /* This is a Converse.js plugin which add support for XEP-0030: Service Discovery */
 /* This is a Converse.js plugin which add support for XEP-0030: Service Discovery */
 
 
-/*global Backbone, define, window */
 (function (root, factory) {
 (function (root, factory) {
     define(["converse-core", "sizzle"], factory);
     define(["converse-core", "sizzle"], factory);
 }(this, function (converse, sizzle) {
 }(this, function (converse, sizzle) {

+ 13 - 5
src/converse-mam.js

@@ -128,13 +128,21 @@
             //
             //
             // New functions which don't exist yet can also be added.
             // New functions which don't exist yet can also be added.
             ChatBox: {
             ChatBox: {
+
                 getMessageAttributesFromStanza (message, original_stanza) {
                 getMessageAttributesFromStanza (message, original_stanza) {
-                    const attrs = this.__super__.getMessageAttributesFromStanza.apply(this, arguments);
-                    const archive_id = getMessageArchiveID(original_stanza);
-                    if (archive_id) {
-                        attrs.archive_id = archive_id;
+                    function _process (attrs) {
+                        const archive_id = getMessageArchiveID(original_stanza);
+                        if (archive_id) {
+                            attrs.archive_id = archive_id;
+                        }
+                        return attrs;
+                    }
+                    const result = this.__super__.getMessageAttributesFromStanza.apply(this, arguments)
+                    if (result instanceof Promise) {
+                        return new Promise((resolve, reject) => result.then((attrs) => resolve(_process(attrs))).catch(reject));
+                    } else {
+                        return _process(result);
                     }
                     }
-                    return attrs;
                 }
                 }
             },
             },
 
 

+ 2 - 1
src/converse-message-view.js

@@ -158,7 +158,8 @@
                             _.partial(u.renderImageURL, _converse))(url);
                             _.partial(u.renderImageURL, _converse))(url);
                     }
                     }
 
 
-                    let text = this.model.get('message');
+                    const encrypted = this.model.get('encrypted');
+                    let text = encrypted ? this.model.get('plaintext') : this.model.get('message');
                     if (is_me_message) {
                     if (is_me_message) {
                         text = text.replace(/^\/me/, '');
                         text = text.replace(/^\/me/, '');
                     }
                     }

+ 1 - 5
src/converse-minimize.js

@@ -428,11 +428,7 @@
                     this.toggleview = new _converse.MinimizedChatsToggleView({
                     this.toggleview = new _converse.MinimizedChatsToggleView({
                         'model': new _converse.MinimizedChatsToggle({'id': id})
                         'model': new _converse.MinimizedChatsToggle({'id': id})
                     });
                     });
-                    try {
-                        this.toggleview.model.browserStorage = new Backbone.BrowserStorage[storage](id);
-                    } catch (e) {
-                        debugger;
-                    }
+                    this.toggleview.model.browserStorage = new Backbone.BrowserStorage[storage](id);
                     this.toggleview.model.fetch();
                     this.toggleview.model.fetch();
                 },
                 },
 
 

+ 10 - 0
src/converse-modal.js

@@ -65,6 +65,16 @@
                 }
                 }
             });
             });
 
 
+            _converse.api.listen.on('afterTearDown', () => {
+                if (!_converse.chatboxviews) {
+                    return;
+                }
+                const container = _converse.chatboxviews.el.querySelector("#converse-modals");
+                if (container) {
+                    container.innerHTML = '';
+                }
+            });
+
 
 
             /************************ BEGIN API ************************/
             /************************ BEGIN API ************************/
             // We extend the default converse.js API to add methods specific to MUC chat rooms.
             // We extend the default converse.js API to add methods specific to MUC chat rooms.

+ 3 - 1
src/converse-muc.js

@@ -926,7 +926,9 @@
                         if (sender === '') {
                         if (sender === '') {
                             return;
                             return;
                         }
                         }
-                        this.incrementUnreadMsgCounter(this.createMessage(stanza, original_stanza));
+                        this.createMessage(stanza, original_stanza)
+                            .then(msg => this.incrementUnreadMsgCounter(msg))
+                            .catch(_.partial(_converse.log, _, Strophe.LogLevel.FATAL));
                     }
                     }
                     if (sender !== this.get('nick')) {
                     if (sender !== this.get('nick')) {
                         // We only emit an event if it's not our own message
                         // We only emit an event if it's not our own message

+ 1062 - 0
src/converse-omemo.js

@@ -0,0 +1,1062 @@
+// Converse.js
+// http://conversejs.org
+//
+// Copyright (c) 2013-2018, the Converse.js developers
+// Licensed under the Mozilla Public License (MPLv2)
+
+/* global libsignal, ArrayBuffer, parseInt */
+
+(function (root, factory) {
+    define([
+        "converse-core",
+        "templates/toolbar_omemo.html"
+    ], factory);
+}(this, function (converse, tpl_toolbar_omemo) {
+
+    const { Backbone, Promise, Strophe, moment, sizzle, $iq, $msg, _, f, b64_sha1 } = converse.env;
+    const u = converse.env.utils;
+
+    Strophe.addNamespace('OMEMO', "eu.siacs.conversations.axolotl");
+    Strophe.addNamespace('OMEMO_DEVICELIST', Strophe.NS.OMEMO+".devicelist");
+    Strophe.addNamespace('OMEMO_VERIFICATION', Strophe.NS.OMEMO+".verification");
+    Strophe.addNamespace('OMEMO_WHITELISTED', Strophe.NS.OMEMO+".whitelisted");
+    Strophe.addNamespace('OMEMO_BUNDLES', Strophe.NS.OMEMO+".bundles");
+
+    const UNDECIDED = 0;
+    const TRUSTED = 1;
+    const UNTRUSTED = -1;
+    const TAG_LENGTH = 128;
+    const KEY_ALGO = {
+        'name': "AES-GCM",
+        'length': 256
+    };
+
+
+    function parseBundle (bundle_el) {
+        /* Given an XML element representing a user's OMEMO bundle, parse it
+         * and return a map.
+         */
+        const signed_prekey_public_el = bundle_el.querySelector('signedPreKeyPublic'),
+              signed_prekey_signature_el = bundle_el.querySelector('signedPreKeySignature'),
+              identity_key_el = bundle_el.querySelector('identityKey');
+
+        const prekeys = _.map(
+            sizzle(`prekeys > preKeyPublic`, bundle_el),
+            (el) => {
+                return {
+                    'id': parseInt(el.getAttribute('preKeyId'), 10),
+                    'key': el.textContent
+                }
+            });
+        return {
+            'identity_key': bundle_el.querySelector('identityKey').textContent.trim(),
+            'signed_prekey': {
+                'id': parseInt(signed_prekey_public_el.getAttribute('signedPreKeyId'), 10),
+                'public_key': signed_prekey_public_el.textContent,
+                'signature': signed_prekey_signature_el.textContent
+            },
+            'prekeys': prekeys
+        }
+    }
+
+
+    converse.plugins.add('converse-omemo', {
+
+        enabled (_converse) {
+            return !_.isNil(window.libsignal) && !f.includes('converse-omemo', _converse.blacklisted_plugins);
+        },
+
+        dependencies: ["converse-chatview"],
+
+        overrides: {
+
+            ProfileModal: {
+                events: {
+                    'change input.select-all': 'selectAll',
+                    'submit .fingerprint-removal': 'removeSelectedFingerprints'
+                },
+
+                initialize () {
+                    const { _converse } = this.__super__;
+                    this.debouncedRender = _.debounce(this.render, 50);
+                    this.devicelist = _converse.devicelists.get(_converse.bare_jid);
+                    this.devicelist.devices.on('change:bundle', this.debouncedRender, this);
+                    this.devicelist.devices.on('reset', this.debouncedRender, this);
+                    this.devicelist.devices.on('remove', this.debouncedRender, this);
+                    this.devicelist.devices.on('add', this.debouncedRender, this);
+                    return this.__super__.initialize.apply(this, arguments);
+                },
+
+                beforeRender () {
+                    const { _converse } = this.__super__,
+                          device_id = _converse.omemo_store.get('device_id');
+                    this.current_device = this.devicelist.devices.get(device_id);
+                    this.other_devices = this.devicelist.devices.filter(d => (d.get('id') !== device_id));
+                    if (this.__super__.beforeRender) {
+                        return this.__super__.beforeRender.apply(this, arguments);
+                    }
+                },
+
+                selectAll (ev) {
+                    let sibling = u.ancestor(ev.target, 'li');
+                    while (sibling) {
+                        sibling.querySelector('input[type="checkbox"]').checked = ev.target.checked;
+                        sibling = sibling.nextElementSibling;
+                    }
+                },
+
+                removeSelectedFingerprints (ev) {
+                    ev.preventDefault();
+                    ev.stopPropagation();
+                    ev.target.querySelector('.select-all').checked = false
+                    const checkboxes = ev.target.querySelectorAll('.fingerprint-removal-item input[type="checkbox"]:checked'),
+                          device_ids = _.map(checkboxes, 'value');
+                    this.devicelist.removeOwnDevices(device_ids)
+                        .then(this.modal.hide)
+                        .catch(err => {
+                            const { _converse } = this.__super__,
+                                  { __ } = _converse;
+                            _converse.log(err, Strophe.LogLevel.ERROR);
+                            _converse.api.alert.show(
+                                Strophe.LogLevel.ERROR,
+                                __('Error'), [__('Sorry, an error occurred while trying to remove the devices.')]
+                            )
+                        });
+                },
+            },
+
+            UserDetailsModal: {
+                events: {
+                    'click .fingerprint-trust .btn input': 'toggleDeviceTrust'
+                },
+
+                initialize () {
+                    const { _converse } = this.__super__;
+                    const jid = this.model.get('jid');
+                    this.devicelist = _converse.devicelists.get(jid) || _converse.devicelists.create({'jid': jid});
+                    this.devicelist.devices.on('change:bundle', this.render, this);
+                    this.devicelist.devices.on('change:trusted', this.render, this);
+                    this.devicelist.devices.on('remove', this.render, this);
+                    this.devicelist.devices.on('add', this.render, this);
+                    this.devicelist.devices.on('reset', this.render, this);
+                    return this.__super__.initialize.apply(this, arguments);
+                },
+
+                toggleDeviceTrust (ev) {
+                    const radio = ev.target;
+                    const device = this.devicelist.devices.get(radio.getAttribute('name'));
+                    device.save('trusted', parseInt(radio.value, 10));
+                }
+            },
+
+            ChatBox: {
+
+                getBundlesAndBuildSessions () {
+                    const { _converse } = this.__super__;
+                    let devices;
+                    return _converse.getDevicesForContact(this.get('jid'))
+                        .then((their_devices) => {
+                            const device_id = _converse.omemo_store.get('device_id'),
+                                  devicelist = _converse.devicelists.get(_converse.bare_jid),
+                                  own_devices = devicelist.devices.filter(device => device.get('id') !== device_id);
+                            devices = _.concat(own_devices, their_devices.models);
+                            return Promise.all(devices.map(device => device.getBundle()));
+                        }).then(() => this.buildSessions(devices))
+                },
+
+                buildSession (device) {
+                    const { _converse } = this.__super__,
+                          address = new libsignal.SignalProtocolAddress(device.get('jid'), device.get('id')),
+                          sessionBuilder = new libsignal.SessionBuilder(_converse.omemo_store, address),
+                          prekey = device.getRandomPreKey();
+
+                    return device.getBundle()
+                        .then(bundle => {
+                            return sessionBuilder.processPreKey({
+                                'registrationId': parseInt(device.get('id'), 10),
+                                'identityKey': u.base64ToArrayBuffer(bundle.identity_key),
+                                'signedPreKey': {
+                                    'keyId': bundle.signed_prekey.id, // <Number>
+                                    'publicKey': u.base64ToArrayBuffer(bundle.signed_prekey.public_key),
+                                    'signature': u.base64ToArrayBuffer(bundle.signed_prekey.signature)
+                                },
+                                'preKey': {
+                                    'keyId': prekey.id, // <Number>
+                                    'publicKey': u.base64ToArrayBuffer(prekey.key),
+                                }
+                            });
+                        });
+                },
+
+                getSession (device) {
+                    const { _converse } = this.__super__,
+                          address = new libsignal.SignalProtocolAddress(device.get('jid'), device.get('id'));
+
+                    return _converse.omemo_store.loadSession(address.toString()).then(session => {
+                        if (session) {
+                            return Promise.resolve();
+                        } else {
+                            return this.buildSession(device);
+                        }
+                    });
+                },
+
+                getKeyAndTag (string) {
+                    return {
+                        'key': string.slice(0, 43), // 256bit key
+                        'tag': string.slice(43, string.length) // rest is tag
+                    }
+                },
+
+                decryptMessage (obj) {
+                    const { _converse } = this.__super__,
+                          key_obj = {
+                              "alg": "A256GCM",
+                              "ext": true,
+                              "k": obj.key,
+                              "key_ops": ["encrypt","decrypt"],
+                              "kty": "oct"
+                          };
+                    return crypto.subtle.importKey('jwk', key_obj, KEY_ALGO, true, ['encrypt','decrypt'])
+                        .then((key_obj) => {
+                            const algo = {
+                                'name': "AES-GCM",
+                                'iv': u.base64ToArrayBuffer(obj.iv),
+                                'tagLength': TAG_LENGTH 
+                            }
+                            return window.crypto.subtle.decrypt(algo, key_obj, u.base64ToArrayBuffer(obj.payload));
+                        }).then(out => (new TextDecoder()).decode(out));
+                },
+
+                reportDecryptionError (e) {
+                    const { _converse } = this.__super__,
+                          { __ } = _converse;
+                    this.messages.create({
+                        'message': __("Sorry, could not decrypt a received OMEMO message due to an error.") + `${e.name} ${e.message}`,
+                        'type': 'error',
+                    });
+                    _converse.log(e, Strophe.LogLevel.ERROR);
+                },
+
+                decrypt (attrs) {
+                    const { _converse } = this.__super__,
+                          address  = new libsignal.SignalProtocolAddress(attrs.from, parseInt(attrs.encrypted.device_id, 10)),
+                          session_cipher = new window.libsignal.SessionCipher(_converse.omemo_store, address),
+                          libsignal_payload = JSON.parse(atob(attrs.encrypted.key));
+
+                    // https://xmpp.org/extensions/xep-0384.html#usecases-receiving
+                    if (attrs.encrypted.prekey === 'true') {
+                        let plaintext;
+                        return session_cipher.decryptPreKeyWhisperMessage(libsignal_payload.body, 'binary')
+                            .then(key_and_tag => {
+                                if (attrs.encrypted.payload) {
+                                    const aes_data = this.getKeyAndTag(u.arrayBufferToString(key_and_tag));
+                                    return this.decryptMessage(_.extend(attrs.encrypted, {'key': aes_data.key, 'tag': aes_data.tag}));
+                                }
+                                return Promise.resolve();
+                            }).then(pt => {
+                                plaintext = pt;
+                                return _converse.omemo_store.generateMissingPreKeys();
+                            }).then(() => _converse.omemo_store.publishBundle())
+                              .then(() => {
+                                if (plaintext) {
+                                    return _.extend(attrs, {'plaintext': plaintext});
+                                } else {
+                                    return _.extend(attrs, {'is_only_key': true});
+                                }
+                            }).catch((e) => {
+                                this.reportDecryptionError(e);
+                                return attrs;
+                            });
+                    } else {
+                        return session_cipher.decryptWhisperMessage(libsignal_payload.body, 'binary')
+                            .then(key_and_tag => {
+                                const aes_data = this.getKeyAndTag(u.arrayBufferToString(key_and_tag));
+                                return this.decryptMessage(_.extend(attrs.encrypted, {'key': aes_data.key, 'tag': aes_data.tag}));
+                            }).then(plaintext => _.extend(attrs, {'plaintext': plaintext}))
+                              .catch((e) => {
+                                  this.reportDecryptionError(e);
+                                  return attrs;
+                              });
+                    }
+                },
+
+                getEncryptionAttributesfromStanza (stanza, original_stanza, attrs) {
+                    const { _converse } = this.__super__,
+                          encrypted = sizzle(`encrypted[xmlns="${Strophe.NS.OMEMO}"]`, original_stanza).pop(),
+                          header = encrypted.querySelector('header'),
+                          key = sizzle(`key[rid="${_converse.omemo_store.get('device_id')}"]`, encrypted).pop();
+                    if (key) {
+                        attrs['is_encrypted'] = true;
+                        attrs['encrypted'] = {
+                            'device_id': header.getAttribute('sid'),
+                            'iv': header.querySelector('iv').textContent,
+                            'key': key.textContent,
+                            'payload': _.get(encrypted.querySelector('payload'), 'textContent', null),
+                            'prekey': key.getAttribute('prekey')
+                        }
+                        return this.decrypt(attrs);
+                    } else {
+                        return Promise.resolve(attrs);
+                    }
+                },
+
+                getMessageAttributesFromStanza (stanza, original_stanza) {
+                    const encrypted = sizzle(`encrypted[xmlns="${Strophe.NS.OMEMO}"]`, original_stanza).pop();
+                    const attrs = this.__super__.getMessageAttributesFromStanza.apply(this, arguments);
+                    if (!encrypted) {
+                        return attrs;
+                    } else {
+                        return this.getEncryptionAttributesfromStanza(stanza, original_stanza, attrs);
+                    }
+                },
+
+                buildSessions (devices) {
+                    return Promise.all(devices.map(device => this.getSession(device))).then(() => devices);
+                },
+
+                encryptMessage (plaintext) {
+                    // The client MUST use fresh, randomly generated key/IV pairs
+                    // with AES-128 in Galois/Counter Mode (GCM).
+                    const iv = window.crypto.getRandomValues(new window.Uint8Array(16));
+                    let key;
+                    return window.crypto.subtle.generateKey(
+                        KEY_ALGO,
+                        true, // extractable
+                        ["encrypt", "decrypt"] // key usages
+                    ).then((result) => {
+                        key = result;
+                        const algo = {
+                            'name': 'AES-GCM',
+                            'iv': iv,
+                            'tagLength': TAG_LENGTH
+                        }
+                        return window.crypto.subtle.encrypt(algo, key, new TextEncoder().encode(plaintext));
+                    }).then(ciphertext => {
+                        return window.crypto.subtle.exportKey("jwk", key)
+                            .then(key_obj => {
+                                const tag = u.arrayBufferToBase64(ciphertext.slice(ciphertext.byteLength - ((TAG_LENGTH + 7) >> 3)));
+                                return Promise.resolve({
+                                    'key': key_obj.k,
+                                    'tag': tag,
+                                    'key_and_tag': key_obj.k + tag,
+                                    'payload': u.arrayBufferToBase64(ciphertext),
+                                    'iv': u.arrayBufferToBase64(iv)
+                                });
+                            });
+                    });
+                },
+
+                encryptKey (plaintext, device) {
+                    const { _converse } = this.__super__,
+                          address = new libsignal.SignalProtocolAddress(device.get('jid'), device.get('id')),
+                          session_cipher = new window.libsignal.SessionCipher(_converse.omemo_store, address);
+
+                    return session_cipher.encrypt(plaintext).then(payload => ({'payload': payload, 'device': device}));
+                },
+
+                addKeysToMessageStanza (stanza, dicts, iv) {
+                    for (var i in dicts) {
+                        if (Object.prototype.hasOwnProperty.call(dicts, i)) {
+                            const payload = dicts[i].payload,
+                                  device = dicts[i].device,
+                                  prekey = 3 == parseInt(payload.type, 10);
+
+                            stanza.c('key', {'rid': device.get('id') }).t(btoa(JSON.stringify(dicts[i].payload)));
+                            if (prekey) {
+                                stanza.attrs({'prekey': prekey});
+                            }
+                            stanza.up();
+                            if (i == dicts.length-1) {
+                                stanza.c('iv').t(iv).up().up()
+                            }
+                        }
+                    }
+                    return Promise.resolve(stanza);
+                },
+
+                createOMEMOMessageStanza (message, devices) {
+                    const { _converse } = this.__super__, { __ } = _converse;
+                    const body = __("This is an OMEMO encrypted message which your client doesn’t seem to support. "+
+                                    "Find more information on https://conversations.im/omemo");
+
+                    if (!message.get('message')) {
+                        throw new Error("No message body to encrypt!");
+                    }
+                    const stanza = $msg({
+                            'from': _converse.connection.jid,
+                            'to': this.get('jid'),
+                            'type': this.get('message_type'),
+                            'id': message.get('msgid')
+                        }).c('body').t(body).up()
+                            // An encrypted header is added to the message for
+                            // each device that is supposed to receive it.
+                            // These headers simply contain the key that the
+                            // payload message is encrypted with,
+                            // and they are separately encrypted using the
+                            // session corresponding to the counterpart device.
+                            .c('encrypted', {'xmlns': Strophe.NS.OMEMO})
+                                .c('header', {'sid':  _converse.omemo_store.get('device_id')});
+
+                    return this.encryptMessage(message.get('message')).then(obj => {
+                        // The 16 bytes key and the GCM authentication tag (The tag
+                        // SHOULD have at least 128 bit) are concatenated and for each
+                        // intended recipient device, i.e. both own devices as well as
+                        // devices associated with the contact, the result of this
+                        // concatenation is encrypted using the corresponding
+                        // long-standing SignalProtocol session.
+                        const promises = devices
+                            .filter(device => device.get('trusted') != UNTRUSTED)
+                            .map(device => this.encryptKey(obj.key_and_tag, device));
+
+                        return Promise.all(promises)
+                            .then(dicts => this.addKeysToMessageStanza(stanza, dicts, obj.iv))
+                            .then(stanza => {
+                                stanza.c('payload').t(obj.payload).up().up();
+                                stanza.c('store', {'xmlns': Strophe.NS.HINTS});
+                                return stanza;
+                            });
+                    });
+                },
+
+                sendMessage (attrs) {
+                    const { _converse } = this.__super__,
+                          { __ } = _converse;
+
+                    if (this.get('omemo_active') && attrs.message) {
+                        attrs['is_encrypted'] = true;
+                        const message = this.messages.create(attrs);
+                        this.getBundlesAndBuildSessions()
+                            .then(devices => this.createOMEMOMessageStanza(message, devices))
+                            .then(stanza => this.sendMessageStanza(stanza))
+                            .catch(e => {
+                                this.messages.create({
+                                    'message': __("Sorry, could not send the message due to an error.") + ` ${e.message}`,
+                                    'type': 'error',
+                                });
+                                _converse.log(e, Strophe.LogLevel.ERROR);
+                            });
+                        
+                    } else {
+                        return this.__super__.sendMessage.apply(this, arguments);
+                    }
+                }
+            },
+
+            ChatBoxView:  {
+                events: {
+                    'click .toggle-omemo': 'toggleOMEMO'
+                },
+
+                showMessage (message) {
+                    // We don't show a message if it's only keying material
+                    if (!message.get('is_only_key')) {
+                        return this.__super__.showMessage.apply(this, arguments);
+                    }
+                },
+
+                renderOMEMOToolbarButton () {
+                    const { _converse } = this.__super__,
+                          { __ } = _converse;
+                    _converse.contactHasOMEMOSupport(this.model.get('jid')).then((support) => {
+                        if (support) {
+                            const icon = this.el.querySelector('.toggle-omemo'),
+                                html = tpl_toolbar_omemo(_.extend(this.model.toJSON(), {'__': __}));
+                            if (icon) {
+                                icon.outerHTML = html;
+                            } else {
+                                this.el.querySelector('.chat-toolbar').insertAdjacentHTML('beforeend', html);
+                            }
+                        }
+                    }).catch(_.partial(_converse.log, _, Strophe.LogLevel.ERROR));
+                },
+
+                toggleOMEMO (ev) {
+                    ev.preventDefault();
+                    this.model.save({'omemo_active': !this.model.get('omemo_active')});
+                    this.renderOMEMOToolbarButton();
+                }
+            }
+        },
+
+        initialize () {
+            /* The initialize function gets called as soon as the plugin is
+             * loaded by Converse.js's plugin machinery.
+             */
+            const { _converse } = this;
+
+            _converse.api.promises.add(['OMEMOInitialized']);
+
+            _converse.NUM_PREKEYS = 100; // Set here so that tests can override
+
+            function generateFingerprint (device) {
+                if (_.get(device.get('bundle'), 'fingerprint')) {
+                    return;
+                }
+                return device.getBundle().then(bundle => {
+                    bundle['fingerprint'] = u.arrayBufferToHex(u.base64ToArrayBuffer(bundle['identity_key']));
+                    device.save('bundle', bundle);
+                    device.trigger('change:bundle'); // Doesn't get triggered automatically due to pass-by-reference
+                });
+            }
+
+            _converse.generateFingerprints = function (jid) {
+                return _converse.getDevicesForContact(jid)
+                    .then(devices => Promise.all(devices.map(d => generateFingerprint(d))))
+            }
+
+            _converse.getDeviceForContact = function (jid, device_id) {
+                return _converse.getDevicesForContact(jid).then(devices => devices.get(device_id));
+            }
+
+            _converse.getDevicesForContact = function (jid) {
+                let devicelist;
+                return _converse.api.waitUntil('OMEMOInitialized')
+                    .then(() => {
+                        devicelist = _converse.devicelists.get(jid) || _converse.devicelists.create({'jid': jid});
+                        return devicelist.fetchDevices();
+                    }).then(() => devicelist.devices);
+            }
+
+            _converse.contactHasOMEMOSupport = function (jid) {
+                /* Checks whether the contact advertises any OMEMO-compatible devices. */
+                return new Promise((resolve, reject) => {
+                    _converse.getDevicesForContact(jid)
+                        .then((devices) => resolve(devices.length > 0))
+                        .catch(_.partial(_converse.log, _, Strophe.LogLevel.ERROR));
+                });
+            }
+
+
+            function generateDeviceID () {
+                /* Generates a device ID, making sure that it's unique */
+                const existing_ids = _converse.devicelists.get(_converse.bare_jid).devices.pluck('id');
+                let device_id = libsignal.KeyHelper.generateRegistrationId();
+                let i = 0;
+                while (_.includes(existing_ids, device_id)) {
+                    device_id = libsignal.KeyHelper.generateRegistrationId();
+                    i++;
+                    if (i == 10) {
+                        throw new Error("Unable to generate a unique device ID");
+                    }
+                }
+                return device_id.toString();
+            }
+
+
+            _converse.OMEMOStore = Backbone.Model.extend({
+
+                Direction: {
+                    SENDING: 1,
+                    RECEIVING: 2,
+                },
+
+                getIdentityKeyPair () {
+                    const keypair = this.get('identity_keypair');
+                    return Promise.resolve({
+                        'privKey': u.base64ToArrayBuffer(keypair.privKey),
+                        'pubKey': u.base64ToArrayBuffer(keypair.pubKey)
+                    });
+                },
+
+                getLocalRegistrationId () {
+                    return Promise.resolve(parseInt(this.get('device_id'), 10));
+                },
+
+                isTrustedIdentity (identifier, identity_key, direction) {
+                    if (_.isNil(identifier)) {
+                        throw new Error("Can't check identity key for invalid key");
+                    }
+                    if (!(identity_key instanceof ArrayBuffer)) {
+                        throw new Error("Expected identity_key to be an ArrayBuffer");
+                    }
+                    const trusted = this.get('identity_key'+identifier);
+                    if (trusted === undefined) {
+                        return Promise.resolve(true);
+                    }
+                    return Promise.resolve(u.arrayBufferToBase64(identity_key) === trusted);
+                },
+
+                loadIdentityKey (identifier) {
+                    if (_.isNil(identifier)) {
+                        throw new Error("Can't load identity_key for invalid identifier");
+                    }
+                    return Promise.resolve(u.base64ToArrayBuffer(this.get('identity_key'+identifier)));
+                },
+
+                saveIdentity (identifier, identity_key) {
+                    if (_.isNil(identifier)) {
+                        throw new Error("Can't save identity_key for invalid identifier");
+                    }
+                    const address = new libsignal.SignalProtocolAddress.fromString(identifier),
+                          existing = this.get('identity_key'+address.getName());
+
+                    const b64_idkey = u.arrayBufferToBase64(identity_key);
+                    this.save('identity_key'+address.getName(), b64_idkey)
+
+                    if (existing && b64_idkey !== existing) {
+                        return Promise.resolve(true);
+                    } else {
+                        return Promise.resolve(false);
+                    }
+                },
+
+                getPreKeys () {
+                    return this.get('prekeys') || {};
+                },
+
+                loadPreKey (key_id) {
+                    const res = this.getPreKeys()[key_id];
+                    if (res) {
+                        return Promise.resolve({
+                            'privKey': u.base64ToArrayBuffer(res.privKey),
+                            'pubKey': u.base64ToArrayBuffer(res.pubKey)
+                        });
+                    }
+                    return Promise.resolve();
+                },
+
+                storePreKey (key_id, key_pair) {
+                    const prekey = {};
+                    prekey[key_id] = {
+                        'pubKey': u.arrayBufferToBase64(key_pair.pubKey),
+                        'privKey': u.arrayBufferToBase64(key_pair.privKey)
+                    }
+                    this.save('prekeys', _.extend(this.getPreKeys(), prekey));
+                    return Promise.resolve();
+                },
+
+                removePreKey (key_id) {
+                    this.save('prekeys', _.omit(this.getPreKeys(), key_id));
+                    return Promise.resolve();
+                },
+
+                loadSignedPreKey (keyId) {
+                    const res = this.get('signed_prekey');
+                    if (res) {
+                        return Promise.resolve({
+                            'privKey': u.base64ToArrayBuffer(res.privKey),
+                            'pubKey': u.base64ToArrayBuffer(res.pubKey)
+                        });
+                    }
+                    return Promise.resolve();
+                },
+
+                storeSignedPreKey (spk) {
+                    if (typeof spk !== "object") {
+                        // XXX: We've changed the signature of this method from the
+                        // example given in InMemorySignalProtocolStore.
+                        // Should be fine because the libsignal code doesn't
+                        // actually call this method.
+                        throw new Error("storeSignedPreKey: expected an object");
+                    }
+                    this.save('signed_prekey', {
+                        'id': spk.keyId,
+                        'privKey': u.arrayBufferToBase64(spk.keyPair.privKey),
+                        'pubKey': u.arrayBufferToBase64(spk.keyPair.pubKey),
+                        // XXX: The InMemorySignalProtocolStore does not pass
+                        // in or store the signature, but we need it when we
+                        // publish out bundle and this method isn't called from
+                        // within libsignal code, so we modify it to also store
+                        // the signature.
+                        'signature': u.arrayBufferToBase64(spk.signature)
+                    });
+                    return Promise.resolve();
+                },
+
+                removeSignedPreKey (key_id) {
+                    if (this.get('signed_prekey')['id'] === key_id) {
+                        this.unset('signed_prekey');
+                        this.save();
+                    }
+                    return Promise.resolve();
+                },
+
+                loadSession (identifier) {
+                    return Promise.resolve(this.get('session'+identifier));
+                },
+
+                storeSession (identifier, record) {
+                    return Promise.resolve(this.save('session'+identifier, record));
+                },
+
+                removeSession (identifier) {
+                    return Promise.resolve(this.unset('session'+identifier));
+                },
+
+                removeAllSessions (identifier) {
+                    const keys = _.filter(_.keys(this.attributes), (key) => {
+                        if (key.startsWith('session'+identifier)) {
+                            return key;
+                        }
+                    });
+                    const attrs = {};
+                    _.forEach(keys, (key) => {attrs[key] = undefined});
+                    this.save(attrs);
+                    return Promise.resolve();
+                },
+
+                publishBundle () {
+                    const signed_prekey = this.get('signed_prekey');
+                    const stanza = $iq({
+                        'from': _converse.bare_jid,
+                        'type': 'set'
+                    }).c('pubsub', {'xmlns': Strophe.NS.PUBSUB})
+                        .c('publish', {'node': `${Strophe.NS.OMEMO_BUNDLES}:${this.get('device_id')}`})
+                            .c('item')
+                                .c('bundle', {'xmlns': Strophe.NS.OMEMO})
+                                    .c('signedPreKeyPublic', {'signedPreKeyId': signed_prekey.id})
+                                        .t(signed_prekey.pubKey).up()
+                                    .c('signedPreKeySignature').t(signed_prekey.signature).up()
+                                    .c('identityKey').t(this.get('identity_keypair').pubKey).up()
+                                    .c('prekeys');
+                    _.forEach(
+                        this.get('prekeys'),
+                        (prekey, id) => stanza.c('preKeyPublic', {'preKeyId': id}).t(prekey.pubKey).up()
+                    );
+                    return _converse.api.sendIQ(stanza);
+                },
+
+                generateMissingPreKeys () {
+                    const current_keys = this.getPreKeys(),
+                          missing_keys = _.difference(_.invokeMap(_.range(0, _converse.NUM_PREKEYS), Number.prototype.toString), _.keys(current_keys));
+
+                    if (missing_keys.length < 1) {
+                        _converse.log("No missing prekeys to generate for our own device", Strophe.LogLevel.WARN);
+                        return Promise.resolve();
+                    }
+                    return Promise.all(_.map(missing_keys, id => libsignal.KeyHelper.generatePreKey(parseInt(id, 10))))
+                        .then(keys => {
+                            _.forEach(keys, k => this.storePreKey(k.keyId, k.keyPair));
+                            const marshalled_keys = _.map(this.getPreKeys(), k => ({'id': k.keyId, 'key': u.arrayBufferToBase64(k.pubKey)})),
+                                  devicelist = _converse.devicelists.get(_converse.bare_jid),
+                                  device = devicelist.devices.get(this.get('device_id'));
+
+                            return device.getBundle()
+                                .then(bundle => device.save('bundle', _.extend(bundle, {'prekeys': marshalled_keys})));
+                        });
+                },
+
+                generateBundle () {
+                    /* The first thing that needs to happen if a client wants to
+                     * start using OMEMO is they need to generate an IdentityKey
+                     * and a Device ID. The IdentityKey is a Curve25519 [6]
+                     * public/private Key pair. The Device ID is a randomly
+                     * generated integer between 1 and 2^31 - 1.
+                     */
+                    const bundle = {};
+                    return libsignal.KeyHelper.generateIdentityKeyPair()
+                        .then(identity_keypair => {
+                            const identity_key = u.arrayBufferToBase64(identity_keypair.pubKey),
+                                  device_id = generateDeviceID();
+
+                            bundle['identity_key'] = identity_key;
+                            bundle['device_id'] = device_id;
+                            this.save({
+                                'device_id': device_id,
+                                'identity_keypair': {
+                                    'privKey': u.arrayBufferToBase64(identity_keypair.privKey),
+                                    'pubKey': identity_key
+                                },
+                                'identity_key': identity_key
+                            });
+                            return libsignal.KeyHelper.generateSignedPreKey(identity_keypair, 0);
+                        }).then(signed_prekey => {
+                            _converse.omemo_store.storeSignedPreKey(signed_prekey);
+                            bundle['signed_prekey'] = {
+                                'id': signed_prekey.keyId,
+                                'public_key': u.arrayBufferToBase64(signed_prekey.keyPair.privKey),
+                                'signature': u.arrayBufferToBase64(signed_prekey.signature)
+                            }
+                            return Promise.all(_.map(_.range(0, _converse.NUM_PREKEYS), id => libsignal.KeyHelper.generatePreKey(id)));
+                        }).then(keys => {
+                            _.forEach(keys, k => _converse.omemo_store.storePreKey(k.keyId, k.keyPair));
+                            const devicelist = _converse.devicelists.get(_converse.bare_jid),
+                                  device = devicelist.devices.create({'id': bundle.device_id, 'jid': _converse.bare_jid}),
+                                  marshalled_keys = _.map(keys, k => ({'id': k.keyId, 'key': u.arrayBufferToBase64(k.keyPair.pubKey)}));
+                            bundle['prekeys'] = marshalled_keys;
+                            device.save('bundle', bundle);
+                        });
+                },
+
+                fetchSession () {
+                    if (_.isUndefined(this._setup_promise)) {
+                        this._setup_promise = new Promise((resolve, reject) => {
+                            this.fetch({
+                                'success': () => {
+                                    if (!_converse.omemo_store.get('device_id')) {
+                                        this.generateBundle().then(resolve).catch(resolve);
+                                    } else {
+                                        resolve();
+                                    }
+                                },
+                                'error': () => {
+                                    this.generateBundle().then(resolve).catch(resolve);
+                                }
+                            });
+                        });
+                    }
+                    return this._setup_promise;
+                }
+            });
+
+            _converse.Device = Backbone.Model.extend({
+                defaults: {
+                    'trusted': UNDECIDED
+                },
+
+                getRandomPreKey () {
+                    // XXX: assumes that the bundle has already been fetched
+                    const bundle = this.get('bundle');
+                    return bundle.prekeys[u.getRandomInt(bundle.prekeys.length)];
+                },
+
+                fetchBundleFromServer () {
+                    const stanza = $iq({
+                        'type': 'get',
+                        'from': _converse.bare_jid,
+                        'to': this.get('jid')
+                    }).c('pubsub', {'xmlns': Strophe.NS.PUBSUB})
+                        .c('items', {'node': `${Strophe.NS.OMEMO_BUNDLES}:${this.get('id')}`});
+
+                    return _converse.api.sendIQ(stanza)
+                        .then(iq => {
+                            const publish_el = sizzle(`items[node="${Strophe.NS.OMEMO_BUNDLES}:${this.get('id')}"]`, iq).pop(),
+                                    bundle_el = sizzle(`bundle[xmlns="${Strophe.NS.OMEMO}"]`, publish_el).pop(),
+                                    bundle = parseBundle(bundle_el);
+                            this.save('bundle', bundle);
+                            return bundle;
+                        }).catch(iq => {
+                            _converse.log(iq.outerHTML, Strophe.LogLevel.ERROR);
+                        });
+                },
+
+                getBundle () {
+                    /* Fetch and save the bundle information associated with
+                     * this device, if the information is not at hand already.
+                     */
+                    if (this.get('bundle')) {
+                        return Promise.resolve(this.get('bundle'), this);
+                    } else {
+                        return this.fetchBundleFromServer();
+                    }
+                }
+            });
+
+            _converse.Devices = Backbone.Collection.extend({
+                model: _converse.Device,
+            });
+
+            _converse.DeviceList = Backbone.Model.extend({
+                idAttribute: 'jid',
+
+                initialize () {
+                    this.devices = new _converse.Devices();
+                    const id = `converse.devicelist-${_converse.bare_jid}-${this.get('jid')}`;
+                    this.devices.browserStorage = new Backbone.BrowserStorage.session(id);
+                    this.fetchDevices();
+                },
+
+                fetchDevices () {
+                    if (_.isUndefined(this._devices_promise)) {
+                        this._devices_promise = new Promise((resolve, reject) => {
+                            this.devices.fetch({
+                                'success': (collection) => {
+                                    if (collection.length === 0) {
+                                        this.fetchDevicesFromServer()
+                                            .then(ids => this.publishCurrentDevice(ids))
+                                            .then(resolve)
+                                            .catch(resolve);
+                                    } else {
+                                        resolve();
+                                    }
+                                }
+                            });
+                        });
+                    }
+                    return this._devices_promise;
+                },
+
+                publishCurrentDevice (device_ids) {
+                    if (this.get('jid') !== _converse.bare_jid) {
+                        // We only publish for ourselves.
+                        return Promise.resolve();
+                    }
+                    return restoreOMEMOSession()
+                        .then(() => {
+                            const device_id = _converse.omemo_store.get('device_id'),
+                                  own_device = this.devices.findWhere({'id': device_id});
+
+                            if (!_.includes(device_ids, device_id)) {
+                                return this.publishDevices();
+                            }
+                        });
+                },
+
+                fetchDevicesFromServer () {
+                    const stanza = $iq({
+                        'type': 'get',
+                        'from': _converse.bare_jid,
+                        'to': this.get('jid')
+                    }).c('pubsub', {'xmlns': Strophe.NS.PUBSUB})
+                        .c('items', {'node': Strophe.NS.OMEMO_DEVICELIST});
+                    return _converse.api.sendIQ(stanza)
+                        .then(iq => {
+                            const device_ids = _.map(sizzle(`list[xmlns="${Strophe.NS.OMEMO}"] device`, iq), dev => dev.getAttribute('id'));
+                            _.forEach(device_ids, id => this.devices.create({'id': id, 'jid': this.get('jid')}));
+                            return device_ids;
+                        });
+                },
+
+                publishDevices () {
+                    const stanza = $iq({
+                        'from': _converse.bare_jid,
+                        'type': 'set'
+                    }).c('pubsub', {'xmlns': Strophe.NS.PUBSUB})
+                        .c('publish', {'node': Strophe.NS.OMEMO_DEVICELIST})
+                            .c('item')
+                                .c('list', {'xmlns': Strophe.NS.OMEMO})
+                    this.devices.each(device => stanza.c('device', {'id': device.get('id')}).up());
+                    return _converse.api.sendIQ(stanza);
+                },
+
+                removeOwnDevices (device_ids) {
+                    if (this.get('jid') !== _converse.bare_jid) {
+                        throw new Error("Cannot remove devices from someone else's device list");
+                    }
+                    _.forEach(device_ids, (device_id) => this.devices.get(device_id).destroy());
+                    return this.publishDevices();
+                }
+            });
+
+            _converse.DeviceLists = Backbone.Collection.extend({
+                model: _converse.DeviceList,
+            });
+
+
+            _converse.omemo = {
+
+            }
+
+            function fetchDeviceLists () {
+                return new Promise((resolve, reject) => _converse.devicelists.fetch({
+                    'success': resolve
+                }));
+            }
+
+            function fetchOwnDevices () {
+                return fetchDeviceLists().then(() => {
+                    let own_devicelist = _converse.devicelists.get(_converse.bare_jid);
+                    if (_.isNil(own_devicelist)) {
+                        own_devicelist = _converse.devicelists.create({'jid': _converse.bare_jid});
+                    }
+                    return own_devicelist.fetchDevices();
+                });
+            }
+
+            function updateBundleFromStanza (stanza) {
+                const items_el = sizzle(`items`, stanza).pop();
+                if (!items_el || !items_el.getAttribute('node').startsWith(Strophe.NS.OMEMO_BUNDLES)) {
+                    return;
+                }
+                const device_id = items_el.getAttribute('node').split(':')[1],
+                      jid = stanza.getAttribute('from'),
+                      bundle_el = sizzle(`item > bundle`, items_el).pop(),
+                      devicelist = _converse.devicelists.get(jid) || _converse.devicelists.create({'jid': jid}),
+                      device = devicelist.devices.get(device_id) || devicelist.devices.create({'id': device_id, 'jid': jid});
+                device.save({'bundle': parseBundle(bundle_el)});
+            }
+
+            function updateDevicesFromStanza (stanza) {
+                const items_el = sizzle(`items[node="${Strophe.NS.OMEMO_DEVICELIST}"]`, stanza).pop();
+                if (!items_el) {
+                    return;
+                }
+                const device_ids = _.map(
+                    sizzle(`item list[xmlns="${Strophe.NS.OMEMO}"] device`, items_el),
+                    (device) => device.getAttribute('id')
+                );
+                const jid = stanza.getAttribute('from'),
+                      devicelist = _converse.devicelists.get(jid) || _converse.devicelists.create({'jid': jid}),
+                      devices = devicelist.devices,
+                      removed_ids = _.difference(devices.pluck('id'), device_ids);
+
+                _.forEach(removed_ids, (id) => {
+                    if (jid === _converse.bare_jid && id === _converse.omemo_store.get('device_id')) {
+                        // We don't remove the current device
+                        return
+                    }
+                    devices.get(id).destroy();
+                });
+
+                _.forEach(device_ids, (device_id) => {
+                    if (!devices.get(device_id)) {
+                        devices.create({'id': device_id, 'jid': jid})
+                    }
+                });
+                if (Strophe.getBareJidFromJid(jid) === _converse.bare_jid) {
+                    // Make sure our own device is on the list (i.e. if it was
+                    // removed, add it again.
+                    _converse.devicelists.get(_converse.bare_jid).publishCurrentDevice(device_ids);
+                }
+            }
+
+            function registerPEPPushHandler () {
+                // Add a handler for devices pushed from other connected clients
+                _converse.connection.addHandler((message) => {
+                    try {
+                        if (sizzle(`event[xmlns="${Strophe.NS.PUBSUB}#event"]`, message).length) {
+                            updateDevicesFromStanza(message);
+                            updateBundleFromStanza(message);
+                        }
+                    } catch (e) {
+                        _converse.log(e.message, Strophe.LogLevel.ERROR);
+                    }
+                    return true;
+                }, null, 'message', 'headline');
+            }
+
+            function restoreOMEMOSession () {
+                if (_.isUndefined(_converse.omemo_store))  {
+                    const storage = _converse.config.get('storage'),
+                          id = `converse.omemosession-${_converse.bare_jid}`;
+                    _converse.omemo_store = new _converse.OMEMOStore({'id': id});
+                    _converse.omemo_store.browserStorage = new Backbone.BrowserStorage[storage](id);
+                }
+                return _converse.omemo_store.fetchSession();
+            }
+
+            function initOMEMO() {
+                _converse.devicelists = new _converse.DeviceLists();
+                const storage = _converse.config.get('storage'),
+                      id = `converse.devicelists-${_converse.bare_jid}`;
+                _converse.devicelists.browserStorage = new Backbone.BrowserStorage[storage](id);
+
+                fetchOwnDevices()
+                    .then(() => restoreOMEMOSession())
+                    .then(() => _converse.omemo_store.publishBundle())
+                    .then(() => _converse.emit('OMEMOInitialized'))
+                    .catch(_.partial(_converse.log, _, Strophe.LogLevel.ERROR));
+            }
+
+            _converse.api.listen.on('afterTearDown', () => {
+                _converse.devicelists.reset();
+                delete _converse.omemo_store;
+            });
+            _converse.api.listen.on('connected', registerPEPPushHandler);
+            _converse.api.listen.on('renderToolbar', (view) => view.renderOMEMOToolbarButton());
+            _converse.api.listen.on('statusInitialized', initOMEMO);
+            _converse.api.listen.on('addClientFeatures',
+                () => _converse.api.disco.own.features.add(`${Strophe.NS.OMEMO_DEVICELIST}+notify`));
+
+            _converse.api.listen.on('userDetailsModalInitialized', (contact) => {
+                const jid = contact.get('jid');
+                _converse.generateFingerprints(jid).catch(_.partial(_converse.log, _, Strophe.LogLevel.ERROR));
+            });
+
+            _converse.api.listen.on('profileModalInitialized', (contact) => {
+                _converse.generateFingerprints(_converse.bare_jid).catch(_.partial(_converse.log, _, Strophe.LogLevel.ERROR));
+            });
+        }
+    });
+}));

+ 1 - 1
src/converse-otr.js

@@ -237,7 +237,7 @@
                             this.trigger('showReceivedOTRMessage', msg);
                             this.trigger('showReceivedOTRMessage', msg);
                         });
                         });
                         this.otr.on('io', (msg) => {
                         this.otr.on('io', (msg) => {
-                            this.sendMessage(new _converse.Message({'message':msg}));
+                            this.sendMessage({'message':msg});
                         });
                         });
                         this.otr.on('error', (msg) => {
                         this.otr.on('error', (msg) => {
                             this.trigger('showOTRError', msg);
                             this.trigger('showOTRError', msg);

+ 14 - 5
src/converse-profile.js

@@ -48,32 +48,41 @@
                 events: {
                 events: {
                     'click .change-avatar': "openFileSelection",
                     'click .change-avatar': "openFileSelection",
                     'change input[type="file"': "updateFilePreview",
                     'change input[type="file"': "updateFilePreview",
-                    'submit form': 'onFormSubmitted'
+                    'submit .profile-form': 'onFormSubmitted'
                 },
                 },
 
 
                 initialize () {
                 initialize () {
-                    _converse.BootstrapModal.prototype.initialize.apply(this, arguments);
                     this.model.on('change', this.render, this);
                     this.model.on('change', this.render, this);
+                    _converse.BootstrapModal.prototype.initialize.apply(this, arguments);
+                    _converse.emit('profileModalInitialized', this.model);
                 },
                 },
 
 
                 toHTML () {
                 toHTML () {
                     return tpl_profile_modal(_.extend(
                     return tpl_profile_modal(_.extend(
                         this.model.toJSON(),
                         this.model.toJSON(),
                         this.model.vcard.toJSON(), {
                         this.model.vcard.toJSON(), {
+                        '_': _,
+                        '__': __,
+                        '_converse': _converse,
+                        'alt_avatar': __('Your avatar image'),
                         'heading_profile': __('Your Profile'),
                         'heading_profile': __('Your Profile'),
                         'label_close': __('Close'),
                         'label_close': __('Close'),
                         'label_email': __('Email'),
                         'label_email': __('Email'),
                         'label_fullname': __('Full Name'),
                         'label_fullname': __('Full Name'),
-                        'label_nickname': __('Nickname'),
                         'label_jid': __('XMPP Address (JID)'),
                         'label_jid': __('XMPP Address (JID)'),
+                        'label_nickname': __('Nickname'),
                         'label_role': __('Role'),
                         'label_role': __('Role'),
                         'label_role_help': __('Use commas to separate multiple roles. Your roles are shown next to your name on your chat messages.'),
                         'label_role_help': __('Use commas to separate multiple roles. Your roles are shown next to your name on your chat messages.'),
-                        'label_save': __('Save'),
                         'label_url': __('URL'),
                         'label_url': __('URL'),
-                        'alt_avatar': __('Your avatar image')
+                        'utils': u,
+                        'view': this
                     }));
                     }));
                 },
                 },
 
 
+                afterRender () {
+                    this.tabs = _.map(this.el.querySelectorAll('.nav-item'), (tab) => new bootstrap.Tab(tab));
+                },
+
                 openFileSelection (ev) {
                 openFileSelection (ev) {
                     ev.preventDefault();
                     ev.preventDefault();
                     this.el.querySelector('input[type="file"]').click();
                     this.el.querySelector('input[type="file"]').click();

+ 1 - 1
src/converse.js

@@ -23,7 +23,7 @@ if (typeof define !== 'undefined') {
         "converse-muc-views",
         "converse-muc-views",
         "converse-muc-views",       // Views related to MUC
         "converse-muc-views",       // Views related to MUC
         "converse-notification",    // HTML5 Notifications
         "converse-notification",    // HTML5 Notifications
-        "converse-oauth",
+        "converse-omemo",
         "converse-ping",            // XEP-0199 XMPP Ping
         "converse-ping",            // XEP-0199 XMPP Ping
         "converse-register",        // XEP-0077 In-band registration
         "converse-register",        // XEP-0077 In-band registration
         "converse-roomslist",       // Show currently open chat rooms
         "converse-roomslist",       // Show currently open chat rooms

+ 3 - 1
src/templates/message.html

@@ -1,4 +1,5 @@
-<div class="message chat-msg {{{o.type}}} {[ if (o.is_me_message) { ]} chat-msg--action {[ } ]} {{{o.extra_classes}}}" data-isodate="{{{o.time}}}" data-msgid="{{{o.msgid}}}" data-from="{{{o.from}}}">
+<div class="message chat-msg {{{o.type}}} {[ if (o.is_me_message) { ]} chat-msg--action {[ } ]} {{{o.extra_classes}}}"
+        data-isodate="{{{o.time}}}" data-msgid="{{{o.msgid}}}" data-from="{{{o.from}}}" data-encrypted="{{{o.is_encrypted}}}">
     {[ if (o.type !== 'headline' && !o.is_me_message) { ]}
     {[ if (o.type !== 'headline' && !o.is_me_message) { ]}
     <canvas class="avatar chat-msg__avatar" height="36" width="36"></canvas>
     <canvas class="avatar chat-msg__avatar" height="36" width="36"></canvas>
     {[ } ]}
     {[ } ]}
@@ -9,6 +10,7 @@
                 {[o.roles.forEach(function (role) { ]} <span class="badge badge-secondary">{{{role}}}</span> {[ }); ]}
                 {[o.roles.forEach(function (role) { ]} <span class="badge badge-secondary">{{{role}}}</span> {[ }); ]}
             </span>
             </span>
             {[ if (!o.is_me_message) { ]}<time timestamp="{{{o.isodate}}}" class="chat-msg__time">{{{o.pretty_time}}}</time>{[ } ]}
             {[ if (!o.is_me_message) { ]}<time timestamp="{{{o.isodate}}}" class="chat-msg__time">{{{o.pretty_time}}}</time>{[ } ]}
+            {[ if (o.is_encrypted) { ]}<span class="fa fa-lock"></span>{[ } ]}
         </span>
         </span>
         {[ if (!o.is_me_message) { ]}<div class="chat-msg__body">{[ } ]}
         {[ if (!o.is_me_message) { ]}<div class="chat-msg__body">{[ } ]}
             {[ if (o.edited) { ]} <i title="{{{o.__('This message has been edited')}}}" class="fa fa-edit chat-msg__edit-modal"></i> {[ } ]}
             {[ if (o.edited) { ]} <i title="{{{o.__('This message has been edited')}}}" class="fa fa-edit chat-msg__edit-modal"></i> {[ } ]}

+ 107 - 44
src/templates/profile_modal.html

@@ -5,54 +5,117 @@
                 <h5 class="modal-title" id="user-profile-modal-label">{{{o.heading_profile}}}</h5>
                 <h5 class="modal-title" id="user-profile-modal-label">{{{o.heading_profile}}}</h5>
                 <button type="button" class="close" data-dismiss="modal" aria-label="{{{o.label_close}}}"><span aria-hidden="true">&times;</span></button>
                 <button type="button" class="close" data-dismiss="modal" aria-label="{{{o.label_close}}}"><span aria-hidden="true">&times;</span></button>
             </div>
             </div>
-            <form class="converse-form">
-                <div class="modal-body">
-                    <div class="row">
-                        <div class="col-auto">
-                            <a class="change-avatar" href="#">
-                                {[ if (o.image) { ]}
-                                    <img alt="{{{o.alt_avatar}}}" class="img-thumbnail avatar align-self-center" height="100px" width="100px" src="data:{{{o.image_type}}};base64,{{{o.image}}}"/>
-                                {[ } ]}
-                                {[ if (!o.image) { ]}
-                                    <canvas class="avatar" height="100px" width="100px"/>
-                                {[ } ]}
-                            </a>
-                            <input class="hidden" name="image" type="file">
-                        </div>
-                        <div class="col">
+            <div class="modal-body">
+                {[ if (o._converse.pluggable.plugins['converse-omemo'].enabled(o._converse)) { ]}
+                <ul class="nav nav-pills justify-content-center">
+                    <li role="presentation" class="nav-item">
+                        <a class="nav-link active" id="profile-tab" href="#profile-tabpanel" aria-controls="profile-tabpanel" role="tab" data-toggle="tab">Profile</a>
+                    </li>
+                    <li role="presentation" class="nav-item">
+                        <a class="nav-link" id="omemo-tab" href="#omemo-tabpanel" aria-controls="omemo-tabpanel" role="tab" data-toggle="tab">OMEMO</a>
+                    </li>
+                </ul>
+                {[ } ]}
+                <div class="tab-content">
+                    <div class="tab-pane fade show active" id="profile-tabpanel" role="tabpanel" aria-labelledby="profile-tab">
+                        <form class="converse-form converse-form--modal profile-form" action="#">
+                            <div class="row">
+                                <div class="col-auto">
+                                    <a class="change-avatar" href="#">
+                                        {[ if (o.image) { ]}
+                                            <img alt="{{{o.alt_avatar}}}" class="img-thumbnail avatar align-self-center" height="100px" width="100px" src="data:{{{o.image_type}}};base64,{{{o.image}}}"/>
+                                        {[ } ]}
+                                        {[ if (!o.image) { ]}
+                                            <canvas class="avatar" height="100px" width="100px"/>
+                                        {[ } ]}
+                                    </a>
+                                    <input class="hidden" name="image" type="file">
+                                </div>
+                                <div class="col">
+                                    <div class="form-group">
+                                        <label class="col-form-label">{{{o.label_jid}}}:</label>
+                                        <div>{{{o.jid}}}</div>
+                                    </div>
+                                </div>
+                            </div>
                             <div class="form-group">
                             <div class="form-group">
-                                <label class="col-form-label">{{{o.label_jid}}}:</label>
-                                <div>{{{o.jid}}}</div>
+                                <label for="vcard-fullname" class="col-form-label">{{{o.label_fullname}}}:</label>
+                                <input id="vcard-fullname" type="text" class="form-control" name="fn" value="{{{o.fullname}}}">
                             </div>
                             </div>
-                        </div>
-                    </div>
-                    <div class="form-group">
-                        <label for="vcard-fullname" class="col-form-label">{{{o.label_fullname}}}:</label>
-                        <input id="vcard-fullname" type="text" class="form-control" name="fn" value="{{{o.fullname}}}">
-                    </div>
-                    <div class="form-group">
-                        <label for="vcard-nickname" class="col-form-label">{{{o.label_nickname}}}:</label>
-                        <input id="vcard-nickname" type="text" class="form-control" name="nickname" value="{{{o.nickname}}}">
-                    </div>
-                    <div class="form-group">
-                        <label for="vcard-url" class="col-form-label">{{{o.label_url}}}:</label>
-                        <input id="vcard-url" type="url" class="form-control" name="url" value="{{{o.url}}}">
-                    </div>
-                    <div class="form-group">
-                        <label for="vcard-email" class="col-form-label">{{{o.label_email}}}:</label>
-                        <input id="vcard-email" type="email" class="form-control" name="email" value="{{{o.email}}}">
-                    </div>
-                    <div class="form-group">
-                        <label for="vcard-role" class="col-form-label">{{{o.label_role}}}:</label>
-                        <input id="vcard-role" type="text" class="form-control" name="role" value="{{{o.role}}}" aria-describedby="vcard-role-help">
-                        <small id="vcard-role-help" class="form-text text-muted">{{{o.label_role_help}}}</small>
+                            <div class="form-group">
+                                <label for="vcard-nickname" class="col-form-label">{{{o.label_nickname}}}:</label>
+                                <input id="vcard-nickname" type="text" class="form-control" name="nickname" value="{{{o.nickname}}}">
+                            </div>
+                            <div class="form-group">
+                                <label for="vcard-url" class="col-form-label">{{{o.label_url}}}:</label>
+                                <input id="vcard-url" type="url" class="form-control" name="url" value="{{{o.url}}}">
+                            </div>
+                            <div class="form-group">
+                                <label for="vcard-email" class="col-form-label">{{{o.label_email}}}:</label>
+                                <input id="vcard-email" type="email" class="form-control" name="email" value="{{{o.email}}}">
+                            </div>
+                            <div class="form-group">
+                                <label for="vcard-role" class="col-form-label">{{{o.label_role}}}:</label>
+                                <input id="vcard-role" type="text" class="form-control" name="role" value="{{{o.role}}}" aria-describedby="vcard-role-help">
+                                <small id="vcard-role-help" class="form-text text-muted">{{{o.label_role_help}}}</small>
+                            </div>
+                            <hr/>
+                            <div class="form-group">
+                                <button type="submit" class="save-form btn btn-primary">{{{o.__('Save and close')}}}</button>
+                            </div>
+                        </form>
                     </div>
                     </div>
+                    {[ if (o._converse.pluggable.plugins['converse-omemo'].enabled(o._converse)) { ]}
+                        <div class="tab-pane fade" id="omemo-tabpanel" role="tabpanel" aria-labelledby="omemo-tab">
+                            <form class="converse-form fingerprint-removal">
+                                <ul class="list-group fingerprints">
+                                    <li class="list-group-item active">{{{o.__("This device's OMEMO fingerprint")}}}</li>
+                                    <li class="list-group-item">
+                                        {[ if (o.view.current_device.get('bundle') && o.view.current_device.get('bundle').fingerprint) { ]}
+                                        <span class="fingerprint">{{{o.utils.formatFingerprint(o.view.current_device.get('bundle').fingerprint)}}}</span>
+                                        {[ } else {]}
+                                            <span class="spinner fa fa-spinner centered"/>
+                                        {[ } ]}
+                                    </li>
+                                </ul>
+                                {[ if (o.view.other_devices.length) { ]}
+                                    <ul class="list-group fingerprints">
+                                        <li class="list-group-item nopadding active">
+                                            <label>
+                                            <input type="checkbox" class="select-all" title="{{{o.__('Select all')}}}"
+                                                   aria-label="{{{o.__('Checkbox to select fingerprints of all other OMEMO devices')}}}">
+                                            {{{o.__('Other OMEMO-enabled devices')}}}
+                                            </label>
+                                        </li>
+                                        {[ o._.forEach(o.view.other_devices, function (device) { ]}
+                                            {[ if (device.get('bundle') && device.get('bundle').fingerprint) { ]}
+                                            <li class="fingerprint-removal-item list-group-item nopadding">
+                                                <label>
+                                                <input type="checkbox" value="{{{device.get('id')}}}"
+                                                       aria-label="{{{o.__('Checkbox for selecting the following fingerprint')}}}">
+                                                <span class="fingerprint">{{{o.utils.formatFingerprint(device.get('bundle').fingerprint)}}}</span>
+                                                </label>
+                                            </li>
+                                            {[ } else {]}
+                                            <li class="fingerprint-removal-item list-group-item nopadding">
+                                                <label>
+                                                <input type="checkbox" value="{{{device.get('id')}}}"
+                                                       aria-label="{{{o.__('Checkbox for selecting the following fingerprint')}}}">
+                                                <span>{{{o.__('Device without a fingerprint')}}}</span>
+                                                </label>
+                                            </li>
+                                            {[ } ]}
+                                        {[ }); ]}
+                                    </ul>
+                                    <div class="form-group">
+                                        <button type="submit" class="save-form btn btn-primary">{{{o.__('Remove checked devices and close')}}}</button>
+                                    </div>
+                                {[ } ]}
+                            </form>
+                        </div>
+                    {[ } ]}
                 </div>
                 </div>
-                <div class="modal-footer">
-                    <button type="submit" class="save-form btn btn-primary">{{{o.label_save}}}</button>
-                    <button type="button" class="btn btn-secondary" data-dismiss="modal">{{{o.label_close}}}</button>
-                </div>
-            </form>
+            </div>
         </div>
         </div>
     </div>
     </div>
 </div>
 </div>

+ 1 - 0
src/templates/toolbar_omemo.html

@@ -0,0 +1 @@
+<li class="toggle-omemo fa {[ if (o.omemo_active) { ]} fa-lock {[ } else { ]} fa-unlock {[ } ]}" title="{{{o.__('Messages are being sent in plaintext')}}}"></li>

+ 44 - 13
src/templates/user_details_modal.html

@@ -1,39 +1,70 @@
-<div class="modal fade" id="user-profile-modal" tabindex="-1" role="dialog" aria-labelledby="user-profile-modal-label" aria-hidden="true">
+<div class="modal fade" id="user-details-modal" tabindex="-1" role="dialog" aria-labelledby="user-details-modal-label" aria-hidden="true">
     <div class="modal-dialog" role="document">
     <div class="modal-dialog" role="document">
         <div class="modal-content">
         <div class="modal-content">
             <div class="modal-header">
             <div class="modal-header">
-                <h5 class="modal-title" id="user-profile-modal-label">{{{o.display_name}}}</h5>
-                <button type="button" class="close" data-dismiss="modal" aria-label="{{{o.label_close}}}"><span aria-hidden="true">&times;</span></button>
+                <h5 class="modal-title" id="user-details-modal-label">{{{o.display_name}}}</h5>
+                <button type="button" class="close" data-dismiss="modal" aria-label="{{{o.__('Close')}}}"><span aria-hidden="true">&times;</span></button>
             </div>
             </div>
             <div class="modal-body">
             <div class="modal-body">
                 {[ if (o.image) { ]}
                 {[ if (o.image) { ]}
-                <img alt="{{{o.alt_profile_image}}}"
+                <img alt="{{{o.__('The User\'s Profile Image')}}}"
                     class="img-thumbnail avatar align-self-center mb-3"
                     class="img-thumbnail avatar align-self-center mb-3"
                     height="100" width="100" src="data:{{{o.image_type}}};base64,{{{o.image}}}"/>
                     height="100" width="100" src="data:{{{o.image_type}}};base64,{{{o.image}}}"/>
                 {[ } ]}
                 {[ } ]}
                 {[ if (o.fullname) { ]}
                 {[ if (o.fullname) { ]}
-                <p><label>{{{o.label_fullname}}}:</label>&nbsp;{{{o.fullname}}}</p>
+                <p><label>{{{o.__('Full Name')}}}:</label>&nbsp;{{{o.fullname}}}</p>
                 {[ } ]}
                 {[ } ]}
-                <p><label>{{{o.label_jid}}}:</label>&nbsp;{{{o.jid}}}</p>
+                <p><label>{{{o.__('XMPP Address')}}}:</label>&nbsp;{{{o.jid}}}</p>
                 {[ if (o.nickname) { ]}
                 {[ if (o.nickname) { ]}
-                <p><label>{{{o.label_nickname}}}:</label>&nbsp;{{{o.nickname}}}</p>
+                <p><label>{{{o.__('Nickname')}}}:</label>&nbsp;{{{o.nickname}}}</p>
                 {[ } ]}
                 {[ } ]}
                 {[ if (o.url) { ]}
                 {[ if (o.url) { ]}
-                <p><label>{{{o.label_url}}}:</label>&nbsp;<a target="_blank" rel="noopener" href="{{{o.url}}}">{{{o.url}}}</a></p>
+                <p><label>{{{o.__('URL')}}}:</label>&nbsp;<a target="_blank" rel="noopener" href="{{{o.url}}}">{{{o.url}}}</a></p>
                 {[ } ]}
                 {[ } ]}
                 {[ if (o.email) { ]}
                 {[ if (o.email) { ]}
-                <p><label>{{{o.label_email}}}:</label>&nbsp;<a href="mailto:{{{o.email}}}">{{{o.email}}}</a></p>
+                <p><label>{{{o.__('Email')}}}:</label>&nbsp;<a href="mailto:{{{o.email}}}">{{{o.email}}}</a></p>
                 {[ } ]}
                 {[ } ]}
                 {[ if (o.role) { ]}
                 {[ if (o.role) { ]}
-                <p><label>{{{o.label_role}}}:</label>&nbsp;{{{o.role}}}</p>
+                <p><label>{{{o.__('Role')}}}:</label>&nbsp;{{{o.role}}}</p>
+                {[ } ]}
+
+                {[ if (o._converse.pluggable.plugins['converse-omemo'].enabled(o._converse)) { ]}
+                    <hr>
+                    <ul class="list-group fingerprints">
+                        <li class="list-group-item active">{{{o.__('OMEMO Fingerprints')}}}</li>
+                        {[ if (!o.view.devicelist.devices) { ]}
+                            <li class="list-group-item"><span class="spinner fa fa-spinner centered"/></li>
+                        {[ } ]}
+                        {[ if (o.view.devicelist.devices) { ]}
+                            {[ o.view.devicelist.devices.each(function (device) { ]}
+                                {[ if (device.get('bundle') && device.get('bundle').fingerprint) { ]}
+                                <li class="list-group-item">
+                                    <form class="fingerprint-trust">
+                                    <div class="btn-group btn-group-toggle">
+                                        <label class="btn btn--small {[ if (device.get('trusted') !== -1) { ]} btn-primary active {[ } else { ]}  btn-secondary {[ } ]}">
+                                            <input type="radio" name="{{{device.get('id')}}}" value="1"
+                                                {[ if (device.get('trusted') !== -1) { ]} checked="checked" {[ } ]}>{{{o.__('Trusted')}}}
+                                        </label>
+                                        <label class="btn btn--small {[ if (device.get('trusted') === -1) { ]} btn-primary active {[ } else { ]} btn-secondary {[ } ]}">
+                                            <input type="radio" name="{{{device.get('id')}}}" value="-1"
+                                                {[ if (device.get('trusted') === -1) { ]} checked="checked" {[ } ]}>{{{o.__('Untrusted')}}}
+                                        </label>
+                                    </div>
+                                    <span class="fingerprint">{{{o.utils.formatFingerprint(device.get('bundle').fingerprint)}}}</span>
+                                    </form>
+                                </li>
+                                {[ } ]}
+                            {[ }); ]}
+                        {[ } ]}
+                    </ul>
                 {[ } ]}
                 {[ } ]}
             </div>
             </div>
             <div class="modal-footer">
             <div class="modal-footer">
                 {[ if (o.allow_contact_removal && o.is_roster_contact) { ]}
                 {[ if (o.allow_contact_removal && o.is_roster_contact) { ]}
-                    <button type="button" class="btn btn-danger remove-contact"><i class="fa fa-trash"> </i>{{{o.label_remove}}}</button>
+                    <button type="button" class="btn btn-danger remove-contact"><i class="fa fa-trash"> </i>{{{o.__('Remove as contact')}}}</button>
                 {[ } ]}
                 {[ } ]}
-                <button type="button" class="btn btn-info refresh-contact"><i class="fa fa-refresh"> </i>{{{o.label_refresh}}}</button>
-                <button type="button" class="btn btn-secondary" data-dismiss="modal">{{{o.label_close}}}</button>
+                <button type="button" class="btn btn-info refresh-contact"><i class="fa fa-refresh"> </i>{{{o.__('Refresh')}}}</button>
+                <button type="button" class="btn btn-secondary" data-dismiss="modal">{{{o.__('Close')}}}</button>
             </div>
             </div>
         </div>
         </div>
     </div>
     </div>

+ 49 - 8
src/utils/core.js

@@ -6,7 +6,7 @@
 // Copyright (c) 2012-2017, Jan-Carel Brand <jc@opkode.com>
 // Copyright (c) 2012-2017, Jan-Carel Brand <jc@opkode.com>
 // Licensed under the Mozilla Public License (MPLv2)
 // Licensed under the Mozilla Public License (MPLv2)
 //
 //
-/*global define, escape, window */
+/*global define, escape, window, Uint8Array */
 (function (root, factory) {
 (function (root, factory) {
     if (typeof define === 'function' && define.amd) {
     if (typeof define === 'function' && define.amd) {
         define([
         define([
@@ -900,14 +900,11 @@
         return text.replace(_converse.geouri_regex, replacement);
         return text.replace(_converse.geouri_regex, replacement);
     };
     };
 
 
-    u.getSelectValues = function(select) {
-        var result = [];
-        var options = select && select.options;
-        var opt;
-
+    u.getSelectValues = function (select) {
+        const result = [];
+        const options = select && select.options;
         for (var i=0, iLen=options.length; i<iLen; i++) {
         for (var i=0, iLen=options.length; i<iLen; i++) {
-            opt = options[i];
-
+            const opt = options[i];
             if (opt.selected) {
             if (opt.selected) {
                 result.push(opt.value || opt.text);
                 result.push(opt.value || opt.text);
             }
             }
@@ -915,6 +912,50 @@
         return result;
         return result;
     };
     };
 
 
+    u.formatFingerprint = function (fp) {
+        fp = fp.replace(/^05/, '');
+        const arr = [];
+        for (let i=1; i<8; i++) {
+            const idx = i*8+i-1;
+            fp = fp.slice(0, idx) + ' ' + fp.slice(idx);
+        }
+        return fp;
+    };
+
+    u.arrayBufferToHex = function (ab) {
+        // https://stackoverflow.com/questions/40031688/javascript-arraybuffer-to-hex#40031979
+        return Array.prototype.map.call(new Uint8Array(ab), x => ('00' + x.toString(16)).slice(-2)).join('');
+    };
+
+    u.arrayBufferToString = function (ab) {
+        const enc = new TextDecoder("utf-8");
+        return enc.decode(ab);
+    };
+
+    u.arrayBufferToBase64 = function (ab) {
+        return btoa((new Uint8Array(ab)).reduce((data, byte) => data + String.fromCharCode(byte), ''));
+    };
+
+    u.stringToArrayBuffer = function (string) {
+        const enc = new TextEncoder(); // always utf-8
+        return enc.encode(string);
+    };
+
+    u.base64ToArrayBuffer = function (b64) {
+        const binary_string =  window.atob(b64),
+              len = binary_string.length,
+              bytes = new Uint8Array(len);
+
+        for (let i = 0; i < len; i++) {
+            bytes[i] = binary_string.charCodeAt(i)
+        }
+        return bytes.buffer
+    };
+
+    u.getRandomInt = function (max) {
+        return Math.floor(Math.random() * Math.floor(max));
+    };
+
     u.putCurserAtEnd = function (textarea) {
     u.putCurserAtEnd = function (textarea) {
         if (textarea !== document.activeElement) {
         if (textarea !== document.activeElement) {
             textarea.focus();
             textarea.focus();

+ 1 - 1
src/utils/form.js

@@ -3,7 +3,7 @@
 //
 //
 // This is the utilities module.
 // This is the utilities module.
 //
 //
-// Copyright (c) 2012-2017, Jan-Carel Brand <jc@opkode.com>
+// Copyright (c) 2013-2018, Jan-Carel Brand <jc@opkode.com>
 // Licensed under the Mozilla Public License (MPLv2)
 // Licensed under the Mozilla Public License (MPLv2)
 //
 //
 /*global define, escape, Jed */
 /*global define, escape, Jed */

+ 61 - 1
tests/mock.js

@@ -6,8 +6,68 @@
     var Strophe = converse.env.Strophe;
     var Strophe = converse.env.Strophe;
     var moment = converse.env.moment;
     var moment = converse.env.moment;
     var $iq = converse.env.$iq;
     var $iq = converse.env.$iq;
-    var mock = {};
+    var u = converse.env.utils;
+
+    window.libsignal = {
+        'SignalProtocolAddress': function (name, device_id) {
+            this.name = name;
+            this.deviceId = device_id;
+        },
+        'SessionCipher': function (storage, remote_address) {
+            this.remoteAddress = remote_address;
+            this.storage = storage;
+            this.encrypt = () => Promise.resolve({
+                'type': 1,
+                'body': 'c1ph3R73X7',
+                'registrationId': '1337' 
+            });
+            this.decryptPreKeyWhisperMessage = (key_and_tag) => {
+                // TODO: remove the prekey
+                return Promise.resolve(u.stringToArrayBuffer(key_and_tag));
+            };
+
+            this.decryptWhisperMessage = (key_and_tag) => {
+                return Promise.resolve(u.stringToArrayBuffer(key_and_tag));
+            }
+        },
+        'SessionBuilder': function (storage, remote_address) {
+            this.processPreKey = function () {
+                return Promise.resolve();
+            }
+        },
+        'KeyHelper': {
+            'generateIdentityKeyPair': function () {
+                return Promise.resolve({
+                    'pubKey': new TextEncoder('utf-8').encode('1234'),
+                    'privKey': new TextEncoder('utf-8').encode('4321')
+                });
+            },
+            'generateRegistrationId': function () {
+                return '123456789';
+            },
+            'generatePreKey': function (keyid) {
+                return Promise.resolve({
+                    'keyId': keyid,
+                    'keyPair': {
+                        'pubKey': new TextEncoder('utf-8').encode('1234'),
+                        'privKey': new TextEncoder('utf-8').encode('4321')
+                    }
+                });
+            },
+            'generateSignedPreKey': function (identity_keypair, keyid) {
+                return Promise.resolve({
+                    'signature': new TextEncoder('utf-8').encode('11112222333344445555'),
+                    'keyId': keyid,
+                    'keyPair': {
+                        'pubKey': new TextEncoder('utf-8').encode('1234'),
+                        'privKey': new TextEncoder('utf-8').encode('4321')
+                    }
+                });
+            }
+        }
+    };
 
 
+    var mock = {};
     mock.view_mode = 'overlayed';
     mock.view_mode = 'overlayed';
 
 
     // Names from http://www.fakenamegenerator.com/
     // Names from http://www.fakenamegenerator.com/

+ 0 - 102
tests/runner-transpiled.js

@@ -1,102 +0,0 @@
-/*global config */
-
-// Extra test dependencies
-config.baseUrl = '../';
-config.paths.jquery = "node_modules/jquery/dist/jquery";
-config.paths.mock = "tests/mock";
-config.paths['wait-until-promise'] = "node_modules/wait-until-promise/index";
-config.paths['test-utils'] = "tests/utils";
-config.paths.sinon = "node_modules/sinon/pkg/sinon";
-config.paths.transcripts = "converse-logs/converse-logs";
-config.paths["jasmine-core"] = "node_modules/jasmine-core/lib/jasmine-core/jasmine";
-config.paths.jasmine = "node_modules/jasmine-core/lib/jasmine-core/boot";
-config.paths["jasmine-console"] = "node_modules/jasmine-core/lib/console/console";
-config.paths["console-reporter"] = "tests/console-reporter";
-config.paths["jasmine-html"] = "node_modules/jasmine-core/lib/jasmine-core/jasmine-html";
-
-config.paths.converse =                 "builds/converse";
-config.paths.utils =                    "builds/utils";
-config.paths["converse-bookmarks"] =    "builds/converse-bookmarks";
-config.paths["converse-chatboxes"] =    "builds/converse-chatboxes";
-config.paths["converse-chatview"] =     "builds/converse-chatview";
-config.paths["converse-controlbox"] =   "builds/converse-controlbox";
-config.paths["converse-core"] =         "builds/converse-core";
-config.paths["converse-disco"] =        "builds/converse-disco";
-config.paths["converse-dragresize"] =   "builds/converse-dragresize";
-config.paths["converse-headline"] =     "builds/converse-headline";
-config.paths["converse-http-file-upload"]="builds/converse-http-file-upload";
-config.paths["converse-fullscreen"] =   "builds/converse-fullscreen";
-config.paths["converse-mam"] =          "builds/converse-mam";
-config.paths["converse-minimize"] =     "builds/converse-minimize";
-config.paths["converse-muc"] =          "builds/converse-muc";
-config.paths["converse-muc-embedded"] = "builds/converse-muc-embedded";
-config.paths["converse-notification"] = "builds/converse-notification";
-config.paths["converse-otr"] =          "builds/converse-otr";
-config.paths["converse-ping"] =         "builds/converse-ping";
-config.paths["converse-profile"] =      "builds/converse-profile";
-config.paths["converse-register"] =     "builds/converse-register";
-config.paths["converse-roomslist"] =    "builds/converse-roomslist";
-config.paths["converse-rosterview"] =   "builds/converse-rosterview";
-config.paths["converse-singleton"] =    "builds/converse-singleton";
-config.paths["converse-vcard"] =        "builds/converse-vcard";
-
-
-config.shim.jasmine = {
-    exports: 'window.jasmineRequire'
-};
-config.shim['jasmine-html'] = {
-    deps: ['jasmine-core'],
-    exports: 'window.jasmineRequire'
-};
-config.shim['jasmine-console'] = {
-    deps: ['jasmine-core'],
-    exports: 'window.jasmineRequire'
-};
-config.shim.jasmine = {
-    deps: ['jasmine-core', 'jasmine-html', 'jasmine-console'],
-    exports: 'window.jasmine'
-};
-require.config(config);
-
-var specs = [
-    "jasmine",
-    //"spec/transcripts",
-    "spec/profiling",
-    "spec/utils",
-    "spec/converse",
-    "spec/bookmarks",
-    "spec/roomslist",
-    "spec/headline",
-    "spec/disco",
-    "spec/protocol",
-    "spec/presence",
-    "spec/eventemitter",
-    "spec/ping",
-    "spec/xmppstatus",
-    "spec/mam",
-    "spec/otr",
-    "spec/controlbox",
-    "spec/roster",
-    "spec/chatbox",
-    "spec/chatroom",
-    "spec/minchats",
-    "spec/notification",
-    "spec/register"
-];
-
-require(['console-reporter', 'mock', 'sinon', 'wait-until-promise', 'pluggable'],
-        function(ConsoleReporter, mock, sinon, waitUntilPromise, pluggable) {
-    window.sinon = sinon;
-    waitUntilPromise.setPromiseImplementation(window.Promise);
-    window.waitUntilPromise = waitUntilPromise.default;
-
-    window.localStorage.clear();
-    window.sessionStorage.clear();
-
-    // Load the specs
-    require(specs, function (jasmine) {
-        var jasmineEnv = jasmine.getEnv();
-        jasmineEnv.addReporter(new ConsoleReporter());
-        window.onload();
-    });
-});

+ 2 - 1
tests/runner.js

@@ -179,6 +179,7 @@ require.config(config);
 var specs = [
 var specs = [
     "jasmine",
     "jasmine",
     //"spec/transcripts",
     //"spec/transcripts",
+    //"spec/otr",
     "spec/spoilers",
     "spec/spoilers",
     "spec/profiling",
     "spec/profiling",
     "spec/utils",
     "spec/utils",
@@ -194,7 +195,7 @@ var specs = [
     "spec/push",
     "spec/push",
     "spec/xmppstatus",
     "spec/xmppstatus",
     "spec/mam",
     "spec/mam",
-    // "spec/otr",
+    "spec/omemo",
     "spec/controlbox",
     "spec/controlbox",
     "spec/roster",
     "spec/roster",
     "spec/chatbox",
     "spec/chatbox",

+ 1 - 1
tests/utils.js

@@ -100,7 +100,7 @@
 
 
     utils.openChatBoxFor = function (_converse, jid) {
     utils.openChatBoxFor = function (_converse, jid) {
         _converse.roster.get(jid).trigger("open");
         _converse.roster.get(jid).trigger("open");
-        return utils.waitUntil(() => _converse.chatboxviews.get(jid));
+        return utils.waitUntil(() => _converse.chatboxviews.get(jid), 1000);
     };
     };
 
 
     utils.openChatRoomViaModal = function (_converse, jid, nick='') {
     utils.openChatRoomViaModal = function (_converse, jid, nick='') {

Some files were not shown because too many files changed in this diff