2017-07-29 04:39:18 +04:00
export { connect , send } ;
import http from 'http' ;
2017-05-29 01:46:00 +03:00
/ * *
* Connects to the server and returns the connection data .
2017-07-29 04:39:18 +04:00
* @ param { String } The bot ID .
* @ promise { Object } Answer from the server .
* @ rejects { Error } If there are errors in operation .
2017-05-29 01:46:00 +03:00
* /
2017-07-29 04:39:18 +04:00
function connect ( uuid ) {
return new Promise ( ( resolve , reject ) => {
if ( ! isVerification ( uuid ) ) reject ( new Error ( 'The UUID is not a valid value!' ) ) ;
const query = {
path : '/api/2.0/json/Chat.init/' + uuid ,
hostname : 'iii.ru' ,
method : 'GET' ,
port : 80 ,
} ;
const request = http . request ( query , ( response ) => {
let json = '' ;
response . on ( 'data' , ( raw ) => json = decryptJSON ( raw ) ) ;
response . on ( 'end' , ( ) => {
if ( json . error ) reject ( json . error ) ;
resolve ( json . result ) ;
} ) ;
2017-06-10 20:42:29 +00:00
} ) ;
2017-07-29 04:39:18 +04:00
request . on ( 'error' , ( error ) => reject ( error ) ) ;
request . end ( ) ;
2017-05-29 01:46:00 +03:00
} ) ;
}
/ * *
* Send a message to the server and return a response .
2017-07-29 04:39:18 +04:00
* @ param { String } cuid - Session identifier .
* @ param { String } text - Message text .
* @ promise { Object } Answer from the server .
* @ rejects { Error } If there are errors in operation .
2017-05-29 01:46:00 +03:00
* /
2017-07-29 04:39:18 +04:00
function send ( cuid , text ) {
return new Promise ( ( resolve , reject ) => {
if ( ! isVerification ( cuid ) ) reject ( new Error ( 'The CUID is not a valid value!' ) ) ;
if ( ! isString ( text ) ) reject ( new Error ( 'The parameter is not a string!' ) ) ;
const query = {
path : '/api/2.0/json/Chat.request' ,
hostname : 'iii.ru' ,
method : 'POST' ,
port : 80 ,
} ;
const request = http . request ( query , ( response ) => {
let json = '' ;
response . on ( 'data' , ( raw ) => json = decryptJSON ( raw ) ) ;
response . on ( 'end' , ( ) => {
if ( json . error ) reject ( json . error ) ;
resolve ( json . result ) ;
} ) ;
2017-06-10 20:42:29 +00:00
} ) ;
2017-07-29 04:39:18 +04:00
request . on ( 'error' , ( error ) => reject ( error ) ) ;
request . write ( createPackage ( cuid , text ) ) ;
request . end ( ) ;
2017-05-29 01:46:00 +03:00
} ) ;
}
/ * *
* Encrypts the incoming data .
2017-07-29 04:39:18 +04:00
* @ param { String } Decrypted data .
* @ returns { String } Encrypted string .
2017-05-29 01:46:00 +03:00
* /
2017-07-29 04:39:18 +04:00
function encrypt ( data ) {
const base64 = Buffer . from ( data ) . toString ( 'base64' ) ;
const string = Buffer . from ( base64 ) ;
2017-05-30 16:03:39 +03:00
return mergerString ( string ) . toString ( 'base64' ) ;
2017-05-29 01:46:00 +03:00
}
/ * *
* Decrypts the incoming data .
2017-07-29 04:39:18 +04:00
* @ param { String } Encrypted data .
* @ returns { String } Decrypted string .
2017-05-29 01:46:00 +03:00
* /
2017-07-29 04:39:18 +04:00
function decrypt ( data ) {
const string = Buffer . from ( data , 'base64' ) ;
const decrypted = mergerString ( string ) . toString ( ) ;
2017-05-29 01:46:00 +03:00
return Buffer . from ( decrypted , 'base64' ) ;
}
/ * *
* Decrypts an encrypted JSON object .
2017-07-29 04:39:18 +04:00
* @ param { String } Encrypted data .
* @ returns { Object } Decrypted JSON .
2017-05-29 01:46:00 +03:00
* /
2017-07-29 04:39:18 +04:00
function decryptJSON ( json ) {
const string = json . toString ( 'ascii' ) ;
const data = decrypt ( string ) ;
2017-05-29 01:46:00 +03:00
return JSON . parse ( data ) ;
}
/ * *
* Merge and convert a string .
2017-07-29 04:39:18 +04:00
* @ param { String } The string to convert .
* @ returns { String } The converted string .
2017-05-29 01:46:00 +03:00
* /
2017-05-30 16:03:39 +03:00
function mergerString ( data ) {
2017-05-29 01:46:00 +03:00
const salt = Buffer . from ( 'some very-very long string without any non-latin characters due to different string representations inside of variable programming languages' ) ;
2017-07-29 04:39:18 +04:00
return data . map ( ( item , index ) => {
return item ^ salt [ index % salt . length ] ;
} ) ;
2017-05-29 01:46:00 +03:00
}
/ * *
* Creates an encrypted package to send .
2017-07-29 04:39:18 +04:00
* @ param { String } cuid - Session identifier .
* @ param { String } text - Message text .
* @ returns { String } Encrypted string .
2017-05-29 01:46:00 +03:00
* /
2017-07-29 04:39:18 +04:00
function createPackage ( cuid , text ) {
let data = [ ] ;
data . push ( cuid ) ;
data . push ( text . toString ( ) ) ;
const json = JSON . stringify ( data ) ;
2017-05-30 16:03:39 +03:00
return encrypt ( json ) ;
2017-05-29 01:46:00 +03:00
}
/ * *
* Validation UUID format string .
2017-07-29 04:39:18 +04:00
* @ param { String } The string to check .
2017-05-29 01:46:00 +03:00
* @ returns { Boolean }
* /
2017-05-30 16:03:39 +03:00
function isVerification ( data ) {
2017-05-29 01:46:00 +03:00
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' ) ;
return regexp . test ( data ) ;
}
2017-07-29 04:39:18 +04:00
/ * *
* Determines if a reference is a String .
* @ param { String } The string to check .
* @ returns { Boolean }
* /
function isString ( data ) {
return typeof data === 'string' ;
}