From 2a95ce003955728d14fe18c3ec71da565a7cd8dd Mon Sep 17 00:00:00 2001
From: Marcus Nordenberg <marcus.nordenberg@ssab.com>
Date: Fri, 20 Mar 2020 12:00:09 +0100
Subject: [PATCH] Add pwr_mailto wrapper to pwrrt package

* Change default email command of PostConfig
* Add pwr_mailto wrapper for sending emails/sms
* Include pwr_mailto in pwrrt package

(cherry picked from commit 586ac7750947ca2c1a15fba14efa60b3ef949133)
---
 src/tools/pkg/deb_x86_64/pwrrt/build.sh    |  9 +---
 src/tools/pkg/deb_x86_64/pwrrt/pwr_mailto  | 37 +++++++++++++
 src/wbl/pwrb/src/pwrb_c_postconfig.wb_load | 60 +++++++++++-----------
 3 files changed, 68 insertions(+), 38 deletions(-)
 create mode 100755 src/tools/pkg/deb_x86_64/pwrrt/pwr_mailto

diff --git a/src/tools/pkg/deb_x86_64/pwrrt/build.sh b/src/tools/pkg/deb_x86_64/pwrrt/build.sh
index 61cd34d80..0575479d0 100755
--- a/src/tools/pkg/deb_x86_64/pwrrt/build.sh
+++ b/src/tools/pkg/deb_x86_64/pwrrt/build.sh
@@ -148,19 +148,12 @@ gzip -fq --best $pkgroot/usr/share/man/man1/rt_rtt.1
 
 # Copy proview
 mkdir $pkgroot/usr/pwrrt
-#currentdir="`eval pwd`"
-#tarfile=$pwre_broot/$pwre_target/bld/pkg/pwrtmp.tar
-#cd $pwre_broot/$pwre_target/exp
 echo "-- Copy release to package tree"
 cp -r $pwre_broot/$pwre_target/exp/* $pkgroot/usr/pwrrt/
-##tar -cf $tarfile *
-#cd $pkgroot/usr/pwrrt
-#tar -xf $tarfile
-#rm $tarfile
 rm -r $pkgroot/usr/pwrrt/lib/*.a
 rm -r $pkgroot/usr/pwrrt/exe/wb*
 cp $pwr_eexe/wb_distr_keepboot.sh $pkgroot/usr/pwrrt/exe
-#cd $currentdir
+cp $pkgsrc/pwr_mailto $pkgroot/usr/pwrrt/exe/
 
 # Copy configuration files to cnf
 cp $pkgsrc/proview.cnf $pkgroot/usr/pwrrt/cnf
diff --git a/src/tools/pkg/deb_x86_64/pwrrt/pwr_mailto b/src/tools/pkg/deb_x86_64/pwrrt/pwr_mailto
new file mode 100755
index 000000000..01a630431
--- /dev/null
+++ b/src/tools/pkg/deb_x86_64/pwrrt/pwr_mailto
@@ -0,0 +1,37 @@
+#!/bin/bash
+
+# To use this you need an MTA installed. This particular script uses sendmail "syntax".
+# If you want it up and running quickly a simple MTA that we (at SSAB) use, is msmtp.
+# It's still available in buster. It uses sendmail syntax aswell...
+# If another MTA is used you need to update this script accordingly or change the
+# sms/email command attribute of the PostConfig object in ProviewR.
+
+# Regarding msmtp:
+# By default, mails are sent in a 'fire and forget' manner. If you need msmtp to actually
+# wait for a delivery status notification (DSN) you need to set this up aswell in the
+# msmtp. The lines:
+# dsn_notify failure
+# dsn_return headers
+# Are in most cases sufficient for a simple propagation of the status code back to ProviewR.
+# These features will block while waiting for the DSN to arrive, therefore ProviewR call
+# sms/email command asynchronous, checking the status of the calls every now and then...
+
+# Arguments should be quoted
+
+# Simple argument count control...
+[[ ${#} -ne 4 ]] && exit 1
+
+MESSAGE="${4}"
+SENDMAIL=/usr/bin/msmtp
+
+${SENDMAIL} -t -- << EOF
+From: ${1}
+To: ${2}
+Subject: ${3}
+
+${MESSAGE}
+EOF
+
+# Do not add more commands here since we need the exit code from "sendmail". If you need
+# to do more things please exit the shell with an appropriate exit code to reflect the
+# actual status of the command that sent the mail :)
diff --git a/src/wbl/pwrb/src/pwrb_c_postconfig.wb_load b/src/wbl/pwrb/src/pwrb_c_postconfig.wb_load
index bbb6b0f78..f747c2315 100644
--- a/src/wbl/pwrb/src/pwrb_c_postconfig.wb_load
+++ b/src/wbl/pwrb/src/pwrb_c_postconfig.wb_load
@@ -1,36 +1,36 @@
-! 
+!
 !  ProviewR   Open Source Process Control.
 !  Copyright (C) 2005-2020 SSAB EMEA AB.
 ! 
 !  This file is part of ProviewR.
 !
-!  This program is free software; you can redistribute it and/or 
-!  modify it under the terms of the GNU General Public License as 
-!  published by the Free Software Foundation, either version 2 of 
+!  This program is free software; you can redistribute it and/or
+!  modify it under the terms of the GNU General Public License as
+!  published by the Free Software Foundation, either version 2 of
 !  the License, or (at your option) any later version.
 !
-!  This program is distributed in the hope that it will be useful 
-!  but WITHOUT ANY WARRANTY; without even the implied warranty of 
-!  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 
+!  This program is distributed in the hope that it will be useful
+!  but WITHOUT ANY WARRANTY; without even the implied warranty of
+!  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 !  GNU General Public License for more details.
 !
-!  You should have received a copy of the GNU General Public License 
+!  You should have received a copy of the GNU General Public License
 !  along with ProviewR. If not, see <http://www.gnu.org/licenses/>
-! 
+!
 !  Linking ProviewR statically or dynamically with other modules is
 !  making a combined work based on ProviewR. Thus, the terms and
-!  conditions of the GNU General Public License cover the whole 
+!  conditions of the GNU General Public License cover the whole
 !  combination.
-! 
+!
 !  In addition, as a special exception, the copyright holders of
 !  ProviewR give you permission to, from the build function in the
 !  ProviewR Configurator, combine ProviewR with modules generated by the
 !  ProviewR PLC Editor to a PLC program, regardless of the license
 !  terms of these modules. You may copy and distribute the resulting
-!  combined work under the terms of your choice, provided that every 
-!  copy of the combined work is accompanied by a complete copy of 
+!  combined work under the terms of your choice, provided that every
+!  copy of the combined work is accompanied by a complete copy of
 !  the source code of ProviewR (the version used to produce the
-!  combined work), being distributed under the terms of the GNU 
+!  combined work), being distributed under the terms of the GNU
 !  General Public License plus this exception.
 !
 !  pwrb_c_postconfig.wb_load -- Defines the class PostConfig.
@@ -41,7 +41,7 @@ SObject pwrb:Class
 !  @Group Servers,NodeConfiguration
 !  @Summary Configuration of event post server.
 !  Configuration of event post server.
-! 
+!
 !  The Post server receives alarm events from the event monitor
 !  and forwards them to SMS numbers and email addresses.
 !
@@ -50,13 +50,13 @@ SObject pwrb:Class
 !  in the sup object, and this recipient is search for in the user database
 !  under the current system group, and from the found user, the SMS number
 !  or email address is fetched. Also a list of recipients can be specifed,
-!  separated by comma, and the event is forwarded to the SMS number or 
+!  separated by comma, and the event is forwarded to the SMS number or
 !  email address of all these users.
 !
 !  A recipient can, in the sup object, be specified by a symbol. The should
 !  be defined in the Symbol table in the PostConfig object. If for example
 !  the recipient is specified with '$on_duty', and a symbol with name 'on_duty'
-!  and value 'john, dennis' is defined in symbol table, the event will be 
+!  and value 'john, dennis' is defined in symbol table, the event will be
 !  forwarded to the users john and dennis.
 !
 !  The command to send a SMS or email depeneds on the utility that is used,
@@ -64,35 +64,35 @@ SObject pwrb:Class
 !  EmailCmd and SMS_Cmd attributes. Replace the email address or SMS number
 !  with the string '$address' and the message text with the string '$text'.
 !*/
