2013-10-05 20:24:36 +02:00
|
|
|
var WeeChatProtocol = function() {
|
2013-10-05 21:06:30 +02:00
|
|
|
this._types = {
|
|
|
|
'chr': this._getChar,
|
|
|
|
'int': this._getInt,
|
|
|
|
'str': this._getString,
|
|
|
|
'inf': this._getInfo,
|
|
|
|
'hda': this._getHdata,
|
|
|
|
'ptr': this._getPointer,
|
|
|
|
'lon': this._getPointer,
|
|
|
|
'tim': this._getPointer,
|
|
|
|
'buf': this._getString,
|
|
|
|
'arr': this._getArray
|
2013-10-05 21:01:51 +02:00
|
|
|
};
|
|
|
|
};
|
2013-10-05 21:03:36 +02:00
|
|
|
WeeChatProtocol._uia2s = function(uia) {
|
2013-10-05 21:01:51 +02:00
|
|
|
var _str = [];
|
2013-10-05 21:03:36 +02:00
|
|
|
|
2013-10-05 21:01:51 +02:00
|
|
|
for (var c = 0; c < uia.length; c++) {
|
|
|
|
_str[c] = String.fromCharCode(uia[c]);
|
|
|
|
}
|
2013-10-05 20:24:36 +02:00
|
|
|
|
2013-10-05 21:01:51 +02:00
|
|
|
return decodeURIComponent(escape(_str.join("")));
|
|
|
|
};
|
|
|
|
WeeChatProtocol.prototype = {
|
2013-10-05 21:07:49 +02:00
|
|
|
_getType: function() {
|
|
|
|
var t = this._getSlice(3);
|
|
|
|
|
|
|
|
return WeeChatProtocol._uia2s(new Uint8Array(t));
|
|
|
|
},
|
|
|
|
_runType: function(type) {
|
|
|
|
var cb = this._types[type];
|
|
|
|
var boundCb = cb.bind(this);
|
|
|
|
|
|
|
|
return boundCb();
|
|
|
|
},
|
2013-10-05 21:06:30 +02:00
|
|
|
_getInfo: function() {
|
2013-10-05 20:24:36 +02:00
|
|
|
var info = {};
|
2013-10-05 21:06:30 +02:00
|
|
|
info.key = this._getString();
|
|
|
|
info.value = this._getString();
|
2013-10-05 20:24:36 +02:00
|
|
|
|
|
|
|
return info;
|
2013-10-05 21:01:51 +02:00
|
|
|
},
|
2013-10-05 21:06:30 +02:00
|
|
|
_getHdata: function() {
|
2013-10-05 21:01:51 +02:00
|
|
|
var self = this;
|
2013-10-05 20:24:36 +02:00
|
|
|
var paths;
|
|
|
|
var count;
|
|
|
|
var objs = [];
|
2013-10-05 21:06:30 +02:00
|
|
|
var hpath = this._getString();
|
2013-10-05 20:24:36 +02:00
|
|
|
|
2013-10-05 21:06:30 +02:00
|
|
|
keys = this._getString().split(',');
|
2013-10-05 20:24:36 +02:00
|
|
|
paths = hpath.split('/');
|
2013-10-05 21:06:30 +02:00
|
|
|
count = this._getInt();
|
2013-10-05 20:24:36 +02:00
|
|
|
|
|
|
|
keys = keys.map(function(key) {
|
|
|
|
return key.split(':');
|
|
|
|
});
|
|
|
|
|
|
|
|
for (var i = 0; i < count; i++) {
|
|
|
|
var tmp = {};
|
|
|
|
|
|
|
|
tmp.pointers = paths.map(function(path) {
|
2013-10-05 21:06:30 +02:00
|
|
|
return self._getPointer();
|
2013-10-05 20:24:36 +02:00
|
|
|
});
|
|
|
|
keys.forEach(function(key) {
|
2013-10-05 21:06:30 +02:00
|
|
|
tmp[key[0]] = self._runType(key[1]);
|
2013-10-05 20:24:36 +02:00
|
|
|
});
|
|
|
|
objs.push(tmp);
|
2013-02-24 20:44:03 +01:00
|
|
|
};
|
|
|
|
|
2013-10-05 20:24:36 +02:00
|
|
|
return objs;
|
2013-10-05 21:01:51 +02:00
|
|
|
},
|
2013-10-05 21:06:30 +02:00
|
|
|
_getPointer: function() {
|
|
|
|
var l = this._getChar();
|
|
|
|
var pointer = this._getSlice(l)
|
2013-10-05 21:10:57 +02:00
|
|
|
var parsedData = new Uint8Array(pointer);
|
2013-10-05 20:24:36 +02:00
|
|
|
|
2013-10-05 21:10:57 +02:00
|
|
|
return WeeChatProtocol._uia2s(parsedData);
|
2013-10-05 21:01:51 +02:00
|
|
|
},
|
2013-10-05 21:06:30 +02:00
|
|
|
_getInt: function() {
|
2013-10-05 21:10:57 +02:00
|
|
|
var parsedData = new Uint8Array(this._getSlice(4));
|
2013-10-05 20:24:36 +02:00
|
|
|
|
2013-10-05 21:10:57 +02:00
|
|
|
return ((parsedData[0] & 0xff) << 24) |
|
|
|
|
((parsedData[1] & 0xff) << 16) |
|
|
|
|
((parsedData[2] & 0xff) << 8) |
|
|
|
|
(parsedData[3] & 0xff);
|
2013-10-05 21:01:51 +02:00
|
|
|
},
|
2013-10-05 21:06:30 +02:00
|
|
|
_getChar: function() {
|
2013-10-05 21:10:57 +02:00
|
|
|
var parsedData = new Uint8Array(this._getSlice(1));
|
2013-10-05 20:24:36 +02:00
|
|
|
|
2013-10-05 21:10:57 +02:00
|
|
|
return parsedData[0];
|
2013-10-05 21:01:51 +02:00
|
|
|
},
|
2013-10-05 21:06:30 +02:00
|
|
|
_getString: function() {
|
|
|
|
var l = this._getInt();
|
2013-10-05 20:24:36 +02:00
|
|
|
|
|
|
|
if (l > 0) {
|
2013-10-05 21:06:30 +02:00
|
|
|
var s = this._getSlice(l);
|
2013-10-05 21:10:57 +02:00
|
|
|
var parsedData = new Uint8Array(s);
|
2013-10-05 20:24:36 +02:00
|
|
|
|
2013-10-05 21:10:57 +02:00
|
|
|
return WeeChatProtocol._uia2s(parsedData);
|
2013-10-05 20:24:36 +02:00
|
|
|
}
|
2013-07-20 20:23:09 +02:00
|
|
|
|
2013-10-05 20:24:36 +02:00
|
|
|
return "";
|
2013-10-05 21:01:51 +02:00
|
|
|
},
|
2013-10-05 21:06:30 +02:00
|
|
|
_getSlice: function(length) {
|
2013-10-05 21:20:42 +02:00
|
|
|
var slice = this._data.slice(this._dataAt, this._dataAt + length);
|
2013-02-24 20:44:03 +01:00
|
|
|
|
2013-10-05 21:20:42 +02:00
|
|
|
this._dataAt += length;
|
2013-02-24 20:44:03 +01:00
|
|
|
|
2013-10-05 20:24:36 +02:00
|
|
|
return slice;
|
2013-10-05 21:01:51 +02:00
|
|
|
},
|
2013-10-05 21:06:30 +02:00
|
|
|
_getHeader: function() {
|
|
|
|
var len = this._getInt();
|
|
|
|
var comp = this._getChar();
|
2013-02-24 20:44:03 +01:00
|
|
|
|
2013-10-05 20:24:36 +02:00
|
|
|
return {
|
|
|
|
length: len,
|
|
|
|
compression: comp,
|
2013-02-24 20:44:03 +01:00
|
|
|
};
|
2013-10-05 21:01:51 +02:00
|
|
|
},
|
2013-10-05 21:06:30 +02:00
|
|
|
_getId: function() {
|
|
|
|
return this._getString();
|
2013-10-05 21:01:51 +02:00
|
|
|
},
|
2013-10-05 21:06:30 +02:00
|
|
|
_getObject: function() {
|
2013-10-05 21:01:51 +02:00
|
|
|
var self = this;
|
2013-10-05 21:06:30 +02:00
|
|
|
var type = this._getType();
|
2013-02-24 20:44:03 +01:00
|
|
|
|
2013-10-05 20:24:36 +02:00
|
|
|
if (type) {
|
2013-10-05 21:10:57 +02:00
|
|
|
return {
|
2013-10-05 20:24:36 +02:00
|
|
|
type: type,
|
2013-10-05 21:06:30 +02:00
|
|
|
content: self._runType(type),
|
2013-10-05 21:10:57 +02:00
|
|
|
};
|
2013-02-24 20:44:03 +01:00
|
|
|
}
|
2013-10-05 21:01:51 +02:00
|
|
|
},
|
2013-10-05 21:07:49 +02:00
|
|
|
_getArray: function() {
|
|
|
|
var self = this;
|
|
|
|
var type;
|
|
|
|
var count;
|
|
|
|
var values;
|
|
|
|
|
|
|
|
type = this._getType();
|
|
|
|
count = this._getInt();
|
|
|
|
values = [];
|
|
|
|
|
|
|
|
for (var i = 0; i < count; i++) {
|
|
|
|
values.push(self._runType(type));
|
|
|
|
};
|
|
|
|
|
|
|
|
return values;
|
|
|
|
},
|
|
|
|
_setData: function (data) {
|
2013-10-05 21:20:42 +02:00
|
|
|
this._data = data;
|
2013-10-05 21:07:49 +02:00
|
|
|
},
|
2013-10-05 21:01:51 +02:00
|
|
|
parse: function(data) {
|
|
|
|
var self = this;
|
2013-10-05 21:20:42 +02:00
|
|
|
|
2013-10-05 21:06:30 +02:00
|
|
|
this._setData(data);
|
2013-10-05 21:20:42 +02:00
|
|
|
this._dataAt = 0;
|
2013-02-24 20:44:03 +01:00
|
|
|
|
2013-10-05 21:06:30 +02:00
|
|
|
var header = this._getHeader();
|
|
|
|
var id = this._getId();
|
2013-10-05 20:24:36 +02:00
|
|
|
var objects = [];
|
2013-10-05 21:06:30 +02:00
|
|
|
var object = this._getObject();
|
2013-10-05 20:24:36 +02:00
|
|
|
|
2013-10-05 21:01:51 +02:00
|
|
|
while (object) {
|
2013-10-05 20:24:36 +02:00
|
|
|
objects.push(object);
|
2013-10-05 21:06:30 +02:00
|
|
|
object = self._getObject();
|
2013-02-24 20:44:03 +01:00
|
|
|
}
|
|
|
|
|
2013-10-05 20:24:36 +02:00
|
|
|
return {
|
|
|
|
header: header,
|
|
|
|
id: id,
|
|
|
|
objects: objects,
|
2013-02-24 20:44:03 +01:00
|
|
|
};
|
2013-10-05 20:24:36 +02:00
|
|
|
}
|
|
|
|
};
|