Skip to content
Snippets Groups Projects
freedombone-addemail 4.03 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 an email address rule 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}-addemail
Bob Mottram's avatar
Bob Mottram committed
export TEXTDOMAINDIR="/usr/share/locale"

MYUSERNAME=$USER
EMAILADDRESS=
SUBJECT_TEXT=
PUBLIC='no'

function show_help {
    echo ''
    echo $"${PROJECT_NAME}-addemail -u [username] -e [email address] -s [subject text] -g [group name] --public [yes|no]"
    echo ''
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"
            ;;
        -e|--email)
            shift
            EMAILADDRESS="$1"
            ;;
        -s|--subject)
            shift
            SUBJECT_TEXT="$1"
            ;;
        -g|--group)
            shift
            GROUP_NAME="$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" && "$GROUP_NAME" ]]; then
Bob Mottram's avatar
Bob Mottram committed
if [ "${#EMAILADDRESS}" -lt 2 ]; then
    if [ ${#SUBJECT_TEXT} -lt 2 ]; then
Bob Mottram's avatar
Bob Mottram committed
        show_help
Bob Mottram's avatar
Bob Mottram committed
MUTTRC="/home/$MYUSERNAME/.muttrc"
PM="/home/$MYUSERNAME/.procmailrc"
LISTDIR="/home/$MYUSERNAME/Maildir/$GROUP_NAME"
proc_rule="  * ^From:.*$EMAILADDRESS"
proc_comment="# Email rule for $EMAILADDRESS -> $GROUP_NAME"
if [ ${#SUBJECT_TEXT} -gt 0 ]; then
    proc_rule="  * ^From:.*$EMAILADDRESS"
    proc_comment="# Email rule for '$SUBJECT_TEXT' -> $GROUP_NAME"
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 ! grep -q "$proc_comment" "$PM"; then
    if [[ $PUBLIC != "yes" ]]; then
Bob Mottram's avatar
Bob Mottram committed
        # private emails go after the encryption stage
        { echo '';
          echo "$proc_comment";
          echo ":0";
          echo "$proc_rule";
          echo "$LISTDIR/new";
          echo "# End of rule"; } >> "$PM"
Bob Mottram's avatar
Bob Mottram committed
    else
Bob Mottram's avatar
Bob Mottram committed
        # public emails are copied before the encryption stage
        if ! grep -q '# encrypt' "$PM"; then
            { echo '';
              echo "$proc_comment";
              echo ":0";
              echo "$proc_rule";
              echo "$LISTDIR/new";
              echo "# End of rule"; } >> "$PM"
        else
            sed -i "/# encrypt/i $proc_comment\\n:0\\n${proc_rule}\\n$LISTDIR/new\\n# End of rule\\n" "$PM"
        fi
Bob Mottram's avatar
Bob Mottram committed
    chown "$MYUSERNAME":"$MYUSERNAME" "$PM"
if [ ! -f "$MUTTRC" ]; then
Bob Mottram's avatar
Bob Mottram committed
    cp /etc/Muttrc "$MUTTRC"
    chown "$MYUSERNAME":"$MYUSERNAME" "$MUTTRC"
PROCMAILLOG=/home/$MYUSERNAME/log
Bob Mottram's avatar
Bob Mottram committed
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" != *$GROUP_NAME* ]]; then
    if ! grep -q "=$GROUP_NAME" "$MUTTRC"; then
        sed -i "s|$MUTT_MAILBOXES|$MUTT_MAILBOXES =$GROUP_NAME|g" "$MUTTRC"
        chown "$MYUSERNAME":"$MYUSERNAME" "$MUTTRC"
Bob Mottram's avatar
Bob Mottram committed
    fi