transport.js
4.12 KB
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
const MODULE_NAME = 'TRANSPORT';
const SLEEP_BEFORE_TERMINATE_ON_ERROR_MS = 5 * 1000;
const bot = require('simple-xmpp');
const config = require('komodo-sdk/config');
const logger = require('tektrans-logger');
const messagingService = require('komodo-center-messaging-client-lib');
const uniqid = require('uniqid');
const customPing = require('./custom-ping');
const killOnIdle = require('./kill-on-idle');
const sender = require('./sender');
let isReady;
bot.on('online', (data) => {
logger.info(`XMPP transport connected, JID: ${data.jid.user}`);
sender.init(bot);
if (config.custom_ping) {
customPing.setBot(bot);
}
bot.getRoster();
setTimeout(
() => {
isReady = true;
logger.verbose('Transport is ready');
if (messagingService.onOnline) {
messagingService.onOnline({
me: config.username,
});
}
},
config.warming_up_ms || (30 * 1000),
);
});
bot.on('chat', (partner, msg) => {
if (!msg || !msg.trim()) {
return;
}
if ((partner || '').toLowerCase() === config.username.toLowerCase().replace(/\/.*$/, '')) {
return;
}
killOnIdle.touch();
const xid = uniqid();
if (!isReady) {
if (!customPing.isPingMessage(msg) && !customPing.isPongMessage(msg)) {
logger.warn('Warming up is not finished yet, ignoring message', {
xid, me: config.username, partner, msg,
});
}
return;
}
if (msg.toLowerCase().indexOf('error') === 0) {
return;
}
if (msg.toLowerCase().indexOf('perintah salah') === 0) {
return;
}
if (msg.toLowerCase().indexOf('pembelian ') === 0) {
return;
}
if (customPing.isPongMessage(msg)) {
if (config.custom_ping && config.custom_ping.verbose) {
logger.verbose(`${MODULE_NAME} 70CDD087: Got PONG message`, {
xid,
partner,
msg,
});
}
return;
}
if (customPing.isPingMessage(msg)) {
if (customPing.isAllowedPartner(partner)) {
if (config.custom_ping && config.custom_ping.verbose) {
logger.verbose(`${MODULE_NAME} ED8C8786: Processing PING message`, {
xid,
partner,
msg,
});
}
customPing.sendPong(xid, partner, msg);
}
return;
}
logger.info('Incoming message via XMPP transport', {
xid, me: config.username, partner, msg,
});
if (messagingService && messagingService.onIncomingMessage) {
messagingService.onIncomingMessage(
{
me: config.username,
partner,
msg: msg.trim(),
xid,
},
);
}
});
bot.on('error', (err) => {
const xid = uniqid();
logger.warn(`${MODULE_NAME} F2E53C12: Error detected.`, {
xid,
eCode: err.code,
eMessage: err.message || err,
});
if (!config.do_not_terminate_on_error) {
logger.warn(`${MODULE_NAME} BA6C0C55: Terminating on error`, {
xid,
millisecondSleepBeforeTerminate: SLEEP_BEFORE_TERMINATE_ON_ERROR_MS,
});
setTimeout(() => {
process.exit(1);
}, SLEEP_BEFORE_TERMINATE_ON_ERROR_MS);
}
});
function send(partner, msg, xid) {
sender.send(partner, msg, xid);
}
bot.on('subscribe', (from) => {
logger.verbose(`Incoming subscribe request from ${from}`);
bot.acceptSubscription(from);
bot.subscribe(from);
});
function ping() {
const xid = uniqid();
if (isReady) sender.send(config.username, `PING ${xid}`, xid, true);
}
function init() {
messagingService.setTransport(exports);
if (!killOnIdle.getDisabled()) {
killOnIdle.init();
}
bot.connect({
jid: config.username,
password: config.password,
host: config.xmpp_host,
});
setInterval(
ping,
config.ping_interval_ms || 25000,
);
}
init();
exports.init = init;
exports.send = send;