weechat-protocol.js: clean code (indentation/trailing spaces)

with-route-provider
Philippe Proulx 12 years ago
parent a7f3412b5d
commit 9bdcb814ab
  1. 2
      js/websockets.js
  2. 290
      js/weechat-protocol.js

@ -324,7 +324,7 @@ weechat.factory('handlers', ['$rootScope', 'colors', 'pluginManager', function($
}]); }]);
weechat.factory('connection', ['$rootScope', '$log', 'handlers', 'colors', function($rootScope, $log, handlers, colors) { weechat.factory('connection', ['$rootScope', '$log', 'handlers', 'colors', function($rootScope, $log, handlers, colors) {
protocol = new Protocol(); protocol = new WeeChatProtocol();
var websocket = null; var websocket = null;

@ -1,180 +1,194 @@
var Protocol = function() { var WeeChatProtocol = function() {
var self = this; var self = this;
var getInfo = function() { var getInfo = function() {
var info = {}; var info = {};
info.key = getString(); info.key = getString();
info.value = getString(); info.value = getString();
return info;
return info;
};
var getHdata = function() {
var paths;
var count;
var objs = [];
var hpath = getString();
keys = getString().split(',');
paths = hpath.split('/');
count = 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 getPointer();
});
keys.forEach(function(key) {
tmp[key[0]] = runType(key[1]);
});
objs.push(tmp);
}; };
var getHdata = function() { return objs;
var paths; };
var count;
var objs = [];
var hpath = getString();
function getPointer() {
var l = getChar();
var pointer = getSlice(l)
var parsed_data = new Uint8Array(pointer);
return _uiatos(parsed_data);
};
keys = getString().split(','); var _uiatos = function(uia) {
paths = hpath.split('/'); var _str = [];
count = getInt(); for (var c = 0; c < uia.length; c++) {
_str[c] = String.fromCharCode(uia[c]);
}
return decodeURIComponent(escape(_str.join("")));
};
var getInt = function() {
var parsed_data = new Uint8Array(getSlice(4));
var i = ((parsed_data[0] & 0xff) << 24) | ((parsed_data[1] & 0xff) << 16) | ((parsed_data[2] & 0xff) << 8) | (parsed_data[3] & 0xff);
return i;
};
keys = keys.map(function(key) { var getChar = function() {
return key.split(':'); var parsed_data = new Uint8Array(getSlice(1));
});
var i;
for (i = 0; i < count; i++) {
var tmp = {};
tmp.pointers = paths.map(function(path) { return parsed_data[0];
return getPointer(); };
});
keys.forEach(function(key) { var getString = function() {
tmp[key[0]] = runType(key[1]); var l = getInt();
});
objs.push(tmp);
};
return objs;
};
function getPointer() { if (l > 0) {
var l = getChar(); var s = getSlice(l);
var parsed_data = new Uint8Array(s);
var pointer = getSlice(l)
var parsed_data = new Uint8Array(pointer);
return _uiatos(parsed_data); return _uiatos(parsed_data);
}
}; return "";
};
var _uiatos =function(uia) { var getSlice = function(length) {
var _str = []; var slice = self.data.slice(0,length);
for (var c = 0; c < uia.length; c++) {
_str[c] = String.fromCharCode(uia[c]);
}
return decodeURIComponent(escape(_str.join("")));
};
var getInt = function() { self.data = self.data.slice(length);
var parsed_data = new Uint8Array(getSlice(4));
var i = ((parsed_data[0] & 0xff) << 24) | ((parsed_data[1] & 0xff) << 16) | ((parsed_data[2] & 0xff) << 8) | (parsed_data[3] & 0xff);
return i;
}; return slice;
};
var getChar = function() { var getType = function() {
var parsed_data = new Uint8Array(getSlice(1)); var t = getSlice(3);
return parsed_data[0];
};
var getString = function() { return _uiatos(new Uint8Array(t));
var l = getInt(); };
if (l > 0) {
var s = getSlice(l);
var parsed_data = new Uint8Array(s);
return _uiatos(parsed_data);
}
return "";
};
var getSlice = function(length) { var runType = function(type) {
var slice = self.data.slice(0,length); if (type in types) {
self.data = self.data.slice(length); return types[type]();
return slice; }
}; };
var getType = function() { var getHeader = function() {
var t = getSlice(3); var len = getInt();
return _uiatos(new Uint8Array(t)); var comp = getChar();
};
var runType = function(type) { return {
if (type in types) { length: len,
return types[type](); compression: comp,
}
0;
}; };
};
var getHeader = function() { var getId = function() {
return { return getString();
length: getInt(), }
compression: getChar(),
}
};
var getId = function() { var getObject = function() {
return getString(); var type = getType();
}
var getObject = function() { if (type) {
var type = getType(); return object = {
if (type) { type: type,
return object = { content: runType(type),
type: type,
content: runType(type),
}
} }
} }
}
self.parse = function(data) { self.parse = function(data) {
self.setData(data); self.setData(data);
var header = getHeader();
var id = getId(); var header = getHeader();
var objects = []; var id = getId();
var object = getObject(); var objects = [];
while(object) { var object = getObject();
objects.push(object);
object = getObject(); while(object) {
} objects.push(object);
return { object = getObject();
header: header,
id: id,
objects: objects,
}
} }
self.setData = function (data) { return {
self.data = data; header: header,
id: id,
objects: objects,
}; };
}
self.setData = function (data) {
self.data = data;
};
function array() {
var type;
var count;
var values;
function array() { type = getType();
var type; count = getInt();
var count; values = [];
var values;
for (var i = 0; i < count; i++) {
type = getType(); values.push(runType(type));
count = getInt();
values = [];
var i;
for (i = 0; i < count; i++) {
values.push(runType(type));
};
return values;
}
var types = {
chr: getChar,
"int": getInt,
str: getString,
inf: getInfo,
hda: getHdata,
ptr: getPointer,
lon: getPointer,
tim: getPointer,
buf: getString,
arr: array
}; };
return values;
}
var types = {
chr: getChar,
"int": getInt,
str: getString,
inf: getInfo,
hda: getHdata,
ptr: getPointer,
lon: getPointer,
tim: getPointer,
buf: getString,
arr: array
};
//TODO: IMPLEMENT THIS STUFF //TODO: IMPLEMENT THIS STUFF
// chr: this.getChar, // chr: this.getChar,
// 'int': getInt, // 'int': getInt,
// hacks // hacks
// hacks // hacks
// htb: getHashtable, // htb: getHashtable,
// inf: Protocol.getInfo, // inf: Protocol.getInfo,
// inl: getInfolist, // inl: getInfolist,
// }, // },
} };

Loading…
Cancel
Save