2013-10-05 20:24:36 +02:00
|
|
|
var WeeChatProtocol = function() {
|
2013-10-05 21:01:51 +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
|
|
|
|
};
|
|
|
|
};
|
|
|
|
WeeChatProtocol._uiatos = function(uia) {
|
|
|
|
var _str = [];
|
|
|
|
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 = {
|
|
|
|
getInfo: function() {
|
2013-10-05 20:24:36 +02:00
|
|
|
var info = {};
|
2013-10-05 21:01:51 +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
|
|
|
},
|
|
|
|
getHdata: function() {
|
|
|
|
var self = this;
|
2013-10-05 20:24:36 +02:00
|
|
|
var paths;
|
|
|
|
var count;
|
|
|
|
var objs = [];
|
2013-10-05 21:01:51 +02:00
|
|
|
var hpath = this.getString();
|
2013-10-05 20:24:36 +02:00
|
|
|
|
2013-10-05 21:01:51 +02:00
|
|
|
keys = this.getString().split(',');
|
2013-10-05 20:24:36 +02:00
|
|
|
paths = hpath.split('/');
|
2013-10-05 21:01:51 +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:01:51 +02:00
|
|
|
return self.getPointer();
|
2013-10-05 20:24:36 +02:00
|
|
|
});
|
|
|
|
keys.forEach(function(key) {
|
2013-10-05 21:01:51 +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
|
|
|
},
|
|
|
|
getPointer: function() {
|
|
|
|
var l = this.getChar();
|
|
|
|
var pointer = this.getSlice(l)
|
2013-10-05 20:24:36 +02:00
|
|
|
var parsed_data = new Uint8Array(pointer);
|
|
|
|
|
2013-10-05 21:01:51 +02:00
|
|
|
return WeeChatProtocol._uiatos(parsed_data);
|
|
|
|
},
|
|
|
|
getInt: function() {
|
|
|
|
var parsed_data = new Uint8Array(this.getSlice(4));
|
2013-10-05 20:24:36 +02:00
|
|
|
|
2013-10-05 21:01:51 +02:00
|
|
|
return ((parsed_data[0] & 0xff) << 24) |
|
|
|
|
((parsed_data[1] & 0xff) << 16) |
|
|
|
|
((parsed_data[2] & 0xff) << 8) |
|
|
|
|
(parsed_data[3] & 0xff);
|
|
|
|
},
|
|
|
|
getChar: function() {
|
|
|
|
var parsed_data = new Uint8Array(this.getSlice(1));
|
2013-10-05 20:24:36 +02:00
|
|
|
|
|
|
|
return parsed_data[0];
|
2013-10-05 21:01:51 +02:00
|
|
|
},
|
|
|
|
getString: function() {
|
|
|
|
var l = this.getInt();
|
2013-10-05 20:24:36 +02:00
|
|
|
|
|
|
|
if (l > 0) {
|
2013-10-05 21:01:51 +02:00
|
|
|
var s = this.getSlice(l);
|
2013-10-05 20:24:36 +02:00
|
|
|
var parsed_data = new Uint8Array(s);
|
|
|
|
|
2013-10-05 21:01:51 +02:00
|
|
|
return WeeChatProtocol._uiatos(parsed_data);
|
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
|
|
|
},
|
|
|
|
getSlice: function(length) {
|
|
|
|
var slice = this.data.slice(0,length);
|
2013-02-24 20:44:03 +01:00
|
|
|
|
2013-10-05 21:01:51 +02:00
|
|
|
this.data = this.data.slice(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
|
|
|
},
|
|
|
|
getType: function() {
|
|
|
|
var t = this.getSlice(3);
|
|
|
|
|
|
|
|
return WeeChatProtocol._uiatos(new Uint8Array(t));
|
|
|
|
},
|
|
|
|
runType: function(type) {
|
|
|
|
var cb = this.types[type];
|
|
|
|
var boundCb = cb.bind(this);
|
|
|
|
|
|
|
|
return boundCb();
|
|
|
|
},
|
|
|
|
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
|
|
|
},
|
|
|
|
getId: function() {
|
|
|
|
return this.getString();
|
|
|
|
},
|
|
|
|
getObject: function() {
|
|
|
|
var self = this;
|
|
|
|
var type = this.getType();
|
2013-02-24 20:44:03 +01:00
|
|
|
|
2013-10-05 20:24:36 +02:00
|
|
|
if (type) {
|
|
|
|
return object = {
|
|
|
|
type: type,
|
2013-10-05 21:01:51 +02:00
|
|
|
content: self.runType(type),
|
2013-02-24 20:44:03 +01:00
|
|
|
}
|
|
|
|
}
|
2013-10-05 21:01:51 +02:00
|
|
|
},
|
|
|
|
parse: function(data) {
|
|
|
|
var self = this;
|
|
|
|
this.setData(data);
|
2013-02-24 20:44:03 +01:00
|
|
|
|
2013-10-05 21:01:51 +02:00
|
|
|
var header = this.getHeader();
|
|
|
|
var id = this.getId();
|
2013-10-05 20:24:36 +02:00
|
|
|
var objects = [];
|
2013-10-05 21:01:51 +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:01:51 +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 21:01:51 +02:00
|
|
|
},
|
|
|
|
setData: function (data) {
|
|
|
|
this.data = data;
|
|
|
|
},
|
|
|
|
getArray: function() {
|
|
|
|
var self = this;
|
2013-10-05 20:24:36 +02:00
|
|
|
var type;
|
|
|
|
var count;
|
|
|
|
var values;
|
|
|
|
|
2013-10-05 21:01:51 +02:00
|
|
|
type = this.getType();
|
|
|
|
count = this.getInt();
|
2013-10-05 20:24:36 +02:00
|
|
|
values = [];
|
|
|
|
|
|
|
|
for (var i = 0; i < count; i++) {
|
2013-10-05 21:01:51 +02:00
|
|
|
values.push(self.runType(type));
|
2013-07-20 20:23:09 +02:00
|
|
|
};
|
2013-10-05 20:24:36 +02:00
|
|
|
|
|
|
|
return values;
|
|
|
|
}
|
|
|
|
};
|