Commit 89cdac3b0cb30a4c95d48e77514a9f7038bd82c9
1 parent
52d540a039
Exists in
master
Tambah keyword blacklsit
Showing 1 changed file with 5 additions and 1 deletions Inline Diff
lib/transport.js
1 | const MODULE_NAME = 'TRANSPORT'; | 1 | const MODULE_NAME = 'TRANSPORT'; |
2 | const SLEEP_BEFORE_TERMINATE_ON_ERROR_MS = 5 * 1000; | 2 | const SLEEP_BEFORE_TERMINATE_ON_ERROR_MS = 5 * 1000; |
3 | 3 | ||
4 | const bot = require('simple-xmpp'); | 4 | const bot = require('simple-xmpp'); |
5 | const config = require('komodo-sdk/config'); | 5 | const config = require('komodo-sdk/config'); |
6 | const logger = require('tektrans-logger'); | 6 | const logger = require('tektrans-logger'); |
7 | const messagingService = require('komodo-center-messaging-client-lib'); | 7 | const messagingService = require('komodo-center-messaging-client-lib'); |
8 | const uniqid = require('uniqid'); | 8 | const uniqid = require('uniqid'); |
9 | 9 | ||
10 | const customPing = require('./custom-ping'); | 10 | const customPing = require('./custom-ping'); |
11 | const killOnIdle = require('./kill-on-idle'); | 11 | const killOnIdle = require('./kill-on-idle'); |
12 | 12 | ||
13 | let isReady; | 13 | let isReady; |
14 | 14 | ||
15 | bot.on('online', (data) => { | 15 | bot.on('online', (data) => { |
16 | logger.info(`XMPP transport connected, JID: ${data.jid.user}`); | 16 | logger.info(`XMPP transport connected, JID: ${data.jid.user}`); |
17 | 17 | ||
18 | if (config.custom_ping) { | 18 | if (config.custom_ping) { |
19 | customPing.setBot(bot); | 19 | customPing.setBot(bot); |
20 | } | 20 | } |
21 | 21 | ||
22 | bot.getRoster(); | 22 | bot.getRoster(); |
23 | 23 | ||
24 | setTimeout( | 24 | setTimeout( |
25 | () => { | 25 | () => { |
26 | isReady = true; | 26 | isReady = true; |
27 | 27 | ||
28 | logger.verbose('Transport is ready'); | 28 | logger.verbose('Transport is ready'); |
29 | 29 | ||
30 | if (messagingService.onOnline) { | 30 | if (messagingService.onOnline) { |
31 | messagingService.onOnline({ | 31 | messagingService.onOnline({ |
32 | me: config.username, | 32 | me: config.username, |
33 | }); | 33 | }); |
34 | } | 34 | } |
35 | 35 | ||
36 | if (!killOnIdle.getDisabled()) { | 36 | if (!killOnIdle.getDisabled()) { |
37 | killOnIdle.init(); | 37 | killOnIdle.init(); |
38 | } | 38 | } |
39 | }, | 39 | }, |
40 | 40 | ||
41 | config.warming_up_ms || (30 * 1000), | 41 | config.warming_up_ms || (30 * 1000), |
42 | ); | 42 | ); |
43 | }); | 43 | }); |
44 | 44 | ||
45 | bot.on('chat', (partner, msg) => { | 45 | bot.on('chat', (partner, msg) => { |
46 | if (!msg || !msg.trim()) { | 46 | if (!msg || !msg.trim()) { |
47 | return; | 47 | return; |
48 | } | 48 | } |
49 | 49 | ||
50 | if (partner === config.username.replace(/\/.*$/, '')) { | 50 | if (partner === config.username.replace(/\/.*$/, '')) { |
51 | return; | 51 | return; |
52 | } | 52 | } |
53 | 53 | ||
54 | killOnIdle.touch(); | 54 | killOnIdle.touch(); |
55 | 55 | ||
56 | const xid = uniqid(); | 56 | const xid = uniqid(); |
57 | 57 | ||
58 | if (!isReady) { | 58 | if (!isReady) { |
59 | if (!customPing.isPingMessage(msg) && !customPing.isPongMessage(msg)) { | 59 | if (!customPing.isPingMessage(msg) && !customPing.isPongMessage(msg)) { |
60 | logger.warn('Warming up is not finished yet, ignoring message', { | 60 | logger.warn('Warming up is not finished yet, ignoring message', { |
61 | xid, me: config.username, partner, msg, | 61 | xid, me: config.username, partner, msg, |
62 | }); | 62 | }); |
63 | } | 63 | } |
64 | return; | 64 | return; |
65 | } | 65 | } |
66 | 66 | ||
67 | if (msg.toLowerCase().indexOf('perintah salah')) { | 67 | if (msg.toLowerCase().indexOf('perintah salah') === 0) { |
68 | return; | ||
69 | } | ||
70 | |||
71 | if (msg.toLowerCase().indexOf('pembelian ') === 0) { | ||
68 | return; | 72 | return; |
69 | } | 73 | } |
70 | 74 | ||
71 | if (customPing.isPongMessage(msg)) { | 75 | if (customPing.isPongMessage(msg)) { |
72 | if (config.custom_ping && config.custom_ping.verbose) { | 76 | if (config.custom_ping && config.custom_ping.verbose) { |
73 | logger.verbose(`${MODULE_NAME} 70CDD087: Got PONG message`, { | 77 | logger.verbose(`${MODULE_NAME} 70CDD087: Got PONG message`, { |
74 | xid, | 78 | xid, |
75 | partner, | 79 | partner, |
76 | msg, | 80 | msg, |
77 | }); | 81 | }); |
78 | } | 82 | } |
79 | return; | 83 | return; |
80 | } | 84 | } |
81 | 85 | ||
82 | if (customPing.isPingMessage(msg)) { | 86 | if (customPing.isPingMessage(msg)) { |
83 | if (customPing.isAllowedPartner(partner)) { | 87 | if (customPing.isAllowedPartner(partner)) { |
84 | if (config.custom_ping && config.custom_ping.verbose) { | 88 | if (config.custom_ping && config.custom_ping.verbose) { |
85 | logger.verbose(`${MODULE_NAME} ED8C8786: Processing PING message`, { | 89 | logger.verbose(`${MODULE_NAME} ED8C8786: Processing PING message`, { |
86 | xid, | 90 | xid, |
87 | partner, | 91 | partner, |
88 | msg, | 92 | msg, |
89 | }); | 93 | }); |
90 | } | 94 | } |
91 | 95 | ||
92 | customPing.sendPong(xid, partner, msg); | 96 | customPing.sendPong(xid, partner, msg); |
93 | } | 97 | } |
94 | 98 | ||
95 | return; | 99 | return; |
96 | } | 100 | } |
97 | 101 | ||
98 | logger.info('Incoming message via XMPP transport', { | 102 | logger.info('Incoming message via XMPP transport', { |
99 | xid, me: config.username, partner, msg, | 103 | xid, me: config.username, partner, msg, |
100 | }); | 104 | }); |
101 | 105 | ||
102 | if (messagingService && messagingService.onIncomingMessage) { | 106 | if (messagingService && messagingService.onIncomingMessage) { |
103 | messagingService.onIncomingMessage( | 107 | messagingService.onIncomingMessage( |
104 | { | 108 | { |
105 | me: config.username, | 109 | me: config.username, |
106 | partner, | 110 | partner, |
107 | msg: msg.trim(), | 111 | msg: msg.trim(), |
108 | xid, | 112 | xid, |
109 | }, | 113 | }, |
110 | ); | 114 | ); |
111 | } | 115 | } |
112 | }); | 116 | }); |
113 | 117 | ||
114 | bot.on('error', (err) => { | 118 | bot.on('error', (err) => { |
115 | const xid = uniqid(); | 119 | const xid = uniqid(); |
116 | 120 | ||
117 | logger.warn(`${MODULE_NAME} F2E53C12: Error detected.`, { | 121 | logger.warn(`${MODULE_NAME} F2E53C12: Error detected.`, { |
118 | xid, | 122 | xid, |
119 | eCode: err.code, | 123 | eCode: err.code, |
120 | eMessage: err.message, | 124 | eMessage: err.message, |
121 | }); | 125 | }); |
122 | 126 | ||
123 | if (!config.do_not_terminate_on_error) { | 127 | if (!config.do_not_terminate_on_error) { |
124 | logger.warn(`${MODULE_NAME} BA6C0C55: Terminating on error`, { | 128 | logger.warn(`${MODULE_NAME} BA6C0C55: Terminating on error`, { |
125 | xid, | 129 | xid, |
126 | millisecondSleepBeforeTerminate: SLEEP_BEFORE_TERMINATE_ON_ERROR_MS, | 130 | millisecondSleepBeforeTerminate: SLEEP_BEFORE_TERMINATE_ON_ERROR_MS, |
127 | }); | 131 | }); |
128 | 132 | ||
129 | setTimeout(() => { | 133 | setTimeout(() => { |
130 | process.exit(1); | 134 | process.exit(1); |
131 | }, SLEEP_BEFORE_TERMINATE_ON_ERROR_MS); | 135 | }, SLEEP_BEFORE_TERMINATE_ON_ERROR_MS); |
132 | } | 136 | } |
133 | }); | 137 | }); |
134 | 138 | ||
135 | function send(partner, msg) { | 139 | function send(partner, msg) { |
136 | logger.verbose('Sending message via XMPP transport', { | 140 | logger.verbose('Sending message via XMPP transport', { |
137 | transport: 'xmpp', me: config.username, partner, msg, | 141 | transport: 'xmpp', me: config.username, partner, msg, |
138 | }); | 142 | }); |
139 | bot.send(partner, msg); | 143 | bot.send(partner, msg); |
140 | } | 144 | } |
141 | 145 | ||
142 | bot.on('subscribe', (from) => { | 146 | bot.on('subscribe', (from) => { |
143 | logger.verbose(`Incoming subscribe request from ${from}`); | 147 | logger.verbose(`Incoming subscribe request from ${from}`); |
144 | bot.acceptSubscription(from); | 148 | bot.acceptSubscription(from); |
145 | bot.subscribe(from); | 149 | bot.subscribe(from); |
146 | }); | 150 | }); |
147 | 151 | ||
148 | function ping() { | 152 | function ping() { |
149 | if (isReady) bot.send(config.username, 'PING!'); | 153 | if (isReady) bot.send(config.username, 'PING!'); |
150 | } | 154 | } |
151 | 155 | ||
152 | function init() { | 156 | function init() { |
153 | messagingService.setTransport(exports); | 157 | messagingService.setTransport(exports); |
154 | 158 | ||
155 | bot.connect({ | 159 | bot.connect({ |
156 | jid: config.username, | 160 | jid: config.username, |
157 | password: config.password, | 161 | password: config.password, |
158 | host: config.xmpp_host, | 162 | host: config.xmpp_host, |
159 | }); | 163 | }); |
160 | 164 | ||
161 | setInterval( | 165 | setInterval( |
162 | ping, | 166 | ping, |
163 | config.ping_interval_ms || 60000, | 167 | config.ping_interval_ms || 60000, |
164 | ); | 168 | ); |
165 | } | 169 | } |
166 | 170 | ||
167 | init(); | 171 | init(); |
168 | 172 | ||
169 | exports.init = init; | 173 | exports.init = init; |
170 | exports.send = send; | 174 | exports.send = send; |
171 | 175 |