0
mirror of https://github.com/valentineus/iii-client.git synced 2025-07-01 10:30:27 +03:00

Full code refactoring

This commit is contained in:
2017-09-29 01:01:11 +04:00
parent ed39bc6842
commit 190072e593

View File

@ -1,143 +1,158 @@
export { connect, send };
import http from 'http'; import http from 'http';
/** /**
* Connects to the server and returns the connection data. * @param {String} uuid - Bot ID
* @param {String} The bot ID. * @param {Function} callback - Function handler
* @promise {Object} Answer from the server. * @description Connection to the service and retrieves the session identifier.
* @rejects {Error} If there are errors in operation.
*/ */
function connect(uuid) { function connect(uuid, callback) {
return new Promise((resolve, reject) => { if (!isVerification(uuid)) {
if (!isVerification(uuid)) reject(new Error('The UUID is not a valid value!')); throw new Error('\'uuid\' invalid variable.');
}
const query = { var query = {
path: '/api/2.0/json/Chat.init/' + uuid, path: `/api/2.0/json/Chat.init/${uuid}`,
hostname: 'iii.ru', hostname: 'iii.ru',
method: 'GET', method: 'GET',
port: 80, port: 80
}; };
const request = http.request(query, (response) => { var request = http.request(query, (response) => {
let json = ''; var json = null;
response.on('data', (raw) => json = decryptJSON(raw)); response.on('data', (raw) => {
json = decryptJSON(raw);
});
response.on('end', () => { response.on('end', () => {
if (json.error) reject(json.error); callback(json);
resolve(json.result);
}); });
}); });
request.on('error', (error) => reject(error)); request.on('error', (error) => {
callback(error);
});
request.end(); request.end();
});
} }
/** /**
* Send a message to the server and return a response. * @param {String} cuid - Session ID
* @param {String} cuid - Session identifier. * @param {String} text - Send messages
* @param {String} text - Message text. * @param {Function} callback - Function handler
* @promise {Object} Answer from the server. * @description Sends a message to bot and returns a response.
* @rejects {Error} If there are errors in operation.
*/ */
function send(cuid, text) { function send(cuid, text, callback) {
return new Promise((resolve, reject) => { if (!isVerification(cuid)) {
if (!isVerification(cuid)) reject(new Error('The CUID is not a valid value!')); throw new Error('\'cuid\' invalid variable.');
if (!isString(text)) reject(new Error('The parameter is not a string!')); }
const query = { if (!isString(text)) {
throw new Error('\'text\' invalid variable.');
}
var query = {
path: '/api/2.0/json/Chat.request', path: '/api/2.0/json/Chat.request',
hostname: 'iii.ru', hostname: 'iii.ru',
method: 'POST', method: 'POST',
port: 80, port: 80,
}; };
const request = http.request(query, (response) => { var request = http.request(query, (response) => {
let json = ''; var json = null;
response.on('data', (raw) => json = decryptJSON(raw)); response.on('data', (raw) => {
json = decryptJSON(raw);
});
response.on('end', () => { response.on('end', () => {
if (json.error) reject(json.error); callback(json.result);
resolve(json.result);
}); });
}); });
request.on('error', (error) => reject(error)); request.on('error', (error) => {
callback(error);
});
request.write(createPackage(cuid, text)); request.write(createPackage(cuid, text));
request.end(); request.end();
});
} }
/** /**
* Encrypts the incoming data. * @param {String} data - Data for encryption
* @param {String} Decrypted data. * @returns {String} Encrypted data
* @returns {String} Encrypted string. * @description Encrypts the received string.
*/ */
function encrypt(data) { function encrypt(data) {
const base64 = Buffer.from(data).toString('base64'); var base64 = Buffer.from(data).toString('base64');
const string = Buffer.from(base64); var string = Buffer.from(base64);
return mergerString(string).toString('base64'); return mergerString(string).toString('base64');
} }
/** /**
* Decrypts the incoming data. * @param {String} data - Data for decryption
* @param {String} Encrypted data. * @returns {String} Decrypted data
* @returns {String} Decrypted string. * @description Decrypts the received string.
*/ */
function decrypt(data) { function decrypt(data) {
const string = Buffer.from(data, 'base64'); var string = Buffer.from(data, 'base64');
const decrypted = mergerString(string).toString(); var decrypted = mergerString(string).toString();
return Buffer.from(decrypted, 'base64'); return Buffer.from(decrypted, 'base64');
} }
/** /**
* Decrypts an encrypted JSON object. * @param {String} json - Encrypted object
* @param {String} Encrypted data. * @returns {Object} Decrypted object
* @returns {Object} Decrypted JSON. * @description Decrypts the received object.
*/ */
function decryptJSON(json) { function decryptJSON(json) {
const string = json.toString('ascii'); var string = json.toString('ascii');
const data = decrypt(string); var data = decrypt(string);
return JSON.parse(data); return JSON.parse(data);
} }
/** /**
* Merge and convert a string. * @param {String} data - Source string
* @param {String} The string to convert. * @returns {String} Combined string
* @returns {String} The converted string. * @description Merges the source string.
*/ */
function mergerString(data) { function mergerString(data) {
const salt = Buffer.from('some very-very long string without any non-latin characters due to different string representations inside of variable programming languages'); var salt = Buffer.from('some very-very long string without any non-latin characters due to different string representations inside of variable programming languages');
return data.map((item, index) => { return data.map((item, index) => {
return item ^ salt[index % salt.length]; return item ^ salt[index % salt.length];
}); });
} }
/** /**
* Creates an encrypted package to send. * @param {String} cuid - Session ID
* @param {String} cuid - Session identifier. * @param {String} text - Source string
* @param {String} text - Message text. * @returns {String} Packed request
* @returns {String} Encrypted string. * @description Creates a package to send.
*/ */
function createPackage(cuid, text) { function createPackage(cuid, text) {
let data = []; var data = [];
data.push(cuid); data.push(cuid);
data.push(text.toString()); data.push(text.toString());
const json = JSON.stringify(data); var json = JSON.stringify(data);
return encrypt(json); return encrypt(json);
} }
/** /**
* Validation UUID format string. * @param {String} value - Variable to check
* @param {String} The string to check. * @returns {Boolean} Result of checking
* @returns {Boolean} * @description Checks the type of the variable.
*/ */
function isVerification(data) { function isVerification(value) {
const regexp = new RegExp('^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$', 'i'); var regexp = new RegExp('^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$', 'i');
return regexp.test(data); return regexp.test(value);
} }
/** /**
* Determines if a reference is a String. * @param {String} value - Variable to check
* @param {String} The string to check. * @returns {Boolean} Result of checking
* @returns {Boolean} * @description Checks the type of the variable.
*/ */
function isString(data) { function isString(value) {
return typeof data === 'string'; return typeof value === 'string';
} }
export {
isVerification,
decryptJSON,
connect,
decrypt,
encrypt,
send
};