weechat-protocol.js: factor common commands formatting code

This commit is contained in:
Philippe Proulx 2013-10-05 21:52:01 -04:00
parent 7780a66264
commit 8a07be5aef

View file

@ -66,30 +66,25 @@ WeeChatProtocol._mergeParams = function(defaults, override) {
} }
/** /**
* Joins parts of a full command and wraps it for transmission. * Formats a command.
* *
* @param parts Parts to join * @param id Command ID (null for no ID)
* @return Complete command string * @param name Command name
* @param parts Command parts
* @return Formatted command string
*/ */
WeeChatProtocol._formatWrapCmdParts = function(parts) { WeeChatProtocol._formatCmd = function(id, name, parts) {
return parts.join(' ') + '\n'; var cmdIdName;
};
/**
* Begins a command (gets the first part).
*
* @param id ID of command (or null for no ID)
* @param name Name of command
* @return First part of command
*/
WeeChatProtocol._formatBeginCmd = function(id, name) {
var cmd; var cmd;
cmd = (id !== null) ? '(' + id + ') ' : ''; cmdIdName = (id !== null) ? '(' + id + ') ' : '';
cmd += name; cmdIdName += name;
parts.unshift(cmdIdName);
cmd = parts.join(' ');
cmd += '\n';
return cmd; return cmd;
} };
/** /**
* Formats an init command. * Formats an init command.
@ -100,22 +95,21 @@ WeeChatProtocol._formatBeginCmd = function(id, name) {
* @return Formatted init command string * @return Formatted init command string
*/ */
WeeChatProtocol.formatInit = function(params) { WeeChatProtocol.formatInit = function(params) {
var keys = [];
var parts = [];
var defaultParams = { var defaultParams = {
password: null, password: null,
compression: 'off' compression: 'off'
}; };
var keys = [];
var parts = [];
params = WeeChatProtocol._mergeParams(defaultParams, params); params = WeeChatProtocol._mergeParams(defaultParams, params);
parts.push(WeeChatProtocol._formatBeginCmd(null, 'init'));
keys.push('compression=' + params.compression); keys.push('compression=' + params.compression);
if (params.password !== null) { if (params.password !== null) {
keys.push('password=' + params.password); keys.push('password=' + params.password);
} }
parts.push(keys.join(',')); parts.push(keys.join(','));
return WeeChatProtocol._formatWrapCmdParts(parts); return WeeChatProtocol._formatCmd(null, 'init', parts);
}; };
/** /**
@ -128,20 +122,19 @@ WeeChatProtocol.formatInit = function(params) {
* @return Formatted hdata command string * @return Formatted hdata command string
*/ */
WeeChatProtocol.formatHdata = function(params) { WeeChatProtocol.formatHdata = function(params) {
var parts = [];
var defaultParams = { var defaultParams = {
id: null, id: null,
keys: null keys: null
}; };
var parts = [];
params = WeeChatProtocol._mergeParams(defaultParams, params); params = WeeChatProtocol._mergeParams(defaultParams, params);
parts.push(WeeChatProtocol._formatBeginCmd(params.id, 'hdata'));
parts.push(params.path); parts.push(params.path);
if (params.keys !== null) { if (params.keys !== null) {
parts.push(params.keys.join(',')); parts.push(params.keys.join(','));
} }
return WeeChatProtocol._formatWrapCmdParts(parts); return WeeChatProtocol._formatCmd(params.id, 'hdata', parts);
}; };
/** /**
@ -153,16 +146,15 @@ WeeChatProtocol.formatHdata = function(params) {
* @return Formatted info command string * @return Formatted info command string
*/ */
WeeChatProtocol.formatInfo = function(params) { WeeChatProtocol.formatInfo = function(params) {
var parts = [];
var defaultParams = { var defaultParams = {
id: null id: null
}; };
var parts = [];
params = WeeChatProtocol._mergeParams(defaultParams, params); params = WeeChatProtocol._mergeParams(defaultParams, params);
parts.push(WeeChatProtocol._formatBeginCmd(params.id, 'info'));
parts.push(params.name); parts.push(params.name);
return WeeChatProtocol._formatWrapCmdParts(parts); return WeeChatProtocol._formatCmd(params.id, 'info', parts);
}; };
/** /**
@ -174,19 +166,18 @@ WeeChatProtocol.formatInfo = function(params) {
* @return Formatted nicklist command string * @return Formatted nicklist command string
*/ */
WeeChatProtocol.formatNicklist = function(params) { WeeChatProtocol.formatNicklist = function(params) {
var parts = [];
var defaultParams = { var defaultParams = {
id: null, id: null,
buffer: null buffer: null
}; };
var parts = [];
params = WeeChatProtocol._mergeParams(defaultParams, params); params = WeeChatProtocol._mergeParams(defaultParams, params);
parts.push(WeeChatProtocol._formatBeginCmd(params.id, 'nicklist'));
if (params.buffer !== null) { if (params.buffer !== null) {
parts.push(params.buffer); parts.push(params.buffer);
} }
return WeeChatProtocol._formatWrapCmdParts(parts); return WeeChatProtocol._formatCmd(params.id, 'nicklist', parts);
}; };
/** /**
@ -199,17 +190,16 @@ WeeChatProtocol.formatNicklist = function(params) {
* @return Formatted input command string * @return Formatted input command string
*/ */
WeeChatProtocol.formatInput = function(params) { WeeChatProtocol.formatInput = function(params) {
var parts = [];
var defaultParams = { var defaultParams = {
id: null id: null
}; };
var parts = [];
params = WeeChatProtocol._mergeParams(defaultParams, params); params = WeeChatProtocol._mergeParams(defaultParams, params);
parts.push(WeeChatProtocol._formatBeginCmd(params.id, 'input'));
parts.push(params.buffer); parts.push(params.buffer);
parts.push(params.data); parts.push(params.data);
return WeeChatProtocol._formatWrapCmdParts(parts); return WeeChatProtocol._formatCmd(params.id, 'input', parts);
}; };
/** /**
@ -219,15 +209,14 @@ WeeChatProtocol.formatInput = function(params) {
* @return Formatted sync/desync command string * @return Formatted sync/desync command string
*/ */
WeeChatProtocol._formatSyncDesync = function(cmdName, params) { WeeChatProtocol._formatSyncDesync = function(cmdName, params) {
var parts = [];
var defaultParams = { var defaultParams = {
id: null, id: null,
buffers: null, buffers: null,
options: null options: null
}; };
var parts = [];
params = WeeChatProtocol._mergeParams(defaultParams, params); params = WeeChatProtocol._mergeParams(defaultParams, params);
parts.push(WeeChatProtocol._formatBeginCmd(params.id, cmdName));
if (params.buffers !== null) { if (params.buffers !== null) {
parts.push(params.buffers.join(',')); parts.push(params.buffers.join(','));
if (params.options !== null) { if (params.options !== null) {
@ -235,7 +224,7 @@ WeeChatProtocol._formatSyncDesync = function(cmdName, params) {
} }
} }
return WeeChatProtocol._formatWrapCmdParts(parts); return WeeChatProtocol._formatCmd(params.id, cmdName, parts);
} }
/** /**
@ -272,15 +261,14 @@ WeeChatProtocol.formatDesync = function(params) {
* @return Formatted test command string * @return Formatted test command string
*/ */
WeeChatProtocol.formatTest = function(params) { WeeChatProtocol.formatTest = function(params) {
var parts = [];
var defaultParams = { var defaultParams = {
id: null id: null
}; };
var parts = [];
params = WeeChatProtocol._mergeParams(defaultParams, params); params = WeeChatProtocol._mergeParams(defaultParams, params);
parts.push(WeeChatProtocol._formatBeginCmd(params.id, 'test'));
return WeeChatProtocol._formatWrapCmdParts(parts); return WeeChatProtocol._formatCmd(params.id, 'test', parts);
}; };
/** /**
@ -289,7 +277,7 @@ WeeChatProtocol.formatTest = function(params) {
* @return Formatted quit command string * @return Formatted quit command string
*/ */
WeeChatProtocol.formatQuit = function() { WeeChatProtocol.formatQuit = function() {
return WeeChatProtocol._formatWrapCmdParts(['quit']); return WeeChatProtocol._formatCmd(null, 'quit', []);
}; };
/** /**
@ -301,19 +289,18 @@ WeeChatProtocol.formatQuit = function() {
* @return Formatted ping command string * @return Formatted ping command string
*/ */
WeeChatProtocol.formatPing = function(params) { WeeChatProtocol.formatPing = function(params) {
var parts = [];
var defaultParams = { var defaultParams = {
id: null, id: null,
args: null args: null
}; };
var parts = [];
params = WeeChatProtocol._mergeParams(defaultParams, params); params = WeeChatProtocol._mergeParams(defaultParams, params);
parts.push(WeeChatProtocol._formatBeginCmd(params.id, 'ping'));
if (params.args !== null) { if (params.args !== null) {
parts.push(params.args.join(' ')); parts.push(params.args.join(' '));
} }
return WeeChatProtocol._formatWrapCmdParts(parts); return WeeChatProtocol._formatCmd(params.id, 'ping', parts);
}; };
WeeChatProtocol.prototype = { WeeChatProtocol.prototype = {