failed.js 3.03 KB
const fs = require('fs');
const path = require('path');
const moment = require('moment');
const logger = require('komodo-sdk/logger');
const mailparser = require('../mailparser-wrapper');
const config = require('../../config');

const eventType = 'FAILED';
const maxSecondsToRetry = 5 * 60;

module.exports = async (msgFilename) => {
    const msg = await fs.promises.readFile(msgFilename);

    if (!msg) {
        logger.warn('Sent failed detected but we can not read message file', { eventType, msgFile: msgFilename });
        return;
    }

    const parsedMsg = await mailparser(msg);
    if (!parsedMsg || !parsedMsg.headers) {
        logger.warn('Sent failed detected and we can not parsed message file', {
            eventType,
            msgFile: msgFilename,
            msgFileContent: (msg || '').toString(),
        });
        return;
    }

    const to = parsedMsg.headers.get('to').text;
    const modem = parsedMsg.headers.get('modem');
    const imsi = parsedMsg.headers.get('imsi');
    const imei = parsedMsg.headers.get('imei');
    const failReason = parsedMsg.headers.get('fail_reason');
    const failed = parsedMsg.headers.get('failed');
    const { text } = parsedMsg;

    const originalTsMatched = msgFilename.match(/komodo\.((\d+)_(\d+))/);
    const originalTs = originalTsMatched && originalTsMatched[1];
    const originalTsParsed = originalTs && moment(originalTs, 'YYYYMMDD_HHmmss');

    const deltaTimeMs = originalTsParsed && Number(new Date() - originalTsParsed);
    const retry = (deltaTimeMs && (deltaTimeMs < (maxSecondsToRetry * 1000)))
        && !config.smstools_disable_retry_on_failed;

    logger.verbose('Sent failed detected', {
        eventType,
        msgFile: msgFilename,
        to,
        modem,
        imsi,
        imei,
        failReason,
        failed,
        originalTsParsed,
        deltaTimeMs,
        maxSecondsToRetry,
        configAllowRetry: config.smstools_disable_retry_on_failed,
        retry,
        text,
        textLength: text.length,
    });

    if (!retry) {
        return;
    }

    try {
        await fs.promises.unlink(msgFilename);
    } catch (e) {
        logger.warn(`Can not unlink failed message for send retry. ${e.message || e.toString()}`, {
            eventType,
            msgFile: msgFilename,
            to,
            text,
        });
        return;
    }

    const newMessageContent = `
To: ${to}

${text}
    `.trim();

    const outboxDir = config.outbox_dir || '/var/spool/sms/outgoing';
    const newFileName = `${outboxDir}/${path.basename(msgFilename)}`;

    try {
        fs.promises.writeFile(newFileName, newMessageContent, { mode: 0o660 });
    } catch (e) {
        logger.warn(`Can not create retry file. ${e.message || e.toString()}`, {
            eventType,
            msgFile: msgFilename,
            newFileName,
            to,
            text,
        });

        return;
    }

    logger.info('Retry file written', {
        eventType,
        msgFile: msgFilename,
        newFileName,
        to,
        text,
        textLength: text.length,
    });
};