Commit 493e4fa093bb7cebecd57d91518c9622df182c57

Authored by Adhidarma Hadiwinoto
1 parent 7303fe10e5
Exists in master

Fix log on pingSender

Showing 1 changed file with 3 additions and 1 deletions Inline Diff

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 logger.verbose(`${MODULE_NAME} A063F39F: Bot registered for custom ping`);
18 bot = botFromCaller; 18 bot = botFromCaller;
19 }; 19 };
20 exports.setBot = setBot; 20 exports.setBot = setBot;
21 21
22 const isPingMessage = (msg) => { 22 const isPingMessage = (msg) => {
23 if (!msg) return false; 23 if (!msg) return false;
24 if (typeof msg !== 'string') return false; 24 if (typeof msg !== 'string') return false;
25 25
26 return ((msg || '').trim().toUpperCase().search(/^PING($| )/) === 0); 26 return ((msg || '').trim().toUpperCase().search(/^PING($| )/) === 0);
27 }; 27 };
28 exports.isPingMessage = isPingMessage; 28 exports.isPingMessage = isPingMessage;
29 29
30 const isPongMessage = (msg) => { 30 const isPongMessage = (msg) => {
31 if (!msg) return false; 31 if (!msg) return false;
32 if (typeof msg !== 'string') return false; 32 if (typeof msg !== 'string') return false;
33 33
34 return ((msg || '').trim().toUpperCase().search(/^PONG($| )/) === 0); 34 return ((msg || '').trim().toUpperCase().search(/^PONG($| )/) === 0);
35 }; 35 };
36 exports.isPongMessage = isPongMessage; 36 exports.isPongMessage = isPongMessage;
37 37
38 const isAllowedPartner = (partner) => { 38 const isAllowedPartner = (partner) => {
39 if (!config.custom_ping) return false; 39 if (!config.custom_ping) return false;
40 if (config.custom_ping.disable_pong) return false; 40 if (config.custom_ping.disable_pong) return false;
41 41
42 if (config.custom_ping.allowed_from_all) return true; 42 if (config.custom_ping.allowed_from_all) return true;
43 return allowedFromPartnerList.indexOf((partner || '').trim().toUpperCase()) >= 0; 43 return allowedFromPartnerList.indexOf((partner || '').trim().toUpperCase()) >= 0;
44 }; 44 };
45 exports.isAllowedPartner = isAllowedPartner; 45 exports.isAllowedPartner = isAllowedPartner;
46 46
47 const sendPong = (xid, partner, pingMessage) => { 47 const sendPong = (xid, partner, pingMessage) => {
48 if (!bot) return; 48 if (!bot) return;
49 if (!partner || typeof partner !== 'string' || !partner.trim()) return; 49 if (!partner || typeof partner !== 'string' || !partner.trim()) return;
50 50
51 const tokens = pingMessage.trim().split(/ +/); 51 const tokens = pingMessage.trim().split(/ +/);
52 52
53 const pongMessage = [ 53 const pongMessage = [
54 'PONG', 54 'PONG',
55 (tokens && tokens[1]) || null, 55 (tokens && tokens[1]) || null,
56 ].filter((item) => item) 56 ].filter((item) => item)
57 .join(' '); 57 .join(' ');
58 58
59 if (config.custom_ping && config.custom_ping.verbose) { 59 if (config.custom_ping && config.custom_ping.verbose) {
60 logger.verbose(`${MODULE_NAME} F0D18EF4: Responding PING message`, { 60 logger.verbose(`${MODULE_NAME} F0D18EF4: Responding PING message`, {
61 xid, partner, pongMessage, 61 xid, partner, pongMessage,
62 }); 62 });
63 } 63 }
64 64
65 bot.send(partner.trim(), pongMessage); 65 bot.send(partner.trim(), pongMessage);
66 }; 66 };
67 exports.sendPong = sendPong; 67 exports.sendPong = sendPong;
68 68
69 const pingSender = () => { 69 const pingSender = () => {
70 const xid = uniqid(); 70 const xid = uniqid();
71 if ( 71 if (
72 !bot 72 !bot
73 || !config.custom_ping 73 || !config.custom_ping
74 || !config.custom_ping.send_to 74 || !config.custom_ping.send_to
75 || !Array.isArray(config.custom_ping.send_to) 75 || !Array.isArray(config.custom_ping.send_to)
76 || !config.custom_ping.send_to.length 76 || !config.custom_ping.send_to.length
77 ) return; 77 ) return;
78 78
79 config.custom_ping.send_to.forEach((partner) => { 79 config.custom_ping.send_to.forEach((partner) => {
80 const msg = `PING ${xid}`;
80 if (config.custom_ping.verbose) { 81 if (config.custom_ping.verbose) {
81 logger.verbose(`${MODULE_NAME} 21358F45: Sending custom ping`, { 82 logger.verbose(`${MODULE_NAME} 21358F45: Sending custom ping`, {
82 xid, 83 xid,
83 partner, 84 partner,
85 msg,
84 }); 86 });
85 } 87 }
86 88
87 bot.send(partner, `PING ${xid}`); 89 bot.send(partner, msg);
88 }); 90 });
89 }; 91 };
90 92
91 if (config.custom_ping && config.custom_ping.send_to) { 93 if (config.custom_ping && config.custom_ping.send_to) {
92 const intervalMs = config.custom_ping.interval_ms || (25 * 1000); 94 const intervalMs = config.custom_ping.interval_ms || (25 * 1000);
93 logger.verbose(`${MODULE_NAME} 324034EE: Registering ping sender`, { 95 logger.verbose(`${MODULE_NAME} 324034EE: Registering ping sender`, {
94 intervalMs, 96 intervalMs,
95 partners: config.custom_ping.send_to, 97 partners: config.custom_ping.send_to,
96 }); 98 });
97 99
98 setInterval(() => { 100 setInterval(() => {
99 pingSender(); 101 pingSender();
100 }, intervalMs); 102 }, intervalMs);
101 } 103 }
102 104