Skip to content
Snippets Groups Projects
freedombone-addlist 4.53 KiB
Newer Older
#!/bin/bash
Bob Mottram's avatar
Bob Mottram committed
#  _____               _           _
# |   __|___ ___ ___ _| |___ _____| |_ ___ ___ ___
# |   __|  _| -_| -_| . | . |     | . | . |   | -_|
# |__|  |_| |___|___|___|___|_|_|_|___|___|_|_|___|
Bob Mottram's avatar
Bob Mottram committed
#
Bob Mottram's avatar
Bob Mottram committed
#                              Freedom in the Cloud

# Adds a mailing list to the email configuration

# License
# =======
#
Bob Mottram's avatar
Bob Mottram committed
# Copyright (C) 2015-2019 Bob Mottram <bob@freedombone.net>
#
# This program is free software: you can redistribute it and/or modify
Bob Mottram's avatar
Bob Mottram committed
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 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
Bob Mottram's avatar
Bob Mottram committed
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
Bob Mottram's avatar
Bob Mottram committed
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
Bob Mottram's avatar
Bob Mottram committed
PROJECT_NAME='freedombone'

Bob Mottram's avatar
Bob Mottram committed
export TEXTDOMAIN=${PROJECT_NAME}-addlist
Bob Mottram's avatar
Bob Mottram committed
export TEXTDOMAINDIR="/usr/share/locale"

MYUSERNAME=$USER
MAILINGLIST=
SUBJECTTAG=
LIST_ADDRESS=
PUBLIC='yes'

function show_help {
    echo ''
    echo $"${PROJECT_NAME}-addlist -u [username] -l [mailing list name] -s [subject tag] -e [list email address] --public [yes|no]"
Bob Mottram's avatar
Bob Mottram committed
    exit 0
Bob Mottram's avatar
Bob Mottram committed
while [ $# -gt 1 ]
Bob Mottram's avatar
Bob Mottram committed
    key="$1"

    case $key in
Bob Mottram's avatar
Bob Mottram committed
        -h|--help)
            show_help
            ;;
        -u|--user)
            shift
            MYUSERNAME="$1"
            ;;
        -l|--list)
            shift
            MAILINGLIST="$1"
            ;;
        -s|--subject)
            shift
            SUBJECTTAG="$1"
            ;;
        -e|--email)
            shift
            LIST_ADDRESS="$1"
            ;;
        -p|--public)
            shift
            PUBLIC="$1"
            ;;
        *)
            # unknown option
            ;;
Bob Mottram's avatar
Bob Mottram committed
    esac
Bob Mottram's avatar
Bob Mottram committed
if [ ! "$MYUSERNAME" ]; then
Bob Mottram's avatar
Bob Mottram committed
if [[ ! "$MAILINGLIST" && ! "$SUBJECTTAG" ]]; then
Bob Mottram's avatar
Bob Mottram committed
MUTTRC="/home/$MYUSERNAME/.muttrc"
PM="/home/$MYUSERNAME/.procmailrc"
LISTDIR="/home/$MYUSERNAME/Maildir/$MAILINGLIST"
Bob Mottram's avatar
Bob Mottram committed
if grep -q "=$MAILINGLIST" "$MUTTRC"; then
    echo $"Mailing list $MAILINGLIST was already added"
fi

if [ ! -d "$LISTDIR" ]; then
Bob Mottram's avatar
Bob Mottram committed
    mkdir -m 700 "$LISTDIR"
    mkdir -m 700 "$LISTDIR/tmp"
    mkdir -m 700 "$LISTDIR/new"
    mkdir -m 700 "$LISTDIR/cur"
Bob Mottram's avatar
Bob Mottram committed
chown -R "$MYUSERNAME":"$MYUSERNAME" "$LISTDIR"
if [ ${#SUBJECTTAG} -gt 0 ]; then
    # use the subject tag
Bob Mottram's avatar
Bob Mottram committed
    if ! grep -q "Subject:.*()\\[$SUBJECTTAG\\]" "$PM"; then
        if [[ $PUBLIC != "yes" ]]; then
            # private emails go after the encryption stage
            { echo "# Email rule for $MAILINGLIST subject [$SUBJECTTAG]";
              echo ":0";
              echo "  * ^Subject:.*()\\[$SUBJECTTAG\\]";
              echo "$LISTDIR/new";
              echo "# End of rule";
              echo ""; } >> "$PM"
Bob Mottram's avatar
Bob Mottram committed
        else
Bob Mottram's avatar
Bob Mottram committed
            # public emails are copied before hte encryption stage
            if ! grep -q '# encrypt' "$PM"; then
                { echo "# Email rule for $MAILINGLIST subject [$SUBJECTTAG]";
                  echo ":0";
                  echo "  * ^Subject:.*()\\[$SUBJECTTAG\\]";
                  echo "$LISTDIR/new";
                  echo "# End of rule";
                  echo ""; } >> "$PM"
            else
                sed -i "/# encrypt/i # Email rule for $MAILINGLIST subject [$SUBJECTTAG]\\n:0\\n  * ^Subject:.*()\\\\[$SUBJECTTAG\\\\]\\n$LISTDIR/new\\n# End of rule\\n" "$PM"
            fi
Bob Mottram's avatar
Bob Mottram committed
        fi
Bob Mottram's avatar
Bob Mottram committed
        chown "$MYUSERNAME":"$MYUSERNAME" "$PM"
Bob Mottram's avatar
Bob Mottram committed
    exit "$("${PROJECT_NAME}-addemail" -u "$MYUSERNAME" -e "$LIST_ADDRESS" -g "$MAILINGLIST" --public "$PUBLIC")"

if [ ! -f "$MUTTRC" ]; then
Bob Mottram's avatar
Bob Mottram committed
    cp /etc/Muttrc "$MUTTRC"
    chown "$MYUSERNAME":"$MYUSERNAME" "$MUTTRC"
Bob Mottram's avatar
Bob Mottram committed
PROCMAILLOG="/home/$MYUSERNAME/log"
if [ ! -d "$PROCMAILLOG" ]; then
    mkdir "$PROCMAILLOG"
    chown -R "$MYUSERNAME":"$MYUSERNAME" "$PROCMAILLOG"
Bob Mottram's avatar
Bob Mottram committed
MUTT_MAILBOXES=$(grep "mailboxes =" "$MUTTRC")
if [[ "$MUTT_MAILBOXES" != *$MAILINGLIST* ]]; then
    if ! grep -q "=$MAILINGLIST" "$MUTTRC"; then
        sed -i "s|$MUTT_MAILBOXES|$MUTT_MAILBOXES =$MAILINGLIST|g" "$MUTTRC"
        chown "$MYUSERNAME":"$MYUSERNAME" "$MUTTRC"
Bob Mottram's avatar
Bob Mottram committed
if [ "$LIST_ADDRESS" ]; then
    sed -i "s|unsubscribe $LIST_ADDRESS|subscribe $LIST_ADDRESS|g" "$MUTTRC"
    if ! grep -q "subscribe $LIST_ADDRESS" "$MUTTRC"; then
        echo "subscribe $LIST_ADDRESS" >> "$MUTTRC"