source: bearmail/bin/bearmail-sieve_forwarder @ 477

Last change on this file since 477 was 477, checked in by zerodeux, 11 years ago

Release 0.3.1: gave up bearmail- prefix on config files (which are already in /etc/bearmail/)

  • Property svn:executable set to *
File size: 5.4 KB
Line 
1#!/bin/bash
2
3# Copyright (C) 2009 Bearstech - http://bearstech.com/
4#
5# This program is free software: you can redistribute it and/or modify
6# it under the terms of the GNU General Public License as published by
7# the Free Software Foundation, either version 3 of the License, or
8# (at your option) any later version.
9#
10# This program is distributed in the hope that it will be useful,
11# but WITHOUT ANY WARRANTY; without even the implied warranty of
12# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13# GNU General Public License for more details.
14#
15# You should have received a copy of the GNU General Public License
16# along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
18# bearmail-sieve_forwarder - part of bearmail
19#
20# This is a simple script for creating forwards with sieve on a bearmail
21# configuration.
22
23set -e
24
25NAME=${0##*/}
26SIEVE_DIR="/etc/bearmail/dovecot/sieve"
27BMMAILMAP="/etc/bearmail/mailmap"
28BMSUC="/usr/sbin/bearmail-sieve_userconf"
29
30function usage() {
31cat << EOF
32Usage: $NAME [OPTION]
33 -h,  --help             print this help and exit
34 -u,  --user             mail account
35 -a,  --add-forward      forward to addresses
36 -r,  --remove-original  don't keep original mail into INBOX
37 -d,  --del-forward      remove forward
38 -l,  --list             list forwards
39
40
41Example:
42  $NAME -u me@domain.tld -a me@another.tld,you@domain.tld -r
43  $NAME -u me@domain.tld -d all
44  $NAME -u me@domain.tld -l
45EOF
46}
47
48done=0
49while test "$done" = "0"; do
50        case "$1" in
51                 -h | -help | --h | --help)
52                    usage
53                    exit 0 ;;
54                 -u | --user)
55                    user_mail="$2"
56                    shift ; shift ;;
57                 -a | --add-forward)
58                    add_forward="$2"
59                    shift ; shift ;;
60                 -r | --remove-original)
61                    remove_message="yes"
62                    shift ;;
63                 -d | --del-forward)
64                    del_forward="$2"
65                    shift ; shift ;;
66                 -l | --list)
67                    list_forward="yes"
68                    shift ;;
69                 --*)
70                    echo "unknown option: $1, try '$NAME -h' for help"
71                    exit 1 ;;
72                 -*)
73                    echo "unknown option: $1, try '$NAME -h' for help"
74                    exit 1 ;;
75                 *)
76                    done=1 ;;
77        esac
78done
79
80if [ $# != 0 ]; then
81  usage
82  exit 1
83fi
84
85# check if options are correctly set
86if [ -z "$user_mail" ] ; then
87  usage exit 1
88fi
89if [ -n "$add_forward" ] ; then
90  if [ -n "$del_forward" ] || [ -n "$list_forward" ] ; then
91    usage
92    exit 1
93  fi
94fi
95if [ -n "$del_forward" ] && [ -n "$remove_message" ] ; then
96  usage
97  exit 1
98fi
99if [ -n "$list_forward" ] ; then
100  if [ -n "$add_forward" ] || [ -n "$del_forward" ] || \
101     [ -n "$remove_message" ]  ; then
102    usage
103    exit 1
104  fi
105fi
106
107# check if user is a real account
108if [ ! "$(grep "^$user_mail:" $BMMAILMAP | grep ":local$")" ] ; then
109  echo "Error: $user_mail is not a main account."
110  exit 1
111fi
112
113# sieve param
114USER_DOMAIN=`echo $user_mail | sed -e "s/.*@//"`
115USER_NAME=`echo $user_mail | sed -e "s/@.*//"`
116SIEVE_FILE="$SIEVE_DIR/$USER_DOMAIN.$USER_NAME"
117
118# if add option
119if [ -n "$add_forward" ] ; then
120  MY_FORWARD=`echo "$add_forward" | sed "s/,/ /g"`
121  # check sieve conf
122  if [ "$($BMSUC -u $user_mail -s | grep "is not active")" ] ; then
123    $BMSUC -u $user_mail -a
124  fi
125  if [ ! "$($BMSUC -u $user_mail -s | grep "is active")" ] ; then
126    $BMSUC -u $user_mail -s
127    exit 1
128  fi
129  # keep option
130  if [ -z "$(grep "^keep;$" $SIEVE_FILE)" ] ; then
131    if [ ! "$remove_message" ] ; then
132      echo "warning: adding keep option"
133      echo "keep;" >> $SIEVE_FILE
134    fi
135  else
136    if [ "$remove_message" ] ; then
137      echo "warning: removing keep option"
138      sed '/^keep;$/d' -i $SIEVE_FILE
139    fi
140  fi
141  # if there is a require
142  for forwarder in $MY_FORWARD; do
143    if [ "$(grep "redirect \"$forwarder\";" $SIEVE_FILE)" ] ; then
144      echo "$forwarder is already in sieve configuration"
145    else
146      echo "adding $forwarder into sieve rules"
147      echo "redirect \"$forwarder\";" >> $SIEVE_FILE
148    fi
149  done
150fi
151
152# if list
153if [ -n "$list_forward" ] ; then
154  # check sieve conf
155  if [ "$($BMSUC -u $user_mail -s | grep "is active")" ] ; then
156    MY_REDIRECT=`grep "^redirect.*;" $SIEVE_FILE | sed "s/redirect \"//;s/\";//"`
157    if [ -n "$MY_REDIRECT" ] ; then
158      echo "Forwards for $user_mail :"
159      for redirection in $MY_REDIRECT ; do
160        echo -e "\t- $redirection"
161      done
162    fi
163  else
164    $BMSUC -u $user_mail -s
165    exit 1
166  fi
167 exit
168fi
169
170# if delete
171if [ -n "$del_forward" ] ; then
172  DEL_FORWARD=`echo "$del_forward" | sed "s/,/ /g"`
173  # check sieve conf
174  if [ ! "$($BMSUC -u $user_mail -s | grep "is active")" ] ; then
175    $BMSUC -u $user_mail -s
176    exit 1
177  fi
178  # remove forward
179  for forwarder in $DEL_FORWARD; do
180    if [ "$forwarder" = "all" ] ; then
181      echo "Deleting all forwards for $user_mail"
182      sed "/^redirect .*\";/d" -i $SIEVE_FILE
183      sed '/^keep;/d' -i $SIEVE_FILE
184      exit 0
185    fi
186    if [ "$(grep "redirect \"$forwarder\";" $SIEVE_FILE)" ] ; then
187      echo "deleting $forwarder from sieve rules"
188      sed "/^redirect \"$forwarder\";$/d" -i $SIEVE_FILE
189      if [ ! "$(grep "^redirect .*\";" $SIEVE_FILE)" ] ; then
190        sed '/^keep;/d' -i $SIEVE_FILE
191      fi
192    else
193      echo "$forwarder is not in the sieve rules"
194      exit 1
195    fi
196  done
197fi
Note: See TracBrowser for help on using the repository browser.