source: bearmail/bin/bearmail-sieve_userconf @ 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: 5.1 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_userconf - part of bearmail
19#
20# This is a simple script for creating basic user configuration with sieve
21# on a bearmail configuration.
22
23NAME=${0##*/}
24SIEVE_DIR="/etc/bearmail/dovecot/bearmail-sieve"
25BMMAILMAP="/etc/bearmail/mailmap"
26BMDIR="/var/spool/bearmail"
27CRON="/etc/cron.d/bearmail-sieve"
28BMUSER="bearmail"
29
30usage() {
31cat << EOF
32Usage: $NAME [OPTION]
33        --help             print this help and exit
34 -u,    --user             mail account
35 -a,    --add              add sieve rules
36 -d,    --delete           delete sieve rules
37 -s,    --status           status of conf files
38
39Example:
40  $NAME -u me@domain.tld -a
41  $NAME -u me@domain.tld -d
42  $NAME -u me@domain.tld -s
43EOF
44}
45
46done=0
47while test "$done" = "0"; do
48        case "$1" in
49                 -h | -help | --h | --help)
50                    usage
51                    exit 0 ;;
52                 -u | --user)
53                    user_mail="$2"
54                    shift ; shift ;;
55                 -a | --add)
56                    add_rules="yes"
57                    shift ;;
58                 -d | --del_rules)
59                    del_rules="yes"
60                    shift ;;
61                 -s | --status)
62                    print_status="yes"
63                    shift ;;
64                 --*)
65                    echo "unknown option: $1, try '$NAME -h' for help"
66                    exit 1 ;;
67                 -*)
68                    echo "unknown option: $1, try '$NAME -h' for help"
69                    exit 1 ;;
70                 *)
71                    done=1 ;;
72        esac
73done
74
75if [ $# != 0 ]; then
76  usage
77  exit 1
78fi
79
80# check if options are correctly set
81if [ -z "$user_mail" ] ; then
82  usage
83  exit 1
84fi
85if [ -n "$print_status" ] ; then
86  if [ -n "$add_rules" ] || [ -n "$del_rules" ] ; then
87    usage
88    exit 1
89  fi
90else
91  if [ -n "$add_rules" ] && [ -n "$del_rules" ] ; then
92    usage
93    exit 1
94  fi
95fi 
96
97# check if user is a real account
98if [ ! "$(grep "^$user_mail:" $BMMAILMAP | grep ":local$")" ] ; then
99  echo "Error: $user_mail is not a main account."
100  exit 1
101fi
102
103# define sieve files
104USER_DOMAIN=`echo $user_mail | sed -e "s/.*@//"`
105USER_NAME=`echo $user_mail | sed -e "s/@.*//"`
106SIEVE_FILE="$SIEVE_DIR/$USER_DOMAIN.$USER_NAME"
107MY_BMSF="$BMDIR/$USER_DOMAIN/$USER_NAME/.dovecot.sieve"
108
109# status function
110status_function() {
111  if [ "$(ls $SIEVE_FILE 2> /dev/null)" ] ; then
112    SIEVEF="1" ; else SIEVEF="0"
113  fi
114  if [ "$(ls $MY_BMSF 2> /dev/null)" ] ; then
115    SIEVEL="1" ; else SIEVEL="0"
116  fi
117
118  if [ "$SIEVEF" -eq "1" ] ; then
119    if [ "$SIEVEL" -eq "0" ] ; then
120      echo "Error: $SIEVE_FILE exists but $MY_BMSF does not"
121      echo "You should remove this file and create rules with bearmail scripts."
122      exit 1
123    fi
124    if [ ! "$(grep "^## bearmail sieve configuration$" $SIEVE_FILE)" ] ; then
125      echo "Error: $SIEVE_FILE is not configured by bearmail."
126      echo "You should remove this file and create rules with bearmail scripts."
127      exit 1
128    fi
129  fi
130
131  if [ "$SIEVEL" -eq "1" ] ; then
132    if [ "$SIEVEF" -eq "0" ] ; then
133      echo "Error: $MY_BMSF exists but $SIEVE_FILE does not"
134      echo "You should remove this file and create rules with bearmail scripts."
135      exit 1
136    fi   
137    if [ ! -L "$MY_BMSF" ] ; then
138      echo "Error: $MY_BMSF is not configured by bearmail."
139      echo "You should remove $SIEVE_FILE and $MY_BMSF then create rules with \
140bearmail scripts."
141      exit 1
142    fi
143  fi
144
145  if [ "$SIEVEF" -eq "1" ] && [ "$SIEVEL" -eq "1" ] ; then
146      echo "Bearmail sieve configuration is active for $user_mail"
147  else
148      echo "Bearmail sieve configuration is not active for $user_mail"
149  fi
150}
151
152# add
153if [ "$add_rules" = "yes" ] ; then
154  if [ "$(status_function | grep "is not active")" ] ; then
155    echo "creating $SIEVE_FILE"
156    echo -e "## bearmail sieve configuration\n" > $SIEVE_FILE
157    mkdir -p $BMDIR/$USER_DOMAIN/$USER_NAME
158    chown $BMUSER: $BMDIR/$USER_DOMAIN/$USER_NAME
159    echo "linking $SIEVE_FILE to $MY_BMSF"
160    ln -s $SIEVE_FILE $MY_BMSF
161  else
162    status_function
163    exit 1
164  fi
165fi
166
167# del
168if [ "$del_rules" = "yes" ] ; then
169   if [ "$(ls $SIEVE_FILE 2> /dev/null)" ] ; then
170    echo "removing $SIEVE_FILE"
171    rm $SIEVE_FILE
172  fi
173  if [ "$(ls $MY_BMSF 2> /dev/null)" ] ; then
174    echo "removing $MY_BMSF"
175    rm $MY_BMSF
176  fi
177  if [ -f "$CRON" ] ; then
178    sed "/$user_mail/d" -i $CRON
179    sed "/$USER_DOMAIN.*$USER_NAME.*dovecot.sieve/d" -i $CRON
180  fi
181fi
182
183# status
184if [ "$print_status" ] ; then
185  status_function
186fi
Note: See TracBrowser for help on using the repository browser.