]> git.ipfire.org Git - thirdparty/mlmmj.git/commitdiff
mlmmj-make-ml: implement non-interactive mailing list creation 4/head
authorSiva Mahadevan <me@svmhdvn.name>
Wed, 14 Jun 2023 03:30:32 +0000 (23:30 -0400)
committerSiva Mahadevan <me@svmhdvn.name>
Thu, 15 Jun 2023 21:06:36 +0000 (17:06 -0400)
src/mlmmj-make-ml.in

index 273a0b0d843ad039ce73b36a47ec281ec5ecc3bd..f74699a8937337bb1cd1078c643884ce35d4ed22 100755 (executable)
@@ -3,39 +3,62 @@
 # mlmmj-make-ml - henne@hennevogel.de
 #
 
-VERSION="0.1"
-DEFAULTDIR="/var/spool/mlmmj"
+VERSION="0.2"
 ALIASFILE=/etc/aliases
+CRONTAB=/etc/crontab
+TEXTLIBDIR="@textlibdir@"
 
 USAGE="mlmmj-make-ml $VERSION
-$0
-             [-h] [-L listname] [-s spooldir] [-a] [-c user] [-z]
- -h: display this help text
- -L: the name of the mailing list
- -s: your spool directory (default $DEFAULTDIR)
+
+$0 <options>
+
+options:
+ -L listname: the name of the mailing list
  -a: create the needed entries in your $ALIASFILE file
- -c: user to chown the spool directory to (default not to chown at all)
+ -b: add needed entry to $CRONTAB
+ -c user: user to chown the spool directory to
+ -d fqdn: fully qualified domain name of the mailing list
+ -f answerfile: use answerfile to create mailing list non-interactively
+ -h: display this help text
+ -n answerfile: Do nothing. Instead, create answer file for later non-interactive use with -f
+ -s spooldir: mlmmj spool directory
+ -t lang: list text directory relative to $TEXTLIBDIR for given language
 "
 
-while getopts ":hL:s:ac:" Option
+while getopts ":L:abc:d:f:hn:s:t:" Option
 do
 case "$Option" in
+       L )
+       LISTNAME="$OPTARG"
+       ;;
+       a )
+       ADDALIAS="y"
+       ;;
+       b )
+       ADDCRON="y"
+       ;;
+       c )
+       DO_CHOWN="y"
+       CHOWN="$OPTARG"
+       ;;
+       d )
+       FQDN="$OPTARG"
+       ;;
+       f )
+       ANSWERFILEIN="$OPTARG"
+       ;;
        h )
        echo "$USAGE"
        exit 0
        ;;
-       L )
-       LISTNAME="$OPTARG"
+       n )
+       ANSWERFILEOUT="$OPTARG"
        ;;
        s )
        SPOOLDIR="$OPTARG"
        ;;
-       a )
-       A_CREATE="YES"
-       ;;
-       c )
-       DO_CHOWN=1
-       CHOWN="$OPTARG"
+       t )
+       TEXTLANG="$OPTARG"
        ;;
        * )
        echo "$0: invalid option"
@@ -46,87 +69,77 @@ done
 SHIFTVAL=$((OPTIND-1))
 shift $SHIFTVAL
 
-if [ -z "$SPOOLDIR" ]; then
-       SPOOLDIR="$DEFAULTDIR"
+if [ -r "$ANSWERFILEIN" ]; then
+       # shellcheck source=/dev/null
+       . "$ANSWERFILEIN"
 fi
 
-echo "Creating Directories below $SPOOLDIR. Use '-s spooldir' to change"
+if [ ! -d "$SPOOLDIR" ]; then
+       SPOOLDIRDEF=/var/spool/mlmmj
+       printf 'mlmmj spool directory under which to create new mailing list [%s] : ' "$SPOOLDIRDEF"
+       read -r SPOOLDIR
+       if [ ! -d "$SPOOLDIR" ]; then
+               SPOOLDIR="$SPOOLDIRDEF"
+       fi
+fi
 
 if [ -z "$LISTNAME" ]; then
-       printf 'What should the name of the Mailinglist be? [mlmmj-test] : '
+       LISTNAMEDEF="mlmmj-test"
+       printf 'What should the name of the Mailinglist be? [%s] : ' "$LISTNAMEDEF"
        read -r LISTNAME
        if [ -z "$LISTNAME" ]; then
-       LISTNAME="mlmmj-test"
+               LISTNAME="$LISTNAMEDEF"
        fi
 fi
-
 LISTDIR="$SPOOLDIR/$LISTNAME"
 
