From 9966caa22aeefbba371305ca15ae9a6b92fa2766 Mon Sep 17 00:00:00 2001 From: cgeek Date: Sat, 31 Aug 2019 18:19:09 +0200 Subject: [PATCH] [enh] add `cc` for mail in webDiffServers --- src/lib/mail.ts | 16 ++++++++-------- src/lib/types/conf.ts | 1 + src/lib/watchers/webdiff/webdiff-watcher.ts | 8 ++++---- 3 files changed, 13 insertions(+), 12 deletions(-) diff --git a/src/lib/mail.ts b/src/lib/mail.ts index 248afdc..22c2fd2 100644 --- a/src/lib/mail.ts +++ b/src/lib/mail.ts @@ -4,7 +4,7 @@ import {ConfMail} from './types/conf' import * as nodemailer from 'nodemailer' import * as os from 'os' -export async function sendMail(conf: ConfMail, subject: string, html: string) { +export async function sendMail(conf: ConfMail, subject: string, html: string, cc?: string) { if (conf.enabled) { let transporter = nodemailer.createTransport({ @@ -23,7 +23,7 @@ export async function sendMail(conf: ConfMail, subject: string, html: string) { let info = await transporter.sendMail({ from: conf.from, to: conf.to, - cc: conf.cc, + cc: (conf.cc ? [conf.cc] : []).concat(cc ? [cc] : []).join(','), subject, html }) @@ -37,9 +37,9 @@ export const mail = { Connection from [${os.hostname}] to ${target} established on ${moment().format('DD-MM-YYYY HH:mm:ss')}.

`) => { - return async () => { + return async (cc?: string) => { console.log(`${message}`) - await sendMail(conf.mail, `[dwatcher] [${os.hostname}] ${message}`, getHtml()) + await sendMail(conf.mail, `[dwatcher] [${os.hostname}] ${message}`, getHtml(), cc) } }, @@ -51,10 +51,10 @@ export const mail = { Waiting ${(waitingDelay / 1000).toFixed(0)} seconds before trying to reconnect.

`) => { - return async (waitingDelay: number) => { + return async (waitingDelay: number, cc?: string) => { console.log(`${message}`) console.log('Waiting %s seconds...', (waitingDelay / 1000).toFixed(0)) - await sendMail(conf.mail, `[dwatcher] [${os.hostname}] ${message}`, getHtml(waitingDelay)) + await sendMail(conf.mail, `[dwatcher] [${os.hostname}] ${message}`, getHtml(waitingDelay), cc) } }, @@ -63,9 +63,9 @@ export const mail = { Connection from [${os.hostname}] to ${target} was recovered on ${moment().format('dd-MM-YYYY HH:mm:ss')}.

`) => { - return async () => { + return async (cc?: string) => { console.log(`${message}`) - await sendMail(conf.mail, `[dwatcher] [${os.hostname}] ${message}`, getHtml()) + await sendMail(conf.mail, `[dwatcher] [${os.hostname}] ${message}`, getHtml(), cc) } }, diff --git a/src/lib/types/conf.ts b/src/lib/types/conf.ts index 68348d9..c5ca0e4 100644 --- a/src/lib/types/conf.ts +++ b/src/lib/types/conf.ts @@ -25,6 +25,7 @@ export interface ConfWebDiff { file1: string file2: string frequency: number + cc?: string } export interface ConfBMA extends ConfURL { diff --git a/src/lib/watchers/webdiff/webdiff-watcher.ts b/src/lib/watchers/webdiff/webdiff-watcher.ts index 4e42c3a..ad5d77c 100644 --- a/src/lib/watchers/webdiff/webdiff-watcher.ts +++ b/src/lib/watchers/webdiff/webdiff-watcher.ts @@ -62,21 +62,21 @@ export function webDiffWatcher(conf: Conf) { conf.reconnectionDelays, - mail.onEstablished(conf, target, 'webdiff successfully started'), + () => mail.onEstablished(conf, target, 'webdiff successfully started')(webDiffConf.cc), // When a disconnection is detected - mail.onDisconnect(conf, target, 'Diff detected', (waitingDelay: number) => ` + (waitingDelay: number) => mail.onDisconnect(conf, target, 'Diff detected', (waitingDelay: number) => ` ${htmlDiff}

Waiting ${(waitingDelay / 1000).toFixed(0)} seconds before trying to reconnect.

- `), + `)(waitingDelay, webDiffConf.cc), async () => { console.log('Trying to connect to %s', target) }, - mail.onRestartSuccess(conf, target), + () => mail.onRestartSuccess(conf, target)(webDiffConf.cc), async (e) => { console.error(e.message)