source: bearmail/bin/bearmail-sieve_responder @ 329

Last change on this file since 329 was 329, checked in by ben, 11 years ago

adding sieve responder/forward/userconf scripts

  • Property svn:executable set to *
File size: 7.7 KB
Line 
1#!/bin/bash
2
3
4# Copyright (C) 2009 Bearstech - http://bearstech.com/
5#
6# This program is free software: you can redistribute it and/or modify
7# it under the terms of the GNU General Public License as published by
8# the Free Software Foundation, either version 3 of the License, or
9# (at your option) any later version.
10#
11# This program is distributed in the hope that it will be useful,
12# but WITHOUT ANY WARRANTY; without even the implied warranty of
13# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14# GNU General Public License for more details.
15#
16# You should have received a copy of the GNU General Public License
17# along with this program.  If not, see <http://www.gnu.org/licenses/>.
18
19# bearmail-sieve_responder - part of bearmail
20#
21# This is a simple script for creating responders with sieve on a bearmail
22# configuration.
23
24set -e
25
26NAME=${0##*/}
27SIEVE_DIR="/etc/bearmail/dovecot/bearmail-sieve"
28BMMAILMAP="/etc/bearmail/mailmap"
29BMSUC="/usr/sbin/bearmail-sieve_userconf"
30CRON="/etc/cron.d/bearmail_sieve"
31
32usage() {
33cat << EOF
34Usage: $NAME [OPTION]
35       --help              print this help and exit
36 -u,   --user              mail account
37 -a,   --add               add a new responder
38 -b,   --beginning-date     date of the start of responder (YYYYMMDD or now)
39 -e,   --end-date          date of the end (YYYYMMDD)
40 -d,   --del               del responder
41 -l,   --list              list content of the responder
42
43
44Example:
45  $NAME -u me@domain.tld -a -b now -e 20100823
46  $NAME -u me@domain.tld -d
47 
48EOF
49}
50
51done=0
52while test "$done" = "0"; do
53        case "$1" in
54                 -h | -help | --h | --help)
55                    usage
56                    exit 0
57                 ;;
58                 -u | --user)
59                    user_mail="$2"
60                    shift ; shift ;;
61                 -a | --add)
62                    add_responder="yes"
63                    shift ;;
64                 -b | --beginning-date)
65                    beginning_date="$2"
66                    shift ; shift ;;
67                 -e | --end-date)
68                    end_date="$2"
69                    shift ; shift ;;
70                 -d | --del)
71                    del_responder="yes"
72                    shift ;;
73                 -l | --list)
74                    list_content="yes"
75                    shift ;;
76                 --*)
77                    echo "unknown option: $1, try '$NAME -h' for help"
78                    exit 1 ;;
79                 -*)
80                    echo "unknown option: $1, try '$NAME -h' for help"
81                    exit 1 ;;
82                 *)
83                    done=1 ;;
84        esac
85done
86
87# basic validation
88if [ $# != 0 ]; then
89  usage
90  exit 1
91fi
92if [ -z "$user_mail" ] ; then
93  usage
94  exit 1
95fi
96if [ -z "$add_responder" ] && [ -z "$del_responder" ] &&  [ -z "$list_content" ]
97   then
98  usage
99  exit 1
100fi
101if [ -n "$list_content" ] ; then
102  if [ -n "$add_responder" ] ||  [ -n "$del_responder" ] || \
103     [ -n "$beginning_date" ] || [ -n "$end_date" ] ; then
104    usage
105    exit 1
106  fi
107fi
108if [ -n "$add_responder" ] ; then
109  if [ -z "$beginning_date" ] || [ -z "$end_date" ] ; then
110    usage
111    exit 1
112  fi
113fi
114if [ -n "$del_responder" ]; then
115  if [ -n "$beginning_date" ] || [ -n "$end_date" ] ; then
116    usage
117    exit 1
118  fi
119fi
120   
121# check if user is a real account
122if [ ! "$(grep "^$user_mail:" $BMMAILMAP | grep ":local$")" ] ; then
123  echo "Error: $user_mail is not a main account."
124  exit 1
125fi
126
127# define sieve rules file
128USER_DOMAIN=`echo $user_mail | sed -e "s/.*@//"`
129USER_NAME=`echo $user_mail | sed -e "s/@.*//"`
130SIEVE_FILE="$SIEVE_DIR/$USER_DOMAIN.$USER_NAME"
131MY_BMSF="$BMDIR/$USER_DOMAIN/$USER_NAME/.dovecot.sieve"
132
133# list fonction
134if [ -n "$list_content" ] ; then
135  if [ "$($BMSUC -u $user_mail -s | grep "is active")" ] ; then
136    if [ ! "$(grep '# vacation configuration$' $SIEVE_FILE)" ] ; then
137      echo "$user_mail : no responder found for $user_mail"
138      exit
139    else
140      echo "$user_mail : responder auto-message:"
141      sed -n '/^# vacation configuration/,/";/p' $SIEVE_FILE | \
142      sed "s/#_bmcr_//" | sed -n '/^"$/,/^i";$/p' | sed '/^"$/d;/^";$/d'
143      if [ "$(grep "$BMDIR/$USER_DOMAIN/$USER_NAME/.dovecot.sieve" $CRON)" ] ; then
144        MY_BEGIN=`date -d $(date +"%Y")$(grep "$BMDIR/$USER_DOMAIN/$USER_NAME/" $CRON  | awk '{print $4 $3}') +"%d %B"`
145        echo -e "\n- Responder starts on $MY_BEGIN"
146      else
147        echo -e "\n- Responder is active and ends on $MY_END"
148      fi
149      if [ "$(grep " $user_mail " $CRON)" ] ; then
150        MY_END=`date -d $(date +"%Y")$(grep " $user_mail " $CRON  | awk '{print $4 $3}') +"%d %B"`
151        echo -e "\n- Responder ends on $MY_END"
152        exit
153      fi
154    fi
155  else
156    $BMSUC -u $user_mail -s
157  fi
158fi
159
160# delete fonction
161if [ -n "$del_responder" ]; then
162  # check sieve conf
163  if [ ! "$($BMSUC -u $user_mail -s | grep "is active")" ] ; then
164    $BMSUC -u $user_mail -s
165    exit 1
166  fi
167  # remove vacation require if needed
168  if [ "$(grep 'require "vacation";$' $SIEVE_FILE)" ] || \
169     [ "$(grep '# vacation configuration$' $SIEVE_FILE)" ] || \
170     [ "$(grep "$user_mail" $CRON)" ] || [ "$(grep "$MY_BMSF" $CRON)" ]; then
171    echo "$user_mail : deleting responder."
172    sed -e '/require "vacation";$/d' -i $SIEVE_FILE
173    sed "/# vacation configuration$/,\
174/\";/d" -i $SIEVE_FILE
175    sed "/$user_mail/d" -i $CRON
176    sed "/$USER_DOMAIN.*$USER_NAME.*dovecot.sieve/d" -i $CRON
177  else
178    echo "$user_mail : no responder found."
179  fi
180fi
181
182# adding fonction
183if [ -n "$add_responder" ] ; then
184  # check date validity
185  if [ -z "$(date -d $end_date)" ] || \
186     [ "$(date -d $end_date +"%s")" -le "$(date +"%s")" ] ; then
187    echo "Error: invalid end date"
188    exit 1
189  fi
190  if [ "$beginning_date" != "now" ] ; then
191    if [ -z "$(date -d $beginning_date)" ] ; then
192      echo "Error: invalid beginning date"
193      exit 1
194    fi
195    if [ "$beginning_date" -ge "$end_date" ] ; then
196      echo "Error: end date is before beginning date."
197      exit 1
198    fi
199    if [ "$(date -d $beginning_date +"%s")" -le "$(date +"%s")" ] ; then
200      beginning_date="now"
201    fi
202  fi
203  # check sieve conf
204  if [ "$($BMSUC -u $user_mail -s | grep "is not active")" ] ; then
205    $BMSUC -u $user_mail -a
206  fi
207  if [ ! "$($BMSUC -u $user_mail -s | grep "is active")" ] ; then
208    $BMSUC -u $user_mail -s
209    exit 1
210  fi
211  if [ "$(grep '^# vacation configuration$' $SIEVE_FILE)" ] ; then
212    echo "Error: $user_mail have already responder rules"
213    echo "you should remove it before creating another one."
214    exit 1
215  fi
216  # define user's aliases
217  USER_ALIASES=`grep "^::$user_mail$" $BMMAILMAP | sed -e "s/::$user_mail//"`
218  if [ -z "$USER_ALIASES" ] ; then
219    MY_SIEVE_ALIAS=""
220  else
221    MY_SIEVE_ALIAS=`echo -e ", \"$USER_ALIASES" | \
222                    sed -e :a -e '/$/N; s/\n/", "/; ta' | sed -e "s/$/\"/"`
223  fi
224  # ask for responder content
225  echo "Define your vacation message and end it with EOF"
226  while read input ; do
227    my_vacation="$my_vacation\n$input"
228    if [ "$input" = 'EOF' ] ; then
229      break
230    fi
231  done
232  # write vacation configuration
233  sed "2i#_bmcr_require \"vacation\";" -i $SIEVE_FILE
234  cat >> $SIEVE_FILE << EOF
235# vacation configuration
236#_bmcr_vacation
237#_bmcr_:days 15
238#_bmcr_:addresses ["$user_mail"$MY_SIEVE_ALIAS]
239#_bmcr_"
240$(echo -e "$my_vacation" | grep -v "^EOF" | sed -e "s/^/#_bmcr_/")
241";
242EOF
243  # Create cronjob
244  if [ "$beginning_date" = "now" ] ; then
245    sed -e "s/^#_bmcr_//" -i $SIEVE_FILE
246  else
247    # create the cronjob
248    MY_BDAY=`date -d $beginning_date +"%F" | awk -F "-" '{print $3}'`
249    MY_BMONTH=`date -d $beginning_date +"%F" | awk -F "-" '{print $2}'`
250
251    echo "1 0 $MY_BDAY $MY_BMONTH * root sed -e 's/^#_bmcr_//' $BMDIR/$USER_DOMAIN/$USER_NAME/.dovecot.sieve" >> $CRON
252  fi
253  # create end cronjob
254  MY_EDAY=`date -d $end_date +"%F" | awk -F "-" '{print $3}'`
255  MY_EMONTH=`date -d $end_date +"%F" | awk -F "-" '{print $2}'`
256  echo "1 0 $MY_EDAY $MY_EMONTH * root $NAME -u $user_mail -d" >> $CRON
257fi
258
Note: See TracBrowser for help on using the repository browser.