glowing-bear/js/weechat-protocol.js

176 lines
4.1 KiB
JavaScript
Raw Normal View History

var WeeChatProtocol = function() {
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:03:36 +02:00
WeeChatProtocol._uia2s = function(uia) {
2013-10-05 21:29:18 +02:00
var str = [];
2013-10-05 21:03:36 +02:00
for (var c = 0; c < uia.length; c++) {
2013-10-05 21:29:18 +02:00
str.push(String.fromCharCode(uia[c]));
}
2013-10-05 21:29:18 +02:00
return decodeURIComponent(escape(str.join('')));
};
WeeChatProtocol.prototype = {
_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();
},
_getInfo: function() {
var info = {};
info.key = this._getString();
info.value = this._getString();
return info;
},
_getHdata: function() {
var self = this;
var paths;
var count;
var objs = [];
var hpath = this._getString();
keys = this._getString().split(',');
paths = hpath.split('/');
count = this._getInt();
keys = keys.map(function(key) {
return key.split(':');
});
for (var i = 0; i < count; i++) {
var tmp = {};
tmp.pointers = paths.map(function(path) {
return self._getPointer();
});
keys.forEach(function(key) {
tmp[key[0]] = self._runType(key[1]);
});
objs.push(tmp);
};
return objs;
},
_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 21:10:57 +02:00
return WeeChatProtocol._uia2s(parsedData);
},
_getInt: function() {
2013-10-05 21:10:57 +02:00
var parsedData = new Uint8Array(this._getSlice(4));
2013-10-05 21:10:57 +02:00
return ((parsedData[0] & 0xff) << 24) |
((parsedData[1] & 0xff) << 16) |
((parsedData[2] & 0xff) << 8) |
(parsedData[3] & 0xff);
},
_getChar: function() {
2013-10-05 21:10:57 +02:00
var parsedData = new Uint8Array(this._getSlice(1));
2013-10-05 21:10:57 +02:00
return parsedData[0];
},
_getString: function() {
var l = this._getInt();
if (l > 0) {
var s = this._getSlice(l);
2013-10-05 21:10:57 +02:00
var parsedData = new Uint8Array(s);
2013-10-05 21:10:57 +02:00
return WeeChatProtocol._uia2s(parsedData);
}
return "";
},
_getHeader: function() {
var len = this._getInt();
var comp = this._getChar();
return {
length: len,
compression: comp,
};
},
_getId: function() {
return this._getString();
},
_getObject: function() {
var self = this;
var type = this._getType();
if (type) {
2013-10-05 21:10:57 +02:00
return {
type: type,
content: self._runType(type),
2013-10-05 21:10:57 +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;
},
2013-10-05 21:32:32 +02:00
_getSlice: function(length) {
var slice = this._data.slice(this._dataAt, this._dataAt + length);
this._dataAt += length;
return slice;
},
_setData: function (data) {
this._data = data;
},
parse: function(data) {
var self = this;
this._setData(data);
this._dataAt = 0;
var header = this._getHeader();
var id = this._getId();
var objects = [];
var object = this._getObject();
while (object) {
objects.push(object);
object = self._getObject();
}
return {
header: header,
id: id,
objects: objects,
};
}
};