Commit 9a9dc5bca5421b34b67dfedc0997510b48c42b7e
1 parent
ccd4f6b5be
Exists in
master
Ping to ourself
Showing 1 changed file with 5 additions and 3 deletions Inline Diff
lib/custom-ping.js
1 | const MODULE_NAME = 'CUSTOM-PING'; | 1 | const MODULE_NAME = 'CUSTOM-PING'; |
2 | 2 | ||
3 | const locks = require('locks'); | 3 | const locks = require('locks'); |
4 | const uniqid = require('uniqid'); | 4 | const uniqid = require('uniqid'); |
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 | 7 | ||
8 | let bot; | 8 | let bot; |
9 | const mutex = locks.createMutex(); | 9 | const mutex = locks.createMutex(); |
10 | 10 | ||
11 | const allowedFromPartnerList = ( | 11 | const allowedFromPartnerList = ( |
12 | (config.custom_ping && config.custom_ping.allowed_from_partners) || [] | 12 | (config.custom_ping && config.custom_ping.allowed_from_partners) || [] |
13 | ) | 13 | ) |
14 | .filter((item) => typeof item === 'string') | 14 | .filter((item) => typeof item === 'string') |
15 | .map((item) => (item || '').trim().toUpperCase()) | 15 | .map((item) => (item || '').trim().toUpperCase()) |
16 | .filter((item) => item); | 16 | .filter((item) => item); |
17 | 17 | ||
18 | const setBot = (botFromCaller) => { | 18 | const setBot = (botFromCaller) => { |
19 | logger.verbose(`${MODULE_NAME} A063F39F: Bot registered for custom ping`); | 19 | logger.verbose(`${MODULE_NAME} A063F39F: Bot registered for custom ping`); |
20 | bot = botFromCaller; | 20 | bot = botFromCaller; |
21 | }; | 21 | }; |
22 | exports.setBot = setBot; | 22 | exports.setBot = setBot; |
23 | 23 | ||
24 | const isPingMessage = (msg) => { | 24 | const isPingMessage = (msg) => { |
25 | if (!msg) return false; | 25 | if (!msg) return false; |
26 | if (typeof msg !== 'string') return false; | 26 | if (typeof msg !== 'string') return false; |
27 | 27 | ||
28 | return ((msg || '').trim().toUpperCase().search(/^PING($| )/) === 0); | 28 | return ((msg || '').trim().toUpperCase().search(/^PING($| )/) === 0); |
29 | }; | 29 | }; |
30 | exports.isPingMessage = isPingMessage; | 30 | exports.isPingMessage = isPingMessage; |
31 | 31 | ||
32 | const isPongMessage = (msg) => { | 32 | const isPongMessage = (msg) => { |
33 | if (!msg) return false; | 33 | if (!msg) return false; |
34 | if (typeof msg !== 'string') return false; | 34 | if (typeof msg !== 'string') return false; |
35 | 35 | ||
36 | return ((msg || '').trim().toUpperCase().search(/^PONG($| )/) === 0); | 36 | return ((msg || '').trim().toUpperCase().search(/^PONG($| )/) === 0); |
37 | }; | 37 | }; |
38 | exports.isPongMessage = isPongMessage; | 38 | exports.isPongMessage = isPongMessage; |
39 | 39 | ||
40 | const isAllowedPartner = (partner) => { | 40 | const isAllowedPartner = (partner) => { |
41 | if (!config.custom_ping) return false; | 41 | if (!config.custom_ping) return false; |
42 | if (config.custom_ping.disable_pong) return false; | 42 | if (config.custom_ping.disable_pong) return false; |
43 | 43 | ||
44 | if (config.custom_ping.allowed_from_all) return true; | 44 | if (config.custom_ping.allowed_from_all) return true; |
45 | return allowedFromPartnerList.indexOf((partner || '').trim().toUpperCase()) >= 0; | 45 | return allowedFromPartnerList.indexOf((partner || '').trim().toUpperCase()) >= 0; |
46 | }; | 46 | }; |
47 | exports.isAllowedPartner = isAllowedPartner; | 47 | exports.isAllowedPartner = isAllowedPartner; |
48 | 48 | ||
49 | const sendPong = (xid, partner, pingMessage) => { | 49 | const sendPong = (xid, partner, pingMessage) => { |
50 | if (!bot) return; | 50 | if (!bot) return; |
51 | if (!partner || typeof partner !== 'string' || !partner.trim()) return; | 51 | if (!partner || typeof partner !== 'string' || !partner.trim()) return; |
52 | 52 | ||
53 | const tokens = pingMessage.trim().split(/ +/); | 53 | const tokens = pingMessage.trim().split(/ +/); |
54 | 54 | ||
55 | const pongMessage = [ | 55 | const pongMessage = [ |
56 | 'PONG', | 56 | 'PONG', |
57 | (tokens && tokens[1]) || null, | 57 | (tokens && tokens[1]) || null, |
58 | ].filter((item) => item) | 58 | ].filter((item) => item) |
59 | .join(' '); | 59 | .join(' '); |
60 | 60 | ||
61 | if (config.custom_ping && config.custom_ping.verbose) { | 61 | if (config.custom_ping && config.custom_ping.verbose) { |
62 | logger.verbose(`${MODULE_NAME} F0D18EF4: Responding PING message`, { | 62 | logger.verbose(`${MODULE_NAME} F0D18EF4: Responding PING message`, { |
63 | xid, partner, pongMessage, | 63 | xid, partner, pongMessage, |
64 | }); | 64 | }); |
65 | } | 65 | } |
66 | 66 | ||
67 | bot.send(partner.trim(), pongMessage); | 67 | bot.send(partner.trim(), pongMessage); |
68 | }; | 68 | }; |
69 | exports.sendPong = sendPong; | 69 | exports.sendPong = sendPong; |
70 | 70 | ||
71 | const sleepMs = (ms) => new Promise((resolve) => { | 71 | const sleepMs = (ms) => new Promise((resolve) => { |
72 | setTimeout(() => { | 72 | setTimeout(() => { |
73 | resolve(true); | 73 | resolve(true); |
74 | }, ms); | 74 | }, ms); |
75 | }); | 75 | }); |
76 | 76 | ||
77 | const pingSender = async () => { | 77 | const pingSender = async () => { |
78 | const xid = uniqid(); | 78 | const xid = uniqid(); |
79 | 79 | ||
80 | if ( | 80 | if ( |
81 | !bot | 81 | !bot |
82 | || !config.custom_ping | 82 | || !config.custom_ping |
83 | || !config.custom_ping.send_to | 83 | || !config.custom_ping.send_to |
84 | || !Array.isArray(config.custom_ping.send_to) | 84 | || !Array.isArray(config.custom_ping.send_to) |
85 | || !config.custom_ping.send_to.length | 85 | || !config.custom_ping.send_to.length |
86 | ) return; | 86 | ) return; |
87 | 87 | ||
88 | if (mutex.tryLock()) { | 88 | if (mutex.tryLock()) { |
89 | const msg = `PING ${xid}`; | 89 | const msg = `PING ${xid}`; |
90 | 90 | ||
91 | bot.send(config.username, msg); | ||
92 | |||
91 | const partnerCount = config.custom_ping.send_to.length; | 93 | const partnerCount = config.custom_ping.send_to.length; |
92 | for (let i = 0; i < partnerCount; i += 1) { | 94 | for (let i = 0; i < partnerCount; i += 1) { |
95 | // eslint-disable-next-line no-await-in-loop | ||
96 | await sleepMs(1000); | ||
97 | |||
93 | const partner = config.custom_ping.send_to[i]; | 98 | const partner = config.custom_ping.send_to[i]; |
94 | 99 | ||
95 | if (config.custom_ping.verbose) { | 100 | if (config.custom_ping.verbose) { |
96 | logger.verbose(`${MODULE_NAME} 21358F45: Sending custom ping`, { | 101 | logger.verbose(`${MODULE_NAME} 21358F45: Sending custom ping`, { |
97 | xid, | 102 | xid, |
98 | partner, | 103 | partner, |
99 | msg, | 104 | msg, |
100 | }); | 105 | }); |
101 | } | 106 | } |
102 | 107 | ||
103 | bot.send(partner, msg); | 108 | bot.send(partner, msg); |
104 | |||
105 | // eslint-disable-next-line no-await-in-loop | ||
106 | await sleepMs(1000); | ||
107 | } | 109 | } |
108 | 110 | ||
109 | mutex.unlock(); | 111 | mutex.unlock(); |
110 | } | 112 | } |
111 | }; | 113 | }; |
112 | 114 | ||
113 | if (config.custom_ping && config.custom_ping.send_to) { | 115 | if (config.custom_ping && config.custom_ping.send_to) { |
114 | const intervalMs = config.custom_ping.interval_ms || (25 * 1000); | 116 | const intervalMs = config.custom_ping.interval_ms || (25 * 1000); |
115 | logger.verbose(`${MODULE_NAME} 324034EE: Registering ping sender`, { | 117 | logger.verbose(`${MODULE_NAME} 324034EE: Registering ping sender`, { |
116 | intervalMs, | 118 | intervalMs, |
117 | partners: config.custom_ping.send_to, | 119 | partners: config.custom_ping.send_to, |
118 | }); | 120 | }); |
119 | 121 | ||
120 | setInterval(() => { | 122 | setInterval(() => { |
121 | pingSender(); | 123 | pingSender(); |