Linux Training

Linux training for private, public & voluntary sector.

0793 572 8612

City LinUX sample scripts - arcmail


#!/bin/bash
# $Id: arcmail,v 1.58 2015/06/18 18:45:15 fulford Exp $
# $Source: /src/merlin/usr/local/etc/RCS/arcmail,v $
# $Revision: 1.58 $
# Author C W Fulford.
# Copyright 2015 (c) C W Fulford.
# Licensed for public use under the LGPL, .
# For assistance contact fulford@fulford.net 0709 229 5385
########################################################################
cmd=`basename $0`
chkmail=`dirname $0`/chkmail
ver=`echo "$Id: arcmail,v 1.58 2015/06/18 18:45:15 fulford Exp $"|awk '{print $3,$4,$5}'`
syntax="$cmd [[-c <configfile>] [-d] [-i <company_id>] [-l] [-m <max>] [-v] <email_address>]|[-V]"
while [ $# -gt 0 ] ;do
	case $1 in 
		-c) if [ -f "$2" ];then
		       	config=$2;shift 2
	            else
			    echo "$cmd: can't find $2">&2;exit 1
		    fi;;
		-d) set -x;debug=0;shift;;
		-i) co=$2;shift 2;;
		-l) log=:; shift;;
		-m) cbrianax=$2;shift 2;;
		-v) verbose=:;shift;;
		-V) echo "$cmd $Revision: 1.58 $ $Date: 2015/06/18 18:45:15 $"|awk '{print $1,$3,$6}';exit;;  
	     [^-]*@*) ma=$1;shift;;
	         *)echo "$syntax">&2; exit 1;;
	esac
done

[ "$ma" ] ||{ echo "$syntax" >&2;exit 1;}
expr "$ma" : ".*@[^\.]*\.[^\.]*">/dev/null ||{
	echo "$ma is not a valid e-mail address" >&2
	exit 1
}
md=`echo $ma|awk -F@ '{print $2}'`
getent hosts $md 2>&1 >/dev/null ||{
	echo "$cmd can't find IP address for $md">&2 
	exit 
}  
mu=`echo $ma|awk -F@ '{print $1}'`

# Look for a configuration file
for f in $config /usr/local/etc/arcmail.cf /usr/local/etc/chkmail.cf ;do
	[ -r $f ] && { config=$f; break ;}
done
# If none found exit
[ -r $config ] ||{ echo  "$cmd: Can\'t find configuration"; exit ;}

# Get configuration
eval `sed <$config -ne "/^$ma:/,/^$/{/^[\t ]*[^#].*=.*/p}"`

max=${max:-4500}
max=${cbrianax:-$max}
basedir=${basedir:-/var/qmail/mailnames}
maildir=`echo "$basedir/$md/$mu/Maildir"`
archf=$maildir/.Archive
proto=${proto:-ssh}
tmp=/tmp/$cmd$$

[ "$max" -ge 0 ] >/dev/null 2>&1 ||{
	echo "$cmd: max must be set to a positive integer" >&2
	exit 1
}
[ "$log" ] && {
	[ -n "$co" ]||{
		echo "$cmd: Warning - company id required for reporting" >&2
	}
}
[ "$verbose" ] && echo "$cmd: maximum messages set to $max."
[ "$verbose" ]&& echo "$cmd: connecting to $md."

[ "$proto" == "ssh" ] && connect=ssh 
c=`$connect  $md "sudo bash -c \"
	if [ -d $maildir/cur ];then
		cd $maildir/cur
		ls -lt
	else
		echo $maildir not found on $md >&2;exit 1 
	fi\"
"| sed  -e '/^total/d'|tee $tmp | wc -l`
[ $c -eq 0  ] && exit
[ "$c" -le $max ] && {
	echo "$cmd: $mu has $c current messages only" >&2
	exit 1
}
f=`expr $max + 1`
[ "$verbose" ] &&{
       		echo "$cmd: moving `expr $c - $max` messages for $mu."
}
years=`cat $tmp|sed -ne ''$f',$p'|awk '$8 !~ /:/ {print $8 }END{print strftime("%Y",systime())}'|uniq`

ssh $md "sudo bash -c  \"[ -d $archf ] ||{
	[ $verbose ] && echo $cmd: creating $archf 
	mkdir -m 700 $archf 
	chown -R popuser.popuser $archf ;}\"
"
for  yr in $years;do
	archive=$archf.$yr
	ssh $md "sudo bash -c \"[ -d $archive ] ||{
		[ $verbose ] && echo $cmd: creating $archive 
	       	mkdir -p -m 700 $archive/{cur,new,tmp}
		touch $archive/maildirfolder
		chmod 600 $archive/maildirfolder
		chown -R popuser.popuser $archive ;}\"
	"
done
cat $tmp|sed -ne ''$f',$p'|
awk -v cmd=$cmd: -v v=$verbose 'BEGIN{yr=strftime("%Y",systime())}{
	if($8 !~ /:/){ayr=$8} else {ayr=yr}
	printf("ssh '$md' sudo mv '$maildir'/cur/"$9 " '$archf'."ayr"/cur")|"sh";close("sh")
	mvd[ayr]++
}END{
	if(v){
		for (c in mvd){
		   print(cmd,mvd[c],"messages moved to",c,"archive.")
		}
	}
}'
etot=`ssh $md "sudo find $maildir/cur -type f" |wc -l`
mtot=`expr $c - $etot`

[ "$verbose" ]&&{
	echo "$cmd: $etot messages remain in $mu's current INBOX at $md."
}
[ "$log" ]&&{
	logger -p mail.notice -t $cmd "$mtot messages archived for $ma at $co"
	logger -p mail.notice -t $cmd "$etot messages remaining for $ma INBOX at $co"
	if [ -x $chkmail ];then
	       	$chkmail -l $ma 
	else
		echo -e "$cmd: Can\'t update log files, you may need to run \"chkmail -l <user_id>\"" >&2
	fi
}
rm $tmp
######################################################################
# This program is free software: you can redistribute it and or      #
# modify it under the terms of the Lesser GNU 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     #    
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the      #
# GNU General Public License for more details.                       #
#                                                                    # 
# A copy of the Lesser GNU General Public License and the GNU        #
# General Public License is available at                             #
# <http://www.gnu.org/licenses/>.                                    #
######################################################################

The layout and associated style sheets for this page are taken from the World Wide Web Consortium and used here under the W3C software licence.