aboutsummaryrefslogtreecommitdiff
path: root/src/index.js
blob: 869d2de3161b0a3be2377226e0eda3d559d61c67 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import http from 'http';

/**
 * @param {String} uuid - Bot ID
 * @param {Function} callback - Function handler
 * @description Connection to the service and retrieves the session identifier.
 */
function connect(uuid, callback) {
    if (!isVerification(uuid)) {
        throw new Error('\'uuid\' invalid variable.');
    }

    var query = {
        path: `/api/2.0/json/Chat.init/${uuid}`,
        hostname: 'iii.ru',
        method: 'GET',
        port: 80
    };

    var request = http.request(query, (response) => {
        var json = null;
        response.on('data', (raw) => {
            json = decryptJSON(raw);
        });
        response.on('end', () => {
            callback(json);
        });
    });
    request.on('error', (error) => {
        callback(error);
    });
    request.end();
}

/**
 * @param {String} cuid - Session ID
 * @param {String} text - Send messages
 * @param {Function} callback - Function handler
 * @description Sends a message to bot and returns a response.
 */
function send(cuid, text, callback) {
    if (!isVerification(cuid)) {
        throw new Error('\'cuid\' invalid variable.');
    }

    if (!isString(text)) {
        throw new Error('\'text\' invalid variable.');
    }

    var query = {
        path: '/api/2.0/json/Chat.request',
        hostname: 'iii.ru',
        method: 'POST',
        port: 80,
    };

    var request = http.request(query, (response) => {
        var json = null;
        response.on('data', (raw) => {
            json = decryptJSON(raw);
        });
        response.on('end', () => {
            callback(json.result);
        });
    });
    request.on('error', (error) => {
        callback(error);
    });
    request.write(createPackage(cuid, text));
    request.end();
}

/**
 * @param {String} data - Data for encryption
 * @returns {String} Encrypted data
 * @description Encrypts the received string.
 */
function encrypt(data) {
    var base64 = Buffer.from(data).toString('base64');
    var string = Buffer.from(base64);
    return mergerString(string).toString('base64');
}

/**
 * @param {String} data - Data for decryption
 * @returns {String} Decrypted data
 * @description Decrypts the received string.
 */
function decrypt(data) {
    var string = Buffer.from(data, 'base64');
    var decrypted = mergerString(string).toString();
    return Buffer.from(decrypted, 'base64');
}

/**
 * @param {String} json - Encrypted object
 * @returns {Object} Decrypted object
 * @description Decrypts the received object.
 */
function decryptJSON(json) {
    var string = json.toString('ascii');
    var data = decrypt(string);
    return JSON.parse(data);
}

/**
 * @param {String} data - Source string
 * @returns {String} Combined string
 * @description Merges the source string.
 */
function mergerString(data) {
    var salt = Buffer.from('some very-very long string without any non-latin characters due to different string representations inside of variable programming languages');

    for (var i = 0; i < data.length; i++) {
        data[i] ^= salt[i % salt.length];
    }

    return data;
}

/**
 * @param {String} cuid - Session ID
 * @param {String} text - Source string
 * @returns {String} Packed request
 * @description Creates a package to send.
 */
function createPackage(cuid, text) {
    var data = [];
    data.push(cuid);
    data.push(text.toString());
    var json = JSON.stringify(data);
    return encrypt(json);
}

/**
 * @param {String} value - Variable to check
 * @returns {Boolean} Result of checking
 * @description Checks the type of the variable.
 */
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);
}

/**
 * @param {String} value - Variable to check
 * @returns {Boolean} Result of checking
 * @description Checks the type of the variable.
 */
function isString(value) {
    return typeof value === 'string';
}

export {
    isVerification,
    decryptJSON,
    connect,
    decrypt,
    encrypt,
    send
};