-mkdir -p "$LISTDIR"
-
-for DIR in incoming queue queue/discarded archive text subconf unsubconf \
-          bounce control moderation subscribers.d digesters.d requeue \
-          nomailsubs.d
-do
-       mkdir "$LISTDIR/$DIR"
-done
-
-test -f "$LISTDIR/index" || touch "$LISTDIR/index"
-
-printf 'The Domain for the List? [] : '
-read -r FQDN
 if [ -z "$FQDN" ]; then
-       FQDN="$(domainname -f)"
+       printf 'The Fully Qualified Domain Name (FQDN) for the List? [] : '
+       read -r FQDN
+       if [ -z "$FQDN" ]; then
+               FQDN="$(domainname -f)"
+       fi
 fi
 
-printf 'The emailaddress of the list owner? [postmaster] : '
-read -r OWNER
 if [ -z "$OWNER" ]; then
-       OWNER="postmaster"
+       OWNERDEF="postmaster"
+       printf 'The emailaddress of the list owner? [%s] : ' "$OWNERDEF"
+       read -r OWNER
+       if [ -z "$OWNER" ]; then
+               OWNER="$OWNERDEF"
+       fi
 fi
-echo "$OWNER" > "$LISTDIR/control/owner"
 
-TEXTLIBDIR="@textlibdir@"
+if [ -d "$TEXTLIBDIR" ]; then
+       if [ -z "$TEXTLANG" ]; then
+               cat <<EOF
 
-if [ ! -d "$TEXTLIBDIR" ]; then
-       echo "List text library directory not found! Please check compile options." >&2
-       exit 1
-fi
+You can choose between the following languages for the list texts.
 
-echo
-echo "For the list texts you can choose between the following languages or"
-echo "give a absolute path to a directory containing the texts."
-echo
-echo "Available languages:"
-ls "$TEXTLIBDIR"
-
-TEXTPATHDEF=en
-printf 'The path to texts for the list? [%s] : ' $TEXTPATHDEF
-read -r TEXTPATHIN
-if [ -z "$TEXTPATHIN" ] ; then
-       TEXTPATH="$TEXTPATHDEF"
-else
-       TEXTPATH="$TEXTPATHIN"
-fi
+Available languages:
+EOF
+               ls "$TEXTLIBDIR"
 
-case "$TEXTPATH" in
-       /*) ;;
-       *)
-               TEXTPATH="$TEXTLIBDIR/$TEXTPATH"
-esac
+               TEXTLANGDEF=en
+               printf 'mailing list texts language? [%s] : ' $TEXTLANGDEF
+               read -r TEXTLANG
+               if [ -z "$TEXTLANG" ] ; then
+                       TEXTLANG="$TEXTLANGDEF"
+               fi
+       fi
 
-if [ -d "$TEXTPATH" ]; then
-       cp "$TEXTPATH"/* "$LISTDIR/text"
+       TEXTPATH="$TEXTLIBDIR/$TEXTLANG"
 else
-       echo
-       echo "**NOTE** Could not copy the texts for the list"
-       echo "Please manually copy the files from the listtexts/ directory"
-       echo "in the source distribution of mlmmj."
-       sleep 2
+       cat <<EOF
+
+List text library directory not found! Please check compile options.
+**WARNING** Could not copy the texts for the list
+Please manually copy the files from the listtexts/ directory
+in the source distribution of mlmmj.
+EOF
 fi
 
 LISTADDRESS="$LISTNAME@$FQDN"
-echo "$LISTADDRESS" > "$LISTDIR/control/listaddress"
 
 MLMMJRECEIVE=$(which mlmmj-receive 2>/dev/null)
 if [ -z "$MLMMJRECEIVE" ]; then
@@ -138,55 +151,107 @@ if [ -z "$MLMMJMAINTD" ]; then
        MLMMJMAINTD="/path/to/mlmmj-maintd"
 fi
 
-ALIAS="$LISTNAME:  \"|$MLMMJRECEIVE -L $SPOOLDIR/$LISTNAME/\""
-CRONENTRY="0 */2 * * * \"$MLMMJMAINTD -F -L $SPOOLDIR/$LISTNAME/\""
+ALIAS="$LISTNAME:  \"|$MLMMJRECEIVE -L $LISTDIR/\""
+CRONENTRY="0 */2 * * * \"$MLMMJMAINTD -F -L $LISTDIR/\""
 
-if [ -n "$A_CREATE" ]; then
-       echo "I want to add the following to your $ALIASFILE file:"
-       echo "$ALIAS"
 
