From a7dac81cc3fcb3b834550fe9e4e0b4ef5d52e820 Mon Sep 17 00:00:00 2001
From: Niko Ehrenfeuchter <nikolaus.ehrenfeuchter@unibas.ch>
Date: Mon, 18 Sep 2017 18:59:39 +0200
Subject: [PATCH] Add SendTransferInterruptedMail.

Fixes #8
---
 AutoTx/AutoTx.cs                              |  2 +-
 AutoTx/Email.cs                               | 28 +++++++++++++++++++
 .../Mail-Templates/Transfer-Interrupted.txt   | 15 ++++++++++
 3 files changed, 44 insertions(+), 1 deletion(-)
 create mode 100644 AutoTx/Resources/Mail-Templates/Transfer-Interrupted.txt

diff --git a/AutoTx/AutoTx.cs b/AutoTx/AutoTx.cs
index 2a79234..e8e4d4e 100644
--- a/AutoTx/AutoTx.cs
+++ b/AutoTx/AutoTx.cs
@@ -342,7 +342,7 @@ namespace AutoTx
                 writeLog("Not all files were transferred - will resume upon next start");
                 writeLogDebug("CurrentTransferSrc: " + _status.CurrentTransferSrc);
                 // should we delete an incompletely transferred file on the target?
-                // TODO: send a notification to the user of the interrupted transfer (#38)
+                SendTransferInterruptedMail();
             }
             // set the shutdown status to clean:
             _status.CleanShutdown = true;
diff --git a/AutoTx/Email.cs b/AutoTx/Email.cs
index 49b8b9b..013008c 100644
--- a/AutoTx/Email.cs
+++ b/AutoTx/Email.cs
@@ -153,5 +153,33 @@ namespace AutoTx
                 writeLog("Error loading email template: " + ex.Message, true);
             }
         }
+
+        /// <summary>
+        /// Send a notification email when a transfer has been interrupted before completion.
+        /// Recipient address is derived from the global variable CurrentTransferSrc.
+        /// </summary>
+        public void SendTransferInterruptedMail() {
+            if (_config.SendTransferNotification == false)
+                return;
+
+            var userDir = new DirectoryInfo(_status.CurrentTransferSrc).Name;
+
+            var substitutions = new List<Tuple<string, string>> {
+                Tuple.Create("FACILITY_USER", GetFullUserName(userDir)),
+                Tuple.Create("HOST_ALIAS", _config.HostAlias),
+                Tuple.Create("HOST_NAME", Environment.MachineName),
+                Tuple.Create("EMAIL_FROM", _config.EmailFrom)
+            };
+
+            try {
+                var body = LoadMailTemplate("Transfer-Interrupted.txt", substitutions);
+                SendEmail(GetEmailAddress(userDir),
+                    ServiceName + " - Transfer Interrupted - " + _config.HostAlias,
+                    body);
+            }
+            catch (Exception ex) {
+                writeLog("Error loading email template: " + ex.Message, true);
+            }
+        }
     }
 }
\ No newline at end of file
diff --git a/AutoTx/Resources/Mail-Templates/Transfer-Interrupted.txt b/AutoTx/Resources/Mail-Templates/Transfer-Interrupted.txt
new file mode 100644
index 0000000..740b517
--- /dev/null
+++ b/AutoTx/Resources/Mail-Templates/Transfer-Interrupted.txt
@@ -0,0 +1,15 @@
+Dear FACILITY_USER,
+
+unfortunately the transfer service on "HOST_ALIAS" (HOST_NAME)
+has been stopped while one of your transfers was still running, probably
+due the computer being shut down.
+
+Your transfer will resume without any additional action required once the
+service is started again, which happens automatically when the computer has
+finished booting up again.
+
+
+Don't hesitate to contact EMAIL_FROM if you have any additional question.
+
+Have a nice day
+The Facility Team
-- 
GitLab