From 365a302c96baf5481864325fa1bd244769137e68 Mon Sep 17 00:00:00 2001 From: Fred Gleason Date: Thu, 19 Nov 2020 08:52:51 -0500 Subject: [PATCH] 2020-11-18 Fred Gleason * Added an 'RDSendMail()' function in 'lib/rdsendmail.[cpp|h]'. Signed-off-by: Fred Gleason --- .gitignore | 1 + ChangeLog | 2 + lib/Makefile.am | 1 + lib/lib.pro | 2 + lib/librd_cs.ts | 28 ++++++ lib/librd_de.ts | 28 ++++++ lib/librd_es.ts | 28 ++++++ lib/librd_fr.ts | 28 ++++++ lib/librd_nb.ts | 28 ++++++ lib/librd_nn.ts | 28 ++++++ lib/librd_pt_BR.ts | 28 ++++++ lib/rdsendmail.cpp | 203 ++++++++++++++++++++++++++++++++++++++++ lib/rdsendmail.h | 37 ++++++++ lib/rduser.h | 1 + tests/Makefile.am | 4 + tests/sendmail_test.cpp | 97 +++++++++++++++++++ tests/sendmail_test.h | 35 +++++++ 17 files changed, 579 insertions(+) create mode 100644 lib/rdsendmail.cpp create mode 100644 lib/rdsendmail.h create mode 100644 tests/sendmail_test.cpp create mode 100644 tests/sendmail_test.h diff --git a/.gitignore b/.gitignore index d946a238..92d04f80 100644 --- a/.gitignore +++ b/.gitignore @@ -131,6 +131,7 @@ tests/readcd_test tests/reserve_carts_test tests/sas_switch_torture tests/sas_torture +tests/sendmail_test tests/stringcode_test tests/test_hash tests/test_pam diff --git a/ChangeLog b/ChangeLog index af1f9d45..c03e6e7b 100644 --- a/ChangeLog +++ b/ChangeLog @@ -20601,3 +20601,5 @@ Operations Guide. 2020-11-16 Fred Gleason * Incremented the package version to 3.4.1int6. +2020-11-18 Fred Gleason + * Added an 'RDSendMail()' function in 'lib/rdsendmail.[cpp|h]'. diff --git a/lib/Makefile.am b/lib/Makefile.am index ba4aaa07..33e53274 100644 --- a/lib/Makefile.am +++ b/lib/Makefile.am @@ -208,6 +208,7 @@ dist_librd_la_SOURCES = dbversion.h\ rdschedcodes_dialog.cpp rdschedcodes_dialog.h\ rdschedruleslist.cpp rdschedruleslist.h\ rdsegmeter.cpp rdsegmeter.h\ + rdsendmail.cpp rdsendmail.h\ rdsettings.cpp rdsettings.h\ rdsimpleplayer.cpp rdsimpleplayer.h\ rdslider.cpp rdslider.h\ diff --git a/lib/lib.pro b/lib/lib.pro index e259cb54..233acc73 100644 --- a/lib/lib.pro +++ b/lib/lib.pro @@ -148,6 +148,7 @@ SOURCES += rdrssschemas.cpp SOURCES += rdrsscategorybox.cpp SOURCES += rdschedcode.cpp SOURCES += rdsegmeter.cpp +SOURCES += rdsendmail.cpp SOURCES += rdsettings.cpp SOURCES += rdsimpleplayer.cpp SOURCES += rdslider.cpp @@ -292,6 +293,7 @@ HEADERS += rdrssschemas.h HEADERS += rdrsscategorybox.h HEADERS += rdschedcode.h HEADERS += rdsegmeter.h +HEADERS += rdsendmail.h HEADERS += rdsettings.h HEADERS += rdsimpleplayer.h HEADERS += rdslider.h diff --git a/lib/librd_cs.ts b/lib/librd_cs.ts index 6bfb8daa..558e30a4 100644 --- a/lib/librd_cs.ts +++ b/lib/librd_cs.ts @@ -849,6 +849,34 @@ [none] + + address + + + + is invalid + + + + You must supply a "from" address + + + + unable to start sendmail + + + + sendmail crashed + + + + sendmail returned non-zero exit code + + + + ok + + RDAddCart diff --git a/lib/librd_de.ts b/lib/librd_de.ts index 8e849320..1f35a19d 100644 --- a/lib/librd_de.ts +++ b/lib/librd_de.ts @@ -845,6 +845,34 @@ [none] + + address + + + + is invalid + + + + You must supply a "from" address + + + + unable to start sendmail + + + + sendmail crashed + + + + sendmail returned non-zero exit code + + + + ok + + RDAddCart diff --git a/lib/librd_es.ts b/lib/librd_es.ts index 27d98bfd..7329a07c 100644 --- a/lib/librd_es.ts +++ b/lib/librd_es.ts @@ -845,6 +845,34 @@ [none] + + address + + + + is invalid + + + + You must supply a "from" address + + + + unable to start sendmail + + + + sendmail crashed + + + + sendmail returned non-zero exit code + + + + ok + + RDAddCart diff --git a/lib/librd_fr.ts b/lib/librd_fr.ts index abf50a7e..3f83c081 100644 --- a/lib/librd_fr.ts +++ b/lib/librd_fr.ts @@ -815,6 +815,34 @@ [none] + + address + + + + is invalid + + + + You must supply a "from" address + + + + unable to start sendmail + + + + sendmail crashed + + + + sendmail returned non-zero exit code + + + + ok + + RDAddCart diff --git a/lib/librd_nb.ts b/lib/librd_nb.ts index e7758b9c..c7da2396 100644 --- a/lib/librd_nb.ts +++ b/lib/librd_nb.ts @@ -845,6 +845,34 @@ [none] + + address + + + + is invalid + + + + You must supply a "from" address + + + + unable to start sendmail + + + + sendmail crashed + + + + sendmail returned non-zero exit code + + + + ok + + RDAddCart diff --git a/lib/librd_nn.ts b/lib/librd_nn.ts index e7758b9c..c7da2396 100644 --- a/lib/librd_nn.ts +++ b/lib/librd_nn.ts @@ -845,6 +845,34 @@ [none] + + address + + + + is invalid + + + + You must supply a "from" address + + + + unable to start sendmail + + + + sendmail crashed + + + + sendmail returned non-zero exit code + + + + ok + + RDAddCart diff --git a/lib/librd_pt_BR.ts b/lib/librd_pt_BR.ts index 11e9c346..d1333452 100644 --- a/lib/librd_pt_BR.ts +++ b/lib/librd_pt_BR.ts @@ -845,6 +845,34 @@ [none] + + address + + + + is invalid + + + + You must supply a "from" address + + + + unable to start sendmail + + + + sendmail crashed + + + + sendmail returned non-zero exit code + + + + ok + + RDAddCart diff --git a/lib/rdsendmail.cpp b/lib/rdsendmail.cpp new file mode 100644 index 00000000..bf8262a3 --- /dev/null +++ b/lib/rdsendmail.cpp @@ -0,0 +1,203 @@ +// rdsendmail.cpp +// +// Send an e-mail message using sendmail(1) +// +// (C) Copyright 2020 Fred Gleason +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License version 2 as +// published by the Free Software Foundation. +// +// 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 along with this program; if not, write to the Free Software +// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. +// + +#include + +#include "rdsendmail.h" +#include "rduser.h" + +#include + +bool __RDSendMail_IsAscii(const QString &str) +{ + for(int i=0;i127)||(ch.row()>0)) { + return false; + } + } + + return true; +} + + +QByteArray __RDSendMail_EncodeBody(QString *charset,QString *encoding, + const QString &str) +{ + if(__RDSendMail_IsAscii(str)) { + *charset=""; + *encoding=""; + return str.toAscii(); + } + *charset=";charset=utf8"; + *encoding="Content-Transfer-Encoding: base64\r\n"; + QByteArray ret; + QByteArray raw=str.toUtf8(); + for(int i=0;iisEmpty()) { + return false; + } + + // + // Compose Message + // + QString charset; + QString encoding; + QByteArray raw=__RDSendMail_EncodeBody(&charset,&encoding,body); + + msg+="From: "+from_addr+"\r\n"; + + // msg+="Content-Type: text/plain;charset=utf-8\r\n"; + // msg+="Content-Transfer-Encoding: base64\r\n"; + msg+="Content-Type: text/plain"+charset+"\r\n"; + msg+=encoding; + + if(to_addrs.size()>0) { + msg+="To: "; + for(int i=0;i0) { + msg+="Cc: "; + for(int i=0;i0) { + msg+="Bcc: "; + for(int i=0;istart("sendmail",args); + if(!proc->waitForStarted()) { + *err_msg=QObject::tr("unable to start sendmail")+"\n"; + delete proc; + return false; + } + proc->write(msg.toUtf8()); + proc->closeWriteChannel(); + proc->waitForFinished(); + if(proc->exitStatus()!=QProcess::NormalExit) { + *err_msg=QObject::tr("sendmail crashed")+"\r\n"; + delete proc; + return false; + } + if(proc->exitCode()!=0) { + *err_msg=QObject::tr("sendmail returned non-zero exit code")+ + QString().sprintf(": %d [",proc->exitCode())+ + QString::fromUtf8(proc->readAllStandardError())+"]\n"; + delete proc; + return false; + } + delete proc; + + *err_msg=QObject::tr("ok"); + + return true; +} + + +bool RDSendMail(QString *err_msg,const QString &subject,const QString &body, + const QString &from_addr,const QString &to_addrs, + const QString &cc_addrs,const QString &bcc_addrs) +{ + return RDSendMail(err_msg,subject,body,from_addr, + to_addrs.split(",",QString::SkipEmptyParts), + cc_addrs.split(",",QString::SkipEmptyParts), + bcc_addrs.split(",",QString::SkipEmptyParts)); +} diff --git a/lib/rdsendmail.h b/lib/rdsendmail.h new file mode 100644 index 00000000..cc05e0a6 --- /dev/null +++ b/lib/rdsendmail.h @@ -0,0 +1,37 @@ +// rdsendmail.h +// +// Send an e-mail message using sendmail(1) +// +// (C) Copyright 2020 Fred Gleason +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License version 2 as +// published by the Free Software Foundation. +// +// 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 along with this program; if not, write to the Free Software +// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. +// + +#ifndef RDSENDMAIL_H +#define RDSENDMAIL_H + +#include +#include + +bool RDSendMail(QString *err_msg,const QString &subject,const QString &body, + const QString &from_addr,const QStringList &to_addrs, + const QStringList &cc_addrs=QStringList(), + const QStringList &bcc_addrs=QStringList()); +bool RDSendMail(QString *err_msg,const QString &subject,const QString &body, + const QString &from_addr,const QString &to_addrs, + const QString &cc_addrs=QString(), + const QString &bcc_addrs=QString()); + + +#endif // RDSENDMAIL diff --git a/lib/rduser.h b/lib/rduser.h index 21a13d37..9800f512 100644 --- a/lib/rduser.h +++ b/lib/rduser.h @@ -22,6 +22,7 @@ #define RDUSER_H #include +#include class RDUser { diff --git a/tests/Makefile.am b/tests/Makefile.am index e8f08290..0d45ffc7 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -46,6 +46,7 @@ noinst_PROGRAMS = audio_convert_test\ rdxml_parse_test\ readcd_test\ reserve_carts_test\ + sendmail_test\ stringcode_test\ test_hash\ test_pam\ @@ -117,6 +118,9 @@ readcd_test_LDADD = @LIB_RDLIBS@ @LIBVORBIS@ @QT4_LIBS@ @MUSICBRAINZ_LIBS@ -lQt3 dist_reserve_carts_test_SOURCES = reserve_carts_test.cpp reserve_carts_test.h reserve_carts_test_LDADD = @LIB_RDLIBS@ @LIBVORBIS@ @QT4_LIBS@ @MUSICBRAINZ_LIBS@ -lQt3Support +dist_sendmail_test_SOURCES = sendmail_test.cpp sendmail_test.h +sendmail_test_LDADD = @LIB_RDLIBS@ @LIBVORBIS@ @QT4_LIBS@ @MUSICBRAINZ_LIBS@ -lQt3Support + dist_stringcode_test_SOURCES = stringcode_test.cpp stringcode_test.h stringcode_test_LDADD = @LIB_RDLIBS@ @LIBVORBIS@ @QT4_LIBS@ @MUSICBRAINZ_LIBS@ -lQt3Support diff --git a/tests/sendmail_test.cpp b/tests/sendmail_test.cpp new file mode 100644 index 00000000..3427bf08 --- /dev/null +++ b/tests/sendmail_test.cpp @@ -0,0 +1,97 @@ +// sendmail_test.cpp +// +// Test the Rivendell string encoder routines. +// +// (C) Copyright 2013,2016 Fred Gleason +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License version 2 as +// published by the Free Software Foundation. +// +// 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 along with this program; if not, write to the Free Software +// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. +// + +#include +#include + +#include +#include + +#include +#include +#include +#include + +#include "sendmail_test.h" + +MainObject::MainObject(QObject *parent) + :QObject(parent) +{ + QString err_msg; + QString from_addr; + QString to_addrs; + QString cc_addrs; + QString bcc_addrs; + QString subject; + QString body; + + // + // Read Command Options + // + RDCmdSwitch *cmd= + new RDCmdSwitch(qApp->argc(),qApp->argv(),"sendmail_test", + SENDMAIL_TEST_USAGE); + for(unsigned i=0;ikeys();i++) { + if(cmd->key(i)=="--from-addr") { + from_addr=cmd->value(i); + cmd->setProcessed(i,true); + } + if(cmd->key(i)=="--to-addrs") { + to_addrs=cmd->value(i); + cmd->setProcessed(i,true); + } + if(cmd->key(i)=="--cc-addrs") { + cc_addrs=cmd->value(i); + cmd->setProcessed(i,true); + } + if(cmd->key(i)=="--bcc-addrs") { + bcc_addrs=cmd->value(i); + cmd->setProcessed(i,true); + } + if(cmd->key(i)=="--subject") { + subject=cmd->value(i); + cmd->setProcessed(i,true); + } + if(cmd->key(i)=="--body") { + body=cmd->value(i); + cmd->setProcessed(i,true); + } + if(!cmd->processed(i)) { + fprintf(stderr,"sendmail_test: unknown option \"%s\"\n", + (const char *)cmd->key(i)); + exit(RDApplication::ExitInvalidOption); + } + } + + if(!RDSendMail(&err_msg,subject,body,from_addr,to_addrs,cc_addrs,bcc_addrs)) { + fprintf(stderr,"%s\n",err_msg.toUtf8().constData()); + exit(256); + } + + exit(RDApplication::ExitOk); +} + + +int main(int argc,char *argv[]) +{ + QApplication a(argc,argv,false); + new MainObject(); + return a.exec(); +} diff --git a/tests/sendmail_test.h b/tests/sendmail_test.h new file mode 100644 index 00000000..bbc80918 --- /dev/null +++ b/tests/sendmail_test.h @@ -0,0 +1,35 @@ +// sendmail_test.h +// +// Test the Rivendell email sending routines. +// +// (C) Copyright 2020 Fred Gleason +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License version 2 as +// published by the Free Software Foundation. +// +// 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 along with this program; if not, write to the Free Software +// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. +// + +#ifndef SENDMAIL_TEST_H +#define SENDMAIL_TEST_H + +#include + +#define SENDMAIL_TEST_USAGE "[options]\n\nTest the Rivendell email sending routines\n\nOptions are:\n--from-address=\n Originating email address\n\n--to-addresses=\n To addresses (comma seperated)\n\n--cc-addresses=\n CC addresses (comma seperated)\n\n--bcc-addresses=\n BCC addresses (comma seperated)\n\n--subject=\n Message subject\n\n--body=\n Message body\n\n" + +class MainObject : public QObject +{ + public: + MainObject(QObject *parent=0); +}; + + +#endif // SENDMAIL_TEST_H