-       printf 'is this ok? [y/N] : '
-       read -r OKIDOKI
-       case $OKIDOKI in
-               y|Y)
-               echo "$ALIAS" >> $ALIASFILE
-               ;;
-               n|N)
-               exit 0
-               ;;
-               *)
-               echo "Options was: y, Y, n or N"
-       esac
-else
-       echo
-       echo "Don't forget to add this to $ALIASFILE:"
-       echo "$ALIAS"
+if [ -z "$ADDALIAS" ]; then
+       cat <<EOF
+
+Don't forget to add this line to $ALIASFILE:
+$ALIAS
+
+EOF
+       printf 'Should I add it for you? (y/n) [n] '
+       read -r ADDALIAS
+       ADDALIAS=${ADDALIAS:-n}
 fi
 
-if [ "$DO_CHOWN" ] ; then
+if [ -z "$DO_CHOWN" ]; then
+       printf 'Would you like to change the owner of the newly created mailing list directory? (y/n) [n] '
+       read -r DO_CHOWN
+       DO_CHOWN=${DO_CHOWN:-n}
+fi
+case $DO_CHOWN in
+       y|Y)
+               if [ -z "$CHOWN" ]; then
+                       printf 'New owner of %s [] : ' "$LISTDIR"
+                       read -r CHOWN
+               fi
+esac
+
+if [ -z "$ADDCRON" ]; then
+       cat <<EOF
+
+If you're not starting mlmmj-maintd in daemon mode,
+don't forget to add this line to your crontab:
+$CRONENTRY
+
+EOF
+       printf 'Should I add it to the system-wide crontab at %s for you? (y/n) [n] ' "$CRONTAB"
+       read -r ADDCRON
+       ADDCRON=${ADDCRON:-n}
+fi
+
+if [ -n "$ANSWERFILEOUT" ]; then
        echo
-       printf 'chown -R %s %s/%s? [y/n]: ' "$CHOWN" "$SPOOLDIR" "$LISTNAME"
-       read -r OKIDOKI
-       case $OKIDOKI in
+       echo "Writing all selections to $ANSWERFILEOUT. No changes made."
+       cat > "$ANSWERFILEOUT" <<EOF
+SPOOLDIR='$SPOOLDIR'
+LISTNAME='$LISTNAME'
+FQDN='$FQDN'
+OWNER='$OWNER'
+TEXTLANG='$TEXTLANG'
+ADDALIAS='$ADDALIAS'
+DO_CHOWN='$DO_CHOWN'
+CHOWN='$CHOWN'
+ADDCRON='$ADDCRON'
+EOF
+else
+       mkdir -p "$LISTDIR"
+       for DIR in incoming queue queue/discarded archive text subconf unsubconf \
+                  bounce control moderation subscribers.d digesters.d requeue \
+                  nomailsubs.d
+       do
+               mkdir -p "$LISTDIR/$DIR"
+       done
+
+       test -f "$LISTDIR/index" || touch "$LISTDIR/index"
+       echo "$OWNER" > "$LISTDIR/control/owner"
+       echo "$LISTADDRESS" > "$LISTDIR/control/listaddress"
+
+       if [ -d "$TEXTPATH" ]; then
+               cp "$TEXTPATH"/* "$LISTDIR/text"
+       else
+               echo "WARNING: List text language '$TEXTLANG' not found."
+               echo "Proceeding without copying list texts."
+       fi
+
+       case "$ADDALIAS" in
                y|Y)
-                       chown -R "$CHOWN" "$SPOOLDIR/$LISTNAME"
-               ;;
-               n|N)
-                       exit 0
-               ;;
-               *)
-                       echo "option is: y, Y, n, N"
-               ;;
+                       echo "$ALIAS" >> $ALIASFILE
        esac
+
+       case "$DO_CHOWN" in
+               y|Y)
+                       if [ -n "$CHOWN" ]; then
+                               chown -R "$CHOWN" "$LISTDIR"
+                       fi
+       esac
+
+       case "$ADDCRON" in
+               y|Y)
+                       echo "$CRONENTRY" >> $CRONTAB
+       esac
+
+       echo
+       echo "Mailing list $LISTDIR was created successfully."
 fi
 
-echo
-echo "If you're not starting mlmmj-maintd in daemon mode,"
-echo "don't forget to add this to your crontab:"
-echo "$CRONENTRY"
+cat <<EOF
 
-echo
-echo " ** FINAL NOTES **
-1) The mailinglist directory have to be owned by the user running the
+** FINAL NOTES **
+1) The mailinglist directory has to be owned by the user running the
 mailserver (i.e. starting the binaries to work the list)
-2) Run newaliases"
+2) Run newaliases
+EOF