-  Object PostConfig $ClassDef 548 
+  Object PostConfig $ClassDef 548
     Body SysBody
       Attr Editor = pwr_eEditor_AttrEd
       Attr Method = pwr_eMethod_Standard
     EndBody
-    Object RtBody $ObjBodyDef 1 
+    Object RtBody $ObjBodyDef 1
       Body SysBody
         Attr StructName = "PostConfig"
       EndBody
       !/**
       !  Specifies the email shell command.
-      !  The command should contain the strings $address and $text. 
-      !  $address will be replaced by the email address for the 
-      !  specified user, and $text will be replaced by a message 
+      !  The command should contain the strings $address and $text.
+      !  $address will be replaced by the email address for the
+      !  specified user, and $text will be replaced by a message
       !  text with event text and event name.
       !*/
-      Object EmailCmd $Attribute 1 
+      Object EmailCmd $Attribute 1
         Body SysBody
           Attr TypeRef = "pwrs:Type-$String256"
         EndBody
       EndObject
       !/**
       !  Specifies the SMS shell command.
-      !  The command should contain the strings $address and $text. 
-      !  $address will be replaced by the sms number for the specified 
-      !  user, and $text will be replaced by a message text with event 
+      !  The command should contain the strings $address and $text.
+      !  $address will be replaced by the sms number for the specified
+      !  user, and $text will be replaced by a message text with event
       !  text and event name.
       !*/
-      Object SMS_Cmd $Attribute 2 
+      Object SMS_Cmd $Attribute 2
         Body SysBody
           Attr TypeRef = "pwrs:Type-$String256"
         EndBody
@@ -103,7 +103,7 @@ SObject pwrb:Class
       !  hierarchies can be specified. If messages from e.g. a
       !  Watchdog object in a node will be received, then the
       !  name of the node will be specified.
-      !  
+      !
       !  Note! If the SelectList is left blank no message at all is
       !  sent to the user.
       !*/
@@ -125,7 +125,7 @@ SObject pwrb:Class
       !  The Recipient attibute in a DSup is 'john, $on_duty'. A symbol is defined
       !  with the name 'on_duty' and the value 'dennis, jimmy'. The resultant
       !  recipient string will be 'john, dennis, jimmy' and the event will be posted
-      !  to the address of these three. 
+      !  to the address of these three.
       !*/
       Object Symbols $Attribute 4
         Body SysBody
@@ -193,7 +193,7 @@ SObject pwrb:Class
     EndObject
     Object Template PostConfig
       Body RtBody
-        Attr EmailCmd = "mailto 'ProviewR' '$address' 'ProviewR alarm' '$text'"
+        Attr EmailCmd = "pwr_mailto 'ProviewR' '$address' 'ProviewR alarm' '$text'"
       EndBody
     EndObject
   EndObject
-- 
2.30.9