2017-07-29 04:39:18 +04:00
import http from 'http' ;
2017-05-29 01:46:00 +03:00
/ * *
2017-09-29 01:01:11 +04:00
* @ param { String } uuid - Bot ID
* @ param { Function } callback - Function handler
* @ description Connection to the service and retrieves the session identifier .
2017-05-29 01:46:00 +03:00
* /
2017-09-29 01:01:11 +04:00
function connect ( uuid , callback ) {
if ( ! isVerification ( uuid ) ) {
2017-10-09 23:47:44 +04:00
throw new Error ( 'The variable \'uuid\' is not valid.' ) ;
2017-09-29 01:01:11 +04:00
}
2017-07-29 04:39:18 +04:00
2017-10-09 23:47:44 +04:00
var pkg = createPackage ( uuid , null ) ;
forward ( pkg , 'init' , callback ) ;
2017-05-29 01:46:00 +03:00
}
/ * *
2017-10-09 23:47:44 +04:00
* @ param { String } uuid - Session ID
* @ param { String } text - The message you are sending
2017-09-29 01:01:11 +04:00
* @ param { Function } callback - Function handler
* @ description Sends a message to bot and returns a response .
2017-05-29 01:46:00 +03:00
* /
2017-10-09 23:47:44 +04:00
function send ( uuid , text , callback ) {
if ( ! isVerification ( uuid ) ) {
throw new Error ( 'The variable \'uuid\' is not valid.' ) ;
2017-09-29 01:01:11 +04:00
}
2017-07-29 04:39:18 +04:00
2017-09-29 01:01:11 +04:00
if ( ! isString ( text ) ) {
2017-10-09 23:47:44 +04:00
throw new TypeError ( '\'text\' is not a string' ) ;
2017-09-29 01:01:11 +04:00
}
2017-07-29 04:39:18 +04:00
2017-10-09 23:47:44 +04:00
var pkg = createPackage ( uuid , text ) ;
forward ( pkg , 'request' , callback ) ;
}
/ * *
* @ param { String } pkg - The package to send
* @ param { String } path - The final delivery address
* @ param { Function } callback - Function handler
* @ description Send the final packet to the server and return the response .
* /
function forward ( pkg , path , callback ) {
2017-09-29 01:01:11 +04:00
var query = {
2017-10-09 23:47:44 +04:00
path : ` /api/2.0/json/Chat. ${ path } ` ,
2017-09-29 01:01:11 +04:00
hostname : 'iii.ru' ,
method : 'POST' ,
2017-10-09 23:47:44 +04:00
port : 80
2017-09-29 01:01:11 +04:00
} ;
var request = http . request ( query , ( response ) => {
var json = null ;
2017-10-09 23:47:44 +04:00
2017-09-29 01:01:11 +04:00
response . on ( 'data' , ( raw ) => {
json = decryptJSON ( raw ) ;
} ) ;
2017-10-09 23:47:44 +04:00
2017-09-29 01:01:11 +04:00
response . on ( 'end' , ( ) => {
2017-10-09 23:47:44 +04:00
callback ( json ) ;
2017-06-10 20:42:29 +00:00
} ) ;
2017-05-29 01:46:00 +03:00
} ) ;
2017-10-09 23:47:44 +04:00
2017-09-29 01:01:11 +04:00
request . on ( 'error' , ( error ) => {
callback ( error ) ;
} ) ;
2017-10-09 23:47:44 +04:00
request . write ( pkg ) ;
2017-09-29 01:01:11 +04:00
request . end ( ) ;
2017-05-29 01:46:00 +03:00
}
/ * *
2017-09-29 01:01:11 +04:00
* @ param { String } data - Data for encryption
* @ returns { String } Encrypted data
* @ description Encrypts the received string .
2017-05-29 01:46:00 +03:00
* /
2017-07-29 04:39:18 +04:00
function encrypt ( data ) {
2017-09-29 01:43:36 +04:00
var base64 = Buffer . from ( data ) . toString ( 'base64' ) ;
var 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
}
/ * *
2017-09-29 01:01:11 +04:00
* @ param { String } data - Data for decryption
* @ returns { String } Decrypted data
* @ description Decrypts the received string .
2017-05-29 01:46:00 +03:00
* /
2017-07-29 04:39:18 +04:00
function decrypt ( data ) {
2017-09-29 01:43:36 +04:00
var string = Buffer . from ( data , 'base64' ) ;
2017-09-29 01:01:11 +04:00
var decrypted = mergerString ( string ) . toString ( ) ;
2017-09-29 01:43:36 +04:00
return Buffer . from ( decrypted , 'base64' ) ;
2017-05-29 01:46:00 +03:00
}
/ * *
2017-09-29 01:01:11 +04:00
* @ param { String } json - Encrypted object
* @ returns { Object } Decrypted object
* @ description Decrypts the received object .
2017-05-29 01:46:00 +03:00
* /
2017-07-29 04:39:18 +04:00
function decryptJSON ( json ) {
2017-09-29 01:01:11 +04:00
var string = json . toString ( 'ascii' ) ;
var data = decrypt ( string ) ;
2017-05-29 01:46:00 +03:00
return JSON . parse ( data ) ;
}
/ * *
2017-09-29 01:01:11 +04:00
* @ param { String } data - Source string
* @ returns { String } Combined string
* @ description Merges the source string .
2017-05-29 01:46:00 +03:00
* /
2017-05-30 16:03:39 +03:00
function mergerString ( data ) {
2017-09-29 01:43:36 +04:00
var salt = Buffer . from ( 'some very-very long string without any non-latin characters due to different string representations inside of variable programming languages' ) ;
2017-10-09 19:00:02 +04:00
for ( var i = 0 ; i < data . length ; i ++ ) {
data [ i ] ^= salt [ i % salt . length ] ;
}
return data ;
2017-05-29 01:46:00 +03:00
}
/ * *
2017-10-09 23:47:44 +04:00
* @ param { String } uuid - Session ID
2017-09-29 01:01:11 +04:00
* @ param { String } text - Source string
* @ returns { String } Packed request
* @ description Creates a package to send .
2017-05-29 01:46:00 +03:00
* /
2017-10-09 23:47:44 +04:00
function createPackage ( uuid , text ) {
var data = [ uuid , text ] ;
2017-09-29 01:01:11 +04:00
var json = JSON . stringify ( data ) ;
2017-05-30 16:03:39 +03:00
return encrypt ( json ) ;
2017-05-29 01:46:00 +03:00
}
/ * *
2017-09-29 01:01:11 +04:00
* @ param { String } value - Variable to check
* @ returns { Boolean } Result of checking
* @ description Checks the type of the variable .
2017-05-29 01:46:00 +03:00
* /
2017-09-29 01:01:11 +04:00
function isVerification ( value ) {
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 ( value ) ;
2017-05-29 01:46:00 +03:00
}
2017-07-29 04:39:18 +04:00
/ * *
2017-09-29 01:01:11 +04:00
* @ param { String } value - Variable to check
* @ returns { Boolean } Result of checking
* @ description Checks the type of the variable .
2017-07-29 04:39:18 +04:00
* /
2017-09-29 01:01:11 +04:00
function isString ( value ) {
return typeof value === 'string' ;
}
export {
connect ,
send
} ;