Commit b2a835600625377b4bdd59200c50bf64eaf77ab8
1 parent
39e412b9c1
Exists in
master
Register bot for custom-ping on connected
Showing 2 changed files with 6 additions and 0 deletions Inline Diff
lib/custom-ping.js
1 | const MODULE_NAME = 'CUSTOM-PING'; | 1 | const MODULE_NAME = 'CUSTOM-PING'; |
2 | 2 | ||
3 | const uniqid = require('uniqid'); | 3 | const uniqid = require('uniqid'); |
4 | const config = require('komodo-sdk/config'); | 4 | const config = require('komodo-sdk/config'); |
5 | const logger = require('tektrans-logger'); | 5 | const logger = require('tektrans-logger'); |
6 | 6 | ||
7 | let bot; | 7 | let bot; |
8 | 8 | ||
9 | const allowedFromPartnerList = ( | 9 | const allowedFromPartnerList = ( |
10 | (config.custom_ping && config.custom_ping.allowed_from_partners) || [] | 10 | (config.custom_ping && config.custom_ping.allowed_from_partners) || [] |
11 | ) | 11 | ) |
12 | .filter((item) => typeof item === 'string') | 12 | .filter((item) => typeof item === 'string') |
13 | .map((item) => (item || '').trim().toUpperCase()) | 13 | .map((item) => (item || '').trim().toUpperCase()) |
14 | .filter((item) => item); | 14 | .filter((item) => item); |
15 | 15 | ||
16 | const setBot = (botFromCaller) => { | 16 | const setBot = (botFromCaller) => { |
17 | logger.verbose(`${MODULE_NAME} A063F39F: Bot registered for custom ping`); | ||
17 | bot = botFromCaller; | 18 | bot = botFromCaller; |
18 | }; | 19 | }; |
19 | exports.setBot = setBot; | 20 | exports.setBot = setBot; |
20 | 21 | ||
21 | const isPingMessage = (msg) => { | 22 | const isPingMessage = (msg) => { |
22 | if (!msg) return false; | 23 | if (!msg) return false; |
23 | if (typeof msg !== 'string') return false; | 24 | if (typeof msg !== 'string') return false; |
24 | 25 | ||
25 | return ((msg || '').trim().toUpperCase().search(/^PING($| )/) === 0); | 26 | return ((msg || '').trim().toUpperCase().search(/^PING($| )/) === 0); |
26 | }; | 27 | }; |
27 | exports.isPingMessage = isPingMessage; | 28 | exports.isPingMessage = isPingMessage; |
28 | 29 | ||
29 | const isPongMessage = (msg) => { | 30 | const isPongMessage = (msg) => { |
30 | if (!msg) return false; | 31 | if (!msg) return false; |
31 | if (typeof msg !== 'string') return false; | 32 | if (typeof msg !== 'string') return false; |
32 | 33 | ||
33 | return ((msg || '').trim().toUpperCase().search(/^PONG($| )/) === 0); | 34 | return ((msg || '').trim().toUpperCase().search(/^PONG($| )/) === 0); |
34 | }; | 35 | }; |
35 | exports.isPongMessage = isPongMessage; | 36 | exports.isPongMessage = isPongMessage; |
36 | 37 | ||
37 | const isAllowedPartner = (partner) => { | 38 | const isAllowedPartner = (partner) => { |
38 | if (!config.custom_ping) return false; | 39 | if (!config.custom_ping) return false; |
39 | if (config.custom_ping.disable_pong) return false; | 40 | if (config.custom_ping.disable_pong) return false; |
40 | 41 | ||
41 | if (config.custom_ping.allowed_from_all) return true; | 42 | if (config.custom_ping.allowed_from_all) return true; |
42 | return allowedFromPartnerList.indexOf((partner || '').trim().toUpperCase()) >= 0; | 43 | return allowedFromPartnerList.indexOf((partner || '').trim().toUpperCase()) >= 0; |
43 | }; | 44 | }; |
44 | exports.isAllowedPartner = isAllowedPartner; | 45 | exports.isAllowedPartner = isAllowedPartner; |
45 | 46 | ||
46 | const sendPong = (xid, partner, pingMessage) => { | 47 | const sendPong = (xid, partner, pingMessage) => { |
47 | if (!bot) return; | 48 | if (!bot) return; |
48 | if (!partner || typeof partner !== 'string' || !partner.trim()) return; | 49 | if (!partner || typeof partner !== 'string' || !partner.trim()) return; |
49 | 50 | ||
50 | const tokens = pingMessage.trim().split(/ +/); | 51 | const tokens = pingMessage.trim().split(/ +/); |
51 | 52 | ||
52 | const pongMessage = [ | 53 | const pongMessage = [ |
53 | 'PONG', | 54 | 'PONG', |
54 | (tokens && tokens[1]) || null, | 55 | (tokens && tokens[1]) || null, |
55 | ].filter((item) => item) | 56 | ].filter((item) => item) |
56 | .join(' '); | 57 | .join(' '); |
57 | 58 | ||
58 | if (config.custom_ping && config.custom_ping.verbose) { | 59 | if (config.custom_ping && config.custom_ping.verbose) { |
59 | logger.verbose(`${MODULE_NAME} F0D18EF4: Responding PING message`, { | 60 | logger.verbose(`${MODULE_NAME} F0D18EF4: Responding PING message`, { |
60 | xid, partner, pongMessage, | 61 | xid, partner, pongMessage, |
61 | }); | 62 | }); |
62 | } | 63 | } |
63 | 64 | ||
64 | bot.send(partner.trim(), pongMessage); | 65 | bot.send(partner.trim(), pongMessage); |
65 | }; | 66 | }; |
66 | exports.sendPong = sendPong; | 67 | exports.sendPong = sendPong; |
67 | 68 | ||
68 | const pingSender = () => { | 69 | const pingSender = () => { |
69 | const xid = uniqid(); | 70 | const xid = uniqid(); |
70 | if ( | 71 | if ( |
71 | !bot | 72 | !bot |
72 | || !config.custom_ping | 73 | || !config.custom_ping |
73 | || !config.custom_ping.send_to | 74 | || !config.custom_ping.send_to |
74 | || !Array.isArray(config.custom_ping.send_to) | 75 | || !Array.isArray(config.custom_ping.send_to) |
75 | || !config.custom_ping.send_to.length | 76 | || !config.custom_ping.send_to.length |
76 | ) return; | 77 | ) return; |
77 | 78 | ||
78 | config.custom_ping.send_to.forEach((partner) => { | 79 | config.custom_ping.send_to.forEach((partner) => { |
79 | if (config.custom_ping.verbose) { | 80 | if (config.custom_ping.verbose) { |
80 | logger.verbose(`${MODULE_NAME} 21358F45: Sending custom ping`, { | 81 | logger.verbose(`${MODULE_NAME} 21358F45: Sending custom ping`, { |
81 | xid, | 82 | xid, |
82 | partner, | 83 | partner, |
83 | }); | 84 | }); |
84 | } | 85 | } |
85 | 86 | ||
86 | bot.send(partner, `PING ${xid}`); | 87 | bot.send(partner, `PING ${xid}`); |
87 | }); | 88 | }); |
88 | }; | 89 | }; |
89 | 90 | ||
90 | if (config.custom_ping && config.custom_ping.send_to) { | 91 | if (config.custom_ping && config.custom_ping.send_to) { |
91 | const intervalMs = config.custom_ping.interval_ms || (25 * 1000); | 92 | const intervalMs = config.custom_ping.interval_ms || (25 * 1000); |
92 | logger.verbose(`${MODULE_NAME} 324034EE: Registering ping sender`, { | 93 | logger.verbose(`${MODULE_NAME} 324034EE: Registering ping sender`, { |
93 | intervalMs, | 94 | intervalMs, |
94 | partners: config.custom_ping.send_to, | 95 | partners: config.custom_ping.send_to, |
95 | }); | 96 | }); |
96 | 97 | ||
97 | setInterval(() => { | 98 | setInterval(() => { |
98 | pingSender(); | 99 | pingSender(); |
99 | }, intervalMs); | 100 | }, intervalMs); |
100 | } | 101 | } |
101 | 102 |
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 | 11 | ||
12 | let isReady; | 12 | let isReady; |
13 | 13 | ||
14 | bot.on('online', (data) => { | 14 | bot.on('online', (data) => { |
15 | logger.info(`XMPP transport connected, JID: ${data.jid.user}`); | 15 | logger.info(`XMPP transport connected, JID: ${data.jid.user}`); |
16 | |||
17 | if (config.custom_ping) { | ||
18 | customPing.setBot(bot); | ||
19 | } | ||
20 | |||
16 | bot.getRoster(); | 21 | bot.getRoster(); |
17 | 22 | ||
18 | setTimeout( | 23 | setTimeout( |
19 | () => { | 24 | () => { |
20 | isReady = true; | 25 | isReady = true; |
21 | 26 | ||
22 | logger.verbose('Transport is ready'); | 27 | logger.verbose('Transport is ready'); |
23 | 28 | ||
24 | if (messagingService.onOnline) { | 29 | if (messagingService.onOnline) { |
25 | messagingService.onOnline({ | 30 | messagingService.onOnline({ |
26 | me: config.username, | 31 | me: config.username, |
27 | }); | 32 | }); |
28 | } | 33 | } |
29 | }, | 34 | }, |
30 | 35 | ||
31 | config.warming_up_ms || (30 * 1000), | 36 | config.warming_up_ms || (30 * 1000), |
32 | ); | 37 | ); |
33 | }); | 38 | }); |
34 | 39 | ||
35 | bot.on('chat', (partner, msg) => { | 40 | bot.on('chat', (partner, msg) => { |
36 | if (!msg || !msg.trim()) { | 41 | if (!msg || !msg.trim()) { |
37 | return; | 42 | return; |
38 | } | 43 | } |
39 | 44 | ||
40 | if (partner === config.username.replace(/\/.*$/, '')) { | 45 | if (partner === config.username.replace(/\/.*$/, '')) { |
41 | return; | 46 | return; |
42 | } | 47 | } |
43 | 48 | ||
44 | const xid = uniqid(); | 49 | const xid = uniqid(); |
45 | 50 | ||
46 | if (customPing.isPongMessage(msg)) { | 51 | if (customPing.isPongMessage(msg)) { |
47 | if (config.custom_ping && config.custom_ping.verbose) { | 52 | if (config.custom_ping && config.custom_ping.verbose) { |
48 | logger.verbose(`${MODULE_NAME} 70CDD087: Got PONG message`, { | 53 | logger.verbose(`${MODULE_NAME} 70CDD087: Got PONG message`, { |
49 | xid, | 54 | xid, |
50 | partner, | 55 | partner, |
51 | msg, | 56 | msg, |
52 | }); | 57 | }); |
53 | } | 58 | } |
54 | return; | 59 | return; |
55 | } | 60 | } |
56 | 61 | ||
57 | if (customPing.isPingMessage(msg) && customPing.isAllowedPartner(partner)) { | 62 | if (customPing.isPingMessage(msg) && customPing.isAllowedPartner(partner)) { |
58 | if (config.custom_ping && config.custom_ping.verbose) { | 63 | if (config.custom_ping && config.custom_ping.verbose) { |
59 | logger.verbose(`${MODULE_NAME} ED8C8786: Processing PING message`, { | 64 | logger.verbose(`${MODULE_NAME} ED8C8786: Processing PING message`, { |
60 | xid, | 65 | xid, |
61 | partner, | 66 | partner, |
62 | msg, | 67 | msg, |
63 | }); | 68 | }); |
64 | } | 69 | } |
65 | 70 | ||
66 | customPing.sendPong(xid, partner, msg); | 71 | customPing.sendPong(xid, partner, msg); |
67 | return; | 72 | return; |
68 | } | 73 | } |
69 | 74 | ||
70 | if (!isReady) { | 75 | if (!isReady) { |
71 | logger.warn('Warming up is not finished yet, ignoring message', { | 76 | logger.warn('Warming up is not finished yet, ignoring message', { |
72 | xid, me: config.username, partner, msg, | 77 | xid, me: config.username, partner, msg, |
73 | }); | 78 | }); |
74 | 79 | ||
75 | return; | 80 | return; |
76 | } | 81 | } |
77 | 82 | ||
78 | logger.info('Incoming message via XMPP transport', { | 83 | logger.info('Incoming message via XMPP transport', { |
79 | xid, me: config.username, partner, msg, | 84 | xid, me: config.username, partner, msg, |
80 | }); | 85 | }); |
81 | 86 | ||
82 | if (messagingService && messagingService.onIncomingMessage) { | 87 | if (messagingService && messagingService.onIncomingMessage) { |
83 | messagingService.onIncomingMessage( | 88 | messagingService.onIncomingMessage( |
84 | { | 89 | { |
85 | me: config.username, | 90 | me: config.username, |
86 | partner, | 91 | partner, |
87 | msg: msg.trim(), | 92 | msg: msg.trim(), |
88 | xid, | 93 | xid, |
89 | }, | 94 | }, |
90 | ); | 95 | ); |
91 | } | 96 | } |
92 | }); | 97 | }); |
93 | 98 | ||
94 | bot.on('error', (err) => { | 99 | bot.on('error', (err) => { |
95 | const xid = uniqid(); | 100 | const xid = uniqid(); |
96 | 101 | ||
97 | logger.warn(`${MODULE_NAME} F2E53C12: Error detected.`, { | 102 | logger.warn(`${MODULE_NAME} F2E53C12: Error detected.`, { |
98 | xid, | 103 | xid, |
99 | eCode: err.code, | 104 | eCode: err.code, |
100 | eMessage: err.message, | 105 | eMessage: err.message, |
101 | }); | 106 | }); |
102 | 107 | ||
103 | if (!config.do_not_terminate_on_error) { | 108 | if (!config.do_not_terminate_on_error) { |
104 | logger.warn(`${MODULE_NAME} BA6C0C55: Terminating on error`, { | 109 | logger.warn(`${MODULE_NAME} BA6C0C55: Terminating on error`, { |
105 | xid, | 110 | xid, |
106 | millisecondSleepBeforeTerminate: SLEEP_BEFORE_TERMINATE_ON_ERROR_MS, | 111 | millisecondSleepBeforeTerminate: SLEEP_BEFORE_TERMINATE_ON_ERROR_MS, |
107 | }); | 112 | }); |
108 | 113 | ||
109 | setTimeout(() => { | 114 | setTimeout(() => { |
110 | process.exit(1); | 115 | process.exit(1); |
111 | }, SLEEP_BEFORE_TERMINATE_ON_ERROR_MS); | 116 | }, SLEEP_BEFORE_TERMINATE_ON_ERROR_MS); |
112 | } | 117 | } |
113 | }); | 118 | }); |
114 | 119 | ||
115 | function send(partner, msg) { | 120 | function send(partner, msg) { |
116 | logger.verbose('Sending message via XMPP transport', { | 121 | logger.verbose('Sending message via XMPP transport', { |
117 | transport: 'xmpp', me: config.username, partner, msg, | 122 | transport: 'xmpp', me: config.username, partner, msg, |
118 | }); | 123 | }); |
119 | bot.send(partner, msg); | 124 | bot.send(partner, msg); |
120 | } | 125 | } |
121 | 126 | ||
122 | bot.on('subscribe', (from) => { | 127 | bot.on('subscribe', (from) => { |
123 | logger.verbose(`Incoming subscribe request from ${from}`); | 128 | logger.verbose(`Incoming subscribe request from ${from}`); |
124 | bot.acceptSubscription(from); | 129 | bot.acceptSubscription(from); |
125 | bot.subscribe(from); | 130 | bot.subscribe(from); |
126 | }); | 131 | }); |
127 | 132 | ||
128 | function ping() { | 133 | function ping() { |
129 | if (isReady) bot.send(config.username, 'PING!'); | 134 | if (isReady) bot.send(config.username, 'PING!'); |
130 | } | 135 | } |
131 | 136 | ||
132 | function init() { | 137 | function init() { |
133 | messagingService.setTransport(exports); | 138 | messagingService.setTransport(exports); |
134 | 139 | ||
135 | bot.connect({ | 140 | bot.connect({ |
136 | jid: config.username, | 141 | jid: config.username, |
137 | password: config.password, | 142 | password: config.password, |
138 | host: config.xmpp_host, | 143 | host: config.xmpp_host, |
139 | }); | 144 | }); |
140 | 145 | ||
141 | setInterval( | 146 | setInterval( |
142 | ping, | 147 | ping, |
143 | config.ping_interval_ms || 60000, | 148 | config.ping_interval_ms || 60000, |
144 | ); | 149 | ); |
145 | } | 150 | } |
146 | 151 | ||
147 | init(); | 152 | init(); |
148 | 153 | ||
149 | exports.init = init; | 154 | exports.init = init; |
150 | exports.send = send; | 155 | exports.send = send; |
151 | 156 |