From ede337dbcd923c9f0b800431fc84a3d2b9a99301 Mon Sep 17 00:00:00 2001
From: Jerome Charaoui <jerome@riseup.net>
Date: Sat, 23 Jan 2021 22:50:15 -0500
Subject: [PATCH] add reportwrap config parameter

set to wrap at 1000 columns by default to avoid MTA rejection allowed by
SMTP RFC2821
---
 CHANGELOG.md            |  5 +++++
 etc/backupninja.conf.in |  3 +++
 man/backupninja.conf.5  |  6 ++++++
 src/backupninja.in      |  3 ++-
 test/backupninja.bats   | 20 +++++++++++++++++++-
 5 files changed, 35 insertions(+), 2 deletions(-)

diff --git a/CHANGELOG.md b/CHANGELOG.md
index 7e6c548..e5b030a 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
 
 ## [Unreleased]
 
+### Added
+
+- [core] implement reportwrap configuration parameter
+
 ### Changed
 
 - [core] raise error if mail isn't found in $PATH and reportemail = yes
@@ -16,6 +20,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
 - [build] make build reproducible regardless of usrmerge (DEBBUG-915222)
 - [core] silence exit code message unless --debug is used
 - [core] backup halt should trigger email report if enabled
+- [core] wrap report email body to 1000 characters by default (DEBBUG-871793)
 
 ## [1.2.0] - 2021-01-21
 
diff --git a/etc/backupninja.conf.in b/etc/backupninja.conf.in
index a350fc8..f2366a3 100644
--- a/etc/backupninja.conf.in
+++ b/etc/backupninja.conf.in
@@ -46,6 +46,9 @@ reportuser = ninja
 # use a globally unique name, preferably the hostname
 reportdirectory = /var/lib/backupninja/reports
 
+# number of columns the report email body should wrap to
+#reportwrap = 80
+
 # set to the administration group that is allowed to
 # read/write configuration files in /etc/backup.d
 admingroup = root
diff --git a/man/backupninja.conf.5 b/man/backupninja.conf.5
index 72de35a..b4f5602 100644
--- a/man/backupninja.conf.5
+++ b/man/backupninja.conf.5
@@ -54,6 +54,10 @@ If set to 'yes', a report email will be generated even if all modules reported s
 .B reportwarning
 If set to 'yes', a report email will be generated even if there was no error.
 
+.TP
+.B reportwrap
+Number of columns the email report body should wrap to.
+
 .TP
 .B logfile
 The path of the logfile.
@@ -107,6 +111,8 @@ reportsuccess = yes
 .br
 reportwarning = yes
 .br
+reportwrap = 1000
+.br
 logfile = /var/log/backupninja.log
 .br
 configdirectory = /etc/backup.d
diff --git a/src/backupninja.in b/src/backupninja.in
index c8ba5d4..635d127 100755
--- a/src/backupninja.in
+++ b/src/backupninja.in
@@ -543,6 +543,7 @@ getconf RSYNC /usr/bin/rsync
 getconf DSYNC /usr/bin/dsync
 getconf DOVEADM /usr/bin/doveadm
 getconf admingroup root
+getconf reportwrap 1000
 
 if [ ! -d "$configdirectory" ]; then
    echo "Configuration directory '$configdirectory' not found."
@@ -636,7 +637,7 @@ if [ $doit == 1 ]; then
                fi
             done
          fi
-      } | mail -s "backupninja: $hostname $subject" $reportemail
+      } | fold -s -w "$reportwrap" | mail -s "backupninja: $hostname $subject" $reportemail
    else
       error "Unable to locate mail executable, email report not sent!"
       let "errors += 1"
diff --git a/test/backupninja.bats b/test/backupninja.bats
index 7d66150..083beae 100644
--- a/test/backupninja.bats
+++ b/test/backupninja.bats
@@ -128,7 +128,25 @@ create_test_action() {
     mv /usr/bin/mail /usr/bin/mail.moved
     run backupninja --now -f "${BATS_TMPDIR}/backupninja.conf" --run "${BATS_TMPDIR}/backup.d/test.sh"
     [ "$status" -eq 1 ]
-    false
+}
+
+@test "reports: wraps report text to 1000 columns by default" {
+    create_test_action info "$(printf \'=%.0s\' {1..2000})"
+    setconfig backupninja.conf reportsuccess yes
+    setconfig backupninja.conf reportinfo yes
+    run backupninja --now -f "${BATS_TMPDIR}/backupninja.conf" --run "${BATS_TMPDIR}/backup.d/test.sh"
+    sleep 0.1
+    grep -q '^=\{1000\}$' /var/mail/vagrant
+}
+
+@test "reports: wraps report text according to reportwrap" {
+    create_test_action info "$(printf \'=%.0s\' {1..2000})"
+    setconfig backupninja.conf reportsuccess yes
+    setconfig backupninja.conf reportinfo yes
+    setconfig backupninja.conf reportwrap 100
+    run backupninja --now -f "${BATS_TMPDIR}/backupninja.conf" --run "${BATS_TMPDIR}/backup.d/test.sh"
+    sleep 0.1
+    grep -q '^=\{100\}$' /var/mail/vagrant
 }
 
 @test "scheduling: runs when = 'everyday at 01' and time matches" {
-- 
GitLab