Commit e68d4d1d9b804418b19cc79e1036f713f6428acd
1 parent
8ce5a6910f
Exists in
master
ESLINT
Showing 1 changed file with 3 additions and 1 deletions Inline Diff
lib/transport.js
1 | const fs = require('fs'); | 1 | const fs = require('fs'); |
2 | const moment = require('moment'); | 2 | const moment = require('moment'); |
3 | const uniqid = require('uniqid'); | 3 | const uniqid = require('uniqid'); |
4 | 4 | ||
5 | const config = require('komodo-sdk/config'); | 5 | const config = require('komodo-sdk/config'); |
6 | const logger = require('komodo-sdk/logger'); | 6 | const logger = require('komodo-sdk/logger'); |
7 | 7 | ||
8 | async function send(partner, msg) { | 8 | async function send(partner, msg) { |
9 | if (typeof partner !== 'string' || typeof msg !== 'string') return; | 9 | if (typeof partner !== 'string' || typeof msg !== 'string') return; |
10 | if (!partner.trim() || !msg.trim()) return; | 10 | if (!partner.trim() || !msg.trim()) return; |
11 | 11 | ||
12 | const destination = partner.trim().replace(/^0/, '62').replace(/^\+/, ''); | 12 | const destination = partner.trim().replace(/^0/, '62').replace(/^\+/, ''); |
13 | 13 | ||
14 | if (!destination || !Number(destination) || destination.length < 8) return; | 14 | if (!destination || !Number(destination) || destination.length < 8) return; |
15 | 15 | ||
16 | if (msg.trim().search(/sdg DIPROSES$/) >= 0) return; | 16 | if (msg.trim().search(/sdg DIPROSES$/) >= 0) return; |
17 | 17 | ||
18 | const msgFileContent = ` | 18 | const msgFileContent = ` |
19 | To: ${destination} | 19 | To: ${destination} |
20 | 20 | ||
21 | ${msg.trim()} | 21 | ${msg.trim()} |
22 | `.trim(); | 22 | `.trim(); |
23 | 23 | ||
24 | const dir = config.outbox_dir || '/var/spool/sms/outgoing'; | 24 | const dir = config.outbox_dir || '/var/spool/sms/outgoing'; |
25 | const ts = moment().format('YYYYMMDD_HHmmss.SSS'); | 25 | const ts = moment().format('YYYYMMDD_HHmmss.SSS'); |
26 | const filename = `${dir}/komodo.${ts}.${uniqid()}`; | 26 | const filename = `${dir}/komodo.${ts}.${uniqid()}`; |
27 | 27 | ||
28 | fs.exists(dir, (isExists) => { | 28 | fs.exists(dir, (isExists) => { |
29 | if (!isExists) { | 29 | if (!isExists) { |
30 | logger.warn('Outbox dir is not exists', { dir }); | 30 | logger.warn('Outbox dir is not exists', { dir }); |
31 | return; | 31 | return; |
32 | } | 32 | } |
33 | 33 | ||
34 | logger.info('Writing outbox', { to: destination, msg: msg.trim(), msgLength: msg.trim().length, filename }); | 34 | logger.info('Writing outbox', { |
35 | to: destination, msg: msg.trim(), msgLength: msg.trim().length, filename, | ||
36 | }); | ||
35 | try { | 37 | try { |
36 | fs.promises.writeFile(filename, msgFileContent, { mode: 0o660 }); | 38 | fs.promises.writeFile(filename, msgFileContent, { mode: 0o660 }); |
37 | } catch (e) { | 39 | } catch (e) { |
38 | logger.warn('Exception on writing outbox', { | 40 | logger.warn('Exception on writing outbox', { |
39 | to: destination, | 41 | to: destination, |
40 | msg: msg.trim(), | 42 | msg: msg.trim(), |
41 | filename, | 43 | filename, |
42 | err: e.toString(), | 44 | err: e.toString(), |
43 | }); | 45 | }); |
44 | } | 46 | } |
45 | }); | 47 | }); |
46 | } | 48 | } |
47 | 49 | ||
48 | exports.send = send; | 50 | exports.send = send; |
49 | 51 |